1#!/usr/bin/env python3 2# 3# Copyright (C) 2021 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may 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, 13# WITHOUT 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 17import glob 18import os 19from pathlib import Path 20import re 21import shutil 22import subprocess 23import time 24from typing import List, Tuple 25 26from simpleperf_utils import remove 27from . test_utils import TestBase, TestHelper, AdbHelper, INFERNO_SCRIPT 28 29 30class TestExampleBase(TestBase): 31 @classmethod 32 def prepare(cls, example_name, package_name, activity_name, abi=None, adb_root=False, 33 apk_name: str = 'app-debug.apk'): 34 cls.adb = AdbHelper(enable_switch_to_root=adb_root) 35 cls.example_path = TestHelper.testdata_path(example_name) 36 if not os.path.isdir(cls.example_path): 37 log_fatal("can't find " + cls.example_path) 38 apk_files = list(Path(cls.example_path).glob(f'**/{apk_name}')) 39 if not apk_files: 40 log_fatal(f"can't find {apk_name} under " + cls.example_path) 41 cls.apk_path = apk_files[0] 42 cls.package_name = package_name 43 cls.activity_name = activity_name 44 args = ["install", "-r"] 45 if abi: 46 args += ["--abi", abi] 47 args.append(cls.apk_path) 48 cls.adb.check_run(args) 49 cls.adb_root = adb_root 50 cls.has_perf_data_for_report = False 51 # On Android >= P (version 9), we can profile JITed and interpreted Java code. 52 # So only compile Java code on Android <= O (version 8). 53 cls.use_compiled_java_code = TestHelper.android_version <= 8 54 cls.testcase_dir = TestHelper.get_test_dir(cls.__name__) 55 56 @classmethod 57 def tearDownClass(cls): 58 if hasattr(cls, 'testcase_dir'): 59 remove(cls.testcase_dir) 60 if hasattr(cls, 'package_name'): 61 cls.adb.check_run(["uninstall", cls.package_name]) 62 63 def setUp(self): 64 super(TestExampleBase, self).setUp() 65 if TestHelper.android_version == 8 and ( 66 'ExampleJava' in self.id() or 'ExampleKotlin' in self.id()): 67 self.skipTest('Profiling java code needs wrap.sh on Android O (8.x)') 68 if 'TraceOffCpu' in self.id() and not TestHelper.is_trace_offcpu_supported(): 69 self.skipTest('trace-offcpu is not supported on device') 70 # Use testcase_dir to share a common perf.data for reporting. So we don't need to 71 # generate it for each test. 72 if not os.path.isdir(self.testcase_dir): 73 os.makedirs(self.testcase_dir) 74 os.chdir(self.testcase_dir) 75 self.run_app_profiler(compile_java_code=self.use_compiled_java_code) 76 os.chdir(self.test_dir) 77 78 for name in os.listdir(self.testcase_dir): 79 path = os.path.join(self.testcase_dir, name) 80 if os.path.isfile(path): 81 shutil.copy(path, self.test_dir) 82 elif os.path.isdir(path): 83 shutil.copytree(path, os.path.join(self.test_dir, name)) 84 85 def run(self, result=None): 86 self.__class__.test_result = result 87 super(TestExampleBase, self).run(result) 88 89 def run_app_profiler(self, record_arg="-g --duration 10", build_binary_cache=True, 90 start_activity=True, compile_java_code=False): 91 args = ['app_profiler.py', '--app', self.package_name, '-r', record_arg, '-o', 'perf.data'] 92 if not build_binary_cache: 93 args.append("-nb") 94 if compile_java_code: 95 args.append('--compile_java_code') 96 if start_activity: 97 args += ["-a", self.activity_name] 98 args += ["-lib", self.example_path] 99 if not self.adb_root: 100 args.append("--disable_adb_root") 101 self.run_cmd(args) 102 self.check_exist(filename="perf.data") 103 if build_binary_cache: 104 self.check_exist(dirname="binary_cache") 105 106 def check_file_under_dir(self, dirname, filename): 107 self.check_exist(dirname=dirname) 108 for _, _, files in os.walk(dirname): 109 for f in files: 110 if f == filename: 111 return 112 self.fail("Failed to call check_file_under_dir(dir=%s, file=%s)" % (dirname, filename)) 113 114 def check_annotation_summary( 115 self, summary_file: str, check_entries: List[Tuple[str, float, float]]): 116 """ check_entries is a list of (name, accumulated_period, period). 117 This function checks for each entry, if the line containing [name] 118 has at least required accumulated_period and period. 119 """ 120 self.check_exist(filename=summary_file) 121 with open(summary_file, 'r') as fh: 122 summary = fh.read() 123 fulfilled = [False for x in check_entries] 124 summary_check_re = re.compile(r'^\|\s*([\d.]+)%\s*\|\s*([\d.]+)%\s*\|') 125 for line in summary.split('\n'): 126 for i, (name, need_acc_period, need_period) in enumerate(check_entries): 127 if not fulfilled[i] and name in line: 128 m = summary_check_re.search(line) 129 if m: 130 acc_period = float(m.group(1)) 131 period = float(m.group(2)) 132 if acc_period >= need_acc_period and period >= need_period: 133 fulfilled[i] = True 134 135 self.check_fulfilled_entries(fulfilled, check_entries) 136 137 def check_inferno_report_html(self, check_entries, filename="report.html"): 138 self.check_exist(filename=filename) 139 with open(filename, 'r') as fh: 140 data = fh.read() 141 fulfilled = [False for _ in check_entries] 142 for line in data.split('\n'): 143 # each entry is a (function_name, min_percentage) pair. 144 for i, entry in enumerate(check_entries): 145 if fulfilled[i] or line.find(entry[0]) == -1: 146 continue 147 m = re.search(r'(\d+\.\d+)%', line) 148 if m and float(m.group(1)) >= entry[1]: 149 fulfilled[i] = True 150 break 151 self.check_fulfilled_entries(fulfilled, check_entries) 152 153 def common_test_app_profiler(self): 154 self.run_cmd(["app_profiler.py", "-h"]) 155 remove("binary_cache") 156 self.run_app_profiler(build_binary_cache=False) 157 self.assertFalse(os.path.isdir("binary_cache")) 158 args = ["binary_cache_builder.py"] 159 if not self.adb_root: 160 args.append("--disable_adb_root") 161 self.run_cmd(args) 162 self.check_exist(dirname="binary_cache") 163 remove("binary_cache") 164 self.run_app_profiler(build_binary_cache=True) 165 self.run_app_profiler() 166 self.run_app_profiler(start_activity=False) 167 168 def common_test_report(self): 169 self.run_cmd(["report.py", "-h"]) 170 self.run_cmd(["report.py"]) 171 self.run_cmd(["report.py", "-i", "perf.data"]) 172 self.run_cmd(["report.py", "-g"]) 173 self.run_cmd(["report.py", "--self-kill-for-testing", "-g", "--gui"]) 174 175 def common_test_annotate(self): 176 self.run_cmd(["annotate.py", "-h"]) 177 remove("annotated_files") 178 self.run_cmd(["annotate.py", "-s", self.example_path, '--summary-width', '1000']) 179 self.check_exist(dirname="annotated_files") 180 181 def common_test_report_sample(self, check_strings): 182 self.run_cmd(["report_sample.py", "-h"]) 183 self.run_cmd(["report_sample.py"]) 184 output = self.run_cmd(["report_sample.py", "-i", "perf.data"], return_output=True) 185 self.check_strings_in_content(output, check_strings) 186 187 def common_test_pprof_proto_generator(self, check_strings_with_lines, 188 check_strings_without_lines): 189 self.run_cmd(["pprof_proto_generator.py", "-h"]) 190 self.run_cmd(["pprof_proto_generator.py"]) 191 remove("pprof.profile") 192 self.run_cmd(["pprof_proto_generator.py", "-i", "perf.data", "-o", "pprof.profile"]) 193 self.check_exist(filename="pprof.profile") 194 self.run_cmd(["pprof_proto_generator.py", "--show"]) 195 output = self.run_cmd(["pprof_proto_generator.py", "--show", "pprof.profile"], 196 return_output=True) 197 self.check_strings_in_content(output, check_strings_with_lines + ["has_line_numbers: True"]) 198 remove("binary_cache") 199 self.run_cmd(["pprof_proto_generator.py"]) 200 output = self.run_cmd(["pprof_proto_generator.py", "--show", "pprof.profile"], 201 return_output=True) 202 self.check_strings_in_content(output, check_strings_without_lines + 203 ["has_line_numbers: False"]) 204 205 def common_test_inferno(self): 206 self.run_cmd([INFERNO_SCRIPT, "-h"]) 207 remove("perf.data") 208 append_args = [] if self.adb_root else ["--disable_adb_root"] 209 self.run_cmd([INFERNO_SCRIPT, "-p", self.package_name, "-t", "3"] + append_args) 210 self.check_exist(filename="perf.data") 211 self.run_cmd([INFERNO_SCRIPT, "-p", self.package_name, "-f", "1000", "-du", "-t", "1"] + 212 append_args) 213 self.run_cmd([INFERNO_SCRIPT, "-p", self.package_name, "-e", "100000 cpu-cycles", 214 "-t", "1"] + append_args) 215 self.run_cmd([INFERNO_SCRIPT, "-sc"]) 216 217 def common_test_report_html(self): 218 self.run_cmd(['report_html.py', '-h']) 219 self.run_cmd(['report_html.py']) 220 self.run_cmd(['report_html.py', '--add_source_code', '--source_dirs', 'testdata']) 221 self.run_cmd(['report_html.py', '--add_disassembly']) 222 # Test with multiple perf.data. 223 shutil.move('perf.data', 'perf2.data') 224 self.run_app_profiler(record_arg='-g -f 1000 --duration 3 -e task-clock:u') 225 self.run_cmd(['report_html.py', '-i', 'perf.data', 'perf2.data']) 226 227 228class TestRecordingRealApps(TestBase): 229 def setUp(self): 230 super(TestRecordingRealApps, self).setUp() 231 self.adb = TestHelper.adb 232 self.installed_packages = [] 233 234 def tearDown(self): 235 for package in self.installed_packages: 236 self.adb.run(['shell', 'pm', 'uninstall', package]) 237 super(TestRecordingRealApps, self).tearDown() 238 239 def install_apk(self, apk_path, package_name): 240 self.adb.run(['uninstall', package_name]) 241 self.adb.run(['install', '-t', apk_path]) 242 self.installed_packages.append(package_name) 243 244 def start_app(self, start_cmd): 245 subprocess.Popen(self.adb.adb_path + ' ' + start_cmd, shell=True, 246 stdout=TestHelper.log_fh, stderr=TestHelper.log_fh) 247 248 def record_data(self, package_name, record_arg): 249 self.run_cmd(['app_profiler.py', '--app', package_name, '-r', record_arg]) 250 251 def check_symbol_in_record_file(self, symbol_name): 252 self.run_cmd(['report.py', '--children', '-o', 'report.txt']) 253 self.check_strings_in_file('report.txt', [symbol_name]) 254 255 def test_recording_displaybitmaps(self): 256 self.install_apk(TestHelper.testdata_path('DisplayBitmaps.apk'), 257 'com.example.android.displayingbitmaps') 258 self.install_apk(TestHelper.testdata_path('DisplayBitmapsTest.apk'), 259 'com.example.android.displayingbitmaps.test') 260 self.start_app('shell am instrument -w -r -e debug false -e class ' + 261 'com.example.android.displayingbitmaps.tests.GridViewTest ' + 262 'com.example.android.displayingbitmaps.test/' + 263 'androidx.test.runner.AndroidJUnitRunner') 264 self.record_data('com.example.android.displayingbitmaps', '-e cpu-clock -g --duration 10') 265 if TestHelper.android_version >= 9: 266 self.check_symbol_in_record_file('androidx.test.espresso') 267 268 def test_recording_endless_tunnel(self): 269 self.install_apk(TestHelper.testdata_path( 270 'EndlessTunnel.apk'), 'com.google.sample.tunnel') 271 self.start_app('shell am start -n com.google.sample.tunnel/android.app.NativeActivity -a ' + 272 'android.intent.action.MAIN -c android.intent.category.LAUNCHER') 273 self.record_data('com.google.sample.tunnel', '-e cpu-clock -g --duration 10') 274 self.check_symbol_in_record_file('PlayScene::DoFrame') 275