blob: a5fdf77ea27e3bd768eafe4ca2d6222118da17ad [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 Merinob2ff5692014-06-30 17:41:50 +010013def trace_parser_explode_array(string):
14 """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
17 load3=2". Currently, it only supports one array in string
18
19 """
20
21 match = re.search(r"[^ ]+={[^}]+}", string)
22 if match is None:
23 return string
24
25 to_explode = match.group()
26 col_basename = re.match(r"([^=]+)=", to_explode).groups()[0]
27 vals_str = re.search(r"{(.+)}", to_explode).groups()[0]
28 vals_array = vals_str.split(' ')
29
30 exploded_str = ""
31 for (idx, val) in enumerate(vals_array):
32 exploded_str += "{}{}={} ".format(col_basename, idx, val)
33
34 exploded_str = exploded_str[:-1]
35 begin_idx = match.start()
36 end_idx = match.end()
37
38 return string[:begin_idx] + exploded_str + string[end_idx:]
39
Javi Merinoc2ec5682014-04-01 15:16:06 +010040class BaseThermal(object):
Javi Merino5bd3d442014-04-08 12:55:13 +010041 """Base class to parse trace.dat dumps.
42
43 Don't use directly, create a subclass that defines the unique_word
44 you want to match in the output"""
Javi Merino68461552014-04-08 11:40:09 +010045 def __init__(self, basepath, unique_word):
46 if basepath is None:
47 basepath = "."
Javi Merinoc2ec5682014-04-01 15:16:06 +010048
Javi Merino68461552014-04-08 11:40:09 +010049 self.basepath = basepath
Javi Merino952815a2014-03-31 18:08:32 +010050 self.data_csv = ""
Javi Merino22d85f82014-06-21 19:15:04 +010051 self.data_frame = None
Javi Merino68461552014-04-08 11:40:09 +010052 self.unique_word = unique_word
53
54 if not os.path.isfile(os.path.join(basepath, "trace.txt")):
55 self.__run_trace_cmd_report()
Javi Merino572049d2014-03-31 16:45:23 +010056
Javi Merino572049d2014-03-31 16:45:23 +010057 def __run_trace_cmd_report(self):
Javi Merino5bd3d442014-04-08 12:55:13 +010058 """Run "trace-cmd report > trace.txt".
59
60 Overwrites the contents of trace.txt if it exists."""
Javi Merinoee56c362014-03-31 17:30:34 +010061 from subprocess import check_output
62
Javi Merino68461552014-04-08 11:40:09 +010063 if not os.path.isfile(os.path.join(self.basepath, "trace.dat")):
Javi Merino1a3725a2014-03-31 18:35:15 +010064 raise IOError("No such file or directory: trace.dat")
65
Javi Merino68461552014-04-08 11:40:09 +010066 previous_path = os.getcwd()
67 os.chdir(self.basepath)
Javi Merino572049d2014-03-31 16:45:23 +010068
Javi Merino68461552014-04-08 11:40:09 +010069 # This would better be done with a context manager (i.e.
70 # http://stackoverflow.com/a/13197763/970766)
71 try:
72 with open(os.devnull) as devnull:
73 out = check_output(["trace-cmd", "report"], stderr=devnull)
74
75 finally:
76 os.chdir(previous_path)
77
Javi Merino5bd3d442014-04-08 12:55:13 +010078 with open(os.path.join(self.basepath, "trace.txt"), "w") as fout:
79 fout.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010080
Javi Merinoc2ec5682014-04-01 15:16:06 +010081 def parse_into_csv(self):
Javi Merino5bd3d442014-04-08 12:55:13 +010082 """Create a csv representation of the thermal data and store
83 it in self.data_csv"""
Javi Merinoc08ca682014-03-31 17:29:27 +010084 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
Javi Merinocfb49b72014-06-30 17:51:51 +010085 pat_data = re.compile(r"[A-Za-z0-9_]+=([^ {]+)")
Javi Merino0e83b612014-06-05 11:45:23 +010086 pat_header = re.compile(r"([A-Za-z0-9_]+)=[^ ]+")
Javi Merinoc08ca682014-03-31 17:29:27 +010087 header = ""
88
Javi Merino68461552014-04-08 11:40:09 +010089 with open(os.path.join(self.basepath, "trace.txt")) as fin:
Javi Merino952815a2014-03-31 18:08:32 +010090 for line in fin:
Javi Merinoc2ec5682014-04-01 15:16:06 +010091 if not re.search(self.unique_word, line):
Javi Merino952815a2014-03-31 18:08:32 +010092 continue
93
94 line = line[:-1]
95
Javi Merino5bd3d442014-04-08 12:55:13 +010096 timestamp_match = re.search(pat_timestamp, line)
97 timestamp = timestamp_match.group(1)
Javi Merino952815a2014-03-31 18:08:32 +010098
Javi Merinoc2ec5682014-04-01 15:16:06 +010099 data_start_idx = re.search(r"[A-Za-z0-9_]+=", line).start()
100 data_str = line[data_start_idx:]
Javi Merino952815a2014-03-31 18:08:32 +0100101
Javi Merinocfb49b72014-06-30 17:51:51 +0100102 data_str = trace_parser_explode_array(data_str)
103
Javi Merino952815a2014-03-31 18:08:32 +0100104 if not header:
Javi Merino0af47212014-04-02 16:23:23 +0100105 header = re.sub(pat_header, r"\1", data_str)
106 header = re.sub(r" ", r",", header)
Javi Merino2a16a442014-06-21 19:23:45 +0100107 header = "Time," + header + "\n"
Javi Merino952815a2014-03-31 18:08:32 +0100108 self.data_csv = header
109
Javi Merino0af47212014-04-02 16:23:23 +0100110 parsed_data = re.sub(pat_data, r"\1", data_str)
111 parsed_data = re.sub(r" ", r",", parsed_data)
Javi Merino952815a2014-03-31 18:08:32 +0100112
113 parsed_data = timestamp + "," + parsed_data + "\n"
114 self.data_csv += parsed_data
115
Javi Merinof78ea5b2014-03-31 17:51:38 +0100116 def get_data_frame(self):
117 """Return a pandas data frame for the run"""
Javi Merino22d85f82014-06-21 19:15:04 +0100118 if self.data_frame is not None:
Javi Merinof78ea5b2014-03-31 17:51:38 +0100119 return self.data_frame
120
Javi Merino952815a2014-03-31 18:08:32 +0100121 if not self.data_csv:
Javi Merinoc2ec5682014-04-01 15:16:06 +0100122 self.parse_into_csv()
Javi Merinof78ea5b2014-03-31 17:51:38 +0100123
Javi Merinof0f51ff2014-04-10 12:34:53 +0100124 if self.data_csv is "":
125 return pd.DataFrame()
126
127 unordered_df = pd.read_csv(StringIO(self.data_csv))
Javi Merino2a16a442014-06-21 19:23:45 +0100128 self.data_frame = unordered_df.set_index("Time")
Javi Merino04f27492014-04-02 09:59:23 +0100129
Javi Merinof78ea5b2014-03-31 17:51:38 +0100130 return self.data_frame
Javi Merinodf8316a2014-03-31 18:39:42 +0100131
Javi Merino51db3632014-06-13 11:24:51 +0100132 def plot_multivalue(self, values, title, width, height):
133 """Plot multiple values of the DataFrame
Javi Merino05983ef2014-04-08 12:54:20 +0100134
Javi Merino51db3632014-06-13 11:24:51 +0100135 values is an array with the keys of the DataFrame to plot
136 """
Javi Merinoc0e104d2014-06-17 17:31:13 +0100137
Javi Merino51db3632014-06-13 11:24:51 +0100138 dfr = self.get_data_frame()
Javi Merino051db6c2014-06-18 15:29:15 +0100139
Javi Merino3a736552014-06-19 19:22:44 +0100140 ax = pre_plot_setup(width, height)
141 dfr[values].plot(ax=ax)
142 post_plot_setup(ax, title=title)
Javi Merino7a7cd702014-04-14 15:41:15 +0100143
Javi Merino0e83b612014-06-05 11:45:23 +0100144class Thermal(BaseThermal):
145 """Process the thermal framework data in a ftrace dump"""
146 def __init__(self, path=None):
147 super(Thermal, self).__init__(
148 basepath=path,
149 unique_word="thermal_zone=",
150 )
151
Javi Merino516d5942014-06-26 15:06:04 +0100152 def plot_temperature(self, control_temperature=None, title="", width=None,
153 height=None, ylim="range"):
154 """Plot the temperature.
155
156 If control_temp is a pd.Series() representing the (possible)
157 variation of control_temp during the run, draw it using a
158 dashed yellow line. Otherwise, only the temperature is
159 plotted.
160
161 """
Javi Merinoc68737a2014-06-10 15:21:59 +0100162 dfr = self.get_data_frame()
163 title = normalize_title("Temperature", title)
164
Javi Merino3a736552014-06-19 19:22:44 +0100165 ax = pre_plot_setup(width, height)
166 (dfr["temp"] / 1000).plot(ax=ax)
Javi Merino516d5942014-06-26 15:06:04 +0100167 if control_temperature is not None:
168 control_temperature.plot(ax=ax, color="y", linestyle="--",
169 label="control temperature")
Javi Merino3a736552014-06-19 19:22:44 +0100170 post_plot_setup(ax, title=title, ylim=ylim)
Javi Merinoc68737a2014-06-10 15:21:59 +0100171
Javi Merinoc68737a2014-06-10 15:21:59 +0100172 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 Merinoc00feff2014-04-14 15:41:51 +0100190 def plot_input_power(self, title="", width=None, height=None):
Javi Merinof1dcb2d2014-04-02 16:52:11 +0100191 """Plot input power"""
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100192 dfr = self.get_data_frame()
193 in_cols = [s for s in dfr.columns
194 if re.match("P.*_in", s) and s != "Ptot_in"]
195
Javi Merinoc00feff2014-04-14 15:41:51 +0100196 title = normalize_title("Input Power", title)
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100197 self.plot_multivalue(in_cols, title, width, height)
Javi Merino9c010772014-04-02 16:54:41 +0100198
Javi Merinoc00feff2014-04-14 15:41:51 +0100199 def plot_output_power(self, title="", width=None, height=None):
Javi Merino9c010772014-04-02 16:54:41 +0100200 """Plot output power"""
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100201 dfr = self.get_data_frame()
202 out_cols = [s for s in dfr.columns
203 if re.match("P.*_out", s) and s != "Ptot_out"]
204
Javi Merinoc00feff2014-04-14 15:41:51 +0100205 title = normalize_title("Output Power", title)
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100206 self.plot_multivalue(out_cols,
Javi Merinoc00feff2014-04-14 15:41:51 +0100207 title, width, height)
Javi Merinocd4a8272014-04-14 15:50:01 +0100208
Javi Merino9fc54852014-05-07 19:06:53 +0100209 def plot_inout_power(self, title="", width=None, height=None):
210 """Make multiple plots showing input and output power for each actor"""
211 dfr = self.get_data_frame()
212
213 actors = []
214 for col in dfr.columns:
215 match = re.match("P(.*)_in", col)
216 if match and col != "Ptot_in":
217 actors.append(match.group(1))
218
219 for actor in actors:
220 cols = ["P" + actor + "_in", "P" + actor + "_out"]
221 this_title = normalize_title(actor, title)
222 dfr[cols].plot(title=this_title)