1e41f4b71Sopenharmony_ci# HiTraceMeter
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Introduction
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciHiTraceMeter is the OpenHarmony subsystem that provides APIs to implement call chain trace throughout a service process. With HiTraceMeter, you can quickly obtain the run log specific to the call chain of a service process and locate faults in inter-device, inter-process, or inter-thread communications. HiTraceMeter supports event logging in user mode and can collect trace data in user mode and kernel mode for performance trace and analysis.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Basic Concepts
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciThe HiTraceMeter subsystem consists of three parts:
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci- JS/C++ HiTraceMeter APIs for application logging
12e41f4b71Sopenharmony_ci- hitrace CLI tool for data collection
13e41f4b71Sopenharmony_ci- smartperf tool for graphical data analysis 
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ciWherein, HiTraceMeter APIs and the hitrace CLI tool run on the device side, and the smartperf tool runs on the PC side. HiTraceMeter APIs are provided in C++ and JS for event logging, which aims to generate the trace data necessary for performance trace and analysis during the development process.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciThe hitrace CLI tool is used to collect trace data. It captures trace data flows and saves the data as a text file.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciThe smartperf tool allows you to perform data analysis manually or use the analysis script for automated data analysis. If you want to get the data analysis done automatically, you need to supply the data file generated by the hitrace CLI tool as the input for the smartperf tool.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci  Traces data is classified by trace tag or trace category. Generally, one device subsystem corresponds to one tag. The tag is passed as the **Tag** parameter to event logging APIs. When you use the hitrace CLI tool to collect trace data, only the trace data specified by the **Tag** parameter is collected. Trace data of applications is fixedly labeled as **APP Tag**, and therefore, no **Tag** parameter needs to be passed to JS APIs. The following is a list of trace tags supported by HiTraceMeter. You can view the tags in [hitrace_meter.h](https://gitee.com/openharmony/hiviewdfx_hitrace/blob/master/interfaces/native/innerkits/include/hitrace_meter/hitrace_meter.h).
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci```cpp
24e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_NEVER = 0; // This tag is never enabled.
25e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_ALWAYS = (1ULL << 0); // This tag is always enabled.
26e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_DLP_CREDENTIAL = (1ULL << 21); // This tag is dlp credential service.
27e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_ACCESS_CONTROL = (1ULL << 22); // This tag is access control tag.
28e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_NET = (1ULL << 23); // Net tag.
29e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_NWEB = (1ULL << 24); // NWeb tag.
30e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_HUKS = (1ULL << 25); // This tag is huks.
31e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_USERIAM = (1ULL << 26); // This tag is useriam.
32e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_DISTRIBUTED_AUDIO = (1ULL << 27); // Distributed audio tag.
33e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_DLSM = (1ULL << 28); // device security level tag.
34e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_FILEMANAGEMENT = (1ULL << 29); // filemanagement tag.
35e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_OHOS = (1ULL << 30); // OHOS generic tag.
36e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_ABILITY_MANAGER = (1ULL << 31); // Ability Manager tag.
37e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_ZCAMERA = (1ULL << 32); // Camera module tag.
38e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_ZMEDIA = (1ULL << 33); // Media module tag.
39e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_ZIMAGE = (1ULL << 34); // Image module tag.
40e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_ZAUDIO = (1ULL << 35); // Audio module tag.
41e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_DISTRIBUTEDDATA = (1ULL << 36); // Distributeddata manager module tag.
42e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_MDFS = (1ULL << 37); // Mobile distributed file system tag.
43e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_GRAPHIC_AGP = (1ULL << 38); // Graphic module tag.
44e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_ACE = (1ULL << 39); // ACE development framework tag.
45e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_NOTIFICATION = (1ULL << 40); // Notification module tag.
46e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_MISC = (1ULL << 41); // Notification module tag.
47e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_MULTIMODALINPUT = (1ULL << 42); // Multi modal module tag.
48e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_SENSORS = (1ULL << 43); // Sensors mudule tag.
49e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_MSDP = (1ULL << 44); // Multimodal Sensor Data Platform module tag.
50e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_DSOFTBUS = (1ULL << 45); // Distributed Softbus tag.
51e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag.
52e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_ARK = (1ULL << 47); // ARK tag.
53e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_WINDOW_MANAGER = (1ULL << 48); // window manager tag.
54e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_ACCOUNT_MANAGER = (1ULL << 49); // account manager tag.
55e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_DISTRIBUTED_SCREEN = (1ULL << 50); // Distributed screen tag.
56e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_DISTRIBUTED_CAMERA = (1ULL << 51); // Distributed camera tag.
57e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_DISTRIBUTED_HARDWARE_FWK = (1ULL << 52); // Distributed hardware fwk tag.
58e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_GLOBAL_RESMGR = (1ULL << 53); // Global resource manager tag.
59e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_DEVICE_MANAGER = (1ULL << 54); // Distributed hardware devicemanager tag.
60e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_SAMGR = (1ULL << 55); // SA tag.
61e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_POWER = (1ULL << 56); // power manager tag.
62e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_DISTRIBUTED_SCHEDULE = (1ULL << 57); // Distributed schedule tag.
63e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_DEVICE_PROFILE = (1ULL << 58); // device profile tag.
64e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_DISTRIBUTED_INPUT = (1ULL << 59); // Distributed input tag.
65e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_BLUETOOTH = (1ULL << 60); // bluetooth tag.
66e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_ACCESSIBILITY_MANAGER = (1ULL << 61); // accessibility manager tag.
67e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_APP = (1ULL << 62); // App tag.
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_LAST = HITRACE_TAG_APP;
70e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_NOT_READY = (1ULL << 63); // Reserved for initialization.
71e41f4b71Sopenharmony_ciconstexpr uint64_t HITRACE_TAG_VALID_MASK = ((HITRACE_TAG_LAST - 1) | HITRACE_TAG_LAST);
72e41f4b71Sopenharmony_ci```
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci## Implementation Principles
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ciHiTraceMeter provides the hitrace CLI tool for capturing trace data in user mode and kernel mode, and provides C++ (innerkits) and JS (kits) APIs for event logging in user mode. Through extending kernel's ftrace functionality, HiTraceMeter can use the trace_marker node of ftrace to write the data, which is written into the user space by event logging APIs, to the kernel buffer. The following figure shows the basic HiTraceMeter architecture.
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci![HiTraceMeter architecture](figures/HiTraceMeter.png)
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci## Constraints
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci- The implementation of HiTraceMeter functions and APIs depends on the ftrace functionality, a framework provided by the kernel. It enables developers to add more trace functions via plug-ins. Therefore, make sure that ftrace is enabled before you use HiTraceMeter.
87e41f4b71Sopenharmony_ci  For most Linux kernels, ftrace is enabled by default. For details, see the ftrace documentation you may obtain.
88e41f4b71Sopenharmony_ci- HiTraceMeter is available only for the mini system and standard system.
89e41f4b71Sopenharmony_ci  
90e41f4b71Sopenharmony_ci  
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci# HiTraceMeter Development
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ciHiTraceMeter development focuses on two parts: JS/C++ event logging APIs and the hitrace CLI tool.
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci## When to Use
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ciYou may encounter unexpected issues like app freezing during app development or need to view the code's call chain during code debugging. With the APIs provided by HiTraceMeter, you'll be able to trace the application delay and call chain to identify performance problems.
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci## Available APIs
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ciOnly C++ APIs are now open for system developers. If you're developing a JS app, skip this section. The following table describes the APIs applicable to the standard system. You can find the APIs in [hitrace_meter.h](https://gitee.com/openharmony/hiviewdfx_hitrace/blob/master/interfaces/native/innerkits/include/hitrace_meter/hitrace_meter.h).
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci**Table 1** Sync APIs
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci| Sync trace                                                                   | Function     | Parameter Description                                                                 |
109e41f4b71Sopenharmony_ci|:---------------------------------------------------------------------------- | --------- | --------------------------------------------------------------------- |
110e41f4b71Sopenharmony_ci| void StartTrace(uint64_t label, const std::string& value, float limit = -1); | Starts a synchronous trace.| **label**: trace category.<br>**value**: trace data that indicates the specific status, such as the memory size and queue length.|
111e41f4b71Sopenharmony_ci| void FinishTrace(uint64_t label);                                            | Stops a synchronous trace.| **label**: trace category.                                               |
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci**StartTrace** and **FinishTrace** must be used in pairs, and **FinishTrace** matches the latest **StartTrace**. The two APIs can be used in nested mode. The stack data structure is used for matching during trace data parsing. The **limit** parameter is used for flow control, and you are advised to use the default value.
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci**Table 2** Async APIs
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci| Async trace                                                                                       | Function     | Parameter Description                                                                                                |
118e41f4b71Sopenharmony_ci| ------------------------------------------------------------------------------------------------- | --------- | ---------------------------------------------------------------------------------------------------- |
119e41f4b71Sopenharmony_ci| void StartAsyncTrace(uint64_t label, const std::string& value, int32_t taskId, float limit = -1); | Starts an asynchronous trace.| **label**: trace category.<br>**value**: trace data that indicates the specific status, such as the memory size and queue length.<br>**taskId**: ID used to indicate the association of APIs in an asynchronous trace.|
120e41f4b71Sopenharmony_ci| void FinishAsyncTrace(uint64_t label, const std::string& value, int32_t taskId);                  | Stops an asynchronous trace.| **label**: trace category.<br>**value**: trace data that indicates the specific status, such as the memory size and queue length.<br>**taskId**: ID used to indicate the association of APIs in an asynchronous trace.|
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ciThe trace data of **StartAsyncTrace** and **FinishAsyncTrace** is matched based on **value** and **taskId**, and therefore, the two APIs can be used without a strict sequence. In C++ applications, asynchronous traces are seldom used.
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci**Table 3** Counter APIs
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci| Counter Trace                                                      | Function   | Parameter Description                                                          |
129e41f4b71Sopenharmony_ci| ------------------------------------------------------------------ | ------- | -------------------------------------------------------------- |
130e41f4b71Sopenharmony_ci| void CountTrace(uint64_t label, const std::string& name, int64_t); | Count trace.| **label**: trace category.<br>**name**: trace name displayed in the IDE.|
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci## How to Develop
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci1. Add the build dependencies to the build configuration file **base\hiviewdfx\hitrace\cmd\BUILD.gn**.
135e41f4b71Sopenharmony_ci   
136e41f4b71Sopenharmony_ci   ```
137e41f4b71Sopenharmony_ci   external_deps = [ "hitrace:hitrace_meter"]
138e41f4b71Sopenharmony_ci   ```
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci2. Add the header file dependencies.
141e41f4b71Sopenharmony_ci   
142e41f4b71Sopenharmony_ci   ```cpp
143e41f4b71Sopenharmony_ci   #include "hitrace_meter.h"// Header file for defining APIs
144e41f4b71Sopenharmony_ci   ```
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci3. When calling an API, pass the trace value as an input parameter. The trace tags currently supported are listed in **hitrace_meter.h**. Assume that the trace tag is **OHOS** and trace data of **func1** and **func2** needs to be captured. After the hitrace command is executed in the shell, the trace data is automatically captured. The captured data includes the function call process and the memory and time consumed during this process. You can use the data to analyze the call process to identify performance problems.
147e41f4b71Sopenharmony_ci   
148e41f4b71Sopenharmony_ci   ```cpp
149e41f4b71Sopenharmony_ci   #include "hitrace_meter.h" // Include hitrace_meter.h
150e41f4b71Sopenharmony_ci   using namespace std;
151e41f4b71Sopenharmony_ci   
152e41f4b71Sopenharmony_ci   int main()
153e41f4b71Sopenharmony_ci   {
154e41f4b71Sopenharmony_ci      uint64_t label = BYTRACE_TAG_OHOS;
155e41f4b71Sopenharmony_ci      sleep(1);
156e41f4b71Sopenharmony_ci      CountTrace(label, "count number", 2000);  // Integer trace
157e41f4b71Sopenharmony_ci   
158e41f4b71Sopenharmony_ci      StartTrace(label, "func1Trace", -1); // Trace start point of func1Start
159e41f4b71Sopenharmony_ci      sleep(1);
160e41f4b71Sopenharmony_ci      StartTrace(label, "func2Trace", -1);   // Trace start point of func2Start
161e41f4b71Sopenharmony_ci      sleep(2);
162e41f4b71Sopenharmony_ci      FinishTrace (label); // Trace end point of func2Trace
163e41f4b71Sopenharmony_ci      sleep(1);
164e41f4b71Sopenharmony_ci      FinishTrace(label);   // Trace end point of func1Trace
165e41f4b71Sopenharmony_ci   
166e41f4b71Sopenharmony_ci      StartAsyncTrace(label, "asyncTrace1", 1234); // Trace start point of asyncTrace1  
167e41f4b71Sopenharmony_ci      FinishAsyncTrace(label, "asyncTrace1", 1234); // Trace end point of asyncTrace1
168e41f4b71Sopenharmony_ci   
169e41f4b71Sopenharmony_ci      return 0;
170e41f4b71Sopenharmony_ci   } 
171e41f4b71Sopenharmony_ci   ```
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci4. On completion of HiTraceMeter building and deployment, start a trace. After you run the application in the shell on the device, trace data will be captured automatically.
174e41f4b71Sopenharmony_ci   
175e41f4b71Sopenharmony_ci   ```
176e41f4b71Sopenharmony_ci   hdc_std shell hitrace -t 10 ohos > .\myapp_demo.ftrace
177e41f4b71Sopenharmony_ci   ```
178e41f4b71Sopenharmony_ci   
179e41f4b71Sopenharmony_ci   You can open the captured data by clicking **Open trace file** in the smartperf tool or dragging the data to the graphics area. For details, see [smartperf](https://toscode.gitee.com/openharmony-sig/smartperf).
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci## Verification
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ciThe following is a demo debugging process, where the **StartTrace** and **FinishTrace** APIs are used in synchronization mode.
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ci1. Write the test code file [hitrace_example.cpp](https://gitee.com/openharmony/hiviewdfx_hitrace/blob/master/cmd/example/hitrace_example.cpp) by adding the **StartTrace** and **FinishTrace** APIs to the code.
186e41f4b71Sopenharmony_ci   
187e41f4b71Sopenharmony_ci   ```cpp
188e41f4b71Sopenharmony_ci   int main()
189e41f4b71Sopenharmony_ci   {
190e41f4b71Sopenharmony_ci       thread t1(ThreadFunc1);
191e41f4b71Sopenharmony_ci       t1.join();
192e41f4b71Sopenharmony_ci   
193e41f4b71Sopenharmony_ci       StartTrace(LABEL, "testStart");
194e41f4b71Sopenharmony_ci       sleep(SLEEP_ONE_SECOND);
195e41f4b71Sopenharmony_ci   
196e41f4b71Sopenharmony_ci       StartTrace(LABEL, "funcAStart", SLEEP_ONE_SECOND); // Trace start point
197e41f4b71Sopenharmony_ci       FuncA();
198e41f4b71Sopenharmony_ci       FinishTrace(LABEL);
199e41f4b71Sopenharmony_ci       sleep(SLEEP_TWO_SECOND);
200e41f4b71Sopenharmony_ci   
201e41f4b71Sopenharmony_ci       thread t2(ThreadFunc2);
202e41f4b71Sopenharmony_ci       t2.join();
203e41f4b71Sopenharmony_ci   
204e41f4b71Sopenharmony_ci       StartTrace(LABEL, "funcBStart", SLEEP_TWO_SECOND);
205e41f4b71Sopenharmony_ci       FuncB();
206e41f4b71Sopenharmony_ci       FinishTrace(LABEL);// Trace end point
207e41f4b71Sopenharmony_ci       sleep(SLEEP_TWO_SECOND);
208e41f4b71Sopenharmony_ci   
209e41f4b71Sopenharmony_ci       sleep(SLEEP_ONE_SECOND);
210e41f4b71Sopenharmony_ci       FinishTrace(LABEL);
211e41f4b71Sopenharmony_ci       FuncC();
212e41f4b71Sopenharmony_ci   
213e41f4b71Sopenharmony_ci       return 0;
214e41f4b71Sopenharmony_ci   }
215e41f4b71Sopenharmony_ci   ```
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci2. Modify the **base\hiviewdfx\hitrace\cmd\BUILD.gn** file, and start building.
218e41f4b71Sopenharmony_ci   
219e41f4b71Sopenharmony_ci   ```
220e41f4b71Sopenharmony_ci   ohos_executable("hitrace_example") {
221e41f4b71Sopenharmony_ci     sources = [ "example/hitrace_example.cpp" ]
222e41f4b71Sopenharmony_ci   
223e41f4b71Sopenharmony_ci     external_deps = [ "hitrace:hitrace_meter" ]
224e41f4b71Sopenharmony_ci   
225e41f4b71Sopenharmony_ci     subsystem_name = "hiviewdfx"
226e41f4b71Sopenharmony_ci     part_name = "hitrace_native"
227e41f4b71Sopenharmony_ci   }
228e41f4b71Sopenharmony_ci   
229e41f4b71Sopenharmony_ci   group("hitrace_target") {
230e41f4b71Sopenharmony_ci     deps = [
231e41f4b71Sopenharmony_ci       ":hitrace",
232e41f4b71Sopenharmony_ci       ":hitrace_example",
233e41f4b71Sopenharmony_ci     ]
234e41f4b71Sopenharmony_ci   }
235e41f4b71Sopenharmony_ci   ```
236e41f4b71Sopenharmony_ci
237e41f4b71Sopenharmony_ci3. Place the **hitrace_example** executable file in the **/system/bin** directory of the device, and run the following commands in sequence in the shell:
238e41f4b71Sopenharmony_ci   
239e41f4b71Sopenharmony_ci   ```shell
240e41f4b71Sopenharmony_ci   hitrace --trace_begin ohos
241e41f4b71Sopenharmony_ci   hitrace_exampe
242e41f4b71Sopenharmony_ci   hitrace --trace_dump
243e41f4b71Sopenharmony_ci   ```
244e41f4b71Sopenharmony_ci   
245e41f4b71Sopenharmony_ci    If the expected trace value is present in the trace data, the capture of trace data is successful. For example:
246e41f4b71Sopenharmony_ci   
247e41f4b71Sopenharmony_ci   ```
248e41f4b71Sopenharmony_ci   <...>-1651    (-------) [002] ....   327.194136: tracing_mark_write: S|1650|H:testAsync 111
249e41f4b71Sopenharmony_ci   <...>-1650    (-------) [001] ....   332.197640: tracing_mark_write: B|1650|H:testStart
250e41f4b71Sopenharmony_ci   <...>-1650    (-------) [001] ....   333.198018: tracing_mark_write: B|1650|H:funcAStart
251e41f4b71Sopenharmony_ci   <...>-1650    (-------) [001] ....   334.198507: tracing_mark_write: E|1650|
252e41f4b71Sopenharmony_ci   <...>-1654    (-------) [003] ....   341.201673: tracing_mark_write: F|1650|H:testAsync 111
253e41f4b71Sopenharmony_ci   <...>-1650    (-------) [001] ....   341.202168: tracing_mark_write: B|1650|H:funcBStart
254e41f4b71Sopenharmony_ci   <...>-1650    (-------) [001] ....   343.202557: tracing_mark_write: E|1650|
255e41f4b71Sopenharmony_ci   <...>-1650    (-------) [001] ....   346.203178: tracing_mark_write: E|1650|
256e41f4b71Sopenharmony_ci   <...>-1650    (-------) [001] ....   346.203457: tracing_mark_write: C|1650|H:count number 1
257e41f4b71Sopenharmony_ci   <...>-1650    (-------) [001] ....   347.203818: tracing_mark_write: C|1650|H:count number 2
258e41f4b71Sopenharmony_ci   <...>-1650    (-------) [001] ....   348.204207: tracing_mark_write: C|1650|H:count number 3
259e41f4b71Sopenharmony_ci   <...>-1650    (-------) [001] ....   349.204473: tracing_mark_write: C|1650|H:count number 4
260e41f4b71Sopenharmony_ci   <...>-1650    (-------) [001] ....   350.204851: tracing_mark_write: C|1650|H:count number 5
261e41f4b71Sopenharmony_ci   <...>-1655    (-------) [001] ....   365.944658: tracing_mark_write: trace_event_clock_sync: realtime_ts=1502021460925
262e41f4b71Sopenharmony_ci   <...>-1655    (-------) [001] ....   365.944686: tracing_mark_write: trace_event_clock_sync: parent_ts=365.944641
263e41f4b71Sopenharmony_ci   ```
264e41f4b71Sopenharmony_ci   
265e41f4b71Sopenharmony_ci   
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ci# Using the hitrace CLI Tool
268e41f4b71Sopenharmony_ci
269e41f4b71Sopenharmony_ciThe hitrace CLI tool is an executable binary program. On an OpenHarmony-powered device, you can run the following commands in the shell to capture kernel's running data.
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ci**Table 4** Command list
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ci| Option                        | Description                                              |
274e41f4b71Sopenharmony_ci| ----------------------------- | -------------------------------------------------------- |
275e41f4b71Sopenharmony_ci| -h, --help                    | Views the help Information.                                              |
276e41f4b71Sopenharmony_ci| -b *n*, --buffer_size *n*         | Sets the buffer size for trace data in KB. The default value is **2048**.                         |
277e41f4b71Sopenharmony_ci| -t *n*, --time *n*                | Sets the trace uptime in seconds, which depends on the time required for analysis.                       |
278e41f4b71Sopenharmony_ci| --trace_clock clock           | Sets the type of the clock for adding a timestamp to a trace. The value can be **boot** (default), **global**, **mono**, **uptime**, or **perf**.|
279e41f4b71Sopenharmony_ci| --trace_begin                 | Starts capturing trace data.                                                |
280e41f4b71Sopenharmony_ci| --trace_dump                  | Dumps trace data to the specified position. The default position is the console.                                       |
281e41f4b71Sopenharmony_ci| --trace_finish                | Stops capturing trace data and dumps trace data to the specified position. The default position is the console.                             |
282e41f4b71Sopenharmony_ci| --trace_finish_nodump         | Stops capturing trace packets and dumping trace data.                                |
283e41f4b71Sopenharmony_ci| -l, --list_categories         | Lists the trace categories supported by the device.                                         |
284e41f4b71Sopenharmony_ci| --overwrite                   | Sets the action to take when the buffer is full. If this option is used, the latest trace data is discarded. If this option is not used, the earliest trace data is discarded (default).                           |
285e41f4b71Sopenharmony_ci| -o *filename*, --output *filename*| Outputs trace data to the specified file.                                             |
286e41f4b71Sopenharmony_ci| -z                            | Compresses the trace data.                                            |
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ciExamples:
289e41f4b71Sopenharmony_ci
290e41f4b71Sopenharmony_ci- Query supported labels.
291e41f4b71Sopenharmony_ci  
292e41f4b71Sopenharmony_ci  ```
293e41f4b71Sopenharmony_ci  
294e41f4b71Sopenharmony_ci  hitrace -l
295e41f4b71Sopenharmony_ci  
296e41f4b71Sopenharmony_ci  ```
297e41f4b71Sopenharmony_ci  
298e41f4b71Sopenharmony_ci  Alternatively, run the following command:
299e41f4b71Sopenharmony_ci  
300e41f4b71Sopenharmony_ci  ```
301e41f4b71Sopenharmony_ci  
302e41f4b71Sopenharmony_ci  hitrace --list_categories
303e41f4b71Sopenharmony_ci  
304e41f4b71Sopenharmony_ci  ```
305e41f4b71Sopenharmony_ci
306e41f4b71Sopenharmony_ci- Trace ability information for 10 seconds and store the trace data in a buffer of 4 MB.
307e41f4b71Sopenharmony_ci  
308e41f4b71Sopenharmony_ci  ```
309e41f4b71Sopenharmony_ci  
310e41f4b71Sopenharmony_ci  hitrace -b 4096 -t 10 --overwrite ability > /data/log/mytrace.ftrace
311e41f4b71Sopenharmony_ci  
312e41f4b71Sopenharmony_ci  ```
313e41f4b71Sopenharmony_ci
314e41f4b71Sopenharmony_ci- Set the clock type to **mono**.
315e41f4b71Sopenharmony_ci  
316e41f4b71Sopenharmony_ci  ```
317e41f4b71Sopenharmony_ci  
318e41f4b71Sopenharmony_ci  hitrace --trace_clock mono  -b 4096 -t 10 --overwrite ability > /data/log/mytrace.ftrace
319e41f4b71Sopenharmony_ci  
320e41f4b71Sopenharmony_ci  ```
321e41f4b71Sopenharmony_ci
322e41f4b71Sopenharmony_ci- Compress the trace data.
323e41f4b71Sopenharmony_ci  
324e41f4b71Sopenharmony_ci  ```
325e41f4b71Sopenharmony_ci  
326e41f4b71Sopenharmony_ci  hitrace -z  -b 4096 -t 10 --overwrite ability > /data/log/mytrace.ftrace
327e41f4b71Sopenharmony_ci  
328e41f4b71Sopenharmony_ci  ```
329e41f4b71Sopenharmony_ci  
330e41f4b71Sopenharmony_ci  
331e41f4b71Sopenharmony_ci
332e41f4b71Sopenharmony_ci# FAQs
333e41f4b71Sopenharmony_ci
334e41f4b71Sopenharmony_ci### Incomplete or Empty Data Captured by hitrace
335e41f4b71Sopenharmony_ci
336e41f4b71Sopenharmony_ci#### Symptom
337e41f4b71Sopenharmony_ci
338e41f4b71Sopenharmony_ciThe data captured by running the hitrace command is incomplete or no data is captured.
339e41f4b71Sopenharmony_ci
340e41f4b71Sopenharmony_ci#### **Root Cause**
341e41f4b71Sopenharmony_ci
342e41f4b71Sopenharmony_ciThe value of **-t** or **-b** buffer is too small, leading to data loss.
343e41f4b71Sopenharmony_ci
344e41f4b71Sopenharmony_ci#### Solution
345e41f4b71Sopenharmony_ci
346e41f4b71Sopenharmony_ciYou can set **-t** to **60** and **-b** to **204800** to increase the trace time and buffer size.
347e41f4b71Sopenharmony_ci
348e41f4b71Sopenharmony_ci
349e41f4b71Sopenharmony_ci
350e41f4b71Sopenharmony_ci# Reference
351e41f4b71Sopenharmony_ci
352e41f4b71Sopenharmony_ciFor details about HiTraceMeter, see [hiviewdfx_hitrace: Lightweight Distributed Trace](https://gitee.com/openharmony/hiviewdfx_hitrace).
353