forked from Atlantic777/pmu-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tldata.py
85 lines (77 loc) · 2.54 KB
/
tldata.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
77
78
79
80
81
82
83
84
85
import os
import gen_level
from collections import defaultdict
import csv
import re
class TLData:
"""Read a toplev output CSV file.
Exported:
times[n] All time stamps
vals[n] All values, as dicts mapping (name, cpu)->float
levels{name} All levels (includes metrics), name->list of fields
headers(set) All headers (including metrics)
metrics(set) All metrics
helptxt[col] All help texts.
cpus(set) All CPUs
"""
def __init__(self, fn, verbose=False):
self.times = []
self.vals = []
self.fn = fn
self.levels = defaultdict(set)
self.metrics = set()
self.headers = set()
self.mtime = None
self.helptxt = dict()
self.cpus = set()
self.verbose = verbose
def update(self):
mtime = os.path.getmtime(self.fn)
if self.mtime == mtime:
return
self.mtime = mtime
csvf = csv.reader(open(self.fn, 'r'))
prevts = None
val = dict()
for r in csvf:
if r[0].strip().startswith("#"):
continue
if re.match(r'[CS]\d+.*', r[1]):
ts, cpu, name, pct, state, helptxt = r[0], r[1], r[2], r[3], r[4], r[5]
else:
ts, name, pct, state, helptxt = r[0], r[1], r[2], r[3], r[4]
cpu = None
key = (name, cpu)
ts, pct = float(ts), float(pct.replace("%", ""))
if name not in self.helptxt or self.helptxt[name] == "":
self.helptxt[name] = helptxt
if state == "below" and not self.verbose:
continue
if prevts and ts != prevts:
self.times.append(prevts)
self.vals.append(val)
val = dict()
val[key] = pct
n = gen_level.level_name(name)
if cpu:
self.cpus.add(cpu)
self.headers.add(name)
if gen_level.is_metric(name):
self.metrics.add(n)
self.levels[n].add(name)
prevts = ts
early_plots = ["TopLevel", "CPU utilization", "Power", "Frequency", "CPU-METRIC"]
def sort_pos(i, data):
if i in early_plots:
return early_plots.index(i)
if i in data.metrics:
return 30
return 20
def cmp_level(a, b, data):
ap, bp = sort_pos(a, data), sort_pos(b, data)
if ap != bp:
return ap - bp
return cmp(a, b)
def level_order(data):
"""Return plot order of all levels."""
return sorted(data.levels.keys(), cmp=lambda a, b: cmp_level(a, b, data))