Class: GetProcessMem

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#pidObject (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

#bytesObject



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

#inspectObject



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

Returns:

  • (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_typeObject



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