Skip to content

Commit

Permalink
[BACKPORT 2.14][#13255] DocDb: Use ThreadLocal instead of ThreadSafeR…
Browse files Browse the repository at this point in the history
…andom in Trace::NewTrace

Summary:
Original commit: 7330639 / D18257
Trace::NewTrace introduced in 11f564f / https://phabricator.dev.yugabyte.com/D15678 uses Thread Safe Random.

ThreadSafeRandom may contend on its spinlock. To avoid this, we could use a ThreadLocal.
Jira: DB-2905

Test Plan: ybd --cxx-test cluster_trace-test

Reviewers: bogdan, mbautin, rsami

Reviewed By: bogdan

Subscribers: amitanand, ybase

Differential Revision: https://phorge.dev.yugabyte.com/D25604
  • Loading branch information
karan-yb authored and amitanandaiyer committed May 23, 2023
1 parent f568a55 commit 1a20034
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/yb/util/trace.cc
Expand Up @@ -44,6 +44,7 @@

#include "yb/util/flag_tags.h"
#include "yb/util/random.h"
#include "yb/util/threadlocal.h"
#include "yb/util/memory/arena.h"
#include "yb/util/memory/memory.h"
#include "yb/util/object_pool.h"
Expand Down Expand Up @@ -285,12 +286,12 @@ scoped_refptr<Trace> Trace::NewTrace() {
}
const int32_t sampling_freq = GetAtomicFlag(&FLAGS_sampled_trace_1_in_n);
if (sampling_freq <= 0) {
VLOG(2) << "Sampled tracing returns " << nullptr;
VLOG(2) << "Sampled tracing returns nullptr";
return nullptr;
}
static yb::ThreadSafeRandom rng(static_cast<uint32_t>(GetCurrentTimeMicros()));

auto ret = scoped_refptr<Trace>(rng.OneIn(sampling_freq) ? new Trace() : nullptr);
BLOCK_STATIC_THREAD_LOCAL(yb::Random, rng_ptr, static_cast<uint32_t>(GetCurrentTimeMicros()));
auto ret = scoped_refptr<Trace>(rng_ptr->OneIn(sampling_freq) ? new Trace() : nullptr);
VLOG(2) << "Sampled tracing returns " << (ret ? "non-null" : "nullptr");
if (ret) {
TRACE_TO(ret.get(), "Sampled trace created probabilistically");
Expand Down

0 comments on commit 1a20034

Please sign in to comment.