Class: ServerScripts::MemoryMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/server_scripts/memory_monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid: nil, pname: nil, duration: "00:00:30", interval: "00:00:01") ⇒ MemoryMonitor

Returns a new instance of MemoryMonitor.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/server_scripts/memory_monitor.rb', line 10

def initialize(pid: nil, pname: nil, duration: "00:00:30", interval: "00:00:01")
  @vmrss = []
  @vmsize = []
  
  if pname.nil? && pid
    @pname = `ps -p #{pid} -o comm=`.strip
    @pid = pid
  end
  
  if pid.nil? && pname
    @pname = pname
    @pid = `pidof #{pname}`.strip.to_i
  end

  parse_time_intervals duration, interval
end

Instance Attribute Details

#duration_secObject (readonly)

Returns the value of attribute duration_sec.



5
6
7
# File 'lib/server_scripts/memory_monitor.rb', line 5

def duration_sec
  @duration_sec
end

#interval_secObject (readonly)

Returns the value of attribute interval_sec.



6
7
8
# File 'lib/server_scripts/memory_monitor.rb', line 6

def interval_sec
  @interval_sec
end

#pidObject (readonly)

Returns the value of attribute pid.



3
4
5
# File 'lib/server_scripts/memory_monitor.rb', line 3

def pid
  @pid
end

#pnameObject (readonly)

Returns the value of attribute pname.



4
5
6
# File 'lib/server_scripts/memory_monitor.rb', line 4

def pname
  @pname
end

#vmrssObject (readonly)

Returns the value of attribute vmrss.



7
8
9
# File 'lib/server_scripts/memory_monitor.rb', line 7

def vmrss
  @vmrss
end

#vmsizeObject (readonly)

Returns the value of attribute vmsize.



8
9
10
# File 'lib/server_scripts/memory_monitor.rb', line 8

def vmsize
  @vmsize
end

Instance Method Details

#start!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/server_scripts/memory_monitor.rb', line 27

def start!
  @duration_sec.times do
    file = File.open "/proc/#{@pid}/status"
    file.each_line do |l|
      rss = l.match(/VmRSS:\s+(\d+)\s+kB/)
      @vmrss << (rss[1].to_i / 1e3) if rss

      size = l.match(/VmSize:\s+(\d+)\s+kB/)
      @vmsize << (size[1].to_i / 1e3) if size
    end

    sleep @interval_sec
  end
end