• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# hiperf 使用指南
2
3
4
5hiperf是为开发人员提供性能采样分析的工具,基于内核perf机制进行的用户态能力的扩展,可以对指定的程序或者整个系统进行性能采样。
6
7
8hiperf支持的命令有:list、stat、record、report等,可以通过hiperf -h进行查看。
9
10
11下面对hiperf的常用命令进行详细说明:
12
13
14## list 命令
15
16
17### 参数说明
18
19列出设备上支持的所有perf事件名称,事件名称用于 stat 和 record命令的 -e 和 -g 参数。
20
21```
22Usage: hiperf list [event type]
23```
24
25| 参数 | 功能说明 |
26| -------- | -------- |
27| hw | 列出支持的 hardware 事件(PMU支持) |
28| sw | 列出支持的 software 事件 |
29| tp | 列出支持 tracepotint 事件 |
30| cache | 列出支持的 cache 事件(PMU支持) |
31| raw | 列出支持的原始PMU事件 |
32
33
34### 使用示例
35
36下面列出了设备支持的 HW 事件,并且会提示哪些事件此设备不支持。
37
38```
39hiperf list hw
40event not support hw-stalled-cycles-backend
41event not support hw-stalled-cycles-frontend
42event not support hw-ref-cpu-cycles
43
44Supported events for hardware:
45        hw-cpu-cycles
46        hw-instructions
47        hw-cache-references
48        hw-cache-misses
49        hw-branch-instructions
50        hw-branch-misses
51        hw-bus-cycles
52```
53
54
55## stat 命令
56
57
58### 参数说明
59
60监听指定目标程序,周期性打印性能计数器的值。
61```
62Usage: hiperf stat [options]
63       Collect performance counter information.
64```
65
66
67| 参数 | 功能说明 |
68| -------- | -------- |
69| -a | 采集监听整个系统的所有线程和默认的性能计数器的值。 |
70| -c | 指定cpu id,可以是0,1,2用逗号分隔。 |
71| -d <_sec_> | 指定监听的时间,单位为秒。 |
72| -i <_ms_> | 指定监听事件的间隔打印时间,单位毫秒。 |
73| -e | 指定监听的事件,可以通过list命令查看支持的所有事件,其中event:u 或者 :k 表示限制事件为用户空间或者内核空间。 |
74| -g | 指定需要在同一组监听的事件,同一组事件会被放入同一个PMU监听。 |
75| --no-inherit | 不监听目标线程/进程启动的子线程。 |
76| -p | 指定需要监听的进程pid。 |
77| -t | 指定需要监听的线程tid。 |
78| --verbose | 显示详细的报告内容,包括一些原始的时间数据等等。 |
79
80
81### 使用示例
82
83下面是通过 stat 监听整个系统3秒的示例:
84
85```
86hiperf stat -d 3 -a
87this is root mode, perfEventParanoid assume as -1
88Start Profiling...
89Timeout exit (total 3009 ms)
90                count                           name | comment                          | coverage
91               132523         hw-branch-instructions | 15.750 M/sec                     | (100%)
92                62554               hw-branch-misses | 47.202372% miss rate             | (100%)
93              6994768                  hw-cpu-cycles | 0.832068 GHz                     | (100%)
94              1237627                hw-instructions | 5.651758 cycles per instruction  | (100%)
95                  248            sw-context-switches | 29.959 K/sec                     | (100%)
96                    0                 sw-page-faults | 0.000 /sec                       | (100%)
97              9402580                  sw-task-clock | 0.002758 cpus used               | (100%)
98```
99
100
101### 字段说明
102
103| 字段名 | 含义 |
104| -------- | -------- |
105| count | 表示事件发生的次数 |
106| name | 事件的名称,在 list 命令中可以看到所有支持的命令,hw 和 sw 的前缀分别代表是hardware事件还是software事件。 |
107| comment | 一些经过计算的方便用户阅读的值。例如 CPU 的相对主频,千位进制换算等等。 |
108| coverage | 该事件在PMU中的 enable 百分比,受限于PMU数量,每次装载的PMU监听事件是有限的。 |
109
110
111## record 命令
112
113
114### 参数说明
115
116采样指定目标程序,并且将采样数据保存到指定的文件中(默认为perf.data117
118```
119Usage: hiperf record [options]
120       Collect performance sampling information.
121```
122
123| 参数 | 功能说明 |
124| -------- | -------- |
125| -a | 对整个系统的所有进程和线程进行采样。 |
126| --exclude-hiperf | 不采样hiperf自身进程。 |
127| -c | 指定采样的cpu id。 |
128| --cpu-limit <_percent_> | 限定采样过程占用的CPU最大百分比。 |
129| -d <sec> | 指定采样时长,单位秒。 |
130| -f&nbsp;&lt;freq&gt; | 设置采样事件的触发频率,默认为4000。<br/>说明:该频率越高cpu负载会越高,但是数据采样会越多。 |
131| --period&nbsp;&lt;_num_&gt; | 设置采样事件触发周期,以次数为单位,即发生指定的次数后采样一次。 |
132| -e | 指定监听的事件,可以通过list命令查看支持的所有事件,其中event:u&nbsp;或者&nbsp;:k&nbsp;可以表示限制事件为用户空间或者内核空间。 |
133| -g | 指定需要在同一组监听的事件,同一组事件会被放入同一个PMU监听。 |
134| --no-inherit | 不监听目标线程或者进程启动的子线程。 |
135| -p | 指定需要监听的进程。 |
136| -t | 指定需要监听的线程。 |
137| --offcpu | 监听cpu调度事件,它等价于&nbsp;--period&nbsp;1&nbsp;-e&nbsp;sched:sched_switch&nbsp;事件。 |
138| -j&nbsp;&lt;_branch_filter1_&gt;[,_branch_filter2_]... | 监听分支预测事件。分支预测就是指令存在多个if&nbsp;else判定的情况下,cpu去预测下一步即将执行哪一条指令。 |
139| -s&nbsp;/&nbsp;--call-stack&nbsp;&lt;_fp&nbsp;\\|&nbsp;dwarf[,size]_&gt; | 设置用户栈的回栈方式,支持fp和dwarf两种方式。dwarf&nbsp;后面可以指定采样的用户堆栈大小,默认是&nbsp;65528。 |
140| --delay-unwind | 延迟回栈,等到采样结束再进行回栈。 |
141| --disable-unwind | 不进行回栈,用户的寄存器和堆栈数据会保存在perf.data中,供离线回栈使用。 |
142| --disable-callstack-expend | 默认会对回栈的调用栈信息进行合并扩展,此选项会关闭该功能。 |
143| --clockid&nbsp;&lt;_clock&nbsp;type_&gt; | 设置采样数据的时钟源,支持monotonic、boottime、realtime等。 |
144| --symbol-dir&nbsp;&lt;_dir_&gt; | 指定符号表路径,如果指定了回栈时会优先使用该路径下的符号表。 |
145| -m&nbsp;&lt;_mmap&nbsp;pages_&gt; | 指定缓存的大小,默认值为1024,以页为单位。参数需要为2的幂,范围为&nbsp;[2&nbsp;-&nbsp;1024]。<br/>说明:该值越高,事件丢失率会越低,但是内存占用会增大。 |
146| --app&nbsp;&lt;_package&nbsp;name_&gt; | 指定采样的目标应用的包名,默认超时等待时间为10秒。 |
147| --data-limit&nbsp;&lt;_SIZE[K\|M|G]_&gt; | 指定采样结果的最大容量,支持K/M/G(B)为单位,默认无大小限制。 |
148| -o&nbsp;&lt;_output&nbsp;file&nbsp;name_&gt; | 指定采样数据结果文件名,默认为/data/local/tmp/perf.data。 |
149| -z | 启动压缩模式,在保存到文件的时候,会用gzip进行压缩。 |
150| --verbose | 采样的时候显示更详细的日志信息。 |
151
152
153### 使用示例
154
155- 对全系统所有进程采样3秒,并且显示详细的日志信息
156  ```
157  hiperf record -d 3 -a --verbose
158  ```
159
160- 指定使用fp方式进行回栈
161  ```
162  hiperf record -s fp -d 3 -a
163  ```
164
165- 指定使用dwarf方式进行回栈
166  ```
167  hiperf record -s dwarf -d 3 -a
168  ```
169
170- 指定采样offcpu事件
171  ```
172  hiperf record --offcpu -s dwarf -d 3 -a
173  ```
174
175- 指定延迟回栈
176  ```
177  hiperf record -d 3 -s dwarf --delay-unwind -a
178  ```
179
180- 禁止回栈,此时会保存堆栈数据到perf.data文件
181  ```
182  hiperf record -d 3 -s dwarf --disable-unwind -a
183  ```
184
185- 指定追踪的包名,如果指定的包名对应的进程不存在则会延时等待10秒,超时后进程退出
186  ```
187  hiperf record -d 3 -s dwarf --app com.ohos.launch
188  ```
189
190- 指定对采样结果数据进行压缩保存
191  ```
192  hiperf record -z -s dwarf -d 3  -a
193  ```
194
195
196## report 命令
197
198此命令主要用于展示record中抓取的采样数据
199
200```
201Usage: hiperf report [option]
202       Report sampling information from perf.data.
203```
204
205| 参数 | 功能说明 |
206| -------- | -------- |
207| --symbol-dir&nbsp;&lt;_dir_&gt; | 指定符号表路径。 |
208| --limit-percent&nbsp;&lt;_number_&gt; | 指定展示结果的最低百分比(低于的不显示)。 |
209| -s&nbsp;/&nbsp;--call-stack | 指定该选项后结果数据中会包含详细的调用栈信息。 |
210| --call-stack-limit-percent&nbsp;&lt;_number_&gt; | 调用栈的百分比最小值(低于的不显示)。 |
211| --proto | 将输入的&nbsp;perf.data&nbsp;文件转换为&nbsp;proto&nbsp;格式,默认文件名为&nbsp;perf.proto。 |
212| --json | 将输入的&nbsp;perf.data&nbsp;文件转换为&nbsp;json&nbsp;格式,默认文件名为&nbsp;perf.json。 |
213| --branch | 按分支预测的结果地址来展示报告,而不是调用栈的IP地址。 |
214| --&lt;_keys_&gt;&nbsp;&lt;_keyname1_&gt;[,_keyname2_][,...] | 按给出的关键字来过滤展示报告,keys支持:comms、pids、tids等&nbsp;例如&nbsp;--comms&nbsp;hiperf,hilog&nbsp;表示仅显示进程/线程名称为&nbsp;hiperf或者hilog的记录条目。 |
215| --sort&nbsp;&lt;_key1_&gt;[,_key2_][,...] | 按照给出的关键字来排序和显示,支持:pid、tid、comm等,可以指定多个关键字。 |
216| -i&nbsp;&lt;_filename_&gt; | 指定输入的采样数据(默认为perf.data)。 |
217| -o&nbsp;&lt;_filename_&gt; | 指定输出的报告文件名。 |
218
219
220## 使用示例
221
222- 输出采样数据的报告,默认读取perf.data文件
223  ```
224  hiperf report
225  ```
226
227  输出结果举例:
228
229  ```
230  Heating count    comm            pid  tid  dso                                func
231   5.68%  15073949 hiperf_example_ 1085 1091 /system/lib/ld-musl-arm.so.1       malloc
232   2.57%   6834119 hiperf_example_ 1085 1091 [kernel.kallsyms]                  vector_swi
233   2.27%   6013910 hiperf_example_ 1085 1087 /system/lib/ld-musl-arm.so.1       malloc
234   2.19%   5805738 hiperf_example_ 1085 1091 /system/lib/ld-musl-arm.so.1       vfprintf
235   2.09%   5543362 hiperf_example_ 1085 1091 [kernel.kallsyms]                  ktime_get_ts64
236  report done
237  ```
238
239- 输出采集数据的调用栈报告
240  ```
241  hiperf report -s
242  ```
243
244- 指定符号表路径
245  ```
246  hiperf report -s --symbol-dir /data/local/tmp
247  ```
248
249- 指定结果过滤的关键字,指定后会只显示包含该关键字的信息。比如过滤libutils.z.so则命令如下:
250  ```
251  hiperf report --dsos libuitls.z.so
252  ```
253
254- 指定按照特定的关键字进行排序展示,比如按照dso进行排序:
255  ```
256  hiperf report --sort dso
257  ```
258
259