gperftools

 Sat, 07-Nov-2026 13:20:45

compile m4

cd /tmp && curl --http1.1 -fsSL -o m420.tar.gz https://ftp.gnu.org/gnu/m4/m4-1.4.20.tar.gz && tar -xzf m420.tar.gz && cd m4-1.4.20 && ./configure --prefix=/home/chenmin/tools/autotools >/dev/null 2>&1 && make -j$(nproc) >/dev/null 2>&1 && make install >/dev/null 2>&1 && /home/chenmin/tools/autotools/bin/m4 --version | head -1

新编译器默认 C23 与旧 gnulib 不兼容,用 -std=gnu11 规避:

以 gnu11 标准重新构建安装 m4

cd /tmp/m4-1.4.20 && make distclean >/dev/null 2>&1; ./configure --prefix=/home/chenmin/tools/autotools CFLAGS="-O2 -std=gnu11" >/dev/null 2>&1 && make -j$(nproc) >/dev/null 2>&1 && make install >/dev/null 2>&1 && /home/chenmin/tools/autotools/bin/m4 --version | head -1

这是 glibc 2.42+ 用 _Generic 宏重新声明 bsearch/qsort 与旧 gnulib 的冲突。查看另外两处并直接修补生成的头文件:

查看 string.h 和 wchar.h 中冲突的声明

 

echo '#include <stdlib.h>
#include <string.h>' | gcc -E -dM - | grep -E "define (bsearch|qsort|memset|memcmp|memchr|wmemcmp)" | head

确认了:glibc 2.42+ 把 bsearch、memchr 定义成了 _Generic 宏,与旧 gnulib 的重声明冲突。修补生成的头文件,在重声明前 #undef:

在 gnulib 生成的头文件中对函数宏先 undef 再重声明

cd /tmp/m4-1.4.20/lib && sed -i 's|^_GL_EXTERN_C void \*bsearch (|#undef bsearch\n_GL_EXTERN_C void *bsearch (|' stdlib.h && sed -i 's|^_GL_EXTERN_C void \*memchr (|#undef memchr\n_GL_EXTERN_C void *memchr (|' string.h && grep -n "undef bsearch" stdlib.h && grep -n "undef memchr" string.h

cd /tmp/m4-1.4.20/lib && sed -i 's|^_GL_EXTERN_C wchar_t \*wmemchr (|#   undef wmemchr\n_GL_EXTERN_C wchar_t *wmemchr (|' wchar.h && cd /tmp/m4-1.4.20 && make -j$(nproc) 2>&1 | grep -iE " error|Error [0-9]" | head -5; make install >/dev/null 2>&1 && /home/chenmin/tools/autotools/bin/m4 --version | head -1

export PATH=/home/chenmin/tools/autotools/bin:$PATH && cd /tmp/autoconf-2.72 && ./configure --prefix=/home/chenmin/tools/autotools >/dev/null 2>&1 && make -j$(nproc) >/dev/null 2>&1 && make install >/dev/null 2>&1 && autoconf --version | head -1

export PATH=/home/chenmin/tools/autotools/bin:$PATH && cd /tmp && curl --http1.1 -fsSL -o am.tar.xz https://ftp.gnu.org/gnu/automake/automake-1.17.tar.xz && tar -xJf am.tar.xz && cd automake-1.17 && ./configure --prefix=/home/chenmin/tools/autotools >/dev/null 2>&1 && make -j$(nproc) >/dev/null 2>&1 && make install >/dev/null 2>&1 && automake --version | head -1

export PATH=/home/chenmin/tools/autotools/bin:$PATH && cd /tmp && curl --http1.1 -fsSL -o lt.tar.xz https://ftp.gnu.org/gnu/libtool/libtool-2.5.4.tar.xz && tar -xJf lt.tar.xz && cd libtool-2.5.4 && ./configure --prefix=/home/chenmin/tools/autotools >/dev/null 2>&1 && make -j$(nproc) >/dev/null 2>&1 && make install >/dev/null 2>&1 && libtool --version | head -1 && autoreconf --version | head -1

export PATH=/home/chenmin/tools/autotools/bin:$PATH && cd /home/chenmin/code/gperftools && ./autogen.sh 2>&1 | tail -5 && ls -la configure

cd /home/chenmin/code/gperftools && mkdir -p build-autotools && cd build-autotools && ../configure --prefix=/home/chenmin/tools/gperftools 2>&1 | tail -8

cd /home/chenmin/code/gperftools/build-autotools && make -j$(nproc) 2>&1 | grep -ciE " error|Error [0-9]"; tail -3 <(make 2>&1)

cd /home/chenmin/code/gperftools/build-autotools && make 2>&1 | tail -3; ls .libs/*.so* 2>/dev/null | head; ls src/tcmalloc.o >/dev/null 2>&1 && echo BUILD_OK

compile pprof

cd /home/chenmin/code/pprof && GOPROXY=https://goproxy.cn,direct go build -o /home/chenmin/workspace/gperftools_test/tools/pprof . && /home/chenmin/workspace/gperftools_test/tools/pprof -h 2>&1 | head -8

 

 

cmake_minimum_required(VERSION 3.10)
project(gperftools_test CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
# gperftools 采栈需要可靠的调用栈,同时保留符号以便 pprof 显示函数名
add_compile_options(-g -fno-omit-frame-pointer)

# gperftools 安装路径(可通过 -DGPERFTOOLS_ROOT=... 覆盖)
set(GPERFTOOLS_ROOT "/home/chenmin/tools/gperftools" CACHE PATH "gperftools install prefix")
include_directories(${GPERFTOOLS_ROOT}/include)
link_directories(${GPERFTOOLS_ROOT}/lib)

# 1) tcmalloc + 堆内存 profile
#    直接链接 tcmalloc 后,程序内所有 new/malloc 都由 tcmalloc 接管
add_executable(heap_test heap_test.cpp)
target_link_libraries(heap_test PRIVATE tcmalloc)

# 2) CPU profile(若需同时测 CPU + 内存,可链接 tcmalloc_and_profiler)
add_executable(cpu_test cpu_test.cpp)
target_link_libraries(cpu_test PRIVATE profiler)

set -e
cd "$(dirname "$0")"

PPROF=tools/pprof
BUILD=build
PROFILE=profile

# 1. 编译
mkdir -p "$BUILD" "$PROFILE" tools
(cd "$BUILD" && cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo && make -j$(nproc))

rm -f "$PROFILE"/*.heap "$PROFILE"/*.prof

# 2. 运行测试程序,生成 profile
"$BUILD/heap_test"
"$BUILD/cpu_test"

echo
echo "==================== CPU profile (top) ===================="
"$PPROF" -top -nodecount=10 "$BUILD/cpu_test" "$PROFILE/cpu.prof"

echo
echo "==================== HEAP profile (top, inuse_space) ===================="
"$PPROF" -top -lines -nodecount=10 "$BUILD/heap_test" "$PROFILE/heap.0002.heap"

echo
echo "更多分析方式(需手动执行):"
echo "  交互模式:      $PPROF $BUILD/cpu_test $PROFILE/cpu.prof"
echo "  文本调用树:    $PPROF -traces $BUILD/cpu_test $PROFILE/cpu.prof"
echo "  生成 Web 页面: $PPROF -http=:8080 $BUILD/cpu_test $PROFILE/cpu.prof"
echo "  堆增量(泄漏检测): $PPROF -top -inuse_space $BUILD/heap_test $PROFILE/heap.0001.heap $PROFILE/heap.0002.heap"