19.2. 通过 perf circular buffer 收集特定数据以监控性能瓶颈
使用 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 周期,在 option
-e选项后指定,直到perf检测到uprobe,该触发器事件在--switch-output-event选项后指定的触发器事件。此时,perf采用循环缓冲区中所有数据的快照,并将其存储在由时间戳标识的唯一perf.data文件中。本例生成总计 2 个快照,最后的perf.data文件是通过按 Ctrl+C 强制的。