1 // Copyright 2019 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_INFO_H_
16 #define ABSL_STRINGS_INTERNAL_CORDZ_INFO_H_
17
18 #include <atomic>
19 #include <cstdint>
20 #include <functional>
21
22 #include "absl/base/config.h"
23 #include "absl/base/internal/raw_logging.h"
24 #include "absl/base/internal/spinlock.h"
25 #include "absl/base/thread_annotations.h"
26 #include "absl/strings/internal/cord_internal.h"
27 #include "absl/strings/internal/cordz_functions.h"
28 #include "absl/strings/internal/cordz_handle.h"
29 #include "absl/strings/internal/cordz_statistics.h"
30 #include "absl/strings/internal/cordz_update_tracker.h"
31 #include "absl/synchronization/mutex.h"
32 #include "absl/types/span.h"
33
34 namespace absl {
35 ABSL_NAMESPACE_BEGIN
36 namespace cord_internal {
37
38 // CordzInfo tracks a profiled Cord. Each of these objects can be in two places.
39 // If a Cord is alive, the CordzInfo will be in the global_cordz_infos map, and
40 // can also be retrieved via the linked list starting with
41 // global_cordz_infos_head and continued via the cordz_info_next() method. When
42 // a Cord has reached the end of its lifespan, the CordzInfo object will be
43 // migrated out of the global_cordz_infos list and the global_cordz_infos_map,
44 // and will either be deleted or appended to the global_delete_queue. If it is
45 // placed on the global_delete_queue, the CordzInfo object will be cleaned in
46 // the destructor of a CordzSampleToken object.
47 class ABSL_LOCKABLE CordzInfo : public CordzHandle {
48 public:
49 using MethodIdentifier = CordzUpdateTracker::MethodIdentifier;
50
51 // TrackCord creates a CordzInfo instance which tracks important metrics of
52 // a sampled cord, and stores the created CordzInfo instance into `cord'. All
53 // CordzInfo instances are placed in a global list which is used to discover
54 // and snapshot all actively tracked cords. Callers are responsible for
55 // calling UntrackCord() before the tracked Cord instance is deleted, or to
56 // stop tracking the sampled Cord. Callers are also responsible for guarding
57 // changes to the 'tree' value of a Cord (InlineData.tree) through the Lock()
58 // and Unlock() calls. Any change resulting in a new tree value for the cord
59 // requires a call to SetCordRep() before the old tree has been unreffed
60 // and/or deleted. `method` identifies the Cord public API method initiating
61 // the cord to be sampled.
62 // Requires `cord` to hold a tree, and `cord.cordz_info()` to be null.
63 static void TrackCord(InlineData& cord, MethodIdentifier method,
64 int64_t sampling_stride);
65
66 // Identical to TrackCord(), except that this function fills the
67 // `parent_stack` and `parent_method` properties of the returned CordzInfo
68 // instance from the provided `src` instance if `src` is sampled.
69 // This function should be used for sampling 'copy constructed' and 'copy
70 // assigned' cords. This function allows 'cord` to be already sampled, in
71 // which case the CordzInfo will be newly created from `src`.
72 static void TrackCord(InlineData& cord, const InlineData& src,
73 MethodIdentifier method);
74
75 // Maybe sample the cord identified by 'cord' for method 'method'.
76 // Uses `cordz_should_profile` to randomly pick cords to be sampled, and if
77 // so, invokes `TrackCord` to start sampling `cord`.
78 static void MaybeTrackCord(InlineData& cord, MethodIdentifier method);
79
80 // Maybe sample the cord identified by 'cord' for method 'method'.
81 // `src` identifies a 'parent' cord which is assigned to `cord`, typically the
82 // input cord for a copy constructor, or an assign method such as `operator=`
83 // `cord` will be sampled if (and only if) `src` is sampled.
84 // If `cord` is currently being sampled and `src` is not being sampled, then
85 // this function will stop sampling the cord and reset the cord's cordz_info.
86 //
87 // Previously this function defined that `cord` will be sampled if either
88 // `src` is sampled, or if `cord` is randomly picked for sampling. However,
89 // this can cause issues, as there may be paths where some cord is assigned an
90 // indirect copy of it's own value. As such a 'string of copies' would then
91 // remain sampled (`src.is_profiled`), then assigning such a cord back to
92 // 'itself' creates a cycle where the cord will converge to 'always sampled`.
93 //
94 // For example:
95 //
96 // Cord x;
97 // for (...) {
98 // // Copy ctor --> y.is_profiled := x.is_profiled | random(...)
99 // Cord y = x;
100 // ...
101 // // Assign x = y --> x.is_profiled = y.is_profiled | random(...)
102 // // ==> x.is_profiled |= random(...)
103 // // ==> x converges to 'always profiled'
104 // x = y;
105 // }
106 static void MaybeTrackCord(InlineData& cord, const InlineData& src,
107 MethodIdentifier method);
108
109 // Stops tracking changes for a sampled cord, and deletes the provided info.
110 // This function must be called before the sampled cord instance is deleted,
111 // and before the root cordrep of the sampled cord is unreffed.
112 // This function may extend the lifetime of the cordrep in cases where the
113 // CordInfo instance is being held by a concurrent collection thread.
114 void Untrack();
115
116 // Invokes UntrackCord() on `info` if `info` is not null.
117 static void MaybeUntrackCord(CordzInfo* info);
118
119 CordzInfo() = delete;
120 CordzInfo(const CordzInfo&) = delete;
121 CordzInfo& operator=(const CordzInfo&) = delete;
122
123 // Retrieves the oldest existing CordzInfo.
124 static CordzInfo* Head(const CordzSnapshot& snapshot)
125 ABSL_NO_THREAD_SAFETY_ANALYSIS;
126
127 // Retrieves the next oldest existing CordzInfo older than 'this' instance.
128 CordzInfo* Next(const CordzSnapshot& snapshot) const
129 ABSL_NO_THREAD_SAFETY_ANALYSIS;
130
131 // Locks this instance for the update identified by `method`.
132 // Increases the count for `method` in `update_tracker`.
133 void Lock(MethodIdentifier method) ABSL_EXCLUSIVE_LOCK_FUNCTION(mutex_);
134
135 // Unlocks this instance. If the contained `rep` has been set to null
136 // indicating the Cord has been cleared or is otherwise no longer sampled,
137 // then this method will delete this CordzInfo instance.
138 void Unlock() ABSL_UNLOCK_FUNCTION(mutex_);
139
140 // Asserts that this CordzInfo instance is locked.
141 void AssertHeld() ABSL_ASSERT_EXCLUSIVE_LOCK(mutex_);
142
143 // Updates the `rep` property of this instance. This methods is invoked by
144 // Cord logic each time the root node of a sampled Cord changes, and before
145 // the old root reference count is deleted. This guarantees that collection
146 // code can always safely take a reference on the tracked cord.
147 // Requires a lock to be held through the `Lock()` method.
148 // TODO(b/117940323): annotate with ABSL_EXCLUSIVE_LOCKS_REQUIRED once all
149 // Cord code is in a state where this can be proven true by the compiler.
150 void SetCordRep(CordRep* rep);
151
152 // Returns the current `rep` property of this instance with a reference
153 // added, or null if this instance represents a cord that has since been
154 // deleted or untracked.
155 CordRep* RefCordRep() const ABSL_LOCKS_EXCLUDED(mutex_);
156
157 // Returns the current value of `rep_` for testing purposes only.
GetCordRepForTesting()158 CordRep* GetCordRepForTesting() const ABSL_NO_THREAD_SAFETY_ANALYSIS {
159 return rep_;
160 }
161
162 // Sets the current value of `rep_` for testing purposes only.
SetCordRepForTesting(CordRep * rep)163 void SetCordRepForTesting(CordRep* rep) ABSL_NO_THREAD_SAFETY_ANALYSIS {
164 rep_ = rep;
165 }
166
167 // Returns the stack trace for where the cord was first sampled. Cords are
168 // potentially sampled when they promote from an inlined cord to a tree or
169 // ring representation, which is not necessarily the location where the cord
170 // was first created. Some cords are created as inlined cords, and only as
171 // data is added do they become a non-inlined cord. However, typically the
172 // location represents reasonably well where the cord is 'created'.
173 absl::Span<void* const> GetStack() const;
174
175 // Returns the stack trace for a sampled cord's 'parent stack trace'. This
176 // value may be set if the cord is sampled (promoted) after being created
177 // from, or being assigned the value of an existing (sampled) cord.
178 absl::Span<void* const> GetParentStack() const;
179
180 // Retrieves the CordzStatistics associated with this Cord. The statistics
181 // are only updated when a Cord goes through a mutation, such as an Append
182 // or RemovePrefix.
183 CordzStatistics GetCordzStatistics() const;
184
sampling_stride()185 int64_t sampling_stride() const { return sampling_stride_; }
186
187 private:
188 using SpinLock = absl::base_internal::SpinLock;
189 using SpinLockHolder = ::absl::base_internal::SpinLockHolder;
190
191 // Global cordz info list. CordzInfo stores a pointer to the global list
192 // instance to harden against ODR violations.
193 struct List {
ListList194 constexpr explicit List(absl::ConstInitType)
195 : mutex(absl::kConstInit,
196 absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL) {}
197
198 SpinLock mutex;
ABSL_GUARDED_BYList199 std::atomic<CordzInfo*> head ABSL_GUARDED_BY(mutex){nullptr};
200 };
201
202 static constexpr size_t kMaxStackDepth = 64;
203
204 explicit CordzInfo(CordRep* rep, const CordzInfo* src,
205 MethodIdentifier method, int64_t weight);
206 ~CordzInfo() override;
207
208 // Sets `rep_` without holding a lock.
209 void UnsafeSetCordRep(CordRep* rep) ABSL_NO_THREAD_SAFETY_ANALYSIS;
210
211 void Track();
212
213 // Returns the parent method from `src`, which is either `parent_method_` or
214 // `method_` depending on `parent_method_` being kUnknown.
215 // Returns kUnknown if `src` is null.
216 static MethodIdentifier GetParentMethod(const CordzInfo* src);
217
218 // Fills the provided stack from `src`, copying either `parent_stack_` or
219 // `stack_` depending on `parent_stack_` being empty, returning the size of
220 // the parent stack.
221 // Returns 0 if `src` is null.
222 static size_t FillParentStack(const CordzInfo* src, void** stack);
223
ODRCheck()224 void ODRCheck() const {
225 #ifndef NDEBUG
226 ABSL_RAW_CHECK(list_ == &global_list_, "ODR violation in Cord");
227 #endif
228 }
229
230 // Non-inlined implementation of `MaybeTrackCord`, which is executed if
231 // either `src` is sampled or `cord` is sampled, and either untracks or
232 // tracks `cord` as documented per `MaybeTrackCord`.
233 static void MaybeTrackCordImpl(InlineData& cord, const InlineData& src,
234 MethodIdentifier method);
235
236 ABSL_CONST_INIT static List global_list_;
237 List* const list_ = &global_list_;
238
239 // ci_prev_ and ci_next_ require the global list mutex to be held.
240 // Unfortunately we can't use thread annotations such that the thread safety
241 // analysis understands that list_ and global_list_ are one and the same.
242 std::atomic<CordzInfo*> ci_prev_{nullptr};
243 std::atomic<CordzInfo*> ci_next_{nullptr};
244
245 mutable absl::Mutex mutex_;
246 CordRep* rep_ ABSL_GUARDED_BY(mutex_);
247
248 void* stack_[kMaxStackDepth];
249 void* parent_stack_[kMaxStackDepth];
250 const size_t stack_depth_;
251 const size_t parent_stack_depth_;
252 const MethodIdentifier method_;
253 const MethodIdentifier parent_method_;
254 CordzUpdateTracker update_tracker_;
255 const absl::Time create_time_;
256 const int64_t sampling_stride_;
257 };
258
MaybeTrackCord(InlineData & cord,MethodIdentifier method)259 inline ABSL_ATTRIBUTE_ALWAYS_INLINE void CordzInfo::MaybeTrackCord(
260 InlineData& cord, MethodIdentifier method) {
261 auto stride = cordz_should_profile();
262 if (ABSL_PREDICT_FALSE(stride > 0)) {
263 TrackCord(cord, method, stride);
264 }
265 }
266
MaybeTrackCord(InlineData & cord,const InlineData & src,MethodIdentifier method)267 inline ABSL_ATTRIBUTE_ALWAYS_INLINE void CordzInfo::MaybeTrackCord(
268 InlineData& cord, const InlineData& src, MethodIdentifier method) {
269 if (ABSL_PREDICT_FALSE(InlineData::is_either_profiled(cord, src))) {
270 MaybeTrackCordImpl(cord, src, method);
271 }
272 }
273
MaybeUntrackCord(CordzInfo * info)274 inline ABSL_ATTRIBUTE_ALWAYS_INLINE void CordzInfo::MaybeUntrackCord(
275 CordzInfo* info) {
276 if (ABSL_PREDICT_FALSE(info)) {
277 info->Untrack();
278 }
279 }
280
AssertHeld()281 inline void CordzInfo::AssertHeld() ABSL_ASSERT_EXCLUSIVE_LOCK(mutex_) {
282 #ifndef NDEBUG
283 mutex_.AssertHeld();
284 #endif
285 }
286
SetCordRep(CordRep * rep)287 inline void CordzInfo::SetCordRep(CordRep* rep) {
288 AssertHeld();
289 rep_ = rep;
290 }
291
UnsafeSetCordRep(CordRep * rep)292 inline void CordzInfo::UnsafeSetCordRep(CordRep* rep) { rep_ = rep; }
293
RefCordRep()294 inline CordRep* CordzInfo::RefCordRep() const ABSL_LOCKS_EXCLUDED(mutex_) {
295 MutexLock lock(&mutex_);
296 return rep_ ? CordRep::Ref(rep_) : nullptr;
297 }
298
299 } // namespace cord_internal
300 ABSL_NAMESPACE_END
301 } // namespace absl
302
303 #endif // ABSL_STRINGS_INTERNAL_CORDZ_INFO_H_
304