This is an automated email from the ASF dual-hosted git repository.

wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 09141d4d Opt contention: support tcmalloc GetStackTrace and reduce 
hash calculations (#2488)
09141d4d is described below

commit 09141d4d87e2a68736144ebc6dad55eba449c6cc
Author: Bright Chen <chenguangmin...@foxmail.com>
AuthorDate: Wed Jan 17 10:28:21 2024 +0800

    Opt contention: support tcmalloc GetStackTrace and reduce hash calculations 
(#2488)
---
 BUILD.bazel                                        |  1 +
 CMakeLists.txt                                     |  3 +-
 config_brpc.sh                                     |  1 +
 example/asynchronous_echo_c++/CMakeLists.txt       |  3 +-
 example/auto_concurrency_limiter/CMakeLists.txt    |  1 +
 example/backup_request_c++/CMakeLists.txt          |  3 +-
 example/bthread_tag_echo_c++/CMakeLists.txt        |  3 +-
 example/cancel_c++/CMakeLists.txt                  |  3 +-
 example/cascade_echo_c++/CMakeLists.txt            |  3 +-
 example/dynamic_partition_echo_c++/CMakeLists.txt  |  3 +-
 example/echo_c++/CMakeLists.txt                    |  3 +-
 example/grpc_c++/CMakeLists.txt                    |  1 +
 example/http_c++/CMakeLists.txt                    |  3 +-
 example/memcache_c++/CMakeLists.txt                |  3 +-
 example/multi_threaded_echo_c++/CMakeLists.txt     |  3 +-
 example/multi_threaded_echo_fns_c++/CMakeLists.txt |  3 +-
 example/nshead_extension_c++/CMakeLists.txt        |  3 +-
 example/nshead_pb_extension_c++/CMakeLists.txt     |  3 +-
 example/parallel_echo_c++/CMakeLists.txt           |  3 +-
 example/partition_echo_c++/CMakeLists.txt          |  3 +-
 example/rdma_performance/CMakeLists.txt            |  3 +-
 example/redis_c++/CMakeLists.txt                   |  3 +-
 example/selective_echo_c++/CMakeLists.txt          |  3 +-
 .../session_data_and_thread_local/CMakeLists.txt   |  3 +-
 example/streaming_echo_c++/CMakeLists.txt          |  3 +-
 src/brpc/builtin/index_service.cpp                 |  4 ++-
 src/bthread/mutex.cpp                              | 32 ++++++++++++++++------
 src/butil/debug/stack_trace_posix.cc               | 14 +++++++---
 28 files changed, 82 insertions(+), 35 deletions(-)

diff --git a/BUILD.bazel b/BUILD.bazel
index f8eaaa46..0bdaa4d3 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -61,6 +61,7 @@ LINKOPTS = [
         "-Wl,-U,_MallocExtension_ReleaseFreeMemory",
         "-Wl,-U,_ProfilerStart",
         "-Wl,-U,_ProfilerStop",
+        "-Wl,-U,__Z13GetStackTracePPvii",
         "-Wl,-U,_RegisterThriftProtocol",
     ],
     "//conditions:default": [
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4bf116b6..337701f3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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
diff --git a/config_brpc.sh b/config_brpc.sh
index a660171c..add316c2 100755
--- a/config_brpc.sh
+++ b/config_brpc.sh
@@ -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() {
diff --git a/example/asynchronous_echo_c++/CMakeLists.txt 
b/example/asynchronous_echo_c++/CMakeLists.txt
index 1c8da4e5..60854454 100644
--- a/example/asynchronous_echo_c++/CMakeLists.txt
+++ b/example/asynchronous_echo_c++/CMakeLists.txt
@@ -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})
diff --git a/example/auto_concurrency_limiter/CMakeLists.txt 
b/example/auto_concurrency_limiter/CMakeLists.txt
index 213b357b..c19a8f40 100644
--- a/example/auto_concurrency_limiter/CMakeLists.txt
+++ b/example/auto_concurrency_limiter/CMakeLists.txt
@@ -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()
 
diff --git a/example/backup_request_c++/CMakeLists.txt 
b/example/backup_request_c++/CMakeLists.txt
index 5a520a4d..7aab5e57 100644
--- a/example/backup_request_c++/CMakeLists.txt
+++ b/example/backup_request_c++/CMakeLists.txt
@@ -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})
diff --git a/example/bthread_tag_echo_c++/CMakeLists.txt 
b/example/bthread_tag_echo_c++/CMakeLists.txt
index 2baea53e..56aec0df 100644
--- a/example/bthread_tag_echo_c++/CMakeLists.txt
+++ b/example/bthread_tag_echo_c++/CMakeLists.txt
@@ -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})
diff --git a/example/cancel_c++/CMakeLists.txt 
b/example/cancel_c++/CMakeLists.txt
index c8f4d7f3..07d80b4c 100644
--- a/example/cancel_c++/CMakeLists.txt
+++ b/example/cancel_c++/CMakeLists.txt
@@ -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})
diff --git a/example/cascade_echo_c++/CMakeLists.txt 
b/example/cascade_echo_c++/CMakeLists.txt
index 56248abd..b6d8c0b7 100644
--- a/example/cascade_echo_c++/CMakeLists.txt
+++ b/example/cascade_echo_c++/CMakeLists.txt
@@ -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})
diff --git a/example/dynamic_partition_echo_c++/CMakeLists.txt 
b/example/dynamic_partition_echo_c++/CMakeLists.txt
index dbe7adda..2394b7fc 100644
--- a/example/dynamic_partition_echo_c++/CMakeLists.txt
+++ b/example/dynamic_partition_echo_c++/CMakeLists.txt
@@ -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})
diff --git a/example/echo_c++/CMakeLists.txt b/example/echo_c++/CMakeLists.txt
index badd49e1..7746957b 100644
--- a/example/echo_c++/CMakeLists.txt
+++ b/example/echo_c++/CMakeLists.txt
@@ -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})
diff --git a/example/grpc_c++/CMakeLists.txt b/example/grpc_c++/CMakeLists.txt
index c1d0817a..516735d6 100644
--- a/example/grpc_c++/CMakeLists.txt
+++ b/example/grpc_c++/CMakeLists.txt
@@ -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()
 
