blob: 0f7ba65a9904f87ec7124cd02a872a220b32d5ef [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 Merino7686f362014-04-02 11:26:40 +010011GOLDEN_RATIO = 1.618034
12
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
26
27 if not os.path.isfile(os.path.join(basepath, "trace.txt")):
28 self.__run_trace_cmd_report()
Javi Merino572049d2014-03-31 16:45:23 +010029
Javi Merino572049d2014-03-31 16:45:23 +010030 def __run_trace_cmd_report(self):
Javi Merino5bd3d442014-04-08 12:55:13 +010031 """Run "trace-cmd report > trace.txt".
32
33 Overwrites the contents of trace.txt if it exists."""
Javi Merinoee56c362014-03-31 17:30:34 +010034 from subprocess import check_output
35
Javi Merino68461552014-04-08 11:40:09 +010036 if not os.path.isfile(os.path.join(self.basepath, "trace.dat")):
Javi Merino1a3725a2014-03-31 18:35:15 +010037 raise IOError("No such file or directory: trace.dat")
38
Javi Merino68461552014-04-08 11:40:09 +010039 previous_path = os.getcwd()
40 os.chdir(self.basepath)
Javi Merino572049d2014-03-31 16:45:23 +010041
Javi Merino68461552014-04-08 11:40:09 +010042 # This would better be done with a context manager (i.e.
43 # http://stackoverflow.com/a/13197763/970766)
44 try:
45 with open(os.devnull) as devnull:
46 out = check_output(["trace-cmd", "report"], stderr=devnull)
47
48 finally:
49 os.chdir(previous_path)
50
Javi Merino5bd3d442014-04-08 12:55:13 +010051 with open(os.path.join(self.basepath, "trace.txt"), "w") as fout:
52 fout.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010053
Javi Merinoc2ec5682014-04-01 15:16:06 +010054 def parse_into_csv(self):
Javi Merino5bd3d442014-04-08 12:55:13 +010055 """Create a csv representation of the thermal data and store
56 it in self.data_csv"""
Javi Merinoc08ca682014-03-31 17:29:27 +010057 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
Javi Merino0e83b612014-06-05 11:45:23 +010058 pat_data = re.compile(r"[A-Za-z0-9_]+=([^ ]+)")
59 pat_header = re.compile(r"([A-Za-z0-9_]+)=[^ ]+")
Javi Merinoc08ca682014-03-31 17:29:27 +010060 header = ""
61
Javi Merino68461552014-04-08 11:40:09 +010062 with open(os.path.join(self.basepath, "trace.txt")) as fin:
Javi Merino952815a2014-03-31 18:08:32 +010063 for line in fin:
Javi Merinoc2ec5682014-04-01 15:16:06 +010064 if not re.search(self.unique_word, line):
Javi Merino952815a2014-03-31 18:08:32 +010065 continue
66
67 line = line[:-1]
68
Javi Merino5bd3d442014-04-08 12:55:13 +010069 timestamp_match = re.search(pat_timestamp, line)
70 timestamp = timestamp_match.group(1)
Javi Merino952815a2014-03-31 18:08:32 +010071
Javi Merinoc2ec5682014-04-01 15:16:06 +010072 data_start_idx = re.search(r"[A-Za-z0-9_]+=", line).start()
73 data_str = line[data_start_idx:]
Javi Merino952815a2014-03-31 18:08:32 +010074
75 if not header:
Javi Merino0af47212014-04-02 16:23:23 +010076 header = re.sub(pat_header, r"\1", data_str)
77 header = re.sub(r" ", r",", header)
Javi Merino952815a2014-03-31 18:08:32 +010078 header = "time," + header + "\n"
79 self.data_csv = header
80
Javi Merino0af47212014-04-02 16:23:23 +010081 parsed_data = re.sub(pat_data, r"\1", data_str)
82 parsed_data = re.sub(r" ", r",", parsed_data)
Javi Merino952815a2014-03-31 18:08:32 +010083
84 parsed_data = timestamp + "," + parsed_data + "\n"
85 self.data_csv += parsed_data
86
Javi Merinof78ea5b2014-03-31 17:51:38 +010087 def get_data_frame(self):
88 """Return a pandas data frame for the run"""
Javi Merino92f1a6d2014-04-10 16:30:36 +010089 if self.data_frame is None:
Javi Merinof78ea5b2014-03-31 17:51:38 +010090 return self.data_frame
91
Javi Merino952815a2014-03-31 18:08:32 +010092 if not self.data_csv:
Javi Merinoc2ec5682014-04-01 15:16:06 +010093 self.parse_into_csv()
Javi Merinof78ea5b2014-03-31 17:51:38 +010094
Javi Merinof0f51ff2014-04-10 12:34:53 +010095 if self.data_csv is "":
96 return pd.DataFrame()
97
98 unordered_df = pd.read_csv(StringIO(self.data_csv))
99 self.data_frame = unordered_df.set_index("time")
Javi Merino04f27492014-04-02 09:59:23 +0100100
Javi Merinof78ea5b2014-03-31 17:51:38 +0100101 return self.data_frame
Javi Merinodf8316a2014-03-31 18:39:42 +0100102
Javi Merinofb2e8fd2014-04-08 12:27:38 +0100103def set_plot_size(width, height):
104 """Set the plot size.
105
106 This has to be called before calls to .plot()
107 """
108 if height is None:
109 if width is None:
110 height = 6
111 width = 10
112 else:
113 height = width / GOLDEN_RATIO
114 else:
115 if width is None:
116 width = height * GOLDEN_RATIO
117
118 plt.figure(figsize=(width, height))
119
Javi Merino05983ef2014-04-08 12:54:20 +0100120def default_plot_settings(title=""):
121 """Set xlabel and title of the plot
122
123 This has to be called after calls to .plot()
124 """
125
126 plt.xlabel("Time")
127 if title:
128 plt.title(title)
129
Javi Merino7a7cd702014-04-14 15:41:15 +0100130def normalize_title(title, opt_title):
131 """
132 Return a string with that contains the title and opt_title if it's not the empty string
133
134 See test_normalize_title() for usage
135 """
136 if opt_title is not "":
137 title = opt_title + " - " + title
138
139 return title
140
Javi Merino0e83b612014-06-05 11:45:23 +0100141class Thermal(BaseThermal):
142 """Process the thermal framework data in a ftrace dump"""
143 def __init__(self, path=None):
144 super(Thermal, self).__init__(
145 basepath=path,
146 unique_word="thermal_zone=",
147 )
148
Javi Merinoc68737a2014-06-10 15:21:59 +0100149 def plot_temperature(self, title="", width=None, height=None):
150 """Plot the temperature"""
151 dfr = self.get_data_frame()
152 title = normalize_title("Temperature", title)
153
154 set_plot_size(width, height)
155
156 (dfr["temp"] / 1000).plot()
157
158 default_plot_settings(title=title)
159 plt.legend()
160
161
Javi Merino1e69e2c2014-06-04 18:25:09 +0100162class ThermalGovernor(BaseThermal):
Javi Merino5bd3d442014-04-08 12:55:13 +0100163 """Process the power allocator data in a ftrace dump"""
Javi Merino68461552014-04-08 11:40:09 +0100164 def __init__(self, path=None):
Javi Merino1e69e2c2014-06-04 18:25:09 +0100165 super(ThermalGovernor, self).__init__(
Javi Merino68461552014-04-08 11:40:09 +0100166 basepath=path,
167 unique_word="Ptot_out",
Javi Merinoc2ec5682014-04-01 15:16:06 +0100168 )
169
170 def write_thermal_csv(self):
171 """Write the csv info in thermal.csv"""
172 if not self.data_csv:
173 self.parse_into_csv()
174
175 with open("thermal.csv", "w") as fout:
176 fout.write(self.data_csv)
177
Javi Merinoa3553e92014-04-08 15:57:08 +0100178 def plot_temperature(self, title="", width=None, height=None):
Javi Merinodf8316a2014-03-31 18:39:42 +0100179 """Plot the temperature"""
Javi Merino5bd3d442014-04-08 12:55:13 +0100180 dfr = self.get_data_frame()
Javi Merino9e186092014-04-08 15:41:41 +0100181 control_temp_series = (dfr["currT"] + dfr["deltaT"]) / 1000
Javi Merino7a7cd702014-04-14 15:41:15 +0100182 title = normalize_title("Temperature", title)
Javi Merino9e186092014-04-08 15:41:41 +0100183
184 set_plot_size(width, height)
185
Javi Merino5bd3d442014-04-08 12:55:13 +0100186 (dfr["currT"] / 1000).plot()
Javi Merino9e186092014-04-08 15:41:41 +0100187 control_temp_series.plot(color="y", linestyle="--",
188 label="control temperature")
189
Javi Merinoa3553e92014-04-08 15:57:08 +0100190 default_plot_settings(title=title)
Javi Merino9e186092014-04-08 15:41:41 +0100191 plt.legend()
Javi Merino749b26a2014-03-31 19:17:30 +0100192
Javi Merinof1dcb2d2014-04-02 16:52:11 +0100193 def plot_multivalue(self, values, title, width, height):
194 """Plot multiple values of the DataFrame
195
196 values is an array with the keys of the DataFrame to plot
197 """
Javi Merinofb2e8fd2014-04-08 12:27:38 +0100198 set_plot_size(width, height)
Javi Merino5bd3d442014-04-08 12:55:13 +0100199 dfr = self.get_data_frame()
200 dfr[values].plot()
Javi Merino05983ef2014-04-08 12:54:20 +0100201 default_plot_settings(title=title)
Javi Merinof1dcb2d2014-04-02 16:52:11 +0100202
Javi Merinoc00feff2014-04-14 15:41:51 +0100203 def plot_input_power(self, title="", width=None, height=None):
Javi Merinof1dcb2d2014-04-02 16:52:11 +0100204 """Plot input power"""
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100205 dfr = self.get_data_frame()
206 in_cols = [s for s in dfr.columns
207 if re.match("P.*_in", s) and s != "Ptot_in"]
208
Javi Merinoc00feff2014-04-14 15:41:51 +0100209 title = normalize_title("Input Power", title)
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100210 self.plot_multivalue(in_cols, title, width, height)
Javi Merino9c010772014-04-02 16:54:41 +0100211
Javi Merinoc00feff2014-04-14 15:41:51 +0100212 def plot_output_power(self, title="", width=None, height=None):
Javi Merino9c010772014-04-02 16:54:41 +0100213 """Plot output power"""
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100214 dfr = self.get_data_frame()
215 out_cols = [s for s in dfr.columns
216 if re.match("P.*_out", s) and s != "Ptot_out"]
217
Javi Merinoc00feff2014-04-14 15:41:51 +0100218 title = normalize_title("Output Power", title)
Javi Merinoe0ddf0d2014-05-07 18:40:12 +0100219 self.plot_multivalue(out_cols,
Javi Merinoc00feff2014-04-14 15:41:51 +0100220 title, width, height)
Javi Merinocd4a8272014-04-14 15:50:01 +0100221
Javi Merino9fc54852014-05-07 19:06:53 +0100222 def plot_inout_power(self, title="", width=None, height=None):
223 """Make multiple plots showing input and output power for each actor"""
224 dfr = self.get_data_frame()
225
226 actors = []
227 for col in dfr.columns:
228 match = re.match("P(.*)_in", col)
229 if match and col != "Ptot_in":
230 actors.append(match.group(1))
231
232 for actor in actors:
233 cols = ["P" + actor + "_in", "P" + actor + "_out"]
234 this_title = normalize_title(actor, title)
235 dfr[cols].plot(title=this_title)
236
Javi Merinocd4a8272014-04-14 15:50:01 +0100237 def summary_plots(self, **kwords):
238 self.plot_temperature(**kwords)
239 self.plot_input_power(**kwords)
240 self.plot_output_power(**kwords)