UCP Trace Instructions

UCP Trace provides the ability to in-depth analysis of the scheduling logic of UCP applications by embedding trace recording on the critical path executed by UCP. When performance anomalies occur, it can quickly locate the time point of the anomaly by analyzing UCP trace.

UCP trace provides two trace backend options: Perfetto Trace and Chrome Trace. You can choose between them by setting an environment variable to meet your specific performance tracking needs.

  • Perfetto trace can retrieve ucp recorded traces, as well as system status, ftrace information, etc.

  • Chrome trace can only retrieve ucp recorded traces and is mainly used to analyze UCP's scheduling logic.

Basic Usage and Results

Perfetto Trace

Perfetto is a performance analysis tool developed and open-sourced by Google. It can collect performance data from different data sources and provides Perfetto UI for visualization and analysis. For more details about Perfetto, refer to the official Perfetto documentation.

in_process Mode Example

In in_process mode, only trace information inside the UCP process can be captured, and no Perfetto background process is required. The steps are as follows:

  1. Configure environment variables.

    # Specify the UCP Perfetto config path.
    export HB_UCP_PERFETTO_CONFIG_PATH=ucp_in_process.json
    
    # Enable Perfetto.
    export HB_UCP_ENABLE_PERFETTO=true
Note

ucp_in_process.json specifies the Perfetto configuration file ucp_in_process.cfg, in which output_path defines the output path of the trace file. Since Perfetto does not support directly overwriting an existing trace file, you must delete the file first if it already exists.

  1. Run the UCP application. hrt_model_exec is used here as an example.
Note

Because the specified file path is a relative path, the trace configuration files and scripts must be placed in the same directory as the executable. Also make sure that the environment variables are configured and the program is run in the same shell session.

./hrt_model_exec perf                      \
    --model_file resnet50_224x224_nv12.hbm \
    --frame_count 1000                     \
    --thread_num 8
  1. The generated trace is saved as ucp.pftrace, which you can open with Perfetto UI.

    ucp_in_process
  2. Click a created task on the timeline to view the complete scheduling flow from task creation to task release.

    ucp_trace_flow
  3. Common Perfetto UI operations are listed below. For more details, refer to the help page.

    OperationDescription
    w or ctrl + mouse wheel upZoom in
    s or ctrl + mouse wheel downZoom out
    a or drag the top timeline leftMove left
    d or drag the top timeline rightMove right
    ?Open help page

System Mode Example

In system mode, UCP Trace is only one of the data sources, so you need to run the corresponding tracebox commands to complete trace capture. The steps are as follows:

  1. Start the Perfetto background processes.

    # Start the trace service.
    # This only needs to be started once. If it is already running, you do not need to start it again.
    tracebox traced --background
    
    # Start the data capture service.
    # This only needs to be started once. If it is already running, you do not need to start it again.
    tracebox traced_probes --background --reset-ftrace
  2. Trigger data capture.

    # -c: specifies the Perfetto config file.
    # -o: specifies the output trace data file path.
    tracebox perfetto --txt -c ucp_system.cfg -o ucp.pftrace
  3. Open another terminal and configure UCP environment variables.

    # If HB_UCP_PERFETTO_CONFIG_PATH is not specified, system mode is used by default.
    # If it does not take effect, the current version may not support automatic selection,
    # and you need to explicitly specify the system config file.
    export HB_UCP_PERFETTO_CONFIG_PATH=ucp_system.json
    
    # Enable Perfetto.
    export HB_UCP_ENABLE_PERFETTO=true
  4. Run the UCP application. hrt_model_exec is used here as an example.

Warning

To capture complete data, make sure the perfetto process does not exit before hrt_model_exec finishes.

./hrt_model_exec perf                      \
    --model_file resnet50_224x224_nv12.hbm \
    --frame_count 1000                     \
    --thread_num 8
  1. The generated trace is saved to the output file ucp.pftrace specified by the perfetto command, and you can open it with Perfetto UI.

    ucp_system

BPU Trace Example

To demonstrate BPU trace during multi-model inference, a multi-process application example is provided here. Except for the different programs being launched, the remaining steps are the same as in the previous section.

./hrt_model_exec perf                        \
    --model_file resnet50_224x224_nv12.hbm   \
    --frame_count 50                         \
    --thread_num 1 &                         \

./hrt_model_exec perf                        \
    --model_file googlenet_224x224_nv12.hbm  \
    --frame_count 50                         \
    --thread_num 1

BPU Trace visualization requires the Horizon-customized hbperfetto tool. You can obtain this tool by contacting Horizon system software technical support. The following figure shows the effect after opening the trace file with hbperfetto:

bpu_trace_overview

The following figure shows the scheduling status of different model inference tasks presented in BPU Trace:

bpu_trace_schedule

hbperfetto supports correlation between UCP Trace and BPU Trace. The following figure shows the complete flow from creation, submission, and scheduled execution of a UCP model inference task to task completion and final release.

ucp_bpu_trace

In addition, you can query the raw BPU trace data through SQL.

