目录

Viewing HiTraceMeter Logs

Viewing Logs on DevEco Studio

In DevEco Studio Profiler, you can display HiTraceMeter logs to analyze the CPU usage and thread running status of applications and services and view the CPU consumption in a specified period. For details, see CPU Activity Analysis.

Viewing Logs Using Command Line Tools

  1. Set up the environment for HarmonyOS Device Connector (hdc). For details, see Environment Setup.

  2. In the Terminal window of DevEco Studio or the CLI on the host, run the hdc shell command to connect to the device, and run the HiTrace command on the device to enable the HiTraceMeter logging service.

    PS D:\xxx\xxx> hdc shell
    $ hitrace --trace_begin app
  3. Run the program that enables the HiTraceMeter logging on the device.

  4. Use HiTrace to collect trace information in text format and save the trace file to the /data/local/tmp/ directory of the device. For details, see Capturing Text Trace Data in Snapshot Mode.

  5. Exit the device, enter the host, and export the HiTraceMeter text logs to the current directory. (Change the file name trace.ftrace in the following command based on the file name set during collection.)

    $ exit
    PS D:\xxx\xxx> hdc file recv /data/local/tmp/trace.ftrace ./
  6. You can search for keywords such as the logging name in the HiTraceMeter logs to check whether the logging is successful.

  7. Display and analyze the HiTraceMeter text file.

    • Import the log file into DevEco Studio for analysis.

      In DevEco Studio Profiler, select Open File to import the HiTraceMeter log file.

      For details, see CPU Activity Analysis.

    • Use the Smartperf_Host tool for analysis.

User-Mode Trace Format

You can run the hitrace command to collect user-mode trace logs generated by the HiTraceMeter API to analyze application performance. The following describes the format and extension rule for user-mode traces of the synchronous, asynchronous, and integer value types, including the field composition, description, and relationship between fields and input parameters, as well as sample trace logs in different scenarios for reference.

For details about the user-mode trace format since API version 19, see User-mode Trace Format Since API Version 19. For details about the user-mode trace format in versions earlier than API version 19, see User-mode Trace Format Before API Version 19.

[h2]User-mode Trace Format Since API Version 19

Synchronous trace

A synchronous trace consists of a start trace (Type identifier: B) and an end trace (Type identifier: E).

Asynchronous trace

An asynchronous trace consists of a start trace (Type identifier: S) and an end trace (Type identifier: F).

Integer value trace

An integer value trace contains only one independent trace whose type identifier is C.

  1. A valid and complete synchronous trace or asynchronous trace contains a start trace and an end trace. You need to ensure that the API calls match. Otherwise, the performance trace may be abnormal. For details, see @ohos.hiTraceMeter (Performance Tracing) or C++ APIs in trace.h.
  2. The maximum length of a user-mode trace is 512 characters. Excess characters will be truncated.

[h2]Fields

The following describes fields of the user-mode trace, including the field composition, description, and relationship between fields and logging input parameters. For details, see @ohos.hiTraceMeter (Performance Tracing).

[h2]Field Extension Rules

Since API version 19, new fields are added to the end of the current user-mode trace format in the format of |field.

[h2]Sample Trace Logs in Different Scenarios

Trace Type Format When HiTraceChain is Enabled Format When HiTraceChain is Disabled
Start Sync Trace Point B|1234|H:[a92ab94c18e1341,0,0]#tracename|M62|key1=value1,key2=value2 B|1234|H:tracename|M62|key1=value1,key2=value2
Stop a synchronous trace logging E|1234|M62 E|1234|M62
Start Async Trace Point S|1234|H:[a92ab94c18e1341,0,0]#tracename|428|M62|appcategory01|key1=value1,key2=value2 S|1234|H:tracename|428|M62|appcategory01|key1=value1,key2=value2
Stop an asynchronous trace logging F|1234|H:[a92ab94c18e1341,0,0]#tracename|428|M62 F|1234|H:tracename|428|M62
Log an integer trace C|1234|H:[a92ab94c18e1341,0,0]#tracename|5678|M62 C|1234|H:tracename|5678|M62

In the trace log samples above, the Process ID is 1234, the trace point content is tracename, and the level is M, representing the COMMERCIAL level. The tag set is 62, which contains only one tag, with the corresponding tag name being app. When HiTraceChain is enabled, the distributed tracing chain ID is [a92ab94c18e1341,0,0]. The custom key-value pairs for both synchronous tracing and asynchronous tracing are key1=value1,key2=value2. For the asynchronous tracing trace point, the async task ID is 428 and the custom aggregation name is appcategory01. The trace value for integer tracing is 5678.

