148f512ceSopenharmony_ci#!/usr/bin/env python
248f512ceSopenharmony_ci# -*- coding: utf-8 -*-
348f512ceSopenharmony_ci#   Copyright (c) 2021 Huawei Device Co., Ltd.
448f512ceSopenharmony_ci#   Licensed under the Apache License, Version 2.0 (the "License");
548f512ceSopenharmony_ci#   you may not use this file except in compliance with the License.
648f512ceSopenharmony_ci#   You may obtain a copy of the License at
748f512ceSopenharmony_ci#
848f512ceSopenharmony_ci#       http://www.apache.org/licenses/LICENSE-2.0
948f512ceSopenharmony_ci#
1048f512ceSopenharmony_ci#   Unless required by applicable law or agreed to in writing, software
1148f512ceSopenharmony_ci#   distributed under the License is distributed on an "AS IS" BASIS,
1248f512ceSopenharmony_ci#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1348f512ceSopenharmony_ci#   See the License for the specific language governing permissions and
1448f512ceSopenharmony_ci#   limitations under the License.
1548f512ceSopenharmony_ci
1648f512ceSopenharmony_ciimport os
1748f512ceSopenharmony_ciimport time
1848f512ceSopenharmony_ciimport argparse
1948f512ceSopenharmony_ciimport subprocess
2048f512ceSopenharmony_cifrom hiperf_utils import HdcInterface
2148f512ceSopenharmony_cifrom hiperf_utils import get_build_id
2248f512ceSopenharmony_cifrom hiperf_utils import get_architecture
2348f512ceSopenharmony_cifrom hiperf_utils import PerformanceProfile
2448f512ceSopenharmony_cifrom hiperf_utils import dir_check
2548f512ceSopenharmony_cifrom hiperf_utils import get_hiperf_binary_path
2648f512ceSopenharmony_cifrom hiperf_utils import bytes_to_str
2748f512ceSopenharmony_cifrom hiperf_utils import str_to_bytes
2848f512ceSopenharmony_cifrom hiperf_utils import remove
2948f512ceSopenharmony_ci
3048f512ceSopenharmony_ci
3148f512ceSopenharmony_cidef check_args(args):
3248f512ceSopenharmony_ci    if (not args.package_name) and args.ability:
3348f512ceSopenharmony_ci        raise Exception('-a can only be used when profiling an OHOS '
3448f512ceSopenharmony_ci                        'package_name.')
3548f512ceSopenharmony_ci    return True
3648f512ceSopenharmony_ci
3748f512ceSopenharmony_ci
3848f512ceSopenharmony_cidef parser_add_argument():
3948f512ceSopenharmony_ci    description = "Collect performance sampling information of" \
4048f512ceSopenharmony_ci                  " running [command]."
4148f512ceSopenharmony_ci    parser = argparse.ArgumentParser(description=description)
4248f512ceSopenharmony_ci    target_group = parser.add_argument_group(title='Select profiling target') \
4348f512ceSopenharmony_ci        .add_mutually_exclusive_group(required=True)
4448f512ceSopenharmony_ci    target_group.add_argument('-app', '--package_name',
4548f512ceSopenharmony_ci                              help="""Collect profile info for an OHOS app""")
4648f512ceSopenharmony_ci
4748f512ceSopenharmony_ci    target_group.add_argument('-lp', '--local_program',
4848f512ceSopenharmony_ci                              help="""Collect profile info
4948f512ceSopenharmony_ci                               for an local program.""")
5048f512ceSopenharmony_ci
5148f512ceSopenharmony_ci    target_group.add_argument('-cmd',
5248f512ceSopenharmony_ci                              help="""Running a command on the OHOS device.
5348f512ceSopenharmony_ci                              like as : -cmd "'ps -ef'".
5448f512ceSopenharmony_ci                              the ps will open as child process of hiperf
5548f512ceSopenharmony_ci                              and sample this process.""")
5648f512ceSopenharmony_ci
5748f512ceSopenharmony_ci    target_group.add_argument('-p', '--pid', nargs='*',
5848f512ceSopenharmony_ci                              help="""Limit the process id of the collection
5948f512ceSopenharmony_ci                         target.""")
6048f512ceSopenharmony_ci
6148f512ceSopenharmony_ci    target_group.add_argument('-t', '--tid', nargs='*',
6248f512ceSopenharmony_ci                              help="""Limit the thread id of the collection
6348f512ceSopenharmony_ci                        target.""")
6448f512ceSopenharmony_ci
6548f512ceSopenharmony_ci    target_group.add_argument('-sw', '--system_wide', action='store_true',
6648f512ceSopenharmony_ci                              help="""Collect system-wide information.
6748f512ceSopenharmony_ci                        This requires CAP_PERFMON (since Linux 5.8) or
6848f512ceSopenharmony_ci                        CAP_SYS_ADMIN capability or a
6948f512ceSopenharmony_ci                        /proc/sys/kernel/perf_event_paranoid
7048f512ceSopenharmony_ci                        value of less than 1.""")
7148f512ceSopenharmony_ci    record_group = parser.add_argument_group('Select recording options')
7248f512ceSopenharmony_ci    record_group.add_argument('-a', '--ability',
7348f512ceSopenharmony_ci                              help="""Used with -p. Profile the launch time of
7448f512ceSopenharmony_ci                        an ability in an OHOS app. The app will be started or
7548f512ceSopenharmony_ci                        restarted to run the ability.
7648f512ceSopenharmony_ci                        Like : -a .MainAbility """)
7748f512ceSopenharmony_ci
7848f512ceSopenharmony_ci    record_group.add_argument('-r', '--record_options',
7948f512ceSopenharmony_ci                              default='-f 1000 -d 10 -s dwarf',
8048f512ceSopenharmony_ci                              help="""Set recording options for `hiperf record`
8148f512ceSopenharmony_ci                         command. Default is "'-f 1000 -d 10 -s dwarf'".""")
8248f512ceSopenharmony_ci
8348f512ceSopenharmony_ci    record_group.add_argument('-lib', '--local_lib_dir', type=dir_check,
8448f512ceSopenharmony_ci                              help="""When profiling an OHOS app containing
8548f512ceSopenharmony_ci                         local thelocal libraries are usually stripped and lake
8648f512ceSopenharmony_ci                         of symbols and debug information to provide good
8748f512ceSopenharmony_ci                         profiling result. By using -lib, you tell
8848f512ceSopenharmony_ci                         command_script.py the path storing unstripped local
8948f512ceSopenharmony_ci                         libraries, and script will search all shared libraries
9048f512ceSopenharmony_ci                         with suffix .so in the directory. Then the local
9148f512ceSopenharmony_ci                         libraries will be downloaded on device and collected
9248f512ceSopenharmony_ci                         in build_cache.""")
9348f512ceSopenharmony_ci
9448f512ceSopenharmony_ci    record_group.add_argument('-o', '--output_perf_data', default='perf.data',
9548f512ceSopenharmony_ci                              help='The path to store profiling data. '
9648f512ceSopenharmony_ci                                   'Default is perf.data.')
9748f512ceSopenharmony_ci
9848f512ceSopenharmony_ci    other_group = parser.add_argument_group('Other options')
9948f512ceSopenharmony_ci
10048f512ceSopenharmony_ci    other_group.add_argument('--not_hdc_root', action='store_true',
10148f512ceSopenharmony_ci                             help="""Force hdc to run in non root mode. """)
10248f512ceSopenharmony_ci    args = parser.parse_args()
10348f512ceSopenharmony_ci    return args
10448f512ceSopenharmony_ci
10548f512ceSopenharmony_ci
10648f512ceSopenharmony_cidef main(args):
10748f512ceSopenharmony_ci    check_args(args)
10848f512ceSopenharmony_ci    profiler = PerformanceProfile(args)
10948f512ceSopenharmony_ci    profiler.profile()
11048f512ceSopenharmony_ci    return True
11148f512ceSopenharmony_ci
11248f512ceSopenharmony_ci
11348f512ceSopenharmony_ciif __name__ == '__main__':
11448f512ceSopenharmony_ci    main(parser_add_argument())
115