This repository has been archived by the owner on Nov 26, 2020. It is now read-only.
/
dstat_top_cputime.py
76 lines (57 loc) · 2.44 KB
/
dstat_top_cputime.py
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
### Authority: dag@wieers.com
### For more information, see:
### http://eaglet.rain.com/rick/linux/schedstat/
class dstat_plugin(dstat):
"""
Name and total amount of CPU time consumed in milliseconds of the process
that has the highest total amount of cputime for the measured timeframe.
"""
def __init__(self):
self.name = 'highest total'
self.type = 's'
self.width = 17
self.scale = 0
self.vars = ('cputime process',)
self.pid = str(os.getpid())
self.pidset1 = {}; self.pidset2 = {}
def check(self):
if not os.access('/proc/self/schedstat', os.R_OK):
raise Exception, 'Kernel has no scheduler statistics, use at least 2.6.12'
def extract(self):
self.val['result'] = 0
self.val['process'] = ''
for pid in os.listdir('/proc/'):
try:
### Is it a pid ?
int(pid)
### Filter out dstat
if pid == self.pid: continue
### Reset values
if not self.pidset1.has_key(pid):
self.pidset1[pid] = {'run_ticks': 0}
### Extract name
name = open('/proc/%s/stat' % pid).read().split()[1][1:-1]
### Extract counters
l = open('/proc/%s/schedstat' % pid).read().split()
except ValueError:
continue
except IOError:
continue
if len(l) != 3: continue
self.pidset2[pid] = {'run_ticks': long(l[0])}
totrun = (self.pidset2[pid]['run_ticks'] - self.pidset1[pid]['run_ticks']) * 1.0 / elapsed
### Get the process that spends the most jiffies
if totrun > self.val['result']:
self.val['result'] = totrun
self.val['pid'] = pid
self.val['name'] = getnamebypid(pid, name)
if step == op.delay:
for pid in self.pidset2.keys():
self.pidset1[pid].update(self.pidset2[pid])
if self.val['result'] != 0.0:
self.val['cputime process'] = '%-*s%s' % (self.width-4, self.val['name'][0:self.width-4], cprint(self.val['result'], 'd', 4, 100))
### Debug (show PID)
# self.val['cputime process'] = '%*s %-*s' % (5, self.val['pid'], self.width-6, self.val['name'])
def showcsv(self):
return '%s / %.4f' % (self.val['name'], self.val['result'])
# vim:ts=4:sw=4:et