| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/smallhash.h |
| Date: | 2026-10-25 02:40:14 |
| 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 | 191004 | SmallHashBase() { | |
| 38 | 191004 | keys_ = NULL; | |
| 39 | 191004 | values_ = NULL; | |
| 40 | 191004 | hasher_ = NULL; | |
| 41 | 191004 | bytes_allocated_ = 0; | |
| 42 | 191004 | num_collisions_ = 0; | |
| 43 | 191004 | max_collisions_ = 0; | |
| 44 | |||
| 45 | // Properly initialized by Init() | ||
| 46 | 191004 | capacity_ = 0; | |
| 47 | 191004 | initial_capacity_ = 0; | |
| 48 | 191004 | size_ = 0; | |
| 49 | 191004 | } | |
| 50 | |||
| 51 | 190866 | ~SmallHashBase() { DeallocMemory(keys_, values_, capacity_); } | |
| 52 | |||
| 53 | 190986 | void Init(uint32_t expected_size, Key empty, | |
| 54 | uint32_t (*hasher)(const Key &key)) { | ||
| 55 | 190986 | hasher_ = hasher; | |
| 56 | 190986 | empty_key_ = empty; | |
| 57 | 190986 | capacity_ = static_cast<uint32_t>(static_cast<double>(expected_size) | |
| 58 | 190986 | / kLoadFactor); | |
| 59 | 190986 | initial_capacity_ = capacity_; | |
| 60 | 190986 | static_cast<Derived *>(this)->SetThresholds(); // No-op for fixed size | |
| 61 | 190986 | AllocMemory(); | |
| 62 | 190986 | this->DoClear(false); | |
| 63 | 190986 | } | |
| 64 | |||
| 65 | 56675930 | bool Lookup(const Key &key, Value *value) const { | |
| 66 | uint32_t bucket; | ||
| 67 | uint32_t collisions; | ||
| 68 |
1/2✓ Branch 1 taken 56652656 times.
✗ Branch 2 not taken.
|
56675930 | const bool found = DoLookup(key, &bucket, &collisions); |
| 69 |
2/2✓ Branch 0 taken 39973763 times.
✓ Branch 1 taken 16678893 times.
|
56693387 | if (found) |
| 70 |
1/2✓ Branch 1 taken 70909 times.
✗ Branch 2 not taken.
|
39987597 | *value = values_[bucket]; |
| 71 | 56693387 | 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 | 492 | bool LookupEx(Key *key, Value *value) const { | |
| 82 | 492 | uint32_t bucket = ScaleHash(*key); | |
| 83 |
2/2✓ Branch 1 taken 164 times.
✓ Branch 2 taken 82 times.
|
492 | while (!(keys_[bucket] == empty_key_)) { |
| 84 |
1/2✓ Branch 1 taken 164 times.
✗ Branch 2 not taken.
|
328 | if (keys_[bucket] == *key) { |
| 85 | 328 | *key = keys_[bucket]; | |
| 86 | 328 | *value = values_[bucket]; | |
| 87 | 328 | return true; | |
| 88 | } | ||
| 89 | ✗ | bucket = (bucket + 1) % capacity_; | |
| 90 | } | ||
| 91 | 164 | return false; | |
| 92 | } | ||
| 93 | |||
| 94 | 9020784 | bool Contains(const Key &key) const { | |
| 95 | uint32_t bucket; | ||
| 96 | uint32_t collisions; | ||
| 97 |
1/2✓ Branch 1 taken 9019999 times.
✗ Branch 2 not taken.
|
9020784 | const bool found = DoLookup(key, &bucket, &collisions); |
| 98 | 9020784 | return found; | |
| 99 | } | ||
| 100 | |||
| 101 | 571626409 | void Insert(const Key &key, const Value &value) { | |
| 102 | 571626409 | static_cast<Derived *>(this)->Grow(); // No-op if fixed-size | |
| 103 | 570443674 | const bool overwritten = DoInsert(key, value, true); | |
| 104 | 570795886 | size_ += !overwritten; // size + 1 if the key was not yet in the map | |
| 105 | 570795886 | } | |
| 106 | |||
| 107 | 44620473 | bool Erase(const Key &key) { | |
| 108 | uint32_t bucket; | ||
| 109 | uint32_t collisions; | ||
| 110 |
1/2✓ Branch 1 taken 43588337 times.
✗ Branch 2 not taken.
|
44620473 | const bool found = DoLookup(key, &bucket, &collisions); |
| 111 |
1/2✓ Branch 0 taken 43592318 times.
✗ Branch 1 not taken.
|
43592460 | if (found) { |
| 112 | 43596129 | keys_[bucket] = empty_key_; | |
| 113 | 43596129 | size_--; | |
| 114 | 43596129 | bucket = (bucket + 1) % capacity_; | |
| 115 |
3/3✓ Branch 0 taken 76360293 times.
✓ Branch 1 taken 40387848 times.
✓ Branch 2 taken 3319022 times.
|
120070974 | while (!(keys_[bucket] == empty_key_)) { |
| 116 | 76846788 | Key rehash = keys_[bucket]; | |
| 117 | 76846788 | keys_[bucket] = empty_key_; | |
| 118 |
1/2✓ Branch 1 taken 76474845 times.
✗ Branch 2 not taken.
|
76846788 | DoInsert(rehash, values_[bucket], false); |
| 119 | 76474845 | bucket = (bucket + 1) % capacity_; | |
| 120 | } | ||
| 121 |
1/2✓ Branch 1 taken 43788461 times.
✗ Branch 2 not taken.
|
43224186 | static_cast<Derived *>(this)->Shrink(); // No-op if fixed-size |
| 122 | } | ||
| 123 | 43823565 | return found; | |
| 124 | } | ||
| 125 | |||
| 126 | 1063 | void Clear() { DoClear(true); } | |
| 127 | |||
| 128 | 7042 | uint64_t bytes_allocated() const { return bytes_allocated_; } | |
| 129 | 2304 | static double GetEntrySize() { | |
| 130 | 4608 | const double unit = sizeof(Key) + sizeof(Value); | |
| 131 | 4608 | 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 | 56416 | Key empty_key() const { return empty_key_; } | |
| 143 | 207698 | Key *keys() const { return keys_; } | |
| 144 | 671 | 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 | 847279076 | uint32_t ScaleHash(const Key &key) const { | |
| 151 | 847279076 | const double bucket = (static_cast<double>(hasher_(key)) | |
| 152 | 844201358 | * static_cast<double>(capacity_) | |
| 153 | / static_cast<double>(static_cast<uint32_t>(-1))); | ||
| 154 | 844201358 | return static_cast<uint32_t>(bucket) % capacity_; | |
| 155 | } | ||
| 156 | |||
| 157 | 232570 | void AllocMemory() { | |
| 158 | 232570 | keys_ = static_cast<Key *>(smmap(capacity_ * sizeof(Key))); | |
| 159 | 232624 | values_ = static_cast<Value *>(smmap(capacity_ * sizeof(Value))); | |
| 160 |
2/2✓ Branch 0 taken 567625158 times.
✓ Branch 1 taken 118010 times.
|
1124872894 | for (uint32_t i = 0; i < capacity_; ++i) { |
| 161 | 1124640270 | /*keys_[i] =*/new (keys_ + i) Key(); | |
| 162 | } | ||
| 163 |
2/2✓ Branch 0 taken 565654005 times.
✓ Branch 1 taken 111566 times.
|
1120917700 | for (uint32_t i = 0; i < capacity_; ++i) { |
| 164 | 1120697964 | /*values_[i] =*/new (values_ + i) Value(); | |
| 165 | } | ||
| 166 | 219736 | bytes_allocated_ = (sizeof(Key) + sizeof(Value)) * capacity_; | |
| 167 | 219736 | } | |
| 168 | |||
| 169 | 232504 | void DeallocMemory(Key *k, Value *v, uint32_t c) { | |
| 170 |
2/2✓ Branch 0 taken 568427526 times.
✓ Branch 1 taken 117949 times.
|
1126480240 | for (uint32_t i = 0; i < c; ++i) { |
| 171 | 1126247736 | k[i].~Key(); | |
| 172 | } | ||
| 173 |
2/2✓ Branch 0 taken 568635615 times.
✓ Branch 1 taken 117949 times.
|
1126896418 | for (uint32_t i = 0; i < c; ++i) { |
| 174 | 1126663914 | v[i].~Value(); | |
| 175 | } | ||
| 176 |
2/2✓ Branch 0 taken 117940 times.
✓ Branch 1 taken 9 times.
|
232504 | if (k) |
| 177 | 232486 | smunmap(k); | |
| 178 |
2/2✓ Branch 0 taken 117940 times.
✓ Branch 1 taken 9 times.
|
232504 | if (v) |
| 179 | 232486 | smunmap(v); | |
| 180 | 232504 | k = NULL; | |
| 181 | 232504 | v = NULL; | |
| 182 | 232504 | } | |
| 183 | |||
| 184 | // Returns true iff the key is overwritten | ||
| 185 | 690432325 | 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 376902125 times.
✗ Branch 2 not taken.
|
690432325 | const bool overwritten = DoLookup(key, &bucket, &collisions); |
| 190 |
2/2✓ Branch 0 taken 300628559 times.
✓ Branch 1 taken 76273566 times.
|
687700549 | if (count_collisions) { |
| 191 | 569430868 | num_collisions_ += collisions; | |
| 192 | 569430868 | max_collisions_ = std::max(collisions, max_collisions_); | |
| 193 | } | ||
| 194 | 688916803 | keys_[bucket] = key; | |
| 195 |
1/2✓ Branch 1 taken 34371518 times.
✗ Branch 2 not taken.
|
688916803 | values_[bucket] = value; |
| 196 | 688916803 | return overwritten; | |
| 197 | } | ||
| 198 | |||
| 199 | 848130944 | bool DoLookup(const Key &key, uint32_t *bucket, uint32_t *collisions) const { | |
| 200 | 848130944 | *bucket = ScaleHash(key); | |
| 201 | 843707645 | *collisions = 0; | |
| 202 |
3/3✓ Branch 0 taken 18175770310 times.
✓ Branch 1 taken 368264264 times.
✓ Branch 2 taken 75891819 times.
|
19331254950 | while (!(keys_[*bucket] == empty_key_)) { |
| 203 |
3/3✓ Branch 0 taken 94840936 times.
✓ Branch 1 taken 18093895185 times.
✓ Branch 2 taken 56537978 times.
|
18649493363 | if (keys_[*bucket] == key) |
| 204 | 161946058 | return true; | |
| 205 | 18487547305 | *bucket = (*bucket + 1) % capacity_; | |
| 206 | 18487547305 | (*collisions)++; | |
| 207 | } | ||
| 208 | 681761587 | return false; | |
| 209 | } | ||
| 210 | |||
| 211 | 232690 | void DoClear(const bool reset_capacity) { | |
| 212 |
2/2✓ Branch 0 taken 1063 times.
✓ Branch 1 taken 117013 times.
|
232690 | if (reset_capacity) |
| 213 | 1499 | static_cast<Derived *>(this)->ResetCapacity(); // No-op if fixed-size | |
| 214 |
2/2✓ Branch 0 taken 566952876 times.
✓ Branch 1 taken 118076 times.
|
1123438306 | for (uint32_t i = 0; i < capacity_; ++i) |
| 215 | 1123205616 | keys_[i] = empty_key_; | |
| 216 | 232690 | size_ = 0; | |
| 217 | 232690 | } | |
| 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 | 6976 | void SetThresholds() { } | |
| 247 | 338167 | void Grow() { } | |
| 248 | 34962 | void Shrink() { } | |
| 249 | 66 | 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 | 184028 | SmallHashDynamic() : Base() { | |
| 264 | 184028 | num_migrates_ = 0; | |
| 265 | |||
| 266 | // Properly set by Init | ||
| 267 | 184028 | threshold_grow_ = 0; | |
| 268 | 184028 | threshold_shrink_ = 0; | |
| 269 | 184028 | } | |
| 270 | |||
| 271 | SmallHashDynamic(const SmallHashDynamic<Key, Value> &other) : Base() { | ||
| 272 | num_migrates_ = 0; | ||
| 273 | CopyFrom(other); | ||
| 274 | } | ||
| 275 | |||
| 276 | 297 | SmallHashDynamic<Key, Value> &operator=( | |
| 277 | const SmallHashDynamic<Key, Value> &other) { | ||
| 278 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 153 times.
|
297 | if (&other == this) |
| 279 | ✗ | return *this; | |
| 280 | |||
| 281 | 297 | CopyFrom(other); | |
| 282 | 297 | return *this; | |
| 283 | } | ||
| 284 | |||
| 285 | 40502627 | uint32_t capacity() const { return Base::capacity_; } | |
| 286 | 347872486 | uint32_t size() const { return Base::size_; } | |
| 287 | uint32_t num_migrates() const { return num_migrates_; } | ||
| 288 | |||
| 289 | protected: | ||
| 290 | 225612 | void SetThresholds() { | |
| 291 | 225612 | threshold_grow_ = static_cast<uint32_t>(static_cast<double>(capacity()) | |
| 292 | 225612 | * kThresholdGrow); | |
| 293 | 225612 | threshold_shrink_ = static_cast<uint32_t>(static_cast<double>(capacity()) | |
| 294 | 225594 | * kThresholdShrink); | |
| 295 | 225594 | } | |
| 296 | |||
| 297 | 570456164 | void Grow() { | |
| 298 |
2/2✓ Branch 1 taken 13881 times.
✓ Branch 2 taken 300752260 times.
|
570456164 | if (size() > threshold_grow_) |
| 299 | 27762 | Migrate(capacity() * 2); | |
| 300 | 570078512 | } | |
| 301 | |||
| 302 | 43727157 | void Shrink() { | |
| 303 |
2/2✓ Branch 1 taken 19865543 times.
✓ Branch 2 taken 23895504 times.
|
43727157 | if (size() < threshold_shrink_) { |
| 304 | 19869354 | const uint32_t target_capacity = capacity() / 2; | |
| 305 |
2/2✓ Branch 0 taken 6192 times.
✓ Branch 1 taken 19854293 times.
|
19864296 | if (target_capacity >= Base::initial_capacity_) |
| 306 | 6192 | Migrate(target_capacity); | |
| 307 | } | ||
| 308 | 43759800 | } | |
| 309 | |||
| 310 | 1433 | void ResetCapacity() { | |
| 311 | 1433 | Base::DeallocMemory(Base::keys_, Base::values_, Base::capacity_); | |
| 312 | 1433 | Base::capacity_ = Base::initial_capacity_; | |
| 313 | 1433 | Base::AllocMemory(); | |
| 314 | 1433 | SetThresholds(); | |
| 315 | 1433 | } | |
| 316 | |||
| 317 | private: | ||
| 318 | // Returns a random permutation of indices [0..N-1] that is allocated | ||
| 319 | // by smmap (Knuth's shuffle algorithm) | ||
| 320 | 12690 | uint32_t *ShuffleIndices(const uint32_t N) { | |
| 321 | 12690 | uint32_t *shuffled = static_cast<uint32_t *>(smmap(N * sizeof(uint32_t))); | |
| 322 | // Init with identity | ||
| 323 |
2/2✓ Branch 0 taken 101419911 times.
✓ Branch 1 taken 6345 times.
|
202852512 | for (unsigned i = 0; i < N; ++i) |
| 324 | 202839822 | shuffled[i] = i; | |
| 325 | // Shuffle (no shuffling for the last element) | ||
| 326 |
1/2✓ Branch 0 taken 96604047 times.
✗ Branch 1 not taken.
|
193119300 | for (unsigned i = 0; i < N - 1; ++i) { |
| 327 | 193208094 | const uint32_t swap_idx = i + g_prng.Next(N - i); | |
| 328 | 193106610 | const uint32_t tmp = shuffled[i]; | |
| 329 | 193106610 | shuffled[i] = shuffled[swap_idx]; | |
| 330 | 193106610 | shuffled[swap_idx] = tmp; | |
| 331 | } | ||
| 332 | 297 | return shuffled; | |
| 333 | } | ||
| 334 | |||
| 335 | 40146 | void Migrate(const uint32_t new_capacity) { | |
| 336 | 40146 | Key *old_keys = Base::keys_; | |
| 337 | 40146 | Value *old_values = Base::values_; | |
| 338 | 40146 | const uint32_t old_capacity = capacity(); | |
| 339 | 40128 | const uint32_t old_size = size(); | |
| 340 | |||
| 341 | 40128 | Base::capacity_ = new_capacity; | |
| 342 | 40128 | SetThresholds(); | |
| 343 | 40110 | Base::AllocMemory(); | |
| 344 | 40164 | Base::DoClear(false); | |
| 345 |
2/2✓ Branch 0 taken 6192 times.
✓ Branch 1 taken 13890 times.
|
40164 | if (new_capacity < old_capacity) { |
| 346 | 12384 | uint32_t *shuffled_indices = ShuffleIndices(old_capacity); | |
| 347 |
1/2✓ Branch 0 taken 87259014 times.
✗ Branch 1 not taken.
|
174233340 | for (uint32_t i = 0; i < old_capacity; ++i) { |
| 348 |
2/3✓ Branch 0 taken 22008933 times.
✓ Branch 1 taken 65250081 times.
✗ Branch 2 not taken.
|
174518028 | if (old_keys[shuffled_indices[i]] != Base::empty_key_) { |
| 349 | 44017866 | Base::Insert(old_keys[shuffled_indices[i]], | |
| 350 | 44017866 | old_values[shuffled_indices[i]]); | |
| 351 | } | ||
| 352 | } | ||
| 353 | ✗ | smunmap(shuffled_indices); | |
| 354 | } else { | ||
| 355 |
2/2✓ Branch 0 taken 169183431 times.
✓ Branch 1 taken 7572 times.
|
338382006 | for (uint32_t i = 0; i < old_capacity; ++i) { |
| 356 |
3/3✓ Branch 0 taken 94274757 times.
✓ Branch 1 taken 64022057 times.
✓ Branch 2 taken 10886617 times.
|
338366862 | if (old_keys[i] != Base::empty_key_) |
| 357 | 253876174 | Base::Insert(old_keys[i], old_values[i]); | |
| 358 | } | ||
| 359 | } | ||
| 360 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 20082 times.
|
27528 | assert(size() == old_size); |
| 361 | |||
| 362 | 40164 | Base::DeallocMemory(old_keys, old_values, old_capacity); | |
| 363 | 40164 | num_migrates_++; | |
| 364 | 40164 | } | |
| 365 | |||
| 366 | 297 | void CopyFrom(const SmallHashDynamic<Key, Value> &other) { | |
| 367 | 297 | uint32_t *shuffled_indices = ShuffleIndices(other.capacity_); | |
| 368 |
2/2✓ Branch 0 taken 12389328 times.
✓ Branch 1 taken 153 times.
|
12392649 | for (uint32_t i = 0; i < other.capacity_; ++i) { |
| 369 |
2/3✗ Branch 0 not taken.
✓ Branch 1 taken 9001512 times.
✓ Branch 2 taken 3387816 times.
|
12392352 | if (other.keys_[shuffled_indices[i]] != other.empty_key_) { |
| 370 | 9000000 | this->Insert(other.keys_[shuffled_indices[i]], | |
| 371 | 9000000 | other.values_[shuffled_indices[i]]); | |
| 372 | } | ||
| 373 | } | ||
| 374 | 297 | smunmap(shuffled_indices); | |
| 375 | 297 | } | |
| 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 | 90 | MultiHash() { | |
| 392 | 90 | num_hashmaps_ = 0; | |
| 393 | 90 | hashmaps_ = NULL; | |
| 394 | 90 | locks_ = NULL; | |
| 395 | 90 | } | |
| 396 | |||
| 397 | 90 | 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 90 times.
|
90 | assert(num_hashmaps > 0); |
| 400 | 90 | const uint8_t N = num_hashmaps; | |
| 401 | 90 | num_hashmaps_ = N; | |
| 402 |
3/4✓ Branch 2 taken 3780 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3780 times.
✓ Branch 5 taken 90 times.
|
3870 | hashmaps_ = new SmallHashDynamic<Key, Value>[N](); |
| 403 | 90 | locks_ = static_cast<pthread_mutex_t *>( | |
| 404 | 90 | smalloc(N * sizeof(pthread_mutex_t))); | |
| 405 |
2/2✓ Branch 0 taken 3780 times.
✓ Branch 1 taken 90 times.
|
3870 | for (uint8_t i = 0; i < N; ++i) { |
| 406 | 3780 | int retval = pthread_mutex_init(&locks_[i], NULL); | |
| 407 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3780 times.
|
3780 | assert(retval == 0); |
| 408 | 3780 | hashmaps_[i].Init(128, empty_key, hasher); | |
| 409 | } | ||
| 410 | 90 | } | |
| 411 | |||
| 412 | 90 | ~MultiHash() { | |
| 413 |
2/2✓ Branch 0 taken 3780 times.
✓ Branch 1 taken 90 times.
|
3870 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { |
| 414 | 3780 | pthread_mutex_destroy(&locks_[i]); | |
| 415 | } | ||
| 416 | 90 | free(locks_); | |
| 417 |
3/4✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3780 times.
✓ Branch 3 taken 90 times.
|
3870 | delete[] hashmaps_; |
| 418 | 90 | } | |
| 419 | |||
| 420 | 9000000 | bool Lookup(const Key &key, Value *value) { | |
| 421 | 9000000 | uint8_t target = SelectHashmap(key); | |
| 422 | 9000000 | Lock(target); | |
| 423 | 9000000 | const bool result = hashmaps_[target].Lookup(key, value); | |
| 424 | 9000000 | Unlock(target); | |
| 425 | 9000000 | return result; | |
| 426 | } | ||
| 427 | |||
| 428 | 33855012 | void Insert(const Key &key, const Value &value) { | |
| 429 | 33855012 | uint8_t target = SelectHashmap(key); | |
| 430 | 33346908 | Lock(target); | |
| 431 | 33993198 | hashmaps_[target].Insert(key, value); | |
| 432 | 32958342 | Unlock(target); | |
| 433 | 34472124 | } | |
| 434 | |||
| 435 | 16375023 | void Erase(const Key &key) { | |
| 436 | 16375023 | uint8_t target = SelectHashmap(key); | |
| 437 | 15864480 | Lock(target); | |
| 438 | 16491645 | hashmaps_[target].Erase(key); | |
| 439 | 15004656 | Unlock(target); | |
| 440 | 16789419 | } | |
| 441 | |||
| 442 | 9 | void Clear() { | |
| 443 |
2/2✓ Branch 0 taken 378 times.
✓ Branch 1 taken 9 times.
|
387 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 444 | 378 | Lock(i); | |
| 445 |
2/2✓ Branch 0 taken 378 times.
✓ Branch 1 taken 9 times.
|
387 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 446 | 378 | hashmaps_[i].Clear(); | |
| 447 |
2/2✓ Branch 0 taken 378 times.
✓ Branch 1 taken 9 times.
|
387 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 448 | 378 | Unlock(i); | |
| 449 | 9 | } | |
| 450 | |||
| 451 | 90 | uint8_t num_hashmaps() const { return num_hashmaps_; } | |
| 452 | |||
| 453 | 72 | void GetSizes(uint32_t *sizes) { | |
| 454 |
2/2✓ Branch 0 taken 3024 times.
✓ Branch 1 taken 72 times.
|
3096 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { |
| 455 | 3024 | Lock(i); | |
| 456 | 3024 | sizes[i] = hashmaps_[i].size(); | |
| 457 | 3024 | Unlock(i); | |
| 458 | } | ||
| 459 | 72 | } | |
| 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 | 60759711 | inline uint8_t SelectHashmap(const Key &key) { | |
| 471 | 60759711 | uint32_t hash = MurmurHash2(&key, sizeof(key), 0x37); | |
| 472 | 58474629 | double bucket = static_cast<double>(hash) | |
| 473 | 58474629 | * static_cast<double>(num_hashmaps_) | |
| 474 | / static_cast<double>(static_cast<uint32_t>(-1)); | ||
| 475 | 58474629 | return static_cast<uint32_t>(bucket) % num_hashmaps_; | |
| 476 | } | ||
| 477 | |||
| 478 | 57824010 | inline void Lock(const uint8_t target) { | |
| 479 | 57824010 | int retval = pthread_mutex_lock(&locks_[target]); | |
| 480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61912503 times.
|
61912503 | assert(retval == 0); |
| 481 | 61912503 | } | |
| 482 | |||
| 483 | 56974977 | inline void Unlock(const uint8_t target) { | |
| 484 | 56974977 | int retval = pthread_mutex_unlock(&locks_[target]); | |
| 485 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62223318 times.
|
62223318 | assert(retval == 0); |
| 486 | 62223318 | } | |
| 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 |