1# Development of Performance Tracing (ArkTS) 2 3## Overview 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 20- Due 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, count: number) | void | Marks the value changes of a numeric variable in a trace task. | 33 34## How to Develop 35 36Start performance tracing upon page loading and stop it upon completion of service usage. 37 381. 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: 39 40 ```ts 41 import hiTraceMeter from '@ohos.hiTraceMeter'; 42 43 @Entry 44 @Component 45 struct Index { 46 @State message: string = 'Running'; 47 48 build() { 49 Row() { 50 Column() { 51 Text(this.message) 52 .fontSize(50) 53 .fontWeight(FontWeight.Bold) 54 .onClick(() => { 55 this.message = 'Hello ArkUI'; 56 57 // Start trace tasks with the same name concurrently. 58 hiTraceMeter.startTrace("HITRACE_TAG_APP", 1001); 59 // Keep the service process running. 60 console.log(`HITRACE_TAG_APP running`); 61 62 // 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. 63 hiTraceMeter.startTrace("HITRACE_TAG_APP", 1002); 64 // Keep the service process running. 65 console.log(`HITRACE_TAG_APP running`); 66 67 hiTraceMeter.finishTrace("HITRACE_TAG_APP", 1001); 68 hiTraceMeter.finishTrace("HITRACE_TAG_APP", 1002); 69 70 // If trace tasks with the same name are not run concurrently, the same taskId can be used. 71 hiTraceMeter.startTrace("HITRACE_TAG_APP", 1003); 72 // Keep the service process running. 73 console.log(`HITRACE_TAG_APP running`); 74 // End the first trace task. 75 hiTraceMeter.finishTrace("HITRACE_TAG_APP", 1003); 76 77 // Start the second trace task with the same name in serial mode. It uses a taskId different from the first trace task. 78 hiTraceMeter.startTrace("HITRACE_TAG_APP", 1004); 79 // Keep the service process running. 80 console.log(`HITRACE_TAG_APP running`); 81 let traceCount = 3; 82 hiTraceMeter.traceByValue("myTestCount", traceCount); 83 hiTraceMeter.finishTrace("HITRACE_TAG_APP", 1004); 84 85 // Start the third trace task with the same name in serial mode. It uses a taskId same as the second trace task. 86 hiTraceMeter.startTrace("HITRACE_TAG_APP", 1004); 87 // Keep the service process running. 88 console.log(`HITRACE_TAG_APP running`); 89 // End the third trace task. 90 hiTraceMeter.finishTrace("HITRACE_TAG_APP", 1004); 91 92 }) 93 } 94 .width('100%') 95 } 96 .height('100%') 97 } 98 } 99 ``` 100 1013. Click the run button on the application page. Then, run the following commands in sequence in shell: 102 103 ```shell 104 hdc shell 105 hitrace --trace_begin app 106 ``` 107 108 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: 109 110 ```shell 111 hitrace --trace_dump | grep tracing_mark_write 112 hitrace --trace_finish 113 ``` 114 115 The following is an example of the captured trace data: 116 117 ``` 118 <...>-3310 (-------) [005] .... 351382.921936: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1001 119 <...>-3310 (-------) [005] .... 351382.922138: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1002 120 <...>-3310 (-------) [005] .... 351382.922165: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1001 121 <...>-3310 (-------) [005] .... 351382.922175: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1002 122 <...>-3310 (-------) [005] .... 351382.922182: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1003 123 <...>-3310 (-------) [005] .... 351382.922203: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1003 124 <...>-3310 (-------) [005] .... 351382.922210: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1004 125 <...>-3310 (-------) [005] .... 351382.922233: tracing_mark_write: C|3310|H:myTestCount 3 126 <...>-3310 (-------) [005] .... 351382.922240: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1004 127 <...>-3310 (-------) [005] .... 351382.922247: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1004 128 <...>-3310 (-------) [005] .... 351382.922266: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1004 129 ``` 130