Module: MemoryProfiler
- Defined in:
- lib/memory-profiler.rb
Defined Under Namespace
Modules: ObjectSpaceAnalyser
Constant Summary collapse
- DEFAULTS =
{ # general options :sort_by => :current, :only => [], :ignore => [], :limit => 20, :force_gc => false, # daemon options :delay => 60, :filename => nil, # ObjectSpaceAnalyser options :string_debug => false, :marshal_size => false, }
- @@daemon_thread =
:nodoc:
nil
- @@daemon_sync =
:nodoc:
Sync.new
- @@start_data =
:nodoc:
nil
- @@start_sync =
:nodoc:
Sync.new
Class Method Summary collapse
- ._delta(curr, prev, opt = {}) ⇒ Object
-
.format(data) ⇒ Object
Formats data, such as that returned by #start , into a printable, readable string.
-
.report(opt = {}) ⇒ Object
Generates an instantaneous report on the current Ruby ObjectSpace, saved to a text file at: /tmp/memory_profiler-<pid>-<time>.log.
-
.restart(opt = {}) ⇒ Object
Stops the current analysis, emits the results, and immediately starts a new analysis.
-
.start(opt = {}, &block) ⇒ Object
If a block is given, executes it and returns a summary.
-
.start_daemon(opt = {}) ⇒ Object
Begins an analysis thread that runs periodically, reporting to a text file at: /tmp/memory_profiler-<pid>.log.
-
.stop ⇒ Object
Stops the current analysis and emits the results.
-
.stop_daemon ⇒ Object
Terminates the analysis thread started by #start_daemon.
Class Method Details
._delta(curr, prev, opt = {}) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/memory-profiler.rb', line 225 def self._delta(curr, prev, opt={}) #:nodoc: opt = DEFAULTS.merge(opt) # determine the difference between current and previous delta = Hash.new(0) (curr.keys + prev.keys).each do |k| delta[k] = curr[k] - prev[k] end data = delta.map{|k,d| [k, curr[k].to_i, d]} # organise data according to given options case opt[:sort_by] when :none opt[:limit] = -1 when :current data = data.sort_by{|k,c,d| -( c ) } when :delta data = data.sort_by{|k,c,d| -( d ) } when :absdelta data = data.sort_by{|k,c,d| -( d.abs ) } else warn "MemoryProfiler: invalid option :sort_by => #{opt[:sort_by].inspect}; using :none" opt[:limit] = -1 end data = data[0,opt[:limit]] if opt[:limit] > 0 and opt[:limit] < data.length # return it data end |
.format(data) ⇒ Object
Formats data, such as that returned by #start , into a printable, readable string.
259 260 261 262 263 |
# File 'lib/memory-profiler.rb', line 259 def self.format(data) " Curr. Delta Class\n" + " ----- ----- -----\n" + data.map{|k,c,d| sprintf(" %5d %+5d %s\n", c, d, k.name) }.join end |
.report(opt = {}) ⇒ Object
Generates an instantaneous report on the current Ruby ObjectSpace, saved to a text file at: /tmp/memory_profiler-<pid>-<time>.log
Returns the filename used.
See: #start for valid/default options, except that :sort_by may only have the value :current or :none when using #report
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/memory-profiler.rb', line 112 def self.report(opt = {}) opt = DEFAULTS.merge(opt) GC.start if opt[:force_gc] data = ObjectSpaceAnalyser.analyse(opt) if opt[:sort_by] == :current data = data.to_a.sort_by{|k,v| -v } data = data[0,opt[:limit]] if opt[:limit] > 0 and opt[:limit] < data.length elsif opt[:sort_by] != :none warn "MemoryProfiler: invalid option :sort_by => #{opt[:sort_by].inspect}; using :none" end filename = opt[:filename] || "/tmp/memory_profiler-#{Process.pid}-#{Time.now.to_i}.log" File.open(filename, 'w') do |f| data.each {|k,c| f.printf( "%5d %s\n", c, k.name ) } end GC.start if opt[:force_gc] filename end |
.restart(opt = {}) ⇒ Object
Stops the current analysis, emits the results, and immediately starts a new analysis.
See: #stop, #start
217 218 219 220 221 |
# File 'lib/memory-profiler.rb', line 217 def self.restart(opt = {}) res = self.stop self.start(opt) res end |
.start(opt = {}, &block) ⇒ Object
If a block is given, executes it and returns a summary. Otherwise, starts the analyser, and waits for a call to #restart or #stop.
Returned data is an array of:
[ [Class, current_usage, usage_delta], ... ]
Options:
:sort_by => :current # how to order classes; :current | :delta | :absdelta | :none
:only => [] # list of only classes to scan; if empty, scans all classes
:ignore => [] # list of classes to exclude from reports (including sub-classes and modules, but not namespaces)
:limit => 20 # how many of the top classes to report (less than 1 means 'all'); only matters if :sort_by is not :none
:force_gc => true # if true, forces a garbage collection before and after generating report
:string_debug => false # see ObjectSpaceAnalyser#analyse
:marshal_size => false # see ObjectSpaceAnalyser#analyse
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/memory-profiler.rb', line 153 def self.start(opt = {}, &block) opt = DEFAULTS.merge(opt) if block_given? # get pre-block analysis of ObjectSpace GC.start if opt[:force_gc] prev = ObjectSpaceAnalyser.analyse(opt) GC.start if opt[:force_gc] yield # get post-block analysis of ObjectSpace GC.start if opt[:force_gc] curr = ObjectSpaceAnalyser.analyse(opt) # calculate the differences before and after execution data = self._delta(curr, prev, opt) # return it GC.start if opt[:force_gc] data else @@start_sync.synchronize(:EX) do raise 'already started' if @@start_data GC.start if opt[:force_gc] @@start_data = [ObjectSpaceAnalyser.analyse(opt), opt] GC.start if opt[:force_gc] end self end end |
.start_daemon(opt = {}) ⇒ Object
Begins an analysis thread that runs periodically, reporting to a text file at: /tmp/memory_profiler-<pid>.log
Returns the filename used.
Options:
:delay => 60 # number of seconds between summaries
:filename => nil # override the generated default
See: #start for other options
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/memory-profiler.rb', line 60 def self.start_daemon(opt = {}) opt = DEFAULTS.merge(opt) filename = opt[:filename] || "/tmp/memory_profiler-#{Process.pid}.log" @@daemon_sync.synchronize(:EX) do raise 'daemon process already running' if @@daemon_thread @@daemon_thread = Thread.new do prev = Hash.new(0) file = File.open(filename, 'w') loop do begin GC.start if opt[:force_gc] curr = ObjectSpaceAnalyser.analyse(opt) data = self._delta(curr, prev, opt) file.puts '-'*80 file.puts Time.now.to_s data.each {|k,c,d| file.printf( "%5d %+5d %s\n", c, d, k.name ) } prev = curr GC.start if opt[:force_gc] rescue ::Exception => err $stderr.puts "** MemoryProfiler daemon error: #{err}", err.backtrace.map{|b| "\t#{b}" } end sleep opt[:delay] end #loop end #Thread.new end filename end |
.stop ⇒ Object
Stops the current analysis and emits the results.
See: #start
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/memory-profiler.rb', line 190 def self.stop prev = nil opt = nil @@start_sync.synchronize(:EX) do raise 'not started' unless @@start_data prev, opt = @@start_data @@start_data = nil end # get the current state of affairs GC.start if opt[:force_gc] curr = ObjectSpaceAnalyser.analyse(opt) # calculate the differences before and after execution data = self._delta(curr, prev, opt) # return it GC.start if opt[:force_gc] data end |
.stop_daemon ⇒ Object
Terminates the analysis thread started by #start_daemon
93 94 95 96 97 98 99 100 101 |
# File 'lib/memory-profiler.rb', line 93 def self.stop_daemon @@daemon_sync.synchronize(:EX) do raise 'no daemon process running' unless @@daemon_thread @@daemon_thread.kill @@daemon_thread.join @@daemon_thread = nil end self end |