blob: 185c032d3ba465d5e64666201f750b44a64a65d1 [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 Merino3a736552014-06-19 19:22:44 +010011from plot_utils import normalize_title, pre_plot_setup, post_plot_setup
Javi Merino51db3632014-06-13 11:24:51 +010012
Javi Merino9ce2fb62014-07-04 20:02:13 +010013def trace_parser_explode_array(string, array_lengths):
Javi Merinob2ff5692014-06-30 17:41:50 +010014 """Explode an array in the trace into individual elements for easy parsing
15
16 Basically, turn "load={1 1 2 2}" into "load0=1 load1=1 load2=2
Javi Merino9ce2fb62014-07-04 20:02:13 +010017 load3=2". array_lengths is a dictionary of array names and their
18 expected length. If we get array that's shorter than the expected
19 length, additional keys have to be introduced with value 0 to
20 compensate. For example, "load={1 2}" with array_lengths being
21 {"load": 4} returns "load0=1 load1=2 load2=0 load3=0"
Javi Merinob2ff5692014-06-30 17:41:50 +010022
23 """
24
Javi Merino9ce2fb62014-07-04 20:02:13 +010025 while True:
Javi Merinod44312f2014-07-02 18:34:24 +010026 match = re.search(r"[^ ]+={[^}]+}", string)
27 if match is None:
28 break
Javi Merinob2ff5692014-06-30 17:41:50 +010029
Javi Merinod44312f2014-07-02 18:34:24 +010030 to_explode = match.group()
31 col_basename = re.match(r"([^=]+)=", to_explode).groups()[0]
32 vals_str = re.search(r"{(.+)}", to_explode).groups()[0]
33 vals_array = vals_str.split(' ')
Javi Merinob2ff5692014-06-30 17:41:50 +010034
Javi Merinod44312f2014-07-02 18:34:24 +010035 exploded_str = ""
36 for (idx, val) in enumerate(vals_array):
37 exploded_str += "{}{}={} ".format(col_basename, idx, val)
Javi Merinob2ff5692014-06-30 17:41:50 +010038
Javi Merino9ce2fb62014-07-04 20:02:13 +010039 vals_added = len(vals_array)
40 if vals_added < array_lengths[col_basename]:
41 for idx in range(vals_added, array_lengths[col_basename]):
42 exploded_str += "{}{}=0 ".format(col_basename, idx)
43
Javi Merinod44312f2014-07-02 18:34:24 +010044 exploded_str = exploded_str[:-1]
45 begin_idx = match.start()
46 end_idx = match.end()
Javi Merinob2ff5692014-06-30 17:41:50 +010047
Javi Merinod44312f2014-07-02 18:34:24 +010048 string = string[:begin_idx] + exploded_str + string[end_idx:]
49
50 return string
Javi Merinob2ff5692014-06-30 17:41:50 +010051
Javi Merinoc2ec5682014-04-01 15:16:06 +010052class BaseThermal(object):
Javi Merino5bd3d442014-04-08 12:55:13 +010053 """Base class to parse trace.dat dumps.
54
55 Don't use directly, create a subclass that defines the unique_word
56 you want to match in the output"""
Javi Merino68461552014-04-08 11:40:09 +010057 def __init__(self, basepath, unique_word):
58 if basepath is None:
59 basepath = "."
Javi Merinoc2ec5682014-04-01 15:16:06 +010060
Javi Merino68461552014-04-08 11:40:09 +010061 self.basepath = basepath
Javi Merino952815a2014-03-31 18:08:32 +010062 self.data_csv = ""
Javi Merino0df4de72014-08-11 11:34:40 +010063 self.data_frame = pd.DataFrame()
Javi Merino68461552014-04-08 11:40:09 +010064 self.unique_word = unique_word
65
66 if not os.path.isfile(os.path.join(basepath, "trace.txt")):
67 self.__run_trace_cmd_report()
Javi Merino572049d2014-03-31 16:45:23 +010068
Javi Merinod5562282014-08-08 17:28:03 +010069 self.__parse_into_csv()
Javi Merino92f4d012014-08-08 17:55:32 +010070 self.__create_data_frame()
Javi Merinod5562282014-08-08 17:28:03 +010071
Javi Merino572049d2014-03-31 16:45:23 +010072 def __run_trace_cmd_report(self):
Javi Merino5bd3d442014-04-08 12:55:13 +010073 """Run "trace-cmd report > trace.txt".
74
75 Overwrites the contents of trace.txt if it exists."""
Javi Merinoee56c362014-03-31 17:30:34 +010076 from subprocess import check_output
77
Javi Merino68461552014-04-08 11:40:09 +010078 if not os.path.isfile(os.path.join(self.basepath, "trace.dat")):
Javi Merino1a3725a2014-03-31 18:35:15 +010079 raise IOError("No such file or directory: trace.dat")
80
Javi Merino68461552014-04-08 11:40:09 +010081 previous_path = os.getcwd()
82 os.chdir(self.basepath)
Javi Merino572049d2014-03-31 16:45:23 +010083
Javi Merino68461552014-04-08 11:40:09 +010084 # This would better be done with a context manager (i.e.
85 # http://stackoverflow.com/a/13197763/970766)
86 try:
87 with open(os.devnull) as devnull:
88 out = check_output(["trace-cmd", "report"], stderr=devnull)
89
90 finally:
91 os.chdir(previous_path)
92
Javi Merino5bd3d442014-04-08 12:55:13 +010093 with open(os.path.join(self.basepath, "trace.txt"), "w") as fout:
94 fout.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010095
Javi Merino9ce2fb62014-07-04 20:02:13 +010096 def get_trace_array_lengths(self, fname):
97 """Calculate the lengths of all arrays in the trace
98
99 Returns a dict with the name of each array found in the trace
100 as keys and their corresponding length as value
101
102 """
Javi Merinoa3e98c82014-08-01 14:38:22 +0100103 from collections import defaultdict
Javi Merino9ce2fb62014-07-04 20:02:13 +0100104
105 pat_array = re.compile(r"([A-Za-z0-9_]+)={([^}]+)}")
106
Javi Merinoa3e98c82014-08-01 14:38:22 +0100107 ret = defaultdict(int)
Javi Merino9ce2fb62014-07-04 20:02:13 +0100108
109 with open(fname) as fin:
110 for line in fin:
111 if not re.search(self.unique_word, line):
112 continue
113
114 while True:
115 match = re.search(pat_array, line)
116 if not match:
117 break
118
119 (array_name, array_elements) = match.groups()
120
121 array_len = len(array_elements.split(' '))
122
Javi Merinoa3e98c82014-08-01 14:38:22 +0100123 if array_len > ret[array_name]:
Javi Merino9ce2fb62014-07-04 20:02:13 +0100124 ret[array_name] = array_len
125
126 line = line[match.end():]
127
128 return ret
129
Javi Merinod5562282014-08-08 17:28:03 +0100130 def __parse_into_csv(self):
Javi Merino5bd3d442014-04-08 12:55:13 +0100131 """Create a csv representation of the thermal data and store
132 it in self.data_csv"""
Javi Merino9ce2fb62014-07-04 20:02:13 +0100133
134 fin_fname = os.path.join(self.basepath, "trace.txt")
135
136 array_lengths = self.get_trace_array_lengths(fin_fname)
137
Javi Merinoc08ca682014-03-31 17:29:27 +0100138 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
Javi Merinocfb49b72014-06-30 17:51:51 +0100139 pat_data = re.compile(r"[A-Za-z0-9_]+=([^ {]+)")
Javi Merino0e83b612014-06-05 11:45:23 +0100140 pat_header = re.compile(r"([A-Za-z0-9_]+)=[^ ]+")
Javi Merino45a59c32014-07-02 19:21:17 +0100141 pat_empty_array = re.compile(r"[A-Za-z0-9_]+=\{\} ")
Javi Merinoc08ca682014-03-31 17:29:27 +0100142 header = ""
143
Javi Merino9ce2fb62014-07-04 20:02:13 +0100144 with open(fin_fname) as fin:
Javi Merino952815a2014-03-31 18:08:32 +0100145 for line in fin:
Javi Merinoc2ec5682014-04-01 15:16:06 +0100146 if not re.search(self.unique_word, line):
Javi Merino952815a2014-03-31 18:08:32 +0100147 continue
148
149 line = line[:-1]
150
Javi Merino5bd3d442014-04-08 12:55:13 +0100151 timestamp_match = re.search(pat_timestamp, line)
152 timestamp = timestamp_match.group(1)
Javi Merino952815a2014-03-31 18:08:32 +0100153
Javi Merinoc2ec5682014-04-01 15:16:06 +0100154 data_start_idx = re.search(r"[A-Za-z0-9_]+=", line).start()
155 data_str = line[data_start_idx:]
Javi Merino952815a2014-03-31 18:08:32 +0100156
Javi Merino45a59c32014-07-02 19:21:17 +0100157 # Remove empty arrays from the trace
158 data_str = re.sub(pat_empty_array, r"", data_str)
159
Javi Merino9ce2fb62014-07-04 20:02:13 +0100160 data_str = trace_parser_explode_array(data_str, array_lengths)
Javi Merinocfb49b72014-06-30 17:51:51 +0100161
Javi Merino952815a2014-03-31 18:08:32 +0100162 if not header:
Javi Merino0af47212014-04-02 16:23:23 +0100163 header = re.sub(pat_header, r"\1", data_str)
164 header = re.sub(r" ", r",", header)
Javi Merino2a16a442014-06-21 19:23:45 +0100165 header = "Time," + header + "\n"
Javi Merino952815a2014-03-31 18:08:32 +0100166 self.data_csv = header
167
Javi Merino0af47212014-04-02 16:23:23 +0100168 parsed_data = re.sub(pat_data, r"\1", data_str)
Javi Merino35c1ac72014-07-04 10:29:04 +0100169 parsed_data = re.sub(r",", r"", parsed_data)
Javi Merino0af47212014-04-02 16:23:23 +0100170 parsed_data = re.sub(r" ", r",", parsed_data)
Javi Merino952815a2014-03-31 18:08:32 +0100171
172 parsed_data = timestamp + "," + parsed_data + "\n"
173 self.data_csv += parsed_data
174
Javi Merino92f4d012014-08-08 17:55:32 +0100175 def __create_data_frame(self):
176 """Create a pandas data frame for the run in self.data_frame"""
Javi Merinof0f51ff2014-04-10 12:34:53 +0100177 if self.data_csv is "":
Javi Merino92f4d012014-08-08 17:55:32 +0100178 self.data_frame = pd.DataFrame()
179 else:
Javi Merino988df232014-08-08 18:17:29 +0100180 self.data_frame = pd.read_csv(StringIO(self.data_csv))
181 self.data_frame.set_index("Time", inplace=True)
Javi Merinodf8316a2014-03-31 18:39:42 +0100182
Javi Merino4d28cbc2014-08-11 11:19:48 +0100183 def normalize_time(self, basetime):
184 """Substract basetime from the Time of the data frame"""
185 self.data_frame.reset_index(inplace=True)
186 self.data_frame["Time"] = self.data_frame["Time"] - basetime
187 self.data_frame.set_index("Time", inplace=True)
188
Javi Merino0e83b612014-06-05 11:45:23 +0100189class Thermal(BaseThermal):
190 """Process the thermal framework data in a ftrace dump"""
191 def __init__(self, path=None):
192 super(Thermal, self).__init__(
193 basepath=path,
Javi Merinoaf45d872014-07-03 09:49:40 +0100194 unique_word="thermal_temperature:",
Javi Merino0e83b612014-06-05 11:45:23 +0100195 )
196
Javi Merino516d5942014-06-26 15:06:04 +0100197 def plot_temperature(self, control_temperature=None, title="", width=None,
Javi Merino80114162014-08-08 16:48:32 +0100198 height=None, ylim="range", ax=None, legend_label=""):
Javi Merino516d5942014-06-26 15:06:04 +0100199 """Plot the temperature.
200
201 If control_temp is a pd.Series() representing the (possible)
202 variation of control_temp during the run, draw it using a
203 dashed yellow line. Otherwise, only the temperature is
204 plotted.
205
206 """
Javi Merinoc68737a2014-06-10 15:21:59 +0100207 title = normalize_title("Temperature", title)
208
Javi Merino49cbcfe2014-08-08 16:03:49 +0100209 setup_plot = False
210 if not ax:
211 ax = pre_plot_setup(width, height)
212 setup_plot = True
213
Javi Merino80114162014-08-08 16:48:32 +0100214 temp_label = normalize_title("Temperature", legend_label)
Javi Merino92f4d012014-08-08 17:55:32 +0100215 (self.data_frame["temp"] / 1000).plot(ax=ax, label=temp_label)
Javi Merino516d5942014-06-26 15:06:04 +0100216 if control_temperature is not None:
Javi Merino80114162014-08-08 16:48:32 +0100217 ct_label = normalize_title("Control", legend_label)
Javi Merino516d5942014-06-26 15:06:04 +0100218 control_temperature.plot(ax=ax, color="y", linestyle="--",
Javi Merino80114162014-08-08 16:48:32 +0100219 label=ct_label)
Javi Merinoc68737a2014-06-10 15:21:59 +0100220
Javi Merino49cbcfe2014-08-08 16:03:49 +0100221 if setup_plot:
222 post_plot_setup(ax, title=title, ylim=ylim)
223 plt.legend()
Javi Merinoc68737a2014-06-10 15:21:59 +0100224
Javi Merino1e69e2c2014-06-04 18:25:09 +0100225class ThermalGovernor(BaseThermal):
Javi Merino5bd3d442014-04-08 12:55:13 +0100226 """Process the power allocator data in a ftrace dump"""
Javi Merino68461552014-04-08 11:40:09 +0100227 def __init__(self, path=None):
Javi Merino1e69e2c2014-06-04 18:25:09 +0100228 super(ThermalGovernor, self).__init__(
Javi Merino68461552014-04-08 11:40:09 +0100229 basepath=path,
Javi Merinoa67b86f2014-07-03 15:44:19 +0100230 unique_word="thermal_power_allocator:",
Javi Merinoc2ec5682014-04-01 15:16:06 +0100231 )
232
233 def write_thermal_csv(self):
234 """Write the csv info in thermal.csv"""
Javi Merinoc2ec5682014-04-01 15:16:06 +0100235 with open("thermal.csv", "w") as fout:
236 fout.write(self.data_csv)
237
Javi Merinod6d5f892014-07-03 16:24:23 +0100238 def plot_input_power(self, actor_order, title="", width=None, height=None):
239 """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
256 ax = pre_plot_setup(width, height)
Javi Merinod6d5f892014-07-03 16:24:23 +0100257 plot_dfr.plot(ax=ax)
Javi Merino8ecd8172014-07-03 16:09:01 +0100258 post_plot_setup(ax, title=title)
Javi Merino9c010772014-04-02 16:54:41 +0100259
Javi Merinod6d5f892014-07-03 16:24:23 +0100260 def plot_output_power(self, actor_order, title="", width=None, height=None):
261 """Plot output power
262
263 actor_order is an array with the order in which the actors were registered.
264 """
265
Javi Merino92f4d012014-08-08 17:55:32 +0100266 out_cols = [s for s in self.data_frame.columns
Javi Merinof7968a72014-07-03 15:35:02 +0100267 if re.match("granted_power[0-9]+", s)]
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100268
Javi Merinod6d5f892014-07-03 16:24:23 +0100269 # See the note in plot_input_power()
Javi Merino92f4d012014-08-08 17:55:32 +0100270 plot_dfr = self.data_frame[out_cols]
Javi Merinod6d5f892014-07-03 16:24:23 +0100271 plot_dfr.columns = actor_order
272
Javi Merinoc00feff2014-04-14 15:41:51 +0100273 title = normalize_title("Output Power", title)
Javi Merino8ecd8172014-07-03 16:09:01 +0100274
275 ax = pre_plot_setup(width, height)
Javi Merinod6d5f892014-07-03 16:24:23 +0100276 plot_dfr.plot(ax=ax)
Javi Merino8ecd8172014-07-03 16:09:01 +0100277 post_plot_setup(ax, title=title)
Javi Merinocd4a8272014-04-14 15:50:01 +0100278
Javi Merino9fc54852014-05-07 19:06:53 +0100279 def plot_inout_power(self, title="", width=None, height=None):
280 """Make multiple plots showing input and output power for each actor"""
Javi Merino92f4d012014-08-08 17:55:32 +0100281 dfr = self.data_frame
Javi Merino9fc54852014-05-07 19:06:53 +0100282
283 actors = []
284 for col in dfr.columns:
285 match = re.match("P(.*)_in", col)
286 if match and col != "Ptot_in":
287 actors.append(match.group(1))
288
289 for actor in actors:
290 cols = ["P" + actor + "_in", "P" + actor + "_out"]
291 this_title = normalize_title(actor, title)
292 dfr[cols].plot(title=this_title)