aboutsummaryrefslogtreecommitdiff
path: root/libs/utils/android/workloads/jankbench.py
blob: 3c9133f5f47dd7c8fe4c20639e63225743e86d55 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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 subprocess import Popen, PIPE
from time import sleep

from android import Screen, System, Workload

# Available test workloads
_jankbench = {
    'list_view'         : 0,
    'image_list_view'   : 1,
    'shadow_grid'       : 2,
    'low_hitrate_text'  : 3,
    'high_hitrate_text' : 4,
    'edit_text'         : 5,
}

# Regexps for benchmark synchronization
JANKBENCH_BENCHMARK_START_RE = re.compile(
    r'ActivityManager: START.*'
    '(cmp=com.android.benchmark/.app.RunLocalBenchmarksActivity)'
)
JANKBENCH_ITERATION_COUNT_RE = re.compile(
    r'System.out: iteration: (?P<iteration>[0-9]+)'
)
JANKBENCH_ITERATION_METRICS_RE = re.compile(
    r'System.out: Mean: (?P<mean>[0-9\.]+)\s+JankP: (?P<junk_p>[0-9\.]+)\s+'
    'StdDev: (?P<std_dev>[0-9\.]+)\s+Count Bad: (?P<count_bad>[0-9]+)\s+'
    'Count Jank: (?P<count_junk>[0-9]+)'
)
JANKBENCH_BENCHMARK_DONE_RE = re.compile(
    r'I BENCH\s+:\s+BenchmarkDone!'
)

JANKBENCH_DB_PATH = '/data/data/com.android.benchmark/databases/'
JANKBENCH_DB_NAME = 'BenchmarkResults'

# The amounts of time to collect energy for each test. Used when USB communication with the device
# is disabled during energy collection to prevent it from interfering with energy sampling. Tests
# may run longer or shorter than the amount of time energy is collected.
JANKBENCH_ENERGY_DURATIONS = {
    'list_view'         : 30,
    'image_list_view'   : 30,
    'shadow_grid'       : 30,
    'low_hitrate_text'  : 30,
    'high_hitrate_text' : 30,
    'edit_text'         : 9,
}

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

    # Package required by this workload
    package = 'com.android.benchmark'

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

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

    def run(self, out_dir, test_name, iterations, collect):
        """
        Run Jankbench workload for a number of iterations.
        Returns a collection of results.

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

        :param test_name: Name of the test to run
        :type test_name: str

        :param iterations: Number of iterations for the named test
        :type iterations: int

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

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

        # Setup test id
        try:
            test_id = _jankbench[test_name]
        except KeyError:
            raise ValueError('Jankbench test [%s] not supported', test_name)

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

        # Close and clear application
        System.force_stop(self._target, self.package, clear=True)

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

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

        # Force screen in PORTRAIT mode
        Screen.set_orientation(self._target, portrait=True)

        # Clear logcat
        self._target.clear_logcat()

        self._log.debug('Start Jank Benchmark [%d:%s]', test_id, test_name)
        test_cmd = 'am start -n "com.android.benchmark/.app.RunLocalBenchmarksActivity" '\
                    '--eia "com.android.benchmark.EXTRA_ENABLED_BENCHMARK_IDS" {0} '\
                    '--ei "com.android.benchmark.EXTRA_RUN_COUNT" {1}'\
                    .format(test_id, iterations)
        self._log.info(test_cmd)
        self._target.execute(test_cmd);

        if 'energy' in collect:
            # Start collecting energy readings
            self.tracingStart()
            self._log.debug('Benchmark started!')
            self._log.info('Running energy meter for {} seconds'
                .format(iterations * JANKBENCH_ENERGY_DURATIONS[test_name]))

            # Sleep for the approximate amount of time the test should take to run the specified
            # number of iterations.
            for i in xrange(0, iterations):
                sleep(JANKBENCH_ENERGY_DURATIONS[test_name])
                self._log.debug('Iteration: %2d', i + 1)

            # Stop collecting energy. The test may or may not be done at this point.
            self._log.debug('Benchmark done!')
            self.tracingStop()
        else:
            # Parse logcat output lines
            logcat_cmd = self._adb(
                    'logcat ActivityManager:* System.out:I *:S BENCH:*'\
                    .format(self._target.adb_name))
            self._log.info(logcat_cmd)
            logcat = Popen(logcat_cmd, shell=True, stdout=PIPE)
            self._log.debug('Iterations:')
            while True:

                # read next logcat line (up to max 1024 chars)
                message = logcat.stdout.readline(1024)

                # Benchmark start trigger
                match = JANKBENCH_BENCHMARK_START_RE.search(message)
                if match:
                    self.tracingStart()
                    self._log.debug('Benchmark started!')

                # Benchmark completed trigger
                match = JANKBENCH_BENCHMARK_DONE_RE.search(message)
                if match:
                    self._log.debug('Benchmark done!')
                    self.tracingStop()
                    break

                # Iteration completed
                match = JANKBENCH_ITERATION_COUNT_RE.search(message)
                if match:
                    self._log.debug('Iteration: %2d',
                                    int(match.group('iteration'))+1)
                # Iteration metrics
                match = JANKBENCH_ITERATION_METRICS_RE.search(message)
                if match:
                    self._log.info('   Mean: %7.3f JankP: %7.3f StdDev: %7.3f Count Bad: %4d Count Jank: %4d',
                                   float(match.group('mean')),
                                   float(match.group('junk_p')),
                                   float(match.group('std_dev')),
                                   int(match.group('count_bad')),
                                   int(match.group('count_junk')))

        # Wait until the database file is available
        db_adb = JANKBENCH_DB_PATH + JANKBENCH_DB_NAME
        while (db_adb not in self._target.execute('ls {}'.format(db_adb), check_exit_code=False)):
            sleep(1)

        # Get results
        self.db_file = os.path.join(out_dir, JANKBENCH_DB_NAME)
        self._target.pull(db_adb, self.db_file)

        # Stop the benchmark app
        System.force_stop(self._target, self.package, clear=True)

        # Go back to home screen
        System.home(self._target)

        # Reset initial setup
        # Set orientation back to auto
        Screen.set_orientation(self._target, auto=True)

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

        # Set brightness back to auto
        Screen.set_brightness(self._target, auto=True)

# vim :set tabstop=4 shiftwidth=4 expandtab