select * from bpu_trace
ucp_bpu_trace

Chrome Trace

Chrome Trace supports capturing only UCP Trace and does not support multi-source capture. If you need to capture multiple data sources, use Perfetto Trace. Chrome Trace is simple and easy to use. It records traces in text logs and does not rely on additional third-party libraries or tools. If you only care about UCP scheduling logic, you can use Chrome Trace for capture.

Usage Example

  1. Configure environment variables.

    # Disable Perfetto.
    export HB_UCP_ENABLE_PERFETTO=false
    
    # Set the UCP Trace log level to 0.
    export HB_UCP_TRACE_LOG_LEVEL=0
    
    # Set the path for saving UCP logs.
    export HB_UCP_LOG_PATH=ucp_log.txt
Note

Before starting a new capture, it is recommended to delete the old log file first to avoid interference from old data.

  1. Run the UCP application. hrt_model_exec is used here as an example.

    ./hrt_model_exec perf                      \
        --model_file resnet50_224x224_nv12.hbm \
        --frame_count 1000                     \
        --thread_num 8
  2. Run the trace capture script.

    After trace log capture is complete, run catch_trace.sh provided in the UCP release package to convert the raw trace log into a JSON-format trace file.

    # -i: specifies the input trace log file.
    # -o: specifies the output JSON trace log file.
    ./catch_trace.sh -i ucp_log.txt -o ucp_trace_task.json
    
    # By default, the trace is visualized from the task perspective.
    # You can also switch to the thread perspective.
    # -m: specifies the conversion mode. task: task perspective (default), thread: thread perspective.
    ./catch_trace.sh -m thread -i ucp_log.txt -o ucp_trace_thread.json
  3. Open ucp_trace_task.json and ucp_trace_thread.json with Perfetto UI.

    Perfetto UI opening ucp_trace_thread.json:

    chrome_thread

Configuration and Tooling

Tool and Configuration File Locations

UCP Trace tools and configuration files are located in samples/ucp_tutorial/tools. The directory structure is as follows:

  tools/
  └── trace                          # trace tools
    ├── catch_trace.sh               # chrome trace capture script
    ├── configs                      # reference configuration files
    │   ├── ucp_bpu_trace.cfg        # Perfetto config file for enabling BPU trace
    │   ├── ucp_dsp_trace.cfg        # Perfetto config file for enabling DSP trace
    │   ├── ucp_in_process.cfg       # Perfetto config file for in_process mode
    │   ├── ucp_in_process.json      # UCP config file for in_process mode
    │   ├── ucp_system.cfg           # Perfetto config file for system mode
    │   └── ucp_system.json          # UCP config file for system mode

Environment Variables

Environment VariableValue RangeDefaultDescription
HB_UCP_ENABLE_PERFETTOtrue, falsefalseWhether to enable Perfetto Trace. true enables it, false disables it. Disabled by default.
HB_UCP_PERFETTO_CONFIG_PATHPerfetto config path""Specifies the path to the Perfetto config file. The default is empty. If this environment variable is not configured, system mode is used by default.
HB_UCP_TRACE_LOG_LEVEL[0, 6]6Specifies the UCP Trace log level. The default is 6, which means no output.
HB_UCP_USE_ALOGtrue, falsefalseWhether to enable the alog sink. true enables it, false disables it. Disabled by default. After enabling the alog sink, logs are written to the alog buffer and captured with logcat, while terminal log output is disabled.
Note

Perfetto Trace has higher priority. If export HB_UCP_ENABLE_PERFETTO=true and export HB_UCP_TRACE_LOG_LEVEL=0 are both set, only Perfetto Trace is started and the UCP Trace log level is ignored.

Configuration File Parameters

UCP Trace Configuration File Parameters

ParameterData TypeDescriptionRelated Parameter
backendstring

Function:

  • in_process indicates in-process mode, where Perfetto Trace is directly saved to a file within the process.
  • system indicates system mode, where trace capture is handled centrally by the background services traced and traced_probes.
Value Range: "in_process", "system"

None.
trace_configstring

Function: This parameter is a configuration file in protobuf text format.
Value Range: Path to the Perfetto configuration file.

This parameter takes effect only when backend is set to "in_process".
Note

The UCP Trace configuration file is optional. If your application has already initialized Perfetto, you only need to set export HB_UCP_ENABLE_PERFETTO=true to enable Perfetto.

Long Trace Configuration File Parameters

By default, Perfetto stores trace data in an in-memory buffer and dumps the buffered data to a file only when the trace session ends. If the volume of trace data exceeds the buffer capacity, data integrity cannot be guaranteed.

Perfetto supports periodically writing buffered data to a file. You only need to add the following fields to the trace configuration file.

ParameterData TypeDescription
write_into_filebooltrue enables periodic log writing to a file. The default is false, which means disabled.
file_write_period_msuint32Sets the file write interval. The default value is 5s. You can set an appropriate interval based on the amount of data generated per second and the trace buffer size.
max_file_size_bytesuint64Sets the maximum size of the trace file. When this value is exceeded, the trace stops automatically. Unlimited by default.

