• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Abseil Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ABSL_STRINGS_INTERNAL_CORDZ_UPDATE_TRACKER_H_
16 #define ABSL_STRINGS_INTERNAL_CORDZ_UPDATE_TRACKER_H_
17 
18 #include <atomic>
19 #include <cstdint>
20 
21 #include "absl/base/config.h"
22 
23 namespace absl {
24 ABSL_NAMESPACE_BEGIN
25 namespace cord_internal {
26 
27 // CordzUpdateTracker tracks counters for Cord update methods.
28 //
29 // The purpose of CordzUpdateTracker is to track the number of calls to methods
30 // updating Cord data for sampled cords. The class internally uses 'lossy'
31 // atomic operations: Cord is thread-compatible, so there is no need to
32 // synchronize updates. However, Cordz collection threads may call 'Value()' at
33 // any point, so the class needs to provide thread safe access.
34 //
35 // This class is thread-safe. But as per above comments, all non-const methods
36 // should be used single-threaded only: updates are thread-safe but lossy.
37 class CordzUpdateTracker {
38  public:
39   // Tracked update methods.
40   enum MethodIdentifier {
41     kUnknown,
42     kAppendCord,
43     kAppendExternalMemory,
44     kAppendString,
45     kAssignCord,
46     kAssignString,
47     kClear,
48     kConstructorCord,
49     kConstructorString,
50     kCordReader,
51     kFlatten,
52     kGetAppendRegion,
53     kMakeCordFromExternal,
54     kMoveAppendCord,
55     kMoveAssignCord,
56     kMovePrependCord,
57     kPrependCord,
58     kPrependString,
59     kRemovePrefix,
60     kRemoveSuffix,
61     kSubCord,
62 
63     // kNumMethods defines the number of entries: must be the last entry.
64     kNumMethods,
65   };
66 
67   // Constructs a new instance. All counters are zero-initialized.
CordzUpdateTracker()68   constexpr CordzUpdateTracker() noexcept : values_{} {}
69 
70   // Copy constructs a new instance.
CordzUpdateTracker(const CordzUpdateTracker & rhs)71   CordzUpdateTracker(const CordzUpdateTracker& rhs) noexcept { *this = rhs; }
72 
73   // Assigns the provided value to this instance.
74   CordzUpdateTracker& operator=(const CordzUpdateTracker& rhs) noexcept {
75     for (int i = 0; i < kNumMethods; ++i) {
76       values_[i].store(rhs.values_[i].load(std::memory_order_relaxed),
77                        std::memory_order_relaxed);
78     }
79     return *this;
80   }
81 
82   // Returns the value for the specified method.
Value(MethodIdentifier method)83   int64_t Value(MethodIdentifier method) const {
84     return values_[method].load(std::memory_order_relaxed);
85   }
86 
87   // Increases the value for the specified method by `n`
88   void LossyAdd(MethodIdentifier method, int64_t n = 1) {
89     auto& value = values_[method];
90     value.store(value.load(std::memory_order_relaxed) + n,
91                 std::memory_order_relaxed);
92   }
93 
94   // Adds all the values from `src` to this instance
LossyAdd(const CordzUpdateTracker & src)95   void LossyAdd(const CordzUpdateTracker& src) {
96     for (int i = 0; i < kNumMethods; ++i) {
97       MethodIdentifier method = static_cast<MethodIdentifier>(i);
98       if (int64_t value = src.Value(method)) {
99         LossyAdd(method, value);
100       }
101     }
102   }
103 
104  private:
105   // Until C++20 std::atomic is not constexpr default-constructible, so we need
106   // a wrapper for this class to be constexpr constructible.
107   class Counter : public std::atomic<int64_t> {
108    public:
Counter()109     constexpr Counter() noexcept : std::atomic<int64_t>(0) {}
110   };
111 
112   Counter values_[kNumMethods];
113 };
114 
115 }  // namespace cord_internal
116 ABSL_NAMESPACE_END
117 }  // namespace absl
118 
119 #endif  // ABSL_STRINGS_INTERNAL_CORDZ_UPDATE_TRACKER_H_
120