Skip to content

Commit 9e8e47a

Browse files
committed
Bugfix: SignalTrace mode has memory and deadlock issues
1 parent 5bc6da6 commit 9e8e47a

11 files changed

Lines changed: 194 additions & 284 deletions

File tree

.github/actions/init-ut-make-config/action.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ runs:
77
steps:
88
- run: |
99
sudo apt-get update && sudo apt-get install -y clang-12 lldb-12 lld-12 libgtest-dev cmake gdb libstdc++6-11-dbg
10+
shell: bash
11+
- run: |
1012
cd /usr/src/gtest && export CC=clang-12 && export CXX=clang++-12 && sudo cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 .
1113
sudo make -j ${{env.proc_num}} && sudo mv lib/libgtest* /usr/lib/
1214
shell: bash
@@ -22,5 +24,11 @@ runs:
2224
sudo ./autogen.sh && sudo CC=clang-12 CXX=clang++-12 ./configure --prefix=/gperftools --enable-frame-pointers
2325
sudo make -j ${{env.proc_num}} && sudo make install
2426
shell: bash
25-
- run: sh config_brpc.sh --headers="/libunwind/include /gperftools/include /usr/include" --libs="/libunwind/lib /gperftools/lib /usr/lib /usr/lib64" ${{inputs.options}}
27+
- run: |
28+
sudo git clone https://github.com/abseil/abseil-cpp.git
29+
cd abseil-cpp && sudo git checkout lts_2022_06_23 && sudo mkdir -p /abseil-cpp
30+
sudo CC=clang-12 CXX=clang++-12 cmake -DBUILD_TESTING=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_INSTALL_PREFIX=/abseil-cpp -DCMAKE_POLICY_VERSION_MINIMUM=3.5 .
31+
sudo make -j ${{env.proc_num}} && sudo sudo make install
32+
shell: bash
33+
- run: sh config_brpc.sh --headers="/libunwind/include /gperftools/include /abseil-cpp/include /usr/include" --libs="/libunwind/lib /gperftools/lib /abseil-cpp/lib /usr/lib /usr/lib64" --cc=clang-12 --cxx=clang++-12 ${{inputs.options}} && cat config.mk
2634
shell: bash

.github/actions/install-all-dependencies/action.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ runs:
44
- uses: ./.github/actions/install-essential-dependencies
55
- run: sudo apt-get install -y libunwind-dev libgoogle-glog-dev automake bison flex libboost-all-dev libevent-dev libtool pkg-config libibverbs1 libibverbs-dev
66
shell: bash
7-
- run: wget https://archive.apache.org/dist/thrift/0.11.0/thrift-0.11.0.tar.gz && tar -xf thrift-0.11.0.tar.gz
7+
- run: |
8+
wget https://archive.apache.org/dist/thrift/0.11.0/thrift-0.11.0.tar.gz && tar -xf thrift-0.11.0.tar.gz && cd thrift-0.11.0/
9+
./configure --prefix=/usr --with-rs=no --with-ruby=no --with-python=no --with-java=no --with-go=no --with-perl=no --with-php=no --with-csharp=no --with-erlang=no --with-lua=no --with-nodejs=no --with-haskell=no --with-dotnetcore=no CXXFLAGS="-Wno-unused-variable"
10+
make -j ${{env.proc_num}} && sudo make install
811
shell: bash
9-
- run: cd thrift-0.11.0/ && ./configure --prefix=/usr --with-rs=no --with-ruby=no --with-python=no --with-java=no --with-go=no --with-perl=no --with-php=no --with-csharp=no --with-erlang=no --with-lua=no --with-nodejs=no --with-haskell=no --with-dotnetcore=no CXXFLAGS="-Wno-unused-variable" && make -j ${{env.proc_num}} && sudo make install
12+
- run: |
13+
git clone https://github.com/abseil/abseil-cpp.git && cd abseil-cpp && git checkout lts_2022_06_23
14+
cmake -DBUILD_TESTING=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_POLICY_VERSION_MINIMUM=3.5 .
15+
make -j ${{env.proc_num}} && sudo make install
1016
shell: bash

