summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozpower/mozpower/powerbase.py
blob: 3eb91e3e3b4c35d87ff5b553ab373e080418cde6 (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os

from .mozpowerutils import get_logger


class IPGExecutableMissingError(Exception):
    """IPGExecutableMissingError is raised when we cannot find
    the executable for Intel Power Gadget at the expected location.
    """

    pass


class PlatformUnsupportedError(Exception):
    """PlatformUnsupportedError is raised when we cannot find
    an expected IPG path for the OS being tested.
    """

    pass


class PowerBase(object):
    """PowerBase provides an interface for power measurement objects
    that depend on the os and cpu. When using this class as a base class
    the `initialize_power_measurements`, `finalize_power_measurements`,
    and `get_perfherder_data` functions must be implemented, otherwise
    a NotImplementedError will be raised.

    PowerBase should only be used as the base class for other
    classes and should not be instantiated directly. To enforce this
    restriction calling PowerBase's constructor will raise a
    NonImplementedError exception.

    ::

       from mozpower.powerbase import PowerBase

       try:
           pb = PowerBase()
       except NotImplementedError:
           print "PowerBase cannot be instantiated."

    """

    def __init__(self, logger_name="mozpower", os=None, cpu=None):
        """Initializes the PowerBase object.

        :param str logger_name: logging logger name. Defaults to 'mozpower'.
        :param str os: operating system being tested. Defaults to None.
        :param str cpu: cpu type being tested (either intel or arm64). Defaults to None.
        :raises: * NotImplementedError
        """
        if self.__class__ == PowerBase:
            raise NotImplementedError

        self._logger_name = logger_name
        self._logger = get_logger(logger_name)
        self._os = os
        self._cpu = cpu
        self._ipg_path = self.get_ipg_path()

    @property
    def ipg_path(self):
        return self._ipg_path

    @property
    def logger_name(self):
        return self._logger_name

    def initialize_power_measurements(self):
        """Starts power measurement gathering, must be implemented by subclass.

        :raises: * NotImplementedError
        """
        raise NotImplementedError

    def finalize_power_measurements(self, test_name="power-testing", **kwargs):
        """Stops power measurement gathering, must be implemented by subclass.

        :raises: * NotImplementedError
        """
        raise NotImplementedError

    def get_perfherder_data(self):
        """Returns the perfherder data output produced from the tests, must
        be implemented by subclass.

        :raises: * NotImplementedError
        """
        raise NotImplementedError

    def get_ipg_path(self):
        """Returns the path to where we expect to find Intel Power Gadget
        depending on the OS being tested. Raises a PlatformUnsupportedError
        if the OS being tested is unsupported, and raises a IPGExecutableMissingError
        if the Intel Power Gadget executable cannot be found at the expected
        location.

        :returns: str
        :raises: * IPGExecutableMissingError
                 * PlatformUnsupportedError
        """
        if self._cpu != "intel":
            return None

        if self._os == "darwin":
            exe_path = "/Applications/Intel Power Gadget/PowerLog"
        else:
            raise PlatformUnsupportedError(
                "%s platform currently not supported for Intel Power Gadget measurements"
                % self._os
            )
        if not os.path.exists(exe_path):
            raise IPGExecutableMissingError(
                "Intel Power Gadget is not installed, or cannot be found at the location %s"
                % exe_path
            )

        return exe_path