public
Fork of mojombo/god
Description: Ruby process monitor
Homepage: http://god.rubyforge.org
Clone URL: git://github.com/kevinclark/god.git
Search Repo:
Split System::Process into ps and /proc based pollers
kevinclark (author)
Tue May 13 20:23:43 -0700 2008
commit  466dbc3416c15c7f968f3012aa23904e157ad50f
tree    c8efc4b3567df8cfd6ae71031deb94b40b033cac
parent  87c194332a77399b96efec701a242a0df16778d8
...
20
21
22
 
23
 
 
 
24
25
26
...
20
21
22
23
24
25
26
27
28
29
30
0
@@ -20,7 +20,11 @@
0
 # internal requires
0
 require 'god/errors'
0
 require 'god/logger'
0
+
0
 require 'god/system/process'
0
+require 'god/system/portable_poller'
0
+require 'god/system/slash_proc_poller'
0
+
0
 require 'god/dependency_graph'
0
 require 'god/timeline'
0
 require 'god/configurable'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
0
@@ -1 +1,43 @@
0
+module God
0
+ module System
0
+ class PortablePoller
0
+ def initialize(pid)
0
+ @pid = pid
0
+ end
0
+ # Memory usage in kilobytes (resident set size)
0
+ def memory
0
+ ps_int('rss')
0
+ end
0
+
0
+ # Percentage memory usage
0
+ def percent_memory
0
+ ps_float('%mem')
0
+ end
0
+
0
+ # Percentage CPU usage
0
+ def percent_cpu
0
+ ps_float('%cpu')
0
+ end
0
+
0
+ private
0
+
0
+ def ps_int(keyword)
0
+ `ps -o #{keyword}= -p #{@pid}`.to_i
0
+ end
0
+
0
+ def ps_float(keyword)
0
+ `ps -o #{keyword}= -p #{@pid}`.to_f
0
+ end
0
+
0
+ def ps_string(keyword)
0
+ `ps -o #{keyword}= -p #{@pid}`.strip
0
+ end
0
+
0
+ def time_string_to_seconds(text)
0
+ _, minutes, seconds, useconds = *text.match(/(\d+):(\d{2}).(\d{2})/)
0
+ (minutes.to_i * 60) + seconds.to_i
0
+ end
0
+ end
0
+ end
0
+end
...
4
5
6
 
7
8
9
10
11
 
12
13
14
15
16
 
17
18
19
20
21
 
22
23
24
25
26
 
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 
 
 
 
 
 
51
52
53
...
4
5
6
7
8
9
10
11
 
12
13
14
15
16
 
17
18
19
20
21
 
22
23
24
25
26
 
27
28
29
 
 
 
 
 
30
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
33
34
35
36
37
38
39
40
0
@@ -4,50 +4,37 @@
0
     class Process
0
       def initialize(pid)
0
         @pid = pid.to_i
0
+ @poller = fetch_system_poller
0
       end
0
       
0
       # Return true if this process is running, false otherwise
0
       def exists?
0
- system("kill -0 #{@pid} &> /dev/null")
0
+ !!Process.kill(0, @pid) rescue false
0
       end
0
       
0
       # Memory usage in kilobytes (resident set size)
0
       def memory
0
- ps_int('rss')
0
+ @poller.memory(@pid)
0
       end
0
       
0
       # Percentage memory usage
0
       def percent_memory
0
- ps_float('%mem')
0
+ @poller.percent_memory(@pid)
0
       end
0
       
0
       # Percentage CPU usage
0
       def percent_cpu
0
- ps_float('%cpu')
0
+ @poller.percent_cpu(@pid)
0
       end
0
       
0
- # Seconds of CPU time (accumulated cpu time, user + system)
0
- def cpu_time
0
- time_string_to_seconds(ps_string('time'))
0
- end
0
-
0
       private
0
       
0
- def ps_int(keyword)
0
- `ps -o #{keyword}= -p #{@pid}`.to_i
0
- end
0
-
0
- def ps_float(keyword)
0
- `ps -o #{keyword}= -p #{@pid}`.to_f
0
- end
0
-
0
- def ps_string(keyword)
0
- `ps -o #{keyword}= -p #{@pid}`.strip
0
- end
0
-
0
- def time_string_to_seconds(text)
0
- _, minutes, seconds, useconds = *text.match(/(\d+):(\d{2}).(\d{2})/)
0
- (minutes.to_i * 60) + seconds.to_i
0
+ def fetch_system_poller
0
+ if test(?d, '/proc')
0
+ SlashProcPoller
0
+ else
0
+ PortablePoller
0
+ end
0
       end
0
     end
0
   
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
0
@@ -1 +1,63 @@
0
+module God
0
+ module System
0
+ class SlashProcPoller < PortablePoller
0
+ @@kb_per_page = 4 # TODO: Need to make this portable
0
+ @@hertz = 100
0
+
0
+ def initialize(pid)
0
+ super(pid)
0
+
0
+ unless @@total_mem # in K
0
+ File.open("/proc/meminfo") do |f|
0
+ @@total_mem = f.gets.split[1]
0
+ end
0
+ end
0
+ end
0
+
0
+ def memory
0
+ stat[:rss].to_i * @@kb_per_page
0
+ end
0
+
0
+ def precent_memory(pid)
0
+ (memory / @@total_mem.to_f) * 100
0
+ end
0
+
0
+ # TODO: Change this to calculate the wma instead
0
+ def cpu_percent
0
+ percent_cpu = 0
0
+ stats = stat
0
+ total_time = stats[:utime].to_i + stats[:stime].to_i # in jiffies
0
+ seconds = uptime - stats[:starttime].to_i / @@hertz
0
+ if seconds == 0
0
+ 0
0
+ else
0
+ ((total_time * 1000 / @@hertz) / seconds) / 10
0
+ end
0
+ end
0
+
0
+ private
0
+
0
+ # in seconds
0
+ def uptime
0
+ File.read('/proc/uptime').split[0].to_f
0
+ end
0
+
0
+ def stat
0
+ stats = {}
0
+ stats[:pid], stats[:comm], stats[:state], stats[:ppid], stats[:pgrp],
0
+ stats[:session], stats[:tty_nr], stats[:tpgid], stats[:flags],
0
+ stats[:minflt], stats[:cminflt], stats[:majflt], stats[:cmajflt],
0
+ stats[:utime], stats[:stime], stats[:cutime], stats[:cstime],
0
+ stats[:priority], stats[:nice], _, stats[:itrealvalue],
0
+ stats[:starttime], stats[:vsize], stats[:rss], stats[:rlim],
0
+ stats[:startcode], stats[:endcode], stats[:startstack], stats[:kstkesp],
0
+ stats[:kstkeip], stats[:signal], stats[:blocked], stats[:sigignore],
0
+ stats[:sigcatch], stats[:wchan], stats[:nswap], stats[:cnswap],
0
+ stats[:exit_signal], stats[:processor], stats[:rt_priority],
0
+ stats[:policy] = File.read("/proc/#{@pid}/stat").split
0
+ stats
0
+ end
0
+ end
0
+ end
0
+end

Comments

    No one has commented yet.