• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.hiTraceMeter (Performance Tracing)
2
3The **hiTraceMeter** module provides the functions of tracing service processes and monitoring the system performance. It provides the data needed for hiTraceMeter to carry out performance analysis.
4
5> **NOTE**<br>
6> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
7
8
9## Modules to Import
10
11```js
12import hiTraceMeter from '@ohos.hiTraceMeter';
13```
14
15
16## hiTraceMeter.startTrace
17
18startTrace(name: string, taskId: number): void
19
20Starts a trace task.
21
22If 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**.
23
24If the trace tasks with the same name are not performed at the same time, the same taskId can be used. For a specific example, refer to an example in [hiTraceMeter.finishTrace](#hitracemeterfinishtrace).
25
26**System capability**: SystemCapability.HiviewDFX.HiTrace
27
28**Parameters**
29
30| Name| Type | Mandatory | Description |
31| -------- | -------- | -------- | -------- |
32| name | string | Yes | Name of the trace task to start. |
33| taskId | number | Yes| Task ID. |
34
35**Example**
36
37```js
38hiTraceMeter.startTrace("myTestFunc", 1);
39```
40
41
42## hiTraceMeter.finishTrace
43
44finishTrace(name: string, taskId: number): void
45
46Stops a trace task.
47
48To stop a trace task, the values of name and task ID in **finishTrace** must be the same as those in [startTrace](#hitracemeterstarttrace).
49
50**System capability**: SystemCapability.HiviewDFX.HiTrace
51
52**Parameters**
53
54| Name| Type| Mandatory| Description|
55| -------- | -------- | -------- | -------- |
56| name | string | Yes| Name of the trace task to start. |
57| taskId | number | Yes| Task ID. |
58
59**Example**
60
61```js
62hiTraceMeter.finishTrace("myTestFunc", 1);
63```
64
65```js
66// Start track tasks with the same name concurrently.
67hiTraceMeter.startTrace("myTestFunc", 1);
68// Service flow
69hiTraceMeter.startTrace("myTestFunc", 2);  // The second trace task starts while the first task is still running. The first and second tasks have the same name but different task IDs.
70// Service flow
71hiTraceMeter.finishTrace("myTestFunc", 1);
72// Service flow
73hiTraceMeter.finishTrace("myTestFunc", 2);
74```
75
76```js
77// Start track tasks with the same name at different times.
78hiTraceMeter.startTrace("myTestFunc", 1);
79// Service flow
80hiTraceMeter.finishTrace("myTestFunc", 1);  // The first trace task ends.
81// Service flow
82hiTraceMeter.startTrace("myTestFunc", 1);   // The second trace task starts after the first task ends. The two tasks have the same name and task ID.
83// Service flow
84hiTraceMeter.finishTrace("myTestFunc", 1);
85```
86
87
88## hiTraceMeter.traceByValue
89
90traceByValue(name: string, count: number): void
91
92Traces the value changes of a variable.
93
94**System capability**: SystemCapability.HiviewDFX.HiTrace
95
96**Parameters**
97
98| Name| Type| Mandatory| Description|
99| -------- | -------- | -------- | -------- |
100| name | string | Yes | Name of the variable. |
101| count | number | Yes | Value of the variable. |
102
103**Example**
104```js
105let traceCount = 3;
106hiTraceMeter.traceByValue("myTestCount", traceCount);
107traceCount = 4;
108hiTraceMeter.traceByValue("myTestCount", traceCount);
109// Service flow
110```
111