aboutsummaryrefslogtreecommitdiff
path: root/tests/test_thermal.py
blob: a61b0403e33f5541ecd41af2832ef53f8a1d06af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/python

import unittest
import os, sys
import matplotlib, re, shutil, tempfile
import pandas as pd

import utils_tests
from cr2 import Thermal, ThermalGovernor
sys.path.append(os.path.join(utils_tests.TESTS_DIRECTORY, "..", "cr2"))
import thermal

class TestThermalMethods(unittest.TestCase):
    """Test simple methods that don't need to set up a directory"""
    def test_trace_parser_explode_array(self):
        """Basic test of trace_parser_explode_array()"""

        line = "cpus=0000000f freq=1400000 raw_cpu_power=189 load={3 2 12 2} power=14"
        expected = "cpus=0000000f freq=1400000 raw_cpu_power=189 load0=3 load1=2 load2=12 load3=2 power=14"

        self.assertEquals(thermal.trace_parser_explode_array(line), expected)

    def test_trace_parser_explode_array_nop(self):
        """trace_parser_explode_array() returns the same string if there's no array in it"""

        line = "cpus=0000000f freq=1400000 raw_cpu_power=189 load0=3 load1=2 load2=12 load3=2 power=14"
        self.assertEquals(thermal.trace_parser_explode_array(line), line)

    def test_trace_parser_explode_array_2(self):
        """trace_parser_explode_array() works if there's two arrays in the string"""

        line = "cpus=0000000f freq=1400000 load={3 2 12 2} power=14 req_power={10 7 2 34}"
        expected = "cpus=0000000f freq=1400000 load0=3 load1=2 load2=12 load3=2 power=14 req_power0=10 req_power1=7 req_power2=2 req_power3=34"

        self.assertEquals(thermal.trace_parser_explode_array(line), expected)

class BaseTestThermal(utils_tests.SetupDirectory):
    def __init__(self, *args, **kwargs):
        super(BaseTestThermal, self).__init__(
             ["trace.dat"],
             *args,
             **kwargs)

class TestThermalBase(utils_tests.SetupDirectory):
    """Incomplete tests for the ThermalBase class"""

    def __init__(self, *args, **kwargs):
        super(TestThermalBase, self).__init__(
             [],
             *args,
             **kwargs)

    def test_parse_into_csv_empty_array(self):
        """Test that trace that has an empty array creates valid csv"""

        in_data = """     kworker/4:1-397   [004]   720.741315: thermal_power_actor_cpu_get_dyn_power: cpus=000000f0 freq=1900000 raw_cpu_power=1259 load={} power=61
     kworker/4:1-397   [004]   720.741349: thermal_power_actor_cpu_get_dyn_power: cpus=0000000f freq=1400000 raw_cpu_power=189 load={} power=14"""
        expected_header = "Time,cpus,freq,raw_cpu_power,power"
        expected_first_data = "720.741315,000000f0,1900000,1259,61"

        with open("trace.txt", "w") as fout:
            fout.write(in_data)

        base = thermal.BaseThermal(None, "thermal_power_actor_cpu_get_dyn_power")
        base.parse_into_csv()

        data_csv_lines = base.data_csv.split('\n')

        self.assertEquals(data_csv_lines[0], expected_header)
        self.assertEquals(data_csv_lines[1], expected_first_data)

class TestThermal(BaseTestThermal):
    def test_get_dataframe(self):
        df = Thermal().get_data_frame()

        self.assertTrue("thermal_zone" in df.columns)
        self.assertEquals(df["temp"].iloc[0], 29000)

    def test_plot_temperature(self):
        """Test ThermalGovernor.plot_temperature()

        Can't check that the graph is ok, so just see that the method
        doesn't blow up

        """

        th_data = Thermal()
        dfr = th_data.get_data_frame()
        ct_series = pd.Series([57, 57], index=(dfr.index[0], dfr.index[-1]))

        th_data.plot_temperature()
        matplotlib.pyplot.close('all')

        th_data.plot_temperature(title="Antutu", control_temperature=ct_series)
        matplotlib.pyplot.close('all')

        th_data.plot_temperature(title="Antutu", ylim=[0, 60])
        matplotlib.pyplot.close('all')

