使用 Linux Perf 分析 Node.js 性能

Linux Perf 提供底层 CPU 性能分析,能显示 JavaScript、原生代码以及操作系统层面的调用栈帧。

操作方法

通常可以通过 linux-tools-common 软件包获得 Linux Perf。使用 --perf-basic-prof 或 --perf-basic-prof-only-functions 启动 Node.js 应用,即可支持 perf_events。

--perf-basic-prof 会持续写入文件 /tmp/perf-PID.map,可能导致磁盘占用无限增长。如果需要避免这种情况,可使用 linux-perf 模块,或改用 --perf-basic-prof-only-functions。

两者的主要区别是:--perf-basic-prof-only-functions 产生的输出更少,可用于生产环境的性能分析。

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

然后按需要的采样频率记录事件:

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

在此阶段,可以对应用进行负载测试,以产生更多记录,使分析更加可靠。完成后,向 perf 进程发送 SIGINT(Ctrl-C)结束记录。

/tmp 目录中会生成一个映射文件,通常名为 /tmp/perf-PID.map;在上面的示例中,它是 /tmp/perf-3870.map,包含被调用函数的映射信息。

要将结果汇总到指定文件中,执行:

$ sudo perf script > perfs.out
$ cat ./perfs.out
node 3870 25147.878454:          1 cycles:
        ffffffffb5878b06 native_write_msr+0x6 ([kernel.kallsyms])
        ffffffffb580d9d5 intel_tfa_pmu_enable_all+0x35 ([kernel.kallsyms])
        ffffffffb5807ac8 x86_pmu_enable+0x118 ([kernel.kallsyms])
        ffffffffb5a0a93d perf_pmu_enable.part.0+0xd ([kernel.kallsyms])
        ffffffffb5a10c06 __perf_event_task_sched_in+0x186 ([kernel.kallsyms])
        ffffffffb58d3e1d finish_task_switch+0xfd ([kernel.kallsyms])
        ffffffffb62d46fb __sched_text_start+0x2eb ([kernel.kallsyms])
        ffffffffb62d4b92 schedule+0x42 ([kernel.kallsyms])
        ffffffffb62d87a9 schedule_hrtimeout_range_clock+0xf9 ([kernel.kallsyms])
        ffffffffb62d87d3 schedule_hrtimeout_range+0x13 ([kernel.kallsyms])
        ffffffffb5b35980 ep_poll+0x400 ([kernel.kallsyms])
        ffffffffb5b35a88 do_epoll_wait+0xb8 ([kernel.kallsyms])
        ffffffffb5b35abe __x64_sys_epoll_wait+0x1e ([kernel.kallsyms])
        ffffffffb58044c7 do_syscall_64+0x57 ([kernel.kallsyms])
        ffffffffb640008c entry_SYSCALL_64_after_hwframe+0x44 ([kernel.kallsyms])
....

原始输出可能不易理解,因此通常会根据原始文件生成火焰图,以改善可视化效果。

Node.js 火焰图示例(原文图片)
Node.js 火焰图示例(原文图片)

要根据这些结果生成火焰图,请从 该教程 的第 6 步开始操作。

perf 的输出并非专门为 Node.js 设计,因此可能无法妥善处理 Node.js 中 JavaScript 代码的优化方式。详情参阅 perf 输出问题。

相关链接

来源:Node.js 官方学习文档。中文翻译;代码和原始命令输出保持原文,未在本地执行。映射文件由启用相应选项的 Node.js/V8 进程写出;上述输出为官方示例。原图保留官方地址,尚未本地物化。

原文许可:MIT;Node.js 项目贡献者。

MIT License

Copyright Node.js Website WG contributors. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容