openharmony 鸿蒙 hitracemeter-view

2025-06-12 浏览 (1)

Viewing HiTraceMeter Logs

Viewing Logs on DevEco Studio

In CPU Insight of DevEco Studio Profiler, you can display HiTraceMeter logs for analyzing the CPU usage and thread running status of applications and services and viewing the CPU consumption in a specified period. For details, see CPU Activity Analysis: CPU.

Viewing Logs Using a Command Line Tool

  1. Set up the environment for OpenHarmony 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. Dump the HiTraceMeter log file that contains the HiTraceMeter logging information in step 3.

    • Logs are printed in the window by default.
    $ hitrace --trace_dump
    
    • You can create a file to save logs in the /data/local/tmp/ directory (other directories are unavailable).
    $ hitrace --trace_dump -o /data/local/tmp/trace.ftrace
    
  5. Run the HiTrace command on the device to stop the HiTraceMeter logging service.

    $ hitrace --trace_finish
    
  6. Exit the device, enter the host, and export the HiTraceMeter text file to the current directory.

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

  8. Display and analyze the HiTraceMeter text file.

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.

User-mode Trace Format Since API Version 19

Synchronous Trace

  • Start trace: [Type identifier]|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content]|[Level and tag set]|[Custom key-value pair]

  • End trace: [Type identifier]|[Process ID]|[Level and tag set]

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

Asynchronous Trace

  • Start trace: [Type identifier]|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content]|[Asynchronous task ID]|[Level and tag set]|[Custom category name]|[Custom key-value pair]

  • End trace: [Type identifier]|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content]|[Asynchronous task ID]|[Level and tag set]

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

Integer Value Trace

  • Trace format: [Type identifier]|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content]|[Integer value]|[Level and tag set]

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

NOTE

  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 the ArkTS APIs @ohos.hiTraceMeter (Performance Tracing) or C++ APIs HiTrace.
  2. The maximum length of a user-mode trace is 512 characters. Excess characters will be truncated.

Fields

