blob: 96d0ae06ca17a69d37e5e7aa1b7ad1ac90c1e104 [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 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 Merino1a3dca32014-08-11 16:12:50 +010010from plot_utils import normalize_title, pre_plot_setup, post_plot_setup, plot_hist
Javi Merino51db3632014-06-13 11:24:51 +010011
Javi Merino9ce2fb62014-07-04 20:02:13 +010012def trace_parser_explode_array(string, array_lengths):
Javi Merinob2ff5692014-06-30 17:41:50 +010013 """Explode an array in the trace into individual elements for easy parsing
14
15 Basically, turn "load={1 1 2 2}" into "load0=1 load1=1 load2=2
Javi Merino9ce2fb62014-07-04 20:02:13 +010016 load3=2". array_lengths is a dictionary of array names and their
17 expected length. If we get array that's shorter than the expected
18 length, additional keys have to be introduced with value 0 to
19 compensate. For example, "load={1 2}" with array_lengths being
20 {"load": 4} returns "load0=1 load1=2 load2=0 load3=0"
Javi Merinob2ff5692014-06-30 17:41:50 +010021
22 """
23
Javi Merino9ce2fb62014-07-04 20:02:13 +010024 while True:
Javi Merinod44312f2014-07-02 18:34:24 +010025 match = re.search(r"[^ ]+={[^}]+}", string)
26 if match is None:
27 break
Javi Merinob2ff5692014-06-30 17:41:50 +010028
Javi Merinod44312f2014-07-02 18:34:24 +010029 to_explode = match.group()
30 col_basename = re.match(r"([^=]+)=", to_explode).groups()[0]
31 vals_str = re.search(r"{(.+)}", to_explode).groups()[0]
32 vals_array = vals_str.split(' ')
Javi Merinob2ff5692014-06-30 17:41:50 +010033
Javi Merinod44312f2014-07-02 18:34:24 +010034 exploded_str = ""
35 for (idx, val) in enumerate(vals_array):
36 exploded_str += "{}{}={} ".format(col_basename, idx, val)
Javi Merinob2ff5692014-06-30 17:41:50 +010037
Javi Merino9ce2fb62014-07-04 20:02:13 +010038 vals_added = len(vals_array)
39 if vals_added < array_lengths[col_basename]:
40 for idx in range(vals_added, array_lengths[col_basename]):
41 exploded_str += "{}{}=0 ".format(col_basename, idx)
42
Javi Merinod44312f2014-07-02 18:34:24 +010043 exploded_str = exploded_str[:-1]
44 begin_idx = match.start()
45 end_idx = match.end()
Javi Merinob2ff5692014-06-30 17:41:50 +010046
Javi Merinod44312f2014-07-02 18:34:24 +010047 string = string[:begin_idx] + exploded_str + string[end_idx:]
48
49 return string
Javi Merinob2ff5692014-06-30 17:41:50 +010050
Javi Merinoc2ec5682014-04-01 15:16:06 +010051class BaseThermal(object):
Javi Merino5bd3d442014-04-08 12:55:13 +010052 """Base class to parse trace.dat dumps.
53
54 Don't use directly, create a subclass that defines the unique_word
55 you want to match in the output"""
Javi Merino68461552014-04-08 11:40:09 +010056 def __init__(self, basepath, unique_word):
57 if basepath is None:
58 basepath = "."
Javi Merinoc2ec5682014-04-01 15:16:06 +010059
Javi Merino68461552014-04-08 11:40:09 +010060 self.basepath = basepath
Javi Merino0df4de72014-08-11 11:34:40 +010061 self.data_frame = pd.DataFrame()
Javi Merino68461552014-04-08 11:40:09 +010062 self.unique_word = unique_word
63
64 if not os.path.isfile(os.path.join(basepath, "trace.txt")):
65 self.__run_trace_cmd_report()
Javi Merino572049d2014-03-31 16:45:23 +010066
Javi Merinod6284da2014-08-14 12:12:13 +010067 self.__parse_into_dataframe()
Javi Merinod5562282014-08-08 17:28:03 +010068
Javi Merino572049d2014-03-31 16:45:23 +010069 def __run_trace_cmd_report(self):
Javi Merino5bd3d442014-04-08 12:55:13 +010070 """Run "trace-cmd report > trace.txt".
71
72 Overwrites the contents of trace.txt if it exists."""
Javi Merinoee56c362014-03-31 17:30:34 +010073 from subprocess import check_output
74
Javi Merinod24c27d2014-08-20 10:55:43 +010075 trace_fname = os.path.join(self.basepath, "trace.dat")
76 if not os.path.isfile(trace_fname):
77 raise IOError("No such file or directory: {}".format(trace_fname))
Javi Merino1a3725a2014-03-31 18:35:15 +010078
Javi Merino68461552014-04-08 11:40:09 +010079 previous_path = os.getcwd()
80 os.chdir(self.basepath)
Javi Merino572049d2014-03-31 16:45:23 +010081
Javi Merino68461552014-04-08 11:40:09 +010082 # This would better be done with a context manager (i.e.
83 # http://stackoverflow.com/a/13197763/970766)
84 try:
85 with open(os.devnull) as devnull:
86 out = check_output(["trace-cmd", "report"], stderr=devnull)
87
88 finally:
89 os.chdir(previous_path)
90
Javi Merino5bd3d442014-04-08 12:55:13 +010091 with open(os.path.join(self.basepath, "trace.txt"), "w") as fout:
92 fout.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010093
Javi Merino9ce2fb62014-07-04 20:02:13 +010094 def get_trace_array_lengths(self, fname):
95 """Calculate the lengths of all arrays in the trace
96
97 Returns a dict with the name of each array found in the trace
98 as keys and their corresponding length as value
99
100 """
Javi Merinoa3e98c82014-08-01 14:38:22 +0100101 from collections import defaultdict
Javi Merino9ce2fb62014-07-04 20:02:13 +0100102
103 pat_array = re.compile(r"([A-Za-z0-9_]+)={([^}]+)}")
104
Javi Merinoa3e98c82014-08-01 14:38:22 +0100105 ret = defaultdict(int)
Javi Merino9ce2fb62014-07-04 20:02:13 +0100106
107 with open(fname) as fin:
108 for line in fin:
109 if not re.search(self.unique_word, line):
110 continue
111
112 while True:
113 match = re.search(pat_array, line)
114 if not match:
115 break
116
117 (array_name, array_elements) = match.groups()
118
119 array_len = len(array_elements.split(' '))
120
Javi Merinoa3e98c82014-08-01 14:38:22 +0100121 if array_len > ret[array_name]:
Javi Merino9ce2fb62014-07-04 20:02:13 +0100122 ret[array_name] = array_len
123
124 line = line[match.end():]
125
126 return ret
127
Javi Merinod6284da2014-08-14 12:12:13 +0100128 def __parse_into_dataframe(self):
129 """parse the trace and create a pandas DataFrame"""
Javi Merino9ce2fb62014-07-04 20:02:13 +0100130
131 fin_fname = os.path.join(self.basepath, "trace.txt")
132
133 array_lengths = self.get_trace_array_lengths(fin_fname)
134
Javi Merinoc08ca682014-03-31 17:29:27 +0100135 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
Javi Merinod6284da2014-08-14 12:12:13 +0100136 pat_data_start = re.compile("[A-Za-z0-9_]+=")
Javi Merino45a59c32014-07-02 19:21:17 +0100137 pat_empty_array = re.compile(r"[A-Za-z0-9_]+=\{\} ")
Javi Merinod6284da2014-08-14 12:12:13 +0100138
139 parsed_data = []
140 time_array = []
Javi Merinoc08ca682014-03-31 17:29:27 +0100141
Javi Merino9ce2fb62014-07-04 20:02:13 +0100142 with open(fin_fname) as fin:
Javi Merino952815a2014-03-31 18:08:32 +0100143 for line in fin:
Javi Merinoc2ec5682014-04-01 15:16:06 +0100144 if not re.search(self.unique_word, line):
Javi Merino952815a2014-03-31 18:08:32 +0100145 continue
146
147 line = line[:-1]
148
Javi Merino5bd3d442014-04-08 12:55:13 +0100149 timestamp_match = re.search(pat_timestamp, line)
Javi Merinod6284da2014-08-14 12:12:13 +0100150 timestamp = float(timestamp_match.group(1))
151 time_array.append(timestamp)
Javi Merino952815a2014-03-31 18:08:32 +0100152
Javi Merinod6284da2014-08-14 12:12:13 +0100153 data_start_idx = re.search(pat_data_start, line).start()
Javi Merinoc2ec5682014-04-01 15:16:06 +0100154 data_str = line[data_start_idx:]
Javi Merino952815a2014-03-31 18:08:32 +0100155
Javi Merino45a59c32014-07-02 19:21:17 +0100156 # Remove empty arrays from the trace
157 data_str = re.sub(pat_empty_array, r"", data_str)
158
Javi Merino9ce2fb62014-07-04 20:02:13 +0100159 data_str = trace_parser_explode_array(data_str, array_lengths)
Javi Merinocfb49b72014-06-30 17:51:51 +0100160
Javi Merinod6284da2014-08-14 12:12:13 +0100161 line_data = {}
162 for field in data_str.split():
163 (key, value) = field.split('=')
164 try:
165 value = int(value)
166 except ValueError:
167 pass
168 line_data[key] = value
Javi Merino952815a2014-03-31 18:08:32 +0100169
Javi Merinod6284da2014-08-14 12:12:13 +0100170 parsed_data.append(line_data)
Javi Merino952815a2014-03-31 18:08:32 +0100171
Javi Merinod6284da2014-08-14 12:12:13 +0100172 time_idx = pd.Index(time_array, name="Time")
173 self.data_frame = pd.DataFrame(parsed_data, index=time_idx)
Javi Merinodf8316a2014-03-31 18:39:42 +0100174
Javi Merinof6023f02014-08-13 18:22:53 +0100175 def write_csv(self, fname):
176 """Write the csv info in thermal.csv"""
Javi Merinod6284da2014-08-14 12:12:13 +0100177 self.data_frame.to_csv(fname)
Javi Merinof6023f02014-08-13 18:22:53 +0100178
Javi Merino4d28cbc2014-08-11 11:19:48 +0100179 def normalize_time(self, basetime):
180 """Substract basetime from the Time of the data frame"""
181 self.data_frame.reset_index(inplace=True)
182 self.data_frame["Time"] = self.data_frame["Time"] - basetime
183 self.data_frame.set_index("Time", inplace=True)
184
Javi Merino0e83b612014-06-05 11:45:23 +0100185class Thermal(BaseThermal):
186 """Process the thermal framework data in a ftrace dump"""
187 def __init__(self, path=None):
188 super(Thermal, self).__init__(
189 basepath=path,
Javi Merinoaf45d872014-07-03 09:49:40 +0100190 unique_word="thermal_temperature:",
Javi Merino0e83b612014-06-05 11:45:23 +0100191 )
192
Javi Merino516d5942014-06-26 15:06:04 +0100193 def plot_temperature(self, control_temperature=None, title="", width=None,
Javi Merino80114162014-08-08 16:48:32 +0100194 height=None, ylim="range", ax=None, legend_label=""):
Javi Merino516d5942014-06-26 15:06:04 +0100195 """Plot the temperature.
196
197 If control_temp is a pd.Series() representing the (possible)
198 variation of control_temp during the run, draw it using a
199 dashed yellow line. Otherwise, only the temperature is
200 plotted.
201
202 """
Javi Merinoc68737a2014-06-10 15:21:59 +0100203 title = normalize_title("Temperature", title)
204
Javi Merino49cbcfe2014-08-08 16:03:49 +0100205 setup_plot = False
206 if not ax:
207 ax = pre_plot_setup(width, height)
208 setup_plot = True
209
Javi Merino80114162014-08-08 16:48:32 +0100210 temp_label = normalize_title("Temperature", legend_label)
Javi Merino92f4d012014-08-08 17:55:32 +0100211 (self.data_frame["temp"] / 1000).plot(ax=ax, label=temp_label)
Javi Merino516d5942014-06-26 15:06:04 +0100212 if control_temperature is not None:
Javi Merino80114162014-08-08 16:48:32 +0100213 ct_label = normalize_title("Control", legend_label)
Javi Merino516d5942014-06-26 15:06:04 +0100214 control_temperature.plot(ax=ax, color="y", linestyle="--",
Javi Merino80114162014-08-08 16:48:32 +0100215 label=ct_label)
Javi Merinoc68737a2014-06-10 15:21:59 +0100216
Javi Merino49cbcfe2014-08-08 16:03:49 +0100217 if setup_plot:
218 post_plot_setup(ax, title=title, ylim=ylim)
219 plt.legend()
Javi Merinoc68737a2014-06-10 15:21:59 +0100220
Javi Merinoe5ea60a2014-08-12 16:41:42 +0100221 def plot_temperature_hist(self, ax, title):
Javi Merino1a3dca32014-08-11 16:12:50 +0100222 """Plot a temperature histogram"""
223
224 temps = self.data_frame["temp"] / 1000
225 title = normalize_title("Temperature", title)
226 xlim = (0, temps.max())
227
Javi Merinoe5ea60a2014-08-12 16:41:42 +0100228 plot_hist(temps, ax, title, 30, "Temperature", xlim, "default")
Javi Merino1a3dca32014-08-11 16:12:50 +0100229
Javi Merino1e69e2c2014-06-04 18:25:09 +0100230class ThermalGovernor(BaseThermal):
Javi Merino5bd3d442014-04-08 12:55:13 +0100231 """Process the power allocator data in a ftrace dump"""
Javi Merino68461552014-04-08 11:40:09 +0100232 def __init__(self, path=None):
Javi Merino1e69e2c2014-06-04 18:25:09 +0100233 super(ThermalGovernor, self).__init__(
Javi Merino68461552014-04-08 11:40:09 +0100234 basepath=path,
Javi Merinoa67b86f2014-07-03 15:44:19 +0100235 unique_word="thermal_power_allocator:",
Javi Merinoc2ec5682014-04-01 15:16:06 +0100236 )
237
Javi Merino29223812014-08-12 15:24:43 +0100238 def plot_input_power(self, actor_order, title="", width=None, height=None, ax=None):
Javi Merinod6d5f892014-07-03 16:24:23 +0100239 """Plot input power
240
241 actor_order is an array with the order in which the actors were registered.
242 """
243
Javi Merino92f4d012014-08-08 17:55:32 +0100244 dfr = self.data_frame
Javi Merinof7968a72014-07-03 15:35:02 +0100245 in_cols = [s for s in dfr.columns if re.match("req_power[0-9]+", s)]
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100246
Javi Merinod6d5f892014-07-03 16:24:23 +0100247 plot_dfr = dfr[in_cols]
248 # Rename the columns from "req_power0" to "A15" or whatever is
249 # in actor_order. Note that we can do it just with an
250 # assignment because the columns are already sorted (i.e.:
251 # req_power0, req_power1...)
252 plot_dfr.columns = actor_order
253
Javi Merinoc00feff2014-04-14 15:41:51 +0100254 title = normalize_title("Input Power", title)
Javi Merino8ecd8172014-07-03 16:09:01 +0100255
Javi Merino29223812014-08-12 15:24:43 +0100256 if not ax:
257 ax = pre_plot_setup(width, height)
258
Javi Merinod6d5f892014-07-03 16:24:23 +0100259 plot_dfr.plot(ax=ax)
Javi Merino8ecd8172014-07-03 16:09:01 +0100260 post_plot_setup(ax, title=title)
Javi Merino9c010772014-04-02 16:54:41 +0100261
Javi Merino6003b252014-08-12 15:32:17 +0100262 def plot_output_power(self, actor_order, title="", width=None, height=None, ax=None):
Javi Merinod6d5f892014-07-03 16:24:23 +0100263 """Plot output power
264
265 actor_order is an array with the order in which the actors were registered.
266 """
267
Javi Merino92f4d012014-08-08 17:55:32 +0100268 out_cols = [s for s in self.data_frame.columns
Javi Merinof7968a72014-07-03 15:35:02 +0100269 if re.match("granted_power[0-9]+", s)]
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100270
Javi Merinod6d5f892014-07-03 16:24:23 +0100271 # See the note in plot_input_power()
Javi Merino92f4d012014-08-08 17:55:32 +0100272 plot_dfr = self.data_frame[out_cols]
Javi Merinod6d5f892014-07-03 16:24:23 +0100273 plot_dfr.columns = actor_order
274
Javi Merinoc00feff2014-04-14 15:41:51 +0100275 title = normalize_title("Output Power", title)
Javi Merino8ecd8172014-07-03 16:09:01 +0100276
Javi Merino6003b252014-08-12 15:32:17 +0100277 if not ax:
278 ax = pre_plot_setup(width, height)
279
Javi Merinod6d5f892014-07-03 16:24:23 +0100280 plot_dfr.plot(ax=ax)
Javi Merino8ecd8172014-07-03 16:09:01 +0100281 post_plot_setup(ax, title=title)
Javi Merinocd4a8272014-04-14 15:50:01 +0100282
Javi Merino9fc54852014-05-07 19:06:53 +0100283 def plot_inout_power(self, title="", width=None, height=None):
284 """Make multiple plots showing input and output power for each actor"""
Javi Merino92f4d012014-08-08 17:55:32 +0100285 dfr = self.data_frame
Javi Merino9fc54852014-05-07 19:06:53 +0100286
287 actors = []
288 for col in dfr.columns:
289 match = re.match("P(.*)_in", col)
290 if match and col != "Ptot_in":
291 actors.append(match.group(1))
292
293 for actor in actors:
294 cols = ["P" + actor + "_in", "P" + actor + "_out"]
295 this_title = normalize_title(actor, title)
296 dfr[cols].plot(title=this_title)