blob: 5e569e997c47b26be851e0bd9bde002eaf59adfc [file] [log] [blame]
Javi Merino491cf732014-03-31 17:34:44 +01001#!/usr/bin/python
Javi Merino5bd3d442014-04-08 12:55:13 +01002"""Process the output of the power allocator trace in the current
3directory's trace.dat"""
Javi Merino572049d2014-03-31 16:45:23 +01004
5import os
Javi Merinoee56c362014-03-31 17:30:34 +01006import re
Javi Merino952815a2014-03-31 18:08:32 +01007from StringIO import StringIO
Javi Merinof78ea5b2014-03-31 17:51:38 +01008import pandas as pd
Javi Merinoa6399fb2014-03-31 19:17:08 +01009from matplotlib import pyplot as plt
Javi Merino572049d2014-03-31 16:45:23 +010010
Javi Merino7686f362014-04-02 11:26:40 +010011GOLDEN_RATIO = 1.618034
12
Javi Merinoa2c990a2014-06-10 15:33:49 +010013def set_plot_size(width, height):
14 """Set the plot size.
15
16 This has to be called before calls to .plot()
17 """
18 if height is None:
19 if width is None:
20 height = 6
21 width = 10
22 else:
23 height = width / GOLDEN_RATIO
24 else:
25 if width is None:
26 width = height * GOLDEN_RATIO
27
28 plt.figure(figsize=(width, height))
29
Javi Merinoc2ec5682014-04-01 15:16:06 +010030class BaseThermal(object):
Javi Merino5bd3d442014-04-08 12:55:13 +010031 """Base class to parse trace.dat dumps.
32
33 Don't use directly, create a subclass that defines the unique_word
34 you want to match in the output"""
Javi Merino68461552014-04-08 11:40:09 +010035 def __init__(self, basepath, unique_word):
36 if basepath is None:
37 basepath = "."
Javi Merinoc2ec5682014-04-01 15:16:06 +010038
Javi Merino68461552014-04-08 11:40:09 +010039 self.basepath = basepath
Javi Merino952815a2014-03-31 18:08:32 +010040 self.data_csv = ""
Javi Merinof78ea5b2014-03-31 17:51:38 +010041 self.data_frame = False
Javi Merino68461552014-04-08 11:40:09 +010042 self.unique_word = unique_word
Javi Merinoa2c990a2014-06-10 15:33:49 +010043 self.ax = None
Javi Merino68461552014-04-08 11:40:09 +010044
45 if not os.path.isfile(os.path.join(basepath, "trace.txt")):
46 self.__run_trace_cmd_report()
Javi Merino572049d2014-03-31 16:45:23 +010047
Javi Merino572049d2014-03-31 16:45:23 +010048 def __run_trace_cmd_report(self):
Javi Merino5bd3d442014-04-08 12:55:13 +010049 """Run "trace-cmd report > trace.txt".
50
51 Overwrites the contents of trace.txt if it exists."""
Javi Merinoee56c362014-03-31 17:30:34 +010052 from subprocess import check_output
53
Javi Merino68461552014-04-08 11:40:09 +010054 if not os.path.isfile(os.path.join(self.basepath, "trace.dat")):
Javi Merino1a3725a2014-03-31 18:35:15 +010055 raise IOError("No such file or directory: trace.dat")
56
Javi Merino68461552014-04-08 11:40:09 +010057 previous_path = os.getcwd()
58 os.chdir(self.basepath)
Javi Merino572049d2014-03-31 16:45:23 +010059
Javi Merino68461552014-04-08 11:40:09 +010060 # This would better be done with a context manager (i.e.
61 # http://stackoverflow.com/a/13197763/970766)
62 try:
63 with open(os.devnull) as devnull:
64 out = check_output(["trace-cmd", "report"], stderr=devnull)
65
66 finally:
67 os.chdir(previous_path)
68
Javi Merino5bd3d442014-04-08 12:55:13 +010069 with open(os.path.join(self.basepath, "trace.txt"), "w") as fout:
70 fout.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010071
Javi Merinoc2ec5682014-04-01 15:16:06 +010072 def parse_into_csv(self):
Javi Merino5bd3d442014-04-08 12:55:13 +010073 """Create a csv representation of the thermal data and store
74 it in self.data_csv"""
Javi Merinoc08ca682014-03-31 17:29:27 +010075 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
Javi Merino0e83b612014-06-05 11:45:23 +010076 pat_data = re.compile(r"[A-Za-z0-9_]+=([^ ]+)")
77 pat_header = re.compile(r"([A-Za-z0-9_]+)=[^ ]+")
Javi Merinoc08ca682014-03-31 17:29:27 +010078 header = ""
79
Javi Merino68461552014-04-08 11:40:09 +010080 with open(os.path.join(self.basepath, "trace.txt")) as fin:
Javi Merino952815a2014-03-31 18:08:32 +010081 for line in fin:
Javi Merinoc2ec5682014-04-01 15:16:06 +010082 if not re.search(self.unique_word, line):
Javi Merino952815a2014-03-31 18:08:32 +010083 continue
84
85 line = line[:-1]
86
Javi Merino5bd3d442014-04-08 12:55:13 +010087 timestamp_match = re.search(pat_timestamp, line)
88 timestamp = timestamp_match.group(1)
Javi Merino952815a2014-03-31 18:08:32 +010089
Javi Merinoc2ec5682014-04-01 15:16:06 +010090 data_start_idx = re.search(r"[A-Za-z0-9_]+=", line).start()
91 data_str = line[data_start_idx:]
Javi Merino952815a2014-03-31 18:08:32 +010092
93 if not header:
Javi Merino0af47212014-04-02 16:23:23 +010094 header = re.sub(pat_header, r"\1", data_str)
95 header = re.sub(r" ", r",", header)
Javi Merino952815a2014-03-31 18:08:32 +010096 header = "time," + header + "\n"
97 self.data_csv = header
98
Javi Merino0af47212014-04-02 16:23:23 +010099 parsed_data = re.sub(pat_data, r"\1", data_str)
100 parsed_data = re.sub(r" ", r",", parsed_data)
Javi Merino952815a2014-03-31 18:08:32 +0100101
102 parsed_data = timestamp + "," + parsed_data + "\n"
103 self.data_csv += parsed_data
104
Javi Merinof78ea5b2014-03-31 17:51:38 +0100105 def get_data_frame(self):
106 """Return a pandas data frame for the run"""
Javi Merino92f1a6d2014-04-10 16:30:36 +0100107 if self.data_frame is None:
Javi Merinof78ea5b2014-03-31 17:51:38 +0100108 return self.data_frame
109
Javi Merino952815a2014-03-31 18:08:32 +0100110 if not self.data_csv:
Javi Merinoc2ec5682014-04-01 15:16:06 +0100111 self.parse_into_csv()
Javi Merinof78ea5b2014-03-31 17:51:38 +0100112
Javi Merinof0f51ff2014-04-10 12:34:53 +0100113 if self.data_csv is "":
114 return pd.DataFrame()
115
116 unordered_df = pd.read_csv(StringIO(self.data_csv))
117 self.data_frame = unordered_df.set_index("time")
Javi Merino04f27492014-04-02 09:59:23 +0100118
Javi Merinof78ea5b2014-03-31 17:51:38 +0100119 return self.data_frame
Javi Merinodf8316a2014-03-31 18:39:42 +0100120
Javi Merinoa2c990a2014-06-10 15:33:49 +0100121 def init_fig(self, width=None, height=None, ylim=None):
122 """initialize a figure
Javi Merinofb2e8fd2014-04-08 12:27:38 +0100123
Javi Merinoa2c990a2014-06-10 15:33:49 +0100124 width and height are numbers, ylim is a tuple with min and max values
125 for the y axis"""
126 set_plot_size(width, height)
Javi Merinofb2e8fd2014-04-08 12:27:38 +0100127
Javi Merinoa2c990a2014-06-10 15:33:49 +0100128 _, self.ax = plt.subplots()
129
130 if (ylim):
131 self.ax.set_ylim(*ylim)
Javi Merinofb2e8fd2014-04-08 12:27:38 +0100132
Javi Merino05983ef2014-04-08 12:54:20 +0100133def default_plot_settings(title=""):
134 """Set xlabel and title of the plot
135
136 This has to be called after calls to .plot()
137 """
138
139 plt.xlabel("Time")
140 if title:
141 plt.title(title)
142
Javi Merino7a7cd702014-04-14 15:41:15 +0100143def normalize_title(title, opt_title):
144 """
145 Return a string with that contains the title and opt_title if it's not the empty string
146
147 See test_normalize_title() for usage
148 """
149 if opt_title is not "":
150 title = opt_title + " - " + title
151
152 return title
153
Javi Merino0e83b612014-06-05 11:45:23 +0100154class Thermal(BaseThermal):
155 """Process the thermal framework data in a ftrace dump"""
156 def __init__(self, path=None):
157 super(Thermal, self).__init__(
158 basepath=path,
159 unique_word="thermal_zone=",
160 )
161
Javi Merinoa2c990a2014-06-10 15:33:49 +0100162 def plot_temperature(self, title="", width=None, height=None, ylim=None):
Javi Merinoc68737a2014-06-10 15:21:59 +0100163 """Plot the temperature"""
164 dfr = self.get_data_frame()
165 title = normalize_title("Temperature", title)
166
Javi Merinoa2c990a2014-06-10 15:33:49 +0100167 self.init_fig(width, height, ylim)
Javi Merinoc68737a2014-06-10 15:21:59 +0100168
Javi Merinoa2c990a2014-06-10 15:33:49 +0100169 (dfr["temp"] / 1000).plot(ax=self.ax)
Javi Merinoc68737a2014-06-10 15:21:59 +0100170
171 default_plot_settings(title=title)
172 plt.legend()
173
Javi Merino1e69e2c2014-06-04 18:25:09 +0100174class ThermalGovernor(BaseThermal):
Javi Merino5bd3d442014-04-08 12:55:13 +0100175 """Process the power allocator data in a ftrace dump"""
Javi Merino68461552014-04-08 11:40:09 +0100176 def __init__(self, path=None):
Javi Merino1e69e2c2014-06-04 18:25:09 +0100177 super(ThermalGovernor, self).__init__(
Javi Merino68461552014-04-08 11:40:09 +0100178 basepath=path,
179 unique_word="Ptot_out",
Javi Merinoc2ec5682014-04-01 15:16:06 +0100180 )
181
182 def write_thermal_csv(self):
183 """Write the csv info in thermal.csv"""
184 if not self.data_csv:
185 self.parse_into_csv()
186
187 with open("thermal.csv", "w") as fout:
188 fout.write(self.data_csv)
189
Javi Merinoa2c990a2014-06-10 15:33:49 +0100190 def plot_temperature(self, title="", width=None, height=None, ylim=None):
Javi Merinodf8316a2014-03-31 18:39:42 +0100191 """Plot the temperature"""
Javi Merino5bd3d442014-04-08 12:55:13 +0100192 dfr = self.get_data_frame()
Javi Merino9e186092014-04-08 15:41:41 +0100193 control_temp_series = (dfr["currT"] + dfr["deltaT"]) / 1000
Javi Merino7a7cd702014-04-14 15:41:15 +0100194 title = normalize_title("Temperature", title)
Javi Merino9e186092014-04-08 15:41:41 +0100195
Javi Merinoa2c990a2014-06-10 15:33:49 +0100196 self.init_fig(width, height, ylim)
Javi Merino9e186092014-04-08 15:41:41 +0100197
Javi Merinoa2c990a2014-06-10 15:33:49 +0100198 (dfr["currT"] / 1000).plot(ax=self.ax)
199 control_temp_series.plot(ax=self.ax, color="y", linestyle="--",
Javi Merino9e186092014-04-08 15:41:41 +0100200 label="control temperature")
201
Javi Merinoa3553e92014-04-08 15:57:08 +0100202 default_plot_settings(title=title)
Javi Merino9e186092014-04-08 15:41:41 +0100203 plt.legend()
Javi Merino749b26a2014-03-31 19:17:30 +0100204
Javi Merinof1dcb2d2014-04-02 16:52:11 +0100205 def plot_multivalue(self, values, title, width, height):
206 """Plot multiple values of the DataFrame
207
208 values is an array with the keys of the DataFrame to plot
209 """
Javi Merinofb2e8fd2014-04-08 12:27:38 +0100210 set_plot_size(width, height)
Javi Merino5bd3d442014-04-08 12:55:13 +0100211 dfr = self.get_data_frame()
212 dfr[values].plot()
Javi Merino05983ef2014-04-08 12:54:20 +0100213 default_plot_settings(title=title)
Javi Merinof1dcb2d2014-04-02 16:52:11 +0100214
Javi Merinoc00feff2014-04-14 15:41:51 +0100215 def plot_input_power(self, title="", width=None, height=None):
Javi Merinof1dcb2d2014-04-02 16:52:11 +0100216 """Plot input power"""
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100217 dfr = self.get_data_frame()
218 in_cols = [s for s in dfr.columns
219 if re.match("P.*_in", s) and s != "Ptot_in"]
220
Javi Merinoc00feff2014-04-14 15:41:51 +0100221 title = normalize_title("Input Power", title)
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100222 self.plot_multivalue(in_cols, title, width, height)
Javi Merino9c010772014-04-02 16:54:41 +0100223
Javi Merinoc00feff2014-04-14 15:41:51 +0100224 def plot_output_power(self, title="", width=None, height=None):
Javi Merino9c010772014-04-02 16:54:41 +0100225 """Plot output power"""
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100226 dfr = self.get_data_frame()
227 out_cols = [s for s in dfr.columns
228 if re.match("P.*_out", s) and s != "Ptot_out"]
229
Javi Merinoc00feff2014-04-14 15:41:51 +0100230 title = normalize_title("Output Power", title)
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100231 self.plot_multivalue(out_cols,
Javi Merinoc00feff2014-04-14 15:41:51 +0100232 title, width, height)
Javi Merinocd4a8272014-04-14 15:50:01 +0100233
Javi Merino9fc54852014-05-07 19:06:53 +0100234 def plot_inout_power(self, title="", width=None, height=None):
235 """Make multiple plots showing input and output power for each actor"""
236 dfr = self.get_data_frame()
237
238 actors = []
239 for col in dfr.columns:
240 match = re.match("P(.*)_in", col)
241 if match and col != "Ptot_in":
242 actors.append(match.group(1))
243
244 for actor in actors:
245 cols = ["P" + actor + "_in", "P" + actor + "_out"]
246 this_title = normalize_title(actor, title)
247 dfr[cols].plot(title=this_title)
248
Javi Merinocd4a8272014-04-14 15:50:01 +0100249 def summary_plots(self, **kwords):
250 self.plot_temperature(**kwords)
251 self.plot_input_power(**kwords)
252 self.plot_output_power(**kwords)