class GetProcessMem
Cribbed from Unicorn Worker Killer, thanks!
Constants
- CONVERSION
- GB_TO_BYTE
- KB_TO_BYTE
- MB_TO_BYTE
- ROUND_UP
- RUNS_ON_DARWIN
- RUNS_ON_WINDOWS
- VERSION
Attributes
pid[R]
Public Class Methods
new(pid = Process.pid)
click to toggle source
# File lib/get_process_mem.rb, line 48 def initialize(pid = Process.pid) @status_file = Pathname.new "/proc/#{pid}/status" @process_file = Pathname.new "/proc/#{pid}/smaps" @pid = Integer(pid) @linux = @status_file.exist? end
Private Class Methods
number_to_bigdecimal(value)
click to toggle source
# File lib/get_process_mem.rb, line 6 def self.number_to_bigdecimal(value) BigDecimal(value) end
Public Instance Methods
bytes()
click to toggle source
# File lib/get_process_mem.rb, line 59 def bytes memory = linux_status_memory if linux? memory ||= darwin_memory if RUNS_ON_DARWIN memory ||= ps_memory memory end
darwin_memory()
click to toggle source
# File lib/get_process_mem.rb, line 122 def darwin_memory Darwin.resident_size(pid) rescue Errno::EPERM nil end
gb(b = bytes)
click to toggle source
# File lib/get_process_mem.rb, line 74 def gb(b = bytes) (b / GB_TO_BYTE).to_f end
inspect()
click to toggle source
# File lib/get_process_mem.rb, line 78 def inspect b = bytes "#<#{self.class}:0x%08x @mb=#{mb b} @gb=#{gb b} @kb=#{kb b} @bytes=#{b}>" % (object_id * 2) end
kb(b = bytes)
click to toggle source
# File lib/get_process_mem.rb, line 66 def kb(b = bytes) (b / KB_TO_BYTE).to_f end
linux?()
click to toggle source
# File lib/get_process_mem.rb, line 55 def linux? @linux end
linux_memory(file = @process_file)
click to toggle source
linux stores detailed memory info in a file “/proc/#{pid}/smaps”
# File lib/get_process_mem.rb, line 95 def linux_memory(file = @process_file) lines = file.each_line.select { |line| line.match(/^Rss/) } return if lines.empty? lines.reduce(0) do |sum, line| line.match(/(?<value>\d*\.{0,1}\d+)\s+(?<unit>\w\w)/) do |m| value = number_to_bigdecimal(m[:value]) + ROUND_UP unit = m[:unit].downcase sum += CONVERSION[unit] * value end sum end rescue Errno::EACCES 0 end
linux_status_memory(file = @status_file)
click to toggle source
linux stores memory info in a file “/proc/#{pid}/status” If it's available it uses less resources than shelling out to ps
# File lib/get_process_mem.rb, line 85 def linux_status_memory(file = @status_file) line = file.each_line.detect { |line| line.start_with? "VmRSS".freeze } return unless line return unless (_name, value, unit = line.split(nil)).length == 3 CONVERSION[unit.downcase!] * value.to_i rescue Errno::EACCES, Errno::ENOENT 0 end
mb(b = bytes)
click to toggle source
# File lib/get_process_mem.rb, line 70 def mb(b = bytes) (b / MB_TO_BYTE).to_f end
ps_memory()
click to toggle source
Pull memory from `ps` command, takes more resources and can freeze in low memory situations
# File lib/get_process_mem.rb, line 112 def ps_memory if RUNS_ON_WINDOWS size = ProcTable.ps(pid: pid).working_set_size number_to_bigdecimal(size) else mem = `ps -o rss= -p #{pid}` KB_TO_BYTE * number_to_bigdecimal((mem == "") ? 0 : mem) end end
Private Instance Methods
number_to_bigdecimal(value)
click to toggle source
# File lib/get_process_mem.rb, line 10 def number_to_bigdecimal(value) self.class.send(:number_to_bigdecimal, value) end