blob: 9145c49cafacb6321f8050b82114efebc0061e15 [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 Merinoe4c1d452014-04-01 17:00:47 +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 Merinoc2ec5682014-04-01 15:16:06 +010055 header = re.sub(pat_header, r"\1,", data_str)
Javi Merino952815a2014-03-31 18:08:32 +010056 header = header[:-1]
57 header = "time," + header + "\n"
58 self.data_csv = header
59
60 parsed_data = re.sub(pat_data, r"\1,", data_str)
61 # Drop the last comma
62 parsed_data = parsed_data[:-1]
63
64 parsed_data = timestamp + "," + parsed_data + "\n"
65 self.data_csv += parsed_data
66
Javi Merinof78ea5b2014-03-31 17:51:38 +010067 def get_data_frame(self):
68 """Return a pandas data frame for the run"""
69 if self.data_frame:
70 return self.data_frame
71
Javi Merino952815a2014-03-31 18:08:32 +010072 if not self.data_csv:
Javi Merinoc2ec5682014-04-01 15:16:06 +010073 self.parse_into_csv()
Javi Merinof78ea5b2014-03-31 17:51:38 +010074
Javi Merino04f27492014-04-02 09:59:23 +010075 try:
76 self.data_frame = pd.read_csv(StringIO(self.data_csv)).set_index("time")
77 except StopIteration:
78 if not self.data_frame:
79 return pd.DataFrame()
80 raise
81
Javi Merinof78ea5b2014-03-31 17:51:38 +010082 return self.data_frame
Javi Merinodf8316a2014-03-31 18:39:42 +010083
Javi Merinoc2ec5682014-04-01 15:16:06 +010084class Thermal(BaseThermal):
85 def __init__(self):
86 super(Thermal, self).__init__(
87 unique_word="Ptot_out"
88 )
89
90 def write_thermal_csv(self):
91 """Write the csv info in thermal.csv"""
92 if not self.data_csv:
93 self.parse_into_csv()
94
95 with open("thermal.csv", "w") as fout:
96 fout.write(self.data_csv)
97
Javi Merino7686f362014-04-02 11:26:40 +010098 def set_plot_size(self, width, height):
99 """Set the plot size.
100
101 This has to be called before plot()
102 """
103 if height is None:
104 if width is None:
105 height = 6
106 width = 10
107 else:
108 height = width / GOLDEN_RATIO
109 else:
110 if width is None:
111 width = height * GOLDEN_RATIO
112
113 plt.figure(figsize=(width, height))
114
Javi Merinoa6399fb2014-03-31 19:17:08 +0100115 def __default_plot_settings(self, title=""):
Javi Merino1b74ef12014-04-02 11:27:01 +0100116 """Set xlabel and title of the plot
117
118 This has to be called after the plot() command
119 """
120
Javi Merinoa6399fb2014-03-31 19:17:08 +0100121 plt.xlabel("Time")
122 if title:
123 plt.title(title)
124
Javi Merino7686f362014-04-02 11:26:40 +0100125 def plot_temperature(self, width=None, height=None):
Javi Merinodf8316a2014-03-31 18:39:42 +0100126 """Plot the temperature"""
Javi Merino7686f362014-04-02 11:26:40 +0100127 self.set_plot_size(width, height)
Javi Merinodf8316a2014-03-31 18:39:42 +0100128 df = self.get_data_frame()
129 (df["currT"] / 1000).plot()
Javi Merinoa6399fb2014-03-31 19:17:08 +0100130 self.__default_plot_settings(title="Temperature")
Javi Merino749b26a2014-03-31 19:17:30 +0100131
Javi Merino7686f362014-04-02 11:26:40 +0100132 def plot_input_power(self, width=None, height=None):
Javi Merino749b26a2014-03-31 19:17:30 +0100133 """Plot input power"""
Javi Merino7686f362014-04-02 11:26:40 +0100134 self.set_plot_size(width, height)
Javi Merino749b26a2014-03-31 19:17:30 +0100135 df = self.get_data_frame()
136 df[["Pa7_in", "Pa15_in", "Pgpu_in"]].plot()
137 self.__default_plot_settings(title="Input Power")