26.2. 收集特定数据以监控使用 perf 环形缓冲的性能瓶颈
使用 perf
工具,您可以创建由您指定的事件触发的环形缓冲,以便仅收集您感兴趣的数据。要创建收集事件数据的 circular 缓冲,请将 --overwrite
和 --switch-output-event
选项用于 perf
。
先决条件
-
已安装
perf
用户空间工具,如安装 perf 所述。 您已在想要监测的进程或应用程序中放置了一个 uprobe:
# perf probe -x /path/to/executable -a function Added new event: probe_executable:function (on function in /path/to/executable) You can now use it in all perf tools, such as: perf record -e probe_executable:function -aR sleep 1
流程
使用 uprobe 作为触发器事件创建环形缓冲:
# perf record --overwrite -e cycles --switch-output-event probe_executable:function ./executable [ perf record: dump data: Woken up 1 times ] [ perf record: Dump perf.data.2021021012231959 ] [ perf record: dump data: Woken up 1 times ] [ perf record: Dump perf.data.2021021012232008 ] ^C[ perf record: dump data: Woken up 1 times ] [ perf record: Dump perf.data.2021021012232082 ] [ perf record: Captured and wrote 5.621 MB perf.data.<timestamp> ]
这个示例启动可执行文件并收集 cpu 周期(在
-e
选项后指定),直到perf
检测到 uprobe(在--switch-output-event
选项后指定的触发器事件)。此时,perf
采用循环缓冲区中所有数据的快照,并将其存储在由时间戳标识的唯一perf.data
文件中。此示例生成总共 2 个快照,最后的perf.data
文件是通过按 Ctrl+c 强制执行的。