blob: 8541dfd50bee098ae7a8684b524c4bdcc373f45d [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 Merino952815a2014-03-31 18:08:32 +01006from StringIO import StringIO
Javi Merinof78ea5b2014-03-31 17:51:38 +01007import pandas as pd
Javi Merinoa6399fb2014-03-31 19:17:08 +01008from matplotlib import pyplot as plt
Javi Merino572049d2014-03-31 16:45:23 +01009
10class Thermal(object):
11 def __init__(self):
12 if not os.path.isfile("trace.txt"):
13 self.__run_trace_cmd_report()
Javi Merino952815a2014-03-31 18:08:32 +010014 self.data_csv = ""
Javi Merinof78ea5b2014-03-31 17:51:38 +010015 self.data_frame = False
Javi Merino572049d2014-03-31 16:45:23 +010016
17
18 def __run_trace_cmd_report(self):
19 """Run "trace-cmd report > trace.txt". Overwrites the contents of trace.txt if it exists."""
Javi Merinoee56c362014-03-31 17:30:34 +010020 from subprocess import check_output
21
Javi Merino1a3725a2014-03-31 18:35:15 +010022 if not os.path.isfile("trace.dat"):
23 raise IOError("No such file or directory: trace.dat")
24
Javi Merino572049d2014-03-31 16:45:23 +010025 with open(os.devnull) as devnull:
Javi Merinoee56c362014-03-31 17:30:34 +010026 out = check_output(["trace-cmd", "report"], stderr=devnull)
Javi Merino572049d2014-03-31 16:45:23 +010027
28 with open("trace.txt", "w") as f:
29 f.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010030
Javi Merino952815a2014-03-31 18:08:32 +010031 def __parse_into_csv(self):
32 """Create a csv representation of the thermal data and store it in self.data_csv"""
Javi Merinoc08ca682014-03-31 17:29:27 +010033 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
34 pat_data = re.compile(r"[A-Za-z0-9_]+=([0-9]+) ")
35 header = ""
36
Javi Merino952815a2014-03-31 18:08:32 +010037 with open("trace.txt") as fin:
38 for line in fin:
39 if not re.search("Ptot_out", line):
40 continue
41
42 line = line[:-1]
43
44 m = re.search(pat_timestamp, line)
45 timestamp = m.group(1)
46
47 semi_idx = line.index(" : ")
48 data_str = line[semi_idx + 3:]
49
50 if not header:
51 header = re.sub(r"([A-Za-z0-9_]+)=[0-9]+ ", r"\1,", data_str)
52 header = header[:-1]
53 header = "time," + header + "\n"
54 self.data_csv = header
55
56 parsed_data = re.sub(pat_data, r"\1,", data_str)
57 # Drop the last comma
58 parsed_data = parsed_data[:-1]
59
60 parsed_data = timestamp + "," + parsed_data + "\n"
61 self.data_csv += parsed_data
62
63 def write_thermal_csv(self):
64 """Write the csv info in thermal.csv"""
65 if not self.data_csv:
66 self.__parse_into_csv()
67
Javi Merinoc08ca682014-03-31 17:29:27 +010068 with open("thermal.csv", "w") as fout:
Javi Merino952815a2014-03-31 18:08:32 +010069 fout.write(self.data_csv)
Javi Merinof78ea5b2014-03-31 17:51:38 +010070
71 def get_data_frame(self):
72 """Return a pandas data frame for the run"""
73 if self.data_frame:
74 return self.data_frame
75
Javi Merino952815a2014-03-31 18:08:32 +010076 if not self.data_csv:
77 self.__parse_into_csv()
Javi Merinof78ea5b2014-03-31 17:51:38 +010078
Javi Merino952815a2014-03-31 18:08:32 +010079 self.data_frame = pd.read_csv(StringIO(self.data_csv)).set_index("time")
Javi Merinof78ea5b2014-03-31 17:51:38 +010080 return self.data_frame
Javi Merinodf8316a2014-03-31 18:39:42 +010081
Javi Merinoa6399fb2014-03-31 19:17:08 +010082 def __default_plot_settings(self, title=""):
83 plt.xlabel("Time")
84 if title:
85 plt.title(title)
86
Javi Merinodf8316a2014-03-31 18:39:42 +010087 def plot_temperature(self):
88 """Plot the temperature"""
89 df = self.get_data_frame()
90 (df["currT"] / 1000).plot()
Javi Merinoa6399fb2014-03-31 19:17:08 +010091 self.__default_plot_settings(title="Temperature")