class TestThermalGovernor(BaseTestThermal):
    def test_do_txt_if_not_there(self):
        c = ThermalGovernor()

        found = False
        with open("trace.txt") as f:
            for line in f:
                if re.search("thermal", line):
                    found = True
                    break

        self.assertTrue(found)

    def test_fail_if_no_trace_dat(self):
        """Raise an IOError if there's no trace.dat and trace.txt"""
        os.remove("trace.dat")
        self.assertRaises(IOError, ThermalGovernor)

    def test_get_thermal_csv(self):
        ThermalGovernor().write_thermal_csv()
        first_data_line = '720.741380,0,61,14,75,2608,5036,756,8400,8400,8511,50000,7000\n'

        with open("thermal.csv") as f:
            first_line = f.readline()
            self.assertTrue(first_line.startswith("Time,Pgpu_in"))

            second_line = f.readline()
            self.assertEquals(second_line, first_data_line)

    def test_get_dataframe(self):
        df = ThermalGovernor().get_data_frame()

        self.assertTrue(len(df) > 0)
        self.assertEquals(df["currT"].iloc[0], 50000)
        self.assertTrue("Ptot_out" in df.columns)
        self.assertFalse("time" in df.columns)

    def test_plot_input_power(self):
        """Test plot_input_power()

        Can't check that the graph is ok, so just see that the method doesn't blow up"""
        ThermalGovernor().plot_input_power()
        ThermalGovernor().plot_input_power(title="Antutu")
        matplotlib.pyplot.close('all')

    def test_plot_output_power(self):
        """Test plot_output_power()

        Can't check that the graph is ok, so just see that the method doesn't blow up"""
        ThermalGovernor().plot_output_power()
        ThermalGovernor().plot_output_power(title="Antutu")
        matplotlib.pyplot.close('all')

    def test_plot_inout_power(self):
        """Test plot_inout_power()

        Can't check that the graph is ok, so just see that the method doesn't blow up"""
        ThermalGovernor().plot_inout_power()
        ThermalGovernor().plot_inout_power(title="Antutu")
        matplotlib.pyplot.close('all')

    def test_other_directory(self):
        """ThermalGovernor can grab the trace.dat from other directories"""

        other_random_dir = tempfile.mkdtemp()
        os.chdir(other_random_dir)

        t = ThermalGovernor(self.out_dir)
        df = t.get_data_frame()

        self.assertTrue(len(df) > 0)
        self.assertEquals(os.getcwd(), other_random_dir)

    def test_double_thermal(self):
        """Make sure that multiple invocations of ThermalGovernor work fine

        It should memoize the DataFrame() from the call to
        get_data_frame() to the plot_temperature() one and the latter
        shouldn't bomb

        """

        t = ThermalGovernor()
        dfr = t.get_data_frame()
        t.plot_inout_power()

class TestEmptyThermalGovernor(unittest.TestCase):
    def setUp(self):
        self.previous_dir = os.getcwd()
        self.out_dir = tempfile.mkdtemp()
        os.chdir(self.out_dir)
        with open("trace.txt", "w") as fout:
            fout.write("""version = 6
cpus=8
CPU:7 [204600 EVENTS DROPPED]
           <...>-3979  [007]   217.975284: sched_stat_runtime:   comm=Thread-103 pid=3979 runtime=5014167 [ns] vruntime=244334517704 [ns]
           <...>-3979  [007]   217.975298: sched_task_load_contrib: comm=Thread-103 pid=3979 load_contrib=2500
           <...>-3979  [007]   217.975314: sched_task_runnable_ratio: comm=Thread-103 pid=3979 ratio=1023
           <...>-3979  [007]   217.975332: sched_rq_runnable_ratio: cpu=7 ratio=1023
           <...>-3979  [007]   217.975345: sched_rq_runnable_load: cpu=7 load=127
           <...>-3979  [007]   217.975366: softirq_raise:        vec=7 [action=SCHED]
           <...>-3979  [007]   217.975446: irq_handler_exit:     irq=163 ret=handled
           <...>-3979  [007]   217.975502: softirq_entry:        vec=1 [action=TIMER]
           <...>-3979  [007]   217.975523: softirq_exit:         vec=1 [action=TIMER]
           <...>-3979  [007]   217.975535: softirq_entry:        vec=7 [action=SCHED]
           <...>-3979  [007]   217.975559: sched_rq_runnable_ratio: cpu=7 ratio=1023
           <...>-3979  [007]   217.975571: sched_rq_runnable_load: cpu=7 load=127
           <...>-3979  [007]   217.975584: softirq_exit:         vec=7 [action=SCHED]
           <...>-3979  [007]   217.980139: irq_handler_entry:    irq=163 name=mct_tick7
           <...>-3979  [007]   217.980216: softirq_raise:        vec=1 [action=TIMER]
           <...>-3979  [007]   217.980253: sched_stat_runtime:   comm=Thread-103 pid=3979 runtime=4990542 [ns] vruntime=244336561007 [ns]
           <...>-3979  [007]   217.980268: sched_task_load_contrib: comm=Thread-103 pid=3979 load_contrib=2500""")

    def tearDown(self):
        os.chdir(self.previous_dir)
        shutil.rmtree(self.out_dir)

    def test_empty_trace_txt(self):
        df = ThermalGovernor().get_data_frame()
        self.assertEquals(len(df), 0)