Configuration File Templates

UCP Trace Configuration File Template

Used to configure how UCP uses Perfetto. You can specify it through the environment variable HB_UCP_PERFETTO_CONFIG_PATH.

in_process Mode

ucp_in_process.json
{
    "backend": "in_process",
    "trace_config": "ucp_in_process.cfg"
}

System Mode

ucp_system.json
{
    "backend": "system"
}
Note

When backend is set to system, there is no need to specify trace_config for UCP separately.

Perfetto Configuration File Template

For details about Perfetto configuration files, refer to the Perfetto TraceConfig Reference. UCP provides reference configuration files ucp_in_process.cfg and ucp_system.cfg, which you can modify according to your application scenario.

ucp_in_process.cfg
# Enable periodic flushing of the trace buffer into the output file.
write_into_file: true

# Output file path
output_path: "ucp.pftrace"

# Sampling duration: 10s
duration_ms: 10000

# Writes the userspace buffer into the file every 2.5 seconds.
file_write_period_ms: 2500

buffers {
  # buffer size
  size_kb: 65535
  # DISCARD: no new sampling data will be stored when the storage is full.
  # RING_BUFFER: old sampling data will be discarded and new data will be stored when the storage is full.
  fill_policy: RING_BUFFER
}

# UCP data source
data_sources: {
    config {
        name: "track_event"
        track_event_config {
           enabled_categories: "dnn"
        }
    }
}

BPU Trace Configuration File Template

In system mode, BPU trace capture is supported. You only need to add the BPU trace data source to the Perfetto configuration file. The ucp_bpu_trace.cfg file already includes the BPU trace data source by default, and the relevant configuration is shown below:

ucp_bpu_trace.cfg
data_sources: {
    config {
        name: "linux.sys_stats"
        sys_stats_config {
            bputrace_period_ms: 500
        }
    }
}

The bputrace_period_ms is used to set the interval for reading BPU Trace. You can adjust this parameter according to the actual usage scenario. When the BPU load is high, you can appropriately shorten the interval to avoid trace data being overwritten due to mismatched read and write speeds.

Note

Currently, BPU Trace does not support dynamic enabling at runtime. If you need to capture BPU Trace data in real time while the application is running, you must enable this feature manually before the application starts. The command is: echo 1 > /sys/devices/system/bpu/bpu0/trace. Before executing this command, make sure the value of /sys/devices/system/bpu/bpu0/power_enable is 0. If it is not 0, run echo 0 > /sys/devices/system/bpu/bpu0/power_enable first.

DSP Trace Configuration File Template

In system mode, DSP trace capture is supported. You only need to add the DSP trace data source to the Perfetto configuration file. The ucp_dsp_trace.cfg file already includes the DSP trace data source by default, and the valid configuration is shown below:

ucp_dsp_trace.cfg
data_sources {
  config {
    name: "linux.sys_stats"
    sys_stats_config {
      dsptrace_period_ms: 500
    }
  }
}

dsptrace_period_ms is used to set the interval for reading DSP Trace. You can adjust this parameter according to the actual usage scenario. When the DSP load is high, you can appropriately shorten the interval to avoid trace data being overwritten due to mismatched read and write speeds.

Note

Currently, DSP Trace does not support dynamic enabling at runtime. If you need to capture DSP Trace data in real time while the application is running, you must enable this feature manually before the application starts. The reference commands are as follows:

# stop vdsp fw
echo stop > /sys/class/remoteproc/remoteproc1/state

# stop or start dsp trace
echo 0 > /sys/devices/virtual/misc/vdsp0/vdsp_ctrl/trace_switch
echo 1 > /sys/devices/virtual/misc/vdsp0/vdsp_ctrl/trace_switch

# check dsp trace status
cat /sys/devices/virtual/misc/vdsp0/vdsp_ctrl/trace_switch

UCP Trace Record Points

UCP embeds trace records in application APIs and internal key scheduling paths, including task trace records and operator trace records.

Task Trace Record Points

NameDescription
hbDNNInferCreate model inference task
hbVPxxxCreate vision processing task
hbHPLxxxCreate high-performance computing task
hbUCPSubmitTaskSubmit task
${TaskType}::WaitWait for task completion
TaskSetDoneNotify task completion
hbUCPReleaseTaskRelease task

Operator Trace Record Points

NameDescription
SubmitOpSubmit operator
OpInferOperator starts execution
OpFnishOperator finishes execution

Common Data Sources

Event TypeData Source NameCustomized by hbperfettoConfigurationDescription
Application track eventtrack_eventNotrack_event_configUsed to capture data instrumented through the Perfetto SDK API in applications. enabled_categories is used to specify enabled categories. For example, dnn corresponds to UCP Trace.
BPU tracelinux.sys_statsYessys_stats_configRecords BPU trace information and uses bputrace_period_ms to set the sampling period.
DSP tracelinux.sys_statsYessys_stats_configRecords DSP trace information and uses dsptrace_period_ms to set the sampling period.