• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 性能打点跟踪开发指导(ArkTS)
2
3## 简介
4
5hiTraceMeter为开发者提供系统性能打点接口。开发者通过在自己的业务逻辑中的关键代码位置调用HiTraceMeter接口提供的API接口,能够有效跟踪进程轨迹、查看系统性能。
6
7## 基本概念
8
9- **hiTraceMeter Tag**
10
11  跟踪数据使用类别分类,称作hiTraceMeter Tag或hiTraceMeter Category,一般每个软件子系统对应一个Tag,该Tag在打点API中以类别Tag参数传入。hiTraceMeter命令行工具采集跟踪数据时,只采集给定的Tag类别选项指定的跟踪数据。
12
13## 实现原理
14
15- 应用程序通过hiTraceMeter函数接口进行打点,hiTraceMeter函数将跟踪数据通过内核sysfs文件接口输出到内核的ftrace数据缓冲区。
16- hiTraceMeter命令行工具读取内核ftrace缓冲区中的跟踪数据,将文本格式的跟踪数据保存到设备侧的文件中。
17
18## 约束与限制
19
20- 由于JS程序的异步IO特性,现在hiTraceMeter只提供了异步接口。
21
22## 接口说明
23
24性能打点跟踪接口由hiTraceMeter模块提供,详细API请参考[性能打点跟踪API参考](../reference/apis/js-apis-hitracemeter.md)。
25
26**性能打点跟踪接口功能介绍:**
27
28| 接口名                                                    | 返回值  | 描述                                                                                                                   |
29| ------------------------------------------------------ | ---- | -------------------------------------------------------------------------------------------------------------------- |
30| hiTraceMeter.startTrace(name: string, taskId: number)  | void | 标记一个预跟踪耗时任务的开始。taskId是trace中用来表示关联的ID,如果有多个name相同的任务是并行执行的,则每次调用startTrace的taskId不同。如果具有相同name的任务是串行执行的,则taskId可以相同。 |
31| hiTraceMeter.finishTrace(name: string, taskId: number) | void | name和taskId必须与流程开始的hiTraceMeter.startTrace对应参数值保持一致。                                                                 |
32| hiTraceMeter.traceByValue(name: string, value: number) | void | 用来标记一个预跟踪的数值变量,该变量的数值会不断变化。                                                                                          |
33
34## 开发步骤
35
36在应用启动执行页面加载后,开始分布式跟踪,完成业务之后,停止分布式跟踪。
37
381. 新建一个ArkTS应用工程,在“Project”窗口点击“entry > src > main > ets > pages > index”,打开工程中的“index.ets”文件,在页面执行加载后,在自己的业务中调用hiTraceMeter的接口,进行性能打点跟踪,以任务名name为HITRACE_TAG_APP为例 示例代码如下:
39
40   ```ts
41   import hitrace from '@ohos.hiTraceMeter';
42
43   @Entry
44   @Component
45   struct Index {
46     @State message: string = 'Hello World';
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               // 跟踪并行执行的同名任务
58               hitrace.startTrace("HITRACE_TAG_APP", 1001);
59               // 业务流程
60               console.log(`HITRACE_TAG_APP running`);
61
62               // 第二个跟踪任务开始,同时第一个跟踪的同名任务还没结束,出现了并行执行,对应接口的taskId需要不同。
63               hitrace.startTrace("HITRACE_TAG_APP", 1002);
64               // 业务流程
65               console.log(`HITRACE_TAG_APP running`);
66
67               hitrace.finishTrace("HITRACE_TAG_APP", 1001);
68               hitrace.finishTrace("HITRACE_TAG_APP", 1002);
69
70               // 跟踪串行执行的同名任务,taskId可以不同,也可以相同
71               hitrace.startTrace("HITRACE_TAG_APP", 1003);
72               // 业务流程
73               console.log(`HITRACE_TAG_APP running`);
74               //第一个跟踪的任务结束
75               hitrace.finishTrace("HITRACE_TAG_APP", 1003);
76
77               // 第二个跟踪任务开始,同名的待跟踪任务串行执行,且taskId不同
78               hitrace.startTrace("HITRACE_TAG_APP", 1004);
79               // 业务流程
80               console.log(`HITRACE_TAG_APP running`);
81               let traceCount = 3;
82               hitrace.traceByValue("myTestCount", traceCount);
83               hitrace.finishTrace("HITRACE_TAG_APP", 1004);
84
85               // 第三个跟踪任务开始,同名的待跟踪任务串行执行,且taskId与上一个相同
86               hitrace.startTrace("HITRACE_TAG_APP", 1004);
87               // 业务流程
88               console.log(`HITRACE_TAG_APP running`);
89               //第三个跟踪的任务结束
90               hitrace.finishTrace("HITRACE_TAG_APP", 1004);
91
92             })
93          }
94          .width('100%')
95        }
96        .height('100%')
97      }
98   }
99   ```
100
1013. 运行项目,点击应用界面上的运行按钮,在shell中依次执行如下命令:
102
103   ```shell
104   hdc shell
105   hitrace --trace_begin app
106   ```
107
108   执行抓取trace命令后,先在设备中自己的业务里面调用接口,继续依次执行如下命令:
109
110   ```shell
111   hitrace --trace_dump | grep tracing_mark_write
112   hitrace --trace_finish
113   ```
114
115   抓取trace成功的数据如下所示
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