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

Opt contention: support tcmalloc GetStackTrace and reduce hash calculations #2488

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ LINKOPTS = [
"-Wl,-U,_MallocExtension_ReleaseFreeMemory",
"-Wl,-U,_ProfilerStart",
"-Wl,-U,_ProfilerStop",
"-Wl,-U,__Z13GetStackTracePPvii",
"-Wl,-U,_RegisterThriftProtocol",
],
"//conditions:default": [
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-framework Foundation"
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop")
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
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 @@ -196,6 +196,7 @@ if [ "$SYSTEM" = "Darwin" ]; then
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_MallocExtension_ReleaseFreeMemory"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStart"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStop"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,__Z13GetStackTracePPvii"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_RegisterThriftProtocol"
fi
append_linking() {
Expand Down
3 changes: 2 additions & 1 deletion example/asynchronous_echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-framework Foundation"
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop")
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
endif()

add_executable(asynchronous_echo_client client.cpp ${PROTO_SRC})
Expand Down
1 change: 1 addition & 0 deletions example/auto_concurrency_limiter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_RegisterThriftProtocol")
endif()

Expand Down
3 changes: 2 additions & 1 deletion example/backup_request_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-framework Foundation"
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop")
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
endif()

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

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

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

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

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

add_executable(echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
1 change: 1 addition & 0 deletions example/grpc_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii"
"-Wl,-U,_RegisterThriftProtocol")
endif()

Expand Down
3 changes: 2 additions & 1 deletion example/http_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-framework Foundation"
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop")
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
endif()

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

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

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

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

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

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

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

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

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

add_executable(redis_cli redis_cli.cpp)
Expand Down
3 changes: 2 additions & 1 deletion example/selective_echo_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
"-framework Foundation"
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop")
"-Wl,-U,_ProfilerStop"
"-Wl,-U,__Z13GetStackTracePPvii")
endif()

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

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

add_executable(streaming_echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
Expand Down
4 changes: 3 additions & 1 deletion src/brpc/builtin/index_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ void IndexService::default_method(::google::protobuf::RpcController* controller,
<< (!IsHeapProfilerEnabled() ? " (disabled)" : "") << NL
<< Path("/hotspots/growth", html_addr)
<< " : Profiling growth of heap"
<< (!IsHeapProfilerEnabled() ? " (disabled)" : "") << NL;
<< (!IsHeapProfilerEnabled() ? " (disabled)" : "") << NL
<< Path("/hotspots/contention", html_addr)
<< " : Profiling contention of lock" << NL;
}
os << "curl -H 'Content-Type: application/json' -d 'JSON' ";
if (butil::is_endpoint_extended(server->listen_address())) {
Expand Down
32 changes: 23 additions & 9 deletions src/bthread/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@
extern "C" {
extern void* __attribute__((weak)) _dl_sym(void* handle, const char* symbol, void* caller);
}
extern int __attribute__((weak)) GetStackTrace(void** result, int max_depth, int skip_count);

namespace bthread {
// Warm up backtrace before main().
void* dummy_buf[4];
const int ALLOW_UNUSED dummy_bt = backtrace(dummy_buf, arraysize(dummy_buf));
const int ALLOW_UNUSED dummy_bt = GetStackTrace
? GetStackTrace(dummy_buf, arraysize(dummy_buf), 0)
: backtrace(dummy_buf, arraysize(dummy_buf));

// For controlling contentions collected per second.
static bvar::CollectorSpeedLimit g_cp_sl = BVAR_COLLECTOR_SPEED_LIMIT_INITIALIZER;
Expand All @@ -64,24 +67,32 @@ struct SampledContention : public bvar::Collected {
int64_t duration_ns;
// number of samples, normalized according to to sampling_range
double count;
int nframes; // #elements in stack
void* stack[26]; // backtrace.
int nframes; // #elements in stack

// Implement bvar::Collected
void dump_and_destroy(size_t round) override;
void destroy() override;
bvar::CollectorSpeedLimit* speed_limit() override { return &g_cp_sl; }

// For combining samples with hashmap.
size_t hash_code() const {
if (nframes == 0) {
return 0;
}
uint32_t code = 1;
uint32_t seed = nframes;
butil::MurmurHash3_x86_32(stack, sizeof(void*) * nframes, seed, &code);
return code;
if (_hash_code == 0) {
_hash_code = 1;
uint32_t seed = nframes;
butil::MurmurHash3_x86_32(stack, sizeof(void*) * nframes, seed, &_hash_code);
}
return _hash_code;
}
private:
friend butil::ObjectPool<SampledContention>;
SampledContention()
: duration_ns(0), count(0), stack{NULL}, nframes(0), _hash_code(0) {}
~SampledContention() override = default;

mutable uint32_t _hash_code; // For combining samples with hashmap.
};

BAIDU_CASSERT(sizeof(SampledContention) == 256, be_friendly_to_allocator);
Expand Down Expand Up @@ -294,6 +305,7 @@ void SampledContention::dump_and_destroy(size_t /*round*/) {
}

void SampledContention::destroy() {
_hash_code = 0;
butil::return_object(this);
}

Expand Down Expand Up @@ -512,14 +524,16 @@ inline bool remove_pthread_contention_site(
// Submit the contention along with the callsite('s stacktrace)
void submit_contention(const bthread_contention_site_t& csite, int64_t now_ns) {
tls_inside_lock = true;
SampledContention* sc = butil::get_object<SampledContention>();
auto sc = butil::get_object<SampledContention>();
// Normalize duration_us and count so that they're addable in later
// processings. Notice that sampling_range is adjusted periodically by
// collecting thread.
sc->duration_ns = csite.duration_ns * bvar::COLLECTOR_SAMPLING_BASE
/ csite.sampling_range;
sc->count = bvar::COLLECTOR_SAMPLING_BASE / (double)csite.sampling_range;
sc->nframes = backtrace(sc->stack, arraysize(sc->stack)); // may lock
sc->nframes = GetStackTrace
? GetStackTrace(sc->stack, arraysize(sc->stack), 0)
: backtrace(sc->stack, arraysize(sc->stack)); // may lock
sc->submit(now_ns / 1000); // may lock
tls_inside_lock = false;
}
Expand Down
14 changes: 10 additions & 4 deletions src/butil/debug/stack_trace_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include "butil/third_party/symbolize/symbolize.h"
#endif

extern int __attribute__((weak)) GetStackTrace(void** result, int max_depth, int skip_count);

namespace butil {
namespace debug {

Expand Down Expand Up @@ -748,13 +750,17 @@ StackTrace::StackTrace() {
// NOTE: This code MUST be async-signal safe (it's used by in-process
// stack dumping signal handler). NO malloc or stdio is allowed here.

if (GetStackTrace) {
count_ = GetStackTrace(trace_, arraysize(trace_), 0);
} else {
#if !defined(__UCLIBC__)
// Though the backtrace API man page does not list any possible negative
// return values, we take no chance.
count_ = butil::saturated_cast<size_t>(backtrace(trace_, arraysize(trace_)));
// Though the backtrace API man page does not list any possible negative
// return values, we take no chance.
count_ = butil::saturated_cast<size_t>(backtrace(trace_, arraysize(trace_)));
#else
count_ = 0;
count_ = 0;
#endif
}
}

void StackTrace::Print() const {
Expand Down
Loading