Skip to content

Commit

Permalink
Add gperftools helper header
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright committed Feb 11, 2025
1 parent cf01b5c commit 6c605ab
Show file tree
Hide file tree
Showing 30 changed files with 83 additions and 183 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- run: bazel test --verbose_failures --define with_mesalink=false --define with_glog=true --define with_thrift=true --define with_debug_bthread_sche_safety=true --define with_debug_lock=true --define with_asan=true -- //... -//example/...
- run: bazel build --verbose_failures --define with_mesalink=false --define with_glog=true --define with_thrift=true --define with_debug_bthread_sche_safety=true --define with_debug_lock=true --define with_asan=true -- //... -//example/...

clang-compile-with-make:
runs-on: ubuntu-20.04
Expand Down
2 changes: 1 addition & 1 deletion config_brpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ if [ -z "$TCMALLOC_LIB" ]; then
elif [ $WITH_ASAN != 0 ]; then
append_to_output " \$(error \"gperftools is not compatible with ASAN\")"
else
append_to_output " CPPFLAGS+=-DBRPC_WITH_GPERFTOOLS"
append_to_output " CPPFLAGS+=-DBRPC_ENABLE_CPU_PROFILER"
append_to_output_libs "$TCMALLOC_LIB" " "
if [ -f $TCMALLOC_LIB/libtcmalloc.$SO ]; then
append_to_output " DYNAMIC_LINKINGS+=-ltcmalloc_and_profiler"
Expand Down
2 changes: 2 additions & 0 deletions docs/cn/sanitizers.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ BUTIL_ASAN_POISON_MEMORY_REGION(addr, size);
BUTIL_ASAN_UNPOISON_MEMORY_REGION(addr, size);
```
其他问题:如果ASan报告中new/delete的调用栈不完整,可以通过设置`fast_unwind_on_malloc=0`回溯出完整的调用栈了。需要注意的是`fast_unwind_on_malloc=0`很耗性能。
## ThreadSanitizer(TSan)
待支持(TODO)
Expand Down
4 changes: 2 additions & 2 deletions src/bthread/task_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ int TaskGroup::init(size_t runqueue_capacity) {
}

#ifdef BUTIL_USE_ASAN
void TaskGroup::bthread_task_runner(intptr_t) {
void TaskGroup::asan_task_runner(intptr_t) {
// This is a new thread, and it doesn't have the fake stack yet. ASan will
// create it lazily, for now just pass NULL.
internal::FinishSwitchFiber(NULL);
Expand Down Expand Up @@ -623,7 +623,7 @@ void TaskGroup::ending_sched(TaskGroup** pg) {
} else {
#ifdef BUTIL_USE_ASAN
ContextualStack* stk = get_stack(
next_meta->stack_type(), bthread_task_runner);
next_meta->stack_type(), asan_task_runner);
#else
ContextualStack* stk = get_stack(next_meta->stack_type(), task_runner);
#endif // BUTIL_USE_ASAN
Expand Down
2 changes: 1 addition & 1 deletion src/bthread/task_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ friend class TaskControl;
~TaskGroup();

#ifdef BUTIL_USE_ASAN
static void bthread_task_runner(intptr_t);
static void asan_task_runner(intptr_t);
#endif // BUTIL_USE_ASAN
static void task_runner(intptr_t skip_remained);

Expand Down
2 changes: 1 addition & 1 deletion src/bthread/task_group_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ inline void TaskGroup::sched_to(TaskGroup** pg, bthread_t next_tid) {
TaskMeta* next_meta = address_meta(next_tid);
if (next_meta->stack == NULL) {
#ifdef BUTIL_USE_ASAN
ContextualStack* stk = get_stack(next_meta->stack_type(), bthread_task_runner);
ContextualStack* stk = get_stack(next_meta->stack_type(), asan_task_runner);
#else
ContextualStack* stk = get_stack(next_meta->stack_type(), task_runner);
#endif // BUTIL_USE_ASAN
Expand Down
2 changes: 1 addition & 1 deletion src/butil/debug/address_annotations.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int __asan_address_is_poisoned(void const volatile *addr);
__sanitizer_finish_switch_fiber(fake_stack_save, bottom_old, size_old)

#else
// If ASan are used, the annotations should be no-ops.
// If ASan is not used, these annotations are no-ops.
#define BUTIL_ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
#define BUTIL_ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
#define BUTIL_ASAN_START_SWITCH_FIBER(fake_stack_save, bottom, size) \
Expand Down
2 changes: 1 addition & 1 deletion src/butil/object_pool_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
#define OBJECT_POOL_ASAN_POISON_MEMORY_REGION(ptr) asan_poison_memory_region(ptr)
#define OBJECT_POOL_ASAN_UNPOISON_MEMORY_REGION(ptr) asan_unpoison_memory_region(ptr)
#else
#define OBJECT_POOL_ASAN_POISON_MEMORY_REGION(ptr) ((void)(ptr))
#define OBJECT_POOL_ASAN_POISON_MEMORY_REGION(ptr) ((void)(ptr))
#define OBJECT_POOL_ASAN_UNPOISON_MEMORY_REGION(ptr) ((void)(ptr))
#endif // BUTIL_USE_ASAN

Expand Down
10 changes: 10 additions & 0 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ cc_library(
],
)

cc_library(
name = "gperftools_helper",
hdrs = [
"gperftools_helper.h",
],
)

cc_test(
name = "butil_test",
srcs = TEST_BUTIL_SOURCES + [
Expand All @@ -187,6 +194,7 @@ cc_test(
deps = [
":cc_test_proto",
":sstream_workaround",
":gperftools_helper",
"//:brpc",
"@com_google_googletest//:gtest",
],
Expand All @@ -206,6 +214,7 @@ cc_test(
copts = COPTS,
deps = [
":sstream_workaround",
":gperftools_helper",
"//:bvar",
"@com_google_googletest//:gtest",
],
Expand Down Expand Up @@ -234,6 +243,7 @@ cc_test(
copts = COPTS,
deps = [
":sstream_workaround",
":gperftools_helper",
"//:brpc",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
Expand Down
12 changes: 5 additions & 7 deletions test/brpc_builtin_service_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
#include <gtest/gtest.h>
#include <gflags/gflags.h>
#include <google/protobuf/descriptor.h>
#ifdef BRPC_WITH_GPERFTOOLS
#include "butil/gperftools_profiler.h"
#endif // BRPC_WITH_GPERFTOOLS
#include "gperftools_helper.h"
#include "butil/time.h"
#include "butil/macros.h"
#include "brpc/socket.h"
Expand Down Expand Up @@ -719,7 +717,7 @@ TEST_F(BuiltinServiceTest, rpcz) {
}
}

#ifdef BRPC_WITH_GPERFTOOLS
#if defined(BRPC_ENABLE_CPU_PROFILER) || defined(BAIDU_RPC_ENABLE_CPU_PROFILER)
TEST_F(BuiltinServiceTest, pprof) {
brpc::PProfService service;
{
Expand Down Expand Up @@ -761,7 +759,7 @@ TEST_F(BuiltinServiceTest, pprof) {
CheckContent(cntl, "brpc_builtin_service_unittest");
}
}
#endif // BRPC_WITH_GPERFTOOLS
#endif // BRPC_ENABLE_CPU_PROFILER || BAIDU_RPC_ENABLE_CPU_PROFILER

TEST_F(BuiltinServiceTest, dir) {
brpc::DirService service;
Expand Down Expand Up @@ -937,7 +935,7 @@ TEST_F(BuiltinServiceTest, sockets) {
}
}

#ifdef BRPC_WITH_GPERFTOOLS
#if defined(BRPC_ENABLE_CPU_PROFILER) || defined(BAIDU_RPC_ENABLE_CPU_PROFILER)
TEST_F(BuiltinServiceTest, memory) {
brpc::MemoryService service;
brpc::MemoryRequest req;
Expand All @@ -955,4 +953,4 @@ TEST_F(BuiltinServiceTest, memory) {
CheckContent(cntl, "tcmalloc.pageheap_free_bytes");
CheckContent(cntl, "tcmalloc.pageheap_unmapped_bytes");
}
#endif // BRPC_WITH_GPERFTOOLS
#endif // BRPC_ENABLE_CPU_PROFILER || BAIDU_RPC_ENABLE_CPU_PROFILER
8 changes: 1 addition & 7 deletions test/brpc_event_dispatcher_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <gtest/gtest.h>
#ifdef BRPC_WITH_GPERFTOOLS
#include "butil/gperftools_profiler.h"
#endif // BRPC_WITH_GPERFTOOLS
#include "gperftools_helper.h"
#include "butil/time.h"
#include "butil/macros.h"
#include "butil/fd_utility.h"
Expand Down Expand Up @@ -344,17 +342,13 @@ TEST_F(EventDispatcherTest, dispatch_tasks) {
}

LOG(INFO) << "Begin to profile... (5 seconds)";
#ifdef BRPC_WITH_GPERFTOOLS
ProfilerStart("event_dispatcher.prof");
#endif // BRPC_WITH_GPERFTOOLS
butil::Timer tm;
tm.start();
sleep(5);
tm.stop();
#ifdef BRPC_WITH_GPERFTOOLS
ProfilerStop();
LOG(INFO) << "End profiling";
#endif // BRPC_WITH_GPERFTOOLS

size_t client_bytes = 0;
size_t server_bytes = 0;
Expand Down
8 changes: 1 addition & 7 deletions test/brpc_h2_unsent_message_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
#include "butil/atomicops.h"
#include "brpc/policy/http_rpc_protocol.h"
#include "brpc/policy/http2_rpc_protocol.h"
#ifdef BRPC_WITH_GPERFTOOLS
#include "butil/gperftools_profiler.h"
#endif // BRPC_WITH_GPERFTOOLS
#include "gperftools_helper.h"

int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
Expand Down Expand Up @@ -58,18 +56,14 @@ TEST(H2UnsentMessage, request_throughput) {

// calc H2UnsentRequest throughput
butil::IOBuf dummy_buf;
#ifdef BRPC_WITH_GPERFTOOLS
ProfilerStart("h2_unsent_req.prof");
#endif // BRPC_WITH_GPERFTOOLS
int64_t start_us = butil::gettimeofday_us();
for (int i = 0; i < ntotal; ++i) {
brpc::policy::H2UnsentRequest* req = brpc::policy::H2UnsentRequest::New(&cntl);
req->AppendAndDestroySelf(&dummy_buf, h2_client_sock.get());
}
int64_t end_us = butil::gettimeofday_us();
#ifdef BRPC_WITH_GPERFTOOLS
ProfilerStop();
#endif // BRPC_WITH_GPERFTOOLS
int64_t elapsed = end_us - start_us;
LOG(INFO) << "H2UnsentRequest average qps="
<< (ntotal * 1000000L) / elapsed << "/s, data throughput="
Expand Down
4 changes: 2 additions & 2 deletions test/brpc_http_rpc_protocol_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ TEST_F(HttpTest, broken_socket_stops_progressive_reading) {
ASSERT_EQ(ECONNRESET, reader->destroying_status().error_code());
}

#ifdef BRPC_WITH_GPERFTOOLS
#ifndef BUTIL_USE_ASAN
static const std::string TEST_PROGRESSIVE_HEADER = "Progressive";
static const std::string TEST_PROGRESSIVE_HEADER_VAL = "Progressive-val";

Expand Down Expand Up @@ -1187,7 +1187,7 @@ TEST_F(HttpTest, server_end_read_failed) {
channel.CallMethod(NULL, &cntl, NULL, NULL, NULL);
ASSERT_TRUE(cntl.Failed());
}
#endif // BRPC_WITH_GPERFTOOLS
#endif // BUTIL_USE_ASAN

TEST_F(HttpTest, http2_sanity) {
const int port = 8923;
Expand Down
9 changes: 1 addition & 8 deletions test/brpc_input_messenger_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
#include <sys/socket.h>
#include <netdb.h>
#include <gtest/gtest.h>
#ifdef BRPC_WITH_GPERFTOOLS
#include "butil/gperftools_profiler.h"
#endif // BRPC_WITH_GPERFTOOLS
#include "gperftools_helper.h"
#include "butil/time.h"
#include "butil/macros.h"
#include "butil/fd_utility.h"
Expand Down Expand Up @@ -184,10 +182,8 @@ TEST_F(MessengerTest, dispatch_tasks) {
sleep(1);


#ifdef BRPC_WITH_GPERFTOOLS
LOG(INFO) << "Begin to profile... (5 seconds)";
ProfilerStart("input_messenger.prof");
#endif // BRPC_WITH_GPERFTOOLS

size_t start_client_bytes = 0;
for (size_t i = 0; i < NCLIENT; ++i) {
Expand All @@ -199,11 +195,8 @@ TEST_F(MessengerTest, dispatch_tasks) {
sleep(5);

tm.stop();

#ifdef BRPC_WITH_GPERFTOOLS
ProfilerStop();
LOG(INFO) << "End profiling";
#endif // BRPC_WITH_GPERFTOOLS

client_stop = true;

Expand Down
12 changes: 1 addition & 11 deletions test/brpc_load_balancer_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
#include <map>
#include <gtest/gtest.h>
#include "bthread/bthread.h"
#ifdef BRPC_WITH_GPERFTOOLS
#include "butil/gperftools_profiler.h"
#endif // BRPC_WITH_GPERFTOOLS
#include "gperftools_helper.h"
#include "butil/containers/doubly_buffered_data.h"
#include "brpc/describable.h"
#include "brpc/socket.h"
Expand Down Expand Up @@ -252,9 +250,7 @@ void PerfTest(int thread_num, bool modify_during_reading) {
g_started = true;
char prof_name[32];
snprintf(prof_name, sizeof(prof_name), "doubly_buffered_data_%d.prof", ++g_prof_name_counter);
#ifdef BRPC_WITH_GPERFTOOLS
ProfilerStart(prof_name);
#endif // BRPC_WITH_GPERFTOOLS
int64_t run_ms = 5 * 1000;
if (modify_during_reading) {
int64_t start = butil::gettimeofday_ms();
Expand All @@ -266,9 +262,7 @@ void PerfTest(int thread_num, bool modify_during_reading) {
} else {
usleep(run_ms * 1000);
}
#ifdef BRPC_WITH_GPERFTOOLS
ProfilerStop();
#endif // BRPC_WITH_GPERFTOOLS
g_stopped = true;
int64_t wait_time = 0;
int64_t count = 0;
Expand Down Expand Up @@ -646,13 +640,9 @@ TEST_F(LoadBalancerTest, fairness) {
ASSERT_EQ(0, pthread_create(&th[i], NULL, select_server, &sa));
}
bthread_usleep(10000);
#ifdef BRPC_WITH_GPERFTOOLS
ProfilerStart((lb_name + ".prof").c_str());
#endif // BRPC_WITH_GPERFTOOLS
bthread_usleep(300000);
#ifdef BRPC_WITH_GPERFTOOLS
ProfilerStop();
#endif // BRPC_WITH_GPERFTOOLS

global_stop = true;

Expand Down
Loading

0 comments on commit 6c605ab

Please sign in to comment.