跳到内容

使用 Linux Perf

【Using Linux Perf】

Linux Perf 提供针对 JavaScript、本地以及操作系统级别的低级 CPU 分析。

重要:本教程仅适用于 Linux。

如何

【How To】

Linux Perf 通常可以通过 linux-tools-common 软件包获得。通过 --perf-basic-prof--perf-basic-prof-only-functions,我们能够启动一个支持 perf_events 的 Node.js 应用。

【Linux Perf is usually available through the linux-tools-common package. Through either --perf-basic-prof or --perf-basic-prof-only-functions we are able to start a Node.js application supporting perf_events.】

--perf-basic-prof 将始终写入文件(/tmp/perf-PID.map),这可能导致磁盘无限增长。如果这是一个问题,可以使用模块:linux-perf--perf-basic-prof-only-functions

两者之间的主要区别在于,--perf-basic-prof-only-functions 产生的输出较少,它是生产环境分析的可行选项。

【The main difference between both is that --perf-basic-prof-only-functions produces less output, it is a viable option for production profiling.】

# Launch the application an get the PID
$ node --perf-basic-prof-only-functions index.js &
[1] 3870

然后根据所需频率记录事件:

【Then record events based in the desired frequency:】

$ sudo perf record -F 99 -p 3870 -g

在此阶段,你可能希望在应用中使用负载测试,以生成更多记录以进行可靠的分析。作业完成后,通过向命令发送 SIGINT(Ctrl-C)来关闭 perf 进程。

【In this phase, you may want to use a load test in the application in order to generate more records for a reliable analysis. When the job is done, close the perf process by sending a SIGINT (Ctrl-C) to the command.】

perf 会在 /tmp 文件夹中生成一个文件,通常名为 /tmp/perf-PID.map(在上例中为 /tmp/perf-3870.map),其中包含每个被调用函数的跟踪信息。

【The perf will generate a file inside the /tmp folder, usually called /tmp/perf-PID.map (in above example: /tmp/perf-3870.map) containing the traces for each function called.】

要将这些结果汇总到特定文件中,请执行:

【To aggregate those results in a specific file execute:】

$ sudo perf script > perfs.out

原始输出可能有点难以理解,因此通常会使用原始文件来生成火焰图,以便更好地可视化。

【The raw output can be a bit hard to understand so typically the raw file is used to generate flamegraphs for a better visualization.】

Example nodejs flamegraph

要从此结果生成火焰图,请按照第 6 步开始的本教程操作。

【To generate a flamegraph from this result, follow this tutorial from step 6.】

因为 perf 输出不是 Node.js 特定的工具,它可能在处理 Node.js 中 JavaScript 代码优化时出现问题。有关更多参考,请参阅 perf 输出问题

【Because perf output is not a Node.js specific tool, it might have issues with how JavaScript code is optimized in Node.js. See perf output issues for a further reference.】

有用的链接

【Useful Links】

阅读时间
3 min
目录
  1. 如何
  2. 有用的链接