C++ 性能分析的实战指南(gperftools工具)[建议收藏]

使用gperftools进行 C++ 性能分析的实战指南

在软件开发过程中,性能优化是一项至关重要的任务,尤其是对于复杂的 C++ 应用程序来说。gperftools 是一套功能强大的性能分析工具,它为 C++ 开发者提供了分析 CPU 使用和内存使用的有效手段。在这篇博客中,我们将详细介绍如何在 C++ 项目中使用 gperftools 来识别和解决性能瓶颈。

一、编译安装 gperftools

首先,我们需要安装 gperftools。以下步骤将指导您完成下载和安装过程:

1. 下载源代码:
wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.15/gperftools-2.15.tar.gz
tar -xvf gperftools-2.15.tar.gz
2. 编译和安装:
cd gperftools-2.15
./configure --prefix=$PWD/build --enable-shared=no --enable-static=yes --enable-libunwind
make -j8
make install

注意:请确保你的 CMake 版本在 3.12 或以上。

二、编写测试程序

安装完成后,我们可以编写一个简单的 C++ 程序来测试 gperftools 的功能。以下是编译程序所需的命令:

g++ -o main hot.cpp -I./include -L ./lib -lprofiler -ltcmalloc -lpthread

这里,我们假设源文件名为 hot.cpp

三、使用 gperftools 代码示例

在 C++ 程序中,可以通过以下方式集成 gperftools:

#include <gperftools/profiler.h>
#include <gperftools/heap-profiler.h>

int main() {
    ProfilerStart("cpu-profiler.prof");
    HeapProfilerStart("memory-profiler");

    // 你的代码

    ProfilerStop();
    HeapProfilerDump("test");
    HeapProfilerStop();
    return 0;
}

在上面的例子中,我们使用 ProfilerStartHeapProfilerStart 开启 CPU 和内存分析,执行我们需要测试的代码,然后使用 ProfilerStopHeapProfilerStop 停止分析。

四、查看分析结果

生成的分析文件可以使用 gperftools 提供的 pprof 工具来查看:

pprof ./main ./cpu-profiler.prof --text
pprof ./main ./memory-profiler.0001.heap --text

这些命令将输出文本形式的性能分析报告,帮助理解程序中的热点。

五、一份实际代码实例及实操

1.代码实例

假设应用程序包含了大量的数据库操作和数据处理,可以通过 gperftools 来识别哪些函数占用了最多的 CPU 时间或内存:

#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <chrono>
#include <random>
#include <memory>
#include <functional>

// 数据库模拟类
class Database {
private:
    std::mutex db_mutex;

public:
    int getData(int id) {
        std::lock_guard<std::mutex> lock(db_mutex);
        std::this_thread::sleep_for(std::chrono::milliseconds(10)); // 模拟数据库延迟
        return id * id; // 简单计算,模拟数据处理
    }

    void updateData(int id, int value) {
        std::lock_guard<std::mutex> lock(db_mutex);
        std::this_thread::sleep_for(std::chrono::milliseconds(15)); // 模拟写入延迟
        // 实际更新操作省略
    }
};

// 请求类
struct Request {
    int id;
    int value;
    bool isRead; // true if read, false if write
};

// 处理请求的工作线程
class Worker {
public:
    Worker(std::shared_ptr<Database> db) : db_(db) {}

    void operator()(const Request& request) {
        auto start = std::chrono::high_resolution_clock::now();
        if (request.isRead) {
            int result = db_->getData(request.id);
            std::cout << "Read request for ID " << request.id << ": " << result << std::endl;
        } else {
            db_->updateData(request.id, request.value);
            std::cout << "Write request for ID " << request.id << " with new value " << request.value << std::endl;
        }
        auto end = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double, std::milli> elapsed = end - start;
        std::cout << "Request ID " << request.id << " processed in " << elapsed.count() << " ms" << std::endl;
    }

private:
    std::shared_ptr<Database> db_;
};

#include <gperftools/profiler.h>
#include <gperftools/heap-profiler.h>


