1e41f4b71Sopenharmony_ci# trace.h
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci## Overview
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ciDefines APIs of the HiTraceMeter module for performance trace.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ciExample
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ciSynchronous time slice trace event:
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci  
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ciOH_HiTrace_StartTrace("hitraceTest");
15e41f4b71Sopenharmony_ciOH_HiTrace_FinishTrace();
16e41f4b71Sopenharmony_ci```
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ciOutput:
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci  
21e41f4b71Sopenharmony_ci```
22e41f4b71Sopenharmony_ci<...>-1668 (----—) [003] .... 135.059377: tracing_mark_write: B|1668|H:hitraceTest
23e41f4b71Sopenharmony_ci<...>-1668 (----—) [003] .... 135.059415: tracing_mark_write: E|1668|
24e41f4b71Sopenharmony_ci```
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ciAsynchronous time slice trace event:
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci  
29e41f4b71Sopenharmony_ci```
30e41f4b71Sopenharmony_ciOH_HiTrace_StartAsyncTrace("hitraceTest", 123);
31e41f4b71Sopenharmony_ciOH_HiTrace_FinishAsyncTrace("hitraceTest", 123);
32e41f4b71Sopenharmony_ci```
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ciOutput:
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci  
37e41f4b71Sopenharmony_ci```
38e41f4b71Sopenharmony_ci<...>-2477 (----—) [001] .... 396.427165: tracing_mark_write: S|2477|H:hitraceTest 123
39e41f4b71Sopenharmony_ci<...>-2477 (----—) [001] .... 396.427196: tracing_mark_write: F|2477|H:hitraceTest 123
40e41f4b71Sopenharmony_ci```
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ciInteger value trace event:
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci  
45e41f4b71Sopenharmony_ci```
46e41f4b71Sopenharmony_ciOH_HiTrace_CountTrace("hitraceTest", 500);
47e41f4b71Sopenharmony_ci```
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ciOutput:
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci  
52e41f4b71Sopenharmony_ci```
53e41f4b71Sopenharmony_ci<...>-2638 (----—) [002] .... 458.904382: tracing_mark_write: C|2638|H:hitraceTest 500
54e41f4b71Sopenharmony_ci```
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci**Since**
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci10
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci**Related Modules**
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci[Hitrace](_hitrace.md)
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci## Summary
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci### Structs
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci| Name | Description | 
70e41f4b71Sopenharmony_ci| -------- | -------- |
71e41f4b71Sopenharmony_ci| struct&nbsp;&nbsp;[HiTraceId](_hi_trace_id.md) | Defines the **HiTraceId** instance. | 
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci### Types
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci| Name | Description | 
76e41f4b71Sopenharmony_ci| -------- | -------- |
77e41f4b71Sopenharmony_ci| typedef enum [HiTraceId_Valid](_hitrace.md#hitraceid_valid) [HiTraceId_Valid](_hitrace.md#hitraceid_valid) | Defines an enum for whether a **HiTraceId** instance is valid. | 
78e41f4b71Sopenharmony_ci| typedef enum [HiTrace_Version](_hitrace.md#hitrace_version) [HiTrace_Version](_hitrace.md#hitrace_version) | Defines an enum for the version number of **HiTrace**. | 
79e41f4b71Sopenharmony_ci| typedef enum [HiTrace_Flag](_hitrace.md#hitrace_flag) [HiTrace_Flag](_hitrace.md#hitrace_flag) | Defines an enum for the flag of **HiTrace**. | 
80e41f4b71Sopenharmony_ci| typedef enum [HiTrace_Tracepoint_Type](_hitrace.md#hitrace_tracepoint_type) [HiTrace_Tracepoint_Type](_hitrace.md#hitrace_tracepoint_type) | Defines an enum for the tracepoint type of **HiTrace**. | 
81e41f4b71Sopenharmony_ci| typedef enum [HiTrace_Communication_Mode](_hitrace.md#hitrace_communication_mode) [HiTrace_Communication_Mode](_hitrace.md#hitrace_communication_mode) |  | 
82e41f4b71Sopenharmony_ci|  typedef struct [HiTraceId](_hi_trace_id.md)**HiTraceId** |  | 
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci### Enums
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci| Name | Description | 
88e41f4b71Sopenharmony_ci| -------- | -------- |
89e41f4b71Sopenharmony_ci| [HiTraceId_Valid](_hitrace.md#hitraceid_valid) { [HITRACE_ID_INVALID](_hitrace.md) = 0, [HITRACE_ID_VALID](_hitrace.md) = 1 } | Enumerates whether a **HiTraceId** instance is valid. | 
90e41f4b71Sopenharmony_ci| [HiTrace_Version](_hitrace.md#hitrace_version) { [HITRACE_VER_1](_hitrace.md) = 0 } | Enumerates the version numbers of **HiTrace**. | 
91e41f4b71Sopenharmony_ci| [HiTrace_Flag](_hitrace.md#hitrace_flag) {<br>[HITRACE_FLAG_DEFAULT](_hitrace.md) = 0, [HITRACE_FLAG_INCLUDE_ASYNC](_hitrace.md) = 1 &lt;&lt; 0, [HITRACE_FLAG_DONOT_CREATE_SPAN](_hitrace.md) = 1 &lt;&lt; 1, [HITRACE_FLAG_TP_INFO](_hitrace.md) = 1 &lt;&lt; 2,<br>[HITRACE_FLAG_NO_BE_INFO](_hitrace.md) = 1 &lt;&lt; 3, [HITRACE_FLAG_DONOT_ENABLE_LOG](_hitrace.md) = 1 &lt;&lt; 4, [HITRACE_FLAG_FAULT_TRIGGER](_hitrace.md) = 1 &lt;&lt; 5, [HITRACE_FLAG_D2D_TP_INFO](_hitrace.md) = 1 &lt;&lt; 6<br>} | Defines an enum for the flag of **HiTrace**. | 
92e41f4b71Sopenharmony_ci| [HiTrace_Tracepoint_Type](_hitrace.md#hitrace_tracepoint_type) {<br>[HITRACE_TP_CS](_hitrace.md) = 0, [HITRACE_TP_CR](_hitrace.md) = 1, [HITRACE_TP_SS](_hitrace.md) = 2, [HITRACE_TP_SR](_hitrace.md) = 3,<br>[HITRACE_TP_GENERAL](_hitrace.md) = 4<br>} | Enumerates the tracepoint types of **HiTrace**. | 
93e41f4b71Sopenharmony_ci| [HiTrace_Communication_Mode](_hitrace.md#hitrace_communication_mode) { [HITRACE_CM_DEFAULT](_hitrace.md) = 0, [HITRACE_CM_THREAD](_hitrace.md) = 1, [HITRACE_CM_PROCESS](_hitrace.md) = 2, [HITRACE_CM_DEVICE](_hitrace.md) = 3 } |  | 
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci### Functions
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci| Name | Description | 
99e41f4b71Sopenharmony_ci| -------- | -------- |
100e41f4b71Sopenharmony_ci| [HiTraceId](_hi_trace_id.md) [OH_HiTrace_BeginChain](_hitrace.md#oh_hitrace_beginchain) (const char \*name, int flags) | Starts tracing a process. | 
101e41f4b71Sopenharmony_ci| void [OH_HiTrace_EndChain](_hitrace.md#oh_hitrace_endchain) () | Stops tracing the process and clears the trace ID of the calling thread if the given trace ID is valid. Otherwise, no operation is performed. | 
102e41f4b71Sopenharmony_ci| [HiTraceId](_hi_trace_id.md) [OH_HiTrace_GetId](_hitrace.md#oh_hitrace_getid) () | Obtains the trace ID of the calling thread. If the calling thread does not have a trace ID, an invalid trace ID is returned. | 
103e41f4b71Sopenharmony_ci| void [OH_HiTrace_SetId](_hitrace.md#oh_hitrace_setid) (const [HiTraceId](_hi_trace_id.md) \*id) | Sets the trace ID of the calling thread. If the ID is invalid, no operation is performed. | 
104e41f4b71Sopenharmony_ci| void [OH_HiTrace_ClearId](_hitrace.md#oh_hitrace_clearid) (void) | Clears the trace ID of the calling thread and invalidates it. | 
105e41f4b71Sopenharmony_ci| [HiTraceId](_hi_trace_id.md) [OH_HiTrace_CreateSpan](_hitrace.md#oh_hitrace_createspan) (void) | Creates a span ID based on the trace ID of the calling thread. | 
106e41f4b71Sopenharmony_ci| void [OH_HiTrace_Tracepoint](_hitrace.md#oh_hitrace_tracepoint) ([HiTrace_Communication_Mode](_hitrace.md#hitrace_communication_mode) mode, [HiTrace_Tracepoint_Type](_hitrace.md#hitrace_tracepoint_type) type, const [HiTraceId](_hi_trace_id.md) \*id, const char \*fmt,...) | Prints HiTrace information, including the trace ID. | 
107e41f4b71Sopenharmony_ci| void [OH_HiTrace_InitId](_hitrace.md#oh_hitrace_initid) ([HiTraceId](_hi_trace_id.md) \*id) | Initializes a **HiTraceId** instance. | 
108e41f4b71Sopenharmony_ci| void [OH_HiTrace_IdFromBytes](_hitrace.md#oh_hitrace_idfrombytes) ([HiTraceId](_hi_trace_id.md) \*id, const uint8_t \*pIdArray, int len) | Creates a **HiTraceId** instance based on a byte array. | 
109e41f4b71Sopenharmony_ci| bool [OH_HiTrace_IsIdValid](_hitrace.md#oh_hitrace_isidvalid) (const [HiTraceId](_hi_trace_id.md) \*id) | Checks whether a **HiTraceId** instance is valid. | 
110e41f4b71Sopenharmony_ci| bool [OH_HiTrace_IsFlagEnabled](_hitrace.md#oh_hitrace_isflagenabled) (const [HiTraceId](_hi_trace_id.md) \*id, [HiTrace_Flag](_hitrace.md#hitrace_flag) flag) | Checks whether the specified trace flag is enabled in a **HiTraceId** instance. | 
111e41f4b71Sopenharmony_ci| void [OH_HiTrace_EnableFlag](_hitrace.md#oh_hitrace_enableflag) (const [HiTraceId](_hi_trace_id.md) \*id, [HiTrace_Flag](_hitrace.md#hitrace_flag) flag) | Enables the specified trace flag in a **HiTraceId** instance. | 
112e41f4b71Sopenharmony_ci| int [OH_HiTrace_GetFlags](_hitrace.md#oh_hitrace_getflags) (const [HiTraceId](_hi_trace_id.md) \*id) | Obtains the trace flags in a **HiTraceId** instance. | 
113e41f4b71Sopenharmony_ci| void [OH_HiTrace_SetFlags](_hitrace.md#oh_hitrace_setflags) ([HiTraceId](_hi_trace_id.md) \*id, int flags) | Sets trace flags in a **HiTraceId** instance. | 
114e41f4b71Sopenharmony_ci| uint64_t [OH_HiTrace_GetChainId](_hitrace.md#oh_hitrace_getchainid) (const [HiTraceId](_hi_trace_id.md) \*id) | Obtains a trace chain ID. | 
115e41f4b71Sopenharmony_ci| void [OH_HiTrace_SetChainId](_hitrace.md#oh_hitrace_setchainid) ([HiTraceId](_hi_trace_id.md) \*id, uint64_t chainId) | Sets the trace chain ID in a **HiTraceId** instance. | 
116e41f4b71Sopenharmony_ci| uint64_t [OH_HiTrace_GetSpanId](_hitrace.md#oh_hitrace_getspanid) (const [HiTraceId](_hi_trace_id.md) \*id) | Obtains the span ID in a **HiTraceId** instance. | 
117e41f4b71Sopenharmony_ci| void [OH_HiTrace_SetSpanId](_hitrace.md#oh_hitrace_setspanid) ([HiTraceId](_hi_trace_id.md) \*id, uint64_t spanId) | Sets the span ID in a **HiTraceId** instance. | 
118e41f4b71Sopenharmony_ci| uint64_t [OH_HiTrace_GetParentSpanId](_hitrace.md#oh_hitrace_getparentspanid) (const [HiTraceId](_hi_trace_id.md) \*id) | Obtains the parent span ID in a **HiTraceId** instance. | 
119e41f4b71Sopenharmony_ci| void [OH_HiTrace_SetParentSpanId](_hitrace.md#oh_hitrace_setparentspanid) ([HiTraceId](_hi_trace_id.md) \*id, uint64_t parentSpanId) | Sets the **ParentSpanId** in a **HiTraceId** instance. | 
120e41f4b71Sopenharmony_ci| int [OH_HiTrace_IdToBytes](_hitrace.md#oh_hitrace_idtobytes) (const [HiTraceId](_hi_trace_id.md) \*id, uint8_t \*pIdArray, int len) | Converts a **HiTraceId** instance into a byte array for caching or transfer. | 
121e41f4b71Sopenharmony_ci| void [OH_HiTrace_StartTrace](_hitrace.md#oh_hitrace_starttrace) (const char \*name) | Marks the start of a synchronous trace. | 
122e41f4b71Sopenharmony_ci| void [OH_HiTrace_FinishTrace](_hitrace.md#oh_hitrace_finishtrace) (void) | Marks the end of a synchronous trace. | 
123e41f4b71Sopenharmony_ci| void [OH_HiTrace_StartAsyncTrace](_hitrace.md#oh_hitrace_startasynctrace) (const char \*name, int32_t taskId) | Marks the start of an asynchronous trace. | 
124e41f4b71Sopenharmony_ci| void [OH_HiTrace_FinishAsyncTrace](_hitrace.md#oh_hitrace_finishasynctrace) (const char \*name, int32_t taskId) | Marks the end of an asynchronous trace. | 
125e41f4b71Sopenharmony_ci| void [OH_HiTrace_CountTrace](_hitrace.md#oh_hitrace_counttrace) (const char \*name, int64_t count) | Traces the value change of an integer variable based on its name. | 
126