openharmony 鸿蒙 hiperf

2025-06-12 浏览 (1)

hiperf

hiperf is a command-line tool provided to capture performance data of a specific program or the entire system, like the kernel's perf tool. It can run on Windows, Linux, and macOS.

Environment Setup

  • The environment for OpenHarmony Device Connector (hdc) has been set up. For details, see Environment Setup.

  • The devices are properly connected and hdc shell is executed.

hiperf

ParameterDescription
-h/--helpDisplays the help information.
--debugOutputs debug logs.
--hilogWrites logs to the hilog.
--logpathSpecifies the log path.
--logtagSpecifies the log level.
--mixlogOutputs mixed logs.
--muchOutput as many logs as possible.
--nodebugOutputs no log.
--verboseOutputs verbose logs.

help

Run the --help command to view help information.

hiperf --help

Run the following command to view the help information about a command:

hiperf [command] --help

list

Lists all the supported events on the device. The event names are used for the -e and -g parameters of the stat and record commands.

Parameters of the list command

ParameterDescription
-h/--helpDisplays the help information.
hwLists the hardware events.
swLists the software events.
tpTracepoint event.
cacheLists the hardware cache events.
rawLists the original PMU events.
Usage: hiperf list [event type name]

Run the help command to query the supported event types.

hiperf list --help

Run the following command to list the hardware events supported and not supported by the device.

hiperf list hw

record

Specifies the target program for sampling and saves the sampled data to a file (/data/local/tmp/perf.data by default).

Parameters of the record command

ParameterDescription
-h/--helpDisplays the help information.
-aCollects the full information of the system for evaluating all processes and threads.
--exclude-hiperfCollects no hiperf data.
-cSpecifies the ID of the CPU to collect its data.
--cpu-limitSets the maximum CPU usage during collection. Value range: 1 to 100. The default value is 25.
-dSets the collection duration.
-fSets the collection frequency. The default value is 4000 times per second. This parameter cannot be used together with --period.
--periodSets the event collection period, that is, the number of events to be collected each time. This parameter cannot be used together with -f.
-eSpecifies the events to collect, which are separated by commas (,).
-gSpecifies the event groups to collect, which are separated by commas (,).
--no-inheritCollects no subprocess data.
-pSpecifies the IDs of the processes to collect, which are separated by commas (,). This parameter cannot be used together with -a.
-tSpecifies the IDs of the threads to collect, which are separated by commas (,). This parameter cannot be used together with -a.
--exclude-tidSpecifies the IDs of the threads not to collect, which are separated by commas (,). This parameter cannot be used together with -a.
--exclude-threadSpecifies the names of the threads not to collect, which are separated by commas (,). This parameter cannot be used together with -a.
--exclude-processSpecifies the names of the processes not to collect, which are separated by commas (,). This parameter must be used together with -a.
--offcpuTraces the time when a thread is out of CPU scheduling.
-jSamples branch stacks. The following filters are supported: any, any_call, any_ret, ind_call, ind_jmp, cond and call.
-s/--callstackSets the stack mode.
--kernel-callchainCollects kernel stack information. This parameter must be used together with -s fp/dwarf.
--callchain-useronlyCollects only user stacks.
--delay-unwindUnwinds the stack after recording. If -s dwarf is set, the stack is unwound during recording.
--disable-unwindDisables the unwinding of the stack during recording by default when -s dwarf is set.
--disable-callstack-expandDisables the 64 KB stack limit when -s dwarf is set. By default, call stacks are combined to a more complete call stack, which may be inaccurate.
--enable-debuginfo-symbolicParses the symbols in the .gnu_debugdata section of elf when -s fp/dwarf is set. By default, the symbols are not parsed.
--clockidSets the type of the clock to collect, which can be monotonic or monotonic_raw.
--symbol-dirSpecifies the directory of the online symbol table file.
-mSets the number of mmap pages. Value range: 2 to 1024. The default value is 1024.
--appSpecifies the names of the applications to collect, which are separated by commas (,). The applications must be in debuggable mode. If an application is not started, the system waits for 20s.
--chkmsSets the check interval. Value range: 1 to 200. The default value is 10.
--data-limitSets the limit of the output data size. When this limit is reached, the collection stops. By default, there is no limit.
-oSpecifies the output file path.
-zOutputs the data in a compressed file.
--restartCollects performance indicator information about application startup. If a process is not started within 30 seconds, the record exits.
--verboseOutputs a more detailed report.
--control [command]Controls the collection command. The following commands are supported: prepare/start/pause/resume/output/stop.
--dedup_stackDeletes duplicate stacks from a record. This parameter cannot be used together with -a.
--cmdline-sizeSets the value of the /sys/kernel/tracing/saved_cmdlines_size node. Value range: 512 to 4096.
--reportObtains the stack report after collection. This parameter cannot be used together with -a.
--backtrackCollects data in a previous period. This parameter must be used together with --control prepare.
--backtrack-secCollects the duration of previous data. The value ranges from 5 to 30. The default value is 10s. This parameter must be used together with --backtrack.
--dumpoptionsDumps the command options.
Usage: hiperf record [options] [command [command-args]]

Sample the process 267 for 10 seconds and use dwarf to collect and unwind stack memory of the process.

hiperf record -p 267 -d 10 -s dwarf

stat

Monitors the specified application and periodically prints the values of performance counters.

Parameters of the stat command

