Class: GetProcessMem
- Inherits:
-
Object
- Object
- GetProcessMem
- 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}
- MEM_TYPE =
"rss"- VERSION =
"0.0.2"
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, mem_type = MEM_TYPE) ⇒ GetProcessMem
constructor
A new instance of GetProcessMem.
- #inspect ⇒ Object
- #kb(b = bytes) ⇒ Object
- #linux? ⇒ Boolean
- #mb(b = bytes) ⇒ Object
- #mem_type ⇒ Object
- #mem_type=(mem_type) ⇒ Object
Constructor Details
#initialize(pid = Process.pid, mem_type = MEM_TYPE) ⇒ GetProcessMem
Returns a new instance of GetProcessMem.
12 13 14 15 16 17 |
# File 'lib/get_process_mem.rb', line 12 def initialize(pid = Process.pid, mem_type = MEM_TYPE) @process_file = Pathname.new "/proc/#{pid}/status" @pid = pid @linux = @process_file.exist? self.mem_type = mem_type end |
Instance Attribute Details
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
10 11 12 |
# File 'lib/get_process_mem.rb', line 10 def pid @pid end |
Instance Method Details
#bytes ⇒ Object
23 24 25 26 |
# File 'lib/get_process_mem.rb', line 23 def bytes memory = linux_memory if linux? memory ||= ps_memory end |
#gb(b = bytes) ⇒ Object
36 37 38 |
# File 'lib/get_process_mem.rb', line 36 def gb(b = bytes) b.fdiv(GB_TO_BYTE) end |
#inspect ⇒ Object
40 41 42 43 |
# File 'lib/get_process_mem.rb', line 40 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
28 29 30 |
# File 'lib/get_process_mem.rb', line 28 def kb(b = bytes) b.fdiv(KB_TO_BYTE) end |
#linux? ⇒ Boolean
19 20 21 |
# File 'lib/get_process_mem.rb', line 19 def linux? @linux end |
#mb(b = bytes) ⇒ Object
32 33 34 |
# File 'lib/get_process_mem.rb', line 32 def mb(b = bytes) b.fdiv(MB_TO_BYTE) end |
#mem_type ⇒ Object
45 46 47 |
# File 'lib/get_process_mem.rb', line 45 def mem_type @mem_type end |
#mem_type=(mem_type) ⇒ Object
49 50 51 |
# File 'lib/get_process_mem.rb', line 49 def mem_type=(mem_type) @mem_type = mem_type.downcase end |