diff --git a/example/http_c++/CMakeLists.txt b/example/http_c++/CMakeLists.txt
index 8d0507d2..3fd9a088 100644
--- a/example/http_c++/CMakeLists.txt
+++ b/example/http_c++/CMakeLists.txt
@@ -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)
diff --git a/example/memcache_c++/CMakeLists.txt 
b/example/memcache_c++/CMakeLists.txt
index 4f3c2ed1..5941c344 100644
--- a/example/memcache_c++/CMakeLists.txt
+++ b/example/memcache_c++/CMakeLists.txt
@@ -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)
diff --git a/example/multi_threaded_echo_c++/CMakeLists.txt 
b/example/multi_threaded_echo_c++/CMakeLists.txt
index a3c47a35..c13b34bd 100644
--- a/example/multi_threaded_echo_c++/CMakeLists.txt
+++ b/example/multi_threaded_echo_c++/CMakeLists.txt
@@ -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})
diff --git a/example/multi_threaded_echo_fns_c++/CMakeLists.txt 
b/example/multi_threaded_echo_fns_c++/CMakeLists.txt
index 79077743..887541b8 100644
--- a/example/multi_threaded_echo_fns_c++/CMakeLists.txt
+++ b/example/multi_threaded_echo_fns_c++/CMakeLists.txt
@@ -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})
diff --git a/example/nshead_extension_c++/CMakeLists.txt 
b/example/nshead_extension_c++/CMakeLists.txt
index 0c3d18ab..6ddf3826 100644
--- a/example/nshead_extension_c++/CMakeLists.txt
+++ b/example/nshead_extension_c++/CMakeLists.txt
@@ -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)
diff --git a/example/nshead_pb_extension_c++/CMakeLists.txt 
b/example/nshead_pb_extension_c++/CMakeLists.txt
index 005840e6..8d0edf60 100644
--- a/example/nshead_pb_extension_c++/CMakeLists.txt
+++ b/example/nshead_pb_extension_c++/CMakeLists.txt
@@ -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})
diff --git a/example/parallel_echo_c++/CMakeLists.txt 
b/example/parallel_echo_c++/CMakeLists.txt
index d668ed43..5bd08183 100644
--- a/example/parallel_echo_c++/CMakeLists.txt
+++ b/example/parallel_echo_c++/CMakeLists.txt
@@ -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})
diff --git a/example/partition_echo_c++/CMakeLists.txt 
b/example/partition_echo_c++/CMakeLists.txt
index 1d770a98..636e19b4 100644
--- a/example/partition_echo_c++/CMakeLists.txt
+++ b/example/partition_echo_c++/CMakeLists.txt
@@ -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})
diff --git a/example/rdma_performance/CMakeLists.txt 
b/example/rdma_performance/CMakeLists.txt
index 226aa170..0fe0fe20 100644
--- a/example/rdma_performance/CMakeLists.txt
+++ b/example/rdma_performance/CMakeLists.txt
@@ -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})
diff --git a/example/redis_c++/CMakeLists.txt b/example/redis_c++/CMakeLists.txt
index 14dc839a..1447e606 100644
--- a/example/redis_c++/CMakeLists.txt
+++ b/example/redis_c++/CMakeLists.txt
@@ -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)
diff --git a/example/selective_echo_c++/CMakeLists.txt 
b/example/selective_echo_c++/CMakeLists.txt
index ca39217f..4b304fb6 100644
--- a/example/selective_echo_c++/CMakeLists.txt
+++ b/example/selective_echo_c++/CMakeLists.txt
@@ -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})
diff --git a/example/session_data_and_thread_local/CMakeLists.txt 
b/example/session_data_and_thread_local/CMakeLists.txt
index bfadec79..3678ab76 100644
--- a/example/session_data_and_thread_local/CMakeLists.txt
+++ b/example/session_data_and_thread_local/CMakeLists.txt
@@ -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})
diff --git a/example/streaming_echo_c++/CMakeLists.txt 
b/example/streaming_echo_c++/CMakeLists.txt
index 29ffcb16..ca4579d3 100644
--- a/example/streaming_echo_c++/CMakeLists.txt
+++ b/example/streaming_echo_c++/CMakeLists.txt
@@ -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})
diff --git a/src/brpc/builtin/index_service.cpp 
b/src/brpc/builtin/index_service.cpp
index fec74e86..7f65823c 100644
--- a/src/brpc/builtin/index_service.cpp
+++ b/src/brpc/builtin/index_service.cpp
@@ -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())) {
diff --git a/src/bthread/mutex.cpp b/src/bthread/mutex.cpp
index 0c39f783..b4008fd7 100644
--- a/src/bthread/mutex.cpp
+++ b/src/bthread/mutex.cpp
@@ -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;
@@ -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);
@@ -294,6 +305,7 @@ void SampledContention::dump_and_destroy(size_t /*round*/) {
 }
 
 void SampledContention::destroy() {
+    _hash_code = 0;
     butil::return_object(this);
 }
 
@@ -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;
 }
diff --git a/src/butil/debug/stack_trace_posix.cc 
b/src/butil/debug/stack_trace_posix.cc
index 3d19d90f..b96f1c8b 100644
--- a/src/butil/debug/stack_trace_posix.cc
+++ b/src/butil/debug/stack_trace_posix.cc
@@ -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 {
 
@@ -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 {


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org
For additional commands, e-mail: dev-h...@brpc.apache.org

Reply via email to