ATrace 使用外部 crate tracing_android_trace 对 HAR 执行性能跟踪。
为 HAR 启用跟踪
如需为参考 harry-app 启用性能跟踪,请启用 Cargo 功能 harry-app-platform-specific/tracing-android。
# crates/reference/harry-app/Cargo.toml
harry-sdv = ["harry-app-platform-specific/tracing-android", ...]
向 HAR 添加自定义跟踪记录
let custom_trace = tracing::info_span!("custom_trace_span").entered()
// code here will be traced
drop(custom_trace)

图 1. Perfetto 界面,显示了来自 harry 进程 (HAR) 的示例跟踪记录
DriverUI 的性能跟踪
DriverUI 使用 Android 系统跟踪,无需额外配置。
向 DriverUI 添加自定义跟踪记录
您可以按照开发者指南中的说明,向 Kotlin 代码添加自定义事件。
收集跟踪记录
我们建议使用 Torq 在 AAOS 上收集跟踪记录。
使用 Torq
Torq 是一款 CLI 工具,用于在 AAOS 和 SDV 上收集性能跟踪记录。
克隆 Torq 代码库。
按照代码库中的说明进行构建,然后运行:
export ANDROID_SERIAL=<your_device_serial>
# Collect a 7 second trace
torq -d 7000
收集两个虚拟机的统一性能跟踪记录
由于 Display Safety 在两个虚拟机上运行,因此同时从两个虚拟机收集跟踪记录有助于找出问题。
- Torq 通过 Perfetto 的
traced_relay支持统一跟踪记录。 - 如需捕获统一跟踪记录,请先确定两个虚拟机的 cid:
# Get the cid for SDV Media VM. For example: 3
adb -s <sdv_media_vm_serial> shell getprop ro.sdv.vsock_local_cid
# Get the cid for SDV IVI VM. For example: 4
adb -s <sdv_ivi_vm_serial> shell getprop ro.sdv.vsock_local_cid
在此示例中,使用 SDV-Media 虚拟机 (cid=3) 作为中继。
# Configure your VMs for unified tracing
torq vm configure --primary <sdv_ivi_vm_serial> --primary-cid 4 --secondary <sdv_media_vm_serial>
# Collect a 7 second trace from the SDV IVI VM, it will now collect a unified trace
torq --serial <sdv_ivi_vm_serial> -d 7000
例如:

图 2. Perfetto 界面显示了一个统一跟踪记录示例,其中包含两个虚拟机中的两个进程:harry (HAR) 和 com.android.car.driverui (DriverUI)。
收集启动跟踪记录
如需收集启动跟踪记录以分析启动性能,您必须启用一些标志。
为启动跟踪记录设置 SDV 专用持久性标志。
export ANDROID_SERIAL=<your_device_serial> adb root adb shell setprop persist.debug.sdv.boottrace 1 adb shell setprop persist.debug.perfetto.boottrace 1设置内核 cmdline 参数
androidboot.fastboot.boottrace=enabled。
# For Cuttlefish, set when creating CVD
cvd create -extra_kernel_cmdline=androidboot.fastboot.boottrace=enabled
使用 record_android_trace
执行以下操作:
# Set your adb device serial
export ANDROID_SERIAL=<your_device_serial>
# Run record_android_trace from the Android repo
cd /<your_repo_path>
build/envsetup.sh
$ANDROID_BUILD_TOP/external/perfetto/tools/record_android_trace --config $ANDROID_BUILD_TOP/system/software_defined_vehicle/core_services/samples/tracing/config/trace_cfg.pbtx