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