blob: 86e8590d5091fee01867c58fac5e26dd5920781d [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 Merino20066a02014-08-20 10:59:36 +010079 with open(os.devnull) as devnull:
80 out = check_output(["trace-cmd", "report", trace_fname],
81 stderr=devnull)
Javi Merino68461552014-04-08 11:40:09 +010082
Javi Merino5bd3d442014-04-08 12:55:13 +010083 with open(os.path.join(self.basepath, "trace.txt"), "w") as fout:
84 fout.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010085
Javi Merino9ce2fb62014-07-04 20:02:13 +010086 def get_trace_array_lengths(self, fname):
87 """Calculate the lengths of all arrays in the trace
88
89 Returns a dict with the name of each array found in the trace
90 as keys and their corresponding length as value
91
92 """
Javi Merinoa3e98c82014-08-01 14:38:22 +010093 from collections import defaultdict
Javi Merino9ce2fb62014-07-04 20:02:13 +010094
95 pat_array = re.compile(r"([A-Za-z0-9_]+)={([^}]+)}")
96
Javi Merinoa3e98c82014-08-01 14:38:22 +010097 ret = defaultdict(int)
Javi Merino9ce2fb62014-07-04 20:02:13 +010098
99 with open(fname) as fin:
100 for line in fin:
101 if not re.search(self.unique_word, line):
102 continue
103
104 while True:
105 match = re.search(pat_array, line)
106 if not match:
107 break
108
109 (array_name, array_elements) = match.groups()
110
111 array_len = len(array_elements.split(' '))
112
Javi Merinoa3e98c82014-08-01 14:38:22 +0100113 if array_len > ret[array_name]:
Javi Merino9ce2fb62014-07-04 20:02:13 +0100114 ret[array_name] = array_len
115
116 line = line[match.end():]
117
118 return ret
119
Javi Merinod6284da2014-08-14 12:12:13 +0100120 def __parse_into_dataframe(self):
121 """parse the trace and create a pandas DataFrame"""
Javi Merino9ce2fb62014-07-04 20:02:13 +0100122
123 fin_fname = os.path.join(self.basepath, "trace.txt")
124
125 array_lengths = self.get_trace_array_lengths(fin_fname)
126
Javi Merinoc08ca682014-03-31 17:29:27 +0100127 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
Javi Merinod6284da2014-08-14 12:12:13 +0100128 pat_data_start = re.compile("[A-Za-z0-9_]+=")
Javi Merino45a59c32014-07-02 19:21:17 +0100129 pat_empty_array = re.compile(r"[A-Za-z0-9_]+=\{\} ")
Javi Merinod6284da2014-08-14 12:12:13 +0100130
131 parsed_data = []
132 time_array = []
Javi Merinoc08ca682014-03-31 17:29:27 +0100133
Javi Merino9ce2fb62014-07-04 20:02:13 +0100134 with open(fin_fname) as fin:
Javi Merino952815a2014-03-31 18:08:32 +0100135 for line in fin:
Javi Merinoc2ec5682014-04-01 15:16:06 +0100136 if not re.search(self.unique_word, line):
Javi Merino952815a2014-03-31 18:08:32 +0100137 continue
138
139 line = line[:-1]
140
Javi Merino5bd3d442014-04-08 12:55:13 +0100141 timestamp_match = re.search(pat_timestamp, line)
Javi Merinod6284da2014-08-14 12:12:13 +0100142 timestamp = float(timestamp_match.group(1))
143 time_array.append(timestamp)
Javi Merino952815a2014-03-31 18:08:32 +0100144
Javi Merinod6284da2014-08-14 12:12:13 +0100145 data_start_idx = re.search(pat_data_start, line).start()
Javi Merinoc2ec5682014-04-01 15:16:06 +0100146 data_str = line[data_start_idx:]
Javi Merino952815a2014-03-31 18:08:32 +0100147
Javi Merino45a59c32014-07-02 19:21:17 +0100148 # Remove empty arrays from the trace
149 data_str = re.sub(pat_empty_array, r"", data_str)
150
Javi Merino9ce2fb62014-07-04 20:02:13 +0100151 data_str = trace_parser_explode_array(data_str, array_lengths)
Javi Merinocfb49b72014-06-30 17:51:51 +0100152
Javi Merinod6284da2014-08-14 12:12:13 +0100153 line_data = {}
154 for field in data_str.split():
155 (key, value) = field.split('=')
156 try:
157 value = int(value)
158 except ValueError:
159 pass
160 line_data[key] = value
Javi Merino952815a2014-03-31 18:08:32 +0100161
Javi Merinod6284da2014-08-14 12:12:13 +0100162 parsed_data.append(line_data)
Javi Merino952815a2014-03-31 18:08:32 +0100163
Javi Merinod6284da2014-08-14 12:12:13 +0100164 time_idx = pd.Index(time_array, name="Time")
165 self.data_frame = pd.DataFrame(parsed_data, index=time_idx)
Javi Merinodf8316a2014-03-31 18:39:42 +0100166
Javi Merinof6023f02014-08-13 18:22:53 +0100167 def write_csv(self, fname):
168 """Write the csv info in thermal.csv"""
Javi Merinod6284da2014-08-14 12:12:13 +0100169 self.data_frame.to_csv(fname)
Javi Merinof6023f02014-08-13 18:22:53 +0100170
Javi Merino4d28cbc2014-08-11 11:19:48 +0100171 def normalize_time(self, basetime):
172 """Substract basetime from the Time of the data frame"""
Dietmar Eggemann6c339782014-11-16 15:15:58 +0000173 if basetime:
174 self.data_frame.reset_index(inplace=True)
175 self.data_frame["Time"] = self.data_frame["Time"] - basetime
176 self.data_frame.set_index("Time", inplace=True)
177
Javi Merino0e83b612014-06-05 11:45:23 +0100178class Thermal(BaseThermal):
179 """Process the thermal framework data in a ftrace dump"""
180 def __init__(self, path=None):
181 super(Thermal, self).__init__(
182 basepath=path,
Javi Merinoaf45d872014-07-03 09:49:40 +0100183 unique_word="thermal_temperature:",
Javi Merino0e83b612014-06-05 11:45:23 +0100184 )
185
Javi Merino516d5942014-06-26 15:06:04 +0100186 def plot_temperature(self, control_temperature=None, title="", width=None,
Javi Merino80114162014-08-08 16:48:32 +0100187 height=None, ylim="range", ax=None, legend_label=""):
Javi Merino516d5942014-06-26 15:06:04 +0100188 """Plot the temperature.
189
190 If control_temp is a pd.Series() representing the (possible)
191 variation of control_temp during the run, draw it using a
192 dashed yellow line. Otherwise, only the temperature is
193 plotted.
194
195 """
Javi Merinoc68737a2014-06-10 15:21:59 +0100196 title = normalize_title("Temperature", title)
197
Javi Merino49cbcfe2014-08-08 16:03:49 +0100198 setup_plot = False
199 if not ax:
200 ax = pre_plot_setup(width, height)
201 setup_plot = True
202
Javi Merino80114162014-08-08 16:48:32 +0100203 temp_label = normalize_title("Temperature", legend_label)
Javi Merino92f4d012014-08-08 17:55:32 +0100204 (self.data_frame["temp"] / 1000).plot(ax=ax, label=temp_label)
Javi Merino516d5942014-06-26 15:06:04 +0100205 if control_temperature is not None:
Javi Merino80114162014-08-08 16:48:32 +0100206 ct_label = normalize_title("Control", legend_label)
Javi Merino516d5942014-06-26 15:06:04 +0100207 control_temperature.plot(ax=ax, color="y", linestyle="--",
Javi Merino80114162014-08-08 16:48:32 +0100208 label=ct_label)
Javi Merinoc68737a2014-06-10 15:21:59 +0100209
Javi Merino49cbcfe2014-08-08 16:03:49 +0100210 if setup_plot:
211 post_plot_setup(ax, title=title, ylim=ylim)
212 plt.legend()
Javi Merinoc68737a2014-06-10 15:21:59 +0100213
Javi Merinoe5ea60a2014-08-12 16:41:42 +0100214 def plot_temperature_hist(self, ax, title):
Javi Merino1a3dca32014-08-11 16:12:50 +0100215 """Plot a temperature histogram"""
216
217 temps = self.data_frame["temp"] / 1000
218 title = normalize_title("Temperature", title)
219 xlim = (0, temps.max())
220
Javi Merinoe5ea60a2014-08-12 16:41:42 +0100221 plot_hist(temps, ax, title, 30, "Temperature", xlim, "default")
Javi Merino1a3dca32014-08-11 16:12:50 +0100222
Javi Merino1e69e2c2014-06-04 18:25:09 +0100223class ThermalGovernor(BaseThermal):
Javi Merino5bd3d442014-04-08 12:55:13 +0100224 """Process the power allocator data in a ftrace dump"""
Javi Merino68461552014-04-08 11:40:09 +0100225 def __init__(self, path=None):
Javi Merino1e69e2c2014-06-04 18:25:09 +0100226 super(ThermalGovernor, self).__init__(
Javi Merino68461552014-04-08 11:40:09 +0100227 basepath=path,
Javi Merinoa67b86f2014-07-03 15:44:19 +0100228 unique_word="thermal_power_allocator:",
Javi Merinoc2ec5682014-04-01 15:16:06 +0100229 )
230
Javi Merino29223812014-08-12 15:24:43 +0100231 def plot_input_power(self, actor_order, title="", width=None, height=None, ax=None):
Javi Merinod6d5f892014-07-03 16:24:23 +0100232 """Plot input power
233
234 actor_order is an array with the order in which the actors were registered.
235 """
236
Javi Merino92f4d012014-08-08 17:55:32 +0100237 dfr = self.data_frame
Javi Merinof7968a72014-07-03 15:35:02 +0100238 in_cols = [s for s in dfr.columns if re.match("req_power[0-9]+", s)]
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100239
Javi Merinod6d5f892014-07-03 16:24:23 +0100240 plot_dfr = dfr[in_cols]
241 # Rename the columns from "req_power0" to "A15" or whatever is
242 # in actor_order. Note that we can do it just with an
243 # assignment because the columns are already sorted (i.e.:
244 # req_power0, req_power1...)
245 plot_dfr.columns = actor_order
246
Javi Merinoc00feff2014-04-14 15:41:51 +0100247 title = normalize_title("Input Power", title)
Javi Merino8ecd8172014-07-03 16:09:01 +0100248
Javi Merino29223812014-08-12 15:24:43 +0100249 if not ax:
250 ax = pre_plot_setup(width, height)
251
Javi Merinod6d5f892014-07-03 16:24:23 +0100252 plot_dfr.plot(ax=ax)
Javi Merino8ecd8172014-07-03 16:09:01 +0100253 post_plot_setup(ax, title=title)
Javi Merino9c010772014-04-02 16:54:41 +0100254
Javi Merino6003b252014-08-12 15:32:17 +0100255 def plot_output_power(self, actor_order, title="", width=None, height=None, ax=None):
Javi Merinod6d5f892014-07-03 16:24:23 +0100256 """Plot output power
257
258 actor_order is an array with the order in which the actors were registered.
259 """
260
Javi Merino92f4d012014-08-08 17:55:32 +0100261 out_cols = [s for s in self.data_frame.columns
Javi Merinof7968a72014-07-03 15:35:02 +0100262 if re.match("granted_power[0-9]+", s)]
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100263
Javi Merinod6d5f892014-07-03 16:24:23 +0100264 # See the note in plot_input_power()
Javi Merino92f4d012014-08-08 17:55:32 +0100265 plot_dfr = self.data_frame[out_cols]
Javi Merinod6d5f892014-07-03 16:24:23 +0100266 plot_dfr.columns = actor_order
267
Javi Merinoc00feff2014-04-14 15:41:51 +0100268 title = normalize_title("Output Power", title)
Javi Merino8ecd8172014-07-03 16:09:01 +0100269
Javi Merino6003b252014-08-12 15:32:17 +0100270 if not ax:
271 ax = pre_plot_setup(width, height)
272
Javi Merinod6d5f892014-07-03 16:24:23 +0100273 plot_dfr.plot(ax=ax)
Javi Merino8ecd8172014-07-03 16:09:01 +0100274 post_plot_setup(ax, title=title)
Javi Merinocd4a8272014-04-14 15:50:01 +0100275
Javi Merino9fc54852014-05-07 19:06:53 +0100276 def plot_inout_power(self, title="", width=None, height=None):
277 """Make multiple plots showing input and output power for each actor"""
Javi Merino92f4d012014-08-08 17:55:32 +0100278 dfr = self.data_frame
Javi Merino9fc54852014-05-07 19:06:53 +0100279
280 actors = []
281 for col in dfr.columns:
282 match = re.match("P(.*)_in", col)
283 if match and col != "Ptot_in":
284 actors.append(match.group(1))
285
286 for actor in actors:
287 cols = ["P" + actor + "_in", "P" + actor + "_out"]
288 this_title = normalize_title(actor, title)
289 dfr[cols].plot(title=this_title)