blob: 2a31c6a88266cc07a1f4d90bd61f6f3149f3be07 [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
Javi Merinod44312f2014-07-02 18:34:24 +010017 load3=2".
Javi Merinob2ff5692014-06-30 17:41:50 +010018
19 """
20
Javi Merinod44312f2014-07-02 18:34:24 +010021 while 1:
22 match = re.search(r"[^ ]+={[^}]+}", string)
23 if match is None:
24 break
Javi Merinob2ff5692014-06-30 17:41:50 +010025
Javi Merinod44312f2014-07-02 18:34:24 +010026 to_explode = match.group()
27 col_basename = re.match(r"([^=]+)=", to_explode).groups()[0]
28 vals_str = re.search(r"{(.+)}", to_explode).groups()[0]
29 vals_array = vals_str.split(' ')
Javi Merinob2ff5692014-06-30 17:41:50 +010030
Javi Merinod44312f2014-07-02 18:34:24 +010031 exploded_str = ""
32 for (idx, val) in enumerate(vals_array):
33 exploded_str += "{}{}={} ".format(col_basename, idx, val)
Javi Merinob2ff5692014-06-30 17:41:50 +010034
Javi Merinod44312f2014-07-02 18:34:24 +010035 exploded_str = exploded_str[:-1]
36 begin_idx = match.start()
37 end_idx = match.end()
Javi Merinob2ff5692014-06-30 17:41:50 +010038
Javi Merinod44312f2014-07-02 18:34:24 +010039 string = string[:begin_idx] + exploded_str + string[end_idx:]
40
41 return string
Javi Merinob2ff5692014-06-30 17:41:50 +010042
Javi Merinoc2ec5682014-04-01 15:16:06 +010043class BaseThermal(object):
Javi Merino5bd3d442014-04-08 12:55:13 +010044 """Base class to parse trace.dat dumps.
45
46 Don't use directly, create a subclass that defines the unique_word
47 you want to match in the output"""
Javi Merino68461552014-04-08 11:40:09 +010048 def __init__(self, basepath, unique_word):
49 if basepath is None:
50 basepath = "."
Javi Merinoc2ec5682014-04-01 15:16:06 +010051
Javi Merino68461552014-04-08 11:40:09 +010052 self.basepath = basepath
Javi Merino952815a2014-03-31 18:08:32 +010053 self.data_csv = ""
Javi Merino22d85f82014-06-21 19:15:04 +010054 self.data_frame = None
Javi Merino68461552014-04-08 11:40:09 +010055 self.unique_word = unique_word
56
57 if not os.path.isfile(os.path.join(basepath, "trace.txt")):
58 self.__run_trace_cmd_report()
Javi Merino572049d2014-03-31 16:45:23 +010059
Javi Merino572049d2014-03-31 16:45:23 +010060 def __run_trace_cmd_report(self):
Javi Merino5bd3d442014-04-08 12:55:13 +010061 """Run "trace-cmd report > trace.txt".
62
63 Overwrites the contents of trace.txt if it exists."""
Javi Merinoee56c362014-03-31 17:30:34 +010064 from subprocess import check_output
65
Javi Merino68461552014-04-08 11:40:09 +010066 if not os.path.isfile(os.path.join(self.basepath, "trace.dat")):
Javi Merino1a3725a2014-03-31 18:35:15 +010067 raise IOError("No such file or directory: trace.dat")
68
Javi Merino68461552014-04-08 11:40:09 +010069 previous_path = os.getcwd()
70 os.chdir(self.basepath)
Javi Merino572049d2014-03-31 16:45:23 +010071
Javi Merino68461552014-04-08 11:40:09 +010072 # This would better be done with a context manager (i.e.
73 # http://stackoverflow.com/a/13197763/970766)
74 try:
75 with open(os.devnull) as devnull:
76 out = check_output(["trace-cmd", "report"], stderr=devnull)
77
78 finally:
79 os.chdir(previous_path)
80
Javi Merino5bd3d442014-04-08 12:55:13 +010081 with open(os.path.join(self.basepath, "trace.txt"), "w") as fout:
82 fout.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010083
Javi Merinoc2ec5682014-04-01 15:16:06 +010084 def parse_into_csv(self):
Javi Merino5bd3d442014-04-08 12:55:13 +010085 """Create a csv representation of the thermal data and store
86 it in self.data_csv"""
Javi Merinoc08ca682014-03-31 17:29:27 +010087 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
Javi Merinocfb49b72014-06-30 17:51:51 +010088 pat_data = re.compile(r"[A-Za-z0-9_]+=([^ {]+)")
Javi Merino0e83b612014-06-05 11:45:23 +010089 pat_header = re.compile(r"([A-Za-z0-9_]+)=[^ ]+")
Javi Merino45a59c32014-07-02 19:21:17 +010090 pat_empty_array = re.compile(r"[A-Za-z0-9_]+=\{\} ")
Javi Merinoc08ca682014-03-31 17:29:27 +010091 header = ""
92
Javi Merino68461552014-04-08 11:40:09 +010093 with open(os.path.join(self.basepath, "trace.txt")) as fin:
Javi Merino952815a2014-03-31 18:08:32 +010094 for line in fin:
Javi Merinoc2ec5682014-04-01 15:16:06 +010095 if not re.search(self.unique_word, line):
Javi Merino952815a2014-03-31 18:08:32 +010096 continue
97
98 line = line[:-1]
99
Javi Merino5bd3d442014-04-08 12:55:13 +0100100 timestamp_match = re.search(pat_timestamp, line)
101 timestamp = timestamp_match.group(1)
Javi Merino952815a2014-03-31 18:08:32 +0100102
Javi Merinoc2ec5682014-04-01 15:16:06 +0100103 data_start_idx = re.search(r"[A-Za-z0-9_]+=", line).start()
104 data_str = line[data_start_idx:]
Javi Merino952815a2014-03-31 18:08:32 +0100105
Javi Merino45a59c32014-07-02 19:21:17 +0100106 # Remove empty arrays from the trace
107 data_str = re.sub(pat_empty_array, r"", data_str)
108
Javi Merinocfb49b72014-06-30 17:51:51 +0100109 data_str = trace_parser_explode_array(data_str)
110
Javi Merino952815a2014-03-31 18:08:32 +0100111 if not header:
Javi Merino0af47212014-04-02 16:23:23 +0100112 header = re.sub(pat_header, r"\1", data_str)
113 header = re.sub(r" ", r",", header)
Javi Merino2a16a442014-06-21 19:23:45 +0100114 header = "Time," + header + "\n"
Javi Merino952815a2014-03-31 18:08:32 +0100115 self.data_csv = header
116
Javi Merino0af47212014-04-02 16:23:23 +0100117 parsed_data = re.sub(pat_data, r"\1", data_str)
118 parsed_data = re.sub(r" ", r",", parsed_data)
Javi Merino952815a2014-03-31 18:08:32 +0100119
120 parsed_data = timestamp + "," + parsed_data + "\n"
121 self.data_csv += parsed_data
122
Javi Merinof78ea5b2014-03-31 17:51:38 +0100123 def get_data_frame(self):
124 """Return a pandas data frame for the run"""
Javi Merino22d85f82014-06-21 19:15:04 +0100125 if self.data_frame is not None:
Javi Merinof78ea5b2014-03-31 17:51:38 +0100126 return self.data_frame
127
Javi Merino952815a2014-03-31 18:08:32 +0100128 if not self.data_csv:
Javi Merinoc2ec5682014-04-01 15:16:06 +0100129 self.parse_into_csv()
Javi Merinof78ea5b2014-03-31 17:51:38 +0100130
Javi Merinof0f51ff2014-04-10 12:34:53 +0100131 if self.data_csv is "":
132 return pd.DataFrame()
133
134 unordered_df = pd.read_csv(StringIO(self.data_csv))
Javi Merino2a16a442014-06-21 19:23:45 +0100135 self.data_frame = unordered_df.set_index("Time")
Javi Merino04f27492014-04-02 09:59:23 +0100136
Javi Merinof78ea5b2014-03-31 17:51:38 +0100137 return self.data_frame
Javi Merinodf8316a2014-03-31 18:39:42 +0100138
Javi Merino51db3632014-06-13 11:24:51 +0100139 def plot_multivalue(self, values, title, width, height):
140 """Plot multiple values of the DataFrame
Javi Merino05983ef2014-04-08 12:54:20 +0100141
Javi Merino51db3632014-06-13 11:24:51 +0100142 values is an array with the keys of the DataFrame to plot
143 """
Javi Merinoc0e104d2014-06-17 17:31:13 +0100144
Javi Merino51db3632014-06-13 11:24:51 +0100145 dfr = self.get_data_frame()
Javi Merino051db6c2014-06-18 15:29:15 +0100146
Javi Merino3a736552014-06-19 19:22:44 +0100147 ax = pre_plot_setup(width, height)
148 dfr[values].plot(ax=ax)
149 post_plot_setup(ax, title=title)
Javi Merino7a7cd702014-04-14 15:41:15 +0100150
Javi Merino0e83b612014-06-05 11:45:23 +0100151class Thermal(BaseThermal):
152 """Process the thermal framework data in a ftrace dump"""
153 def __init__(self, path=None):
154 super(Thermal, self).__init__(
155 basepath=path,
Javi Merinoaf45d872014-07-03 09:49:40 +0100156 unique_word="thermal_temperature:",
Javi Merino0e83b612014-06-05 11:45:23 +0100157 )
158
Javi Merino516d5942014-06-26 15:06:04 +0100159 def plot_temperature(self, control_temperature=None, title="", width=None,
160 height=None, ylim="range"):
161 """Plot the temperature.
162
163 If control_temp is a pd.Series() representing the (possible)
164 variation of control_temp during the run, draw it using a
165 dashed yellow line. Otherwise, only the temperature is
166 plotted.
167
168 """
Javi Merinoc68737a2014-06-10 15:21:59 +0100169 dfr = self.get_data_frame()
170 title = normalize_title("Temperature", title)
171
Javi Merino3a736552014-06-19 19:22:44 +0100172 ax = pre_plot_setup(width, height)
173 (dfr["temp"] / 1000).plot(ax=ax)
Javi Merino516d5942014-06-26 15:06:04 +0100174 if control_temperature is not None:
175 control_temperature.plot(ax=ax, color="y", linestyle="--",
176 label="control temperature")
Javi Merino3a736552014-06-19 19:22:44 +0100177 post_plot_setup(ax, title=title, ylim=ylim)
Javi Merinoc68737a2014-06-10 15:21:59 +0100178
Javi Merinoc68737a2014-06-10 15:21:59 +0100179 plt.legend()
180
Javi Merino1e69e2c2014-06-04 18:25:09 +0100181class ThermalGovernor(BaseThermal):
Javi Merino5bd3d442014-04-08 12:55:13 +0100182 """Process the power allocator data in a ftrace dump"""
Javi Merino68461552014-04-08 11:40:09 +0100183 def __init__(self, path=None):
Javi Merino1e69e2c2014-06-04 18:25:09 +0100184 super(ThermalGovernor, self).__init__(
Javi Merino68461552014-04-08 11:40:09 +0100185 basepath=path,
Javi Merinobdf79d02014-07-03 09:50:21 +0100186 unique_word="max_allocatable_power",
Javi Merinoc2ec5682014-04-01 15:16:06 +0100187 )
188
189 def write_thermal_csv(self):
190 """Write the csv info in thermal.csv"""
191 if not self.data_csv:
192 self.parse_into_csv()
193
194 with open("thermal.csv", "w") as fout:
195 fout.write(self.data_csv)
196
Javi Merinoc00feff2014-04-14 15:41:51 +0100197 def plot_input_power(self, title="", width=None, height=None):
Javi Merinof1dcb2d2014-04-02 16:52:11 +0100198 """Plot input power"""
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100199 dfr = self.get_data_frame()
Javi Merinof7968a72014-07-03 15:35:02 +0100200 in_cols = [s for s in dfr.columns if re.match("req_power[0-9]+", s)]
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100201
Javi Merinoc00feff2014-04-14 15:41:51 +0100202 title = normalize_title("Input Power", title)
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100203 self.plot_multivalue(in_cols, title, width, height)
Javi Merino9c010772014-04-02 16:54:41 +0100204
Javi Merinoc00feff2014-04-14 15:41:51 +0100205 def plot_output_power(self, title="", width=None, height=None):
Javi Merino9c010772014-04-02 16:54:41 +0100206 """Plot output power"""
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100207 dfr = self.get_data_frame()
208 out_cols = [s for s in dfr.columns
Javi Merinof7968a72014-07-03 15:35:02 +0100209 if re.match("granted_power[0-9]+", s)]
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100210
Javi Merinoc00feff2014-04-14 15:41:51 +0100211 title = normalize_title("Output Power", title)
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100212 self.plot_multivalue(out_cols,
Javi Merinoc00feff2014-04-14 15:41:51 +0100213 title, width, height)
Javi Merinocd4a8272014-04-14 15:50:01 +0100214
Javi Merino9fc54852014-05-07 19:06:53 +0100215 def plot_inout_power(self, title="", width=None, height=None):
216 """Make multiple plots showing input and output power for each actor"""
217 dfr = self.get_data_frame()
218
219 actors = []
220 for col in dfr.columns:
221 match = re.match("P(.*)_in", col)
222 if match and col != "Ptot_in":
223 actors.append(match.group(1))
224
225 for actor in actors:
226 cols = ["P" + actor + "_in", "P" + actor + "_out"]
227 this_title = normalize_title(actor, title)
228 dfr[cols].plot(title=this_title)