<commit_before>#!/usr/bin/python3
"""Process the output of the power allocator trace in the current directory's trace.dat"""

import os
import re, subprocess

class Thermal(object):
    def __init__(self):
        if not os.path.isfile("trace.txt"):
            self.__run_trace_cmd_report()


    def __run_trace_cmd_report(self):
        """Run "trace-cmd report > trace.txt".  Overwrites the contents of trace.txt if it exists."""
        with open(os.devnull) as devnull:
            out = subprocess.check_output(["trace-cmd", "report"], stderr=devnull)

        with open("trace.txt", "w") as f:
            f.write(out)

    def write_thermal_csv(self):
        pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
        pat_data = re.compile(r"[A-Za-z0-9_]+=([0-9]+) ")
        header = ""

        with open("thermal.csv", "w") as fout:
            with open("trace.txt") as fin:
                for line in fin:
                    if not re.search("Ptot_out", line):
                        continue

                    line = line[:-1]

                    m = re.search(pat_timestamp, line)<commit_msg>thermal: move the subprocess import to the function that uses it<commit_after>#!/usr/bin/python3
"""Process the output of the power allocator trace in the current directory's trace.dat"""

import os
import re

class Thermal(object):
    def __init__(self):
        if not os.path.isfile("trace.txt"):
            self.__run_trace_cmd_report()


    def __run_trace_cmd_report(self):
        """Run "trace-cmd report > trace.txt".  Overwrites the contents of trace.txt if it exists."""
        from subprocess import check_output

        with open(os.devnull) as devnull:
            out = check_output(["trace-cmd", "report"], stderr=devnull)

        with open("trace.txt", "w") as f:
            f.write(out)

    def write_thermal_csv(self):
        pat_timestamp = re.compile(r"([0-9]+\.[0-9]+):")
        pat_data = re.compile(r"[A-Za-z0-9_]+=([0-9]+) ")
        header = ""

        with open("thermal.csv", "w") as fout:
            with open("trace.txt") as fin:
                for line in fin:
                    if not re.search("Ptot_out", line):
                        continue

                    line = line[:-1]

                    m = re.search(pat_timestamp, line)
{'fraction_non_alphanumeric': 0.13689700130378096, 'fraction_numerical': 0.008691873098652759, 'mean_word_length': 2.1024258760107815, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5236047', 'n_tokens_mistral': 736, 'n_tokens_neox': 696, 'n_words': 182}