diff options
author | Abseil Team <absl-team@google.com> | 2021-04-15 19:42:51 -0700 |
---|---|---|
committer | Dino Radaković <dinor@google.com> | 2021-04-15 19:45:30 -0700 |
commit | e20fe888fabc1fc995dc61180e8a31b5f809a95f (patch) | |
tree | 5877907889fe35eb3162aa71111e16bd1d59fde4 /absl/strings/internal/cordz_sample_token_test.cc | |
parent | 46dfbfe31ca1dd414e4c33cbcbcd7199bb4efde3 (diff) |
Export of internal Abseil changes
--
341670bce317dd6af8d3c066970230591a47e80c by Martijn Vels <mvels@google.com>:
Change GetStack() and GetParentStack() to return absl::Span
PiperOrigin-RevId: 368765721
--
6aaab9536d6957303c7aba100c3afaa6fb0ea2c8 by Martijn Vels <mvels@google.com>:
Remove locking from parent stack.
This change removes the need to lock all access to `parent_stack' by making the 'copy constructor' logic specify the 'copied from' CordzInfo (where available) to the TrackCord function, after which parent_stack is immutable.
PiperOrigin-RevId: 368760630
--
b19e2059cada35a8ede994833018edac94de6ddc by Martijn Vels <mvels@google.com>:
Add cordz instrumentation to Cord
PiperOrigin-RevId: 368746225
--
67b8bbf980f0f4e1db79aa32968e9a715a09b51a by Martijn Vels <mvels@google.com>:
Create ABSL_INTERNAL_CORDZ_ENABLED define controlling when Cordz code is enabled
There are specific builds and condtions under which we don't support cordz sampling, which is per this change represented by ABSL_INTERNAL_CORDZ_ENABLED being defined.
PiperOrigin-RevId: 368731603
--
8cbfe0e3169637a620f4b66ad2bc2ce340879cb0 by Martijn Vels <mvels@google.com>:
Add a `rep` property to CordzInfo to be managed by Cord logic.
This change adds a `rep` property to CordzInfo, which is intended to be used by collection logic.
Mini design:
Cord invokes TrackCord() providing the active 'root' cordrep of the newly sampled Cord, returning a CordzInfo with a weak (uncounted) reference to this root. Cord invokes `SetCordRep()` each time the root cordrep of the sampled Cord is updated while holding `mutex()`. Cord must also obtain `mutex()` _before_ removing a reference on the old root. i.e.: Cord must guarantee that the (weak) reference held in CordzInfo is at all times valid.
CordzInfo collection code can then safely obtain a (reference counted) rep pointer by adding a reference to `rep_` while holding `mutex()`. This requires only a very brief critical section inside CordzInfo logic, minimizing contention with concurrent Cord updates.
Cord code should typically obtain and hold `mutex()` for the entirety of each mutating Cord operation on a sampled cord. As Cord is thread compatible, it never competes on the lock with any other thread. The only possible concurrent access is from Cordz collection code, which should be a relatively rare event.
PiperOrigin-RevId: 368673758
--
1255120dce2bdd6b4205a34a0e555e0b74b6152f by Martijn Vels <mvels@google.com>:
Remove 'depth' from active recorded metrics.
Going forward we do not 'live' record depth (and size), but will observe these at collection time only.
PiperOrigin-RevId: 368636572
--
83e5146e35f221736b49e9f0a8805f8c159a51db by Martijn Vels <mvels@google.com>:
Make cordz targets visible in OSS
PiperOrigin-RevId: 368615010
--
dcb16a4f1239151f0a8c70a8cfeb29dabbd113b8 by Martijn Vels <mvels@google.com>:
Internal cleanup
PiperOrigin-RevId: 368514666
GitOrigin-RevId: 341670bce317dd6af8d3c066970230591a47e80c
Change-Id: I94cecfbbd441eb386f99fc5186c468a7a5538862
Diffstat (limited to 'absl/strings/internal/cordz_sample_token_test.cc')
-rw-r--r-- | absl/strings/internal/cordz_sample_token_test.cc | 209 |
1 files changed, 209 insertions, 0 deletions
diff --git a/absl/strings/internal/cordz_sample_token_test.cc b/absl/strings/internal/cordz_sample_token_test.cc new file mode 100644 index 00000000..8c052642 --- /dev/null +++ b/absl/strings/internal/cordz_sample_token_test.cc @@ -0,0 +1,209 @@ +// Copyright 2019 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "absl/strings/internal/cordz_sample_token.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "absl/memory/memory.h" +#include "absl/random/random.h" +#include "absl/strings/internal/cord_rep_flat.h" +#include "absl/strings/internal/cordz_handle.h" +#include "absl/strings/internal/cordz_info.h" +#include "absl/synchronization/internal/thread_pool.h" +#include "absl/synchronization/notification.h" +#include "absl/time/clock.h" +#include "absl/time/time.h" + +namespace absl { +ABSL_NAMESPACE_BEGIN +namespace cord_internal { +namespace { + +using ::testing::ElementsAre; +using ::testing::Eq; +using ::testing::Ne; + +struct TestCordRep { + CordRepFlat* rep; + + TestCordRep() { + rep = CordRepFlat::New(100); + rep->length = 100; + memset(rep->Data(), 1, 100); + } + ~TestCordRep() { CordRepFlat::Delete(rep); } +}; + +TEST(CordzSampleTokenTest, IteratorTraits) { + static_assert(std::is_copy_constructible<CordzSampleToken::Iterator>::value, + ""); + static_assert(std::is_copy_assignable<CordzSampleToken::Iterator>::value, ""); + static_assert(std::is_move_constructible<CordzSampleToken::Iterator>::value, + ""); + static_assert(std::is_move_assignable<CordzSampleToken::Iterator>::value, ""); + static_assert( + std::is_same< + std::iterator_traits<CordzSampleToken::Iterator>::iterator_category, + std::input_iterator_tag>::value, + ""); + static_assert( + std::is_same<std::iterator_traits<CordzSampleToken::Iterator>::value_type, + const CordzInfo&>::value, + ""); + static_assert( + std::is_same< + std::iterator_traits<CordzSampleToken::Iterator>::difference_type, + ptrdiff_t>::value, + ""); + static_assert( + std::is_same<std::iterator_traits<CordzSampleToken::Iterator>::pointer, + const CordzInfo*>::value, + ""); + static_assert( + std::is_same<std::iterator_traits<CordzSampleToken::Iterator>::reference, + const CordzInfo&>::value, + ""); +} + +TEST(CordzSampleTokenTest, IteratorEmpty) { + CordzSampleToken token; + EXPECT_THAT(token.begin(), Eq(token.end())); +} + +TEST(CordzSampleTokenTest, Iterator) { + TestCordRep rep1; + TestCordRep rep2; + TestCordRep rep3; + CordzInfo* info1 = CordzInfo::TrackCord(rep1.rep); + CordzInfo* info2 = CordzInfo::TrackCord(rep2.rep); + CordzInfo* info3 = CordzInfo::TrackCord(rep3.rep); + + CordzSampleToken token; + std::vector<const CordzInfo*> found; + for (const CordzInfo& cord_info : token) { + found.push_back(&cord_info); + } + + EXPECT_THAT(found, ElementsAre(info3, info2, info1)); + + CordzInfo::UntrackCord(info1); + CordzInfo::UntrackCord(info2); + CordzInfo::UntrackCord(info3); +} + +TEST(CordzSampleTokenTest, IteratorEquality) { + TestCordRep rep1; + TestCordRep rep2; + TestCordRep rep3; + CordzInfo* info1 = CordzInfo::TrackCord(rep1.rep); + + CordzSampleToken token1; + // lhs starts with the CordzInfo corresponding to cord1 at the head. + CordzSampleToken::Iterator lhs = token1.begin(); + + CordzInfo* info2 = CordzInfo::TrackCord(rep2.rep); + + CordzSampleToken token2; + // rhs starts with the CordzInfo corresponding to cord2 at the head. + CordzSampleToken::Iterator rhs = token2.begin(); + + CordzInfo* info3 = CordzInfo::TrackCord(rep3.rep); + + // lhs is on cord1 while rhs is on cord2. + EXPECT_THAT(lhs, Ne(rhs)); + + rhs++; + // lhs and rhs are both on cord1, but they didn't come from the same + // CordzSampleToken. + EXPECT_THAT(lhs, Ne(rhs)); + + lhs++; + rhs++; + // Both lhs and rhs are done, so they are on nullptr. + EXPECT_THAT(lhs, Eq(rhs)); + + CordzInfo::UntrackCord(info1); + CordzInfo::UntrackCord(info2); + CordzInfo::UntrackCord(info3); +} + +TEST(CordzSampleTokenTest, MultiThreaded) { + Notification stop; + static constexpr int kNumThreads = 4; + static constexpr int kNumCords = 3; + static constexpr int kNumTokens = 3; + absl::synchronization_internal::ThreadPool pool(kNumThreads); + + for (int i = 0; i < kNumThreads; ++i) { + pool.Schedule([&stop]() { + absl::BitGen gen; + TestCordRep reps[kNumCords]; + CordzInfo* infos[kNumCords] = {nullptr}; + std::vector<std::unique_ptr<CordzSampleToken>> tokens; + tokens.resize(kNumTokens); + + while (!stop.HasBeenNotified()) { + // Randomly perform one of five actions: + // 1) Untrack + // 2) Track + // 3) Iterate over Cords visible to a token. + // 4) Unsample + // 5) Sample + int index = absl::Uniform(gen, 0, kNumCords); + if (absl::Bernoulli(gen, 0.5)) { + // Track/untrack. + if (infos[index]) { + // 1) Untrack + CordzInfo::UntrackCord(infos[index]); + infos[index] = nullptr; + } else { + // 2) Track + infos[index] = CordzInfo::TrackCord(reps[index].rep); + } + } else { + if (tokens[index]) { + if (absl::Bernoulli(gen, 0.5)) { + // 3) Iterate over Cords visible to a token. + for (const CordzInfo& info : *tokens[index]) { + // This is trivial work to allow us to compile the loop. + EXPECT_THAT(info.Next(*tokens[index]), Ne(&info)); + } + } else { + // 4) Unsample + tokens[index].reset(); + } + } else { + // 5) Sample + tokens[index] = absl::make_unique<CordzSampleToken>(); + } + } + } + for (CordzInfo* info : infos) { + if (info != nullptr) { + CordzInfo::UntrackCord(info); + } + } + }); + } + // The threads will hammer away. Give it a little bit of time for tsan to + // spot errors. + absl::SleepFor(absl::Seconds(3)); + stop.Notify(); +} + +} // namespace +} // namespace cord_internal +ABSL_NAMESPACE_END +} // namespace absl |