Class: Memstat::Proc::Smaps
Defined Under Namespace
Classes: Item
Constant Summary collapse
- FIELDS =
%w[size rss pss shared_clean shared_dirty private_clean private_dirty swap]
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
-
#lines ⇒ Object
Returns the value of attribute lines.
Attributes inherited from Base
Instance Method Summary collapse
- #command ⇒ Object
-
#initialize(options = {}) ⇒ Smaps
constructor
A new instance of Smaps.
- #number_with_delimiter(n) ⇒ Object
- #print ⇒ Object
- #run ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ Smaps
Returns a new instance of Smaps.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/memstat/proc/smaps.rb', line 8 def initialize( = {}) super @path ||= "/proc/#{@pid}/smaps" FIELDS.each do |field| send("#{field}=", 0) end run end |
Instance Attribute Details
#items ⇒ Object
Returns the value of attribute items.
6 7 8 |
# File 'lib/memstat/proc/smaps.rb', line 6 def items @items end |
#lines ⇒ Object
Returns the value of attribute lines.
6 7 8 |
# File 'lib/memstat/proc/smaps.rb', line 6 def lines @lines end |
Instance Method Details
#command ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/memstat/proc/smaps.rb', line 57 def command return unless pid? commandline = File.read("/proc/#{@pid}/cmdline").split("\0") if commandline.first =~ /java$/ then loop { break if commandline.shift == "-jar" } return "[java] #{commandline.shift}" end return commandline.join(' ') end |
#number_with_delimiter(n) ⇒ Object
67 68 69 |
# File 'lib/memstat/proc/smaps.rb', line 67 def number_with_delimiter(n) n.to_s.gsub(/(\d)(?=\d{3}+$)/, '\\1,') end |
#print ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/memstat/proc/smaps.rb', line 44 def print @print ||= begin lines = [] lines << "#{"Process:".ljust(20)} #{@pid || '[unspecified]'}" lines << "#{"Command Line:".ljust(20)} #{command || '[unspecified]'}" lines << "Memory Summary:" FIELDS.each do |field| lines << " #{field.ljust(20)} #{number_with_delimiter(send(field)/1024).rjust(12)} kB" end lines.join("\n") end end |
#run ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/memstat/proc/smaps.rb', line 19 def run @lines = File.readlines(@path).map(&:strip) @items = [] item = nil @lines.each.with_index do |line, index| case line when /[0-9a-f]+:[0-9a-f]+\s+/ item = Item.new @items << item item.parse_first_line(line) when /\w+:\s+/ item.parse_field_line(line) else raise Error.new("invalid format at line #{index + 1}: #{line}") end end @items.each do |item| FIELDS.each do |field| send "#{field}=", (send(field) + item.send(field)) end end end |