forked from astro/lxc-top
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxc-top.rb
executable file
·77 lines (65 loc) · 1.74 KB
/
lxc-top.rb
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env ruby
require 'ubygems'
require 'terminal-table/import'
CGROUPFS = "/sys/fs/cgroup"
$cgroups = {}
def read_values!(path = '/')
fpath = CGROUPFS + path
tasks = []
IO.readlines(fpath + '/tasks').each do |l|
l.chomp!
tasks << l
end
mem_usage = IO.readlines(fpath + '/memory.usage_in_bytes').to_s.to_i
memsw_usage = IO.readlines(fpath + '/memory.memsw.usage_in_bytes').to_s.to_i
cpu_elapsed = IO.readlines(fpath + '/cpuacct.usage').to_s.to_f / 1E9
cpu_usage = cpu_elapsed - ($old_cgroups[path] && $old_cgroups[path][:cpu] || cpu_elapsed)
$cgroups[path] = { :tasks => tasks.length,
:mem => mem_usage,
:swap => memsw_usage - mem_usage,
:cpu => cpu_elapsed,
:usage => cpu_usage
}
# Recurse
Dir.foreach(fpath) do |f|
if f =~ /^[^\.]/ && File.directory?(fpath + f)
read_values!(path + (path =~ /\/$/ ? '' : '/') + f)
end
end
end
def human(i)
affix = ''
units = %w(K M G T P E)
while i >= 8192 && units.length > 0
i /= 1024
affix = units.shift
end
"#{i}#{affix}"
end
def display!
keys = $cgroups.keys.sort
puts table([nil, 'Tasks', 'Mem', 'Swap', 'CPU [s]', 'CPU%'],
*keys.select { |g|
$cgroups[g][:tasks] > 0
}.map { |g|
[g, $cgroups[g][:tasks],
human($cgroups[g][:mem]), human($cgroups[g][:swap]),
($cgroups[g][:cpu] * 1000).to_i / 1000.0, ($cgroups[g][:usage] * 100).to_i]
})
end
["INT","TERM", "TRAP", "USR1", "HUP"].each { |sig|
Signal.trap(sig) do
exit
end
}
loop do
$old_cgroups = $cgroups
$cgroups = {}
puts "\e[H\e[2J"
last_tick = Time.now.to_f
read_values!
#p $cgroups
display!
delay = last_tick + 1 - Time.now.to_f
sleep delay if delay > 0
end