Skip to content

Commit 0f77dfa

Browse files
andreas-abelcopybara-github
authored andcommitted
Make RPC buffer selection thread-safe
PiperOrigin-RevId: 934366192 Change-Id: Iedee832eeaf631157c0010ead46ec17587df5c61
1 parent 48fad2f commit 0f77dfa

5 files changed

Lines changed: 25 additions & 1 deletion

File tree

fleetbench/common/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ cc_library(
1212
linkstatic = 1,
1313
deps = [
1414
"@bazel_tools//tools/cpp/runfiles",
15+
"@com_google_absl//absl/base:core_headers",
1516
"@com_google_absl//absl/container:btree",
1617
"@com_google_absl//absl/flags:flag",
1718
"@com_google_absl//absl/log",
1819
"@com_google_absl//absl/log:check",
1920
"@com_google_absl//absl/strings",
21+
"@com_google_absl//absl/synchronization",
2022
"@com_google_benchmark//:benchmark",
2123
],
2224
alwayslink = 1,

fleetbench/common/common.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <vector>
2727

2828
#include "tools/cpp/runfiles/runfiles.h"
29+
#include "absl/base/const_init.h"
2930
#include "absl/container/btree_map.h"
3031
#include "absl/flags/flag.h"
3132
#include "absl/log/check.h"
@@ -35,6 +36,7 @@
3536
#include "absl/strings/str_cat.h"
3637
#include "absl/strings/str_split.h"
3738
#include "absl/strings/string_view.h"
39+
#include "absl/synchronization/mutex.h"
3840
#include "benchmark/benchmark.h"
3941

4042
#include "absl/log/log.h"
@@ -89,6 +91,15 @@ void Random::Reset() {
8991
}
9092
}
9193

94+
std::default_random_engine& GetThreadLocalRNG() {
95+
static absl::Mutex mutex(absl::kConstInit);
96+
thread_local std::default_random_engine local_rng = [&]() {
97+
absl::MutexLock l(mutex);
98+
return std::default_random_engine(GetRNG()());
99+
}();
100+
return local_rng;
101+
}
102+
92103
std::vector<std::filesystem::path> GetMatchingFiles(absl::string_view dir,
93104
absl::string_view prefix) {
94105
std::vector<std::filesystem::path> files;

fleetbench/common/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class Random {
6262

6363
inline std::default_random_engine& GetRNG() { return Random::instance().rng(); }
6464

65+
// Returns a thread-local random engine, seeded from GetRNG().
66+
// This is thread-safe.
67+
std::default_random_engine& GetThreadLocalRNG();
68+
6569
// Returns a sorted list of the files in directory 'dir' whose filenames start
6670
// with 'prefix'.
6771
std::vector<std::filesystem::path> GetMatchingFiles(absl::string_view dir,

fleetbench/common/common_test.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ TEST(RandomTest, Seed) {
116116
EXPECT_EQ(random1, random2);
117117
}
118118

119+
TEST(RandomTest, GetThreadLocalRNG) {
120+
auto& rng1 = GetThreadLocalRNG();
121+
auto& rng2 = GetThreadLocalRNG();
122+
EXPECT_EQ(&rng1, &rng2);
123+
}
124+
119125
TEST(DeathTest, SeedFlags) {
120126
ASSERT_DEATH(
121127
{

fleetbench/rpc/grpc_server.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <chrono> // NOLINT
1818
#include <cstddef>
1919
#include <memory>
20+
#include <random>
2021
#include <utility>
2122

2223
#include "absl/log/check.h"
@@ -103,7 +104,7 @@ void GRPCServer::Start(absl::string_view filepath,
103104
}
104105

105106
const fleetbench::rpc::ResponseMessage& GRPCServer::Buffer() const {
106-
return message_buffers_[GetRNG()() % message_buffers_.size()];
107+
return message_buffers_[GetThreadLocalRNG()() % message_buffers_.size()];
107108
}
108109

109110
void GRPCServer::Delay() {

0 commit comments

Comments
 (0)