惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
The Exploit Database - CXSecurity.com
The Hacker News
The Hacker News
Security Latest
Security Latest
Attack and Defense Labs
Attack and Defense Labs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Scott Helme
Scott Helme
H
Heimdal Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
WordPress大学
WordPress大学
雷峰网
雷峰网
L
LangChain Blog
Y
Y Combinator Blog
I
Intezer
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
W
WeLiveSecurity
P
Privacy International News Feed
V
Visual Studio Blog
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
AI
AI
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
Martin Fowler
Martin Fowler
K
Kaspersky official blog
U
Unit 42
S
Schneier on Security
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
T
Tor Project blog
aimingoo的专栏
aimingoo的专栏
Engineering at Meta
Engineering at Meta
G
GRAHAM CLULEY
美团技术团队
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
P
Proofpoint News Feed
爱范儿
爱范儿
F
Fortinet All Blogs
有赞技术团队
有赞技术团队

博客园 - xcywt

IT人备考心得分享 一个大龄程序员的回乡记 一种刚接触的语法。只在linux可用 固件打包流程 malloc底层实现以及和new的比较 C++异步调用 future async promise packaged_task 如果在单例模式中返回share_ptr ??? 记录一道面试题(哈希表 稀疏矩阵) Qt实现自定义控件-按钮 一个线程池的例子 C++ 条件变量condition_variable的例子 C++中share_ptr中循环引用的问题 C++14的一些新特性 C++11的一些特性 ubuntu编译grpc & protobuf 一个cmakelist的例子(自动处理多个proto) Linux下eCal测试计划及进度记录 windows编译ecal 记录一次重装gitlab
perf笔记
xcywt · 2024-09-18 · via 博客园 - xcywt

记录一下这个工具,感觉还挺好用的。参考:

如何读懂火焰图? - 阮一峰的网络日志 (ruanyifeng.com)

perf的基本使用方法-菜鸟笔记 (coonote.com)

使用 Perf 和火焰图分析 CPU 性能 | Senlin's Blog (senlinzhan.github.io)

安装:

# Ubuntu系统
sudo apt-get install linux-tools-common linux-tools-generic linux-tools-`uname -r`
# Centos系统
yum install -y perf

查看版本:

$ perf --version
perf version 5.4.44

测试:

#include <stdio.h>
#include <stdlib.h>

void long_test() {
    int i, j;
    for (i = 0; i < 1000000; i++) j = i;
}

void foo2() {
    int i;
    for (i = 0; i < 10; i++) long_test();
}

void foo1() {
    int i;
    for (i = 0; i < 100; i++) long_test();
}
int main(void) {
    foo1();
    foo2();
}

采样60s,会生成一个perf.data文件。

# 方式一:对一个正在运行的进程,进行采样
perf record -p PID -g -- sleep 60
# 方式二:全新运行一个二进制文件main,进行采样
sudo perf record -F 99 -g ./main -- sleep 60

perf stat:

生成火焰图:

利用这个开源工具可以将报告生成可视化的svg图片,更容易查看对应的CPU开销时间和调用栈深度。

git clone --depth 1 https://github.com/brendangregg/FlameGraph.git
# 安装perl
yum install -y perl
# Ubuntu
apt install  -y perl

生成火焰图的脚本

对二进制文件main进行10秒的采样,然后生成火焰图

非root用户需要加sudo

perf record -g ./main  sleep 10
perf script -i perf.data &> perf.unfold
./FlameGraph/stackcollapse-perf.pl perf.unfold &> perf.folded
./FlameGraph/flamegraph.pl perf.folded > perf.svg

其他用法:

其他工具: