Skip to content

Commit

Permalink
Reduce hash calculations of SampledContention
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright committed Dec 30, 2023
1 parent 023fa14 commit 9f672f6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
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
21 changes: 15 additions & 6 deletions src/bthread/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,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 +302,7 @@ void SampledContention::dump_and_destroy(size_t /*round*/) {
}

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

Expand Down

0 comments on commit 9f672f6

Please sign in to comment.