int main() {
    ProfilerStart("cpu-profiler.prof");
    HeapProfilerStart("mempry-profiler");


    std::shared_ptr<Database> db = std::make_shared<Database>();
    std::vector<std::thread> workers;
    std::vector<Request> requests;

    // 生成请求
    for (int i = 0; i < 1000; ++i) {
        requests.emplace_back(Request{i, i * 100, i % 2 == 0});
    }

    // 创建并启动工作线程
    for (auto& req : requests) {
        workers.emplace_back(Worker(db), std::ref(req));
    }

    // 等待所有线程完成
    for (auto& worker : workers) {
        if (worker.joinable()) {
            worker.join();
        }
    }


    ProfilerStop();
    HeapProfilerDump("test");
    HeapProfilerStop();


    return 0;
}
2.操作命令
[root@iZj6c0il0t6l26ze71vq4cZ build]# ls
bin  hot.cpp  include  lib  share
[root@iZj6c0il0t6l26ze71vq4cZ build]# g++ -o main hot.cpp -I./include -L ./lib -lprofiler -ltcmalloc -lpthread
[root@iZj6c0il0t6l26ze71vq4cZ build]# ls
bin  hot.cpp  include  lib  main  share
[root@iZj6c0il0t6l26ze71vq4cZ build]# ./main > /dev/null
Starting tracking the heap
PROFILE: interrupts/evictions/bytes = 9/0/672
Dumping heap profile to mempry-profiler.0001.heap (test)
[root@iZj6c0il0t6l26ze71vq4cZ build]# ls
bin  cpu-profiler.prof  hot.cpp  include  lib  main  mempry-profiler.0001.heap  share
[root@iZj6c0il0t6l26ze71vq4cZ build]# bin/pprof ./main ./cpu-profiler.prof --text
Using local file ./main.
Using local file ./cpu-profiler.prof.
Total: 9 samples
       2  22.2%  22.2%        2  22.2% __GI___munmap
       2  22.2%  44.4%        2  22.2% __pthread_create_2_1
       1  11.1%  55.6%        1  11.1% MallocHook::InvokeDeleteHookSlow (inline)
       1  11.1%  66.7%        1  11.1% SpinLock::Lock (inline)
       1  11.1%  77.8%        1  11.1% __futex_abstimed_wait_common
       1  11.1%  88.9%        1  11.1% std::forward
       1  11.1% 100.0%        1  11.1% std::locale::id::_M_id@@GLIBCXX_3.4
       0   0.0% 100.0%        1  11.1% DeleteHook
       0   0.0% 100.0%        2  22.2% MallocHook::InvokeDeleteHook (inline)
       0   0.0% 100.0%        2  22.2% MallocHook::InvokeDeleteHookSlow
       0   0.0% 100.0%        1  11.1% RecordFree (inline)
       0   0.0% 100.0%        1  11.1% SpinLockHolder::SpinLockHolder (inline)
       0   0.0% 100.0%        1  11.1% Worker::operator
       0   0.0% 100.0%        2  22.2% __GI___nptl_deallocate_stack
       0   0.0% 100.0%        3  33.3% __clone3
       0   0.0% 100.0%        3  33.3% __gnu_cxx::new_allocator::construct
       0   0.0% 100.0%        6  66.7% __libc_start_call_main
       0   0.0% 100.0%        6  66.7% __libc_start_main_alias_2
       0   0.0% 100.0%        2  22.2% __nptl_free_stacks
       0   0.0% 100.0%        3  33.3% __pthread_clockjoin_ex
       0   0.0% 100.0%        6  66.7% _start
       0   0.0% 100.0%        2  22.2% invoke_hooks_and_free
       0   0.0% 100.0%        6  66.7% main
       0   0.0% 100.0%        3  33.3% start_thread
       0   0.0% 100.0%        1  11.1% std::__invoke
       0   0.0% 100.0%        1  11.1% std::__invoke_impl
       0   0.0% 100.0%        3  33.3% std::allocator_traits::construct
       0   0.0% 100.0%        3  33.3% std::error_code::default_error_condition@@GLIBCXX_3.4.11
       0   0.0% 100.0%        1  11.1% std::num_put::_M_insert_float
       0   0.0% 100.0%        1  11.1% std::ostream::_M_insert
       0   0.0% 100.0%        1  11.1% std::thread::_Invoker::_M_invoke
       0   0.0% 100.0%        1  11.1% std::thread::_Invoker::operator
       0   0.0% 100.0%        2  22.2% std::thread::_M_start_thread@@GLIBCXX_3.4.22
       0   0.0% 100.0%        1  11.1% std::thread::_State_impl::_M_run
       0   0.0% 100.0%        1  11.1% std::thread::_State_impl::_State_impl
       0   0.0% 100.0%        2  22.2% std::thread::_State_impl::~_State_impl
       0   0.0% 100.0%        3  33.3% std::thread::join@@GLIBCXX_3.4.11
       0   0.0% 100.0%        3  33.3% std::thread::thread
       0   0.0% 100.0%        1  11.1% std::use_facet
       0   0.0% 100.0%        3  33.3% std::vector::emplace_back