This 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).

  • Type Identifier

    The type identifier is associated with the called @ohos.hiTraceMeter (Performance Tracing) API. The mappings are as follows:

    • B: Identifies the start of a synchronous time slice trace.
    • E: Identifies the end of a synchronous time slice trace.
    • S: Identifies the start of an asynchronous time slice trace.
    • F: Identifies the end of an asynchronous time slice trace.
    • C: Identifies the integer trace event.
  • ID of the Distributed Call Chain Tracing Task

    When HiTraceChain (Distributed Tracing) is enabled, the HiTraceId information is recorded in the trace logs in the format of [ChainId,SpanId,ParentSpanId]. The HiTraceId information is separated from the content field by the hash (#) separator.

    When HiTraceChain (Distributed Tracing) is not enabled, this field is not included in the trace logs.

    • ChainId: Chain ID of HiTraceId.
    • SpanId: Span ID of HiTraceId.
    • ParentSpanId: Parent span ID of HiTraceId.

    For details about how to use HiTraceChain, see Using HiTraceChain (ArkTS/JS) and Using HiTraceChain (C/C++).

  • Content

    This field indicates the name of a trace task or an integer variable. It corresponds to the name parameter of the @ohos.hiTraceMeter (Performance Tracing) API.

  • Asynchronous Task ID

    This field and the content field are used to match the start and end of the same asynchronous trace. This field corresponds to the taskId parameter in the @ohos.hiTraceMeter (Performance Tracing) API.

  • Integer Value

    This field is the value of the integer variable to trace and is contained only in the integer value trace task. This field corresponds to count parameter in the @ohos.hiTraceMeter (Performance Tracing) API.

  • Level and Tag Set

    This field consists of the level and tag.

    The level is passed through the logging API, whose value can be D, I, C, or M. It corresponds to the level parameter in the @ohos.hiTraceMeter (Performance Tracing) API. For details about the description and mappings, seeHiTrace Output Levels.

    The tag set indicates the tags of the trace, each of which is represented by a two-digit number. The tag set can contain multiple two-digit numbers in ascending order.

    For example, in B|1314|H:TestFunction|I3062, I indicates that the logging level is Info, and 3062 indicates the tag set of the logging trace. According to the mappings between tag names and values, 30 corresponds to the ohos tag, and 62 corresponds to the app tag. For details, see HiTraceMeter Tags.

    NOTE

    1. Since API version 19, the output level can be customized. In versions earlier than API version 19, the output level is COMMERCIAL by default.

    2. In the system, a user-mode trace may have multiple tags. However, the tag set cannot be specified in the application-side user-mode trace APIs provided by @ohos.hiTraceMeter (Performance Tracing) and HiTrace, whose tag is app and tag value is 62.

  • Custom Category Name

    This field can be used for custom trace collection. It corresponds to the customCategory parameter in the @ohos.hiTraceMeter (Performance Tracing) API. The value of this field may be empty, which changes the log format. For details, see Empty Field Scenarios.

    In the trace analysis tool, asynchronous traces with the same category name in the same process are displayed in the same lane.

  • Custom Key-Value Pair

    This field is in the format of key=value. Multiple key-value pairs are separated by commas (,), for example, key1=value1,key2=value2. It corresponds to the customArgs parameter of the @ohos.hiTraceMeter (Performance Tracing) API. The value of this field may be empty, which changes the log format. For details, see Empty Field Scenarios.

    In the trace analysis tool, the parsing results of custom key-value pairs that meet the format requirements are displayed in key-value pairs.

Field Extension Rule

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

Sample Trace Logs in Different Scenarios

Trace TypeFormat When HiTraceChain is EnabledFormat When HiTraceChain is Disabled
Start of a synchronous traceB|1234|H:[a92ab94c18e1341,0,0]#tracename|M62|user=root,type=2B|1234|H:tracename|M62|user=root,type=2
End of a synchronous traceE|1234|M62E|1234|M62
Start of an asynchronous traceS|1234|H:[a92ab94c18e1341,0,0]#tracename|428|M62|appcategory01|user=root,type=2S|1234|H:tracename|428|M62|appcategory01|user=root,type=2
End of an asynchronous traceF|1234|H:[a92ab94c18e1341,0,0]#tracename|428|M62F|1234|H:tracename|428|M62
Trace of an integerC|1234|H:[a92ab94c18e1341,0,0]#tracename|5678|M62C|1234|H:tracename|5678|M62

In the preceding samples, the process ID is 1234, the content is tracename, the level is M, indicating the COMMERCIAL level, the tag set is 62, corresponding to the app tag. When HiTraceChain is enabled, the ID of the distributed call chain tracing task is [a92ab94c18e1341,0,0]. The custom key-value pairs of synchronous and asynchronous traces are both user=root,type=2. The asynchronous task ID is 428, and the custom category name is appcategory01. The value of the integer variable to trace is 5678.

NOTE

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.

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 when the customCategory and customArgs parameters of the hiTraceMeter.startAsyncTrace19+ and hiTraceMeter.startSyncTrace19+ APIs are empty.

  • Start of a synchronous trace

    • The value of customArgs is not empty:

      B|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content]|[Level and tag set]|[Custom key-value pair]
      
    • The value of customArgs is empty:

      B|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content]|[Level and tag set]
      
  • Start of an asynchronous trace

    • The values of customCategory and customArgs are not empty:

      S|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content]|[Asynchronous task ID]|[Level and tag set]|[Custom category name]|[Custom key-value pair]
      
    • The value of customCategory is empty while the value of customArgs is not empty:

      S|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content]|[Asynchronous task ID]|[Level and tag set]||[Custom key-value pair]
      
    • The value of customCategory is not empty while the value of customArgs is empty:

      S|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content]|[Asynchronous task ID]|[Level and tag set]|[Custom category name]
      
    • The values of customCategory and customArgs are empty:

      S|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content]|[Asynchronous task ID]|[Level and tag set]
      

HiTraceMeter Tags

The following is the user-mode tag collection of HiTraceMeter. The tag values are the left shift values based on the digit 1 at the actual code layer. You can run the hitrace -l command to obtain the description of the user-mode tag. always is the default tag and commercial is the custom tag for the commercial version.

