1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import os 17import time 18import argparse 19import subprocess 20from hiperf_utils import HdcInterface 21from hiperf_utils import get_build_id 22from hiperf_utils import get_architecture 23from hiperf_utils import PerformanceProfile 24from hiperf_utils import dir_check 25from hiperf_utils import get_hiperf_binary_path 26from hiperf_utils import bytes_to_str 27from hiperf_utils import str_to_bytes 28from hiperf_utils import remove 29 30 31def check_args(args): 32 if (not args.package_name) and args.ability: 33 raise Exception('-a can only be used when profiling an OHOS ' 34 'package_name.') 35 return True 36 37 38def parser_add_argument(): 39 description = "Collect performance sampling information of" \ 40 " running [command]." 41 parser = argparse.ArgumentParser(description=description) 42 target_group = parser.add_argument_group(title='Select profiling target') \ 43 .add_mutually_exclusive_group(required=True) 44 target_group.add_argument('-app', '--package_name', 45 help="""Collect profile info for an OHOS app""") 46 47 target_group.add_argument('-lp', '--local_program', 48 help="""Collect profile info 49 for an local program.""") 50 51 target_group.add_argument('-cmd', 52 help="""Running a command on the OHOS device. 53 like as : -cmd "'ps -ef'". 54 the ps will open as child process of hiperf 55 and sample this process.""") 56 57 target_group.add_argument('-p', '--pid', nargs='*', 58 help="""Limit the process id of the collection 59 target.""") 60 61 target_group.add_argument('-t', '--tid', nargs='*', 62 help="""Limit the thread id of the collection 63 target.""") 64 65 target_group.add_argument('-sw', '--system_wide', action='store_true', 66 help="""Collect system-wide information. 67 This requires CAP_PERFMON (since Linux 5.8) or 68 CAP_SYS_ADMIN capability or a 69 /proc/sys/kernel/perf_event_paranoid 70 value of less than 1.""") 71 record_group = parser.add_argument_group('Select recording options') 72 record_group.add_argument('-a', '--ability', 73 help="""Used with -p. Profile the launch time of 74 an ability in an OHOS app. The app will be started or 75 restarted to run the ability. 76 Like : -a .MainAbility """) 77 78 record_group.add_argument('-r', '--record_options', 79 default='-f 1000 -d 10 -s dwarf', 80 help="""Set recording options for `hiperf record` 81 command. Default is "'-f 1000 -d 10 -s dwarf'".""") 82 83 record_group.add_argument('-lib', '--local_lib_dir', type=dir_check, 84 help="""When profiling an OHOS app containing 85 local thelocal libraries are usually stripped and lake 86 of symbols and debug information to provide good 87 profiling result. By using -lib, you tell 88 command_script.py the path storing unstripped local 89 libraries, and script will search all shared libraries 90 with suffix .so in the directory. Then the local 91 libraries will be downloaded on device and collected 92 in build_cache.""") 93 94 record_group.add_argument('-o', '--output_perf_data', default='perf.data', 95 help='The path to store profiling data. ' 96 'Default is perf.data.') 97 98 other_group = parser.add_argument_group('Other options') 99 100 other_group.add_argument('--not_hdc_root', action='store_true', 101 help="""Force hdc to run in non root mode. """) 102 args = parser.parse_args() 103 return args 104 105 106def main(args): 107 check_args(args) 108 profiler = PerformanceProfile(args) 109 profiler.profile() 110 return True 111 112 113if __name__ == '__main__': 114 main(parser_add_argument()) 115