Class: MemoryUsage
- Inherits:
-
Object
- Object
- MemoryUsage
- Defined in:
- lib/memory_usage.rb
Overview
Class which has function for gathering memory stats. Should work on unix/linux and OS X Example usage at the bottom
Instance Method Summary collapse
-
#filter(text = "") ⇒ Object
Filter process by name when printing.
-
#gt(num = 0) ⇒ Object
Filter processes using greater than x MB.
-
#initialize(argv = [""]) ⇒ MemoryUsage
constructor
A new instance of MemoryUsage.
-
#refresh ⇒ Object
Recalculate Memory usage.
-
#report ⇒ Object
Display to stdout Error report.
-
#total ⇒ Object
Return total memory as a String.
Constructor Details
#initialize(argv = [""]) ⇒ MemoryUsage
Returns a new instance of MemoryUsage.
12 13 14 15 |
# File 'lib/memory_usage.rb', line 12 def initialize(argv=[""]) @options = MemoryUsageOptparse.parse(argv) refresh end |
Instance Method Details
#filter(text = "") ⇒ Object
Filter process by name when printing
18 19 20 |
# File 'lib/memory_usage.rb', line 18 def filter (text="") @options.filter = (text) end |
#gt(num = 0) ⇒ Object
Filter processes using greater than x MB
23 24 25 |
# File 'lib/memory_usage.rb', line 23 def gt( num=0 ) @options.min = num end |
#refresh ⇒ Object
Recalculate Memory usage. stops have to call MemoryUsage.new
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/memory_usage.rb', line 29 def refresh #Based on: http://gist.github.com/290988 @total = 0.0 @usage = Array.new @max = {:pid=>0, :rss=>0, :command=>0} `ps -u $USER -o pid,rss,command`.split("\n").each do |p| p.strip! if p.match(/^\d/) p =~ /(\d+)\s+(\d+)\s+(.+)/ pid, rss, command = [$1, $2, $3] # Some complicated logic #Allow if no options set OR #Allow if command matches filter but not with --filter (because that is this file) OR #Filter is memory_usage we will allow it to profile itself rss = rss.to_f @usage << {:pid=>pid, :rss=>(rss/1024), :command=>command } @total += rss/1024 if pid.to_s.size > @max[:pid] @max[:pid] = pid.to_s.size end if rss.to_s.size > @max[:rss] @max[:rss] = rss.to_s.size end if command.size > @max[:command] @max[:command] = command.size end #puts pid + (" %.2f MB " % (rss/1024)) + command end end @usage = @usage.sort_by { |process| process[:rss] } #puts "Your total usage: %.2f MB" % (total / 1024) end |
#report ⇒ Object
Display to stdout Error report
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/memory_usage.rb', line 71 def report @usage.each do |x| if (@options.filter == "") or ((x[:command].match(/.*#{@options.filter}.*/)) and (not x[:command].match(/.*--filter #{@options.filter}.*/) )) or ((x[:command].match(/.*#{@options.filter}.*/)) and (@options.filter.match(/.*memory_usage.*/))) if x[:rss] > @options.min #rjust string.rjust(min length) puts x[:pid].rjust( @max[:pid] ) + (" %.2f MB " % x[:rss]).rjust( @max[:rss]+3 ) + x[:command] end end end puts "Total Memory Usage: %.2f MB" % @total end |
#total ⇒ Object
Return total memory as a String
86 87 88 |
# File 'lib/memory_usage.rb', line 86 def total "%.2f" % @total end |