| 1 |
// Protocol Buffers - Google's data interchange format |
| 2 |
// Copyright 2008 Google Inc. All rights reserved. |
| 3 |
// https://developers.google.com/protocol-buffers/ |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without |
| 6 |
// modification, are permitted provided that the following conditions are |
| 7 |
// met: |
| 8 |
// |
| 9 |
// * Redistributions of source code must retain the above copyright |
| 10 |
// notice, this list of conditions and the following disclaimer. |
| 11 |
// * Redistributions in binary form must reproduce the above |
| 12 |
// copyright notice, this list of conditions and the following disclaimer |
| 13 |
// in the documentation and/or other materials provided with the |
| 14 |
// distribution. |
| 15 |
// * Neither the name of Google Inc. nor the names of its |
| 16 |
// contributors may be used to endorse or promote products derived from |
| 17 |
// this software without specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
|
| 31 |
#include "google/protobuf/arenaz_sampler.h" |
| 32 |
|
| 33 |
#include <atomic> |
| 34 |
#include <limits> |
| 35 |
#include <memory> |
| 36 |
#include <random> |
| 37 |
#include <utility> |
| 38 |
#include <vector> |
| 39 |
|
| 40 |
#include <gmock/gmock.h> |
| 41 |
#include <gtest/gtest.h> |
| 42 |
|
| 43 |
|
| 44 |
// Must be included last. |
| 45 |
#include "google/protobuf/port_def.inc" |
| 46 |
|
| 47 |
namespace google { |
| 48 |
namespace protobuf { |
| 49 |
namespace internal { |
| 50 |
#if defined(PROTOBUF_ARENAZ_SAMPLE) |
| 51 |
class ThreadSafeArenaStatsHandlePeer { |
| 52 |
public: |
| 53 |
static bool IsSampled(const ThreadSafeArenaStatsHandle& h) { |
| 54 |
return h.info_ != nullptr; |
| 55 |
} |
| 56 |
|
| 57 |
static ThreadSafeArenaStats* GetInfo(ThreadSafeArenaStatsHandle* h) { |
| 58 |
return h->info_; |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
std::vector<size_t> GetBytesAllocated(ThreadSafeArenazSampler* s) { |
| 63 |
std::vector<size_t> res; |
| 64 |
s->Iterate([&](const ThreadSafeArenaStats& info) { |
| 65 |
for (const auto& block_stats : info.block_histogram) { |
| 66 |
size_t bytes_allocated = |
| 67 |
block_stats.bytes_allocated.load(std::memory_order_acquire); |
| 68 |
if (bytes_allocated != 0) { |
| 69 |
res.push_back(bytes_allocated); |
| 70 |
} |
| 71 |
} |
| 72 |
}); |
| 73 |
return res; |
| 74 |
} |
| 75 |
|
| 76 |
ThreadSafeArenaStats* Register(ThreadSafeArenazSampler* s, size_t size, |
| 77 |
int64_t stride) { |
| 78 |
auto* info = s->Register(stride); |
| 79 |
assert(info != nullptr); |
| 80 |
info->block_histogram[0].bytes_allocated.store(size, |
| 81 |
std::memory_order_relaxed); |
| 82 |
return info; |
| 83 |
} |
| 84 |
|
| 85 |
#endif // defined(PROTOBUF_ARENAZ_SAMPLE) |
| 86 |
|
| 87 |
namespace { |
| 88 |
|
| 89 |
#if defined(PROTOBUF_ARENAZ_SAMPLE) |
| 90 |
|
| 91 |
TEST(ThreadSafeArenaStatsTest, PrepareForSampling) { |
| 92 |
ThreadSafeArenaStats info; |
| 93 |
constexpr int64_t kTestStride = 107; |
| 94 |
absl::MutexLock l(&info.init_mu); |
| 95 |
info.PrepareForSampling(kTestStride); |
| 96 |
|
| 97 |
for (const auto& block_stats : info.block_histogram) { |
| 98 |
EXPECT_EQ(block_stats.num_allocations.load(std::memory_order_relaxed), 0); |
| 99 |
EXPECT_EQ(block_stats.bytes_used.load(std::memory_order_relaxed), 0); |
| 100 |
EXPECT_EQ(block_stats.bytes_allocated.load(std::memory_order_relaxed), 0); |
| 101 |
EXPECT_EQ(block_stats.bytes_wasted.load(std::memory_order_relaxed), 0); |
| 102 |
} |
| 103 |
EXPECT_EQ(info.max_block_size.load(std::memory_order_relaxed), 0); |
| 104 |
EXPECT_EQ(info.weight, kTestStride); |
| 105 |
|
| 106 |
for (auto& block_stats : info.block_histogram) { |
| 107 |
block_stats.num_allocations.store(1, std::memory_order_relaxed); |
| 108 |
block_stats.bytes_used.store(1, std::memory_order_relaxed); |
| 109 |
block_stats.bytes_allocated.store(1, std::memory_order_relaxed); |
| 110 |
block_stats.bytes_wasted.store(1, std::memory_order_relaxed); |
| 111 |
} |
| 112 |
info.max_block_size.store(1, std::memory_order_relaxed); |
| 113 |
|
| 114 |
info.PrepareForSampling(2 * kTestStride); |
| 115 |
for (auto& block_stats : info.block_histogram) { |
| 116 |
EXPECT_EQ(block_stats.num_allocations.load(std::memory_order_relaxed), 0); |
| 117 |
EXPECT_EQ(block_stats.bytes_used.load(std::memory_order_relaxed), 0); |
| 118 |
EXPECT_EQ(block_stats.bytes_allocated.load(std::memory_order_relaxed), 0); |
| 119 |
EXPECT_EQ(block_stats.bytes_wasted.load(std::memory_order_relaxed), 0); |
| 120 |
} |
| 121 |
EXPECT_EQ(info.max_block_size.load(std::memory_order_relaxed), 0); |
| 122 |
EXPECT_EQ(info.weight, 2 * kTestStride); |
| 123 |
} |
| 124 |
|
| 125 |
TEST(ThreadSafeArenaStatsTest, FindBin) { |
| 126 |
size_t current_bin = 0; |
| 127 |
size_t bytes = 1; |
| 128 |
while (current_bin < ThreadSafeArenaStats::kBlockHistogramBins - 1) { |
| 129 |
size_t next_bin = ThreadSafeArenaStats::FindBin(bytes); |
| 130 |
if (next_bin != current_bin) { |
| 131 |
// Test the bins increase linearly. |
| 132 |
EXPECT_EQ(next_bin, current_bin + 1); |
| 133 |
// Test the bins change only at values of the form 2^k + 1. |
| 134 |
EXPECT_EQ(absl::popcount(bytes - 1), 1); |
| 135 |
current_bin = next_bin; |
| 136 |
} |
| 137 |
++bytes; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
TEST(ThreadSafeArenaStatsTest, MinMaxBlockSizeForBin) { |
| 142 |
std::pair<size_t, size_t> current_limits = |
| 143 |
ThreadSafeArenaStats::MinMaxBlockSizeForBin(0); |
| 144 |
EXPECT_EQ(current_limits.first, 1); |
| 145 |
EXPECT_LT(current_limits.first, current_limits.second); |
| 146 |
for (size_t i = 1; i < ThreadSafeArenaStats::kBlockHistogramBins; ++i) { |
| 147 |
std::pair<size_t, size_t> next_limits = |
| 148 |
ThreadSafeArenaStats::MinMaxBlockSizeForBin(i); |
| 149 |
EXPECT_LT(next_limits.first, next_limits.second); |
| 150 |
// Test the limits do not have gaps. |
| 151 |
EXPECT_EQ(next_limits.first, current_limits.second + 1); |
| 152 |
if (i != ThreadSafeArenaStats::kBlockHistogramBins - 1) { |
| 153 |
EXPECT_EQ(next_limits.second, 2 * current_limits.second); |
| 154 |
} |
| 155 |
current_limits = next_limits; |
| 156 |
} |
| 157 |
// Test the limits cover the entire range possible. |
| 158 |
EXPECT_EQ(current_limits.second, std::numeric_limits<size_t>::max()); |
| 159 |
} |
| 160 |
|
| 161 |
TEST(ThreadSafeArenaStatsTest, RecordAllocateSlow) { |
| 162 |
ThreadSafeArenaStats info; |
| 163 |
constexpr int64_t kTestStride = 458; |
| 164 |
absl::MutexLock l(&info.init_mu); |
| 165 |
info.PrepareForSampling(kTestStride); |
| 166 |
RecordAllocateSlow(&info, /*requested=*/0, /*allocated=*/128, /*wasted=*/0); |
| 167 |
EXPECT_EQ( |
| 168 |
info.block_histogram[0].num_allocations.load(std::memory_order_relaxed), |
| 169 |
1); |
| 170 |
EXPECT_EQ(info.block_histogram[0].bytes_used.load(std::memory_order_relaxed), |
| 171 |
0); |
| 172 |
EXPECT_EQ( |
| 173 |
info.block_histogram[0].bytes_allocated.load(std::memory_order_relaxed), |
| 174 |
128); |
| 175 |
EXPECT_EQ( |
| 176 |
info.block_histogram[0].bytes_wasted.load(std::memory_order_relaxed), 0); |
| 177 |
EXPECT_EQ(info.max_block_size.load(std::memory_order_relaxed), 128); |
| 178 |
RecordAllocateSlow(&info, /*requested=*/100, /*allocated=*/256, |
| 179 |
/*wasted=*/28); |
| 180 |
EXPECT_EQ(info.block_histogram[0].bytes_used.load(std::memory_order_relaxed), |
| 181 |
100); |
| 182 |
EXPECT_EQ( |
| 183 |
info.block_histogram[0].bytes_wasted.load(std::memory_order_relaxed), 28); |
| 184 |
EXPECT_EQ( |
| 185 |
info.block_histogram[1].num_allocations.load(std::memory_order_relaxed), |
| 186 |
1); |
| 187 |
EXPECT_EQ( |
| 188 |
info.block_histogram[1].bytes_allocated.load(std::memory_order_relaxed), |
| 189 |
256); |
| 190 |
EXPECT_EQ(info.max_block_size.load(std::memory_order_relaxed), 256); |
| 191 |
} |
| 192 |
|
| 193 |
TEST(ThreadSafeArenaStatsTest, RecordAllocateSlowMaxBlockSizeTest) { |
| 194 |
ThreadSafeArenaStats info; |
| 195 |
constexpr int64_t kTestStride = 458; |
| 196 |
absl::MutexLock l(&info.init_mu); |
| 197 |
info.PrepareForSampling(kTestStride); |
| 198 |
RecordAllocateSlow(&info, /*requested=*/100, /*allocated=*/128, /*wasted=*/0); |
| 199 |
EXPECT_EQ(info.max_block_size.load(std::memory_order_relaxed), 128); |
| 200 |
RecordAllocateSlow(&info, /*requested=*/100, /*allocated=*/256, |
| 201 |
/*wasted=*/28); |
| 202 |
EXPECT_EQ(info.max_block_size.load(std::memory_order_relaxed), 256); |
| 203 |
RecordAllocateSlow(&info, /*requested=*/100, /*allocated=*/128, |
| 204 |
/*wasted=*/28); |
| 205 |
EXPECT_EQ(info.max_block_size.load(std::memory_order_relaxed), 256); |
| 206 |
} |
| 207 |
|
| 208 |
TEST(ThreadSafeArenazSamplerTest, SamplingCorrectness) { |
| 209 |
SetThreadSafeArenazEnabled(true); |
| 210 |
for (int p = 0; p <= 15; ++p) { |
| 211 |
SetThreadSafeArenazSampleParameter(1 << p); |
| 212 |
SetThreadSafeArenazGlobalNextSample(1 << p); |
| 213 |
const int kTrials = 1000 << p; |
| 214 |
std::vector<ThreadSafeArenaStatsHandle> hv; |
| 215 |
for (int i = 0; i < kTrials; ++i) { |
| 216 |
ThreadSafeArenaStatsHandle h = Sample(); |
| 217 |
if (h.MutableStats() != nullptr) hv.push_back(std::move(h)); |
| 218 |
} |
| 219 |
// Ideally samples << p should be very close to kTrials. But we keep a |
| 220 |
// factor of two guard band. |
| 221 |
EXPECT_GE(hv.size() << p, kTrials / 2); |
| 222 |
EXPECT_LE(hv.size() << p, 2 * kTrials); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
TEST(ThreadSafeArenazSamplerTest, SmallSampleParameter) { |
| 227 |
SetThreadSafeArenazEnabled(true); |
| 228 |
SetThreadSafeArenazSampleParameter(100); |
| 229 |
constexpr int64_t kTestStride = 0; |
| 230 |
|
| 231 |
for (int i = 0; i < 1000; ++i) { |
| 232 |
SamplingState sampling_state = {kTestStride, kTestStride}; |
| 233 |
ThreadSafeArenaStats* sample = SampleSlow(sampling_state); |
| 234 |
EXPECT_GT(sampling_state.next_sample, 0); |
| 235 |
EXPECT_NE(sample, nullptr); |
| 236 |
UnsampleSlow(sample); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
TEST(ThreadSafeArenazSamplerTest, LargeSampleParameter) { |
| 241 |
SetThreadSafeArenazEnabled(true); |
| 242 |
SetThreadSafeArenazSampleParameter(std::numeric_limits<int32_t>::max()); |
| 243 |
constexpr int64_t kTestStride = 0; |
| 244 |
|
| 245 |
for (int i = 0; i < 1000; ++i) { |
| 246 |
SamplingState sampling_state = {kTestStride, kTestStride}; |
| 247 |
ThreadSafeArenaStats* sample = SampleSlow(sampling_state); |
| 248 |
EXPECT_GT(sampling_state.next_sample, 0); |
| 249 |
EXPECT_NE(sample, nullptr); |
| 250 |
UnsampleSlow(sample); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
TEST(ThreadSafeArenazSamplerTest, Sample) { |
| 255 |
SetThreadSafeArenazEnabled(true); |
| 256 |
SetThreadSafeArenazSampleParameter(100); |
| 257 |
SetThreadSafeArenazGlobalNextSample(0); |
| 258 |
int64_t num_sampled = 0; |
| 259 |
int64_t total = 0; |
| 260 |
double sample_rate = 0.0; |
| 261 |
for (int i = 0; i < 1000000; ++i) { |
| 262 |
ThreadSafeArenaStatsHandle h = Sample(); |
| 263 |
++total; |
| 264 |
if (ThreadSafeArenaStatsHandlePeer::IsSampled(h)) { |
| 265 |
++num_sampled; |
| 266 |
} |
| 267 |
sample_rate = static_cast<double>(num_sampled) / total; |
| 268 |
if (0.005 < sample_rate && sample_rate < 0.015) break; |
| 269 |
} |
| 270 |
EXPECT_NEAR(sample_rate, 0.01, 0.005); |
| 271 |
} |
| 272 |
|
| 273 |
TEST(ThreadSafeArenazSamplerTest, Handle) { |
| 274 |
auto& sampler = GlobalThreadSafeArenazSampler(); |
| 275 |
constexpr int64_t kTestStride = 17; |
| 276 |
ThreadSafeArenaStatsHandle h(sampler.Register(kTestStride)); |
| 277 |
auto* info = ThreadSafeArenaStatsHandlePeer::GetInfo(&h); |
| 278 |
info->block_histogram[0].bytes_allocated.store(0x12345678, |
| 279 |
std::memory_order_relaxed); |
| 280 |
|
| 281 |
bool found = false; |
| 282 |
sampler.Iterate([&](const ThreadSafeArenaStats& h) { |
| 283 |
if (&h == info) { |
| 284 |
EXPECT_EQ( |
| 285 |
h.block_histogram[0].bytes_allocated.load(std::memory_order_relaxed), |
| 286 |
0x12345678); |
| 287 |
EXPECT_EQ(h.weight, kTestStride); |
| 288 |
found = true; |
| 289 |
} |
| 290 |
}); |
| 291 |
EXPECT_TRUE(found); |
| 292 |
|
| 293 |
h = ThreadSafeArenaStatsHandle(); |
| 294 |
found = false; |
| 295 |
sampler.Iterate([&](const ThreadSafeArenaStats& h) { |
| 296 |
if (&h == info) { |
| 297 |
// this will only happen if some other thread has resurrected the info |
| 298 |
// the old handle was using. |
| 299 |
if (h.block_histogram[0].bytes_allocated.load( |
| 300 |
std::memory_order_relaxed) == 0x12345678) { |
| 301 |
found = true; |
| 302 |
} |
| 303 |
} |
| 304 |
}); |
| 305 |
EXPECT_FALSE(found); |
| 306 |
} |
| 307 |
|
| 308 |
TEST(ThreadSafeArenazSamplerTest, Registration) { |
| 309 |
ThreadSafeArenazSampler sampler; |
| 310 |
constexpr int64_t kTestStride = 100; |
| 311 |
auto* info1 = Register(&sampler, 1, kTestStride); |
| 312 |
EXPECT_THAT(GetBytesAllocated(&sampler), UnorderedElementsAre(1)); |
| 313 |
|
| 314 |
auto* info2 = Register(&sampler, 2, kTestStride); |
| 315 |
EXPECT_THAT(GetBytesAllocated(&sampler), UnorderedElementsAre(1, 2)); |
| 316 |
info1->block_histogram[0].bytes_allocated.store(3, std::memory_order_relaxed); |
| 317 |
EXPECT_THAT(GetBytesAllocated(&sampler), UnorderedElementsAre(3, 2)); |
| 318 |
|
| 319 |
sampler.Unregister(info1); |
| 320 |
sampler.Unregister(info2); |
| 321 |
} |
| 322 |
|
| 323 |
TEST(ThreadSafeArenazSamplerTest, Unregistration) { |
| 324 |
ThreadSafeArenazSampler sampler; |
| 325 |
std::vector<ThreadSafeArenaStats*> infos; |
| 326 |
constexpr int64_t kTestStride = 200; |
| 327 |
for (size_t i = 0; i < 3; ++i) { |
| 328 |
infos.push_back(Register(&sampler, i + 1, kTestStride)); |
| 329 |
} |
| 330 |
EXPECT_THAT(GetBytesAllocated(&sampler), UnorderedElementsAre(1, 2, 3)); |
| 331 |
|
| 332 |
sampler.Unregister(infos[1]); |
| 333 |
EXPECT_THAT(GetBytesAllocated(&sampler), UnorderedElementsAre(1, 3)); |
| 334 |
|
| 335 |
infos.push_back(Register(&sampler, 3, kTestStride)); |
| 336 |
infos.push_back(Register(&sampler, 4, kTestStride)); |
| 337 |
EXPECT_THAT(GetBytesAllocated(&sampler), UnorderedElementsAre(1, 3, 3, 4)); |
| 338 |
sampler.Unregister(infos[3]); |
| 339 |
EXPECT_THAT(GetBytesAllocated(&sampler), UnorderedElementsAre(1, 3, 4)); |
| 340 |
|
| 341 |
sampler.Unregister(infos[0]); |
| 342 |
sampler.Unregister(infos[2]); |
| 343 |
sampler.Unregister(infos[4]); |
| 344 |
EXPECT_THAT(GetBytesAllocated(&sampler), IsEmpty()); |
| 345 |
} |
| 346 |
|
| 347 |
TEST(ThreadSafeArenazSamplerTest, MultiThreaded) { |
| 348 |
ThreadSafeArenazSampler sampler; |
| 349 |
absl::Notification stop; |
| 350 |
ThreadPool pool(10); |
| 351 |
|
| 352 |
for (int i = 0; i < 10; ++i) { |
| 353 |
const int64_t sampling_stride = 11 + i % 3; |
| 354 |
pool.Schedule([&sampler, &stop, sampling_stride]() { |
| 355 |
std::random_device rd; |
| 356 |
std::mt19937 gen(rd()); |
| 357 |
|
| 358 |
std::vector<ThreadSafeArenaStats*> infoz; |
| 359 |
while (!stop.HasBeenNotified()) { |
| 360 |
if (infoz.empty()) { |
| 361 |
infoz.push_back(sampler.Register(sampling_stride)); |
| 362 |
} |
| 363 |
switch (std::uniform_int_distribution<>(0, 1)(gen)) { |
| 364 |
case 0: { |
| 365 |
infoz.push_back(sampler.Register(sampling_stride)); |
| 366 |
break; |
| 367 |
} |
| 368 |
case 1: { |
| 369 |
size_t p = |
| 370 |
std::uniform_int_distribution<>(0, infoz.size() - 1)(gen); |
| 371 |
ThreadSafeArenaStats* info = infoz[p]; |
| 372 |
infoz[p] = infoz.back(); |
| 373 |
infoz.pop_back(); |
| 374 |
EXPECT_EQ(info->weight, sampling_stride); |
| 375 |
sampler.Unregister(info); |
| 376 |
break; |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
}); |
| 381 |
} |
| 382 |
// The threads will hammer away. Give it a little bit of time for tsan to |
| 383 |
// spot errors. |
| 384 |
absl::SleepFor(absl::Seconds(3)); |
| 385 |
stop.Notify(); |
| 386 |
} |
| 387 |
|
| 388 |
TEST(ThreadSafeArenazSamplerTest, Callback) { |
| 389 |
ThreadSafeArenazSampler sampler; |
| 390 |
constexpr int64_t kTestStride = 203; |
| 391 |
|
| 392 |
auto* info1 = Register(&sampler, 1, kTestStride); |
| 393 |
auto* info2 = Register(&sampler, 2, kTestStride); |
| 394 |
|
| 395 |
static const ThreadSafeArenaStats* expected; |
| 396 |
|
| 397 |
auto callback = [](const ThreadSafeArenaStats& info) { |
| 398 |
// We can't use `info` outside of this callback because the object will be |
| 399 |
// disposed as soon as we return from here. |
| 400 |
EXPECT_EQ(&info, expected); |
| 401 |
}; |
| 402 |
|
| 403 |
// Set the callback. |
| 404 |
EXPECT_EQ(sampler.SetDisposeCallback(callback), nullptr); |
| 405 |
expected = info1; |
| 406 |
sampler.Unregister(info1); |
| 407 |
|
| 408 |
// Unset the callback. |
| 409 |
EXPECT_EQ(callback, sampler.SetDisposeCallback(nullptr)); |
| 410 |
expected = nullptr; // no more calls. |
| 411 |
sampler.Unregister(info2); |
| 412 |
} |
| 413 |
|
| 414 |
TEST(ThreadSafeArenazSamplerTest, InitialBlockReportsZeroUsedAndWasted) { |
| 415 |
SetThreadSafeArenazEnabled(true); |
| 416 |
// Setting 1 as the parameter value means one in every two arenas would be |
| 417 |
// sampled, on average. |
| 418 |
int32_t oldparam = ThreadSafeArenazSampleParameter(); |
| 419 |
SetThreadSafeArenazSampleParameter(1); |
| 420 |
SetThreadSafeArenazGlobalNextSample(0); |
| 421 |
constexpr int kSize = 571; |
| 422 |
int count_found_allocation = 0; |
| 423 |
auto& sampler = GlobalThreadSafeArenazSampler(); |
| 424 |
for (int i = 0; i < 10; ++i) { |
| 425 |
char block[kSize]; |
| 426 |
google::protobuf::Arena arena(/*initial_block=*/block, /*initial_block_size=*/kSize); |
| 427 |
sampler.Iterate([&](const ThreadSafeArenaStats& h) { |
| 428 |
const auto& histbin = |
| 429 |
h.block_histogram[ThreadSafeArenaStats::FindBin(kSize)]; |
| 430 |
if (histbin.bytes_allocated.load(std::memory_order_relaxed) == kSize) { |
| 431 |
count_found_allocation++; |
| 432 |
EXPECT_EQ(histbin.bytes_used, 0); |
| 433 |
EXPECT_EQ(histbin.bytes_wasted, 0); |
| 434 |
} |
| 435 |
}); |
| 436 |
} |
| 437 |
EXPECT_GT(count_found_allocation, 0); |
| 438 |
SetThreadSafeArenazSampleParameter(oldparam); |
| 439 |
} |
| 440 |
|
| 441 |
class ThreadSafeArenazSamplerTestThread : public Thread { |
| 442 |
protected: |
| 443 |
void Run() override { |
| 444 |
google::protobuf::ArenaSafeUniquePtr< |
| 445 |
protobuf_test_messages::proto2::TestAllTypesProto2> |
| 446 |
message = google::protobuf::MakeArenaSafeUnique< |
| 447 |
protobuf_test_messages::proto2::TestAllTypesProto2>(arena_); |
| 448 |
ABSL_CHECK(message != nullptr); |
| 449 |
// Signal that a message on the arena has been created. This should create |
| 450 |
// a SerialArena for this thread. |
| 451 |
if (barrier_->Block()) { |
| 452 |
delete barrier_; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
public: |
| 457 |
ThreadSafeArenazSamplerTestThread(const thread::Options& options, |
| 458 |
absl::string_view name, |
| 459 |
google::protobuf::Arena* arena, |
| 460 |
absl::Barrier* barrier) |
| 461 |
: Thread(options, name), arena_(arena), barrier_(barrier) {} |
| 462 |
|
| 463 |
private: |
| 464 |
google::protobuf::Arena* arena_; |
| 465 |
absl::Barrier* barrier_; |
| 466 |
}; |
| 467 |
|
| 468 |
TEST(ThreadSafeArenazSamplerTest, MultiThread) { |
| 469 |
SetThreadSafeArenazEnabled(true); |
| 470 |
// Setting 1 as the parameter value means one in every two arenas would be |
| 471 |
// sampled, on average. |
| 472 |
int32_t oldparam = ThreadSafeArenazSampleParameter(); |
| 473 |
SetThreadSafeArenazSampleParameter(1); |
| 474 |
SetThreadSafeArenazGlobalNextSample(0); |
| 475 |
auto& sampler = GlobalThreadSafeArenazSampler(); |
| 476 |
int count = 0; |
| 477 |
for (int i = 0; i < 10; ++i) { |
| 478 |
const int kNumThreads = 10; |
| 479 |
absl::Barrier* barrier = new absl::Barrier(kNumThreads + 1); |
| 480 |
google::protobuf::Arena arena; |
| 481 |
thread::Options options; |
| 482 |
options.set_joinable(true); |
| 483 |
std::vector<std::unique_ptr<ThreadSafeArenazSamplerTestThread>> threads; |
| 484 |
for (int i = 0; i < kNumThreads; i++) { |
| 485 |
auto t = std::make_unique<ThreadSafeArenazSamplerTestThread>( |
| 486 |
options, absl::StrCat("thread", i), &arena, barrier); |
| 487 |
t->Start(); |
| 488 |
threads.push_back(std::move(t)); |
| 489 |
} |
| 490 |
// Wait till each thread has created a message on the arena. |
| 491 |
if (barrier->Block()) { |
| 492 |
delete barrier; |
| 493 |
} |
| 494 |
sampler.Iterate([&](const ThreadSafeArenaStats& h) { ++count; }); |
| 495 |
for (int i = 0; i < kNumThreads; i++) { |
| 496 |
threads[i]->Join(); |
| 497 |
} |
| 498 |
} |
| 499 |
EXPECT_GT(count, 0); |
| 500 |
SetThreadSafeArenazSampleParameter(oldparam); |
| 501 |
} |
| 502 |
|
| 503 |
class SampleFirstArenaThread : public Thread { |
| 504 |
protected: |
| 505 |
void Run() override { |
| 506 |
google::protobuf::Arena arena; |
| 507 |
google::protobuf::ArenaSafeUniquePtr< |
| 508 |
protobuf_test_messages::proto2::TestAllTypesProto2> |
| 509 |
message = google::protobuf::MakeArenaSafeUnique< |
| 510 |
protobuf_test_messages::proto2::TestAllTypesProto2>(&arena); |
| 511 |
ABSL_CHECK(message != nullptr); |
| 512 |
arena_created_.Notify(); |
| 513 |
samples_counted_.WaitForNotification(); |
| 514 |
} |
| 515 |
|
| 516 |
public: |
| 517 |
explicit SampleFirstArenaThread(const thread::Options& options) |
| 518 |
: Thread(options, "SampleFirstArenaThread") {} |
| 519 |
|
| 520 |
absl::Notification arena_created_; |
| 521 |
absl::Notification samples_counted_; |
| 522 |
}; |
| 523 |
|
| 524 |
// Test that the first arena created on a thread may and may not be chosen for |
| 525 |
// sampling. |
| 526 |
TEST(ThreadSafeArenazSamplerTest, SampleFirstArena) { |
| 527 |
SetThreadSafeArenazEnabled(true); |
| 528 |
auto& sampler = GlobalThreadSafeArenazSampler(); |
| 529 |
|
| 530 |
enum class SampleResult { |
| 531 |
kSampled, |
| 532 |
kUnsampled, |
| 533 |
kSpoiled, |
| 534 |
}; |
| 535 |
|
| 536 |
auto count_samples = [&]() { |
| 537 |
int count = 0; |
| 538 |
sampler.Iterate([&](const ThreadSafeArenaStats& h) { ++count; }); |
| 539 |
return count; |
| 540 |
}; |
| 541 |
|
| 542 |
auto run_sample_experiment = [&]() { |
| 543 |
int before = count_samples(); |
| 544 |
thread::Options options; |
| 545 |
options.set_joinable(true); |
| 546 |
SampleFirstArenaThread t(options); |
| 547 |
t.Start(); |
| 548 |
t.arena_created_.WaitForNotification(); |
| 549 |
int during = count_samples(); |
| 550 |
t.samples_counted_.Notify(); |
| 551 |
t.Join(); |
| 552 |
int after = count_samples(); |
| 553 |
|
| 554 |
// If we didn't get back where we were, some other thread may have |
| 555 |
// created an arena and produced an invalid experiment run. |
| 556 |
if (before != after) return SampleResult::kSpoiled; |
| 557 |
|
| 558 |
switch (during - before) { |
| 559 |
case 1: |
| 560 |
return SampleResult::kSampled; |
| 561 |
case 0: |
| 562 |
return SampleResult::kUnsampled; |
| 563 |
default: |
| 564 |
return SampleResult::kSpoiled; |
| 565 |
} |
| 566 |
}; |
| 567 |
|
| 568 |
constexpr int kTrials = 10000; |
| 569 |
bool sampled = false; |
| 570 |
bool unsampled = false; |
| 571 |
for (int i = 0; i < kTrials; ++i) { |
| 572 |
switch (run_sample_experiment()) { |
| 573 |
case SampleResult::kSampled: |
| 574 |
sampled = true; |
| 575 |
break; |
| 576 |
case SampleResult::kUnsampled: |
| 577 |
unsampled = true; |
| 578 |
break; |
| 579 |
default: |
| 580 |
break; |
| 581 |
} |
| 582 |
|
| 583 |
// This is the success criteria for the entire test. At some point |
| 584 |
// we sampled the first arena and at some point we did not. |
| 585 |
if (sampled && unsampled) return; |
| 586 |
} |
| 587 |
EXPECT_TRUE(sampled); |
| 588 |
EXPECT_TRUE(unsampled); |
| 589 |
} |
| 590 |
#endif // defined(PROTOBUF_ARENAZ_SAMPLE) |
| 591 |
|
| 592 |
} // namespace |
| 593 |
} // namespace internal |
| 594 |
} // namespace protobuf |
| 595 |
} // namespace google |