0.背景
gperftools是google開發的一款非常實用的工具集,主要包括:性能優異的malloc free內存分配器tcmalloc;基於tcmalloc的堆內存檢測和內存泄漏分析工具heap-profiler,heap-checker;基於tcmalloc實現的程序CPU性能監測工具cpu-profiler.
github地址
上述所說的三種工具在我們服務器進程的性能分析監控,定位內存泄漏,尋找性能熱點,提高malloc free內存分配性能的各個方面上都有非常成功的使用經驗.
1. 編譯安裝
wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.9.1/gperftools-2.9.1.zip
unzip gperftools-2.9.1.zip
cd gperftools-2.9.1
./configure
make -j8
sudo make install
2. 使用
2.1 編譯
官方文檔: Gperftools CPU Profiler
編譯時設置編譯選項 -lprofiler ,添加鏈接目錄 /usr/local/lib .
cmake 添加如下代碼會鏈接動態庫
# gperftools
if(CMAKE_GPERF_TOOLS)
set(CMAKE_CXX_FLAGS "-lprofiler ${CMAKE_CXX_FLAGS}")
link_directories("/usr/local/lib")
message("-lprofiler")
endif()
通過如下代碼鏈接靜態庫
# gperftools
if(CMAKE_GPERF_TOOLS)
target_link_libraries(test_tools libprofiler.a)
endif()
2.2 運行
頭文件: <gperftools/heap-profiler.h>
<google/profiler.h>
程序開始添加代碼:ProfilerStart("test_tcp_echo.prof");
程序結束添加代碼: ProfilerStop();
運行程序到自然結束或者捕捉信號去調用ProfilerStop();
2.3 查看性能報告 text版本
pprof ./test_tools_forward test_tcp_forward.prof --text
結果:
[liyakai@VM-0-2-centos bin]$ pprof ./test_tools_forward test_tcp_forward.prof --text
Using local file ./test_tools_forward.
Using local file test_tcp_forward.prof.
Total: 2847 samples
1221 42.9% 42.9% 1227 43.1% __libc_send
451 15.8% 58.7% 457 16.1% __libc_recv
179 6.3% 65.0% 183 6.4% __GI_epoll_wait
111 3.9% 68.9% 111 3.9% __memmove_ssse3_back
35 1.2% 70.1% 35 1.2% __nanosleep_nocancel
35 1.2% 71.4% 35 1.2% std::__detail::_Mod_range_hashing::operator
25 0.9% 72.3% 688 24.2% EpollSocket::UpdateEpollEvent
25 0.9% 73.1% 31 1.1% __GI___pthread_mutex_unlock
21 0.7% 73.9% 54 1.9% std::basic_string::basic_string
20 0.7% 74.6% 29 1.0% __GI___pthread_mutex_lock
19 0.7% 75.2% 45 1.6% Singleton::Instance
17 0.6% 75.8% 17 0.6% std::__atomic_base::fetch_add
16 0.6% 76.4% 16 0.6% RingBuffer::WriteableSize
16 0.6% 77.0% 16 0.6% std::__once_callable
15 0.5% 77.5% 15 0.5% RingBuffer::ReadableSize
14 0.5% 78.0% 79 2.8% std::_Hashtable::_M_find_node
13 0.5% 78.4% 65 2.3% std::_Hashtable::_M_find_before_node
13 0.5% 78.9% 13 0.5% std::forward
13 0.5% 79.3% 13 0.5% std::operator&
12 0.4% 79.8% 12 0.4% EpollSocket::GetConnID
12 0.4% 80.2% 80 2.8% std::_Hashtable::find
2.4 生成性能報告 pdf版本
執行如下命令 生成pdf版本的報告
pprof ./test_tools_forward test_tcp_forward.prof --pdf > test_tcp_forward.pdf
如果提示 ps2pdf 無效,則需要先安裝 ghostscript:
yum install ghostscript
如果提示 sh: dot: not found, 則需要先安裝 graphviz:
yum install graphviz
生成結果: