blob: 1600a844bcb9e6415d73c422d42fefa5d7f40dde [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
Javi Merino7686f362014-04-02 11:26:40 +010010GOLDEN_RATIO = 1.618034
11
Javi Merinoc2ec5682014-04-01 15:16:06 +010012class BaseThermal(object):
Javi Merino68461552014-04-08 11:40:09 +010013 def __init__(self, basepath, unique_word):
14 if basepath is None:
15 basepath = "."
Javi Merinoc2ec5682014-04-01 15:16:06 +010016
Javi Merino68461552014-04-08 11:40:09 +010017 self.basepath = basepath
Javi Merino952815a2014-03-31 18:08:32 +010018 self.data_csv = ""
Javi Merinof78ea5b2014-03-31 17:51:38 +010019 self.data_frame = False
Javi Merino68461552014-04-08 11:40:09 +010020 self.unique_word = unique_word
21
22 if not os.path.isfile(os.path.join(basepath, "trace.txt")):
23 self.__run_trace_cmd_report()
Javi Merino572049d2014-03-31 16:45:23 +010024
Javi Merino572049d2014-03-31 16:45:23 +010025 def __run_trace_cmd_report(self):
26 """Run "trace-cmd report > trace.txt". Overwrites the contents of trace.txt if it exists."""
Javi Merinoee56c362014-03-31 17:30:34 +010027 from subprocess import check_output
28
Javi Merino68461552014-04-08 11:40:09 +010029 if not os.path.isfile(os.path.join(self.basepath, "trace.dat")):
Javi Merino1a3725a2014-03-31 18:35:15 +010030 raise IOError("No such file or directory: trace.dat")
31
Javi Merino68461552014-04-08 11:40:09 +010032 previous_path = os.getcwd()
33 os.chdir(self.basepath)
Javi Merino572049d2014-03-31 16:45:23 +010034
Javi Merino68461552014-04-08 11:40:09 +010035 # This would better be done with a context manager (i.e.
36 # http://stackoverflow.com/a/13197763/970766)
37 try:
38 with open(os.devnull) as devnull:
39 out = check_output(["trace-cmd", "report"], stderr=devnull)
40
41 finally:
42 os.chdir(previous_path)
43
44 with open(os.path.join(self.basepath, "trace.txt"), "w") as f:
Javi Merino572049d2014-03-31 16:45:23 +010045 f.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010046
Javi Merinoc2ec5682014-04-01 15:16:06 +010047 def parse_into_csv(self):
Javi Merino952815a2014-03-31 18:08:32 +010048 """Create a csv representation of the thermal data and store it in self.data_csv"""
Javi Merinoc08ca682014-03-31 17:29:27 +010049 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
Javi Merinoe284d632014-04-02 18:57:06 +010050 pat_data = re.compile(r"[A-Za-z0-9_]+=([-a-f0-9]+)")
51 pat_header = re.compile(r"([A-Za-z0-9_]+)=[-a-f0-9]+")
Javi Merinoc08ca682014-03-31 17:29:27 +010052 header = ""
53
Javi Merino68461552014-04-08 11:40:09 +010054 with open(os.path.join(self.basepath, "trace.txt")) as fin:
Javi Merino952815a2014-03-31 18:08:32 +010055 for line in fin:
Javi Merinoc2ec5682014-04-01 15:16:06 +010056 if not re.search(self.unique_word, line):
Javi Merino952815a2014-03-31 18:08:32 +010057 continue
58
59 line = line[:-1]
60
61 m = re.search(pat_timestamp, line)
62 timestamp = m.group(1)
63
Javi Merinoc2ec5682014-04-01 15:16:06 +010064 data_start_idx = re.search(r"[A-Za-z0-9_]+=", line).start()
65 data_str = line[data_start_idx:]
Javi Merino952815a2014-03-31 18:08:32 +010066
67 if not header:
Javi Merino0af47212014-04-02 16:23:23 +010068 header = re.sub(pat_header, r"\1", data_str)
69 header = re.sub(r" ", r",", header)
Javi Merino952815a2014-03-31 18:08:32 +010070 header = "time," + header + "\n"
71 self.data_csv = header
72
Javi Merino0af47212014-04-02 16:23:23 +010073 parsed_data = re.sub(pat_data, r"\1", data_str)
74 parsed_data = re.sub(r" ", r",", parsed_data)
Javi Merino952815a2014-03-31 18:08:32 +010075
76 parsed_data = timestamp + "," + parsed_data + "\n"
77 self.data_csv += parsed_data
78
Javi Merinof78ea5b2014-03-31 17:51:38 +010079 def get_data_frame(self):
80 """Return a pandas data frame for the run"""
81 if self.data_frame:
82 return self.data_frame
83
Javi Merino952815a2014-03-31 18:08:32 +010084 if not self.data_csv:
Javi Merinoc2ec5682014-04-01 15:16:06 +010085 self.parse_into_csv()
Javi Merinof78ea5b2014-03-31 17:51:38 +010086
Javi Merino04f27492014-04-02 09:59:23 +010087 try:
88 self.data_frame = pd.read_csv(StringIO(self.data_csv)).set_index("time")
89 except StopIteration:
90 if not self.data_frame:
91 return pd.DataFrame()
92 raise
93
Javi Merinof78ea5b2014-03-31 17:51:38 +010094 return self.data_frame
Javi Merinodf8316a2014-03-31 18:39:42 +010095
Javi Merinofb2e8fd2014-04-08 12:27:38 +010096def set_plot_size(width, height):
97 """Set the plot size.
98
99 This has to be called before calls to .plot()
100 """
101 if height is None:
102 if width is None:
103 height = 6
104 width = 10
105 else:
106 height = width / GOLDEN_RATIO
107 else:
108 if width is None:
109 width = height * GOLDEN_RATIO
110
111 plt.figure(figsize=(width, height))
112
Javi Merinoc2ec5682014-04-01 15:16:06 +0100113class Thermal(BaseThermal):
Javi Merino68461552014-04-08 11:40:09 +0100114 def __init__(self, path=None):
Javi Merinoc2ec5682014-04-01 15:16:06 +0100115 super(Thermal, self).__init__(
Javi Merino68461552014-04-08 11:40:09 +0100116 basepath=path,
117 unique_word="Ptot_out",
Javi Merinoc2ec5682014-04-01 15:16:06 +0100118 )
119
120 def write_thermal_csv(self):
121 """Write the csv info in thermal.csv"""
122 if not self.data_csv:
123 self.parse_into_csv()
124
125 with open("thermal.csv", "w") as fout:
126 fout.write(self.data_csv)
127
Javi Merinoa6399fb2014-03-31 19:17:08 +0100128 def __default_plot_settings(self, title=""):
Javi Merino1b74ef12014-04-02 11:27:01 +0100129 """Set xlabel and title of the plot
130
131 This has to be called after the plot() command
132 """
133
Javi Merinoa6399fb2014-03-31 19:17:08 +0100134 plt.xlabel("Time")
135 if title:
136 plt.title(title)
137
Javi Merino7686f362014-04-02 11:26:40 +0100138 def plot_temperature(self, width=None, height=None):
Javi Merinodf8316a2014-03-31 18:39:42 +0100139 """Plot the temperature"""
Javi Merinofb2e8fd2014-04-08 12:27:38 +0100140 set_plot_size(width, height)
Javi Merinodf8316a2014-03-31 18:39:42 +0100141 df = self.get_data_frame()
142 (df["currT"] / 1000).plot()
Javi Merinoa6399fb2014-03-31 19:17:08 +0100143 self.__default_plot_settings(title="Temperature")
Javi Merino749b26a2014-03-31 19:17:30 +0100144
Javi Merinof1dcb2d2014-04-02 16:52:11 +0100145 def plot_multivalue(self, values, title, width, height):
146 """Plot multiple values of the DataFrame
147
148 values is an array with the keys of the DataFrame to plot
149 """
Javi Merinofb2e8fd2014-04-08 12:27:38 +0100150 set_plot_size(width, height)
Javi Merino749b26a2014-03-31 19:17:30 +0100151 df = self.get_data_frame()
Javi Merinof1dcb2d2014-04-02 16:52:11 +0100152 df[values].plot()
153 self.__default_plot_settings(title=title)
154
155 def plot_input_power(self, width=None, height=None):
156 """Plot input power"""
157 self.plot_multivalue( ["Pa7_in", "Pa15_in", "Pgpu_in"], "Input Power", width, height)
Javi Merino9c010772014-04-02 16:54:41 +0100158
159 def plot_output_power(self, width=None, height=None):
160 """Plot output power"""
161 self.plot_multivalue( ["Pa7_out", "Pa15_out", "Pgpu_out"], "Output Power", width, height)