Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_
00040 #define ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_
00041
00042 #include <atomic>
00043 #include <functional>
00044 #include <memory>
00045 #include <vector>
00046
00047 #include "absl/base/internal/per_thread_tls.h"
00048 #include "absl/base/optimization.h"
00049 #include "absl/container/internal/have_sse.h"
00050 #include "absl/synchronization/mutex.h"
00051 #include "absl/utility/utility.h"
00052
00053 namespace absl {
00054 namespace container_internal {
00055
00056
00057
00058
00059 struct HashtablezInfo {
00060
00061 HashtablezInfo();
00062 ~HashtablezInfo();
00063 HashtablezInfo(const HashtablezInfo&) = delete;
00064 HashtablezInfo& operator=(const HashtablezInfo&) = delete;
00065
00066
00067
00068 void PrepareForSampling() EXCLUSIVE_LOCKS_REQUIRED(init_mu);
00069
00070
00071
00072 std::atomic<size_t> capacity;
00073 std::atomic<size_t> size;
00074 std::atomic<size_t> num_erases;
00075 std::atomic<size_t> max_probe_length;
00076 std::atomic<size_t> total_probe_length;
00077 std::atomic<size_t> hashes_bitwise_or;
00078 std::atomic<size_t> hashes_bitwise_and;
00079
00080
00081
00082
00083
00084 absl::Mutex init_mu;
00085 HashtablezInfo* next;
00086 HashtablezInfo* dead GUARDED_BY(init_mu);
00087
00088
00089
00090
00091
00092
00093 static constexpr int kMaxStackDepth = 64;
00094 absl::Time create_time;
00095 int32_t depth;
00096 void* stack[kMaxStackDepth];
00097 };
00098
00099 inline void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length) {
00100 #if SWISSTABLE_HAVE_SSE2
00101 total_probe_length /= 16;
00102 #else
00103 total_probe_length /= 8;
00104 #endif
00105 info->total_probe_length.store(total_probe_length, std::memory_order_relaxed);
00106 info->num_erases.store(0, std::memory_order_relaxed);
00107 }
00108
00109 inline void RecordStorageChangedSlow(HashtablezInfo* info, size_t size,
00110 size_t capacity) {
00111 info->size.store(size, std::memory_order_relaxed);
00112 info->capacity.store(capacity, std::memory_order_relaxed);
00113 if (size == 0) {
00114
00115 RecordRehashSlow(info, 0);
00116 }
00117 }
00118
00119 void RecordInsertSlow(HashtablezInfo* info, size_t hash,
00120 size_t distance_from_desired);
00121
00122 inline void RecordEraseSlow(HashtablezInfo* info) {
00123 info->size.fetch_sub(1, std::memory_order_relaxed);
00124 info->num_erases.fetch_add(1, std::memory_order_relaxed);
00125 }
00126
00127 HashtablezInfo* SampleSlow(int64_t* next_sample);
00128 void UnsampleSlow(HashtablezInfo* info);
00129
00130 class HashtablezInfoHandle {
00131 public:
00132 explicit HashtablezInfoHandle() : info_(nullptr) {}
00133 explicit HashtablezInfoHandle(HashtablezInfo* info) : info_(info) {}
00134 ~HashtablezInfoHandle() {
00135 if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
00136 UnsampleSlow(info_);
00137 }
00138
00139 HashtablezInfoHandle(const HashtablezInfoHandle&) = delete;
00140 HashtablezInfoHandle& operator=(const HashtablezInfoHandle&) = delete;
00141
00142 HashtablezInfoHandle(HashtablezInfoHandle&& o) noexcept
00143 : info_(absl::exchange(o.info_, nullptr)) {}
00144 HashtablezInfoHandle& operator=(HashtablezInfoHandle&& o) noexcept {
00145 if (ABSL_PREDICT_FALSE(info_ != nullptr)) {
00146 UnsampleSlow(info_);
00147 }
00148 info_ = absl::exchange(o.info_, nullptr);
00149 return *this;
00150 }
00151
00152 inline void RecordStorageChanged(size_t size, size_t capacity) {
00153 if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
00154 RecordStorageChangedSlow(info_, size, capacity);
00155 }
00156
00157 inline void RecordRehash(size_t total_probe_length) {
00158 if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
00159 RecordRehashSlow(info_, total_probe_length);
00160 }
00161
00162 inline void RecordInsert(size_t hash, size_t distance_from_desired) {
00163 if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
00164 RecordInsertSlow(info_, hash, distance_from_desired);
00165 }
00166
00167 inline void RecordErase() {
00168 if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
00169 RecordEraseSlow(info_);
00170 }
00171
00172 friend inline void swap(HashtablezInfoHandle& lhs,
00173 HashtablezInfoHandle& rhs) {
00174 std::swap(lhs.info_, rhs.info_);
00175 }
00176
00177 private:
00178 friend class HashtablezInfoHandlePeer;
00179 HashtablezInfo* info_;
00180 };
00181
00182 #if ABSL_PER_THREAD_TLS == 1
00183 extern ABSL_PER_THREAD_TLS_KEYWORD int64_t global_next_sample;
00184 #endif // ABSL_PER_THREAD_TLS
00185
00186
00187
00188 inline HashtablezInfoHandle Sample() {
00189 #if ABSL_PER_THREAD_TLS == 0
00190 static auto* mu = new absl::Mutex;
00191 static int64_t global_next_sample = 0;
00192 absl::MutexLock l(mu);
00193 #endif // !ABSL_HAVE_THREAD_LOCAL
00194
00195 if (ABSL_PREDICT_TRUE(--global_next_sample > 0)) {
00196 return HashtablezInfoHandle(nullptr);
00197 }
00198 return HashtablezInfoHandle(SampleSlow(&global_next_sample));
00199 }
00200
00201
00202
00203
00204
00205 class HashtablezSampler {
00206 public:
00207
00208 static HashtablezSampler& Global();
00209
00210 HashtablezSampler();
00211 ~HashtablezSampler();
00212
00213
00214 HashtablezInfo* Register();
00215
00216
00217 void Unregister(HashtablezInfo* sample);
00218
00219
00220
00221
00222
00223 using DisposeCallback = void (*)(const HashtablezInfo&);
00224 DisposeCallback SetDisposeCallback(DisposeCallback f);
00225
00226
00227
00228 int64_t Iterate(const std::function<void(const HashtablezInfo& stack)>& f);
00229
00230 private:
00231 void PushNew(HashtablezInfo* sample);
00232 void PushDead(HashtablezInfo* sample);
00233 HashtablezInfo* PopDead();
00234
00235 std::atomic<size_t> dropped_samples_;
00236 std::atomic<size_t> size_estimate_;
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 std::atomic<HashtablezInfo*> all_;
00264 HashtablezInfo graveyard_;
00265
00266 std::atomic<DisposeCallback> dispose_;
00267 };
00268
00269
00270 void SetHashtablezEnabled(bool enabled);
00271
00272
00273 void SetHashtablezSampleParameter(int32_t rate);
00274
00275
00276 void SetHashtablezMaxSamples(int32_t max);
00277
00278
00279
00280
00281
00282
00283 extern "C" const bool kAbslContainerInternalSampleEverything;
00284
00285 }
00286 }
00287
00288 #endif // ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_