[root@iZj6c0il0t6l26ze71vq4cZ build]#
3.结果分析

在这里插入图片描述

根据上述数据,对关键函数分析

__GI___munmap__pthread_create_2_1

  • 这两个函数各占用了22.2%的样本,是CPU使用最多的两个函数。__GI___munmap 被用于内存映射的取消,通常与资源释放相关。而 __pthread_create_2_1 与线程创建相关,表明线程的创建和管理是CPU消耗的一个重要部分。

内存和同步相关的函数

  • MallocHook::InvokeDeleteHookSlowSpinLock::Lock 分别占用了11.1%的样本。这显示内存分配和线程同步也是性能消耗的关键点。

其他系统调用和库函数

  • __futex_abstimed_wait_commonstd::forward 也各占有11.1%,表明系统级同步和模板函数的使用对性能有较大影响。

六、后论

通过这篇博客,应该能够掌握使用 gperftools 来分析和优化 C++ 应用程序的基本方法。无论是 CPU 还是内存优化,gperftools 都是一个强大的工具,可以帮助程序提升应用性能。

gperftools 提供了主要包括 CPU 分析器和堆分析器。适合用来识别程序中的性能热点和内存泄漏。对于想要进行全面性能和资源优化的需求来说,可能还需要考虑一些其他工具和技术作为 gperftools 的互补。

以下是对 gperftools 工具的一些补充:

  1. Valgrind

    • Valgrind 是一个编程工具套件,用于内存调试、内存泄漏检测以及性能分析。虽然 gperftools 的堆分析器能够帮助检测内存泄漏,Valgrind 的 Memcheck 工具在某些情况下可能提供更详细的内存访问和泄漏信息。
  2. AddressSanitizer

    • AddressSanitizer (ASan) 是一个快速的内存错误检测器,可以检测出各种内存访问错误。ASan 被集成在 LLVM/Clang 和 GCC 中,与 gperftools 相比,它在运行时插入的检查可以自动发现如使用后释放、堆栈缓冲区溢出等错误。
  3. ThreadSanitizer

    • ThreadSanitizer (TSan) 是用于检测数据竞争的工具。如果应用程序涉及复杂的多线程,TSan 可以作为 gperftools 的有力补充,帮助识别可能导致不稳定行为和奇怪的 bug 的数据竞争问题。
  4. VisualVM

    • 对于需要分析 Java 应用程序性能的开发者,VisualVM 提供了一套完整的可视化工具,包括线程分析、堆分析和 CPU 分析等。虽然这不是针对 C++ 的工具,但它展示了集成性能分析工具的方向,对于混合语言开发环境非常有用。
  5. Intel VTune Profiler

    • 对于需要在硬件层面上进行性能分析的开发者,Intel VTune Profiler 提供了深入的硬件级性能洞察,包括 CPU 利用率、缓存命中率、分支预测错误等。这对于优化依赖于 CPU 性能的应用程序特别有价值。
  6. Perf

    • Perf 是 Linux 下的一个性能计数器工具,它可以访问 CPU 性能计数器、追踪点等,用于更底层的性能分析。Perf 能够帮助开发者分析应用程序与操作系统之间的交互,并识别潜在的性能问题。
  7. SystemTap

    • SystemTap 提供了一种方法,允许管理员和开发者在 Linux 系统上运行的实时内核中编写和执行脚本,以收集关于系统的运行信息。这是研究和解决操作系统级性能问题的一个强大工具。

通过将 gperftools 与这些工具结合使用,可以获得更全面的性能分析视角,有效地优化和改进软件。

the end~


- 推荐文章 -

C++

