From fb9bdd8da3b233dc10802081ae80914c10a69e55 Mon Sep 17 00:00:00 2001 From: Yan Wang Date: Thu, 20 Jun 2019 15:16:22 -0700 Subject: startop: Rewrite the run app bash script to python. Test: python run_app_with_prefetch.py -p com.android.settings -a com.android.settings.Settings -r fadvise -i input --debug --simulate Test: python run_app_with_prefetch.py -p com.android.settings -a com.android.settings.Settings -r fadvise -i input Test: pytest run_app_with_prefetch_test.py Bug: 135286022 Change-Id: I761e5d20292febcb47b7ca9f87d6847d77250f68 --- startop/scripts/app_startup/lib/adb_utils.py | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 startop/scripts/app_startup/lib/adb_utils.py (limited to 'startop/scripts/app_startup/lib/adb_utils.py') diff --git a/startop/scripts/app_startup/lib/adb_utils.py b/startop/scripts/app_startup/lib/adb_utils.py new file mode 100644 index 000000000000..00e2e9995863 --- /dev/null +++ b/startop/scripts/app_startup/lib/adb_utils.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +# +# Copyright 2019, The Android Open Source Project +# +# 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. + +"""Helper util libraries for calling adb command line.""" + +import os +import sys + +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname( + os.path.abspath(__file__))))) +import lib.cmd_utils as cmd_utils + +def logcat_save_timestamp() -> str: + """Gets the current logcat timestamp. + + Returns: + A string of timestamp. + """ + _, output = cmd_utils.run_adb_shell_command( + "date -u +\'%Y-%m-%d %H:%M:%S.%N\'") + return output + +def vm_drop_cache(): + """Free pagecache and slab object.""" + cmd_utils.run_adb_shell_command('echo 3 > /proc/sys/vm/drop_caches') + +def root(): + """Roots adb and successive adb commands will run under root.""" + cmd_utils.run_shell_command('adb root') + +def disable_selinux(): + """Disables selinux setting.""" + _, output = cmd_utils.run_adb_shell_command('getenforce') + if output == 'Permissive': + return + + print('Disable selinux permissions and restart framework.') + cmd_utils.run_adb_shell_command('setenforce 0') + cmd_utils.run_adb_shell_command('stop') + cmd_utils.run_adb_shell_command('start') + cmd_utils.run_shell_command('adb wait-for-device') + +def pkill(procname: str): + """Kills a process in device by its package name.""" + _, pids = cmd_utils.run_shell_command('adb shell ps | grep "{}" | ' + 'awk \'{{print $2;}}\''. + format(procname)) + + for pid in pids.split('\n'): + cmd_utils.run_adb_shell_command('kill {}'.format(pid)) -- cgit v1.2.3 From 34e2dabcf966102906f2b9e33b96bea2fa9728a4 Mon Sep 17 00:00:00 2001 From: Yan Wang Date: Mon, 8 Jul 2019 17:47:39 -0700 Subject: startop: Move compiled trace file installation after activity checking (bugfix). Test: pytest run_app_with_prefetch_test.py Test: python run_app_with_prefetch.py -p com.android.settings -i input -d -r fadvise Bug: 135286022 Change-Id: I06040bb10bfd8edf3f520576c4aed5a28587b142 --- startop/scripts/app_startup/lib/adb_utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'startop/scripts/app_startup/lib/adb_utils.py') diff --git a/startop/scripts/app_startup/lib/adb_utils.py b/startop/scripts/app_startup/lib/adb_utils.py index 00e2e9995863..761dc2e82204 100644 --- a/startop/scripts/app_startup/lib/adb_utils.py +++ b/startop/scripts/app_startup/lib/adb_utils.py @@ -18,6 +18,7 @@ import os import sys +import time sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname( os.path.abspath(__file__))))) @@ -54,10 +55,13 @@ def disable_selinux(): cmd_utils.run_shell_command('adb wait-for-device') def pkill(procname: str): - """Kills a process in device by its package name.""" + """Kills a process on device specified by the substring pattern in procname""" _, pids = cmd_utils.run_shell_command('adb shell ps | grep "{}" | ' 'awk \'{{print $2;}}\''. format(procname)) for pid in pids.split('\n'): - cmd_utils.run_adb_shell_command('kill {}'.format(pid)) + pid = pid.strip() + if pid: + passed,_ = cmd_utils.run_adb_shell_command('kill {}'.format(pid)) + time.sleep(1) -- cgit v1.2.3 From 3fb2816c59b8c99d1be7ba0f96560e4262dda8a1 Mon Sep 17 00:00:00 2001 From: Yan Wang Date: Mon, 8 Jul 2019 17:48:05 -0700 Subject: startop: Rewrite metrics parsing using python. Shell version metrics parser generates some strange results when called from Python. Test: pytest logcat_utils_test.py Test: pytest adb_utils_test.py Test: pytest run_app_with_prefetch_test.py Change-Id: I44a464f7e87f35ecc283c5108577eb33ad394fc6 --- startop/scripts/app_startup/lib/adb_utils.py | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'startop/scripts/app_startup/lib/adb_utils.py') diff --git a/startop/scripts/app_startup/lib/adb_utils.py b/startop/scripts/app_startup/lib/adb_utils.py index 761dc2e82204..0e0065defd7f 100644 --- a/startop/scripts/app_startup/lib/adb_utils.py +++ b/startop/scripts/app_startup/lib/adb_utils.py @@ -16,13 +16,18 @@ """Helper util libraries for calling adb command line.""" +import datetime import os +import re import sys import time +from typing import Optional sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname( os.path.abspath(__file__))))) import lib.cmd_utils as cmd_utils +import lib.logcat_utils as logcat_utils + def logcat_save_timestamp() -> str: """Gets the current logcat timestamp. @@ -65,3 +70,38 @@ def pkill(procname: str): if pid: passed,_ = cmd_utils.run_adb_shell_command('kill {}'.format(pid)) time.sleep(1) + +def parse_time_to_milliseconds(time: str) -> int: + """Parses the time string to milliseconds.""" + # Example: +1s56ms, +56ms + regex = r'\+((?P\d+?)s)?(?P\d+?)ms' + result = re.search(regex, time) + second = 0 + if result.group('second'): + second = int(result.group('second')) + ms = int(result.group('millisecond')) + return second * 1000 + ms + +def blocking_wait_for_logcat_displayed_time(timestamp: datetime.datetime, + package: str, + timeout: int) -> Optional[int]: + """Parses the displayed time in the logcat. + + Returns: + the displayed time. + """ + pattern = re.compile('.*ActivityTaskManager: Displayed {}.*'.format(package)) + # 2019-07-02 22:28:34.469453349 -> 2019-07-02 22:28:34.469453 + timestamp = datetime.datetime.strptime(timestamp[:-3], + '%Y-%m-%d %H:%M:%S.%f') + timeout_dt = timestamp + datetime.timedelta(0, timeout) + # 2019-07-01 14:54:21.946 27365 27392 I ActivityTaskManager: + # Displayed com.android.settings/.Settings: +927ms + result = logcat_utils.blocking_wait_for_logcat_pattern(timestamp, + pattern, + timeout_dt) + if not result or not '+' in result: + return None + displayed_time = result[result.rfind('+'):] + + return parse_time_to_milliseconds(displayed_time) \ No newline at end of file -- cgit v1.2.3 From 59aab11f092b4f9d817c331a470fa8d2fad445ab Mon Sep 17 00:00:00 2001 From: Yan Wang Date: Wed, 24 Jul 2019 10:40:38 -0700 Subject: startop: Rewrite the perfetto trace collection part. Test: pytest perfetto_trace_collector_test.py Bug: 138233615 Change-Id: If13d895029e734a5e52bed73c5f870bb3f036c2f --- startop/scripts/app_startup/lib/adb_utils.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'startop/scripts/app_startup/lib/adb_utils.py') diff --git a/startop/scripts/app_startup/lib/adb_utils.py b/startop/scripts/app_startup/lib/adb_utils.py index 0e0065defd7f..1c60a17ada1b 100644 --- a/startop/scripts/app_startup/lib/adb_utils.py +++ b/startop/scripts/app_startup/lib/adb_utils.py @@ -104,4 +104,21 @@ def blocking_wait_for_logcat_displayed_time(timestamp: datetime.datetime, return None displayed_time = result[result.rfind('+'):] - return parse_time_to_milliseconds(displayed_time) \ No newline at end of file + return parse_time_to_milliseconds(displayed_time) + +def delete_file_on_device(file_path: str) -> None: + """ Deletes a file on the device. """ + cmd_utils.run_adb_shell_command( + "[[ -f '{file_path}' ]] && rm -f '{file_path}' || " + "exit 0".format(file_path=file_path)) + +def set_prop(property: str, value: str) -> None: + """ Sets property using adb shell. """ + cmd_utils.run_adb_shell_command('setprop "{property}" "{value}"'.format( + property=property, value=value)) + +def pull_file(device_file_path: str, output_file_path: str) -> None: + """ Pulls file from device to output """ + cmd_utils.run_shell_command('adb pull "{device_file_path}" "{output_file_path}"'. + format(device_file_path=device_file_path, + output_file_path=output_file_path)) -- cgit v1.2.3 From 2ae4f03578891bd51aab0d665955e268ff06b27a Mon Sep 17 00:00:00 2001 From: Yan Wang Date: Thu, 8 Aug 2019 18:28:32 -0700 Subject: startop: Change trace duration in compiler.py to timedelta. The time in timestamp is second, while the trace duration is millisecond. Using timedelta to fix this ambiguity. Test: pytest compiler_test.py Change-Id: I6c445c3dac1b60aec77ead14df021b8a2dda7b5e --- startop/scripts/app_startup/lib/adb_utils.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'startop/scripts/app_startup/lib/adb_utils.py') diff --git a/startop/scripts/app_startup/lib/adb_utils.py b/startop/scripts/app_startup/lib/adb_utils.py index 1c60a17ada1b..e56a96848895 100644 --- a/startop/scripts/app_startup/lib/adb_utils.py +++ b/startop/scripts/app_startup/lib/adb_utils.py @@ -42,6 +42,8 @@ def logcat_save_timestamp() -> str: def vm_drop_cache(): """Free pagecache and slab object.""" cmd_utils.run_adb_shell_command('echo 3 > /proc/sys/vm/drop_caches') + # Sleep a little bit to provide enougth time for cache cleanup. + time.sleep(2) def root(): """Roots adb and successive adb commands will run under root.""" -- cgit v1.2.3 From ab89ecbc8210249c7c81c3e9d24b45c378acf652 Mon Sep 17 00:00:00 2001 From: Yan Wang Date: Tue, 3 Sep 2019 11:17:22 -0700 Subject: startop: Fix comments in gerrit CL 9172239. Test: pytest Change-Id: I706730c8d54e575e26ff0e2bdbb668d2e70a5c2e --- startop/scripts/app_startup/lib/adb_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'startop/scripts/app_startup/lib/adb_utils.py') diff --git a/startop/scripts/app_startup/lib/adb_utils.py b/startop/scripts/app_startup/lib/adb_utils.py index e56a96848895..3cebc9a97a50 100644 --- a/startop/scripts/app_startup/lib/adb_utils.py +++ b/startop/scripts/app_startup/lib/adb_utils.py @@ -42,8 +42,8 @@ def logcat_save_timestamp() -> str: def vm_drop_cache(): """Free pagecache and slab object.""" cmd_utils.run_adb_shell_command('echo 3 > /proc/sys/vm/drop_caches') - # Sleep a little bit to provide enougth time for cache cleanup. - time.sleep(2) + # Sleep a little bit to provide enough time for cache cleanup. + time.sleep(1) def root(): """Roots adb and successive adb commands will run under root.""" -- cgit v1.2.3