Skip to content

Commit 694211c

Browse files
authored
Merge branch 'apache:master' into fix_0402
2 parents f7ab9dd + 3d16b5b commit 694211c

12 files changed

Lines changed: 36 additions & 30 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inputs:
55
runs:
66
using: "composite"
77
steps:
8-
- run: sudo apt-get update && sudo apt-get install -y clang-12 lldb-12 lld-12 libgtest-dev cmake gdb libstdc++6-11-dbg && cd /usr/src/gtest && export CC=clang-12 && export CXX=clang++-12 && sudo cmake . && sudo make -j ${{env.proc_num}} && sudo mv lib/libgtest* /usr/lib/
8+
- run: sudo apt-get update && sudo apt-get install -y clang-12 lldb-12 lld-12 libgtest-dev cmake gdb libstdc++6-11-dbg && cd /usr/src/gtest && export CC=clang-12 && export CXX=clang++-12 && sudo cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 . && sudo make -j ${{env.proc_num}} && sudo mv lib/libgtest* /usr/lib/
99
shell: bash
1010
- run: sudo git clone https://github.com/libunwind/libunwind.git && cd libunwind && sudo git checkout tags/v1.8.1 && sudo mkdir -p /libunwind && sudo autoreconf -i && sudo CC=clang-12 CXX=clang++-12 ./configure --prefix=/libunwind && sudo make -j ${{env.proc_num}} && sudo make install
1111
shell: bash

.github/workflows/ci-linux.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
export CC=gcc && export CXX=g++
3737
mkdir build
3838
cd build
39-
cmake ..
39+
cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
4040
- name: compile
4141
run: |
4242
cd build
@@ -76,7 +76,7 @@ jobs:
7676
export CC=gcc && export CXX=g++
7777
mkdir build
7878
cd build
79-
cmake -DWITH_MESALINK=OFF -DWITH_GLOG=ON -DWITH_THRIFT=ON -DWITH_RDMA=ON -DWITH_DEBUG_BTHREAD_SCHE_SAFETY=ON -DWITH_DEBUG_LOCK=ON -DWITH_BTHREAD_TRACER=ON -DWITH_ASAN=ON ..
79+
cmake -DWITH_MESALINK=OFF -DWITH_GLOG=ON -DWITH_THRIFT=ON -DWITH_RDMA=ON -DWITH_DEBUG_BTHREAD_SCHE_SAFETY=ON -DWITH_DEBUG_LOCK=ON -DWITH_BTHREAD_TRACER=ON -DWITH_ASAN=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
8080
- name: compile
8181
run: |
8282
cd build
@@ -110,7 +110,7 @@ jobs:
110110
export CC=clang && export CXX=clang++
111111
mkdir build
112112
cd build
113-
cmake ..
113+
cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
114114
- name: compile
115115
run: |
116116
cd build
@@ -150,7 +150,7 @@ jobs:
150150
export CC=clang && export CXX=clang++
151151
mkdir build
152152
cd build
153-
cmake -DWITH_MESALINK=OFF -DWITH_GLOG=ON -DWITH_THRIFT=ON -DWITH_RDMA=ON -DWITH_DEBUG_BTHREAD_SCHE_SAFETY=ON -DWITH_DEBUG_LOCK=ON -DWITH_BTHREAD_TRACER=ON -DWITH_ASAN=ON ..
153+
cmake -DWITH_MESALINK=OFF -DWITH_GLOG=ON -DWITH_THRIFT=ON -DWITH_RDMA=ON -DWITH_DEBUG_BTHREAD_SCHE_SAFETY=ON -DWITH_DEBUG_LOCK=ON -DWITH_BTHREAD_TRACER=ON -DWITH_ASAN=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
154154
- name: compile
155155
run: |
156156
cd build

