# record-Valgrind-perf **Repository Path**: cpp-thinkmore/record-valgrind-perf ## Basic Information - **Project Name**: record-Valgrind-perf - **Description**: 记录简单的Valgrind和perf进行cpp程序内存泄漏和性能分析 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-03 - **Last Updated**: 2026-01-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Valgrind 和 perf 是 Linux 下非常强大的性能分析和调试工具,分别擅长**内存问题检测**(如内存泄漏、越界访问)和**CPU 性能剖析**(如热点函数、缓存命中率、分支预测等)。涵盖: 1. **安装准备** 2. **用 Valgrind 检测内存泄漏** 3. **用 perf 分析 CPU 瓶颈** 4. **结合使用建议与常见技巧** --- ## 一、安装准备(以 CentOS7 为例) ```bash sudo yum update sudo yum install valgrind linux-tools-common linux-tools-generic linux-tools-$(uname -r) yum install -y perf ``` > 💡 `perf` 属于 Linux 内核工具包,需安装对应内核版本的 `linux-tools`。 验证安装: ```bash valgrind --version perf --version ``` --- ## 二、用 Valgrind 检测内存泄漏 ### 1. 编译程序(带调试信息) 确保你的程序用 `-g` 编译,以便 Valgrind 能显示源码行号: ```bash g++ -g -O0 -o myapp myapp.cpp ``` > ⚠️ 建议关闭优化(`-O0`),否则某些变量可能被优化掉,影响调试。 ### 2. 运行 Valgrind 的 memcheck 工具(默认) ```bash valgrind --leak-check=full \ --show-leak-kinds=all \ --track-origins=yes \ ./myapp ``` #### 参数说明: - `--leak-check=full`:详细报告每个泄漏块。 - `--show-leak-kinds=all`:显示所有类型泄漏(definitely, indirectly, possibly 等)。 - `--track-origins=yes`:追踪未初始化值的来源(会降低速度,但很有用)。 ### 3. 解读输出示例 ```text ==12345== HEAP SUMMARY: ==12345== in use at exit: 40 bytes in 1 blocks ==12345== total heap usage: 2 allocs, 1 frees, 72 bytes allocated ==12345== ==12345== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==12345== at 0x4841888: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==12345== by 0x109152: main (myapp.cpp:5) ``` 👉 `definitely lost` 表示明确的内存泄漏(最严重)。 ### 4. 抑制误报(可选) 如果第三方库有“良性”泄漏,可用 suppressions 文件: ```bash valgrind --suppressions=my.supp ./myapp ``` --- ## 三、用 perf 分析 CPU 瓶颈 ### 1. 编译程序(建议保留符号) ```bash g++ -g -O2 -fno-omit-frame-pointer -o myapp myapp.cpp ``` > 🔑 `-fno-omit-frame-pointer` 对 perf 的调用栈回溯非常重要! ### 2. 基本性能剖析(采样运行时) ```bash # 记录 CPU 事件(默认是 cycles) perf record ./myapp # 查看报告 perf report ``` 在 `perf report` 界面中: - 使用方向键浏览 - 按 `Enter` 钻取到具体函数 - 按 `a` 查看汇编(需 `-g`) - 按 `q` 退出 ### 3. 更详细的分析命令 | 目标 | 命令 | |------|------| | 实时 top 式监控 | `perf top` | | 统计事件次数 | `perf stat ./myapp` | | 按函数汇总耗时 | `perf record -g ./myapp && perf report -g` | | 分支预测失败分析 | `perf record -e branches,branch-misses ./myapp` | | 缓存命中率 | `perf stat -e cache-references,cache-misses ./myapp` | ### 4. 生成火焰图(高级可视化) 需要额外工具 [`FlameGraph`](https://github.com/brendangregg/FlameGraph): ```bash git clone https://github.com/brendangregg/FlameGraph.git perf record -g ./myapp perf script | ./FlameGraph/stackcollapse-perf.pl | ./FlameGraph/flamegraph.pl > profile.svg ``` 然后用浏览器打开 `profile.svg`,直观看到热点函数。 --- ## 四、实战小例子 ### 示例代码 `leak.cpp` ```cpp #include #include int main() { int* p = new int(42); std::vector v(1000000, 1); // 模拟计算 for (size_t i = 0; i < v.size(); ++i) { v[i] *= 2; } // 忘记 delete p → 内存泄漏! return 0; } ``` ### 步骤: 1. 编译: ```bash g++ -g -O0 -fno-omit-frame-pointer -o leak leak.cpp ``` 2. Valgrind 检测泄漏: ```bash valgrind --leak-check=full --show-leak-kinds=all ./leak ``` 3. Perf 分析 CPU: ```bash perf record -g ./leak perf report -g ``` 你会看到: - Valgrind 报告 “definitely lost: 4 bytes” - Perf 显示热点在 `main` 中的循环(或 `std::vector` 构造) --- ## 五、注意事项 & 技巧 | 工具 | 注意事项 | |------|--------| | **Valgrind** | - 运行速度慢(10~50 倍减速)- 不能检测静态/栈内存问题- 多线程程序也能分析,但更慢 | | **perf** | - 需要 root 权限才能访问某些硬件事件(但基本功能无需 root)- `-g` 和 `-fno-omit-frame-pointer` 很关键- 不适用于极短运行程序(采样不到) | --- ## 六、进一步学习资源 - Valgrind 官方文档:https://valgrind.org/docs/ - perf Wiki:https://perf.wiki.kernel.org/ - Brendan Gregg 的性能分析指南(强烈推荐):http://www.brendangregg.com/ ---