blob: e4ceffe7f8ea32b1fbcf0ffd5fb614dedb8e2673 [file] [log] [blame]
Javi Merino491cf732014-03-31 17:34:44 +01001#!/usr/bin/python
Javi Merino572049d2014-03-31 16:45:23 +01002"""Process the output of the power allocator trace in the current directory's trace.dat"""
3
4import os
Javi Merinoee56c362014-03-31 17:30:34 +01005import re
Javi Merino952815a2014-03-31 18:08:32 +01006from StringIO import StringIO
Javi Merinof78ea5b2014-03-31 17:51:38 +01007import pandas as pd
Javi Merinoa6399fb2014-03-31 19:17:08 +01008from matplotlib import pyplot as plt
Javi Merino572049d2014-03-31 16:45:23 +01009
Javi Merino7686f362014-04-02 11:26:40 +010010GOLDEN_RATIO = 1.618034
11
Javi Merinoc2ec5682014-04-01 15:16:06 +010012class BaseThermal(object):
13 def __init__(self, unique_word):
Javi Merino572049d2014-03-31 16:45:23 +010014 if not os.path.isfile("trace.txt"):
15 self.__run_trace_cmd_report()
Javi Merinoc2ec5682014-04-01 15:16:06 +010016
17 self.unique_word = unique_word
Javi Merino952815a2014-03-31 18:08:32 +010018 self.data_csv = ""
Javi Merinof78ea5b2014-03-31 17:51:38 +010019 self.data_frame = False
Javi Merino572049d2014-03-31 16:45:23 +010020
Javi Merino572049d2014-03-31 16:45:23 +010021 def __run_trace_cmd_report(self):
22 """Run "trace-cmd report > trace.txt". Overwrites the contents of trace.txt if it exists."""
Javi Merinoee56c362014-03-31 17:30:34 +010023 from subprocess import check_output
24
Javi Merino1a3725a2014-03-31 18:35:15 +010025 if not os.path.isfile("trace.dat"):
26 raise IOError("No such file or directory: trace.dat")
27
Javi Merino572049d2014-03-31 16:45:23 +010028 with open(os.devnull) as devnull:
Javi Merinoee56c362014-03-31 17:30:34 +010029 out = check_output(["trace-cmd", "report"], stderr=devnull)
Javi Merino572049d2014-03-31 16:45:23 +010030
31 with open("trace.txt", "w") as f:
32 f.write(out)
Javi Merinoc08ca682014-03-31 17:29:27 +010033
Javi Merinoc2ec5682014-04-01 15:16:06 +010034 def parse_into_csv(self):
Javi Merino952815a2014-03-31 18:08:32 +010035 """Create a csv representation of the thermal data and store it in self.data_csv"""
Javi Merinoc08ca682014-03-31 17:29:27 +010036 pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
Javi Merinoe284d632014-04-02 18:57:06 +010037 pat_data = re.compile(r"[A-Za-z0-9_]+=([-a-f0-9]+)")
38 pat_header = re.compile(r"([A-Za-z0-9_]+)=[-a-f0-9]+")
Javi Merinoc08ca682014-03-31 17:29:27 +010039 header = ""
40
Javi Merino952815a2014-03-31 18:08:32 +010041 with open("trace.txt") as fin:
42 for line in fin:
Javi Merinoc2ec5682014-04-01 15:16:06 +010043 if not re.search(self.unique_word, line):
Javi Merino952815a2014-03-31 18:08:32 +010044 continue
45
46 line = line[:-1]
47
48 m = re.search(pat_timestamp, line)
49 timestamp = m.group(1)
50
Javi Merinoc2ec5682014-04-01 15:16:06 +010051 data_start_idx = re.search(r"[A-Za-z0-9_]+=", line).start()
52 data_str = line[data_start_idx:]
Javi Merino952815a2014-03-31 18:08:32 +010053
54 if not header:
Javi Merino0af47212014-04-02 16:23:23 +010055 header = re.sub(pat_header, r"\1", data_str)
56 header = re.sub(r" ", r",", header)
Javi Merino952815a2014-03-31 18:08:32 +010057 header = "time," + header + "\n"
58 self.data_csv = header
59
Javi Merino0af47212014-04-02 16:23:23 +010060 parsed_data = re.sub(pat_data, r"\1", data_str)
61 parsed_data = re.sub(r" ", r",", parsed_data)
Javi Merino952815a2014-03-31 18:08:32 +010062
63 parsed_data = timestamp + "," + parsed_data + "\n"
64 self.data_csv += parsed_data
65
Javi Merinof78ea5b2014-03-31 17:51:38 +010066 def get_data_frame(self):
67 """Return a pandas data frame for the run"""
68 if self.data_frame:
69 return self.data_frame
70
Javi Merino952815a2014-03-31 18:08:32 +010071 if not self.data_csv:
Javi Merinoc2ec5682014-04-01 15:16:06 +010072 self.parse_into_csv()
Javi Merinof78ea5b2014-03-31 17:51:38 +010073
Javi Merino04f27492014-04-02 09:59:23 +010074 try:
75 self.data_frame = pd.read_csv(StringIO(self.data_csv)).set_index("time")
76 except StopIteration:
77 if not self.data_frame:
78 return pd.DataFrame()
79 raise
80
Javi Merinof78ea5b2014-03-31 17:51:38 +010081 return self.data_frame
Javi Merinodf8316a2014-03-31 18:39:42 +010082
Javi Merinoc2ec5682014-04-01 15:16:06 +010083class Thermal(BaseThermal):
84 def __init__(self):
85 super(Thermal, self).__init__(
86 unique_word="Ptot_out"
87 )
88
89 def write_thermal_csv(self):
90 """Write the csv info in thermal.csv"""
91 if not self.data_csv:
92 self.parse_into_csv()
93
94 with open("thermal.csv", "w") as fout:
95 fout.write(self.data_csv)
96
Javi Merino7686f362014-04-02 11:26:40 +010097 def set_plot_size(self, width, height):
98 """Set the plot size.
99
100 This has to be called before plot()
101 """
102 if height is None:
103 if width is None:
104 height = 6
105 width = 10
106 else:
107 height = width / GOLDEN_RATIO
108 else:
109 if width is None:
110 width = height * GOLDEN_RATIO
111
112 plt.figure(figsize=(width, height))
113
Javi Merinoa6399fb2014-03-31 19:17:08 +0100114 def __default_plot_settings(self, title=""):
Javi Merino1b74ef12014-04-02 11:27:01 +0100115 """Set xlabel and title of the plot
116
117 This has to be called after the plot() command
118 """
119
Javi Merinoa6399fb2014-03-31 19:17:08 +0100120 plt.xlabel("Time")
121 if title:
122 plt.title(title)
123
Javi Merino7686f362014-04-02 11:26:40 +0100124 def plot_temperature(self, width=None, height=None):
Javi Merinodf8316a2014-03-31 18:39:42 +0100125 """Plot the temperature"""
Javi Merino7686f362014-04-02 11:26:40 +0100126 self.set_plot_size(width, height)
Javi Merinodf8316a2014-03-31 18:39:42 +0100127 df = self.get_data_frame()
128 (df["currT"] / 1000).plot()
Javi Merinoa6399fb2014-03-31 19:17:08 +0100129 self.__default_plot_settings(title="Temperature")
Javi Merino749b26a2014-03-31 19:17:30 +0100130
Javi Merinof1dcb2d2014-04-02 16:52:11 +0100131 def plot_multivalue(self, values, title, width, height):
132 """Plot multiple values of the DataFrame
133
134 values is an array with the keys of the DataFrame to plot
135 """
Javi Merino7686f362014-04-02 11:26:40 +0100136 self.set_plot_size(width, height)
Javi Merino749b26a2014-03-31 19:17:30 +0100137 df = self.get_data_frame()
Javi Merinof1dcb2d2014-04-02 16:52:11 +0100138 df[values].plot()
139 self.__default_plot_settings(title=title)
140
141 def plot_input_power(self, width=None, height=None):
142 """Plot input power"""
143 self.plot_multivalue( ["Pa7_in", "Pa15_in", "Pgpu_in"], "Input Power", width, height)
Javi Merino9c010772014-04-02 16:54:41 +0100144
145 def plot_output_power(self, width=None, height=None):
146 """Plot output power"""
147 self.plot_multivalue( ["Pa7_out", "Pa15_out", "Pgpu_out"], "Output Power", width, height)