blob: 4fd6318db2251dcdb431aa18fec23b84edc30337 [file] [log] [blame]
Javi Merinoaace7c02015-08-10 14:10:47 +01001# Copyright 2015-2015 ARM Limited
Javi Merino034e7cc2015-04-22 18:39:21 +01002#
Javi Merinoaace7c02015-08-10 14:10:47 +01003# 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
KP Singh7319a882014-12-24 18:18:01 +000016
17import unittest
18import matplotlib
19import pandas as pd
Kapileshwar Singh5ebf1a32015-02-06 15:50:41 +000020import tempfile
21import os
Javi Merino7b860d52015-12-17 11:14:07 +000022import warnings
KP Singh7319a882014-12-24 18:18:01 +000023
24from test_thermal import BaseTestThermal
Javi Merino435457c2015-08-10 15:59:10 +010025import trappy
KP Singh7319a882014-12-24 18:18:01 +000026
27
28class TestPlotter(BaseTestThermal):
29
30 """No Bombing testcases for plotter"""
31
32 def __init__(self, *args, **kwargs):
33 super(TestPlotter, self).__init__(*args, **kwargs)
34
35 def test_plot_no_pivot(self):
36 """Tests LinePlot with no pivot"""
Javi Merinoc26a3232015-12-11 18:00:30 +000037 trace1 = trappy.FTrace(name="first")
38 l = trappy.LinePlot(trace1, trappy.thermal.Thermal, column="temp")
Kapileshwar Singha2040442015-02-23 12:19:18 +000039 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +000040
Javi Merinoc26a3232015-12-11 18:00:30 +000041 def test_plot_multi_trace(self):
42 """Tests LinePlot with no Pivot multi traces"""
43 trace1 = trappy.FTrace(name="first")
44 trace2 = trappy.FTrace(name="second")
Javi Merino435457c2015-08-10 15:59:10 +010045 l = trappy.LinePlot(
Javi Merinoc26a3232015-12-11 18:00:30 +000046 [trace1, trace2], trappy.thermal.Thermal, column="temp")
Kapileshwar Singha2040442015-02-23 12:19:18 +000047 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +000048
49 def test_plot_multi(self):
50 """Tests LinePlot with no Pivot multi attrs"""
Javi Merinoc26a3232015-12-11 18:00:30 +000051 trace1 = trappy.FTrace(name="first")
52 trace2 = trappy.FTrace(name="second")
53 l = trappy.LinePlot([trace1,
54 trace2],
Javi Merino435457c2015-08-10 15:59:10 +010055 [trappy.thermal.Thermal,
56 trappy.thermal.ThermalGovernor],
Kapileshwar Singh18820752015-02-11 12:01:08 +000057 column=["temp",
58 "power_range"])
Kapileshwar Singha2040442015-02-23 12:19:18 +000059 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +000060
61 def test_plot_filter(self):
62 """Tests LinePlot with no Pivot with filters"""
Javi Merinoc26a3232015-12-11 18:00:30 +000063 trace1 = trappy.FTrace(name="first")
64 trace2 = trappy.FTrace(name="second")
65 l = trappy.LinePlot([trace1,
66 trace2],
Javi Merino435457c2015-08-10 15:59:10 +010067 [trappy.cpu_power.CpuOutPower],
Kapileshwar Singh18820752015-02-11 12:01:08 +000068 column=["power"],
69 filters={"cdev_state": [1]})
Kapileshwar Singha2040442015-02-23 12:19:18 +000070 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +000071
72 def test_plot_pivot(self):
73 """Tests LinePlot with Pivot"""
Javi Merinoc26a3232015-12-11 18:00:30 +000074 trace1 = trappy.FTrace(name="first")
Javi Merino435457c2015-08-10 15:59:10 +010075 l = trappy.LinePlot(
Javi Merinoc26a3232015-12-11 18:00:30 +000076 trace1,
Javi Merino435457c2015-08-10 15:59:10 +010077 trappy.thermal.Thermal,
KP Singh7319a882014-12-24 18:18:01 +000078 column="temp",
79 pivot="thermal_zone")
Kapileshwar Singha2040442015-02-23 12:19:18 +000080 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +000081
Javi Merinoc26a3232015-12-11 18:00:30 +000082 def test_plot_multi_trace_pivot(self):
83 """Tests LinePlot with Pivot multi traces"""
84 trace1 = trappy.FTrace(name="first")
85 trace2 = trappy.FTrace(name="second")
Javi Merino435457c2015-08-10 15:59:10 +010086 l = trappy.LinePlot(
Javi Merinoc26a3232015-12-11 18:00:30 +000087 [trace1, trace2], trappy.cpu_power.CpuOutPower, column="power", pivot="cpus")
Kapileshwar Singha2040442015-02-23 12:19:18 +000088 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +000089
90 def test_plot_multi_pivot(self):
91 """Tests LinePlot with Pivot with multi attrs"""
Javi Merinoc26a3232015-12-11 18:00:30 +000092 trace1 = trappy.FTrace(name="first")
93 trace2 = trappy.FTrace(name="second")
94 l = trappy.LinePlot([trace1,
95 trace2],
Javi Merino435457c2015-08-10 15:59:10 +010096 [trappy.cpu_power.CpuInPower,
97 trappy.cpu_power.CpuOutPower],
Kapileshwar Singh18820752015-02-11 12:01:08 +000098 column=["dynamic_power",
99 "power"],
100 pivot="cpus")
Kapileshwar Singha2040442015-02-23 12:19:18 +0000101 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +0000102
103 def test_plot_multi_pivot_filter(self):
104 """Tests LinePlot with Pivot and filters"""
Javi Merinoc26a3232015-12-11 18:00:30 +0000105 trace1 = trappy.FTrace(name="first")
106 trace2 = trappy.FTrace(name="second")
Javi Merino435457c2015-08-10 15:59:10 +0100107 l = trappy.LinePlot(
Javi Merinoc26a3232015-12-11 18:00:30 +0000108 trace1,
Javi Merino435457c2015-08-10 15:59:10 +0100109 trappy.cpu_power.CpuInPower,
KP Singh7319a882014-12-24 18:18:01 +0000110 column=[
Kapileshwar Singh18820752015-02-11 12:01:08 +0000111 "dynamic_power",
KP Singh7319a882014-12-24 18:18:01 +0000112 "load1"],
113 filters={
114 "cdev_state": [
115 1,
116 0]},
117 pivot="cpus")
Kapileshwar Singha2040442015-02-23 12:19:18 +0000118 l.view(test=True)
Kapileshwar Singh5ebf1a32015-02-06 15:50:41 +0000119
120 def test_plot_savefig(self):
121 """Tests plotter: savefig"""
Javi Merinoc26a3232015-12-11 18:00:30 +0000122 trace1 = trappy.FTrace(name="first")
123 trace2 = trappy.FTrace(name="second")
Javi Merino435457c2015-08-10 15:59:10 +0100124 l = trappy.LinePlot(
Javi Merinoc26a3232015-12-11 18:00:30 +0000125 trace1,
Javi Merino435457c2015-08-10 15:59:10 +0100126 trappy.cpu_power.CpuInPower,
Kapileshwar Singh5ebf1a32015-02-06 15:50:41 +0000127 column=[
128 "dynamic_power",
129 "load1"],
130 filters={
131 "cdev_state": [
132 1,
133 0]},
134 pivot="cpus")
135 png_file = tempfile.mktemp(dir="/tmp", suffix=".png")
136 l.savefig(png_file)
137 self.assertTrue(os.path.isfile(png_file))
138 os.remove(png_file)
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000139
140
141 def test_signals(self):
142 """Test signals input for LinePlot"""
143
Javi Merinoc26a3232015-12-11 18:00:30 +0000144 trace1 = trappy.FTrace(name="first")
145 trace2 = trappy.FTrace(name="second")
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000146
Javi Merinoc26a3232015-12-11 18:00:30 +0000147 l = trappy.LinePlot([trace1,
148 trace2],
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000149 signals=["cpu_in_power:dynamic_power",
150 "cpu_out_power:power"],
151 pivot="cpus")
152
153 l.view(test=True)
154
155
156 def test_signals_exceptions(self):
157 """Test incorrect input combinations: signals"""
158
Javi Merinoc26a3232015-12-11 18:00:30 +0000159 trace1 = trappy.FTrace(name="first")
160 trace2 = trappy.FTrace(name="second")
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000161
162 with self.assertRaises(ValueError):
Javi Merinoc26a3232015-12-11 18:00:30 +0000163 l = trappy.LinePlot([trace1, trace2],
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000164 column=[
165 "dynamic_power",
166 "load1"],
167 signals=["cpu_in_power:dynamic_power",
168 "cpu_out_power:power"],
169 pivot="cpus")
170
171 with self.assertRaises(ValueError):
Javi Merinoc26a3232015-12-11 18:00:30 +0000172 l = trappy.LinePlot([trace1, trace2],
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000173 trappy.cpu_power.CpuInPower,
174 signals=["cpu_in_power:dynamic_power",
175 "cpu_out_power:power"],
176 pivot="cpus")
177
178 with self.assertRaises(ValueError):
Javi Merinoc26a3232015-12-11 18:00:30 +0000179 l = trappy.LinePlot([trace1, trace2],
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000180 trappy.cpu_power.CpuInPower,
181 column=[
182 "dynamic_power",
183 "load1"],
184 signals=["cpu_in_power:dynamic_power",
185 "cpu_out_power:power"],
186 pivot="cpus")
Javi Merino7b860d52015-12-17 11:14:07 +0000187
188 def test_get_trace_event_data_corrupted_trace(self):
189 """get_trace_event_data() works with a corrupted trace"""
190 from trappy.plotter.Utils import get_trace_event_data
191
Javi Merinoc26a3232015-12-11 18:00:30 +0000192 trace = trappy.FTrace()
Javi Merino7b860d52015-12-17 11:14:07 +0000193
194 # We create this trace:
195 #
196 # 1 15414 -> 15411
197 # 2 15411 -> 15414
198 # 3 15414 -> 15411 (corrupted, should be dropped)
199 # 4 15413 -> 15411
200 # 5 15411 -> 15413
201 #
202 # Which should plot like:
203 #
204 # CPU
205 # +-------+-------+
206 # 0 | 15411 | 15414 |
207 # +-------+-------+ +-------+
208 # 1 | 15411 |
209 # +-------+
210 # +-------+-------+-------+-------+
211 # 0.1 0.2 0.3 0.4 0.5
212
213 broken_trace = pd.DataFrame({
214 '__comm': ["task2", "task1", "task2", "task3", "task1"],
215 '__cpu': [0, 0, 0, 1, 1],
216 '__pid': [15414, 15411, 15414, 15413, 15411],
217 'next_comm': ["task1", "task2", "task1", "task1", "task3"],
218 'next_pid': [15411, 15414, 15411, 15411, 15413],
219 'prev_comm': ["task2", "task1", "task2", "task3", "task1"],
220 'prev_pid': [15414, 15411, 15414, 15413, 15411],
221 'prev_state': ["S", "R", "S", "S", "S"]},
222 index=pd.Series(range(1, 6), name="Time"))
223
Javi Merinoc26a3232015-12-11 18:00:30 +0000224 trace.sched_switch.data_frame = broken_trace
Javi Merino7b860d52015-12-17 11:14:07 +0000225
226 with warnings.catch_warnings(record=True) as warn:
Javi Merinoc26a3232015-12-11 18:00:30 +0000227 data, procs, window = get_trace_event_data(trace)
Javi Merino7b860d52015-12-17 11:14:07 +0000228 self.assertEquals(len(warn), 1)
229
230 warn_str = str(warn[-1])
231 self.assertTrue("15411" in warn_str)
232 self.assertTrue("4" in warn_str)
233
234 zipped_comms = zip(broken_trace["next_comm"], broken_trace["next_pid"])
235 expected_procs = set("-".join([comm, str(pid)]) for comm, pid in zipped_comms)
236
237 self.assertTrue([1, 2, 0] in data["task1-15411"])
238 self.assertTrue([2, 3, 0] in data["task2-15414"])
239 self.assertTrue([4, 5, 1] in data["task1-15411"])
240 self.assertEquals(procs, expected_procs)
241 self.assertEquals(window, [1, 5])