音视频


  • 13
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
google-perftools 简介 google-perftools 是一款针对 C/C++ 程序的性能分析工具,它是一个遵守 BSD 协议的开源项目。使用该工具可以对 CPU 时间片、内存等系统资源的分配和使用进行分析,本文将重点介绍如何进行 CPU 时间片的剖析。 google-perftools 对一个程序的 CPU 性能剖析包括以下几个步骤。 1. 编译目标程序,加入对 google-perftools 库的依赖。 2. 运行目标程序,并用某种方式启动 / 终止剖析函数并产生剖析结果。 3. 运行剖结果转换工具,将不可读的结果数据转化成某种格式的文档(例如 pdf,txt,gv 等)。 安装 您可以在 google-perftools 的网站 (http://code.google.com/p/google-perftools/downloads/list) 上下载最新版的安装包。为完成步骤 3 的工作,您还需要一个将剖析结果转化为程序员可读文档的工具,例如 gv(http://www.gnu.org/software/gv/)。 编译与运行 您需要在原有的编译选项中加入对 libprofiler.so 的引用,这样在目标程序运行时会加载工具的动态库。例如本例中作者的系统中,libprofiler.so 安装在"/usr/lib"目录下,所以需要在 makefile 文件中的编译选项加入“-L/usr/lib -lprofiler”。 google-perftools 需要在目标代码的开始和结尾点分别调用剖析模块的启动和终止函数,这样在目标程序运行时就可以对这段时间内程序实际占用的 CPU 时间片进行统计和分析工具的启动和终止可以采用以下两种方式。 a. 使用调试工具 gdb 在程序中手动运行性能工具的启动 / 终止函数。 gdb 是 Linux 上广泛使用的调试工具,它提供了强大的命令行功能,使我们可以在程序运行时插入断点并在断点处执行其他函数。具体的文档请参照 http://www.gnu.org/software/gdb/,本文中将只对用到的几个基本功能进行简单介绍。使用以下几个功能就可以满足我们性能调试的基本需求,具体使用请参见下文示例。 命令 功能 ctrl+c 暂停程序的运行 c 继续程序的运行 b 添加函数断点(参数可以是源代码中的行号或者一个函数名) p 打印某个量的值或者执行一个函数调用 b. 在目标代码中直接加入性能工具函数的调用,该方法就是在程序代码中直接加入调试函数的调用。 两种方式都需要对目标程序重新编译,加入对性能工具的库依赖。对于前者,他的好处是使用比较灵活,但工具的启动和终止依赖于程序员的手动操作,常常需要一些暂停函数(比如休眠 sleep)的支持才能达到控制程序的目的,因此精度可能受到影响。对于后者,它需要对目标代码的进行修改,需要处理函数声明等问题,但得到的结果精度较高,缺点是每次重新设置启动点都需要重新编译,灵活度不高,读者可以根据自己的实际需求采用有效的方式。 示例详解 该程序是一个简单的例子,文中有两处耗时的无用操作,并且二者间有一定的调用关系。 清单 1. 示例程序 void consumeSomeCPUTime1(int input){ int i = 0; input++; while(i++ < 10000){ i--; i++; i--; i++; } }; void consumeSomeCPUTime2(int input){ input++; consumeSomeCPUTime1(input); int i = 0; while(i++ < 10000){ i--; i++; i--; i++; } }; int stupidComputing(int a, int b){ int i = 0; while( i++ < 10000){ consumeSomeCPUTime1(i); } int j = 0; while(j++ < 5000){ consumeSomeCPUTime2(j); } return a+b; }; int smartComputing(int a, int b){ return a+b; }; void main(){ int i = 0; printf("reached the start point of performance bottle neck\n"); sleep(5); //ProfilerStart("CPUProfile"); while( i++ MyProfile.pdf 转换后产生的结果文档如下图。图中的数字和框体的大小代表了的某个函数的运行时间占整个剖析时间的比例。由代码的逻辑可知,stupidComputing,stupidComputing2 都是费时操作并且它们和 consumeSomeCPUTime 存在着一定的调用关系。 图 1. 剖析结果 结束语 本文介绍了一个 Linux 平台上的性能剖析工具 google-perftools,并结合实例向读者展示了如何使用该工具配置、使用及分析性能瓶颈。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值