Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support jemalloc profiler #2737

Merged
merged 17 commits into from
Sep 23, 2024
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ LINKOPTS = [
"-Wl,-U,_ProfilerStop",
"-Wl,-U,__Z13GetStackTracePPvii",
"-Wl,-U,_RegisterThriftProtocol",
"-Wl,-U,_mallctl",
],
"//conditions:default": [
"-lrt",
Expand Down
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

# for *.so
Expand Down
1 change: 1 addition & 0 deletions config_brpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ if [ "$SYSTEM" = "Darwin" ]; then
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStop"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,__Z13GetStackTracePPvii"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_RegisterThriftProtocol"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_mallctl"
fi
append_linking() {
if [ -f $1/lib${2}.a ]; then
Expand Down
31 changes: 31 additions & 0 deletions docs/cn/heap_profiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,34 @@ brpc还提供一个类似的growth profiler分析内存的分配去向(不考

1. 安装[standalone pprof](https://github.com/google/pprof),并把下载的pprof二进制文件路径写入环境变量GOOGLE_PPROF_BINARY_PATH中
2. 安装llvm-symbolizer(将函数符号转化为函数名),直接用brew安装即可:`brew install llvm`

# Jemalloc Heap Profiler

## 开启方法

1. 编译[jemalloc](https://github.com/jemalloc/jemalloc)时需--enable-prof以支持profiler, 安装完成后bin目录下会有jeprof文件。
2. 启动进程前最好配置env `JEPROF_FILE=/xxx/jeprof`,否则进程默认用$PATH里的jeprof解析。
3. 启动进程并开启profiler功能:`MALLOC_CONF="prof:true" LD_PRELOAD=/xxx/lib/libjemalloc.so ./bin/test_server`,若静态链接jemalloc,`MALLOC_CONF="prof:true" ./bin/test_server`,MALLOC_CONF是env项,此时只做一些初始化动作,并不会采样。若启动就采样可设置`MALLOC_CONF="prof:true,prof_active:true,lg_prof_sample:19"`,或通过下面的gflags控制,gflags不会反应MALLOC_CONF值。
4. 相关gflags说明:
- FLAGS_je_prof_active:true:开启采样,false:关闭采样。
- FLAGS_je_prof_dump:修改值会生成heap文件,用于手动操作jeprof分析。
- FLAGS_je_prof_reset:清理已采样数据和重置profiler选项,并且动态设置采样率,[默认](https://jemalloc.net/jemalloc.3.html#opt.lg_prof_sample)2^19B(512K)。
5. 若要做memory leak,需`MALLOC_CONF="prof:true,prof_leak:true,prof_final:true"`,进程退出时生成heap文件,注:可`kill pid`,不可`kill -9 pid`。

注:每次dump的都是从采样至今的所有数据,若触发了reset,接来下dump的是从reset至今的所有数据,方便做diff。
更多jemalloc profiler选项请参考[官网](https://jemalloc.net/jemalloc.3.html),如prof_leak_error:true则检测到内存泄漏,进程立即退出。

## 样例

- jeprof命令`jeprof ip:port/pprof/heap`。

![img](../images/cmd_jeprof_text.png)

- curl生成text格式`curl ip:port/pprof/heap?display=text`。

![img](../images/curl_jeprof_text.png)

- curl生成svg图片格式`curl ip:port/pprof/heap?display=svg`。

![img](../images/curl_jeprof_svg.png)

Binary file added docs/images/cmd_jeprof_text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/curl_jeprof_svg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/curl_jeprof_text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion example/asynchronous_echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(asynchronous_echo_client client.cpp ${PROTO_SRC})
Expand Down
4 changes: 3 additions & 1 deletion example/auto_concurrency_limiter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_RegisterThriftProtocol")
"-Wl,-U,_RegisterThriftProtocol"
"-Wl,-U,_mallctl"
)
endif()

add_executable(asynchronous_echo_client client.cpp ${PROTO_SRC})
Expand Down
4 changes: 3 additions & 1 deletion example/backup_request_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(backup_request_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/baidu_proxy_and_generic_call/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/bthread_tag_echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/cancel_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(cancel_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/cascade_echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(cascade_echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/dynamic_partition_echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(dynamic_partition_echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/grpc_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_RegisterThriftProtocol")
"-Wl,-U,_RegisterThriftProtocol"
"-Wl,-U,_mallctl"
)
endif()

add_executable(server server.cpp ${PROTO_SRC} ${PROTO_HEADER} )
Expand Down
4 changes: 3 additions & 1 deletion example/http_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(http_client http_client.cpp)
Expand Down
4 changes: 3 additions & 1 deletion example/memcache_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(memcache_client client.cpp)
Expand Down
4 changes: 3 additions & 1 deletion example/multi_threaded_echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/multi_threaded_echo_fns_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(multi_threaded_echo_fns_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/nshead_extension_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(nshead_extension_client client.cpp)
Expand Down
4 changes: 3 additions & 1 deletion example/nshead_pb_extension_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(nshead_pb_extension_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/parallel_echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(parallel_echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/partition_echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/rdma_performance/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/redis_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(redis_cli redis_cli.cpp)
Expand Down
4 changes: 3 additions & 1 deletion example/rpcz_echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(rpcz_echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/selective_echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(selective_echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/session_data_and_thread_local/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(session_data_and_thread_local_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion example/streaming_echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_mallctl"
)
endif()

add_executable(streaming_echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
11 changes: 11 additions & 0 deletions src/brpc/builtin/pprof_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "brpc/builtin/pprof_service.h"
#include "brpc/builtin/common.h"
#include "brpc/details/tcmalloc_extension.h"
#include "brpc/details/jemalloc_profiler.h"
#include "bthread/bthread.h" // bthread_usleep
#include "butil/fd_guard.h"

Expand Down Expand Up @@ -213,6 +214,16 @@ void PProfService::heap(
::google::protobuf::Closure* done) {
ClosureGuard done_guard(done);
Controller* cntl = static_cast<Controller*>(controller_base);

if (HasJemalloc()) {
if (!HasEnableJemallocProfile()) {
cntl->SetFailed(ENOMETHOD, "Heap profiler is not enabled, (no MALLOC_CONF=prof:true in env)");
return;
}
JeControlProfile(cntl);
return;
}

MallocExtension* malloc_ext = MallocExtension::instance();
if (malloc_ext == NULL || !has_TCMALLOC_SAMPLE_PARAMETER()) {
const char* extra_desc = "";
Expand Down
Loading
Loading