Module: MemoryUsageProfiler
- Defined in:
- lib/memory_usage_profiler.rb,
lib/memory_usage_profiler/version.rb
Constant Summary collapse
- MEMORY_PROFILE_GC_STAT_HASH =
{}
- MEMORY_PROFILE_BANNER =
['name']
- MEMORY_PROFILE_PROCS =
[]
- MEMORY_PROFILE_DURATION =
(ENV['RUBY_MEMORY_PROFILE_DURATION'] || 1).to_i
- MEMORY_PROFILE_OUTPUT_PATH =
ENV['RUBY_MEMORY_PROFILE'] || 'memory-profile-result'
- T_TYPES =
%i{ T_NONE T_OBJECT T_CLASS T_MODULE T_FLOAT T_STRING T_REGEXP T_ARRAY T_HASH T_STRUCT T_BIGNUM T_FILE T_DATA T_MATCH T_COMPLEX T_RATIONAL T_NIL T_TRUE T_FALSE T_SYMBOL T_FIXNUM T_UNDEF T_NODE T_ICLASS T_ZOMBIE }
- VERSION =
"0.0.4"
Class Method Summary collapse
- .add(*name, &b) ⇒ Object
- .add_proc_meminfo(file, fields) ⇒ Object
- .banner ⇒ Object
- .banner_items ⇒ Object
- .kick(name, &callback) ⇒ Object
- .start_thread(duration = MEMORY_PROFILE_DURATION, file = MEMORY_PROFILE_OUTPUT_PATH) ⇒ Object
- .stop_thread ⇒ Object
Class Method Details
.add(*name, &b) ⇒ Object
13 14 15 16 |
# File 'lib/memory_usage_profiler.rb', line 13 def self.add(*name, &b) MEMORY_PROFILE_BANNER.concat name MEMORY_PROFILE_PROCS << b end |
.add_proc_meminfo(file, fields) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/memory_usage_profiler.rb', line 33 def self.add_proc_meminfo(file, fields) return unless FileTest.exist?(file) regexp = /(#{fields.join("|")}):\s*(\d+) kB/ # check = {}; fields.each{|e| check[e] = true} add(*fields) do |result| text = File.read(file) text.scan(regexp){ # check.delete $1 result << $2 '' } # raise check.inspect unless check.empty? end end |
.banner ⇒ Object
81 82 83 |
# File 'lib/memory_usage_profiler.rb', line 81 def self. .join("\t") end |
.banner_items ⇒ Object
77 78 79 |
# File 'lib/memory_usage_profiler.rb', line 77 def self. MEMORY_PROFILE_BANNER end |
.kick(name, &callback) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/memory_usage_profiler.rb', line 85 def self.kick(name, &callback) result = [name.to_s] MEMORY_PROFILE_PROCS.each{|pr| pr.call(result) } callback.call(result) end |
.start_thread(duration = MEMORY_PROFILE_DURATION, file = MEMORY_PROFILE_OUTPUT_PATH) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/memory_usage_profiler.rb', line 93 def self.start_thread(duration=MEMORY_PROFILE_DURATION, file=MEMORY_PROFILE_OUTPUT_PATH) require 'time' file = if file == '-' STDOUT else open(file, 'w') end file.sync = true file.puts @@thread_running = true Thread.new do Thread.current.abort_on_exception = true while @@thread_running kick(Time.now.iso8601) { |result| file.puts result.join("\t") sleep duration } end end end |
.stop_thread ⇒ Object
118 119 120 |
# File 'lib/memory_usage_profiler.rb', line 118 def self.stop_thread @@thread_running = false end |