1e41f4b71Sopenharmony_ci# HiTraceChain Development
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciHiTraceChain tracks the call chain with the same **traceid** throughout the inter-device, inter-process, and inter-thread service processes. It associates and displays the call relationship and various output information during the entire process, helping you analyze and locate faults and optimize the system.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci## When to Use
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ciHiTraceChain can be used for the following purposes:
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci- Associates and reports service process information (such as logs and events) on the device.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci- Displays and analyzes reported information on the cloud to facilitate fault location.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci- Works with the IDE to debug the detailed service process and time consumption distribution for system optimization.
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci    **Figure 1** Use cases of HiTraceChain
19e41f4b71Sopenharmony_ci    ![](figures/use-cases-of-hitrace.png "use-cases-of-hitrace")
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci### Usage Example
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci  **Figure 2** Service calling process \(inter-device and inter-process synchronous call\) 
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci  ![](figures/service-calling-process-(inter-device-and-inter-process-synchronous-call).png "service-calling-process-(inter-device-and-inter-process-synchronous-call)")
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci1. Display the call relationship in the service process, analyze key paths and function dependency, and determine the time consumption and call frequency at each call point to detect performance bottlenecks.
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci     **Figure 3** Service calling process
31e41f4b71Sopenharmony_ci     ![](figures/service-calling-process.png "service-calling-process")
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci     **Figure 4** Time delay in the service calling process
35e41f4b71Sopenharmony_ci     ![](figures/time-delay-in-the-service-calling-process.png "time-delay-in-the-service-calling-process")
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci2. Add **traceid** to logs and events automatically to facilitate comprehensive analysis and quick fault location.
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci## Available APIs
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ciHiTraceChain provides C++ and C APIs. The upper-layer services mainly use HiTraceChain to start and stop call chain trace.
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ciHiTraceChain is implemented at layer C. It works by transferring **traceid** throughout the service calling process. Before service processing, HiTrace sets **traceid** in the thread local storage (TLS) of the calling thread. During service processing, HiTrace obtains **traceid** from the contextual TLS of the calling thread and automatically adds it to the log and event information. After service processing is complete, HiTrace clears **traceid** from the TLS of the calling thread.
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci### Java, C++, and C APIs
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci  **Table 1** Description of C++ and C APIs
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci|  | **C++** | **C** |
52e41f4b71Sopenharmony_ci| -------- | -------- | -------- |
53e41f4b71Sopenharmony_ci| **Class**| **Function**| **Function**|
54e41f4b71Sopenharmony_ci| HiTraceChain | HiTraceId Begin(const std::string& name, int flags) | HiTraceIdStruct HiTraceChainBegin(const char* name, int flags) |
55e41f4b71Sopenharmony_ci|  | void End(const HiTraceId& id) | void HiTraceChainEnd(const HiTraceIdStruct* pId) |
56e41f4b71Sopenharmony_ci|  | HiTraceId GetId(); | HiTraceIdStruct HiTraceChainGetId() |
57e41f4b71Sopenharmony_ci|  | void SetId(const HiTraceId& id) | void HiTraceChainSetId(const HiTraceIdStruct* pId) |
58e41f4b71Sopenharmony_ci|  | void ClearId() | void HiTraceChainClearId() |
59e41f4b71Sopenharmony_ci|  | HiTraceId CreateSpan() | HiTraceIdStruct HiTraceChainCreateSpan() |
60e41f4b71Sopenharmony_ci|  | void Tracepoint(HiTraceTracepointType type, const HiTraceId& id, const char* fmt, ...) | void HiTraceChainTracepoint(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt, ...) |
61e41f4b71Sopenharmony_ci|  | void Tracepoint(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceId& id, const char* fmt, ...) | void HiTraceChainTracepointEx(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt, ...) |
62e41f4b71Sopenharmony_ci| HiTraceId | HiTraceId(); | void HiTraceChainInitId(HiTraceIdStruct* pId) |
63e41f4b71Sopenharmony_ci|  | HiTraceId(const uint8_t* pIdArray, int len) | HiTraceIdStruct HiTraceChainBytesToId(const uint8_t* pIdArray, int len) |
64e41f4b71Sopenharmony_ci|  | bool IsValid() | int HiTraceChainIsValid(const HiTraceIdStruct* pId) |
65e41f4b71Sopenharmony_ci|  | bool IsFlagEnabled(HiTraceFlag flag) | int HiTraceChainIsFlagEnabled(const HiTraceIdStruct* pId, HiTraceFlag flag) |
66e41f4b71Sopenharmony_ci|  | void EnableFlag(HiTraceFlag flag) | void HiTraceChainEnableFlag(HiTraceIdStruct* pId, HiTraceFlag flag) |
67e41f4b71Sopenharmony_ci|  | int GetFlags() | int HiTraceChainGetFlags(const HiTraceIdStruct* pId) |
68e41f4b71Sopenharmony_ci|  | void SetFlags(int flags) | void HiTraceChainSetFlags(HiTraceIdStruct* pId, int flags) |
69e41f4b71Sopenharmony_ci|  | uint64_t GetChainId() | uint64_t HiTraceChainGetChainId(const HiTraceIdStruct* pId) |
70e41f4b71Sopenharmony_ci|  | void SetChainId(uint64_t chainId) | void HiTraceChainSetChainId(HiTraceIdStruct* pId, uint64_t chainId) |
71e41f4b71Sopenharmony_ci|  | uint64_t GetSpanId() | uint64_t HiTraceChainGetSpanId(const HiTraceIdStruct* pId) |
72e41f4b71Sopenharmony_ci|  | void SetSpanId(uint64_t spanId) | void HiTraceChainSetSpanId(HiTraceIdStruct* pId, uint64_t spanId) |
73e41f4b71Sopenharmony_ci|  | uint64_t GetParentSpanId() | uint64_t HiTraceChainGetParentSpanId(const HiTraceIdStruct* pId) |
74e41f4b71Sopenharmony_ci|  | void SetParentSpanId(uint64_t parentSpanId) | void HiTraceChainSetParentSpanId(HiTraceIdStruct* pId, uint64_t parentSpanId) |
75e41f4b71Sopenharmony_ci|  | int ToBytes(uint8_t* pIdArray, int len) | int HiTraceChainIdToBytes(const HiTraceIdStruct_ pId, uint8_t* pIdArray, int len) |
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci### Parameters of C++ APIs
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci  **Table 2** Trace flag combination types
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci| **Name**| **Value**| **Description**|
83e41f4b71Sopenharmony_ci| -------- | -------- | -------- |
84e41f4b71Sopenharmony_ci| HITRACE_FLAG_DEFAULT           | 0      | Default flag.      |
85e41f4b71Sopenharmony_ci| HITRACE_FLAG_INCLUDE_ASYNC | 1      | Asynchronous call flag. By default, only synchronous calls are traced. If this flag is set, both synchronous and asynchronous calls will be traced.  |
86e41f4b71Sopenharmony_ci| HITRACE_FLAG_DONOT_CREATE_SPAN | 1 << 1 | No span flag. By default, spans are created within a trace of synchronous and asynchronous service calls. If this flag is set, no spans are created.    |
87e41f4b71Sopenharmony_ci| HITRACE_FLAG_TP_INFO           | 1 << 2 | Trace point flag. By default, no trace point is added when trace is enabled. This flag is used for debugging. If this flag is set, trace points will be automatically added on the TX and RX sides of synchronous and asynchronous calls to output trace point and timestamp information. Trace points are classified into four types: client send (CS), server receive (SR), server send (SS), and client receive (CR). For a synchronous call, the output trace points are CS, SR, SS, and CR; for an asynchronous call, the output trace points are CS, SR, and SS.      |
88e41f4b71Sopenharmony_ci| HITRACE_FLAG_NO_BE_INFO        | 1 << 3 | No begin/end flag. By default, information about the start and end of the trace task is printed. If this flag is set, information about the start and end of the trace task will not be printed.|
89e41f4b71Sopenharmony_ci| HITRACE_FLAG_DONOT_ENABLE_LOG       | 1 << 4 | Log association flag. If this flag is set, information about the trace task will not be printed. |
90e41f4b71Sopenharmony_ci| HITRACE_FLAG_FAULT_TRIGGER   | 1 << 5 | Failure trigger flag. This flag is reserved for future use. |
91e41f4b71Sopenharmony_ci| HITRACE_FLAG_D2D_TP_INFO       | 1 << 6 | Device-to-device trace point flag. It is a subset of **TP_INFO**. If this flag is set, trace points are added only for call chain trace between devices.|
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci  **Table 3** Trace point types
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci| **Name**| **Value**| **Description**|
96e41f4b71Sopenharmony_ci| -------- | -------- | -------- |
97e41f4b71Sopenharmony_ci| HITRACE_TP_CS       | 0 | CS trace point.       |
98e41f4b71Sopenharmony_ci| HITRACE_TP_CR       | 1 | CR trace point.       |
99e41f4b71Sopenharmony_ci| HITRACE_TP_SS       | 2 | SS trace point.       |
100e41f4b71Sopenharmony_ci| HITRACE_TP_SR       | 3 | SR trace point.       |
101e41f4b71Sopenharmony_ci| HITRACE_TP_GENERAL  | 4 | General trace points except CS, CR, SS, and SR.|
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci  **Table 4** Communication modes
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci| **Name**| **Value**| **Description**|
106e41f4b71Sopenharmony_ci| -------- | -------- | -------- |
107e41f4b71Sopenharmony_ci| HITRACE_CM_DEFAULT  | 0 | Default communication mode.   |
108e41f4b71Sopenharmony_ci| HITRACE_CM_THREAD   | 1 | Inter-thread communication. |
109e41f4b71Sopenharmony_ci| HITRACE_CM_PROCESS  | 2 | Inter-process communication. |
110e41f4b71Sopenharmony_ci| HITRACE_CM_DEVICE   | 3 | Inter-device communication. |
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci  **Table 5** Description of C++ APIs
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci| **Class**| **API**| **Description**|
115e41f4b71Sopenharmony_ci| -------- | -------- | -------- |
116e41f4b71Sopenharmony_ci| HiTraceChain | HiTraceId Begin(const std::string&amp; name, int flags) | Starts HiTraceChain, generates a **HiTraceId** object, and sets it in the TLS of the calling thread.<br>Input arguments:<br>- **name**: name of the service process.<br>- **flags**: trace flags, which can be used in combination. For details, see Table 2.<br>Output arguments: none<br>Return value: a valid **HiTraceId** object if call chain trace is triggered successfully; returns an invalid object otherwise.<br>Note: In nested trace mode, an invalid object will be returned if trace is started at the nested layer.|
117e41f4b71Sopenharmony_ci|  | void End(const HiTraceId&amp; id) | Stops HiTraceChain based on the **HiTraceId** object returned by the **Begin** API, and clears the **HiTraceId** object in the TLS of the calling thread.<br>Input arguments:<br>- **id**: **HiTraceId** object.<br>Output arguments: none<br>Return value: none|
118e41f4b71Sopenharmony_ci|  | HiTraceId GetId(); | Obtains the **HiTraceId** object from the TLS of the calling thread.<br>Input arguments: none<br>Output arguments: none<br>Return value: **HiTraceId** object in the contextual TLS of the calling thread.|
119e41f4b71Sopenharmony_ci|  | void SetId(const HiTraceId&amp; id) | Purpose: Sets the **HiTraceId** object in the TLS of the calling thread.<br>Input arguments:<br>- **id**: **HiTraceId** object.<br>Output arguments: none<br>Return value: none|
120e41f4b71Sopenharmony_ci|  | void ClearId() | Clears the **HiTraceId** object in the TLS of the calling thread.<br>Input arguments: none<br>Output arguments: none<br>Return value: none|
121e41f4b71Sopenharmony_ci|  | HiTraceId CreateSpan() | Obtains the span ID from the current **HiTraceId** object.<br>Input arguments: none<br>Output arguments: none<br>Return value: current span ID.|
122e41f4b71Sopenharmony_ci|  | void Tracepoint(HiTraceTracepointType type, const HiTraceId&amp; id, const char* fmt, ...) | Outputs HiTraceChain trace point information based on the information type. The information includes the timestamp and **HiTraceId** object of the span.<br>Input arguments:<br>- **type**: trace point type. For details, see Table 3.<br>- **id**: ID of the current span.<br>- **fmt**: string describing the format variable parameter.<br>- **args**: variable parameter.<br>Output arguments: none<br>Return value: none|
123e41f4b71Sopenharmony_ci|  | void Tracepoint(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceId&amp; id, const char* fmt, ...) | Outputs HiTraceChain trace point information based on the communication mode and information type. The information includes the timestamp and **HiTraceId** object of the span.<br>Input arguments:<br>- **mode**: communication mode. For details, see Table 4.<br>- **type**: trace point type. For details, see Table 3.<br>- **id**: ID of the current span.<br>- **fmt**: string describing the format variable parameter.<br>- **args**: variable parameter.<br>Output arguments: none<br>Return value: none|
124e41f4b71Sopenharmony_ci| HiTraceId | HiTraceId(); | Represents the default constructor used to generate an invalid **HiTraceId** object. <br>Input arguments: none<br>Output arguments: none<br>Return value: none|
125e41f4b71Sopenharmony_ci|  | HiTraceId(const uint8_t* pIdArray, int len) | Represents the constructor used to create a **HiTraceId** object based on the specified byte array. <br>Input arguments:<br>- **pIdArray**: pointer to a byte array.<br>- **len**: length of the byte array.<br>Output arguments: none<br>Return value: none|
126e41f4b71Sopenharmony_ci|  | bool IsValid() | Checks whether the **HiTraceId** object is valid.<br>Input arguments: none<br>Output arguments: none<br>Return value: **true** if the **HiTraceId** object is valid; **false** otherwise.|
127e41f4b71Sopenharmony_ci|  | bool IsFlagEnabled(HiTraceFlag flag) | Checks whether the trace flag of the **HiTraceId** object is enabled.<br>Input arguments:<br>- **flag**: trace flag. For details, see the description in the **Begin** function.<br>Output arguments: none<br>Return value: **true** if the trace flag is enabled; **false** otherwise.|
128e41f4b71Sopenharmony_ci|  | void EnableFlag(HiTraceFlag flag) | Enables the trace flag of the **HiTraceId** object.<br>Input arguments:<br>- **flag**: trace flag. For details, see the description in the **Begin** function.<br>Output arguments: none<br>Return value: none|
129e41f4b71Sopenharmony_ci|  | int GetFlags() | Obtains the trace flag set in the **HiTraceId** object.<br>Input arguments: none<br>Output arguments: none<br>Return value: Returns the combination of trace flags. For details, see the description in the **Begin** function.|
130e41f4b71Sopenharmony_ci|  | void SetFlags(int flags) | Sets a trace flag in the **HiTraceId** object.<br>Input arguments:<br>- **flags**: combination of trace flags. For details, see the description in the **Begin** function.<br>Output arguments: none<br>Return value: none|
131e41f4b71Sopenharmony_ci|  | uint64_t GetChainId() | Obtains the call chain ID.<br>Input arguments: none<br>Output arguments: none<br>Return value: call chain ID.|
132e41f4b71Sopenharmony_ci|  | void SetChainId(uint64_t chainId) | Sets the call chain ID in the **HiTraceId** object.<br>Input arguments:<br>- **chainId**: call chain ID.<br>Output arguments: none<br>Return value: none|
133e41f4b71Sopenharmony_ci|  | uint64_t GetSpanId() | Obtains the span ID from the current **HiTraceId** object.<br>Input arguments: none<br>Output arguments: none<br>Return value: current span ID.|
134e41f4b71Sopenharmony_ci|  | void SetSpanId(uint64_t spanId) | Sets the span ID in the **HiTraceId** object.<br>Input arguments:<br>- **spanId**: span ID.<br>Output arguments: none<br>Return value: none|
135e41f4b71Sopenharmony_ci|  | uint64_t GetParentSpanId() | Obtains the parent span ID from the current **HiTraceId** object.<br>Input arguments: none<br>Output arguments: none<br>Return value: parent span ID.|
136e41f4b71Sopenharmony_ci|  | void SetParentSpanId(uint64_t parentSpanId) | Sets the parent span ID in the **HiTraceId** object.<br>Input arguments:<br>- **parentSpanId**: parent span ID.<br>Output arguments: none<br>Return value: none|
137e41f4b71Sopenharmony_ci|  | int ToBytes(uint8_t* pIdArray, int len) | Converts the **HiTraceId** object into a byte array to facilitate caching or communication transfer.<br>Input arguments:<br>- **pIdArray**: pointer to a byte array. The minimum length of the byte array is **HITRACE_ID_LEN**.<br>- **len**: length of the byte array.<br>Output parameters:<br>- **pIdArray**: pointer to a byte array. If the object is valid, the object data after conversion is stored.<br>Return value: a value greater than **0** (indicating a valid array of object data) if the conversion is successful; **0** otherwise.|
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci### Call Chain Processing
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ciInter-device, inter-process, and inter-thread calls are implemented through the communication mechanism. HiTraceChain requires transfer of **traceid** in the communication mechanism.
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ciSome built-in communication mechanisms (such as ZIDL) of OpenHarmony already support the transfer of **traceid**.
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ciThe following figure shows the process of transferring **traceid** in synchronous call. The process of transferring **traceid** in asynchronous call is similar.
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci  Extended communication mechanisms can also follow this implementation.
149e41f4b71Sopenharmony_ci  **Figure 5** Call chain trace in synchronous communication
150e41f4b71Sopenharmony_ci  ![](figures/call-chain-trace-in-synchronous-communication.png "call-chain-trace-in-synchronous-communication")
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ciThe process is as follows:
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci1. The service module on the client calls the **begin()** function to start call chain trace.
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci2. The service module on the client synchronously calls the **transact** function to the communication component on the client.
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci3. The communication component on the client performs the following:
159e41f4b71Sopenharmony_ci   1. Obtains **traceid** from the TLS of the calling thread.
160e41f4b71Sopenharmony_ci   2. Generates child **traceid**.
161e41f4b71Sopenharmony_ci   3. Writes child **traceid** into the transaction data (synchronous communication data).
162e41f4b71Sopenharmony_ci   4. Performs CS trace.
163e41f4b71Sopenharmony_ci   5. Sends communication data to the communication component on the server.
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci4. On receiving the communication data, the communication component on the server performs the following:
166e41f4b71Sopenharmony_ci   1. Obtains **traceid** from the data message package.
167e41f4b71Sopenharmony_ci   2. Sets **traceid** in the TLS of the calling thread.
168e41f4b71Sopenharmony_ci   3. Performs SR trace.
169e41f4b71Sopenharmony_ci   4. Synchronously calls the **onTransact** callback to the service module on the server.
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci5. The service module on the server processes the service and sends the transact reply message carrying the processing result to the communication component.
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci6. The communication component on the server performs the following:
174e41f4b71Sopenharmony_ci   1. Performs SS trace.
175e41f4b71Sopenharmony_ci   2. Sends communication data to the communication component on the client.
176e41f4b71Sopenharmony_ci   3. Clears **traceid** from the TLS of the calling thread.
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci7. On receiving the communication data, the communication component on the client performs the following:
179e41f4b71Sopenharmony_ci   1. Performs CR trace.
180e41f4b71Sopenharmony_ci   2. Sends a transact reply response to the service module on the client.
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci8. The service module on the client processes the transact reply response.
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci9. When the process ends, the service module on the client calls the **end()** function to stop call chain trace.
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci## How to Develop
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci### **C++**
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci1. Develop the source code.
193e41f4b71Sopenharmony_ci     Include the **hitracechain** header file in the class definition header file or class implementation source file. For example:
194e41f4b71Sopenharmony_ci     
195e41f4b71Sopenharmony_ci   ```
196e41f4b71Sopenharmony_ci   #include "hitrace/tracechain.h"
197e41f4b71Sopenharmony_ci   ```
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci     Add the code to start and stop call chain trace in the class implementation source file.
200e41f4b71Sopenharmony_ci   
201e41f4b71Sopenharmony_ci   ```
202e41f4b71Sopenharmony_ci   using namespace OHOS::HiviewDFX;
203e41f4b71Sopenharmony_ci   auto traceId = HiTraceChain::Begin("MyServiceFlow", HITRACE_FLAG_DEFAULT);
204e41f4b71Sopenharmony_ci   ......
205e41f4b71Sopenharmony_ci   HiTraceChain::End(traceId);
206e41f4b71Sopenharmony_ci   ```
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci2. Configure compilation information. Specifically, add the subsystem SDK dependency to **BUILD.gn**.
209e41f4b71Sopenharmony_ci   
210e41f4b71Sopenharmony_ci   ```
211e41f4b71Sopenharmony_ci   external_deps = [ "hitrace:libhitracechain" ]
212e41f4b71Sopenharmony_ci   ```
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ci
215e41f4b71Sopenharmony_ci### **C**
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci1. Develop the source code.
218e41f4b71Sopenharmony_ci     Include the **hitracechain** header file in the source file.
219e41f4b71Sopenharmony_ci     
220e41f4b71Sopenharmony_ci   ```
221e41f4b71Sopenharmony_ci   #include "hitrace/tracechain.h"
222e41f4b71Sopenharmony_ci   ```
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ci     Add the code to start and stop call chain trace in the class implementation source file.
225e41f4b71Sopenharmony_ci   
226e41f4b71Sopenharmony_ci   ```
227e41f4b71Sopenharmony_ci   HiTraceIdStruct traceId = HiTraceChainBegin("MyServiceFlow", HITRACE_FLAG_DEFAULT);
228e41f4b71Sopenharmony_ci   ......
229e41f4b71Sopenharmony_ci   HiTraceChainEnd(traceId);
230e41f4b71Sopenharmony_ci   ```
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci2. Configure compilation information. Specifically, add the subsystem SDK dependency to **BUILD.gn**.
233e41f4b71Sopenharmony_ci   
234e41f4b71Sopenharmony_ci   ```
235e41f4b71Sopenharmony_ci   external_deps = [ "hitrace:libhitracechain" ]
236e41f4b71Sopenharmony_ci   ```
237