| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/smallhash.h |
| Date: | 2026-08-09 02:40:25 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 259 | 263 | 98.5% |
| Branches: | 86 | 110 | 78.2% |
| 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 "duplex_testing.h" | ||
| 9 | #include <inttypes.h> | ||
| 10 | #include <pthread.h> | ||
| 11 | #include <stdint.h> | ||
| 12 | |||
| 13 | #include <algorithm> | ||
| 14 | #include <cassert> | ||
| 15 | #include <cstdlib> | ||
| 16 | #include <new> | ||
| 17 | |||
| 18 | #include "util/atomic.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 | 193875 | SmallHashBase() { | |
| 38 | 193875 | keys_ = NULL; | |
| 39 | 193875 | values_ = NULL; | |
| 40 | 193875 | hasher_ = NULL; | |
| 41 | 193875 | bytes_allocated_ = 0; | |
| 42 | 193875 | num_collisions_ = 0; | |
| 43 | 193875 | max_collisions_ = 0; | |
| 44 | |||
| 45 | // Properly initialized by Init() | ||
| 46 | 193875 | capacity_ = 0; | |
| 47 | 193875 | initial_capacity_ = 0; | |
| 48 | 193875 | size_ = 0; | |
| 49 | 193875 | } | |
| 50 | |||
| 51 | 193737 | ~SmallHashBase() { DeallocMemory(keys_, values_, capacity_); } | |
| 52 | |||
| 53 | 193845 | void Init(uint32_t expected_size, Key empty, | |
| 54 | uint32_t (*hasher)(const Key &key)) { | ||
| 55 | 193845 | hasher_ = hasher; | |
| 56 | 193845 | empty_key_ = empty; | |
| 57 | 193845 | capacity_ = static_cast<uint32_t>(static_cast<double>(expected_size) | |
| 58 | 193845 | / kLoadFactor); | |
| 59 | 193845 | initial_capacity_ = capacity_; | |
| 60 | 193845 | static_cast<Derived *>(this)->SetThresholds(); // No-op for fixed size | |
| 61 | 193845 | AllocMemory(); | |
| 62 | 193845 | this->DoClear(false); | |
| 63 | 193845 | } | |
| 64 | |||
| 65 | 77312508 | bool Lookup(const Key &key, Value *value) const { | |
| 66 | uint32_t bucket; | ||
| 67 | uint32_t collisions; | ||
| 68 |
1/2✓ Branch 1 taken 77175028 times.
✗ Branch 2 not taken.
|
77312508 | const bool found = DoLookup(key, &bucket, &collisions); |
| 69 |
2/2✓ Branch 0 taken 57337140 times.
✓ Branch 1 taken 19837888 times.
|
77340861 | if (found) |
| 70 |
1/2✓ Branch 1 taken 100923 times.
✗ Branch 2 not taken.
|
57392348 | *value = values_[bucket]; |
| 71 | 77340861 | 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 | 536 | bool LookupEx(Key *key, Value *value) const { | |
| 82 | 536 | uint32_t bucket = ScaleHash(*key); | |
| 83 |
2/2✓ Branch 1 taken 178 times.
✓ Branch 2 taken 90 times.
|
536 | while (!(keys_[bucket] == empty_key_)) { |
| 84 |
1/2✓ Branch 1 taken 178 times.
✗ Branch 2 not taken.
|
356 | if (keys_[bucket] == *key) { |
| 85 | 356 | *key = keys_[bucket]; | |
| 86 | 356 | *value = values_[bucket]; | |
| 87 | 356 | return true; | |
| 88 | } | ||
| 89 | ✗ | bucket = (bucket + 1) % capacity_; | |
| 90 | } | ||
| 91 | 180 | return false; | |
| 92 | } | ||
| 93 | |||
| 94 | 7075608 | bool Contains(const Key &key) const { | |
| 95 | uint32_t bucket; | ||
| 96 | uint32_t collisions; | ||
| 97 |
1/2✓ Branch 1 taken 7074805 times.
✗ Branch 2 not taken.
|
7075608 | const bool found = DoLookup(key, &bucket, &collisions); |
| 98 | 7075608 | return found; | |
| 99 | } | ||
| 100 | |||
| 101 | 924755433 | void Insert(const Key &key, const Value &value) { | |
| 102 | 924755433 | static_cast<Derived *>(this)->Grow(); // No-op if fixed-size | |
| 103 | 922163853 | const bool overwritten = DoInsert(key, value, true); | |
| 104 | 921770235 | size_ += !overwritten; // size + 1 if the key was not yet in the map | |
| 105 | 921770235 | } | |
| 106 | |||
| 107 | 66899752 | bool Erase(const Key &key) { | |
| 108 | uint32_t bucket; | ||
| 109 | uint32_t collisions; | ||
| 110 |
1/2✓ Branch 1 taken 65348456 times.
✗ Branch 2 not taken.
|
66899752 | const bool found = DoLookup(key, &bucket, &collisions); |
| 111 |
1/2✓ Branch 0 taken 65354416 times.
✗ Branch 1 not taken.
|
65353861 | if (found) { |
| 112 | 65359472 | keys_[bucket] = empty_key_; | |
| 113 | 65359472 | size_--; | |
| 114 | 65359472 | bucket = (bucket + 1) % capacity_; | |
| 115 |
3/3✓ Branch 0 taken 118918436 times.
✓ Branch 1 taken 60672200 times.
✓ Branch 2 taken 5909478 times.
|
185505170 | while (!(keys_[bucket] == empty_key_)) { |
| 116 | 120219708 | Key rehash = keys_[bucket]; | |
| 117 | 120219708 | keys_[bucket] = empty_key_; | |
| 118 |
1/2✓ Branch 1 taken 120145698 times.
✗ Branch 2 not taken.
|
120219708 | DoInsert(rehash, values_[bucket], false); |
| 119 | 120145698 | bucket = (bucket + 1) % capacity_; | |
| 120 | } | ||
| 121 |
1/2✓ Branch 1 taken 65855274 times.
✗ Branch 2 not taken.
|
65285462 | static_cast<Derived *>(this)->Shrink(); // No-op if fixed-size |
| 122 | } | ||
| 123 | 65907283 | return found; | |
| 124 | } | ||
| 125 | |||
| 126 | 1075 | void Clear() { DoClear(true); } | |
| 127 | |||
| 128 | 6560 | uint64_t bytes_allocated() const { return bytes_allocated_; } | |
| 129 | 1779 | static double GetEntrySize() { | |
| 130 | 3558 | const double unit = sizeof(Key) + sizeof(Value); | |
| 131 | 3558 | 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 | 302546 | Key empty_key() const { return empty_key_; } | |
| 143 | 646060 | Key *keys() const { return keys_; } | |
| 144 | 711 | 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 | 1336061859 | uint32_t ScaleHash(const Key &key) const { | |
| 151 | 1336061859 | const double bucket = (static_cast<double>(hasher_(key)) | |
| 152 | 1337246769 | * static_cast<double>(capacity_) | |
| 153 | / static_cast<double>(static_cast<uint32_t>(-1))); | ||
| 154 | 1337246769 | return static_cast<uint32_t>(bucket) % capacity_; | |
| 155 | } | ||
| 156 | |||
| 157 | 264120 | void AllocMemory() { | |
| 158 | 264120 | keys_ = static_cast<Key *>(smmap(capacity_ * sizeof(Key))); | |
| 159 | 264150 | values_ = static_cast<Value *>(smmap(capacity_ * sizeof(Value))); | |
| 160 |
2/2✓ Branch 0 taken 790339600 times.
✓ Branch 1 taken 133740 times.
|
1569699002 | for (uint32_t i = 0; i < capacity_; ++i) { |
| 161 | 1569434852 | /*keys_[i] =*/new (keys_ + i) Key(); | |
| 162 | } | ||
| 163 |
2/2✓ Branch 0 taken 793096510 times.
✓ Branch 1 taken 120510 times.
|
1575186362 | for (uint32_t i = 0; i < capacity_; ++i) { |
| 164 | 1574948672 | /*values_[i] =*/new (values_ + i) Value(); | |
| 165 | } | ||
| 166 | 237690 | bytes_allocated_ = (sizeof(Key) + sizeof(Value)) * capacity_; | |
| 167 | 237690 | } | |
| 168 | |||
| 169 | 264012 | void DeallocMemory(Key *k, Value *v, uint32_t c) { | |
| 170 |
2/2✓ Branch 0 taken 795270562 times.
✓ Branch 1 taken 133670 times.
|
1579563518 | for (uint32_t i = 0; i < c; ++i) { |
| 171 | 1579299506 | k[i].~Key(); | |
| 172 | } | ||
| 173 |
2/2✓ Branch 0 taken 795546877 times.
✓ Branch 1 taken 133670 times.
|
1580116148 | for (uint32_t i = 0; i < c; ++i) { |
| 174 | 1579852136 | v[i].~Value(); | |
| 175 | } | ||
| 176 |
1/2✓ Branch 0 taken 133670 times.
✗ Branch 1 not taken.
|
264012 | if (k) |
| 177 | 264012 | smunmap(k); | |
| 178 |
1/2✓ Branch 0 taken 133670 times.
✗ Branch 1 not taken.
|
264012 | if (v) |
| 179 | 264012 | smunmap(v); | |
| 180 | 264012 | k = NULL; | |
| 181 | 264012 | v = NULL; | |
| 182 | 264012 | } | |
| 183 | |||
| 184 | // Returns true iff the key is overwritten | ||
| 185 | 1113361801 | 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 600393295 times.
✗ Branch 2 not taken.
|
1113361801 | const bool overwritten = DoLookup(key, &bucket, &collisions); |
| 190 |
2/2✓ Branch 0 taken 480441664 times.
✓ Branch 1 taken 119951631 times.
|
1114594174 | if (count_collisions) { |
| 191 | 922645908 | num_collisions_ += collisions; | |
| 192 | 922645908 | max_collisions_ = std::max(collisions, max_collisions_); | |
| 193 | } | ||
| 194 | 1113850957 | keys_[bucket] = key; | |
| 195 |
1/2✓ Branch 1 taken 47918005 times.
✗ Branch 2 not taken.
|
1113850957 | values_[bucket] = value; |
| 196 | 1113850957 | return overwritten; | |
| 197 | } | ||
| 198 | |||
| 199 | 1337046943 | bool DoLookup(const Key &key, uint32_t *bucket, uint32_t *collisions) const { | |
| 200 | 1337046943 | *bucket = ScaleHash(key); | |
| 201 | 1335586747 | *collisions = 0; | |
| 202 |
3/3✓ Branch 0 taken 25425217581 times.
✓ Branch 1 taken 573323346 times.
✓ Branch 2 taken 120598441 times.
|
27278507538 | while (!(keys_[*bucket] == empty_key_)) { |
| 203 |
3/3✓ Branch 0 taken 132403705 times.
✓ Branch 1 taken 25308768512 times.
✓ Branch 2 taken 82198350 times.
|
26174300738 | if (keys_[*bucket] == key) |
| 204 | 231379947 | return true; | |
| 205 | 25942920791 | *bucket = (*bucket + 1) % capacity_; | |
| 206 | 25942920791 | (*collisions)++; | |
| 207 | } | ||
| 208 | 1104206800 | return false; | |
| 209 | } | ||
| 210 | |||
| 211 | 264242 | void DoClear(const bool reset_capacity) { | |
| 212 |
2/2✓ Branch 0 taken 1075 times.
✓ Branch 1 taken 132757 times.
|
264242 | if (reset_capacity) |
| 213 | 1769 | static_cast<Derived *>(this)->ResetCapacity(); // No-op if fixed-size | |
| 214 |
2/2✓ Branch 0 taken 793223905 times.
✓ Branch 1 taken 133832 times.
|
1575342124 | for (uint32_t i = 0; i < capacity_; ++i) |
| 215 | 1575077882 | keys_[i] = empty_key_; | |
| 216 | 264242 | size_ = 0; | |
| 217 | 264242 | } | |
| 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 | 6468 | void SetThresholds() { } | |
| 247 | 1173281 | void Grow() { } | |
| 248 | 52564 | void Shrink() { } | |
| 249 | 92 | 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 | 187407 | SmallHashDynamic() : Base() { | |
| 264 | 187407 | num_migrates_ = 0; | |
| 265 | |||
| 266 | // Properly set by Init | ||
| 267 | 187407 | threshold_grow_ = 0; | |
| 268 | 187407 | threshold_shrink_ = 0; | |
| 269 | 187407 | } | |
| 270 | |||
| 271 | SmallHashDynamic(const SmallHashDynamic<Key, Value> &other) : Base() { | ||
| 272 | num_migrates_ = 0; | ||
| 273 | CopyFrom(other); | ||
| 274 | } | ||
| 275 | |||
| 276 | 343 | SmallHashDynamic<Key, Value> &operator=( | |
| 277 | const SmallHashDynamic<Key, Value> &other) { | ||
| 278 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 179 times.
|
343 | if (&other == this) |
| 279 | ✗ | return *this; | |
| 280 | |||
| 281 | 343 | CopyFrom(other); | |
| 282 | 343 | return *this; | |
| 283 | } | ||
| 284 | |||
| 285 | 51759127 | uint32_t capacity() const { return Base::capacity_; } | |
| 286 | 551159349 | uint32_t size() const { return Base::size_; } | |
| 287 | uint32_t num_migrates() const { return num_migrates_; } | ||
| 288 | |||
| 289 | protected: | ||
| 290 | 257592 | void SetThresholds() { | |
| 291 | 257592 | threshold_grow_ = static_cast<uint32_t>(static_cast<double>(capacity()) | |
| 292 | 257652 | * kThresholdGrow); | |
| 293 | 257652 | threshold_shrink_ = static_cast<uint32_t>(static_cast<double>(capacity()) | |
| 294 | 257652 | * kThresholdShrink); | |
| 295 | 257652 | } | |
| 296 | |||
| 297 | 922322256 | void Grow() { | |
| 298 |
2/2✓ Branch 1 taken 23897 times.
✓ Branch 2 taken 479093040 times.
|
922322256 | if (size() > threshold_grow_) |
| 299 | 47794 | Migrate(capacity() * 2); | |
| 300 | 921195708 | } | |
| 301 | |||
| 302 | 65848717 | void Shrink() { | |
| 303 |
2/2✓ Branch 1 taken 25117636 times.
✓ Branch 2 taken 40694837 times.
|
65848717 | if (size() < threshold_shrink_) { |
| 304 | 25122692 | const uint32_t target_capacity = capacity() / 2; | |
| 305 |
2/2✓ Branch 0 taken 10320 times.
✓ Branch 1 taken 25103524 times.
|
25118900 | if (target_capacity >= Base::initial_capacity_) |
| 306 | 10320 | Migrate(target_capacity); | |
| 307 | } | ||
| 308 | 65813737 | } | |
| 309 | |||
| 310 | 1677 | void ResetCapacity() { | |
| 311 | 1677 | Base::DeallocMemory(Base::keys_, Base::values_, Base::capacity_); | |
| 312 | 1677 | Base::capacity_ = Base::initial_capacity_; | |
| 313 | 1677 | Base::AllocMemory(); | |
| 314 | 1677 | SetThresholds(); | |
| 315 | 1677 | } | |
| 316 | |||
| 317 | private: | ||
| 318 | // Returns a random permutation of indices [0..N-1] that is allocated | ||
| 319 | // by smmap (Knuth's shuffle algorithm) | ||
| 320 | 20998 | uint32_t *ShuffleIndices(const uint32_t N) { | |
| 321 | 20998 | uint32_t *shuffled = static_cast<uint32_t *>(smmap(N * sizeof(uint32_t))); | |
| 322 | // Init with identity | ||
| 323 |
2/2✓ Branch 0 taken 168102009 times.
✓ Branch 1 taken 10499 times.
|
336225016 | for (unsigned i = 0; i < N; ++i) |
| 324 | 336204018 | shuffled[i] = i; | |
| 325 | // Shuffle (no shuffling for the last element) | ||
| 326 |
1/2✓ Branch 0 taken 161773285 times.
✗ Branch 1 not taken.
|
323436768 | for (unsigned i = 0; i < N - 1; ++i) { |
| 327 | 323546570 | const uint32_t swap_idx = i + g_prng.Next(N - i); | |
| 328 | 323415770 | const uint32_t tmp = shuffled[i]; | |
| 329 | 323415770 | shuffled[i] = shuffled[swap_idx]; | |
| 330 | 323415770 | shuffled[swap_idx] = tmp; | |
| 331 | } | ||
| 332 | 343 | return shuffled; | |
| 333 | } | ||
| 334 | |||
| 335 | 68494 | void Migrate(const uint32_t new_capacity) { | |
| 336 | 68494 | Key *old_keys = Base::keys_; | |
| 337 | 68494 | Value *old_values = Base::values_; | |
| 338 | 68494 | const uint32_t old_capacity = capacity(); | |
| 339 | 68494 | const uint32_t old_size = size(); | |
| 340 | |||
| 341 | 68494 | Base::capacity_ = new_capacity; | |
| 342 | 68494 | SetThresholds(); | |
| 343 | 68554 | Base::AllocMemory(); | |
| 344 | 68584 | Base::DoClear(false); | |
| 345 |
2/2✓ Branch 0 taken 10320 times.
✓ Branch 1 taken 23972 times.
|
68584 | if (new_capacity < old_capacity) { |
| 346 | 20640 | uint32_t *shuffled_indices = ShuffleIndices(old_capacity); | |
| 347 |
1/2✓ Branch 0 taken 146585220 times.
✗ Branch 1 not taken.
|
293126610 | for (uint32_t i = 0; i < old_capacity; ++i) { |
| 348 |
2/3✓ Branch 0 taken 36726225 times.
✓ Branch 1 taken 109858995 times.
✗ Branch 2 not taken.
|
293170440 | if (old_keys[shuffled_indices[i]] != Base::empty_key_) { |
| 349 | 73452450 | Base::Insert(old_keys[shuffled_indices[i]], | |
| 350 | 73452450 | old_values[shuffled_indices[i]]); | |
| 351 | } | ||
| 352 | } | ||
| 353 | ✗ | smunmap(shuffled_indices); | |
| 354 | } else { | ||
| 355 |
2/2✓ Branch 0 taken 283103184 times.
✓ Branch 1 taken 18182 times.
|
566242732 | for (uint32_t i = 0; i < old_capacity; ++i) { |
| 356 |
3/3✓ Branch 0 taken 157252124 times.
✓ Branch 1 taken 107473185 times.
✓ Branch 2 taken 18377875 times.
|
566206368 | if (old_keys[i] != Base::empty_key_) |
| 357 | 424786970 | Base::Insert(old_keys[i], old_values[i]); | |
| 358 | } | ||
| 359 | } | ||
| 360 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 34277 times.
|
57004 | assert(size() == old_size); |
| 361 | |||
| 362 | 68554 | Base::DeallocMemory(old_keys, old_values, old_capacity); | |
| 363 | 68584 | num_migrates_++; | |
| 364 | 68584 | } | |
| 365 | |||
| 366 | 343 | void CopyFrom(const SmallHashDynamic<Key, Value> &other) { | |
| 367 | 343 | uint32_t *shuffled_indices = ShuffleIndices(other.capacity_); | |
| 368 |
2/2✓ Branch 0 taken 20647284 times.
✓ Branch 1 taken 179 times.
|
20651071 | for (uint32_t i = 0; i < other.capacity_; ++i) { |
| 369 |
2/3✗ Branch 0 not taken.
✓ Branch 1 taken 15001722 times.
✓ Branch 2 taken 5645562 times.
|
20650728 | if (other.keys_[shuffled_indices[i]] != other.empty_key_) { |
| 370 | 15000000 | this->Insert(other.keys_[shuffled_indices[i]], | |
| 371 | 15000000 | other.values_[shuffled_indices[i]]); | |
| 372 | } | ||
| 373 | } | ||
| 374 | 343 | smunmap(shuffled_indices); | |
| 375 | 343 | } | |
| 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 | 150 | MultiHash() { | |
| 392 | 150 | num_hashmaps_ = 0; | |
| 393 | 150 | hashmaps_ = NULL; | |
| 394 | 150 | locks_ = NULL; | |
| 395 | 150 | } | |
| 396 | |||
| 397 | 150 | 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 150 times.
|
150 | assert(num_hashmaps > 0); |
| 400 | 150 | const uint8_t N = num_hashmaps; | |
| 401 | 150 | num_hashmaps_ = N; | |
| 402 |
3/4✓ Branch 2 taken 6300 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6300 times.
✓ Branch 5 taken 150 times.
|
6450 | hashmaps_ = new SmallHashDynamic<Key, Value>[N](); |
| 403 | 150 | locks_ = static_cast<pthread_mutex_t *>( | |
| 404 | 150 | smalloc(N * sizeof(pthread_mutex_t))); | |
| 405 |
2/2✓ Branch 0 taken 6300 times.
✓ Branch 1 taken 150 times.
|
6450 | for (uint8_t i = 0; i < N; ++i) { |
| 406 | 6300 | int retval = pthread_mutex_init(&locks_[i], NULL); | |
| 407 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6300 times.
|
6300 | assert(retval == 0); |
| 408 | 6300 | hashmaps_[i].Init(128, empty_key, hasher); | |
| 409 | } | ||
| 410 | 150 | } | |
| 411 | |||
| 412 | 150 | ~MultiHash() { | |
| 413 |
2/2✓ Branch 0 taken 6300 times.
✓ Branch 1 taken 150 times.
|
6450 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { |
| 414 | 6300 | pthread_mutex_destroy(&locks_[i]); | |
| 415 | } | ||
| 416 | 150 | free(locks_); | |
| 417 |
3/4✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6300 times.
✓ Branch 3 taken 150 times.
|
6450 | delete[] hashmaps_; |
| 418 | 150 | } | |
| 419 | |||
| 420 | 15000000 | bool Lookup(const Key &key, Value *value) { | |
| 421 | 15000000 | uint8_t target = SelectHashmap(key); | |
| 422 | 15000000 | Lock(target); | |
| 423 | 15000000 | const bool result = hashmaps_[target].Lookup(key, value); | |
| 424 | 15000000 | Unlock(target); | |
| 425 | 15000000 | return result; | |
| 426 | } | ||
| 427 | |||
| 428 | 56151780 | void Insert(const Key &key, const Value &value) { | |
| 429 | 56151780 | uint8_t target = SelectHashmap(key); | |
| 430 | 54402630 | Lock(target); | |
| 431 | 56346420 | hashmaps_[target].Insert(key, value); | |
| 432 | 53878020 | Unlock(target); | |
| 433 | 57227160 | } | |
| 434 | |||
| 435 | 27178095 | void Erase(const Key &key) { | |
| 436 | 27178095 | uint8_t target = SelectHashmap(key); | |
| 437 | 25921245 | Lock(target); | |
| 438 | 27590070 | hashmaps_[target].Erase(key); | |
| 439 | 25414665 | Unlock(target); | |
| 440 | 27924450 | } | |
| 441 | |||
| 442 | 15 | void Clear() { | |
| 443 |
2/2✓ Branch 0 taken 630 times.
✓ Branch 1 taken 15 times.
|
645 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 444 | 630 | Lock(i); | |
| 445 |
2/2✓ Branch 0 taken 630 times.
✓ Branch 1 taken 15 times.
|
645 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 446 | 630 | hashmaps_[i].Clear(); | |
| 447 |
2/2✓ Branch 0 taken 630 times.
✓ Branch 1 taken 15 times.
|
645 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 448 | 630 | Unlock(i); | |
| 449 | 15 | } | |
| 450 | |||
| 451 | 150 | uint8_t num_hashmaps() const { return num_hashmaps_; } | |
| 452 | |||
| 453 | 120 | void GetSizes(uint32_t *sizes) { | |
| 454 |
2/2✓ Branch 0 taken 5040 times.
✓ Branch 1 taken 120 times.
|
5160 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { |
| 455 | 5040 | Lock(i); | |
| 456 | 5040 | sizes[i] = hashmaps_[i].size(); | |
| 457 | 5040 | Unlock(i); | |
| 458 | } | ||
| 459 | 120 | } | |
| 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 | 97784970 | inline uint8_t SelectHashmap(const Key &key) { | |
| 471 | 97784970 | uint32_t hash = MurmurHash2(&key, sizeof(key), 0x37); | |
| 472 | 95869005 | double bucket = static_cast<double>(hash) | |
| 473 | 95869005 | * static_cast<double>(num_hashmaps_) | |
| 474 | / static_cast<double>(static_cast<uint32_t>(-1)); | ||
| 475 | 95869005 | return static_cast<uint32_t>(bucket) % num_hashmaps_; | |
| 476 | } | ||
| 477 | |||
| 478 | 94900560 | inline void Lock(const uint8_t target) { | |
| 479 | 94900560 | int retval = pthread_mutex_lock(&locks_[target]); | |
| 480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 102732735 times.
|
102732735 | assert(retval == 0); |
| 481 | 102732735 | } | |
| 482 | |||
| 483 | 94242150 | inline void Unlock(const uint8_t target) { | |
| 484 | 94242150 | int retval = pthread_mutex_unlock(&locks_[target]); | |
| 485 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 103485480 times.
|
103485480 | assert(retval == 0); |
| 486 | 103485480 | } | |
| 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 |