• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#   Copyright (c) 2025 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.
15import argparse
16from hiperf_utils import PerformanceProfile
17from hiperf_utils import dir_check
18
19
20def check_args(args):
21    if (not args.package_name) and args.ability:
22        raise Exception('-a can only be used when profiling an OHOS '
23                        'package_name.')
24    return True
25
26
27def get_module(args):
28    if args.prepare:
29        return "prepare"
30    elif args.start:
31        return "start"
32    elif args.pause:
33        return "pause"
34    elif args.resume:
35        return "resume"
36    elif args.stop:
37        return "stop"
38
39
40def parser_add_argument():
41    description = "Collect performance sampling information of" \
42                  " running [command]."
43    parser = argparse.ArgumentParser(description=description)
44
45    control_group = parser.add_argument_group(
46        'Select Control options').add_mutually_exclusive_group(required=True)
47    control_group.add_argument('--prepare', action='store_true',
48                               help='prepare need to add profiling target to '
49                                    'execute record .'
50                                    'Like --prepare -p 121')
51    control_group.add_argument('--start', action='store_true',
52                               help='start execute hiperf record')
53    control_group.add_argument('--pause', action='store_true',
54                               help='pause execute hiperf record')
55    control_group.add_argument('--resume', action='store_true',
56                               help='resume execute hiperf record')
57    control_group.add_argument('--stop', action='store_true',
58                               help='stop execute hiperf record'
59                                    'and file recv data file to local')
60
61    target_group = parser.add_argument_group(title='Select profiling target') \
62        .add_mutually_exclusive_group(required=False)
63    target_group.add_argument('-app', '--package_name',
64                              help="""Collect profile info for an OHOS app""")
65
66    target_group.add_argument('-lp', '--local_program',
67                              help="""Collect profile info
68                              for an local program.""")
69
70    target_group.add_argument('-cmd',
71                              help="""Running a command on the OHOS device.
72                              like as : -cmd "'ps -ef'".
73                              the ps will open as child process of hiperf
74                              and sample this process.""")
75
76    target_group.add_argument('-p', '--pid', nargs='*',
77                              help="""Limit the process id of the collection
78                              target.""")
79
80    target_group.add_argument('-t', '--tid', nargs='*',
81                              help="""Limit the thread id of the collection
82                              target.""")
83
84    target_group.add_argument('-sw', '--system_wide', action='store_true',
85                              help="""Collect system-wide information.
86                              This requires CAP_PERFMON (since Linux 5.8) or
87                              CAP_SYS_ADMIN capability or a
88                              /proc/sys/kernel/perf_event_paranoid
89                              value of less than 1.""")
90
91    record_group = parser.add_argument_group('Select recording options')
92    record_group.add_argument('-a', '--ability',
93                              help="""Used with -p. Profile the launch time of
94                              an ability in an OHOS app. The app will be started or
95                              restarted to run the ability.
96                              Like : -a .MainAbility """)
97
98    record_group.add_argument('-r', '--record_options',
99                              default='-f 1000 -s dwarf',
100                              help="""Set recording options for `hiperf record`
101                              command. Default is "'-f 1000 -s dwarf'".""")
102
103    record_group.add_argument('-lib', '--local_lib_dir', type=dir_check,
104                              help="""When profiling an OHOS app containing
105                              local thelocal libraries are usually stripped and lake
106                              of symbols and debug information to provide good
107                              profiling result. By using -lib, you tell
108                              command_script.py the path storing unstripped local
109                              libraries, and script will search all shared libraries
110                              with suffix .so in the directory. Then the local
111                              libraries will be downloaded on device and collected
112                              in build_cache.""")
113
114    record_group.add_argument('-o', '--output_perf_data', default='perf.data',
115                              help='The path to store profiling data. '
116                              'Default is perf.data.')
117
118    other_group = parser.add_argument_group('Other options')
119
120    other_group.add_argument('--not_hdc_root', action='store_true',
121                             help="""Force hdc to run in non root mode. """)
122
123    args = parser.parse_args()
124    return args
125
126
127def main(args):
128    print('cmd:::')
129    check_args(args)
130    print('cmd:::' + args.cmd)
131    print('package_name:' + args.package_name)
132    print('local_program:' + args.local_program)
133    print('system_wide:' + args.system_wide)
134    print('pid:' + args.pid)
135    print('tid:' + args.tid)
136
137    args_mark = [args.package_name, args.local_program, args.cmd,
138                 args.pid, args.tid, args.system_wide]
139    any_args = any(args_mark)
140    if args.prepare and not any_args:
141        print("prepare need one of -app/-lp/-cmd/-p/-t/-sw "
142              "argument to execute record")
143        return False
144    profiler = PerformanceProfile(args, control_module=get_module(args))
145    profiler.profile()
146    return True
147
148
149if __name__ == '__main__':
150    main(parser_add_argument())
151