docs/cn/heap_profiler.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ brpc还提供一个类似的growth profiler分析内存的分配去向(不考
121121
4. 相关gflags说明:
122122
- FLAGS_je_prof_active:true:开启采样,false:关闭采样。
123123
- FLAGS_je_prof_dump:修改值会生成heap文件,用于手动操作jeprof分析。
124-
- FLAGS_je_prof_reset:清理已采样数据和重置profiler选项,并且动态设置采样率,[默认](https://jemalloc.net/jemalloc.3.html#opt.lg_prof_sample)2^19B(512K),对性能影响可忽略。
124+
- FLAGS_je_prof_reset:清理已采样数据,并且动态设置采样率,[默认](https://jemalloc.net/jemalloc.3.html#opt.lg_prof_sample)2^19B(512K),对性能影响可忽略。
125125
5. 若要做memory leak:
126126
- `MALLOC_CONF="prof:true,prof_leak:true,prof_final:true" LD_PRELOAD=/xxx/lib/libjemalloc.so ./bin/test_server` ,进程退出时生成heap文件。
127127
- 注:可`kill pid`优雅退出,不可`kill -9 pid`;可用`FLAGS_graceful_quit_on_sigterm=true FLAGS_graceful_quit_on_sighup=true`来支持优雅退出。
@@ -144,6 +144,8 @@ brpc还提供一个类似的growth profiler分析内存的分配去向(不考
144144

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

147+
- 分配对象个数`curl ip:port/pprof/heap?display=svg&extra_options=inuse_objects`
148+
147149
- curl生成火焰图`curl ip:port/pprof/heap?display=flamegraph`。需配置env FLAMEGRAPH_PL_PATH=/xxx/flamegraph.pl,[flamegraph](https://github.com/brendangregg/FlameGraph)
148150

149151
![img](../images/curl_jeprof_flamegraph.png)

docs/cn/sanitizers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
ASan提供了[对协程的支持](https://reviews.llvm.org/D20913)。 在bthread创建、切换、销毁时,让ASan知道当前bthread的栈信息,主要用于维护[fake stack](https://github.com/google/sanitizers/wiki/AddressSanitizerUseAfterReturn)
88

9-
bRPC中启用ASan的方法:给config_brpc.sh增加`--with-glog`选项、给cmake增加`-DWITH_GLOG=ON`选项或者给bazel增加`--define with_asan=true`选项。
9+
bRPC中启用ASan的方法:给config_brpc.sh增加`--with-asan`选项、给cmake增加`-DWITH_ASAN=ON`选项或者给bazel增加`--define with_asan=true`选项。
1010

1111
另外需要注意的是,ASan没法检测非ASan分配内存或者对象池复用内存。所以我们封装了两个宏,让ASan知道内存块是否能被使用。在非ASan环境下,这两个宏什么也不做,没有开销。
1212

src/brpc/details/jemalloc_profiler.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,12 @@ static int JeProfileReset(size_t lg_sample) {
163163
return ret;
164164
}
165165
LOG(INFO) << "mallctl set prof.reset:" << lg_sample << " succ";
166-
167-
FLAGS_je_prof_active = false;
168-
if (FLAGS_je_prof_active) {
169-
LOG(WARNING) << "reset FLAGS_je_prof_active fail";
170-
return -1;
171-
}
172-
173166
return 0;
174167
}
175168

176169
void JeControlProfile(Controller* cntl) {
177170
const brpc::URI& uri = cntl->http_request().uri();
178-
// http:ip:port/pprof/heap?display=(text|svg|stats|flamegraph)
171+
// http:ip:port/pprof/heap?display=(text|svg|stats|flamegraph)&extra_options=(inuse_space|inuse_objects..)
179172
const std::string* uri_display = uri.GetQuery("display");
180173

181174
butil::IOBuf& buf = cntl->response_attachment();
@@ -236,6 +229,14 @@ void JeControlProfile(Controller* cntl) {
236229
const std::string process_file(process_path, len);
237230

238231
std::string cmd_str = jeprof + " " + process_file + " " + prof_name;
232+
233+
// https://github.com/jemalloc/jemalloc/blob/5.3.0/bin/jeprof.in#L211-L222
234+
// e.g: inuse_space, contentions
235+
const std::string* uri_extra_options = uri.GetQuery("extra_options");
236+
if (uri_extra_options != nullptr && !uri_extra_options->empty()) {
237+
cmd_str += " --" + *uri_extra_options + " ";
238+
}
239+
239240
bool display_img = false;
240241
if (*uri_display == "svg") {
241242
cmd_str += " --svg ";

src/bthread/bthread.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pthread_mutex_t g_task_control_mutex = PTHREAD_MUTEX_INITIALIZER;
7070
TaskControl* g_task_control = NULL;
7171

7272
extern BAIDU_THREAD_LOCAL TaskGroup* tls_task_group;
73+
EXTERN_BAIDU_VOLATILE_THREAD_LOCAL(TaskGroup*, tls_task_group);
7374
extern void (*g_worker_startfn)();
7475
extern void (*g_tagged_worker_startfn)(bthread_tag_t);
7576
extern void* (*g_create_span_func)();
@@ -528,15 +529,15 @@ int bthread_timer_del(bthread_timer_t id) {
528529
}
529530

530531
int bthread_usleep(uint64_t microseconds) {
531-
bthread::TaskGroup* g = bthread::tls_task_group;
532+
bthread::TaskGroup* g = bthread::BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group);
532533
if (NULL != g && !g->is_current_pthread_task()) {
533534
return bthread::TaskGroup::usleep(&g, microseconds);
534535
}
535536
return ::usleep(microseconds);
536537
}
537538

538539
int bthread_yield(void) {
539-
bthread::TaskGroup* g = bthread::tls_task_group;
540+
bthread::TaskGroup* g = bthread::BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group);
540541
if (NULL != g && !g->is_current_pthread_task()) {
541542
bthread::TaskGroup::yield(&g);
542543
return 0;

src/bthread/stack_inl.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ BUTIL_FORCE_INLINE void FinishSwitchFiber(void* fake_stack_save) {
7070

7171
class ScopedASanFiberSwitcher {
7272
public:
73-
ScopedASanFiberSwitcher(StackStorage& storage, bool ending) {
74-
// If bthread will be quit here, pass NULL as `fake_stack_save',
75-
// so that ASan knows it can destroy the fake stack.
76-
StartSwitchFiber(ending ? NULL : &_fake_stack, storage);
73+
ScopedASanFiberSwitcher(StackStorage& next_storage) {
74+
StartSwitchFiber(&_fake_stack, next_storage);
7775
}
7876

7977
~ScopedASanFiberSwitcher() {
@@ -92,16 +90,16 @@ class ScopedASanFiberSwitcher {
9290
#define BTHREAD_ASAN_UNPOISON_MEMORY_REGION(storage) \
9391
::bthread::internal::ASanUnpoisonMemoryRegion(storage)
9492

95-
#define BTHREAD_SCOPED_ASAN_FIBER_SWITCHER(storage, ending) \
96-
::bthread::internal::ScopedASanFiberSwitcher switcher(storage, ending)
93+
#define BTHREAD_SCOPED_ASAN_FIBER_SWITCHER(storage) \
94+
::bthread::internal::ScopedASanFiberSwitcher switcher(storage)
9795

9896
} // namespace internal
9997
#else
10098

10199
// If ASan are used, the annotations should be no-ops.
102100
#define BTHREAD_ASAN_POISON_MEMORY_REGION(storage) ((void)(storage))
103101
#define BTHREAD_ASAN_UNPOISON_MEMORY_REGION(storage) ((void)(storage))
104-
#define BTHREAD_SCOPED_ASAN_FIBER_SWITCHER(storage, ending) ((void)(storage), (void)(ending))
102+
#define BTHREAD_SCOPED_ASAN_FIBER_SWITCHER(storage) ((void)(storage))
105103

106104
#endif // BUTIL_USE_ASAN
107105

src/bthread/task_group.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,7 @@ void TaskGroup::sched_to(TaskGroup** pg, TaskMeta* next_meta, bool cur_ending) {
721721
g->_control->_task_tracer.set_status(TASK_STATUS_JUMPING, next_meta);
722722
#endif // BRPC_BTHREAD_TRACER
723723
{
724-
BTHREAD_SCOPED_ASAN_FIBER_SWITCHER(
725-
cur_meta->stack->storage, cur_ending);
724+
BTHREAD_SCOPED_ASAN_FIBER_SWITCHER(next_meta->stack->storage);
726725
jump_stack(cur_meta->stack, next_meta->stack);
727726
}
728727
// probably went to another group, need to assign g again.

test/brpc_load_balancer_unittest.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <gtest/gtest.h>
2525
#include "bthread/bthread.h"
2626
#include "gperftools_helper.h"
27+
#include "butil/compiler_specific.h"
2728
#include "butil/containers/doubly_buffered_data.h"
2829
#include "brpc/describable.h"
2930
#include "brpc/socket.h"
@@ -1155,6 +1156,7 @@ TEST_F(LoadBalancerTest, revived_from_all_failed_sanity) {
11551156
}
11561157
}
11571158

1159+
#ifndef BUTIL_USE_ASAN
11581160
class EchoServiceImpl : public test::EchoService {
11591161
public:
11601162
EchoServiceImpl()
@@ -1251,14 +1253,14 @@ TEST_F(LoadBalancerTest, revived_from_all_failed_intergrated) {
12511253
}
12521254

12531255
butil::EndPoint point(butil::IP_ANY, 7777);
1254-
brpc::Server server;
12551256
EchoServiceImpl service;
1257+
brpc::Server server;
12561258
ASSERT_EQ(0, server.AddService(&service, brpc::SERVER_DOESNT_OWN_SERVICE));
12571259
ASSERT_EQ(0, server.Start(point, NULL));
12581260

12591261
butil::EndPoint point2(butil::IP_ANY, 7778);
1260-
brpc::Server server2;
12611262
EchoServiceImpl service2;
1263+
brpc::Server server2;
12621264
ASSERT_EQ(0, server2.AddService(&service2, brpc::SERVER_DOESNT_OWN_SERVICE));
12631265
ASSERT_EQ(0, server2.Start(point2, NULL));
12641266

@@ -1287,6 +1289,7 @@ TEST_F(LoadBalancerTest, revived_from_all_failed_intergrated) {
12871289
bthread_usleep(500000 /* sleep longer than timeout of channel */);
12881290
ASSERT_EQ(0, num_failed.load(butil::memory_order_relaxed));
12891291
}
1292+
#endif // BUTIL_USE_ASAN
12901293

12911294
TEST_F(LoadBalancerTest, la_selection_too_long) {
12921295
brpc::GlobalInitializeOrDie();

test/bthread_cond_unittest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ static void launch_many_bthreads() {
445445
}
446446

447447
TEST(CondTest, too_many_bthreads_from_pthread) {
448+
bthread_setconcurrency(16);
448449
launch_many_bthreads();
449450
}
450451

@@ -454,6 +455,7 @@ static void* run_launch_many_bthreads(void*) {
454455
}
455456

456457
TEST(CondTest, too_many_bthreads_from_bthread) {
458+
bthread_setconcurrency(16);
457459
bthread_t th;
458460
ASSERT_EQ(0, bthread_start_urgent(&th, NULL, run_launch_many_bthreads, NULL));
459461
bthread_join(th, NULL);

0 commit comments

Comments
 (0)