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     kAppendCordBuffer,
44     kAppendExternalMemory,
45     kAppendString,
46     kAssignCord,
47     kAssignString,
48     kClear,
49     kConstructorCord,
50     kConstructorString,
51     kCordReader,
52     kFlatten,
53     kGetAppendBuffer,
54     kGetAppendRegion,
55     kMakeCordFromExternal,
56     kMoveAppendCord,
57     kMoveAssignCord,
58     kMovePrependCord,
59     kPrependCord,
60     kPrependCordBuffer,
61     kPrependString,
62     kRemovePrefix,
63     kRemoveSuffix,
64     kSetExpectedChecksum,
65     kSubCord,
66 
67     // kNumMethods defines the number of entries: must be the last entry.
68     kNumMethods,
69   };
70 
71   // Constructs a new instance. All counters are zero-initialized.
CordzUpdateTracker()72   constexpr CordzUpdateTracker() noexcept : values_{} {}
73 
74   // Copy constructs a new instance.
CordzUpdateTracker(const CordzUpdateTracker & rhs)75   CordzUpdateTracker(const CordzUpdateTracker& rhs) noexcept { *this = rhs; }
76 
77   // Assigns the provided value to this instance.
78   CordzUpdateTracker& operator=(const CordzUpdateTracker& rhs) noexcept {
79     for (int i = 0; i < kNumMethods; ++i) {
80       values_[i].store(rhs.values_[i].load(std::memory_order_relaxed),
81                        std::memory_order_relaxed);
82     }
83     return *this;
84   }
85 
86   // Returns the value for the specified method.
Value(MethodIdentifier method)87   int64_t Value(MethodIdentifier method) const {
88     return values_[method].load(std::memory_order_relaxed);
89   }
90 
91   // Increases the value for the specified method by `n`
92   void LossyAdd(MethodIdentifier method, int64_t n = 1) {
93     auto& value = values_[method];
94     value.store(value.load(std::memory_order_relaxed) + n,
95                 std::memory_order_relaxed);
96   }
97 
98   // Adds all the values from `src` to this instance
LossyAdd(const CordzUpdateTracker & src)99   void LossyAdd(const CordzUpdateTracker& src) {
100     for (int i = 0; i < kNumMethods; ++i) {
101       MethodIdentifier method = static_cast<MethodIdentifier>(i);
102       if (int64_t value = src.Value(method)) {
103         LossyAdd(method, value);
104       }
105     }
106   }
107 
108  private:
109   // Until C++20 std::atomic is not constexpr default-constructible, so we need
110   // a wrapper for this class to be constexpr constructible.
111   class Counter : public std::atomic<int64_t> {
112    public:
Counter()113     constexpr Counter() noexcept : std::atomic<int64_t>(0) {}
114   };
115 
116   Counter values_[kNumMethods];
117 };
118 
119 }  // namespace cord_internal
120 ABSL_NAMESPACE_END
121 }  // namespace absl
122 
123 #endif  // ABSL_STRINGS_INTERNAL_CORDZ_UPDATE_TRACKER_H_
124