Class: GetProcessMem
- Inherits:
-
Object
- Object
- GetProcessMem
- Includes:
- Sys
- Defined in:
- lib/get_process_mem.rb,
lib/get_process_mem/version.rb
Overview
Cribbed from Unicorn Worker Killer, thanks!
Constant Summary collapse
- KB_TO_BYTE =
2**10 = 1024
1024
- MB_TO_BYTE =
1024**2 = 1_048_576
1_048_576
- GB_TO_BYTE =
1024**3 = 1_073_741_824
1_073_741_824
- CONVERSION =
{ "kb" => KB_TO_BYTE, "mb" => MB_TO_BYTE, "gb" => GB_TO_BYTE }
- ROUND_UP =
BigDecimal.new("0.5")
- RUNS_ON_WINDOWS =
Gem.win_platform?
- VERSION =
"0.2.1"
Instance Attribute Summary collapse
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Instance Method Summary collapse
- #bytes ⇒ Object
- #gb(b = bytes) ⇒ Object
-
#initialize(pid = Process.pid) ⇒ GetProcessMem
constructor
A new instance of GetProcessMem.
- #inspect ⇒ Object
- #kb(b = bytes) ⇒ Object
- #linux? ⇒ Boolean
-
#linux_memory(file = @process_file) ⇒ Object
linux stores detailed memory info in a file “/proc/##pid/smaps”.
-
#linux_status_memory(file = @status_file) ⇒ Object
linux stores memory info in a file “/proc/##pid/status” If it’s available it uses less resources than shelling out to ps.
- #mb(b = bytes) ⇒ Object
-
#ps_memory ⇒ Object
Pull memory from ‘ps` command, takes more resources and can freeze in low memory situations.
Constructor Details
#initialize(pid = Process.pid) ⇒ GetProcessMem
Returns a new instance of GetProcessMem.
26 27 28 29 30 31 |
# File 'lib/get_process_mem.rb', line 26 def initialize(pid = Process.pid) @status_file = Pathname.new "/proc/#{pid}/status" @process_file = Pathname.new "/proc/#{pid}/smaps" @pid = pid @linux = @status_file.exist? end |
Instance Attribute Details
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
11 12 13 |
# File 'lib/get_process_mem.rb', line 11 def pid @pid end |
Instance Method Details
#bytes ⇒ Object
37 38 39 40 |
# File 'lib/get_process_mem.rb', line 37 def bytes memory = linux_status_memory if linux? memory ||= ps_memory end |
#gb(b = bytes) ⇒ Object
50 51 52 |
# File 'lib/get_process_mem.rb', line 50 def gb(b = bytes) (b/BigDecimal.new(GB_TO_BYTE)).to_f end |
#inspect ⇒ Object
54 55 56 57 |
# File 'lib/get_process_mem.rb', line 54 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) ⇒ Object
42 43 44 |
# File 'lib/get_process_mem.rb', line 42 def kb(b = bytes) (b/BigDecimal.new(KB_TO_BYTE)).to_f end |
#linux? ⇒ Boolean
33 34 35 |
# File 'lib/get_process_mem.rb', line 33 def linux? @linux end |
#linux_memory(file = @process_file) ⇒ Object
linux stores detailed memory info in a file “/proc/##pid/smaps”
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/get_process_mem.rb', line 71 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 = BigDecimal.new(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) ⇒ Object
linux stores memory info in a file “/proc/##pid/status” If it’s available it uses less resources than shelling out to ps
61 62 63 64 65 66 67 68 |
# File 'lib/get_process_mem.rb', line 61 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) ⇒ Object
46 47 48 |
# File 'lib/get_process_mem.rb', line 46 def mb(b = bytes) (b/BigDecimal.new(MB_TO_BYTE)).to_f end |
#ps_memory ⇒ Object
Pull memory from ‘ps` command, takes more resources and can freeze in low memory situations
88 89 90 91 92 93 94 95 |
# File 'lib/get_process_mem.rb', line 88 def ps_memory if RUNS_ON_WINDOWS size = ProcTable.ps(pid).working_set_size BigDecimal.new(size) else KB_TO_BYTE * BigDecimal.new(`ps -o rss= -p #{pid}`) end end |