diff options
Diffstat (limited to 'scripts/pybootchartgui/pybootchartgui/samples.py')
-rw-r--r-- | scripts/pybootchartgui/pybootchartgui/samples.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/scripts/pybootchartgui/pybootchartgui/samples.py b/scripts/pybootchartgui/pybootchartgui/samples.py new file mode 100644 index 0000000000..c94b30d032 --- /dev/null +++ b/scripts/pybootchartgui/pybootchartgui/samples.py | |||
@@ -0,0 +1,93 @@ | |||
1 | class DiskStatSample: | ||
2 | def __init__(self, time): | ||
3 | self.time = time | ||
4 | self.diskdata = [0, 0, 0] | ||
5 | def add_diskdata(self, new_diskdata): | ||
6 | self.diskdata = [ a + b for a, b in zip(self.diskdata, new_diskdata) ] | ||
7 | |||
8 | class CPUSample: | ||
9 | def __init__(self, time, user, sys, io): | ||
10 | self.time = time | ||
11 | self.user = user | ||
12 | self.sys = sys | ||
13 | self.io = io | ||
14 | |||
15 | def __str__(self): | ||
16 | return str(self.time) + "\t" + str(self.user) + "\t" + str(self.sys) + "\t" + str(self.io); | ||
17 | |||
18 | class ProcessSample: | ||
19 | def __init__(self, time, state, cpu_sample): | ||
20 | self.time = time | ||
21 | self.state = state | ||
22 | self.cpu_sample = cpu_sample | ||
23 | |||
24 | def __str__(self): | ||
25 | return str(self.time) + "\t" + str(self.state) + "\t" + str(self.cpu_sample); | ||
26 | |||
27 | class ProcessStats: | ||
28 | def __init__(self, process_list, sample_period, start_time, end_time): | ||
29 | self.process_list = process_list | ||
30 | self.sample_period = sample_period | ||
31 | self.start_time = start_time | ||
32 | self.end_time = end_time | ||
33 | |||
34 | class Process: | ||
35 | def __init__(self, pid, cmd, ppid, start_time): | ||
36 | self.pid = pid | ||
37 | self.cmd = cmd.strip('(').strip(')') | ||
38 | self.ppid = ppid | ||
39 | self.start_time = start_time | ||
40 | self.samples = [] | ||
41 | self.parent = None | ||
42 | self.child_list = [] | ||
43 | |||
44 | self.duration = 0 | ||
45 | self.active = None | ||
46 | |||
47 | self.last_user_cpu_time = None | ||
48 | self.last_sys_cpu_time = None | ||
49 | |||
50 | def __str__(self): | ||
51 | return " ".join([str(self.pid), self.cmd, str(self.ppid), '[ ' + str(len(self.samples)) + ' samples ]' ]) | ||
52 | |||
53 | def calc_stats(self, samplePeriod): | ||
54 | if self.samples: | ||
55 | firstSample = self.samples[0] | ||
56 | lastSample = self.samples[-1] | ||
57 | self.start_time = min(firstSample.time, self.start_time) | ||
58 | self.duration = lastSample.time - self.start_time + samplePeriod | ||
59 | |||
60 | activeCount = sum( [1 for sample in self.samples if sample.cpu_sample and sample.cpu_sample.sys + sample.cpu_sample.user + sample.cpu_sample.io > 0.0] ) | ||
61 | activeCount = activeCount + sum( [1 for sample in self.samples if sample.state == 'D'] ) | ||
62 | self.active = (activeCount>2) | ||
63 | |||
64 | def calc_load(self, userCpu, sysCpu, interval): | ||
65 | |||
66 | userCpuLoad = float(userCpu - self.last_user_cpu_time) / interval | ||
67 | sysCpuLoad = float(sysCpu - self.last_sys_cpu_time) / interval | ||
68 | cpuLoad = userCpuLoad + sysCpuLoad | ||
69 | # normalize | ||
70 | if cpuLoad > 1.0: | ||
71 | userCpuLoad = userCpuLoad / cpuLoad; | ||
72 | sysCpuLoad = sysCpuLoad / cpuLoad; | ||
73 | return (userCpuLoad, sysCpuLoad) | ||
74 | |||
75 | def set_parent(self, processMap): | ||
76 | if self.ppid != None: | ||
77 | self.parent = processMap.get(self.ppid) | ||
78 | if self.parent == None and self.pid > 1: | ||
79 | print "warning: no parent for pid '%i' with ppid '%i'" % (self.pid,self.ppid) | ||
80 | def get_end_time(self): | ||
81 | return self.start_time + self.duration | ||
82 | |||
83 | class DiskSample: | ||
84 | def __init__(self, time, read, write, util): | ||
85 | self.time = time | ||
86 | self.read = read | ||
87 | self.write = write | ||
88 | self.util = util | ||
89 | self.tput = read + write | ||
90 | |||
91 | def __str__(self): | ||
92 | return "\t".join([str(self.time), str(self.read), str(self.write), str(self.util)]) | ||
93 | |||