Javi Merino | 572049d | 2014-03-31 16:45:23 +0100 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | """Process the output of the power allocator trace in the current directory's trace.dat""" |
| 3 | |
| 4 | import os |
Javi Merino | c08ca68 | 2014-03-31 17:29:27 +0100 | [diff] [blame^] | 5 | import re, subprocess |
Javi Merino | 572049d | 2014-03-31 16:45:23 +0100 | [diff] [blame] | 6 | |
| 7 | class Thermal(object): |
| 8 | def __init__(self): |
| 9 | if not os.path.isfile("trace.txt"): |
| 10 | self.__run_trace_cmd_report() |
| 11 | |
| 12 | |
| 13 | def __run_trace_cmd_report(self): |
| 14 | """Run "trace-cmd report > trace.txt". Overwrites the contents of trace.txt if it exists.""" |
| 15 | with open(os.devnull) as devnull: |
| 16 | out = subprocess.check_output(["trace-cmd", "report"], stderr=devnull) |
| 17 | |
| 18 | with open("trace.txt", "w") as f: |
| 19 | f.write(out) |
Javi Merino | c08ca68 | 2014-03-31 17:29:27 +0100 | [diff] [blame^] | 20 | |
| 21 | def write_thermal_csv(self): |
| 22 | pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):") |
| 23 | pat_data = re.compile(r"[A-Za-z0-9_]+=([0-9]+) ") |
| 24 | header = "" |
| 25 | |
| 26 | with open("thermal.csv", "w") as fout: |
| 27 | with open("trace.txt") as fin: |
| 28 | for line in fin: |
| 29 | if not re.search("Ptot_out", line): |
| 30 | continue |
| 31 | |
| 32 | line = line[:-1] |
| 33 | |
| 34 | m = re.search(pat_timestamp, line) |
| 35 | timestamp = m.group(1) |
| 36 | |
| 37 | semi_idx = line.index(" : ") |
| 38 | data_str = line[semi_idx + 3:] |
| 39 | |
| 40 | if not header: |
| 41 | header = re.sub(r"([A-Za-z0-9_]+)=[0-9]+ ", r"\1,", data_str) |
| 42 | header = header[:-1] |
| 43 | header = "time," + header + "\n" |
| 44 | fout.write(header) |
| 45 | |
| 46 | parsed_data = re.sub(pat_data, r"\1,", data_str) |
| 47 | # Drop the last comma |
| 48 | parsed_data = parsed_data[:-1] |
| 49 | |
| 50 | parsed_data = timestamp + "," + parsed_data + "\n" |
| 51 | fout.write(parsed_data) |