1# Development of Performance Tracing 2 3## Introduction 4 5hiTraceMeter provides APIs for system performance tracing. You can call the APIs provided by the hiTraceMeter module in your own service logic to effectively track service processes and check the system performance. 6 7## Basic Concepts 8 9- **hiTraceMeter Tag** 10 11 Tag used for tracing data categorization. It is also known as **hiTraceMeter Category**. Generally, one subsystem maps to a tag. The tag is passed as the **Tag** parameter in performance tracing APIs. When you use the hiTraceMeter CLI tool to collect tracing data, only the tracing data specified by the **Tag** parameter is collected. 12 13## Working Principles 14 15- The application calls hiTraceMeter APIs to perform performance tracing. The APIs output the tracing data to the kernel's ftrace data buffer through the kernel's sysfs file interface. 16- The hiTraceMeter CLI tool reads the tracing data in the ftrace data buffer and saves the trace data as a text file on the device. 17 18## Constraints 19 20Due to the asynchronous I/O feature of JS, the hiTraceMeter module provides only asynchronous APIs. 21 22## Available APIs 23 24The performance tracing APIs are provided by the **hiTraceMeter** module. For details, see [API Reference]( ../reference/apis/js-apis-hitracemeter.md). 25 26**APIs for performance tracing** 27 28| API | Return Value | Description | 29| ---------------------------------------------------------------------------- | --------- | ------------ | 30| hiTraceMeter.startTrace(name: string, taskId: number) | void | Marks the start of a trace task. If multiple trace tasks with the same name need to be performed at the same time or a trace task needs to be performed multiple times concurrently, different task IDs must be specified in **startTrace**. If the trace tasks with the same name are not performed at the same time, the same task ID can be used.| 31| hiTraceMeter.finishTrace(name: string, taskId: number) | void | Marks the end of a trace task. The values of **name** and **taskId** must be the same as those of **hiTraceMeter.startTrace**.| 32| hiTraceMeter.traceByValue(name: string, value: number) | void | Marks the value changes of a numeric variable in a trace task.| 33 34## How to Develop 35 36In this example, distributed call chain tracing begins when the application startup execution page is loaded and stops when the service usage is completed. 37 381. Create a JS application project. In the displayed **Project** window, choose **entry** > **src** > **main** > **js** > **default** > **pages** > **index**, and double-click **index.js**. Add the code to implement performance tracing upon page loading. The sample code is as follows: 39 40 ```js 41 import hiTraceMeter from '@ohos.hiTraceMeter' 42 43 export default { 44 data: { 45 title: "" 46 }, 47 onInit() { 48 this.title = this.$t('strings.world'); 49 50 // Start trace tasks with the same name concurrently. 51 hiTraceMeter.startTrace("business", 1); 52 // Keep the service process running. 53 console.log(`business running`); 54 hiTraceMeter.startTrace("business", 2); // Start the second trace task with the same name while the first task is still running. The tasks are running concurrently and therefore their taskId must be different. 55 // Keep the service process running. 56 console.log(`business running`); 57 hiTraceMeter.finishTrace("business", 1); 58 // Keep the service process running. 59 console.log(`business running`); 60 hiTraceMeter.finishTrace("business", 2); 61 62 // Start trace tasks with the same name in serial mode. 63 hiTraceMeter.startTrace("business", 1); 64 // Keep the service process running. 65 console.log(`business running`); 66 hiTraceMeter.finishTrace("business", 1); // End the first trace task. 67 // Keep the service process running. 68 console.log(`business running`); 69 hiTraceMeter.startTrace("business", 1); // Start the second trace task with the same name in serial mode. 70 // Keep the service process running. 71 console.log(`business running`); 72 73 let traceCount = 3; 74 hiTraceMeter.traceByValue("myTestCount", traceCount); 75 traceCount = 4; 76 hiTraceMeter.traceByValue("myTestCount", traceCount); 77 hiTraceMeter.finishTrace("business", 1); 78 } 79 } 80 ``` 81 822. Create an ArkTs application project. In the displayed **Project** window, choose **entry** > **src** > **main** > **ets** > **pages** > **index**, and double-click **index.js**. Add the code to implement performance tracing upon page loading. For example, if the name of the trace task is **HITRACE\_TAG\_APP**, the sample code is as follows: 83 84 ```ts 85 import hitrace from '@ohos.hiTraceMeter'; 86 87 @Entry 88 @Component 89 struct Index { 90 @State message: string = 'Hello World'; 91 92 build() { 93 Row() { 94 Column() { 95 Text(this.message) 96 .fontSize(50) 97 .fontWeight(FontWeight.Bold) 98 .onClick(() => { 99 this.message = 'Hello ArkUI'; 100 101 // Start trace tasks with the same name concurrently. 102 hitrace.startTrace("HITRACE_TAG_APP", 1001); 103 // Keep the service process running. 104 console.log(`HITRACE_TAG_APP running`); 105 106 // Start the second trace task with the same name while the first task is still running. The tasks are running concurrently and therefore their taskId must be different. 107 hitrace.startTrace("HITRACE_TAG_APP", 1002); 108 // Keep the service process running. 109 console.log(`HITRACE_TAG_APP running`); 110 111 hitrace.finishTrace("HITRACE_TAG_APP", 1001); 112 hitrace.finishTrace("HITRACE_TAG_APP", 1002); 113 114 // If trace tasks with the same name are not run concurrently, the same taskId can be used. 115 hitrace.startTrace("HITRACE_TAG_APP", 1003); 116 // Keep the service process running. 117 console.log(`HITRACE_TAG_APP running`); 118 // End the first trace task. 119 hitrace.finishTrace("HITRACE_TAG_APP", 1003); 120 121 // Start the second trace task with the same name in serial mode. It uses a taskId different from the first trace task. 122 hitrace.startTrace("HITRACE_TAG_APP", 1004); 123 // Keep the service process running. 124 console.log(`HITRACE_TAG_APP running`); 125 let traceCount = 3; 126 hitrace.traceByValue("myTestCount", traceCount); 127 hitrace.finishTrace("HITRACE_TAG_APP", 1004); 128 129 // Start the third trace task with the same name in serial mode. It uses a taskId same as the second trace task. 130 hitrace.startTrace("HITRACE_TAG_APP", 1004); 131 // Keep the service process running. 132 console.log(`HITRACE_TAG_APP running`); 133 // End the third trace task. 134 hitrace.finishTrace("HITRACE_TAG_APP", 1004); 135 136 }) 137 } 138 .width('100%') 139 } 140 .height('100%') 141 } 142 } 143 ``` 144 1453. Click the run button on the application page. Then, run the following commands in sequence in shell: 146 147 ```shell 148 hdc shell 149 hitrace --trace_begin app 150 ``` 151 152 After the trace command is executed, call the hiTraceMeter APIs in your own service logic on the device. Then, run the following commands in sequence: 153 154 ```shell 155 hitrace --trace_dump | grep tracing_mark_write 156 hitrace --trace_finish 157 ``` 158 159 The following is an example of the captured trace data: 160 161 ``` 162 <...>-3310 (-------) [005] .... 351382.921936: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1001 163 <...>-3310 (-------) [005] .... 351382.922138: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1002 164 <...>-3310 (-------) [005] .... 351382.922165: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1001 165 <...>-3310 (-------) [005] .... 351382.922175: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1002 166 <...>-3310 (-------) [005] .... 351382.922182: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1003 167 <...>-3310 (-------) [005] .... 351382.922203: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1003 168 <...>-3310 (-------) [005] .... 351382.922210: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1004 169 <...>-3310 (-------) [005] .... 351382.922233: tracing_mark_write: C|3310|H:myTestCount 3 170 <...>-3310 (-------) [005] .... 351382.922240: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1004 171 <...>-3310 (-------) [005] .... 351382.922247: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1004 172 <...>-3310 (-------) [005] .... 351382.922266: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1004 173 ``` 174