blob: 627af6420059acea50db4a2180b244756fe133cd [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 Merino051db6c2014-06-18 15:29:15 +010011from plot_utils import set_plot_size, normalize_title
Javi Merino51db3632014-06-13 11:24:51 +010012
Javi Merinoc2ec5682014-04-01 15:16:06 +010013class BaseThermal(object):
Javi Merino5bd3d442014-04-08 12:55:13 +010014 """Base class to parse trace.dat dumps.
15
16 Don't use directly, create a subclass that defines the unique_word
17 you want to match in the output"""
Javi Merino68461552014-04-08 11:40:09 +010018 def __init__(self, basepath, unique_word):
19 if basepath is None:
20 basepath = "."
Javi Merinoc2ec5682014-04-01 15:16:06 +010021
Javi Merino68461552014-04-08 11:40:09 +010022 self.basepath = basepath
Javi Merino952815a2014-03-31 18:08:32 +010023 self.data_csv = ""
Javi Merinof78ea5b2014-03-31 17:51:38 +010024 self.data_frame = False
Javi Merino68461552014-04-08 11:40:09 +010025 self.unique_word = unique_word
Javi Merinoa2c990a2014-06-10 15:33:49 +010026 self.ax = None
Javi Merino68461552014-04-08 11:40:09 +010027
28 if not os.path.isfile(os.path.join(basepath, "trace.txt")):
29 self.__run_trace_cmd_report()
Javi Merino572049d2014-03-31 16:45:23 +010030
Javi Merino572049d2014-03-31 16:45:23 +010031 def __run_trace_cmd_report(self):
Javi Merino5bd3d442014-04-08 12:55:13 +010032 """Run "trace-cmd report > trace.txt".
33
34 Overwrites the contents of trace.txt if it exists."""
Javi Merinoee56c362014-03-31 17:30:34 +010035 from subprocess import check_output
36
Javi Merino68461552014-04-08 11:40:09 +010037 if not os.path.isfile(os.path.join(self.basepath, "trace.dat")):
Javi Merino1a3725a2014-03-31 18:35:15 +010038 raise IOError("No such file or directory: trace.dat")
39
Javi Merino68461552014-04-08 11:40:09 +010040 previous_path = os.getcwd()
41 os.chdir(self.basepath)
Javi Merino572049d2014-03-31 16:45:23 +010042
Javi Merino68461552014-04-08 11:40:09 +010043 # This would better be done with a context manager (i.e.
44 # http://stackoverflow.com/a/13197763/970766)
45 try:
46 with open(os.devnull) as devnull:
47 out = check_output(["trace-cmd", "report"], stderr=devnull)
48
49 finally:
50 os.chdir(previous_path)
51
Javi Merino5bd3d442014-04-08 12:55:13 +010052 with open(os.path.join(self.basepath, "trace.txt"), "w") as fout:
53 fout.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010054
Javi Merinoc2ec5682014-04-01 15:16:06 +010055 def parse_into_csv(self):
Javi Merino5bd3d442014-04-08 12:55:13 +010056 """Create a csv representation of the thermal data and store
57 it in self.data_csv"""
Javi Merinoc08ca682014-03-31 17:29:27 +010058 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
Javi Merino0e83b612014-06-05 11:45:23 +010059 pat_data = re.compile(r"[A-Za-z0-9_]+=([^ ]+)")
60 pat_header = re.compile(r"([A-Za-z0-9_]+)=[^ ]+")
Javi Merinoc08ca682014-03-31 17:29:27 +010061 header = ""
62
Javi Merino68461552014-04-08 11:40:09 +010063 with open(os.path.join(self.basepath, "trace.txt")) as fin:
Javi Merino952815a2014-03-31 18:08:32 +010064 for line in fin:
Javi Merinoc2ec5682014-04-01 15:16:06 +010065 if not re.search(self.unique_word, line):
Javi Merino952815a2014-03-31 18:08:32 +010066 continue
67
68 line = line[:-1]
69
Javi Merino5bd3d442014-04-08 12:55:13 +010070 timestamp_match = re.search(pat_timestamp, line)
71 timestamp = timestamp_match.group(1)
Javi Merino952815a2014-03-31 18:08:32 +010072
Javi Merinoc2ec5682014-04-01 15:16:06 +010073 data_start_idx = re.search(r"[A-Za-z0-9_]+=", line).start()
74 data_str = line[data_start_idx:]
Javi Merino952815a2014-03-31 18:08:32 +010075
76 if not header:
Javi Merino0af47212014-04-02 16:23:23 +010077 header = re.sub(pat_header, r"\1", data_str)
78 header = re.sub(r" ", r",", header)
Javi Merino952815a2014-03-31 18:08:32 +010079 header = "time," + header + "\n"
80 self.data_csv = header
81
Javi Merino0af47212014-04-02 16:23:23 +010082 parsed_data = re.sub(pat_data, r"\1", data_str)
83 parsed_data = re.sub(r" ", r",", parsed_data)
Javi Merino952815a2014-03-31 18:08:32 +010084
85 parsed_data = timestamp + "," + parsed_data + "\n"
86 self.data_csv += parsed_data
87
Javi Merinof78ea5b2014-03-31 17:51:38 +010088 def get_data_frame(self):
89 """Return a pandas data frame for the run"""
Javi Merino92f1a6d2014-04-10 16:30:36 +010090 if self.data_frame is None:
Javi Merinof78ea5b2014-03-31 17:51:38 +010091 return self.data_frame
92
Javi Merino952815a2014-03-31 18:08:32 +010093 if not self.data_csv:
Javi Merinoc2ec5682014-04-01 15:16:06 +010094 self.parse_into_csv()
Javi Merinof78ea5b2014-03-31 17:51:38 +010095
Javi Merinof0f51ff2014-04-10 12:34:53 +010096 if self.data_csv is "":
97 return pd.DataFrame()
98
99 unordered_df = pd.read_csv(StringIO(self.data_csv))
100 self.data_frame = unordered_df.set_index("time")
Javi Merino04f27492014-04-02 09:59:23 +0100101
Javi Merinof78ea5b2014-03-31 17:51:38 +0100102 return self.data_frame
Javi Merinodf8316a2014-03-31 18:39:42 +0100103
Javi Merino051db6c2014-06-18 15:29:15 +0100104 def pre_plot_setup(self, width=None, height=None):
Javi Merinoa2c990a2014-06-10 15:33:49 +0100105 """initialize a figure
Javi Merinofb2e8fd2014-04-08 12:27:38 +0100106
Javi Merino051db6c2014-06-18 15:29:15 +0100107 width and height are numbers, ylim is a tuple with min and max
108 values for the y axis. This function should be called before
109 any calls to plot()
110
111 """
112
Javi Merinoa2c990a2014-06-10 15:33:49 +0100113 set_plot_size(width, height)
Javi Merinofb2e8fd2014-04-08 12:27:38 +0100114
Javi Merinoa2c990a2014-06-10 15:33:49 +0100115 _, self.ax = plt.subplots()
116
Javi Merino051db6c2014-06-18 15:29:15 +0100117 def post_plot_setup(self, title="", ylim=None):
118 """Set xlabel, title and ylim of the plot
119
120 This has to be called after calls to .plot()
121
122 """
123
124 plt.xlabel("Time")
125 if title:
126 plt.title(title)
127
128 if not ylim:
129 cur_ylim = self.ax.get_ylim()
130 ylim = (cur_ylim[0] - 0.1 * (cur_ylim[1] - cur_ylim[0]),
131 cur_ylim[1] + 0.1 * (cur_ylim[1] - cur_ylim[0]))
132
133 self.ax.set_ylim(ylim[0], ylim[1])
134
Javi Merino51db3632014-06-13 11:24:51 +0100135 def plot_multivalue(self, values, title, width, height):
136 """Plot multiple values of the DataFrame
Javi Merino05983ef2014-04-08 12:54:20 +0100137
Javi Merino51db3632014-06-13 11:24:51 +0100138 values is an array with the keys of the DataFrame to plot
139 """
Javi Merinoc0e104d2014-06-17 17:31:13 +0100140
Javi Merino51db3632014-06-13 11:24:51 +0100141 dfr = self.get_data_frame()
Javi Merino051db6c2014-06-18 15:29:15 +0100142
143 self.pre_plot_setup(width, height)
Javi Merinoc0e104d2014-06-17 17:31:13 +0100144 dfr[values].plot(ax=self.ax)
Javi Merino051db6c2014-06-18 15:29:15 +0100145 self.post_plot_setup(title=title)
Javi Merino7a7cd702014-04-14 15:41:15 +0100146
Javi Merino0e83b612014-06-05 11:45:23 +0100147class Thermal(BaseThermal):
148 """Process the thermal framework data in a ftrace dump"""
149 def __init__(self, path=None):
150 super(Thermal, self).__init__(
151 basepath=path,
152 unique_word="thermal_zone=",
153 )
154
Javi Merinoa2c990a2014-06-10 15:33:49 +0100155 def plot_temperature(self, title="", width=None, height=None, ylim=None):
Javi Merinoc68737a2014-06-10 15:21:59 +0100156 """Plot the temperature"""
157 dfr = self.get_data_frame()
158 title = normalize_title("Temperature", title)
159
Javi Merino051db6c2014-06-18 15:29:15 +0100160 self.pre_plot_setup(width, height)
Javi Merinoa2c990a2014-06-10 15:33:49 +0100161 (dfr["temp"] / 1000).plot(ax=self.ax)
Javi Merino051db6c2014-06-18 15:29:15 +0100162 self.post_plot_setup(title=title, ylim=ylim)
Javi Merinoc68737a2014-06-10 15:21:59 +0100163
Javi Merinoc68737a2014-06-10 15:21:59 +0100164 plt.legend()
165
Javi Merino1e69e2c2014-06-04 18:25:09 +0100166class ThermalGovernor(BaseThermal):
Javi Merino5bd3d442014-04-08 12:55:13 +0100167 """Process the power allocator data in a ftrace dump"""
Javi Merino68461552014-04-08 11:40:09 +0100168 def __init__(self, path=None):
Javi Merino1e69e2c2014-06-04 18:25:09 +0100169 super(ThermalGovernor, self).__init__(
Javi Merino68461552014-04-08 11:40:09 +0100170 basepath=path,
171 unique_word="Ptot_out",
Javi Merinoc2ec5682014-04-01 15:16:06 +0100172 )
173
174 def write_thermal_csv(self):
175 """Write the csv info in thermal.csv"""
176 if not self.data_csv:
177 self.parse_into_csv()
178
179 with open("thermal.csv", "w") as fout:
180 fout.write(self.data_csv)
181
Javi Merinoa2c990a2014-06-10 15:33:49 +0100182 def plot_temperature(self, title="", width=None, height=None, ylim=None):
Javi Merinodf8316a2014-03-31 18:39:42 +0100183 """Plot the temperature"""
Javi Merino5bd3d442014-04-08 12:55:13 +0100184 dfr = self.get_data_frame()
Javi Merino9e186092014-04-08 15:41:41 +0100185 control_temp_series = (dfr["currT"] + dfr["deltaT"]) / 1000
Javi Merino7a7cd702014-04-14 15:41:15 +0100186 title = normalize_title("Temperature", title)
Javi Merino9e186092014-04-08 15:41:41 +0100187
Javi Merino051db6c2014-06-18 15:29:15 +0100188 self.pre_plot_setup(width, height)
Javi Merino9e186092014-04-08 15:41:41 +0100189
Javi Merinoa2c990a2014-06-10 15:33:49 +0100190 (dfr["currT"] / 1000).plot(ax=self.ax)
191 control_temp_series.plot(ax=self.ax, color="y", linestyle="--",
Javi Merino9e186092014-04-08 15:41:41 +0100192 label="control temperature")
193
Javi Merino051db6c2014-06-18 15:29:15 +0100194 self.post_plot_setup(title=title, ylim=ylim)
Javi Merino9e186092014-04-08 15:41:41 +0100195 plt.legend()
Javi Merino749b26a2014-03-31 19:17:30 +0100196
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()
200 in_cols = [s for s in dfr.columns
201 if re.match("P.*_in", s) and s != "Ptot_in"]
202
Javi Merinoc00feff2014-04-14 15:41:51 +0100203 title = normalize_title("Input Power", title)
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100204 self.plot_multivalue(in_cols, title, width, height)
Javi Merino9c010772014-04-02 16:54:41 +0100205
Javi Merinoc00feff2014-04-14 15:41:51 +0100206 def plot_output_power(self, title="", width=None, height=None):
Javi Merino9c010772014-04-02 16:54:41 +0100207 """Plot output power"""
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100208 dfr = self.get_data_frame()
209 out_cols = [s for s in dfr.columns
210 if re.match("P.*_out", s) and s != "Ptot_out"]
211
Javi Merinoc00feff2014-04-14 15:41:51 +0100212 title = normalize_title("Output Power", title)
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100213 self.plot_multivalue(out_cols,
Javi Merinoc00feff2014-04-14 15:41:51 +0100214 title, width, height)
Javi Merinocd4a8272014-04-14 15:50:01 +0100215
Javi Merino9fc54852014-05-07 19:06:53 +0100216 def plot_inout_power(self, title="", width=None, height=None):
217 """Make multiple plots showing input and output power for each actor"""
218 dfr = self.get_data_frame()
219
220 actors = []
221 for col in dfr.columns:
222 match = re.match("P(.*)_in", col)
223 if match and col != "Ptot_in":
224 actors.append(match.group(1))
225
226 for actor in actors:
227 cols = ["P" + actor + "_in", "P" + actor + "_out"]
228 this_title = normalize_title(actor, title)
229 dfr[cols].plot(title=this_title)