Linux笔记之perf生成火焰图

Linux笔记之perf生成火焰图

—— 杭州 2024-04-01 中午

1.安装perf

sudo apt-get install linux-tools-$(uname -r) linux-tools-generic

2.下载FlameGraph工具

git clone https://github.com/brendangregg/FlameGraph.git

3.方法一:收集进程PID的性能数据

3.1.C++例程(无限循环)

例:无限循环,直到接收到用户输入才停的C++例程

#include <iostream>
#include <thread>
#include <atomic>
#include <cmath>

// Atomic flag to control the execution of the CPU-intensive task
std::atomic<bool> runTask(true);

// Function that simulates a CPU-intensive task
void cpuIntensiveTask() {
    double result = 0.0;
    while (runTask.load()) { // Loop until the flag is set to false
        for (unsigned int i = 0; i < 1000000; ++i) {
            result += std::sin(i) * std::cos(i);
        }
    }
    std::cout << "Result of the computation: " << result << std::endl;
}

int main() {
    std::cout << "Starting a long-running CPU-intensive task in a thread." << std::endl;

    // Start the CPU-intensive task in a separate thread
    std::thread worker(cpuIntensiveTask);

    // Wait for user input to end the task
    std::cout << "Press enter to stop the task..." << std::endl;
    std::cin.get();

    // Signal the task to stop and wait for the thread to finish
    runTask.store(false);
    worker.join();

    std::cout << "Task stopped." << std::endl;
    return 0;
}

3.2.编译运行,编译时开启调试符号

g++ -g -O2 cpu_task.cpp -o cpu_task -lpthread

3.3.运行该例程后找到进程PID

ps -aux | grep cpu_task

在这里插入图片描述

3.4.使用perf工具收集数据

sudo perf record -F 99 -p PID -g -- sleep 30

在这里插入图片描述

3.5.将perf记录的数据转换为火焰图需要的中间格式

sudo perf script > out.perf
或
sudo perf script -i perf.data> out.perf

在这里插入图片描述

3.6.使用FlameGraph把中间格式文件生成火焰图

./stackcollapse-perf.pl < ../out.perf | ./flamegraph.pl > out.svg

在这里插入图片描述

3.7.火焰图查看

在这里插入图片描述

4.方法二。在运行程序时记录程序的性能事件

4.1.C++例程(非无限循环)

例:非无限循环的C++例程

#include <iostream>
#include <thread>
#include <cmath>

// Function that simulates a CPU-intensive task
void cpuIntensiveTask() {
    double result = 0.0;
    for (unsigned int i = 0; i < 100000000; ++i) {
        result += std::sin(i) * std::cos(i);
    }
    std::cout << "Result of the computation: " << result << std::endl;
}

int main() {
    std::cout << "Starting a CPU-intensive task in a thread." << std::endl;
    std::thread worker(cpuIntensiveTask);
    worker.join();
    std::cout << "Completed the CPU-intensive task." << std::endl;
    return 0;
}

4.2.编译运行,编译时开启调试符号

g++ -g -O2 main2.cc -o main2 -lpthread

4.3.使用 perf 来记录程序的性能事件

sudo perf record -g ./main2

在这里插入图片描述

4.4.将perf记录的数据转换为火焰图需要的中间格式

sudo perf script -i perf.data> out.perf

4.5.使用FlameGraph把中间格式文件生成火焰图

../../FlameGraph/stackcollapse-perf.pl < out.perf | ../../FlameGraph/flamegraph.pl > out.svg

4.6.火焰图查看

在这里插入图片描述

相关推荐

  1. Linux CPU火焰

    2024-04-01 20:04:06       39 阅读
  2. Linux工具应用_PERL

    2024-04-01 20:04:06       24 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-01 20:04:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-01 20:04:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-01 20:04:06       82 阅读
  4. Python语言-面向对象

    2024-04-01 20:04:06       91 阅读

热门阅读

  1. 深入探秘Python生成器:揭开神秘的面纱

    2024-04-01 20:04:06       39 阅读
  2. 计算机网络目录

    2024-04-01 20:04:06       42 阅读
  3. ChatGPT助力学术论文写作:方法与实践

    2024-04-01 20:04:06       45 阅读
  4. PostgreSQL中json_to_record函数的神秘面纱

    2024-04-01 20:04:06       46 阅读
  5. 如何利用ChatGPT提升学术研究的效率

    2024-04-01 20:04:06       41 阅读