|
|
|||
File indexing completed on 2025-12-16 09:41:00
0001 // Copyright 2019 The Abseil Authors. 0002 // 0003 // Licensed under the Apache License, Version 2.0 (the "License"); 0004 // you may not use this file except in compliance with the License. 0005 // You may obtain a copy of the License at 0006 // 0007 // https://www.apache.org/licenses/LICENSE-2.0 0008 // 0009 // Unless required by applicable law or agreed to in writing, software 0010 // distributed under the License is distributed on an "AS IS" BASIS, 0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0012 // See the License for the specific language governing permissions and 0013 // limitations under the License. 0014 0015 #ifndef ABSL_STRINGS_INTERNAL_CORDZ_INFO_H_ 0016 #define ABSL_STRINGS_INTERNAL_CORDZ_INFO_H_ 0017 0018 #include <atomic> 0019 #include <cstdint> 0020 #include <functional> 0021 0022 #include "absl/base/config.h" 0023 #include "absl/base/internal/raw_logging.h" 0024 #include "absl/base/internal/spinlock.h" 0025 #include "absl/base/thread_annotations.h" 0026 #include "absl/strings/internal/cord_internal.h" 0027 #include "absl/strings/internal/cordz_functions.h" 0028 #include "absl/strings/internal/cordz_handle.h" 0029 #include "absl/strings/internal/cordz_statistics.h" 0030 #include "absl/strings/internal/cordz_update_tracker.h" 0031 #include "absl/synchronization/mutex.h" 0032 #include "absl/types/span.h" 0033 0034 namespace absl { 0035 ABSL_NAMESPACE_BEGIN 0036 namespace cord_internal { 0037 0038 // CordzInfo tracks a profiled Cord. Each of these objects can be in two places. 0039 // If a Cord is alive, the CordzInfo will be in the global_cordz_infos map, and 0040 // can also be retrieved via the linked list starting with 0041 // global_cordz_infos_head and continued via the cordz_info_next() method. When 0042 // a Cord has reached the end of its lifespan, the CordzInfo object will be 0043 // migrated out of the global_cordz_infos list and the global_cordz_infos_map, 0044 // and will either be deleted or appended to the global_delete_queue. If it is 0045 // placed on the global_delete_queue, the CordzInfo object will be cleaned in 0046 // the destructor of a CordzSampleToken object. 0047 class ABSL_LOCKABLE CordzInfo : public CordzHandle { 0048 public: 0049 using MethodIdentifier = CordzUpdateTracker::MethodIdentifier; 0050 0051 // TrackCord creates a CordzInfo instance which tracks important metrics of 0052 // a sampled cord, and stores the created CordzInfo instance into `cord'. All 0053 // CordzInfo instances are placed in a global list which is used to discover 0054 // and snapshot all actively tracked cords. Callers are responsible for 0055 // calling UntrackCord() before the tracked Cord instance is deleted, or to 0056 // stop tracking the sampled Cord. Callers are also responsible for guarding 0057 // changes to the 'tree' value of a Cord (InlineData.tree) through the Lock() 0058 // and Unlock() calls. Any change resulting in a new tree value for the cord 0059 // requires a call to SetCordRep() before the old tree has been unreffed 0060 // and/or deleted. `method` identifies the Cord public API method initiating 0061 // the cord to be sampled. 0062 // Requires `cord` to hold a tree, and `cord.cordz_info()` to be null. 0063 static void TrackCord(InlineData& cord, MethodIdentifier method, 0064 int64_t sampling_stride); 0065 0066 // Identical to TrackCord(), except that this function fills the 0067 // `parent_stack` and `parent_method` properties of the returned CordzInfo 0068 // instance from the provided `src` instance if `src` is sampled. 0069 // This function should be used for sampling 'copy constructed' and 'copy 0070 // assigned' cords. This function allows 'cord` to be already sampled, in 0071 // which case the CordzInfo will be newly created from `src`. 0072 static void TrackCord(InlineData& cord, const InlineData& src, 0073 MethodIdentifier method); 0074 0075 // Maybe sample the cord identified by 'cord' for method 'method'. 0076 // Uses `cordz_should_profile` to randomly pick cords to be sampled, and if 0077 // so, invokes `TrackCord` to start sampling `cord`. 0078 static void MaybeTrackCord(InlineData& cord, MethodIdentifier method); 0079 0080 // Maybe sample the cord identified by 'cord' for method 'method'. 0081 // `src` identifies a 'parent' cord which is assigned to `cord`, typically the 0082 // input cord for a copy constructor, or an assign method such as `operator=` 0083 // `cord` will be sampled if (and only if) `src` is sampled. 0084 // If `cord` is currently being sampled and `src` is not being sampled, then 0085 // this function will stop sampling the cord and reset the cord's cordz_info. 0086 // 0087 // Previously this function defined that `cord` will be sampled if either 0088 // `src` is sampled, or if `cord` is randomly picked for sampling. However, 0089 // this can cause issues, as there may be paths where some cord is assigned an 0090 // indirect copy of it's own value. As such a 'string of copies' would then 0091 // remain sampled (`src.is_profiled`), then assigning such a cord back to 0092 // 'itself' creates a cycle where the cord will converge to 'always sampled`. 0093 // 0094 // For example: 0095 // 0096 // Cord x; 0097 // for (...) { 0098 // // Copy ctor --> y.is_profiled := x.is_profiled | random(...) 0099 // Cord y = x; 0100 // ... 0101 // // Assign x = y --> x.is_profiled = y.is_profiled | random(...) 0102 // // ==> x.is_profiled |= random(...) 0103 // // ==> x converges to 'always profiled' 0104 // x = y; 0105 // } 0106 static void MaybeTrackCord(InlineData& cord, const InlineData& src, 0107 MethodIdentifier method); 0108 0109 // Stops tracking changes for a sampled cord, and deletes the provided info. 0110 // This function must be called before the sampled cord instance is deleted, 0111 // and before the root cordrep of the sampled cord is unreffed. 0112 // This function may extend the lifetime of the cordrep in cases where the 0113 // CordInfo instance is being held by a concurrent collection thread. 0114 void Untrack(); 0115 0116 // Invokes UntrackCord() on `info` if `info` is not null. 0117 static void MaybeUntrackCord(CordzInfo* info); 0118 0119 CordzInfo() = delete; 0120 CordzInfo(const CordzInfo&) = delete; 0121 CordzInfo& operator=(const CordzInfo&) = delete; 0122 0123 // Retrieves the oldest existing CordzInfo. 0124 static CordzInfo* Head(const CordzSnapshot& snapshot) 0125 ABSL_NO_THREAD_SAFETY_ANALYSIS; 0126 0127 // Retrieves the next oldest existing CordzInfo older than 'this' instance. 0128 CordzInfo* Next(const CordzSnapshot& snapshot) const 0129 ABSL_NO_THREAD_SAFETY_ANALYSIS; 0130 0131 // Locks this instance for the update identified by `method`. 0132 // Increases the count for `method` in `update_tracker`. 0133 void Lock(MethodIdentifier method) ABSL_EXCLUSIVE_LOCK_FUNCTION(mutex_); 0134 0135 // Unlocks this instance. If the contained `rep` has been set to null 0136 // indicating the Cord has been cleared or is otherwise no longer sampled, 0137 // then this method will delete this CordzInfo instance. 0138 void Unlock() ABSL_UNLOCK_FUNCTION(mutex_); 0139 0140 // Asserts that this CordzInfo instance is locked. 0141 void AssertHeld() ABSL_ASSERT_EXCLUSIVE_LOCK(mutex_); 0142 0143 // Updates the `rep` property of this instance. This methods is invoked by 0144 // Cord logic each time the root node of a sampled Cord changes, and before 0145 // the old root reference count is deleted. This guarantees that collection 0146 // code can always safely take a reference on the tracked cord. 0147 // Requires a lock to be held through the `Lock()` method. 0148 // TODO(b/117940323): annotate with ABSL_EXCLUSIVE_LOCKS_REQUIRED once all 0149 // Cord code is in a state where this can be proven true by the compiler. 0150 void SetCordRep(CordRep* rep); 0151 0152 // Returns the current `rep` property of this instance with a reference 0153 // added, or null if this instance represents a cord that has since been 0154 // deleted or untracked. 0155 CordRep* RefCordRep() const ABSL_LOCKS_EXCLUDED(mutex_); 0156 0157 // Returns the current value of `rep_` for testing purposes only. 0158 CordRep* GetCordRepForTesting() const ABSL_NO_THREAD_SAFETY_ANALYSIS { 0159 return rep_; 0160 } 0161 0162 // Sets the current value of `rep_` for testing purposes only. 0163 void SetCordRepForTesting(CordRep* rep) ABSL_NO_THREAD_SAFETY_ANALYSIS { 0164 rep_ = rep; 0165 } 0166 0167 // Returns the stack trace for where the cord was first sampled. Cords are 0168 // potentially sampled when they promote from an inlined cord to a tree or 0169 // ring representation, which is not necessarily the location where the cord 0170 // was first created. Some cords are created as inlined cords, and only as 0171 // data is added do they become a non-inlined cord. However, typically the 0172 // location represents reasonably well where the cord is 'created'. 0173 absl::Span<void* const> GetStack() const; 0174 0175 // Returns the stack trace for a sampled cord's 'parent stack trace'. This 0176 // value may be set if the cord is sampled (promoted) after being created 0177 // from, or being assigned the value of an existing (sampled) cord. 0178 absl::Span<void* const> GetParentStack() const; 0179 0180 // Retrieves the CordzStatistics associated with this Cord. The statistics 0181 // are only updated when a Cord goes through a mutation, such as an Append 0182 // or RemovePrefix. 0183 CordzStatistics GetCordzStatistics() const; 0184 0185 int64_t sampling_stride() const { return sampling_stride_; } 0186 0187 private: 0188 using SpinLock = absl::base_internal::SpinLock; 0189 using SpinLockHolder = ::absl::base_internal::SpinLockHolder; 0190 0191 // Global cordz info list. CordzInfo stores a pointer to the global list 0192 // instance to harden against ODR violations. 0193 struct List { 0194 constexpr explicit List(absl::ConstInitType) 0195 : mutex(absl::kConstInit, 0196 absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL) {} 0197 0198 SpinLock mutex; 0199 std::atomic<CordzInfo*> head ABSL_GUARDED_BY(mutex){nullptr}; 0200 }; 0201 0202 static constexpr size_t kMaxStackDepth = 64; 0203 0204 explicit CordzInfo(CordRep* rep, const CordzInfo* src, 0205 MethodIdentifier method, int64_t weight); 0206 ~CordzInfo() override; 0207 0208 // Sets `rep_` without holding a lock. 0209 void UnsafeSetCordRep(CordRep* rep) ABSL_NO_THREAD_SAFETY_ANALYSIS; 0210 0211 void Track(); 0212 0213 // Returns the parent method from `src`, which is either `parent_method_` or 0214 // `method_` depending on `parent_method_` being kUnknown. 0215 // Returns kUnknown if `src` is null. 0216 static MethodIdentifier GetParentMethod(const CordzInfo* src); 0217 0218 // Fills the provided stack from `src`, copying either `parent_stack_` or 0219 // `stack_` depending on `parent_stack_` being empty, returning the size of 0220 // the parent stack. 0221 // Returns 0 if `src` is null. 0222 static size_t FillParentStack(const CordzInfo* src, void** stack); 0223 0224 void ODRCheck() const { 0225 #ifndef NDEBUG 0226 ABSL_RAW_CHECK(list_ == &global_list_, "ODR violation in Cord"); 0227 #endif 0228 } 0229 0230 // Non-inlined implementation of `MaybeTrackCord`, which is executed if 0231 // either `src` is sampled or `cord` is sampled, and either untracks or 0232 // tracks `cord` as documented per `MaybeTrackCord`. 0233 static void MaybeTrackCordImpl(InlineData& cord, const InlineData& src, 0234 MethodIdentifier method); 0235 0236 ABSL_CONST_INIT static List global_list_; 0237 List* const list_ = &global_list_; 0238 0239 // ci_prev_ and ci_next_ require the global list mutex to be held. 0240 // Unfortunately we can't use thread annotations such that the thread safety 0241 // analysis understands that list_ and global_list_ are one and the same. 0242 std::atomic<CordzInfo*> ci_prev_{nullptr}; 0243 std::atomic<CordzInfo*> ci_next_{nullptr}; 0244 0245 mutable absl::Mutex mutex_; 0246 CordRep* rep_ ABSL_GUARDED_BY(mutex_); 0247 0248 void* stack_[kMaxStackDepth]; 0249 void* parent_stack_[kMaxStackDepth]; 0250 const size_t stack_depth_; 0251 const size_t parent_stack_depth_; 0252 const MethodIdentifier method_; 0253 const MethodIdentifier parent_method_; 0254 CordzUpdateTracker update_tracker_; 0255 const absl::Time create_time_; 0256 const int64_t sampling_stride_; 0257 }; 0258 0259 inline ABSL_ATTRIBUTE_ALWAYS_INLINE void CordzInfo::MaybeTrackCord( 0260 InlineData& cord, MethodIdentifier method) { 0261 auto stride = cordz_should_profile(); 0262 if (ABSL_PREDICT_FALSE(stride > 0)) { 0263 TrackCord(cord, method, stride); 0264 } 0265 } 0266 0267 inline ABSL_ATTRIBUTE_ALWAYS_INLINE void CordzInfo::MaybeTrackCord( 0268 InlineData& cord, const InlineData& src, MethodIdentifier method) { 0269 if (ABSL_PREDICT_FALSE(InlineData::is_either_profiled(cord, src))) { 0270 MaybeTrackCordImpl(cord, src, method); 0271 } 0272 } 0273 0274 inline ABSL_ATTRIBUTE_ALWAYS_INLINE void CordzInfo::MaybeUntrackCord( 0275 CordzInfo* info) { 0276 if (ABSL_PREDICT_FALSE(info)) { 0277 info->Untrack(); 0278 } 0279 } 0280 0281 inline void CordzInfo::AssertHeld() ABSL_ASSERT_EXCLUSIVE_LOCK(mutex_) { 0282 #ifndef NDEBUG 0283 mutex_.AssertHeld(); 0284 #endif 0285 } 0286 0287 inline void CordzInfo::SetCordRep(CordRep* rep) { 0288 AssertHeld(); 0289 rep_ = rep; 0290 } 0291 0292 inline void CordzInfo::UnsafeSetCordRep(CordRep* rep) { rep_ = rep; } 0293 0294 inline CordRep* CordzInfo::RefCordRep() const ABSL_LOCKS_EXCLUDED(mutex_) { 0295 MutexLock lock(&mutex_); 0296 return rep_ ? CordRep::Ref(rep_) : nullptr; 0297 } 0298 0299 } // namespace cord_internal 0300 ABSL_NAMESPACE_END 0301 } // namespace absl 0302 0303 #endif // ABSL_STRINGS_INTERNAL_CORDZ_INFO_H_
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|