1# Using HiTraceMeter (C/C++)
2
3**HiTrace** provides APIs to implement call chain tracing throughout a service process. You can use these APIs to 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.
4
5## Available APIs
6
7The performance tracing APIs are provided by the **HiTraceMeter** module. For details, see [Hitrace](../reference/apis-performance-analysis-kit/_hitrace.md).
8
9| API| Description|
10| -------- | -------- |
11| void OH_HiTrace_StartTrace(const char* name) | Starts a synchronous time slice trace.|
12| void OH_HiTrace_FinishTrace() | Ends a synchronous time slice trace.|
13| void OH_HiTrace_StartAsyncTrace(const char* name, int32_t taskId) | Starts an asynchronous time slice trace.|
14| void OH_HiTrace_FinishAsyncTrace(const char* name, int32_t taskId) | Ends an asynchronous time slice trace.|
15| void OH_HiTrace_CountTrace(const char* name, int64_t count) | Performs an integer trace.|
16
17**Parameter Description**
18
19| Name| Type| Mandatory| Description                                                        |
20| ------ | ------ | ---- | ------------------------------------------------------------ |
21| name   | string | Yes  | Name of the variable.|
22| taskId | number | Yes  | ID used to indicate the association of APIs in a trace. If multiple traces with the same name need to be performed at the same time, different task IDs must be specified in **startTrace**.|
23| count  | number | Yes  | Value of the variable. |
24
25## How to Develop
26
271. Add **libhitrace_ndk.z.so** to **CMakeLists.txt**.
28
29    ```
30    target_link_libraries(entry PUBLIC libhitrace_ndk.z.so)
31    ```
32
332. Include the **trace.h** header file in the source file.
34
35    ```c++
36    #include "hitrace/trace.h"
37    ```
38
393. Trace performance data. The following uses an asynchronous trace as an example. (The sample code is a part of the default **hello.cpp** file. You only need to add related APIs at the required positions. For details about the APIs, see "Available APIs".)
40
41    ```c++
42    #include "napi/native_api.h"
43    #include "hitrace/trace.h"
44    static napi_value Add(napi_env env, napi_callback_info info)
45    {
46        // Start an asynchronous time slice trace.
47        OH_HiTrace_StartAsyncTrace("hitraceTest", 123);
48        // End the asynchronous time slice trace (The start and end points are for reference only. Add them to the code lines where you want to start and end the trace.)
49        OH_HiTrace_FinishAsyncTrace("hitraceTest", 123);
50        size_t requireArgc = 2;
51        size_t argc = 2;
52        napi_value args[2] = {nullptr};
53
54        napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
55
56        napi_valuetype valuetype0;
57        napi_typeof(env, args[0], &valuetype0);
58
59        napi_valuetype valuetype1;
60        napi_typeof(env, args[1], &valuetype1);
61
62        double value0;
63        napi_get_value_double(env, args[0], &value0);
64
65        double value1;
66        napi_get_value_double(env, args[1], &value1);
67
68        napi_value sum;
69        napi_create_double(env, value0 + value1, &sum);
70
71        return sum;
72
73    }
74    ```
75
764. Install the compiled HAP in the device. In the **cmd** window, run **hdc shell** to connect to the device, and run **hitrace --trace_begin app** to start trace.
77
78    ```shell
79    capturing trace...
80    ```
81
825. Click the newly installed HAP several times on the device, and then run **hitrace --trace_dump | grep hitraceTest** in the **shell** window to view the trace result.
83
84    ```shell
85    <...>-2477    (-------) [001] ....   396.427165: tracing_mark_write: S|2477|H:hitraceTest 123
86    <...>-2477    (-------) [001] ....   396.427196: tracing_mark_write: F|2477|H:hitraceTest 123
87    ```
88