1#    Copyright 2015-2016 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
17from bart.sched.SchedAssert import SchedAssert
18from bart.sched.SchedMultiAssert import SchedMultiAssert
19import trappy
20from trappy.stats.Topology import Topology
21import unittest
22
23import utils_tests
24
25
26@unittest.skipUnless(utils_tests.trace_cmd_installed(),
27                     "trace-cmd not installed")
28class TestSchedAssert(utils_tests.SetupDirectory):
29
30    def __init__(self, *args, **kwargs):
31
32        self.BIG = [1,2]
33        self.LITTLE = [0, 3, 4, 5]
34        self.clusters = [self.BIG, self.LITTLE]
35        self.topology = Topology(clusters=self.clusters)
36        super(TestSchedAssert, self).__init__(
37             [("raw_trace.dat", "trace.dat")],
38             *args,
39             **kwargs)
40
41    def test_get_runtime(self):
42
43        r = trappy.FTrace()
44        # The ls process is process we are
45        # testing against with pre calculated
46        # values
47        process = "ls"
48
49        # Complete duration
50        expected_time = 0.0034740000264719129
51        s = SchedAssert(r, self.topology, execname=process)
52        self.assertAlmostEqual(s.getRuntime(), expected_time, places=9)
53        self.assertAlmostEqual(s.getRuntime(), expected_time, places=9)
54
55        # Non Interrupted Window
56        window = (0.0034, 0.003525)
57        expected_time = 0.000125
58        self.assertAlmostEqual(s.getRuntime(window=window), expected_time,
59                               places=9)
60
61        # Interrupted Window
62        window = (0.0030, 0.0032)
63        expected_time = 0.000166
64        self.assertAlmostEqual(s.getRuntime(window=window), expected_time,
65                               places=9)
66
67        # A window with multiple interruptions
68        window = (0.0027, 0.0036)
69        expected_time = 0.000817
70        self.assertAlmostEqual(s.getRuntime(window=window), expected_time,
71                               places=9)
72
73    def test_get_last_cpu(self):
74        """SchedAssert.getLastCpu() gives you the last cpu in which a task ran"""
75        expected_last_cpu = 5
76
77        sa = SchedAssert("trace.dat", self.topology, execname="ls")
78        self.assertEqual(sa.getLastCpu(), expected_last_cpu)
79
80class TestSchedMultiAssert(utils_tests.SetupDirectory):
81    def __init__(self, *args, **kwargs):
82        self.big = [1,2]
83        self.little = [0, 3, 4, 5]
84        self.clusters = [self.big, self.little]
85        self.all_cpus = sorted(self.big + self.little)
86        self.topology = Topology(clusters=self.clusters)
87        super(TestSchedMultiAssert, self).__init__(
88             [("raw_trace.dat", "trace.dat")],
89             *args,
90             **kwargs)
91
92    def test_cpu_busy_time(self):
93        """SchedMultiAssert.getCPUBusyTime() work"""
94
95        # precalculated values against these processes in the trace
96        pids = [4729, 4734]
97        first_time = .000214
98        last_time = .003171
99
100        tr = trappy.FTrace()
101        sma = SchedMultiAssert(tr, self.topology, pids=pids)
102
103        expected_busy_time = 0.0041839999754810708
104        busy_time = sma.getCPUBusyTime("all", self.all_cpus, window=(first_time, last_time))
105        self.assertAlmostEqual(busy_time, expected_busy_time)
106
107        # percent calculation
108        expected_busy_pct = 23.582459561949445
109        busy_pct= sma.getCPUBusyTime("all", self.all_cpus, percent=True,
110                                     window=(first_time, last_time))
111        self.assertAlmostEqual(busy_pct, expected_busy_pct)
112
113        # percent without a window
114        expected_busy_pct = 23.018818156540004
115        busy_pct= sma.getCPUBusyTime("cluster", self.little, percent=True)
116        self.assertAlmostEqual(busy_pct, expected_busy_pct)
117