perf-tools
perf-tools copied to clipboard
Add `PerfTools::FiberTrace.pretty_log_hierarchy`
This:
Example code
spawn(name: "A1") do
sleep
end
spawn(name: "B1") do
spawn(name: "B2") do
sleep
end
sleep
end
# C1 dies
# C2 is orphaned
spawn(name: "C1") do
spawn(name: "C2") do
sleep
end
end
spawn(name: "D1") do
spawn(name: "D2") do
spawn(name: "D3") do
sleep
end
sleep
end
sleep
end
# E1 dies
# E2 and E3 are orphaned
spawn(name: "E1") do
spawn(name: "E2") do
spawn(name: "E3") do
sleep
end
sleep
end
end
# F2 dies
# F3 is orphaned
spawn(name: "F1") do
spawn(name: "F2") do
spawn(name: "F3") do
sleep
end
end
sleep
end
# G1 and G2 die
# G3 is orphaned
spawn(name: "G1") do
spawn(name: "G2") do
spawn(name: "G3") do
sleep
end
end
end
sleep 0.01
PerfTools::FiberTrace.pretty_log_hierarchy(STDOUT)
prints:
#<Fiber:0x1047c2e60: main>
#<Fiber:0x1047c2f00: Fiber Clean Loop>
#<Fiber:0x1047c2dc0: Signal Loop>
#<Fiber:0x1047c2d20: A1>
#<Fiber:0x1047c2c80: B1>
#<Fiber:0x1047c28c0: B2>
#<Fiber:0x1047c2b40: D1>
#<Fiber:0x1047c2780: D2>
#<Fiber:0x1047c2500: D3>
#<Fiber:0x1047c2a00: F1>
(orphaned)
#<Fiber:0x1047c2820: C2>
#<Fiber:0x1047c26e0: E2>
#<Fiber:0x1047c2460: E3>
#<Fiber:0x1047c23c0: F3>
#<Fiber:0x1047c2320: G3>
If -Dpreview_mt
is active, the sibling order in the hierarchy might change slightly, and also the additional worker threads will have no child fibers at all, because all the spawning at the top level still takes place in the main thread, whereas spawning inside another fiber ultimately roots the fiber to the same main thread.
On the other hand, Thread.new
starts a new tree with or without -Dpreview_mt
, and if its body spawns a fiber, the hierarchy will be reflected too:
Thread.new do
spawn(name: "H1") do
sleep
end
sleep
end
sleep 0.1
PerfTools::FiberTrace.pretty_log_hierarchy(STDOUT)
#<Fiber:0x104a06e60: main>
#<Fiber:0x104a06f00: Fiber Clean Loop>
#<Fiber:0x104a06dc0: Signal Loop>
#<Fiber:0x104a06d20: main>
#<Fiber:0x104a06c80: H1>
The line format is simply Fiber#inspect
at the moment, without any unique counter nor associated thread. This can be changed easily.
No provisions for thread safety are done yet.