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'
VERSION =
"0.0.2"

Class Method Summary collapse

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


61
62
63
# File 'lib/memory_usage_profiler.rb', line 61

def self.banner
  banner_items.join("\t")
end


57
58
59
# File 'lib/memory_usage_profiler.rb', line 57

def self.banner_items
  MEMORY_PROFILE_BANNER
end

.kick(name, &callback) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/memory_usage_profiler.rb', line 65

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/memory_usage_profiler.rb', line 73

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 banner

  @@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_threadObject



98
99
100
# File 'lib/memory_usage_profiler.rb', line 98

def self.stop_thread
  @@thread_running = false
end