Skip to content

Instantly share code, notes, and snippets.

@tmm1
Last active September 25, 2015 05:27
Show Gist options
  • Save tmm1/870398 to your computer and use it in GitHub Desktop.
Save tmm1/870398 to your computer and use it in GitHub Desktop.
module CpuProf
require 'ffi'
extend FFI::Library
ffi_lib FFI::CURRENT_PROCESS
begin
attach_function :start, :ProfilerStart, [:string], :void
attach_function :stop, :ProfilerStop, [], :void
rescue FFI::NotFoundError => e
STDERR.puts "*** Are you sure you preloaded libprofiler?"
STDERR.puts "*** {DYLD_INSERT_LIBRARIES,LD_PRELOAD}=/path/to/lib/libprofiler.so ruby file.rb"
STDERR.puts e.inspect
exit!(1)
end
end
# Simple memory profiler using google-perftools' tcmalloc.
#
# Usage:
# HeapProf.start("myapp")
# 100.times{ "a"*1000 }
# HeapProf.dump("checkpoint")
# HeapProf.stop
#
# Reporting:
# pprof `which ruby` myapp.0001.heap --gif > myapp.gif
#
# Advanced (memory growth between two checkpoints):
# pprof `which ruby` --base=myapp.0001.heap myapp.0002.heap --gif > myapp.gif
#
module HeapProf
require 'ffi'
extend FFI::Library
ffi_lib FFI::CURRENT_PROCESS
begin
attach_function :start, :HeapProfilerStart, [:string], :void
attach_function :dump, :HeapProfilerDump, [:string], :void
attach_function :stop, :HeapProfilerStop, [], :void
rescue FFI::NotFoundError
STDERR.puts "*** Are you sure you preloaded libtcmalloc?"
STDERR.puts "*** {DYLD_INSERT_LIBRARIES,LD_PRELOAD}=/path/to/lib/libtcmalloc.so ruby file.rb"
exit!(1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment