MediaPipe 怎么开启 tracing 与 profiling 并在 Visualizer 中分析计算器延迟?
【免费下载链接】mediapipeCross-platform, customizable ML solutions for live and streaming media.项目地址: https://gitcode.com/GitHub_Trending/med/mediapipe
如果你的 MediaPipe 图整体偏慢,想定位是哪个 calculator 在Process()里耗时最多、或者包到达后平均要等多久才开始处理,可以用框架自带的 tracer 和 profiler:tracer 记录包处理相关的计时事件(包括每次Calculator::Process调用的开始与结束时间),并以 binary protobuf 格式写出 trace 日志文件;profiler 为每个运行中的 calculator 累积一份Process调用时延直方图。整套流程是:在图配置中开启 tracing/profiling → 运行图产生.binarypb文件 → 上传到 MediaPipe Visualizer,按列查看各 calculator 的延迟统计。文档说明 tracing 与 profiling 可用平台为 Linux、Android、iOS。
准备:确保 profiling 库已链接
开启一个图的 tracing 和 profiling 需要满足两个条件:
- profiling 库已链接到框架;
- 图配置中已启用 tracing 和 profiling。
Desktop 平台默认已将 profiling 库链接进框架。其他平台在构建时加上 bazel 命令行选项即可链接:
--define MEDIAPIPE_PROFILING=1对应地,Desktop 上如需把 profiling 库从框架中排除,使用--define MEDIAPIPE_PROFILING=0。
在图配置中开启 tracing 与 profiling
把profiler_config消息加到代表该图的CalculatorGraphConfig根部,也就是你的.pbtxt图文件顶层。文档给出的示例:
profiler_config { trace_enabled: true enable_profiler: true trace_log_interval_count: 200 trace_log_path: "/sdcard/Download/" }各字段的用途(trace_log_path示例值/sdcard/Download/是文档中的 Android 路径示例,桌面平台默认写/tmp):
enable_profiler:为 true 时 profiler 在图初始化时开始 profiling。文档明确:不设置它就不会产生任何日志,是必选项。trace_enabled:为 true 时 tracer 记录并上报计时事件,这是离线 profiling 所需的 packet 级信息。trace_log_path:trace 日志的输出目录与基名。日志写到StrCat(trace_log_path, index, ".binarypb")。注意:路径末尾的斜杠是必需的,用于表示其后是目录(且该目录应当已存在)。trace_log_interval_count:每个 trace 文件包含的 interval 数。文档把上面的示例配置描述为“保持 100 秒的 timing events”。总日志时长按trace_log_interval_usec * trace_log_count * trace_log_interval_count计算。
默认情况下,日志文件命名为mediapipe_trace_<index>.binarypb,index 默认在 0 和 1 之间轮换(即保留 2 个轮换文件);每个文件默认记录 5 秒的事件(具体是每 0.5 秒一个 interval、共 10 个 interval),可通过trace_log_interval_usec和trace_log_interval_count覆盖。
完整的字段定义在 calculator.proto 的ProfilerConfig消息中,图根部的profiler_config字段见 calculator.proto。
按平台收集 trace 日志
文档给出的默认日志输出位置:
- Desktop:
/tmp目录; - Android:外部存储目录(如
/storage/emulated/0/); - iOS:通过 Xcode 获取(见下文)。
Linux / Desktop
- 按上一节在图配置中开启 tracing;
- 构建并运行你的 MediaPipe 图;
- 运行中的图会把 trace 事件写到默认目录或你指定的
trace_log_path。
Android
前提是 Android 应用拥有外部存储的写权限:
在
AndroidManifest.xml中加入:<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />授予权限:可以在应用首次启动时授予,或进入
Settings -> Apps & notifications -> <你的应用名> -> Permissions后启用Storage。把
profiler_config消息加到现有的 calculator-graph-config protobuf 中,例如现有的.pbtxt文件。连接设备后运行
adb devices确认设备已识别(文档示例输出):adb devices # should print: # List of devices attached # 805KPWQ1876505 device用
bazel build编译 Android 应用,再用adb install安装到设备。打开应用。运行中的 MediaPipe 图会向 trace 日志文件追加事件(
trace_log_path设为/sdcard/Download/时):/storage/emulated/0/Download/mediapipe_trace_0.binarypb /storage/emulated/0/Download/mediapipe_trace_1.binarypb默认每 5 秒切换到下一个 trace 文件,只保留最近 5 秒的事件。可以用 adb shell 确认文件已写到设备上:
adb shell "ls -la /storage/emulated/0/Download"把 trace 文件从设备拉下来:
adb pull /storage/emulated/0/Download/mediapipe_trace_0.binarypb文档示例输出(文档示例,实际数值不同):
/sdcard/mediapipe_trace_0.binarypb: 1 file pulled. 0.1 MB/s (6766 bytes in 0.045s)
如果需要换目录,用trace_log_path覆盖,例如"/sdcard/Download/profiles/",末尾斜杠不可省略。
iOS
在 Xcode 中打开 Window → Devices and Simulators,选择 "Devices" 标签,找到你的应用并打开 Download Container,日志位于application container/.xcappdata/AppData/Documents/。如果 Xcode 显示下载的容器内容为空,在 Finder 中右键选择 "Show Package Contents",日志位于AppData/Documents/。
在 Visualizer 中上传 trace 文件并读取延迟列
- 打开 MediaPipe Visualizer 在线站点
viz.mediapipe.dev。 - 点击右上角的 "Upload" 按钮。
- 点击 "Upload trace file"。
- 在弹出的文件选择框中选择包含 trace 信息的
.binarypb文件。 - 出现 chart view:所有 calculator 列在左侧,profiling 指标列在顶部。点击列头可对该列交替按升序/降序排序,也可以横向、纵向滚动查看更多列和更多 calculator。
手头还没有真实日志时,可以先用仓库里自带的示例 trace 文件 sample_trace.binarypb 走一遍上传流程,熟悉界面后再替换成自己的日志。
各列含义
| 列名 | 含义 |
|---|---|
| name | calculator 的名称 |
| fps | 该 calculator 平均每秒能产出的帧数,1 / (input_latency_mean + time_mean),单位 1/秒 |
| frequency | 该 calculator 每秒被要求处理包的速率,# of calls total / (last_call_time - first_call_time),单位 1/秒 |
| counter | process()被调用的次数,等于dropped + completed |
| dropped | calculator 被调用但没有产生输出的次数 |
| completed | calculator 被要求处理输入后实际产生输出的次数 |
| processing_rate | 1E+6 / time_mean,该 calculator 平均每秒能运行 process 的次数,单位 1/秒 |
| thread_count | 使用了该 calculator 的线程数 |
| time_mean | 在 calculator 内部花费的平均时间(微秒) |
| time_stddev | time_mean 的标准差(微秒) |
| time_total | 在 calculator 内部花费的总时间(微秒) |
| time_percent | 在 calculator 内部花费的时间占总时间的百分比 |
| input_latency_mean | 某次 calculator 迭代所使用的最早输入包与该 calculator 实际开始处理之间的平均延迟(微秒) |
| input_latency_stddev | input_latency_mean 的标准差(微秒) |
| input_latency_total | 累积的总 input latency(微秒) |
按列排序时,time_mean、time_total、time_percent对应 calculator 自身处理耗时的占比,input_latency_mean对应输入等待延迟;文档只给出上述定义,具体哪个 calculator 是瓶颈需要结合你上传的日志判断。
限制与可调项
- 平台范围:tracing 与 profiling 只在 Linux、Android、iOS 上可用。
enable_profiler必须为 true,否则不产生任何日志。- 默认只保留 2 个轮换文件、每文件 5 秒事件窗口,只覆盖最近 5 秒;要采集更长的时间段,增大
trace_log_interval_count(文档示例设为 200)或调整trace_log_count/trace_log_interval_usec。 - 文档标注
ProfilerConfig中多数字段属于高级设置,一般用不到。常用与高级字段可在 tracing_and_profiling.md 的 "Profiler configuration" 一节核对,包括trace_log_capacity(内存中缓冲的 trace 事件数上限,默认 20000)、trace_log_disabled(关闭落盘,默认是启用写盘)、histogram_interval_size_usec/num_histogram_intervals(Process()时延直方图的区间大小与数量,默认 1 秒单区间)、enable_stream_latency(在enable_profiler为 false 时无效)等;calculator.proto 中还定义了calculator_filter,用于把 calculator 时延直方图限制到一组 calculator 子集。
完整操作步骤与列含义见 tracing_and_profiling.md,Visualizer 的图视图等其余功能见 visualizer.md。
【免费下载链接】mediapipeCross-platform, customizable ML solutions for live and streaming media.项目地址: https://gitcode.com/GitHub_Trending/med/mediapipe
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考