1# hitrace 2 3HiTrace提供业务流程调用链跟踪的维测接口,帮助开发者迅速获取指定业务流程调用链的运行日志,定位跨设备、跨进程、跨线程的故障问题。 4 5## 环境要求 6 7- 根据hdc命令行工具指导,完成[环境准备](hdc.md#环境准备)。 8- 确保设备已正常连接。 9 10## 命令行说明 11 12| 命令 | 含义说明 | 13| --------------------- | ------------------------------------------------------------ | 14| -h | 帮助命令。 | 15| -l | 查看tag列表。 | 16| --trace_begin | 开始捕获trace。 | 17| --trace_finish | 结束捕获trace。 | 18| --trace_dump | 导出trace信息。 | 19| -b N | 设置用于存储和读取trace的缓冲区大小(buffer size KB)。默认的缓冲区大小为2048 KB。 | 20| -t N | 设置hitrace运行时长,单位为秒(默认为5秒)。 | 21| -o | 指定目标文件的名称(默认为stdout)。 | 22| -z | 压缩捕获的跟踪。 | 23| --trace_clock | 设置向trace添加时间戳的时钟类型,可以是引导(默认)、全局、单声道、正常运行时间或性能。 | 24| --trace_finish_nodump | 停止捕获trace时,不打印trace信息。 | 25| --start_bgsrv | 开启快照模式trace捕获。 | 26| --dump_bgsrv | 触发快照模式trace输出到文件。 | 27| --stop_bgsrv | 关闭快照模式trace捕获。 | 28 29> **说明** 30> 31> 快照模式定义为固定trace标签的trace捕获,默认不落盘。 `--dump_bgsrv` 命令触发当前时刻的二进制格式trace转储,文件默认生成在 `/data/log/hitrace` 目录下,命名格式为`trace-YYMMDDHHmmSS@[BOOT_TIME].sys`。可以使用[HiSmartPerf](https://gitee.com/openharmony/developtools_smartperf_host)工具进行可视化trace分析。工具下载链接:[developtools_smartperf_host官方发行版](https://gitee.com/openharmony/developtools_smartperf_host/releases)。 32 33## 常用命令 34 35### 查看hitrace包含的tag 36 37```shell 38hitrace -l 39``` 40**使用样例:** 41 42```shell 43$ hitrace -l 442024/11/14 11:43:00 hitrace enter, running_state is SHOW_LIST_CATEGORY 45 tagName: description: 46 ability - Ability Manager 47 accesscontrol - Access Control Module 48 account - Account Manager 49 ace - ACE development framework 50 animation - Animation 51 app - APP Module 52 ark - ARK Module 53 bluetooth - communicatio bluetooth 54 cloud - Cloud subsystem tag 55 cloudfile - Cloud file system 56 commercial - Commercial version tag 57 commonlibrary - commonlibrary subsystem 58 daudio - Distributed Audio 59 dcamera - Distributed Camera 60 deviceauth - Device Auth 61 devicemanager - Device Manager 62 deviceprofile - Device Profile 63 dhfwk - Distributed Hardware FWK 64 dinput - Distributed Input 65 disk - Disk I/O 66istributeddatamgr - Distributed Data Manager 67 dlpcre - Dlp Credential Service 68 drm - Digital Rights Management 69 dsched - Distributed Schedule 70 dscreen - Distributed Screen 71 dslm - device security level 72 dsoftbus - Distributed Softbus 73 ffrt - ffrt tasks 74 filemanagement - filemanagement 75 freq - CPU Frequency 76 graphic - Graphic Module 77 gresource - Global Resource Manager 78 hdcd - hdcd 79 hdf - hdf subsystem 80 huks - Universal KeyStore 81 i2c - I2C Events 82 idle - CPU Idle 83 interconn - Interconnection subsystem 84 ipa - thermal power allocator 85 irq - IRQ Events 86 irqoff - IRQ-disabled code section tracing 87 load - CPU Load 88 mdfs - Mobile Distributed File System 89 membus - Memory Bus Utilization 90 memory - Memory 91 memreclaim - Kernel Memory Reclaim 92 misc - Misc Module 93 mmc - eMMC commands 94 msdp - Multimodal Sensor Data Platform 95 multimodalinput - HITRACE_TAG_MULTIMODALINPUT 96 net - net 97 notification - Notification Module 98 nweb - NWEB Module 99 ohos - OpenHarmony 100 pagecache - Page cache 101 power - Power Manager 102 preemptoff - Preempt-disabled code section tracing 103 push - Push subsystem 104 regulators - Voltage and Current Regulators 105 rpc - RPC and IPC 106 samgr - samgr 107 sched - CPU Scheduling 108 security - Security subsystem 109 sensors - Sensors Module 110 sync - Synchronization 111 ufs - UFS commands 112 usb - usb subsystem 113 useriam - useriam 114 virse - Virtualization Service 115 window - Window Manager 116 workq - Kernel Workqueues 117 zaudio - OpenHarmony Audio Module 118 zcamera - OpenHarmony Camera Module 119 zimage - OpenHarmony Image Module 120 zmedia - OpenHarmony Media Module 121``` 122 123### 开始捕获指定tag的trace 124 125```shell 126hitrace --trace_begin --record app 127``` 128**使用样例:** 129 130```shell 131$ hitrace --trace_begin --record app 1322024/11/14 11:48:45 hitrace enter, running_state is RECORDING_LONG_BEGIN_RECORD 1332024/11/14 11:48:45 args: tags:app bufferSize:18432 overwrite:1 1342024/11/14 11:48:45 OpenRecording done. 135``` 136 137### 结束捕获trace 138 139默认在命令行窗口打印trace信息。 140 141```shell 142hitrace --trace_finish --record 143``` 144**使用样例1:** 145 146```shell 147$ hitrace --trace_finish --record 1482024/11/14 11:50:33 hitrace enter, running_state is RECORDING_LONG_FINISH_RECORD 1492024/11/14 11:50:33 capture done, output files: 150 /data/log/hitrace/record_trace_20241114115033@3010728-656499531.sys 151``` 152添加输出路径,trace信息将导出到对应文件。 153 154```shell 155hitrace --trace_finish -o /data/local/tmp/test.ftrace 156``` 157**使用样例2:** 158 159```shell 160$ hitrace --trace_finish -o /data/local/tmp/test.ftrace 1612024/11/14 11:50:33 start to read trace. 1622024/11/14 11:50:33 trace read done, output: /data/local/tmp/test.ftrace 163``` 164 165### 设置捕获trace的相关参数 166 167```shell 168hitrace -b 10240 -t 10 -o /data/local/tmp/test2.ftrace app ability 169``` 170**使用样例:** 171 172```shell 173$ hitrace -b 10240 -t 10 -o /data/local/tmp/test2.ftrace app ability 1742024/11/14 11:52:13 start capture, please wait 10s ... 1752024/11/14 11:52:23 capture done, start to read trace. 1762024/11/14 11:52:23 trace read done, output: /data/local/tmp/test2.ftrace 177``` 178- 设置缓冲区大小为10240KB。 179- 设置捕获trace时间为10s。 180- 设置捕获的trace信息存储在文件/data/local/tmp/test2.ftrace中。 181- 开启app、ability两个tag的trace捕获。 182 183### 导出trace信息 184 185默认将信息显示在命令行窗口。 186 187```shell 188hitrace --trace_dump 189``` 190**使用样例1:** 191 192```shell 193$ hitrace --trace_dump 1942024/11/14 11:54:23 start to read trace. 195# tracer: nop 196# 197# entries-in-buffer/entries-written: 2/2 #P:4 198# 199# _-----=> irqs-off 200# / _----=> need-resched 201# | / _---=> hardirq/softirq 202# || / _--=> preempt-depth 203# ||| / delay 204# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION 205# | | | | |||| | | 206 <...>-21829 ( 19280) [003] .... 3011033.731844: tracing_mark_write: trace_event_clock_sync: realtime_ts=1732002022239 207 <...>-21829 ( 19280) [003] .... 3011033.731865: tracing_mark_write: trace_event_clock_sync: parent_ts=3011033.750000 208# 209``` 210添加输出路径,trace信息将导出至对应的文件中。 211 212```shell 213hitrace --trace_dump -o /data/local/tmp/test3.ftrace 214``` 215**使用样例2:** 216 217```shell 218$ hitrace --trace_dump -o /data/local/tmp/test3.ftrace 2192024/11/14 11:54:23 start to read trace. 2202024/11/14 11:54:23 trace read done, output: /data/local/tmp/test3.ftrace 221``` 222也可根据关键词打印trace信息,使用命令`hitrace --trace_dump | grep xxx`进行过滤显示。 223 224### 开启快照模式trace捕获 225 226```shell 227hitrace --start_bgsrv 228``` 229**使用样例:** 230 231```shell 232$ hitrace --start_bgsrv 2332024/11/14 11:55:53 hitrace enter, running_state is SNAPSHOT_START 2342024/11/14 11:55:54 OpenSnapshot done. 235``` 236 237### 快照模式导出trace 238 239默认将二进制格式的trace信息保存到`/data/log/hitrace/`文件夹下,文件命名格式为`trace-YYMMDDHHmmSS@[BOOT_TIME].sys`。可以使用[HiSmartPerf](https://gitee.com/openharmony/developtools_smartperf_host)工具进行可视化trace分析。工具下载链接:[developtools_smartperf_host官方发行版](https://gitee.com/openharmony/developtools_smartperf_host/releases)。 240 241```shell 242hitrace --dump_bgsrv 243``` 244**使用样例:** 245 246```shell 247$ hitrace --dump_bgsrv 2482024/11/14 12:12:56 hitrace enter, running_state is SNAPSHOT_DUMP 2492024/11/14 12:12:57 DumpSnapshot done, output: 250 /data/log/hitrace/record_trace_20241114121257@2566589-103807063.sys 251``` 252 253### 关闭快照模式trace捕获 254 255```shell 256hitrace --stop_bgsrv 257``` 258**使用样例:** 259 260```shell 261$ hitrace --stop_bgsrv 2622024/11/14 11:59:43 hitrace enter, running_state is SNAPSHOT_STOP 2632024/11/14 11:59:43 CloseSnapshot done. 264``` 265 266### 捕获trace后进行压缩 267 268```shell 269hitrace -z -b 102400 -t 10 sched freq idle disk -o /data/local/tmp/test.ftrace 270``` 271**使用样例:** 272 273```shell 274$ hitrace -z -b 102400 -t 10 sched freq idle disk -o /data/local/tmp/test.ftrace 2752024/11/14 12:00:18 start capture, please wait 10s ... 2762024/11/14 12:00:28 capture done, start to read trace. 2772024/11/14 12:00:29 trace read done, output: /data/local/tmp/test.ftrace 278``` 279 280### 设置trace的输出时钟为boot(设备系统时间) 281 282```shell 283hitrace --trace_clock boot -b 102400 -t 10 sched freq idle disk -o /data/local/tmp/test.ftrace 284``` 285**使用样例:** 286 287```shell 288$ hitrace --trace_clock boot -b 102400 -t 10 sched freq idle disk -o /data/local/tmp/test.ftrace 2892024/11/14 12:01:42 start capture, please wait 10s ... 2902024/11/14 12:01:52 capture done, start to read trace. 2912024/11/14 12:01:52 trace read done, output: /data/local/tmp/test.ftrace 292``` 293 294### 结束捕获trace,取消trace信息打印在命令行窗口 295 296默认将trace信息保存到`/data/log/hitrace/`目录。 297 298```shell 299hitrace --trace_finish_nodump 300``` 301**使用样例:** 302 303```shell 304$ hitrace --trace_finish_nodump 3052024/11/14 12:03:07 hitrace enter, running_state is RECORDING_LONG_FINISH_NODUMP 3062024/11/14 12:03:07 end capture trace. 307``` 308