aboutsummaryrefslogtreecommitdiff
path: root/bart/thermal/ThermalAssert.py
blob: d0ffa78c12697f1da460fda4fe1e4a9ffdaa7d38 (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
#    Copyright 2015-2016 ARM Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""A thermal specific library to assert certain thermal
behaviours
"""

from bart.common import Utils
from bart.common.Analyzer import Analyzer
import numpy as np


# pylint: disable=invalid-name
# pylint: disable=too-many-arguments
class ThermalAssert(object):

    """A class that accepts a TRAPpy FTrace object and
    provides assertions for thermal behaviours

    :param ftrace: A path to the trace file or a TRAPpy FTrace object
    :type ftrace: str, :mod:`trappy.ftrace.FTrace`
    """

    def __init__(self, ftrace, config=None):

        self._ftrace = Utils.init_ftrace(ftrace)
        self._analyzer = Analyzer(self._ftrace, config)

    def getThermalResidency(self, temp_range, window, percent=False):
        """Return the total time spent in a given temperature range

        :param temp_range: A tuple of (low_temp, high_temp)
            which specifies the range of temperature that
            one intends to calculate the residency for.
        :type temp_range: tuple

        :param window: A (start, end) tuple to limit the scope of the
            residency calculation.
        :type window: tuple

        :param percent: Returns the residency as a percentage of the total
            duration of the trace
        :type percent: bool

        .. seealso:

            :mod:`bart.thermal.ThermalAssert.ThermalAssert.assertThermalResidency`
        """

        # Get a pivoted thermal temperature data using the grammar
        data = self._analyzer.getStatement("trappy.thermal.Thermal:temp")

        result = {}
        for pivot, data_frame in data.groupby(axis=1, level=0):

            series = data_frame[pivot]
            series = Utils.select_window(series, window)
            mask = (series >= temp_range[0]) & (series <= temp_range[1])
            index = series.index.values
            # pylint fails to recognize numpy members.
            # pylint: disable=no-member
            shift_index = np.roll(index, 1)
            # pylint: enable=no-member
            shift_index[0] = 0

            result[pivot] = sum((index - shift_index)[mask.values])

            if percent:
                result[pivot] = (
                    result[pivot] * 100.0) / self._ftrace.get_duration()

        return result

    def assertThermalResidency(
            self,
            expected_value,
            operator,
            temp_range,
            window,
            percent=False):
        """
        :param expected_value: The expected value of the residency
        :type expected_value: double

        :param operator: A binary operator function that returns
            a boolean. For example:
            ::

                import operator
                op = operator.ge
                assertThermalResidency(temp_range, expected_value, op)

            Will do the following check:
            ::

                getThermalResidency(temp_range) >= expected_value

            A custom function can also be passed:
            ::

                THRESHOLD=5
                def between_threshold(a, expected):
                    return abs(a - expected) <= THRESHOLD

        :param temp_range: A tuple of (low_temp, high_temp)
            which specifies the range of temperature that
            one intends to calculate the residency for.
        :type temp_range: tuple

        :param window: A (start, end) tuple to limit the scope of the
            residency calculation.
        :type window: tuple

        :param percent: Returns the residency as a percentage of the total
            duration of the trace
        :type percent: bool

        .. seealso:

            :mod:`bart.thermal.ThermalAssert.ThermalAssert.assertThermalResidency`
        """

        residency = self.getThermalResidency(temp_range, window, percent)
        return operator(residency, expected_value)