1 # SPDX-License-Identifier: Apache-2.0 2 # 3 # Copyright (C) 2015, ARM Limited and contributors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); you may 6 # not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 18 import re 19 import os 20 import logging 21 from time import sleep 22 23 from subprocess import Popen, PIPE 24 25 from android import Screen, Workload, System 26 27 # Regexps for benchmark synchronization 28 GEEKBENCH_BENCHMARK_START_RE = re.compile( 29 r'ActivityManager: Start.* com.primatelabs.geekbench' 30 ) 31 GEEKBENCH_BENCHMARK_END_RE = re.compile( 32 r'GEEKBENCH_RESULT: (?P<results_file>.+)' 33 ) 34 35 class Geekbench(Workload): 36 """ 37 Android Geekbench workload 38 """ 39 40 # Package required by this workload 41 package = 'com.primatelabs.geekbench' 42 activity = '.HomeActivity' 43 44 def __init__(self, test_env): 45 super(Geekbench, self).__init__(test_env) 46 self._log = logging.getLogger('Geekbench') 47 self._log.debug('Workload created') 48 49 def run(self, out_dir, test_name, collect=''): 50 """ 51 Run single Geekbench workload. 52 53 :param out_dir: Path on host to experiment directory where 54 to store results. 55 :type out_dir: str 56 57 :param test_name: Name of the test to run 58 :type test_name: str 59 60 :param collect: Specifies what to collect. Possible values: 61 - 'energy' 62 - 'systrace' 63 - 'ftrace' 64 - any combination of the above in a space-separated string. 65 :type collect: list(str) 66 """ 67 68 # Initialize energy meter results 69 self.out_dir = out_dir 70 self.collect = collect 71 72 # Clear the stored data for the application, so we always start with 73 # an EULA to clear 74 System.force_stop(self._target, self.package, clear=True) 75 76 # Clear logcat from any previous runs 77 # do this on the target as then we don't need to build a string 78 self._target.clear_logcat() 79 80 # Unlock device screen (assume no password required) 81 System.menu(self._target) 82 # Press Back button to be sure we run the benchmark from the start 83 System.back(self._target) 84 85 # Force screen in PORTRAIT mode 86 Screen.set_orientation(self._target, portrait=True) 87 88 # Set min brightness 89 Screen.set_brightness(self._target, auto=False, percent=0) 90 91 # Start app on the target device 92 System.start_activity(self._target, self.package, self.activity) 93 # Allow the activity to start 94 sleep(2) 95 96 # Parse logcat output lines to find beginning and end 97 logcat_cmd = self._adb( 98 'logcat ActivityManager:* System.out:I *:S GEEKBENCH_RESULT:*'\ 99 .format(self._target.adb_name)) 100 self._log.info("%s", logcat_cmd) 101 logcat = Popen(logcat_cmd, shell=True, stdout=PIPE) 102 103 # Click to accept the EULA 104 System.tap(self._target, 73, 55) 105 sleep(1) 106 107 # The main window opened will have the CPU benchmark 108 # Swipe to get the COMPUTE one 109 if test_name.upper() == 'COMPUTE': 110 System.hswipe(self._target, 10, 80, duration=100, swipe_right=False) 111 112 # Press the 'RUN <test_name> BENCHMARK' button 113 System.tap(self._target, 73, 72) 114 115 while True: 116 117 # read next logcat line (up to max 1024 chars) 118 message = logcat.stdout.readline(1024) 119 120 # Benchmark start trigger 121 match = GEEKBENCH_BENCHMARK_START_RE.search(message) 122 if match: 123 # Start tracing 124 self.tracingStart() 125 self._log.debug("Benchmark started!") 126 127 # Benchmark end trigger 128 match = GEEKBENCH_BENCHMARK_END_RE.search(message) 129 if match: 130 # Stop tracing 131 self.tracingStop() 132 remote_result_file = match.group('results_file') 133 self._log.debug("Benchmark finished! Results are in {}".format(remote_result_file)) 134 break 135 136 # Get Geekbench Results file 137 target_result_file = self._target.path.basename(remote_result_file) 138 result_file = os.path.join(self.out_dir, target_result_file) 139 self._log.debug("result_file={}".format(result_file)) 140 self._target.pull(remote_result_file, result_file) 141 142 # Close the app 143 System.force_stop(self._target, self.package, clear=False) 144 145 # Go back to home screen 146 System.home(self._target) 147 148 # Switch back to screen auto rotation 149 Screen.set_orientation(self._target, auto=True) 150 151 # Set brightness back to auto 152 Screen.set_brightness(self._target, auto=True) 153 154 # vim :set tabstop=4 shiftwidth=4 expandtab 155