| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/smallhash.h |
| Date: | 2026-10-11 02:40:02 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 259 | 263 | 98.5% |
| Branches: | 88 | 110 | 80.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #ifndef CVMFS_SMALLHASH_H_ | ||
| 6 | #define CVMFS_SMALLHASH_H_ | ||
| 7 | |||
| 8 | #include <inttypes.h> | ||
| 9 | #include <pthread.h> | ||
| 10 | #include <stdint.h> | ||
| 11 | |||
| 12 | #include <algorithm> | ||
| 13 | #include <atomic> | ||
| 14 | #include <cassert> | ||
| 15 | #include <cstdlib> | ||
| 16 | #include <new> | ||
| 17 | |||
| 18 | #include "duplex_testing.h" | ||
| 19 | #include "util/murmur.hxx" | ||
| 20 | #include "util/prng.h" | ||
| 21 | #include "util/smalloc.h" | ||
| 22 | |||
| 23 | /** | ||
| 24 | * Hash table with linear probing as collision resolution. Works only for | ||
| 25 | * a fixed (maximum) number of elements, i.e. no resizing. Load factor fixed | ||
| 26 | * to 0.7. | ||
| 27 | */ | ||
| 28 | template<class Key, class Value, class Derived> | ||
| 29 | class SmallHashBase { | ||
| 30 | FRIEND_TEST(T_Smallhash, InsertAndCopyMd5Slow); | ||
| 31 | |||
| 32 | public: | ||
| 33 | static const double kLoadFactor; // mainly useless for the dynamic version | ||
| 34 | static const double kThresholdGrow; // only used for resizable version | ||
| 35 | static const double kThresholdShrink; // only used for resizable version | ||
| 36 | |||
| 37 | 142387 | SmallHashBase() { | |
| 38 | 142387 | keys_ = NULL; | |
| 39 | 142387 | values_ = NULL; | |
| 40 | 142387 | hasher_ = NULL; | |
| 41 | 142387 | bytes_allocated_ = 0; | |
| 42 | 142387 | num_collisions_ = 0; | |
| 43 | 142387 | max_collisions_ = 0; | |
| 44 | |||
| 45 | // Properly initialized by Init() | ||
| 46 | 142387 | capacity_ = 0; | |
| 47 | 142387 | initial_capacity_ = 0; | |
| 48 | 142387 | size_ = 0; | |
| 49 | 142387 | } | |
| 50 | |||
| 51 | 142249 | ~SmallHashBase() { DeallocMemory(keys_, values_, capacity_); } | |
| 52 | |||
| 53 | 142371 | void Init(uint32_t expected_size, Key empty, | |
| 54 | uint32_t (*hasher)(const Key &key)) { | ||
| 55 | 142371 | hasher_ = hasher; | |
| 56 | 142371 | empty_key_ = empty; | |
| 57 | 142371 | capacity_ = static_cast<uint32_t>(static_cast<double>(expected_size) | |
| 58 | 142371 | / kLoadFactor); | |
| 59 | 142371 | initial_capacity_ = capacity_; | |
| 60 | 142371 | static_cast<Derived *>(this)->SetThresholds(); // No-op for fixed size | |
| 61 | 142371 | AllocMemory(); | |
| 62 | 142371 | this->DoClear(false); | |
| 63 | 142371 | } | |
| 64 | |||
| 65 | 38896002 | bool Lookup(const Key &key, Value *value) const { | |
| 66 | uint32_t bucket; | ||
| 67 | uint32_t collisions; | ||
| 68 |
1/2✓ Branch 1 taken 38896136 times.
✗ Branch 2 not taken.
|
38896002 | const bool found = DoLookup(key, &bucket, &collisions); |
| 69 |
2/2✓ Branch 0 taken 29850781 times.
✓ Branch 1 taken 9045355 times.
|
38908656 | if (found) |
| 70 |
1/2✓ Branch 1 taken 2184 times.
✗ Branch 2 not taken.
|
29854452 | *value = values_[bucket]; |
| 71 | 38908656 | return found; | |
| 72 | } | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Returns both the key and the value. That is useful if Key's equality | ||
| 76 | * operator implements an equivalence relation on Key. In this case, LookupEx | ||
| 77 | * returns the key representing the equivalence class that has been used | ||
| 78 | * during Insert(). | ||
| 79 | * Used to return a glue::InodeEx element when looking for an inode. | ||
| 80 | */ | ||
| 81 | 48 | bool LookupEx(Key *key, Value *value) const { | |
| 82 | 48 | uint32_t bucket = ScaleHash(*key); | |
| 83 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 2 taken 8 times.
|
48 | while (!(keys_[bucket] == empty_key_)) { |
| 84 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
32 | if (keys_[bucket] == *key) { |
| 85 | 32 | *key = keys_[bucket]; | |
| 86 | 32 | *value = values_[bucket]; | |
| 87 | 32 | return true; | |
| 88 | } | ||
| 89 | ✗ | bucket = (bucket + 1) % capacity_; | |
| 90 | } | ||
| 91 | 16 | return false; | |
| 92 | } | ||
| 93 | |||
| 94 | 19024065 | bool Contains(const Key &key) const { | |
| 95 | uint32_t bucket; | ||
| 96 | uint32_t collisions; | ||
| 97 |
1/2✓ Branch 1 taken 19024030 times.
✗ Branch 2 not taken.
|
19024065 | const bool found = DoLookup(key, &bucket, &collisions); |
| 98 | 19024065 | return found; | |
| 99 | } | ||
| 100 | |||
| 101 | 609519112 | void Insert(const Key &key, const Value &value) { | |
| 102 | 609519112 | static_cast<Derived *>(this)->Grow(); // No-op if fixed-size | |
| 103 | 608234020 | const bool overwritten = DoInsert(key, value, true); | |
| 104 | 607368498 | size_ += !overwritten; // size + 1 if the key was not yet in the map | |
| 105 | 607368498 | } | |
| 106 | |||
| 107 | 47217298 | bool Erase(const Key &key) { | |
| 108 | uint32_t bucket; | ||
| 109 | uint32_t collisions; | ||
| 110 |
1/2✓ Branch 1 taken 46295423 times.
✗ Branch 2 not taken.
|
47217298 | const bool found = DoLookup(key, &bucket, &collisions); |
| 111 |
1/2✓ Branch 0 taken 46296854 times.
✗ Branch 1 not taken.
|
46299444 | if (found) { |
| 112 | 46300498 | keys_[bucket] = empty_key_; | |
| 113 | 46300498 | size_--; | |
| 114 | 46300498 | bucket = (bucket + 1) % capacity_; | |
| 115 |
3/3✓ Branch 0 taken 38320076 times.
✓ Branch 1 taken 32923088 times.
✓ Branch 2 taken 16200065 times.
|
87446876 | while (!(keys_[bucket] == empty_key_)) { |
| 116 | 41446986 | Key rehash = keys_[bucket]; | |
| 117 | 41446986 | keys_[bucket] = empty_key_; | |
| 118 |
1/2✓ Branch 1 taken 41146375 times.
✗ Branch 2 not taken.
|
41446986 | DoInsert(rehash, values_[bucket], false); |
| 119 | 41146378 | bucket = (bucket + 1) % capacity_; | |
| 120 | } | ||
| 121 |
1/2✓ Branch 1 taken 46419044 times.
✗ Branch 2 not taken.
|
45999890 | static_cast<Derived *>(this)->Shrink(); // No-op if fixed-size |
| 122 | } | ||
| 123 | 46427416 | return found; | |
| 124 | } | ||
| 125 | |||
| 126 | 483 | void Clear() { DoClear(true); } | |
| 127 | |||
| 128 | 5913 | uint64_t bytes_allocated() const { return bytes_allocated_; } | |
| 129 | 1614 | static double GetEntrySize() { | |
| 130 | 3228 | const double unit = sizeof(Key) + sizeof(Value); | |
| 131 | 3228 | return unit / kLoadFactor; | |
| 132 | } | ||
| 133 | |||
| 134 | void GetCollisionStats(uint64_t *num_collisions, | ||
| 135 | uint32_t *max_collisions) const { | ||
| 136 | *num_collisions = num_collisions_; | ||
| 137 | *max_collisions = max_collisions_; | ||
| 138 | } | ||
| 139 | |||
| 140 | // Careful with the direct access TODO: iterator | ||
| 141 | uint32_t capacity() const { return capacity_; } | ||
| 142 | 6254 | Key empty_key() const { return empty_key_; } | |
| 143 | 73018 | Key *keys() const { return keys_; } | |
| 144 | 39 | Value *values() const { return values_; } | |
| 145 | |||
| 146 | // Only needed by compat | ||
| 147 | ✗ | void SetHasher(uint32_t (*hasher)(const Key &key)) { hasher_ = hasher; } | |
| 148 | |||
| 149 | protected: | ||
| 150 | 860167480 | uint32_t ScaleHash(const Key &key) const { | |
| 151 | 860167480 | const double bucket = (static_cast<double>(hasher_(key)) | |
| 152 | 857941116 | * static_cast<double>(capacity_) | |
| 153 | / static_cast<double>(static_cast<uint32_t>(-1))); | ||
| 154 | 857941116 | return static_cast<uint32_t>(bucket) % capacity_; | |
| 155 | } | ||
| 156 | |||
| 157 | 179273 | void AllocMemory() { | |
| 158 | 179273 | keys_ = static_cast<Key *>(smmap(capacity_ * sizeof(Key))); | |
| 159 | 179273 | values_ = static_cast<Value *>(smmap(capacity_ * sizeof(Value))); | |
| 160 |
2/2✓ Branch 0 taken 680471442 times.
✓ Branch 1 taken 91141 times.
|
1351920758 | for (uint32_t i = 0; i < capacity_; ++i) { |
| 161 | 1351741485 | /*keys_[i] =*/new (keys_ + i) Key(); | |
| 162 | } | ||
| 163 |
2/2✓ Branch 0 taken 681401666 times.
✓ Branch 1 taken 94213 times.
|
1353787350 | for (uint32_t i = 0; i < capacity_; ++i) { |
| 164 | 1353601933 | /*values_[i] =*/new (values_ + i) Value(); | |
| 165 | } | ||
| 166 | 185417 | bytes_allocated_ = (sizeof(Key) + sizeof(Value)) * capacity_; | |
| 167 | 185417 | } | |
| 168 | |||
| 169 | 179151 | void DeallocMemory(Key *k, Value *v, uint32_t c) { | |
| 170 |
2/2✓ Branch 0 taken 683431572 times.
✓ Branch 1 taken 91079 times.
|
1357843626 | for (uint32_t i = 0; i < c; ++i) { |
| 171 | 1357664475 | k[i].~Key(); | |
| 172 | } | ||
| 173 |
2/2✓ Branch 0 taken 684435452 times.
✓ Branch 1 taken 91079 times.
|
1359851386 | for (uint32_t i = 0; i < c; ++i) { |
| 174 | 1359672235 | v[i].~Value(); | |
| 175 | } | ||
| 176 |
2/2✓ Branch 0 taken 91071 times.
✓ Branch 1 taken 8 times.
|
179151 | if (k) |
| 177 | 179135 | smunmap(k); | |
| 178 |
2/2✓ Branch 0 taken 91071 times.
✓ Branch 1 taken 8 times.
|
179151 | if (v) |
| 179 | 179135 | smunmap(v); | |
| 180 | 179151 | k = NULL; | |
| 181 | 179151 | v = NULL; | |
| 182 | 179151 | } | |
| 183 | |||
| 184 | // Returns true iff the key is overwritten | ||
| 185 | 689081858 | bool DoInsert(const Key &key, const Value &value, | |
| 186 | const bool count_collisions) { | ||
| 187 | uint32_t bucket; | ||
| 188 | uint32_t collisions; | ||
| 189 |
1/2✓ Branch 1 taken 353835862 times.
✗ Branch 2 not taken.
|
689081858 | const bool overwritten = DoLookup(key, &bucket, &collisions); |
| 190 |
2/2✓ Branch 0 taken 312854195 times.
✓ Branch 1 taken 40981667 times.
|
688093068 | if (count_collisions) { |
| 191 | 607380850 | num_collisions_ += collisions; | |
| 192 | 607380850 | max_collisions_ = std::max(collisions, max_collisions_); | |
| 193 | } | ||
| 194 | 687912964 | keys_[bucket] = key; | |
| 195 |
1/2✓ Branch 1 taken 1041626 times.
✗ Branch 2 not taken.
|
687912964 | values_[bucket] = value; |
| 196 | 687912964 | return overwritten; | |
| 197 | } | ||
| 198 | |||
| 199 | 860785048 | bool DoLookup(const Key &key, uint32_t *bucket, uint32_t *collisions) const { | |
| 200 | 860785048 | *bucket = ScaleHash(key); | |
| 201 | 857618734 | *collisions = 0; | |
| 202 |
3/3✓ Branch 0 taken 866850378 times.
✓ Branch 1 taken 387306393 times.
✓ Branch 2 taken 126322419 times.
|
2162781714 | while (!(keys_[*bucket] == empty_key_)) { |
| 203 |
3/3✓ Branch 0 taken 66355089 times.
✓ Branch 1 taken 838652614 times.
✓ Branch 2 taken 125459802 times.
|
1482023927 | if (keys_[*bucket] == key) |
| 204 | 176860947 | return true; | |
| 205 | 1305162980 | *bucket = (*bucket + 1) % capacity_; | |
| 206 | 1305162980 | (*collisions)++; | |
| 207 | } | ||
| 208 | 680757787 | return false; | |
| 209 | } | ||
| 210 | |||
| 211 | 179275 | void DoClear(const bool reset_capacity) { | |
| 212 |
2/2✓ Branch 0 taken 483 times.
✓ Branch 1 taken 90660 times.
|
179275 | if (reset_capacity) |
| 213 | 828 | static_cast<Derived *>(this)->ResetCapacity(); // No-op if fixed-size | |
| 214 |
2/2✓ Branch 0 taken 683088948 times.
✓ Branch 1 taken 91143 times.
|
1357153042 | for (uint32_t i = 0; i < capacity_; ++i) |
| 215 | 1356973767 | keys_[i] = empty_key_; | |
| 216 | 179275 | size_ = 0; | |
| 217 | 179275 | } | |
| 218 | |||
| 219 | // Methods for resizable version | ||
| 220 | void SetThresholds() { } | ||
| 221 | void Grow() { } | ||
| 222 | void Shrink() { } | ||
| 223 | void ResetCapacity() { } | ||
| 224 | |||
| 225 | // Separate key and value arrays for better locality | ||
| 226 | Key *keys_; | ||
| 227 | Value *values_; | ||
| 228 | uint32_t capacity_; | ||
| 229 | uint32_t initial_capacity_; | ||
| 230 | uint32_t size_; | ||
| 231 | uint32_t (*hasher_)(const Key &key); | ||
| 232 | uint64_t bytes_allocated_; | ||
| 233 | uint64_t num_collisions_; | ||
| 234 | uint32_t max_collisions_; /**< maximum collisions for a single insert */ | ||
| 235 | Key empty_key_; | ||
| 236 | }; | ||
| 237 | |||
| 238 | |||
| 239 | template<class Key, class Value> | ||
| 240 | class SmallHashFixed | ||
| 241 | : public SmallHashBase<Key, Value, SmallHashFixed<Key, Value> > { | ||
| 242 | friend class SmallHashBase<Key, Value, SmallHashFixed<Key, Value> >; | ||
| 243 | |||
| 244 | protected: | ||
| 245 | // No-ops | ||
| 246 | 5911 | void SetThresholds() { } | |
| 247 | 982196 | void Grow() { } | |
| 248 | 5782 | void Shrink() { } | |
| 249 | 2 | void ResetCapacity() { } | |
| 250 | }; | ||
| 251 | |||
| 252 | |||
| 253 | template<class Key, class Value> | ||
| 254 | class SmallHashDynamic | ||
| 255 | : public SmallHashBase<Key, Value, SmallHashDynamic<Key, Value> > { | ||
| 256 | friend class SmallHashBase<Key, Value, SmallHashDynamic<Key, Value> >; | ||
| 257 | |||
| 258 | public: | ||
| 259 | typedef SmallHashBase<Key, Value, SmallHashDynamic<Key, Value> > Base; | ||
| 260 | static const double kThresholdGrow; | ||
| 261 | static const double kThresholdShrink; | ||
| 262 | |||
| 263 | 136476 | SmallHashDynamic() : Base() { | |
| 264 | 136476 | num_migrates_ = 0; | |
| 265 | |||
| 266 | // Properly set by Init | ||
| 267 | 136476 | threshold_grow_ = 0; | |
| 268 | 136476 | threshold_shrink_ = 0; | |
| 269 | 136476 | } | |
| 270 | |||
| 271 | SmallHashDynamic(const SmallHashDynamic<Key, Value> &other) : Base() { | ||
| 272 | num_migrates_ = 0; | ||
| 273 | CopyFrom(other); | ||
| 274 | } | ||
| 275 | |||
| 276 | 376 | SmallHashDynamic<Key, Value> &operator=( | |
| 277 | const SmallHashDynamic<Key, Value> &other) { | ||
| 278 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
|
376 | if (&other == this) |
| 279 | ✗ | return *this; | |
| 280 | |||
| 281 | 376 | CopyFrom(other); | |
| 282 | 376 | return *this; | |
| 283 | } | ||
| 284 | |||
| 285 | 48878786 | uint32_t capacity() const { return Base::capacity_; } | |
| 286 | 375091822 | uint32_t size() const { return Base::size_; } | |
| 287 | uint32_t num_migrates() const { return num_migrates_; } | ||
| 288 | |||
| 289 | protected: | ||
| 290 | 173362 | void SetThresholds() { | |
| 291 | 173362 | threshold_grow_ = static_cast<uint32_t>(static_cast<double>(capacity()) | |
| 292 | 173362 | * kThresholdGrow); | |
| 293 | 173362 | threshold_shrink_ = static_cast<uint32_t>(static_cast<double>(capacity()) | |
| 294 | 173346 | * kThresholdShrink); | |
| 295 | 173346 | } | |
| 296 | |||
| 297 | 607889163 | void Grow() { | |
| 298 |
2/2✓ Branch 1 taken 12524 times.
✓ Branch 2 taken 312326135 times.
|
607889163 | if (size() > threshold_grow_) |
| 299 | 25048 | Migrate(capacity() * 2); | |
| 300 | 607344287 | } | |
| 301 | |||
| 302 | 46359166 | void Shrink() { | |
| 303 |
2/2✓ Branch 1 taken 24196518 times.
✓ Branch 2 taken 22210518 times.
|
46359166 | if (size() < threshold_shrink_) { |
| 304 | 24200162 | const uint32_t target_capacity = capacity() / 2; | |
| 305 |
2/2✓ Branch 0 taken 5504 times.
✓ Branch 1 taken 24189306 times.
|
24198454 | if (target_capacity >= Base::initial_capacity_) |
| 306 | 5504 | Migrate(target_capacity); | |
| 307 | } | ||
| 308 | 46408972 | } | |
| 309 | |||
| 310 | 826 | void ResetCapacity() { | |
| 311 | 826 | Base::DeallocMemory(Base::keys_, Base::values_, Base::capacity_); | |
| 312 | 826 | Base::capacity_ = Base::initial_capacity_; | |
| 313 | 826 | Base::AllocMemory(); | |
| 314 | 826 | SetThresholds(); | |
| 315 | 826 | } | |
| 316 | |||
| 317 | private: | ||
| 318 | // Returns a random permutation of indices [0..N-1] that is allocated | ||
| 319 | // by smmap (Knuth's shuffle algorithm) | ||
| 320 | 11392 | uint32_t *ShuffleIndices(const uint32_t N) { | |
| 321 | 11392 | uint32_t *shuffled = static_cast<uint32_t *>(smmap(N * sizeof(uint32_t))); | |
| 322 | // Init with identity | ||
| 323 |
2/2✓ Branch 0 taken 89598040 times.
✓ Branch 1 taken 5696 times.
|
179207472 | for (unsigned i = 0; i < N; ++i) |
| 324 | 179196080 | shuffled[i] = i; | |
| 325 | // Shuffle (no shuffling for the last element) | ||
| 326 |
1/2✓ Branch 0 taken 84950496 times.
✗ Branch 1 not taken.
|
169767088 | for (unsigned i = 0; i < N - 1; ++i) { |
| 327 | 169900992 | const uint32_t swap_idx = i + g_prng.Next(N - i); | |
| 328 | 169755696 | const uint32_t tmp = shuffled[i]; | |
| 329 | 169755696 | shuffled[i] = shuffled[swap_idx]; | |
| 330 | 169755696 | shuffled[swap_idx] = tmp; | |
| 331 | } | ||
| 332 | 376 | return shuffled; | |
| 333 | } | ||
| 334 | |||
| 335 | 36056 | void Migrate(const uint32_t new_capacity) { | |
| 336 | 36056 | Key *old_keys = Base::keys_; | |
| 337 | 36056 | Value *old_values = Base::values_; | |
| 338 | 36056 | const uint32_t old_capacity = capacity(); | |
| 339 | 36040 | const uint32_t old_size = size(); | |
| 340 | |||
| 341 | 36056 | Base::capacity_ = new_capacity; | |
| 342 | 36056 | SetThresholds(); | |
| 343 | 36056 | Base::AllocMemory(); | |
| 344 | 36072 | Base::DoClear(false); | |
| 345 |
2/2✓ Branch 0 taken 5504 times.
✓ Branch 1 taken 12524 times.
|
36056 | if (new_capacity < old_capacity) { |
| 346 | 11008 | uint32_t *shuffled_indices = ShuffleIndices(old_capacity); | |
| 347 |
1/2✓ Branch 0 taken 77705224 times.
✗ Branch 1 not taken.
|
154888480 | for (uint32_t i = 0; i < old_capacity; ++i) { |
| 348 |
2/3✓ Branch 0 taken 19496504 times.
✓ Branch 1 taken 58208720 times.
✗ Branch 2 not taken.
|
155410448 | if (old_keys[shuffled_indices[i]] != Base::empty_key_) { |
| 349 | 38993008 | Base::Insert(old_keys[shuffled_indices[i]], | |
| 350 | 38993008 | old_values[shuffled_indices[i]]); | |
| 351 | } | ||
| 352 | } | ||
| 353 | ✗ | smunmap(shuffled_indices); | |
| 354 | } else { | ||
| 355 |
2/2✓ Branch 0 taken 175073488 times.
✓ Branch 1 taken 6756 times.
|
350160488 | for (uint32_t i = 0; i < old_capacity; ++i) { |
| 356 |
3/3✓ Branch 0 taken 83599222 times.
✓ Branch 1 taken 75556867 times.
✓ Branch 2 taken 15917399 times.
|
350146976 | if (old_keys[i] != Base::empty_key_) |
| 357 | 262710712 | Base::Insert(old_keys[i], old_values[i]); | |
| 358 | } | ||
| 359 | } | ||
| 360 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 18036 times.
|
24520 | assert(size() == old_size); |
| 361 | |||
| 362 | 36072 | Base::DeallocMemory(old_keys, old_values, old_capacity); | |
| 363 | 36072 | num_migrates_++; | |
| 364 | 36072 | } | |
| 365 | |||
| 366 | 376 | void CopyFrom(const SmallHashDynamic<Key, Value> &other) { | |
| 367 | 376 | uint32_t *shuffled_indices = ShuffleIndices(other.capacity_); | |
| 368 |
2/2✓ Branch 0 taken 11013912 times.
✓ Branch 1 taken 192 times.
|
11018152 | for (uint32_t i = 0; i < other.capacity_; ++i) { |
| 369 |
2/3✗ Branch 0 not taken.
✓ Branch 1 taken 8001932 times.
✓ Branch 2 taken 3011980 times.
|
11017776 | if (other.keys_[shuffled_indices[i]] != other.empty_key_) { |
| 370 | 8000000 | this->Insert(other.keys_[shuffled_indices[i]], | |
| 371 | 8000000 | other.values_[shuffled_indices[i]]); | |
| 372 | } | ||
| 373 | } | ||
| 374 | 376 | smunmap(shuffled_indices); | |
| 375 | 376 | } | |
| 376 | |||
| 377 | uint32_t num_migrates_; | ||
| 378 | uint32_t threshold_grow_; | ||
| 379 | uint32_t threshold_shrink_; | ||
| 380 | static Prng g_prng; | ||
| 381 | }; | ||
| 382 | |||
| 383 | |||
| 384 | /** | ||
| 385 | * Distributes the key-value pairs over $n$ dynamic hash maps with individual | ||
| 386 | * mutexes. Hence low mutex contention, and benefits from multiple processors. | ||
| 387 | */ | ||
| 388 | template<class Key, class Value> | ||
| 389 | class MultiHash { | ||
| 390 | public: | ||
| 391 | 80 | MultiHash() { | |
| 392 | 80 | num_hashmaps_ = 0; | |
| 393 | 80 | hashmaps_ = NULL; | |
| 394 | 80 | locks_ = NULL; | |
| 395 | 80 | } | |
| 396 | |||
| 397 | 80 | void Init(const uint8_t num_hashmaps, const Key &empty_key, | |
| 398 | uint32_t (*hasher)(const Key &key)) { | ||
| 399 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | assert(num_hashmaps > 0); |
| 400 | 80 | const uint8_t N = num_hashmaps; | |
| 401 | 80 | num_hashmaps_ = N; | |
| 402 |
3/4✓ Branch 2 taken 3360 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3360 times.
✓ Branch 5 taken 80 times.
|
3440 | hashmaps_ = new SmallHashDynamic<Key, Value>[N](); |
| 403 | 80 | locks_ = static_cast<pthread_mutex_t *>( | |
| 404 | 80 | smalloc(N * sizeof(pthread_mutex_t))); | |
| 405 |
2/2✓ Branch 0 taken 3360 times.
✓ Branch 1 taken 80 times.
|
3440 | for (uint8_t i = 0; i < N; ++i) { |
| 406 | 3360 | int retval = pthread_mutex_init(&locks_[i], NULL); | |
| 407 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3360 times.
|
3360 | assert(retval == 0); |
| 408 | 3360 | hashmaps_[i].Init(128, empty_key, hasher); | |
| 409 | } | ||
| 410 | 80 | } | |
| 411 | |||
| 412 | 80 | ~MultiHash() { | |
| 413 |
2/2✓ Branch 0 taken 3360 times.
✓ Branch 1 taken 80 times.
|
3440 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { |
| 414 | 3360 | pthread_mutex_destroy(&locks_[i]); | |
| 415 | } | ||
| 416 | 80 | free(locks_); | |
| 417 |
3/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3360 times.
✓ Branch 3 taken 80 times.
|
3440 | delete[] hashmaps_; |
| 418 | 80 | } | |
| 419 | |||
| 420 | 8000000 | bool Lookup(const Key &key, Value *value) { | |
| 421 | 8000000 | uint8_t target = SelectHashmap(key); | |
| 422 | 8000000 | Lock(target); | |
| 423 | 8000000 | const bool result = hashmaps_[target].Lookup(key, value); | |
| 424 | 8000000 | Unlock(target); | |
| 425 | 8000000 | return result; | |
| 426 | } | ||
| 427 | |||
| 428 | 29931200 | void Insert(const Key &key, const Value &value) { | |
| 429 | 29931200 | uint8_t target = SelectHashmap(key); | |
| 430 | 28917216 | Lock(target); | |
| 431 | 30022592 | hashmaps_[target].Insert(key, value); | |
| 432 | 28478608 | Unlock(target); | |
| 433 | 30525896 | } | |
| 434 | |||
| 435 | 14431632 | void Erase(const Key &key) { | |
| 436 | 14431632 | uint8_t target = SelectHashmap(key); | |
| 437 | 13722120 | Lock(target); | |
| 438 | 14608704 | hashmaps_[target].Erase(key); | |
| 439 | 13238840 | Unlock(target); | |
| 440 | 14910272 | } | |
| 441 | |||
| 442 | 8 | void Clear() { | |
| 443 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 8 times.
|
344 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 444 | 336 | Lock(i); | |
| 445 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 8 times.
|
344 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 446 | 336 | hashmaps_[i].Clear(); | |
| 447 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 8 times.
|
344 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 448 | 336 | Unlock(i); | |
| 449 | 8 | } | |
| 450 | |||
| 451 | 80 | uint8_t num_hashmaps() const { return num_hashmaps_; } | |
| 452 | |||
| 453 | 64 | void GetSizes(uint32_t *sizes) { | |
| 454 |
2/2✓ Branch 0 taken 2688 times.
✓ Branch 1 taken 64 times.
|
2752 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { |
| 455 | 2688 | Lock(i); | |
| 456 | 2688 | sizes[i] = hashmaps_[i].size(); | |
| 457 | 2688 | Unlock(i); | |
| 458 | } | ||
| 459 | 64 | } | |
| 460 | |||
| 461 | void GetCollisionStats(uint64_t *num_collisions, uint32_t *max_collisions) { | ||
| 462 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { | ||
| 463 | Lock(i); | ||
| 464 | hashmaps_[i].GetCollisionStats(&num_collisions[i], &max_collisions[i]); | ||
| 465 | Unlock(i); | ||
| 466 | } | ||
| 467 | } | ||
| 468 | |||
| 469 | private: | ||
| 470 | 52060032 | inline uint8_t SelectHashmap(const Key &key) { | |
| 471 | 52060032 | uint32_t hash = MurmurHash2(&key, sizeof(key), 0x37); | |
| 472 | 51012208 | double bucket = static_cast<double>(hash) | |
| 473 | 51012208 | * static_cast<double>(num_hashmaps_) | |
| 474 | / static_cast<double>(static_cast<uint32_t>(-1)); | ||
| 475 | 51012208 | return static_cast<uint32_t>(bucket) % num_hashmaps_; | |
| 476 | } | ||
| 477 | |||
| 478 | 50506216 | inline void Lock(const uint8_t target) { | |
| 479 | 50506216 | int retval = pthread_mutex_lock(&locks_[target]); | |
| 480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54812272 times.
|
54812272 | assert(retval == 0); |
| 481 | 54812272 | } | |
| 482 | |||
| 483 | 49765464 | inline void Unlock(const uint8_t target) { | |
| 484 | 49765464 | int retval = pthread_mutex_unlock(&locks_[target]); | |
| 485 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 55166488 times.
|
55166488 | assert(retval == 0); |
| 486 | 55166488 | } | |
| 487 | |||
| 488 | uint8_t num_hashmaps_; | ||
| 489 | SmallHashDynamic<Key, Value> *hashmaps_; | ||
| 490 | pthread_mutex_t *locks_; | ||
| 491 | }; | ||
| 492 | |||
| 493 | |||
| 494 | // initialize the static fields | ||
| 495 | template<class Key, class Value> | ||
| 496 | Prng SmallHashDynamic<Key, Value>::g_prng; | ||
| 497 | |||
| 498 | template<class Key, class Value, class Derived> | ||
| 499 | const double SmallHashBase<Key, Value, Derived>::kLoadFactor = 0.75; | ||
| 500 | |||
| 501 | template<class Key, class Value> | ||
| 502 | const double SmallHashDynamic<Key, Value>::kThresholdGrow = 0.75; | ||
| 503 | |||
| 504 | template<class Key, class Value> | ||
| 505 | const double SmallHashDynamic<Key, Value>::kThresholdShrink = 0.25; | ||
| 506 | |||
| 507 | #endif // CVMFS_SMALLHASH_H_ | ||
| 508 |