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 matplotlib 18import pandas as pd 19 20from test_thermal import BaseTestThermal 21import trappy 22import cpu_power 23 24class TestCpuPower(BaseTestThermal): 25 def __init__(self, *args, **kwargs): 26 super(TestCpuPower, self).__init__(*args, **kwargs) 27 self.map_label = {"00000000,00000039": "A53", "00000000,00000006": "A57"} 28 29 def test_pivot_with_labels(self): 30 """Test pivot_with_labels()""" 31 map_label = {"000000f0": "A15", "0000000f": "A7"} 32 dfr_in = pd.DataFrame({'cpus': ["000000f0", "0000000f", "000000f0", "0000000f"], 33 'freq': [1, 3, 2, 6]}) 34 35 dfr_out = cpu_power.pivot_with_labels(dfr_in, "freq", "cpus", map_label) 36 37 self.assertEquals(dfr_out["A15"].iloc[0], 1) 38 self.assertEquals(dfr_out["A15"].iloc[1], 1) 39 self.assertEquals(dfr_out["A15"].iloc[2], 2) 40 self.assertEquals(dfr_out["A7"].iloc[1], 3) 41 42 def test_num_cpus_in_mask(self): 43 """num_cpus_in_mask() works with the masks we usually use""" 44 mask = "000000f0" 45 self.assertEquals(cpu_power.num_cpus_in_mask(mask), 4) 46 47 mask = sorted(self.map_label)[0] 48 self.assertEquals(cpu_power.num_cpus_in_mask(mask), 2) 49 50 mask = sorted(self.map_label)[1] 51 self.assertEquals(cpu_power.num_cpus_in_mask(mask), 4) 52 53 def test_cpuoutpower_dataframe(self): 54 """Test that CpuOutPower() creates a proper data_frame""" 55 outp = trappy.FTrace().cpu_out_power 56 57 self.assertEquals(outp.data_frame["power"].iloc[0], 1344) 58 self.assertTrue("cdev_state" in outp.data_frame.columns) 59 60 def test_cpuoutpower_get_all_freqs(self): 61 """Test CpuOutPower.get_all_freqs()""" 62 dfr = trappy.FTrace().cpu_out_power.get_all_freqs(self.map_label) 63 64 self.assertEquals(dfr["A57"].iloc[0], 1100) 65 self.assertEquals(dfr["A53"].iloc[1], 850) 66 67 def test_cpuinpower_get_dataframe(self): 68 """Test that CpuInPower() creates a proper data_frame()""" 69 inp = trappy.FTrace().cpu_in_power 70 71 self.assertTrue("load0" in inp.data_frame.columns) 72 self.assertEquals(inp.data_frame["load0"].iloc[0], 24) 73 74 def test_cpuinpower_big_cpumask(self): 75 """CpuInPower()'s data_frame is not confused by 64-bit cpumasks""" 76 in_data = """ kworker/2:2-679 [002] 676.256284: thermal_power_cpu_get: cpus=00000000,0000000f freq=261888 cdev_state=5 power=12 77 kworker/2:2-679 [002] 676.276200: thermal_power_cpu_get: cpus=00000000,00000030 freq=261888 cdev_state=5 power=0 78 kworker/2:2-679 [002] 676.416202: thermal_power_cpu_get: cpus=00000000,0000000f freq=261888 cdev_state=5 power=0 79 """ 80 with open("trace.txt", "w") as fout: 81 fout.write(in_data) 82 83 inp = trappy.FTrace(normalize_time=False).cpu_in_power 84 self.assertEquals(round(inp.data_frame.index[0], 6), 676.256284) 85 self.assertEquals(inp.data_frame["cpus"].iloc[1], "00000000,00000030") 86 87 def test_cpuinpower_data_frame_asymmetric_clusters(self): 88 """Test that CpuInPower()'s data_frame can handle asymmetric clusters 89 90 That is 2 cpus in one cluster and 4 in another, like Juno 91 """ 92 in_data = """ 93 kworker/2:2-679 [002] 676.256261: thermal_power_cpu_get: cpus=00000000,00000030 freq=1900000 raw_cpu_power=1259 load={74 49} power=451 94 kworker/2:2-679 [002] 676.256271: thermal_power_cpu_get: cpus=00000000,0000000f freq=450000 raw_cpu_power=36 load={1 2 1 3} power=9 95""" 96 97 with open("trace.txt", "w") as fout: 98 fout.write(in_data) 99 100 inp = trappy.FTrace(normalize_time=False).cpu_in_power 101 102 self.assertEquals(inp.data_frame["load0"].iloc[0], 74) 103 self.assertEquals(inp.data_frame["load1"].iloc[0], 49) 104 self.assertEquals(inp.data_frame["load2"].iloc[0], 0) 105 self.assertEquals(inp.data_frame["load3"].iloc[0], 0) 106 self.assertEquals(inp.data_frame["load0"].iloc[1], 1) 107 self.assertEquals(inp.data_frame["load1"].iloc[1], 2) 108 self.assertEquals(inp.data_frame["load2"].iloc[1], 1) 109 self.assertEquals(inp.data_frame["load3"].iloc[1], 3) 110 111 def test_cpuinpower_get_all_freqs(self): 112 """Test CpuInPower.get_all_freqs()""" 113 dfr = trappy.FTrace().cpu_in_power.get_all_freqs(self.map_label) 114 115 self.assertEquals(dfr["A57"].iloc[0], 1100) 116 self.assertEquals(dfr["A53"].iloc[1], 850) 117 self.assertEquals(dfr["A57"].iloc[5], 1100) 118 119 def test_cpuinpower_get_load_data(self): 120 """Test CpuInPower.get_load_data()""" 121 trace = trappy.FTrace() 122 first_load = trace.cpu_in_power.data_frame["load0"].iloc[0] 123 load_data = trace.cpu_in_power.get_load_data(self.map_label) 124 125 self.assertEquals(load_data["A57"].iloc[0], 24 + 19) 126 self.assertEquals(load_data["A53"].iloc[3], 32 + 28 + 46 + 44) 127 self.assertEquals(load_data["A57"].iloc[0], load_data["A57"].iloc[1]) 128 129 self.assertEquals(trace.cpu_in_power.data_frame["load0"].iloc[0], 130 first_load) 131 132 def test_cpuinpower_get_normalized_load_data(self): 133 """Test CpuInPower.get_normalized_load_data()""" 134 trace = trappy.FTrace() 135 first_load = trace.cpu_in_power.data_frame["load0"].iloc[0] 136 load_data = trace.cpu_in_power.get_normalized_load_data(self.map_label) 137 138 # Ideally the trace should have an event in which the cpus are 139 # not running at maximum frequency 140 self.assertEquals(load_data["A57"].iloc[0], 141 (24. + 19) * 1100000 / (1100000 * 2)) 142 self.assertEquals(load_data["A53"].iloc[1], 143 (36. + 49 + 48 + 7) * 850000 / (850000 * 4)) 144 self.assertEquals(trace.cpu_in_power.data_frame["load0"].iloc[0], 145 first_load) 146