.github/workflows/ci-linux.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ jobs:
176176
- uses: ./.github/actions/install-essential-dependencies
177177
- uses: ./.github/actions/init-ut-make-config
178178
with:
179-
options: --cc=clang-12 --cxx=clang++-12 --with-bthread-tracer
179+
options: --with-bthread-tracer
180180
- name: compile tests
181181
run: |
182182
cat config.mk
@@ -194,7 +194,7 @@ jobs:
194194
- uses: ./.github/actions/install-essential-dependencies
195195
- uses: ./.github/actions/init-ut-make-config
196196
with:
197-
options: --cc=clang-12 --cxx=clang++-12 --with-bthread-tracer --with-asan
197+
options: --with-bthread-tracer --with-asan
198198
- name: compile tests
199199
run: |
200200
cat config.mk

BUILD.bazel

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ COPTS = [
5050
}) + select({
5151
"//bazel/config:brpc_with_debug_lock": ["-DBRPC_DEBUG_LOCK=1"],
5252
"//conditions:default": ["-DBRPC_DEBUG_LOCK=0"],
53-
}) + select({
54-
"//bazel/config:brpc_with_bthread_tracer": ["-DBRPC_BTHREAD_TRACER"],
55-
"//conditions:default": [],
5653
}) + select({
5754
"//bazel/config:brpc_with_asan": ["-fsanitize=address"],
5855
"//conditions:default": [""],
@@ -402,6 +399,12 @@ cc_library(
402399
"src/bthread/*.h",
403400
"src/bthread/*.list",
404401
]),
402+
defines = [] + select({
403+
"//bazel/config:brpc_with_bthread_tracer": [
404+
"-DBRPC_BTHREAD_TRACER",
405+
],
406+
"//conditions:default": [],
407+
}),
405408
copts = COPTS,
406409
includes = [
407410
"src/",
@@ -412,7 +415,11 @@ cc_library(
412415
":butil",
413416
":bvar",
414417
] + select({
415-
"//bazel/config:brpc_with_bthread_tracer": ["@com_github_libunwind_libunwind//:libunwind"],
418+
"//bazel/config:brpc_with_bthread_tracer": [
419+
"@com_github_libunwind_libunwind//:libunwind",
420+
"@com_google_absl//absl/debugging:stacktrace",
421+
"@com_google_absl//absl/debugging:symbolize",
422+
],
416423
"//conditions:default": [],
417424
}),
418425
)

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ if (WITH_BTHREAD_TRACER)
9393
if (NOT LIBUNWIND_INCLUDE_PATH OR NOT LIBUNWIND_LIB)
9494
message(FATAL_ERROR "Fail to find libunwind, which is needed by bthread tracer")
9595
endif()
96+
find_package(absl REQUIRED CONFIG)
97+
set(bthread_tracer_ABSL_USED_TARGETS absl::base absl::stacktrace absl::symbolize)
9698
add_definitions(-DBRPC_BTHREAD_TRACER)
9799
include_directories(${LIBUNWIND_INCLUDE_PATH})
98100
endif ()
@@ -329,8 +331,8 @@ if(WITH_SNAPPY)
329331
endif()
330332

331333
if (WITH_BTHREAD_TRACER)
332-
set(DYNAMIC_LIB ${DYNAMIC_LIB} ${LIBUNWIND_LIB} ${LIBUNWIND_X86_64_LIB})
333-
set(BRPC_PRIVATE_LIBS "${BRPC_PRIVATE_LIBS} -lunwind -lunwind-x86_64")
334+
set(DYNAMIC_LIB ${DYNAMIC_LIB} ${LIBUNWIND_LIB} ${LIBUNWIND_X86_64_LIB} ${bthread_tracer_ABSL_USED_TARGETS})
335+
set(BRPC_PRIVATE_LIBS "${BRPC_PRIVATE_LIBS} -lunwind -lunwind-x86_64 -labsl_stacktrace -labsl_symbolize -labsl_debugging_internal -labsl_demangle_internal -labsl_malloc_internal -labsl_raw_logging_internal -labsl_spinlock_wait -labsl_base")
334336
endif()
335337

336338
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ bazel_dep(name = "apple_support", version = "1.17.1")
1616
bazel_dep(name = 'rules_cc', version = '0.0.1')
1717
bazel_dep(name = 'rules_proto', version = '4.0.0')
1818
bazel_dep(name = 'zlib', version = '1.3.1.bcr.5', repo_name = 'com_github_madler_zlib')
19-
bazel_dep(name = "libunwind", version = "1.8.1", repo_name = 'com_github_libunwind_libunwind')
19+
bazel_dep(name = 'libunwind', version = '1.8.1', repo_name = 'com_github_libunwind_libunwind')
2020

2121
# --registry=https://baidu.github.io/babylon/registry
2222
bazel_dep(name = 'leveldb', version = '1.23', repo_name = 'com_github_google_leveldb')

config_brpc.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ if [ $WITH_BTHREAD_TRACER != 0 ]; then
363363
fi
364364
LIBUNWIND_HDR=$(find_dir_of_header_or_die libunwind.h)
365365
LIBUNWIND_LIB=$(find_dir_of_lib_or_die unwind)
366+
ABSL_HDR=$(find_dir_of_header_or_die absl/base/config.h)
367+
ABSL_LIB=$(find_dir_of_lib_or_die absl_symbolize)
366368

367369
CPPFLAGS="${CPPFLAGS} -DBRPC_BTHREAD_TRACER"
368370

@@ -371,6 +373,11 @@ if [ $WITH_BTHREAD_TRACER != 0 ]; then
371373
else
372374
STATIC_LINKINGS="$STATIC_LINKINGS -lunwind -lunwind-x86_64"
373375
fi
376+
if [ -f "$ABSL_LIB/libabsl_base.$SO" ]; then
377+
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -labsl_stacktrace -labsl_symbolize -labsl_debugging_internal -labsl_demangle_internal -labsl_malloc_internal -labsl_raw_logging_internal -labsl_spinlock_wait -labsl_base"
378+
else
379+
STATIC_LINKINGS="$STATIC_LINKINGS -labsl_stacktrace -labsl_symbolize -labsl_debugging_internal -labsl_demangle_internal -labsl_malloc_internal -labsl_raw_logging_internal -labsl_spinlock_wait -labsl_base"
380+
fi
374381
fi
375382

376383
HDRS=$($ECHO "$LIBUNWIND_HDR\n$GFLAGS_HDR\n$PROTOBUF_HDR\n$ABSL_HDR\n$LEVELDB_HDR\n$OPENSSL_HDR" | sort | uniq)

docs/cn/bthread_tracer.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ jump_stack是bthread挂起或者运行的必经之路,也是STB的拦截点。
5858

5959
# 使用方法
6060

61-
1. 下载安装libunwind
62-
2. 给config_brpc.sh增加`--with-bthread-tracer`选项或者给cmake增加`-DWITH_BTHREAD_TRACER=ON`选项。
61+
1. 下载安装libunwind和abseil-cpp
62+
2. 给config_brpc.sh增加`--with-bthread-tracer`选项或者给cmake增加`-DWITH_BTHREAD_TRACER=ON`选项或者给bazel(Bzlmod模式)增加`--define with_bthread_tracer=true`选项。
6363
3. 访问服务的内置服务:`http://ip:port/bthreads/<bthread_id>?st=1`或者代码里调用`bthread::stack_trace()`函数。
6464
4. 如果希望追踪pthread的调用栈,在对应pthread上调用`bthread::init_for_pthread_stack_trace()`函数获取一个伪bthread_t,然后使用步骤3即可获取pthread调用栈。
6565

@@ -75,5 +75,4 @@ jump_stack是bthread挂起或者运行的必经之路,也是STB的拦截点。
7575

7676
# 相关flag
7777

78-
- `enable_fast_unwind`:是否启用快速回溯功能,默认为true。大多数情况下,不需要关闭快速回溯功能。除非你关注的调用栈函数名转换失败,显示为`<unknown>`,则可以尝试关闭快速回溯功能,但这会导致性能下降。以包含30帧的调用栈举例,快速回溯基本上在200us以内就可以完成,而关闭快速回溯则需要4ms左右,性能下降了近20倍。
79-
- `signal_trace_timeout_ms`:信号追踪模式的超时时间,默认为50ms。虽然libunwind文档显示回溯功能是异步信号安全的,但是[gpertools社区发现libunwind在某些情况下会死锁](https://github.com/gperftools/gperftools/issues/775),所以TaskTracer会设置了超时时间,超时后会放弃回溯,打破死锁。
78+
- `signal_trace_timeout_ms`:信号追踪模式的超时时间,默认为50ms。

0 commit comments

Comments
 (0)