1#    Copyright 2015-2017 ARM Limited
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
16
17import unittest
18import matplotlib
19import os
20import pandas as pd
21import shutil
22import sys
23import tempfile
24
25import utils_tests
26import trappy
27
28sys.path.append(os.path.join(utils_tests.TESTS_DIRECTORY, "..", "trappy"))
29
30class BaseTestThermal(utils_tests.SetupDirectory):
31    def __init__(self, *args, **kwargs):
32        super(BaseTestThermal, self).__init__(
33             [("../doc/trace.txt", "trace.txt")],
34             *args,
35             **kwargs)
36
37class TestThermal(BaseTestThermal):
38
39    def test_plot_temperature(self):
40        """Test ThermalGovernor.plot_temperature()
41
42        Can't check that the graph is ok, so just see that the method
43        doesn't blow up
44
45        """
46
47        th_data = trappy.FTrace().thermal
48        dfr = th_data.data_frame
49        ct_series = pd.Series([57, 57], index=(dfr.index[0], dfr.index[-1]))
50
51        th_data.plot_temperature()
52        matplotlib.pyplot.close('all')
53
54        th_data.plot_temperature(title="Antutu", control_temperature=ct_series)
55        matplotlib.pyplot.close('all')
56
57        th_data.plot_temperature(title="Antutu", ylim=[0, 60])
58        matplotlib.pyplot.close('all')
59
60        _, ax = matplotlib.pyplot.subplots()
61        th_data.plot_temperature(ax=ax)
62        matplotlib.pyplot.close('all')
63
64    def test_plot_temperature_hist(self):
65        """Test that plot_temperature_hist() doesn't bomb"""
66
67        _, ax = matplotlib.pyplot.subplots()
68        trappy.FTrace().thermal.plot_temperature_hist(ax, "Foo")
69        matplotlib.pyplot.close('all')
70
71class TestThermalGovernor(BaseTestThermal):
72    def __init__(self, *args, **kwargs):
73        super(TestThermalGovernor, self).__init__(*args, **kwargs)
74        self.actor_order = ["GPU", "A15", "A7"]
75
76    def test_get_dataframe(self):
77        dfr = trappy.FTrace().thermal_governor.data_frame
78
79        self.assertTrue(len(dfr) > 0)
80        self.assertEquals(dfr["current_temperature"].iloc[0], 68775)
81        self.assertTrue("total_granted_power" in dfr.columns)
82        self.assertFalse("time" in dfr.columns)
83
84    def test_plot_temperature(self):
85        """Test ThermalGovernor.plot_temperature()
86
87        Can't check that the graph is ok, so just see that the method doesn't blow up"""
88        gov = trappy.FTrace().thermal_governor
89
90        gov.plot_temperature()
91        gov.plot_temperature(legend_label="power allocator", ylim=(0, 72))
92        matplotlib.pyplot.close('all')
93
94    def test_plot_input_power(self):
95        """plot_input_power() doesn't bomb"""
96        gov = trappy.FTrace().thermal_governor
97
98        gov.plot_input_power(self.actor_order)
99        matplotlib.pyplot.close('all')
100
101        gov.plot_input_power(self.actor_order, title="Antutu")
102        matplotlib.pyplot.close('all')
103
104        _, ax = matplotlib.pyplot.subplots()
105        gov.plot_input_power(self.actor_order, ax=ax)
106        matplotlib.pyplot.close('all')
107
108    def test_plot_weighted_input_power(self):
109        """plot_weighted_input_power() doesn't bomb"""
110
111        gov = trappy.FTrace().thermal_governor
112        weights = zip(self.actor_order, [1024, 256, 512])
113
114        _, ax = matplotlib.pyplot.subplots()
115        gov.plot_weighted_input_power(weights, ax=ax)
116        matplotlib.pyplot.close('all')
117
118    def test_plot_output_power(self):
119        """Test plot_output_power()
120
121        Can't check that the graph is ok, so just see that the method doesn't blow up"""
122        gov = trappy.FTrace().thermal_governor
123
124        gov.plot_output_power(self.actor_order)
125        matplotlib.pyplot.close('all')
126
127        gov.plot_output_power(self.actor_order, title="Antutu")
128        matplotlib.pyplot.close('all')
129
130        _, ax = matplotlib.pyplot.subplots()
131        gov.plot_output_power(self.actor_order, title="Antutu", ax=ax)
132        matplotlib.pyplot.close('all')
133
134    def test_plot_inout_power(self):
135        """Test plot_inout_power()
136
137        Can't check that the graph is ok, so just see that the method doesn't blow up"""
138        trappy.FTrace().thermal_governor.plot_inout_power()
139        trappy.FTrace().thermal_governor.plot_inout_power(title="Antutu")
140        matplotlib.pyplot.close('all')
141
142class TestEmptyThermalGovernor(unittest.TestCase):
143    def setUp(self):
144        self.previous_dir = os.getcwd()
145        self.out_dir = tempfile.mkdtemp()
146        os.chdir(self.out_dir)
147        with open("trace.txt", "w") as fout:
148            fout.write("""version = 6
149cpus=8
150CPU:7 [204600 EVENTS DROPPED]
151           <...>-3979  [007]   217.975284: sched_stat_runtime:   comm=Thread-103 pid=3979 runtime=5014167 [ns] vruntime=244334517704 [ns]
152           <...>-3979  [007]   217.975298: sched_task_load_contrib: comm=Thread-103 pid=3979 load_contrib=2500
153           <...>-3979  [007]   217.975314: sched_task_runnable_ratio: comm=Thread-103 pid=3979 ratio=1023
154           <...>-3979  [007]   217.975332: sched_rq_runnable_ratio: cpu=7 ratio=1023
155           <...>-3979  [007]   217.975345: sched_rq_runnable_load: cpu=7 load=127
156           <...>-3979  [007]   217.975366: softirq_raise:        vec=7 [action=SCHED]
157           <...>-3979  [007]   217.975446: irq_handler_exit:     irq=163 ret=handled
158           <...>-3979  [007]   217.975502: softirq_entry:        vec=1 [action=TIMER]
159           <...>-3979  [007]   217.975523: softirq_exit:         vec=1 [action=TIMER]
160           <...>-3979  [007]   217.975535: softirq_entry:        vec=7 [action=SCHED]
161           <...>-3979  [007]   217.975559: sched_rq_runnable_ratio: cpu=7 ratio=1023
162           <...>-3979  [007]   217.975571: sched_rq_runnable_load: cpu=7 load=127
163           <...>-3979  [007]   217.975584: softirq_exit:         vec=7 [action=SCHED]
164           <...>-3979  [007]   217.980139: irq_handler_entry:    irq=163 name=mct_tick7
165           <...>-3979  [007]   217.980216: softirq_raise:        vec=1 [action=TIMER]
166           <...>-3979  [007]   217.980253: sched_stat_runtime:   comm=Thread-103 pid=3979 runtime=4990542 [ns] vruntime=244336561007 [ns]
167           <...>-3979  [007]   217.980268: sched_task_load_contrib: comm=Thread-103 pid=3979 load_contrib=2500""")
168
169    def tearDown(self):
170        os.chdir(self.previous_dir)
171        shutil.rmtree(self.out_dir)
172
173    def test_empty_trace_txt(self):
174        dfr = trappy.FTrace(normalize_time=False).thermal_governor.data_frame
175        self.assertEquals(len(dfr), 0)
176
177    def test_empty_plot_temperature(self):
178        """trace.thermal.plot_temperature() raises ValueError() on an empty
179        thermal trace"""
180        trace = trappy.FTrace()
181        self.assertRaises(ValueError, trace.thermal.plot_temperature)
182