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

Dump heap from a fork #90

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Dump heap from a fork
Dumping the heap can lock the VM for a very long time.
If you attempt to do this on a webserver process with a timeout
it's very likely you'll hit it and the process might get killed
before the dump completes.

Worse, if the process was processing some requests or jobs, they might
timeout because of it.

Using fork we can make a snapshot of the heap in a very short time
(relative to the dump itself) and savely dump without disrupting the
original process.

If you are familiar with how Redis does sanopshoting, it's very similar.
  • Loading branch information
byroot committed Feb 1, 2023
commit 3e03fd3847dc3a6d8a52704bd7c0219652e89391
28 changes: 26 additions & 2 deletions lib/rbtrace/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,19 @@ def self.run
temp.unlink
end

tracer.eval("file = File.open('#{filename}', 'w'); ObjectSpace.dump_all(output: file); file.close")
tracer.eval(<<-RUBY)
Thread.new do
Thread.current.name = '__RBTrace__'
pid = ::Process.fork do
file = File.open('#{filename}.tmp', 'w')
ObjectSpace.dump_all(output: file)
file.close
File.rename('#{filename}.tmp', '#{filename}')
exit!(0)
end
Process.waitpid(pid)
end
RUBY
puts "Heapdump being written to #{filename}"

elsif opts[:shapesdump_given]
Expand All @@ -517,7 +529,19 @@ def self.run
temp.unlink
end

tracer.eval("file = File.open('#{filename}', 'w'); ObjectSpace.dump_shapes(output: file); file.close")
tracer.eval(<<-RUBY)
Thread.new do
Thread.current.name = '__RBTrace__'
pid = ::Process.fork do
file = File.open('#{filename}.tmp', 'w')
ObjectSpace.dump_shapes(output: file)
file.close
File.rename('#{filename}.tmp', '#{filename}')
exit!(0)
end
Process.waitpid(pid)
end
RUBY
puts "Shapes dump being written to #{filename}"

elsif opts[:eval_given]
Expand Down