Class: Stalin::Watcher::LinuxProcStatm

Inherits:
Object
  • Object
show all
Defined in:
lib/stalin/watcher/linux_proc_statm.rb

Overview

Watcher that uses procfs (specifically /proc/<pid>/statm) to determine memory usage.

Instance Method Summary collapse

Constructor Details

#initialize(pid) ⇒ LinuxProcStatm

Returns a new instance of LinuxProcStatm.

Parameters:

  • pid (Integer)

    target process ID

Raises:



6
7
8
9
10
11
12
13
# File 'lib/stalin/watcher/linux_proc_statm.rb', line 6

def initialize(pid)
  @statm = "/proc/%d/statm" % [pid]
  raise Stalin::Unsupported, "Unreadable or nonexistent file: #{@statm}" unless File.readable?(@statm)

  page_size = `getconf PAGESIZE`
  @page_size = Integer(page_size) rescue nil
  raise Stalin::Unsupported, "Cannot determine page size: #{page_size}" unless $?.success? && @page_size.kind_of?(Integer)
end

Instance Method Details

#watchInteger?

Report on memory usage.

Returns:

  • (Integer, nil)

    target process’ memory usage in bytes, nil if process not found



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stalin/watcher/linux_proc_statm.rb', line 18

def watch
  vsz, rss, shared = File.read(@statm).split(' ')
  vsz              = Integer(vsz) * @page_size
  rss              = Integer(rss) * @page_size
  shared           = Integer(shared) * @page_size

  rss
rescue SystemCallError
  # assume any failure means that process has gone away
  nil
end