"Fossies" - the Fresh Open Source Software Archive 
Member "dstat-0.7.4/plugins/dstat_top_cputime.py" (17 Jan 2019, 2353 Bytes) of package /linux/privat/dstat-0.7.4.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "dstat_top_cputime.py" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
0.7.3_vs_0.7.4.
1 ### Authority: dag@wieers.com
2
3 ### For more information, see:
4 ### http://eaglet.rain.com/rick/linux/schedstat/
5
6 class dstat_plugin(dstat):
7 """
8 Name and total amount of CPU time consumed in milliseconds of the process
9 that has the highest total amount of cputime for the measured timeframe.
10
11 On a system with one CPU and one core, the total cputime is 1000ms. On a
12 system with two cores the total cputime is 2000ms.
13 """
14
15 def __init__(self):
16 self.name = 'highest total'
17 self.vars = ('cputime process',)
18 self.type = 's'
19 self.width = 17
20 self.scale = 0
21 self.pidset1 = {}
22
23 def check(self):
24 if not os.access('/proc/self/schedstat', os.R_OK):
25 raise Exception('Kernel has no scheduler statistics [CONFIG_SCHEDSTATS], use at least 2.6.12')
26
27 def extract(self):
28 self.output = ''
29 self.pidset2 = {}
30 self.val['result'] = 0
31 for pid in proc_pidlist():
32 try:
33 ### Reset values
34 if pid not in self.pidset1:
35 self.pidset1[pid] = {'run_ticks': 0}
36
37 ### Extract name
38 name = proc_splitline('/proc/%s/stat' % pid)[1][1:-1]
39
40 ### Extract counters
41 l = proc_splitline('/proc/%s/schedstat' % pid)
42 except IOError:
43 continue
44 except IndexError:
45 continue
46
47 if len(l) != 3: continue
48
49 self.pidset2[pid] = {'run_ticks': int(l[0])}
50
51 totrun = (self.pidset2[pid]['run_ticks'] - self.pidset1[pid]['run_ticks']) * 1.0 / elapsed
52
53 ### Get the process that spends the most jiffies
54 if totrun > self.val['result']:
55 self.val['result'] = totrun
56 self.val['pid'] = pid
57 self.val['name'] = getnamebypid(pid, name)
58
59 if step == op.delay:
60 self.pidset1 = self.pidset2
61
62 if self.val['result'] != 0.0:
63 self.output = '%-*s%s' % (self.width-4, self.val['name'][0:self.width-4], cprint(self.val['result'], 'd', 4, 100))
64
65 ### Debug (show PID)
66 # self.output = '%*s %-*s' % (5, self.val['pid'], self.width-6, self.val['name'])
67
68 def showcsv(self):
69 return '%s / %.4f' % (self.val['name'], self.val['result'])
70
71 # vim:ts=4:sw=4:et