The maximum length of a user-mode trace is 512 characters. Excess characters will be truncated. Therefore, it is recommended that the total length of the tracename, customCategory, and customArgs fields be less than or equal to 420 characters.

[h2]Empty Field Scenarios

In the user-mode trace, if the middle field is empty, the vertical bar (|) is retained. If the last field is empty, the vertical bar (|) is removed. The following shows examples of the customCategory and customArgs parameters of the hiTraceMeter.startAsyncTrace() and hiTraceMeter.startSyncTrace() APIs.

[h2]HiTraceMeter Tags

The following is the HiTraceMeter User Mode Tag set, where the Tag value is the left shift value based on the number 1 at the actual code layer. The definitions of User Mode Tags can be viewed using the hitrace Command Line Tool, where always and commercial are the system-side default always-on Tag and the commercial version scenario customization Tag, respectively.

Name Constant Value at the Code Layer Value
always 1ULL << 0 00
commercial 1ULL << 5 05
drm 1ULL << 6 06
security 1ULL << 7 07
animation 1ULL << 9 09
push 1ULL << 10 10
virse 1ULL << 11 11
ffrt 1ULL << 13 13
musl 1ULL << 12 12
cloud 1ULL << 14 14
deviceauth 1ULL << 15 15
commonlibrary 1ULL << 16 16
hdcd 1ULL << 17 17
hdf 1ULL << 18 18
usb 1ULL << 19 19
interconn 1ULL << 20 20
dlpcre 1ULL << 21 21
accesscontrol 1ULL << 22 22
net 1ULL << 23 23
nweb 1ULL << 24 24
huks 1ULL << 25 25
useriam 1ULL << 26 26
daudio 1ULL << 27 27
dlsm 1ULL << 28 28
filemanagement 1ULL << 29 29
ohos 1ULL << 30 30
ability 1ULL << 31 31
zcamera 1ULL << 32 32
zmedia 1ULL << 33 33
zimage 1ULL << 34 34
zaudio 1ULL << 35 35
distributeddatamgr 1ULL << 36 36
mdfs 1ULL << 37 37
graphic 1ULL << 38 38
ace 1ULL << 39 39
notification 1ULL << 40 40
misc 1ULL << 41 41
multimodalinput 1ULL << 42 42
sensors 1ULL << 43 43
msdp 1ULL << 44 44
dsoftbus 1ULL << 45 45
rpc 1ULL << 46 46
ark 1ULL << 47 47
window 1ULL << 48 48
account 1ULL << 49 49
dscreen 1ULL << 50 50
dcamera 1ULL << 51 51
dhfwk 1ULL << 52 52
gresource 1ULL << 53 53
devicemanager 1ULL << 54 54
samgr 1ULL << 55 55
power 1ULL << 56 56
dsched 1ULL << 57 57
deviceprofile 1ULL << 58 58
dinput 1ULL << 59 59
bluetooth 1ULL << 60 60
accessibility 1ULL << 61 61
app 1ULL << 62 62

[h2]HiTraceOutputLevel

Enum Name (C/C++) Enum Name (ArkTS) Value Trace Level
HITRACE_LEVEL_DEBUG DEBUG 0 D
HITRACE_LEVEL_INFO INFO 1 I
HITRACE_LEVEL_CRITICAL CRITICAL 2 C
HITRACE_LEVEL_COMMERCIAL COMMERCIAL 3 M

[h2]User-Mode Trace Format Before API version 19

Synchronous trace

Asynchronous trace

Integer value trace

The start and end formats of an asynchronous trace are the same. In versions earlier than API version 19, the asynchronous task ID and integer value fields are separated from the content field by spaces.

[h2]Sample Trace Logs in Different Scenarios Before API Version 19

Trace Type Format When HiTraceChain is Enabled Format When HiTraceChain is Disabled
Start a synchronous trace logging B|1234|H:[a92ab94c18e1341,0,0]#tracename B|1234|H:tracename
Stop a synchronous trace logging E|1234| E|1234|
Start an asynchronous trace logging S|1234|H:[a92ab94c18e1341,0,0]#tracename 428 S|1234|H:tracename 428
Stop an asynchronous trace logging F|1234|H:[a92ab94c18e1341,0,0]#tracename 428 F|1234|H:tracename 428
Trace of an integer value C|1234|H:[a92ab94c18e1341,0,0]#tracename 5678 C|1234|H:tracename 5678

In the preceding samples, the process ID is 1234 and the content is tracename. When HiTraceChain is enabled, the ID of the distributed call chain tracing task is [a92ab94c18e1341,0,0]. The asynchronous task ID is 428, and the integer value to trace is 5678.