blob: 3056b7f5d17d6a30420b5474b9cc3ca395188a61 [file] [log] [blame]
Javi Merino572049d2014-03-31 16:45:23 +01001#!/usr/bin/python3
2"""Process the output of the power allocator trace in the current directory's trace.dat"""
3
4import os
Javi Merinoc08ca682014-03-31 17:29:27 +01005import re, subprocess
Javi Merino572049d2014-03-31 16:45:23 +01006
7class Thermal(object):
8 def __init__(self):
9 if not os.path.isfile("trace.txt"):
10 self.__run_trace_cmd_report()
11
12
13 def __run_trace_cmd_report(self):
14 """Run "trace-cmd report > trace.txt". Overwrites the contents of trace.txt if it exists."""
15 with open(os.devnull) as devnull:
16 out = subprocess.check_output(["trace-cmd", "report"], stderr=devnull)
17
18 with open("trace.txt", "w") as f:
19 f.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010020
21 def write_thermal_csv(self):
22 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
23 pat_data = re.compile(r"[A-Za-z0-9_]+=([0-9]+) ")
24 header = ""
25
26 with open("thermal.csv", "w") as fout:
27 with open("trace.txt") as fin:
28 for line in fin:
29 if not re.search("Ptot_out", line):
30 continue
31
32 line = line[:-1]
33
34 m = re.search(pat_timestamp, line)
35 timestamp = m.group(1)
36
37 semi_idx = line.index(" : ")
38 data_str = line[semi_idx + 3:]
39
40 if not header:
41 header = re.sub(r"([A-Za-z0-9_]+)=[0-9]+ ", r"\1,", data_str)
42 header = header[:-1]
43 header = "time," + header + "\n"
44 fout.write(header)
45
46 parsed_data = re.sub(pat_data, r"\1,", data_str)
47 # Drop the last comma
48 parsed_data = parsed_data[:-1]
49
50 parsed_data = timestamp + "," + parsed_data + "\n"
51 fout.write(parsed_data)