ParameterDescription
-h/--helpDisplays the help information.
-aCollects the full information of the system for evaluating all processes and threads.
-cSpecifies the ID of the CPU to collect its data.
-dSets the collection duration.
-iSets the interval for printing stat information. Unit: ms.
-eSpecifies the events to collect, which are separated by commas (,).
-gSpecifies the event groups to collect, which are separated by commas (,).
--no-inheritCollects no subprocess data.
-pSpecifies the IDs of the processes to collect, which are separated by commas (,). This parameter cannot be used together with -a.
-tSpecifies the IDs of the threads to collect, which are separated by commas (,). This parameter cannot be used together with -a.
--appSpecifies the names of the applications to collect, which are separated by commas (,). The applications must be in debuggable mode. If an application is not started, the system waits for 10s.
--chkmsSets the check interval. Value range: 1 to 200. The default value is 10.
--per-coreObtains the print count of each CPU core.
--pre-threadObtains the print count of each thread.
--restartCollects performance indicator information about application startup. If a process is not started within 30 seconds, the record exits.
--verboseOutputs a more detailed report.
--dumpoptionsDumps the command options.
--control [command]Controls the collection command. The commands include prepare, start, and stop.
NOTE: This parameter is supported since API version 20.
-oSets a path to output a file. This parameter must be used together with --control prepare.
NOTE: This parameter is supported since API version 20.
Usage: hiperf stat [options] [command [command-args]]

Monitor the performance counter of process 2349 on CPU0 for 3 seconds.

hiperf stat -p 2349 -d 3 -c 0

dump

Reads the data in perf.data without processing the file. You can check whether the original sampling data is correct.

Parameters of the dump command

ParameterDescription
-h/--helpDisplays the help information.
--headOutputs only the data header and attributes.
-dOutputs only data segments.
-fOutputs only additional functions.
--syspathSpecifies the path of the symbol table file.
-iSpecifies the resource file path.
-oSpecifies the output file path. If this parameter is not set, the file is displayed on the screen.
--elfOutputs the ELF file.
--protoOutputs data in ProtoBuf format.
--exportExports user stack data to a split file and generates ut data.
Usage: hiperf dump [option] \<filename\>

Run the dump command to read the /data/local/tmp/perf.data file and export it to the /data/local/tmp/perf.dump file.

hiperf dump -i /data/local/tmp/perf.data -o /data/local/tmp/perf.dump

report

Displays the sampled data (read from perf.data) in required format (such as JSON or ProtoBuf).

Parameters of the report command

ParameterDescription
-h/--helpDisplays the help information.
--symbol-dirSpecifies the path of the symbol table file.
--limit-percentSets the percentage of the content to display.
-sDisplays the stack mode.
--call-stack-limit-percentSets the percentage of the stack to display.
-iSpecifies the resource file path. The default value is perf.data.
-oSpecifies the output file path. If this parameter is not set, the file is displayed on the screen.
--protoOutputs data in ProtoBuf format.
--jsonOutputs data in JSON format.
--diffDisplays the differences between the -i and **--diff **files.
--branchDisplays the branch in the address instead of the IP address.
--<keys> <keyname1>[,keyname2][,...]Specifies the keywords, which can be comms, pids, tids, dsos, funcs, from_dsos or from_funcs, for example, --comms hiperf.
--sort [key1],[key2],[...]Sorts the data by keyword.
--hide_countHides values in the report.
--dumpoptionsDumps the command options.
Usage: hiperf report [option] \<filename\>

Display a common report, with the sampling limit of 1%.

hiperf report --limit-percent 1

script

You can use a script to sample data and generate flame graphs. You can obtain the script from developtools_hiperf.

  1. Sample data.

    Run command_script.py to sample data. This script packages the report command.

    usage: command_script.py [-h]
                             (-app PACKAGE_NAME|-lp LOCAL_PROGRAM|-cmd CMD|-p [PID [PID ...]]|-t [TID [TID ...]]|-sw)
                             [-a ABILITY] [-r RECORD_OPTIONS] [-lib LOCAL_LIB_DIR]
                             [-o OUTPUT_PERF_DATA] [--not_hdc_root]
    

    Sample the com.ohos.launch package.

    python command_script.py -app com.ohos.launch
    

    Sample the hdcd process.

    python command_script.py -lp hdcd
    
  2. Collect symbol tables.

    Run recv_binary_cache.py to collect symbol tables. This script searches for the ELF in the specified paths based on the files and libraries recorded in perf.data and their buildids.

    usage: recv_binary_cache.py [-h] [-i PERF_DATA] [-l LOCAL_LIB_DIR [LOCAL_LIB_DIR ...]]
    

    The following example specifies two symbol table paths.

    python recv_binary_cache.py -l Z:\OHOS_MASTER\out\ohos-arm-release\lib.unstripped  Z:\OHOS_MASTER\out\ohos-arm-release\exe.unstripped
    

    The ELF files of the specified symbol table paths are copied to the binary_cache folder. If the paths do not exist, the file in the device will be copied.

  3. Generate a flame graph.

    Run make_report.py to display the sampled data in an HTML page.

    usage: make_report.py [-h] [-i PERF_DATA] [-r REPORT_HTML]
    

    Generate an HTML file. The default file name is hiperf_report.html.

    python make_report.py -i perf.data
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Performance Analysis Kit

harmony 鸿蒙Analyzing Application Freeze

harmony 鸿蒙Development of Application Recovery

harmony 鸿蒙Analyzing C++ Crash

harmony 鸿蒙Development of Error Manager

harmony 鸿蒙hdc

harmony 鸿蒙Event Reporting

harmony 鸿蒙Introduction to HiAppEvent

harmony 鸿蒙Subscribing to Address Sanitizer Events (ArkTS)

harmony 鸿蒙Subscribing to Address Sanitizer Events (C/C++)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/cLXiE4