Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PerfTools::FiberTrace.pretty_log_hierarchy #15

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

HertzDevil
Copy link
Collaborator

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.

@beta-ziliani
Copy link
Member

beta-ziliani commented Jan 8, 2024

If possible, I would rather let each orphaned fiber be hanging from some made-up node that would hold meta-information specific to the dead fiber. Something of the form:

#<Fiber:0x1057c2460: E1> (dead)
  #<Fiber:0x1047c26e0: E2>
    #<Fiber:0x1047c2460: E3>
#<Fiber:0x1067c2460: G1> (dead)
  #<Fiber:0x1048c23c0: G2> (dead)
    #<Fiber:0x1047c2320: G3>

I understand this means to keep information floating for eternity, so this can be opt-in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

2 participants