aboutsummaryrefslogtreecommitdiff
path: root/libs/utils/android/workloads/idle_resume.py
blob: f8871baeec33514f64788b1430f3f51f630c2e94 (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
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (C) 2015, ARM Limited and contributors.
#
# 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.
#

import re
import os
import logging

from time import sleep

from target_script import TargetScript
from android import Screen, System
from android.workload import Workload


class IdleResume(Workload):
    """
    Android IdleResume workload

    This workload tests suspend/resume by turning off the screen and cutting off
    USB. You can collect energy and traces with it.
    IT REQUIRES DEVICE TO BE CONNECTED THROUGH MONSOON SO THAT PASSTHROUGH CAN
    BE TURNED OFF.  The dirty hack we have is we forcefully add 'energy' into the
    collect parameters so that the USB is cut off (as a side effect, we also
    measure energy while idle). There's no way of knowing for sure if device did
    enter idle.
    """

    # Package is optional for this test
    package = 'optional'

    def __init__(self, test_env):
        super(IdleResume, self).__init__(test_env)
        self._log = logging.getLogger('IdleResume')
        self._log.debug('Workload created')

        # Set of output data reported by IdleResume
        self.db_file = None

    def run(self, out_dir, duration_s, collect=''):
        """
        Run single idle/suspend workload.

        :param out_dir: Path to experiment directory where to store results.
        :type out_dir: str

        :param duration_s: Duration of test
        :type duration_s: int

        :type force: str

        :param collect: Specifies what to collect. Possible values:
            - 'energy'
            - 'systrace'
            - 'ftrace'
            - any combination of the above
        :type collect: list(str)
        """

        # Idle/resume should always disconnect USB so rely on
        # the Energy meter to disconnect USB (We only support USB disconnect
        # mode for energy measurement, like with Monsoon
        if 'energy' not in collect:
            collect = collect + ' energy'

        # Keep track of mandatory parameters
        self.out_dir = out_dir
        self.collect = collect

        # Set min brightness
        Screen.set_brightness(self._target, auto=False, percent=0)

        # Set timeout to min value
        Screen.set_timeout(self._target, seconds=0)

        # Prevent screen from dozing
        Screen.set_doze_always_on(self._target, on=False)

        # Turn on airplane mode
        System.set_airplane_mode(self._target, on=True)

        # Unlock device screen (assume no password required)
        Screen.unlock(self._target)

        # Force the device to suspend
        System.force_suspend_start(self._target)

        # Prevent the device from fully suspending by holding a partial wakelock
        System.wakelock(self._target, take=True)

        sleep(1)
        Screen.set_screen(self._target, on=False)
        sleep(1)
        self.tracingStart(screen_always_on=False)

        self._log.info('Waiting for duration {}'.format(duration_s))
        sleep(duration_s)

        self.tracingStop(screen_always_on=False)

        # Resume normal function
        System.force_suspend_stop(self._target)

        # Release wakelock
        System.wakelock(self._target, take=False)

        Screen.set_defaults(self._target)
        System.set_airplane_mode(self._target, on=False)

# vim :set tabstop=4 shiftwidth=4 expandtab