blob: 4d6e808d7dfa86cf59629ef4f3e0e766f555d551 [file] [log] [blame]
Javi Merino491cf732014-03-31 17:34:44 +01001#!/usr/bin/python
Javi Merino572049d2014-03-31 16:45:23 +01002"""Process the output of the power allocator trace in the current directory's trace.dat"""
3
4import os
Javi Merinoee56c362014-03-31 17:30:34 +01005import re
Javi Merinof78ea5b2014-03-31 17:51:38 +01006import pandas as pd
Javi Merino572049d2014-03-31 16:45:23 +01007
8class Thermal(object):
9 def __init__(self):
10 if not os.path.isfile("trace.txt"):
11 self.__run_trace_cmd_report()
Javi Merinof78ea5b2014-03-31 17:51:38 +010012 self.data_frame = False
Javi Merino572049d2014-03-31 16:45:23 +010013
14
15 def __run_trace_cmd_report(self):
16 """Run "trace-cmd report > trace.txt". Overwrites the contents of trace.txt if it exists."""
Javi Merinoee56c362014-03-31 17:30:34 +010017 from subprocess import check_output
18
Javi Merino572049d2014-03-31 16:45:23 +010019 with open(os.devnull) as devnull:
Javi Merinoee56c362014-03-31 17:30:34 +010020 out = check_output(["trace-cmd", "report"], stderr=devnull)
Javi Merino572049d2014-03-31 16:45:23 +010021
22 with open("trace.txt", "w") as f:
23 f.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010024
25 def write_thermal_csv(self):
26 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
27 pat_data = re.compile(r"[A-Za-z0-9_]+=([0-9]+) ")
28 header = ""
29
30 with open("thermal.csv", "w") as fout:
31 with open("trace.txt") as fin:
32 for line in fin:
33 if not re.search("Ptot_out", line):
34 continue
35
36 line = line[:-1]
37
38 m = re.search(pat_timestamp, line)
39 timestamp = m.group(1)
40
41 semi_idx = line.index(" : ")
42 data_str = line[semi_idx + 3:]
43
44 if not header:
45 header = re.sub(r"([A-Za-z0-9_]+)=[0-9]+ ", r"\1,", data_str)
46 header = header[:-1]
47 header = "time," + header + "\n"
48 fout.write(header)
49
50 parsed_data = re.sub(pat_data, r"\1,", data_str)
51 # Drop the last comma
52 parsed_data = parsed_data[:-1]
53
54 parsed_data = timestamp + "," + parsed_data + "\n"
55 fout.write(parsed_data)
Javi Merinof78ea5b2014-03-31 17:51:38 +010056
57 def get_data_frame(self):
58 """Return a pandas data frame for the run"""
59 if self.data_frame:
60 return self.data_frame
61
62 if not os.path.isfile("thermal.csv"):
63 self.write_thermal_csv()
64
65 self.data_frame = pd.read_csv("thermal.csv").set_index("time")
66 return self.data_frame