Class: Memstat::Proc::Smaps

Inherits:
Base
  • Object
show all
Defined in:
lib/memstat/proc/smaps.rb

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

Attributes inherited from Base

#path, #pid

Instance Method Summary collapse

Methods inherited from Base

#pid?

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(options = {})
  super
  @path ||= "/proc/#{@pid}/smaps"

  FIELDS.each do |field|
    send("#{field}=", 0)
  end

  run
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



6
7
8
# File 'lib/memstat/proc/smaps.rb', line 6

def items
  @items
end

#linesObject

Returns the value of attribute lines.



6
7
8
# File 'lib/memstat/proc/smaps.rb', line 6

def lines
  @lines
end

Instance Method Details

#commandObject



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


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

#runObject



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