Class: God::System::SlashProcPoller

Inherits:
PortablePoller show all
Defined in:
lib/god/system/slash_proc_poller.rb

Constant Summary collapse

MeminfoPath =
'/proc/meminfo'
UptimePath =
'/proc/uptime'
RequiredPaths =
[MeminfoPath, UptimePath]
@@kb_per_page =

TODO: Need to make this portable

4
@@hertz =
100
@@total_mem =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid) ⇒ SlashProcPoller

Returns a new instance of SlashProcPoller.



22
23
24
25
26
27
28
29
30
# File 'lib/god/system/slash_proc_poller.rb', line 22

def initialize(pid)
  super(pid)

  unless @@total_mem # in K
    File.open(MeminfoPath) do |f|
      @@total_mem = f.gets.split[1]
    end
  end
end

Class Method Details

.usable?Boolean

FreeBSD has /proc by default, but nothing mounted there! So we should check for the actual required paths! Returns true if RequiredPaths are readable.

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/god/system/slash_proc_poller.rb', line 16

def self.usable?
  RequiredPaths.all? do |path|
    test(?r, path) && readable?(path)
  end
end

Instance Method Details

#memoryObject



32
33
34
35
36
# File 'lib/god/system/slash_proc_poller.rb', line 32

def memory
  stat[:rss].to_i * @@kb_per_page
rescue # This shouldn't fail is there's an error (or proc doesn't exist)
  0
end

#percent_cpuObject

TODO: Change this to calculate the wma instead



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/god/system/slash_proc_poller.rb', line 45

def percent_cpu
  stats = stat
  total_time = stats[:utime].to_i + stats[:stime].to_i # in jiffies
  seconds = uptime - stats[:starttime].to_i / @@hertz
  if seconds == 0
    0
  else
    ((total_time * 1000 / @@hertz) / seconds) / 10
  end
rescue # This shouldn't fail is there's an error (or proc doesn't exist)
  0
end

#percent_memoryObject



38
39
40
41
42
# File 'lib/god/system/slash_proc_poller.rb', line 38

def percent_memory
  (memory / @@total_mem.to_f) * 100
rescue # This shouldn't fail is there's an error (or proc doesn't exist)
  0
end