From 871c47bc1f5a47caabaf92d4ebff76cdfce67462 Mon Sep 17 00:00:00 2001 From: Mikhail Atuchin Date: Fri, 25 Oct 2024 15:11:56 +0400 Subject: [PATCH 1/3] Perf tests: move setup android step from devops --- tools/perf/components/android_tools.py | 31 ++++++++++++++++++++++++- tools/perf/components/common_options.py | 7 ++++++ tools/perf/run_perftests.py | 6 +++++ tools/perf/setup_android_device.sh | 17 ++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100755 tools/perf/setup_android_device.sh diff --git a/tools/perf/components/android_tools.py b/tools/perf/components/android_tools.py index 43469cafdb14..7c0f34192cb7 100644 --- a/tools/perf/components/android_tools.py +++ b/tools/perf/components/android_tools.py @@ -7,12 +7,41 @@ import re import logging -from typing import Optional +import time +from typing import List, Optional from components.perf_test_utils import GetProcessOutput import components.path_util as path_util +def RunFromRoot(cmd: str) -> bool: + result, _ = GetProcessOutput( + [path_util.GetAdbPath(), 'shell', 'su', '-c', '\'' + cmd + '\'']) + return result + + +def SetupAndroidDevice() -> None: + '''Pushes and run setup_android_device.sh script on the device.''' + tmp_file = '/data/local/tmp/setup_android_device.sh' + GetProcessOutput([ + path_util.GetAdbPath(), + 'push', + os.path.join(path_util.GetBravePerfDir(), 'setup_android_device.sh'), + tmp_file, + ], + check=True) + if not RunFromRoot(f'sh {tmp_file}'): + raise RuntimeError('Failed to setup root on the device') + RunFromRoot('killall adbd') # Restart adbd on the device to apply the changes + time.sleep(2) + + +def RebootAndroid() -> None: + logging.debug('Rebooting Android device') + RunFromRoot('reboot') + time.sleep(30) + + def GetPackageVersion(package: str) -> str: _, dump_info = GetProcessOutput( [path_util.GetAdbPath(), 'shell', 'dumpsys', 'package', package], diff --git a/tools/perf/components/common_options.py b/tools/perf/components/common_options.py index 610299c94edd..69b59e715cc2 100644 --- a/tools/perf/components/common_options.py +++ b/tools/perf/components/common_options.py @@ -41,6 +41,7 @@ class CommonOptions: working_directory: str = '' target_os: str = PerfBenchmark.FixupTargetOS(sys.platform) target_arch: str = '' + reboot_android: bool = False do_report: bool = False upload: bool = True @@ -94,6 +95,10 @@ def add_parser_args(cls, parser: argparse.ArgumentParser) -> None: type=str, choices=['windows', 'mac', 'linux', 'android']) parser.add_argument('--target-arch', type=str, choices=['x64', 'arm64']) + parser.add_argument( + '--reboot-android', + action='store_true', + help='Reboot the Android device before running the tests') parser.add_argument( '--local-run', action='store_true', @@ -176,6 +181,8 @@ def from_args(cls, args) -> 'CommonOptions': else: options.target_arch = 'x64' + options.reboot_android = args.reboot_android or args.ci_mode + options.report_on_failure = args.report_on_failure if args.targets is not None: options.targets = args.targets.split(',') diff --git a/tools/perf/run_perftests.py b/tools/perf/run_perftests.py index 7a35416eb17f..633f62ee9c02 100755 --- a/tools/perf/run_perftests.py +++ b/tools/perf/run_perftests.py @@ -23,6 +23,7 @@ import os import tempfile +import components.android_tools as android_tools import components.perf_config as perf_config import components.perf_test_runner as perf_test_runner import components.perf_test_utils as perf_test_utils @@ -127,6 +128,11 @@ def main(): json_config = load_config(args.config, options) config = perf_config.PerfConfig(json_config) + if options.is_android: + if options.reboot_android: + android_tools.RebootAndroid() + android_tools.SetupAndroidDevice() + if options.mode == PerfMode.RUN: if len(config.runners) != 1: raise RuntimeError('Only one configuration should be specified.') diff --git a/tools/perf/setup_android_device.sh b/tools/perf/setup_android_device.sh new file mode 100755 index 000000000000..d6e360f70bb1 --- /dev/null +++ b/tools/perf/setup_android_device.sh @@ -0,0 +1,17 @@ +#!/bin/sh +# Copyright (c) 2024 The Brave Authors. All rights reserved. +# 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 https://mozilla.org/MPL/2.0/. + +# The script is used to setup the android device: +# 1. Disable SELinux +# 2. Enable adb root (adbd must be restarted after) +# 3. Unlock the device screen + +setenforce 0 && \ +resetprop ro.debuggable 1 && \ +magiskpolicy --live "allow adbd adbd process setcurrent" && \ +magiskpolicy --live "allow adbd su process dyntransition" && \ +magiskpolicy --live "permissive { su }" && \ +input keyevent 82 From 94ca885dffa3367aacf7a220f1c146c6580148aa Mon Sep 17 00:00:00 2001 From: Mikhail Atuchin Date: Fri, 25 Oct 2024 15:41:15 +0400 Subject: [PATCH 2/3] Minor changes --- tools/perf/components/android_tools.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/perf/components/android_tools.py b/tools/perf/components/android_tools.py index 7c0f34192cb7..a0ba4f02d94a 100644 --- a/tools/perf/components/android_tools.py +++ b/tools/perf/components/android_tools.py @@ -6,8 +6,8 @@ import os import re import logging - import time + from typing import List, Optional from components.perf_test_utils import GetProcessOutput @@ -31,7 +31,7 @@ def SetupAndroidDevice() -> None: ], check=True) if not RunFromRoot(f'sh {tmp_file}'): - raise RuntimeError('Failed to setup root on the device') + raise RuntimeError('Failed to setup the device: setup_android_device.sh') RunFromRoot('killall adbd') # Restart adbd on the device to apply the changes time.sleep(2) From 82f7d7d0eeb16948ea4829c9e29311a63045f644 Mon Sep 17 00:00:00 2001 From: Mikhail Atuchin Date: Mon, 28 Oct 2024 15:18:55 +0400 Subject: [PATCH 3/3] RunFromRoot() => RunAsRoot() --- tools/perf/components/android_tools.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/perf/components/android_tools.py b/tools/perf/components/android_tools.py index a0ba4f02d94a..4f7fc4ae84ab 100644 --- a/tools/perf/components/android_tools.py +++ b/tools/perf/components/android_tools.py @@ -14,7 +14,7 @@ import components.path_util as path_util -def RunFromRoot(cmd: str) -> bool: +def RunAsRoot(cmd: str) -> bool: result, _ = GetProcessOutput( [path_util.GetAdbPath(), 'shell', 'su', '-c', '\'' + cmd + '\'']) return result @@ -30,15 +30,15 @@ def SetupAndroidDevice() -> None: tmp_file, ], check=True) - if not RunFromRoot(f'sh {tmp_file}'): + if not RunAsRoot(f'sh {tmp_file}'): raise RuntimeError('Failed to setup the device: setup_android_device.sh') - RunFromRoot('killall adbd') # Restart adbd on the device to apply the changes + RunAsRoot('killall adbd') # Restart adbd on the device to apply the changes time.sleep(2) def RebootAndroid() -> None: logging.debug('Rebooting Android device') - RunFromRoot('reboot') + RunAsRoot('reboot') time.sleep(30)