NameConstant Value at the Code LayerValue
always1ULL << 000
commercial1ULL << 505
drm1ULL << 606
security1ULL << 707
animation1ULL << 909
push1ULL << 1010
virse1ULL << 1111
ffrt1ULL << 1313
musl1ULL << 1212
cloud1ULL << 1414
deviceauth1ULL << 1515
commonlibrary1ULL << 1616
hdcd1ULL << 1717
hdf1ULL << 1818
usb1ULL << 1919
interconn1ULL << 2020
dlpcre1ULL << 2121
accesscontrol1ULL << 2222
net1ULL << 2323
nweb1ULL << 2424
huks1ULL << 2525
useriam1ULL << 2626
daudio1ULL << 2727
dlsm1ULL << 2828
filemanagement1ULL << 2929
ohos1ULL << 3030
ability1ULL << 3131
zcamera1ULL << 3232
zmedia1ULL << 3333
zimage1ULL << 3434
zaudio1ULL << 3535
distributeddatamgr1ULL << 3636
mdfs1ULL << 3737
graphic1ULL << 3838
ace1ULL << 3939
notification1ULL << 4040
misc1ULL << 4141
multimodalinput1ULL << 4242
sensors1ULL << 4343
msdp1ULL << 4444
dsoftbus1ULL << 4545
rpc1ULL << 4646
ark1ULL << 4747
window1ULL << 4848
account1ULL << 4949
dscreen1ULL << 5050
dcamera1ULL << 5151
dhfwk1ULL << 5252
gresource1ULL << 5353
devicemanager1ULL << 5454
samgr1ULL << 5555
power1ULL << 5656
dsched1ULL << 5757
deviceprofile1ULL << 5858
dinput1ULL << 5959
bluetooth1ULL << 6060
accessibility1ULL << 6161
app1ULL << 6262

HiTrace Output Levels

Enum Name (C/C++)Enum Name (ArkTS/JS)ValueTrace Level
HITRACE_LEVEL_DEBUGDEBUG0D
HITRACE_LEVEL_INFOINFO1I
HITRACE_LEVEL_CRITICALCRITICAL2C
HITRACE_LEVEL_COMMERCIALCOMMERCIAL3M

User-mode Trace Format Before API version 19

Synchronous Trace

  • Start format: [Type identifier]|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content]

  • End format: [Type identifier]|[Process ID]

Asynchronous Trace

  • Start format: [Type identifier]|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content] [Asynchronous task ID]

  • End format: [Type identifier]|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content] [Asynchronous task ID]

Integer Value Trace

  • Trace format: [Type identifier]|[Process ID]|H:[ID of the distributed call chain tracing task]#[Content] [Integer value]

NOTE

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.

Sample Trace Logs in Different Scenarios Before API Version 19

Trace TypeFormat When HiTraceChain is EnabledFormat When HiTraceChain is Disabled
Start of a synchronous traceB|1234|H:[a92ab94c18e1341,0,0]#tracenameB|1234|H:tracename
End of a synchronous traceE|1234|E|1234|
Start of an asynchronous traceS|1234|H:[a92ab94c18e1341,0,0]#tracename 428S|1234|H:tracename 428
End of an asynchronous traceF|1234|H:[a92ab94c18e1341,0,0]#tracename 428F|1234|H:tracename 428
Trace of an integer valueC|1234|H:[a92ab94c18e1341,0,0]#tracename 5678C|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.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Performance Analysis Kit

harmony 鸿蒙Analyzing Application Freeze

harmony 鸿蒙Development of Application Recovery

harmony 鸿蒙Analyzing C++ Crash

harmony 鸿蒙Development of Error Manager

harmony 鸿蒙hdc

harmony 鸿蒙Event Reporting

harmony 鸿蒙Introduction to HiAppEvent

harmony 鸿蒙Subscribing to Address Sanitizer Events (ArkTS)

harmony 鸿蒙Subscribing to Address Sanitizer Events (C/C++)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/PgjSDR