Debian Codegraph @

unittest/gunit/innodb/ut0lock_free_hash-t.cc

raw symbols SCIP
1 /* Copyright (c) 2015, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 /* See http://code.google.com/p/googletest/wiki/Primer */
25
26 /* Enable this to have the tests below run lots of iterations, suitable for
27 perf testing and comparison, but not suitable for daily automated testing
28 where CPU time is scarce. */
29 #if 0
30 #define HEAVY_TEST
31 #endif
32
33 /* Enable to perf test std::map instead of
34 the InnoDB lock free hash. */
35 #if 0
36 #define TEST_STD_MAP 1
37 #endif
38
39 /* Enable to perf test std::unordered_map instead of
40 the InnoDB lock free hash, compile with -std=c++11 */
41 #if 0
42 #define TEST_STD_UNORDERED_MAP 1
43 #endif
44
45 /* Enable to perf test tbb::concurrent_hash_map instead of
46 the InnoDB lock free hash, download from
47 https://www.threadingbuildingblocks.org/ and also adjust
48 unittest/gunit/innodb/CMakeLists.txt */
49 #if 0
50 #define TEST_TBB 1
51 #endif
52
53 #if (defined(TEST_STD_MAP) && \
54 (defined(TEST_STD_UNORDERED_MAP) || defined(TEST_TBB))) || \
55 (defined(TEST_STD_UNORDERED_MAP) && defined(TEST_TBB))
56 #error TEST_STD_MAP, TEST_STD_UNORDERED_MAP and TEST_TBB are mutually exclusive
57 #endif
58
59 #ifdef TEST_STD_UNORDERED_MAP
60 #include <unordered_map>
61 #endif /* TEST_STD_UNORDERED_MAP */
62
63 #ifdef TEST_STD_MAP
64 #include <map>
65 #endif /* TEST_STD_MAP */
66
67 #ifdef TEST_TBB
68 #include <tbb/concurrent_hash_map.h>
69 #endif /* TEST_TBB */
70
71 #include <gtest/gtest.h>
72 #include <stddef.h>
73 #include <thread>
74
75 #include "my_thread_local.h" /* Needed to access thread local variables */
76 #include "storage/innobase/include/os0event.h" /* os_event_global_*() */
77 #include "storage/innobase/include/os0thread-create.h" /* os_thread_*() */
78 #include "storage/innobase/include/os0thread.h" /* os_thread_*() */
79 #include "storage/innobase/include/srv0conc.h" /* srv_max_n_threads */
80 #include "storage/innobase/include/sync0debug.h" /* sync_check_init(), sync_check_close() */
81 #include "storage/innobase/include/sync0policy.h" /* needed by ib0mutex.h, which is not self contained */
82 #include "storage/innobase/include/univ.i"
83 #include "storage/innobase/include/ut0dbg.h" /* ut_chrono_t */
84 #include "storage/innobase/include/ut0lock_free_hash.h"
85 #include "storage/innobase/include/ut0mutex.h" /* SysMutex, mutex_enter() */
86
87 namespace innodb_lock_free_hash_unittest {
88
89 #if defined(TEST_STD_MAP) || defined(TEST_STD_UNORDERED_MAP)
90 class std_hash_t : public ut_hash_interface_t {
91 public:
92 #ifdef TEST_STD_MAP
93 typedef std::map<uint64_t, int64_t> map_t;
94 #else
95 typedef std::unordered_map<uint64_t, int64_t> map_t;
96 #endif
97
98 /** Constructor. */
99 std_hash_t() { m_map_latch.init(LATCH_ID_NONE, __FILE__, __LINE__); }
100
101 /** Destructor. */
102 ~std_hash_t() { m_map_latch.destroy(); }
103
104 int64_t get(uint64_t key) const {
105 m_map_latch.enter(0, 0, __FILE__, __LINE__);
106
107 map_t::const_iterator it = m_map.find(key);
108
109 int64_t val;
110
111 if (it != m_map.end()) {
112 val = it->second;
113 } else {
114 val = NOT_FOUND;
115 }
116
117 m_map_latch.exit();
118
119 return (val);
120 }
121
122 void set(uint64_t key, int64_t val) {
123 m_map_latch.enter(0, 0, __FILE__, __LINE__);
124
125 m_map[key] = val;
126
127 m_map_latch.exit();
128 }
129
130 void del(uint64_t key) {
131 m_map_latch.enter(0, 0, __FILE__, __LINE__);
132
133 m_map.erase(key);
134
135 m_map_latch.exit();
136 }
137
138 void inc(uint64_t key) {
139 m_map_latch.enter(0, 0, __FILE__, __LINE__);
140
141 map_t::iterator it = m_map.find(key);
142
143 if (it != m_map.end()) {
144 ++it->second;
145 } else {
146 m_map.insert(map_t::value_type(key, 1));
147 }
148
149 m_map_latch.exit();
150 }
151
152 void dec(uint64_t key) {
153 m_map_latch.enter(0, 0, __FILE__, __LINE__);
154
155 map_t::iterator it = m_map.find(key);
156
157 if (it != m_map.end()) {
158 --it->second;
159 } else {
160 m_map.insert(map_t::value_type(key, -1));
161 }
162
163 m_map_latch.exit();
164 }
165
166 #ifdef UT_HASH_IMPLEMENT_PRINT_STATS
167 void print_stats() {}
168 #endif /* UT_HASH_IMPLEMENT_PRINT_STATS */
169
170 private:
171 map_t m_map;
172 mutable OSTrackMutex<NoPolicy> m_map_latch;
173 };
174
175 #elif defined(TEST_TBB)
176
177 class tbb_hash_t : public ut_hash_interface_t {
178 public:
179 typedef uint64_t key_t;
180 typedef int64_t val_t;
181 typedef tbb::concurrent_hash_map<key_t, val_t> map_t;
182
183 /** Constructor. */
184 tbb_hash_t() {}
185
186 /** Destructor. */
187 ~tbb_hash_t() {}
188
189 int64_t get(uint64_t key) const {
190 map_t::const_accessor a;
191
192 if (m_map.find(a, key)) {
193 return (a->second);
194 }
195
196 return (NOT_FOUND);
197 }
198
199 void set(uint64_t key, int64_t val) {
200 map_t::accessor a;
201
202 if (m_map.insert(a, map_t::value_type(key, val))) {
203 /* Insert succeeded, do nothing. */
204 } else {
205 /* A tuple with the given key already exists,
206 overwrite its value. */
207 a->second = val;
208 }
209 }
210
211 void del(uint64_t key) { m_map.erase(key); }
212
213 void inc(uint64_t key) { delta(key, 1); }
214
215 void dec(uint64_t key) { delta(key, -1); }
216
217 #ifdef UT_HASH_IMPLEMENT_PRINT_STATS
218 void print_stats() {}
219 #endif /* UT_HASH_IMPLEMENT_PRINT_STATS */
220
221 private:
222 void delta(uint64_t key, int64_t delta) {
223 map_t::accessor a;
224
225 if (m_map.insert(a, map_t::value_type(key, delta))) {
226 /* Insert succeeded because a tuple with this key
227 did not exist before, do nothing. */
228 } else {
229 /* A tuple with the given key already exists,
230 apply the delta to its value. */
231 os_atomic_increment_uint64(static_cast<uint64_t *>(&a->second), delta);
232 }
233 }
234
235 map_t m_map;
236 };
237 #endif
238
239 /** Generate a key to use in the (key, value) tuples.
240 @param[in] i some sequential number
241 @param[in] extra_bits extra bits to OR into the result
242 @return a key, derived from 'i' and 'extra_bits' */
243 inline uint64_t key_gen(size_t i, uint64_t extra_bits) {
244 return ((i * 7 + 3) | extra_bits);
245 }
246
247 /** Generate a value to use in the (key, value) tuples.
248 @param[in] i some sequential number
249 @return a value derived from 'i' */
250 inline int64_t val_from_i(size_t i) {
251 /* Make sure that the returned value is big enough, so that a few
252 decrements don't make it negative. */
253 return (i * 13 + 10000);
254 }
255
256 /** Insert some tuples in the hash, generating their keys and values
257 @param[in,out] hash hash into which to insert
258 @param[in] n_elements number of elements to insert
259 @param[in] key_extra_bits extra bits to use for key generation */
260 void hash_insert(ut_hash_interface_t *hash, size_t n_elements,
261 uint64_t key_extra_bits) {
262 for (size_t i = 0; i < n_elements; i++) {
263 hash->set(key_gen(i, key_extra_bits), val_from_i(i));
264 }
265 }
266
267 /** Delete the tuples from the hash, inserted by hash_insert(), when called
268 with the same arguments.
269 @param[in,out] hash hash from which to delete
270 @param[in] n_elements number of elements to delete
271 @param[in] key_extra_bits extra bits to use for key generation */
272 void hash_delete(ut_hash_interface_t *hash, size_t n_elements,
273 uint64_t key_extra_bits) {
274 for (size_t i = 0; i < n_elements; i++) {
275 hash->del(key_gen(i, key_extra_bits));
276 }
277 }
278
279 /** Check that the tuples inserted by hash_insert() are present in the hash.
280 @param[in] hash hash to check
281 @param[in] n_elements number of elements inserted by hash_insert()
282 @param[in] key_extra_bits extra bits that were given to hash_insert() */
283 void hash_check_inserted(const ut_hash_interface_t *hash, size_t n_elements,
284 uint64_t key_extra_bits) {
285 for (size_t i = 0; i < n_elements; i++) {
286 const uint64_t key = key_gen(i, key_extra_bits);
287
288 ASSERT_EQ(val_from_i(i), hash->get(key));
289 }
290 }
291
292 /** Check that the tuples deleted by hash_delete() are missing from the hash.
293 @param[in] hash hash to check
294 @param[in] n_elements number of elements deleted by hash_delete()
295 @param[in] key_extra_bits extra bits that were given to hash_delete() */
296 void hash_check_deleted(const ut_hash_interface_t *hash, size_t n_elements,
297 uint64_t key_extra_bits) {
298 for (size_t i = 0; i < n_elements; i++) {
299 const uint64_t key = key_gen(i, key_extra_bits);
300
301 const int64_t not_found = ut_hash_interface_t::NOT_FOUND;
302
303 ASSERT_EQ(not_found, hash->get(key));
304 }
305 }
306
307 class ut0lock_free_hash : public ::testing::Test {
308 public:
309 static void SetUpTestCase() {
310 srv_max_n_threads = 1024;
311
312 os_event_global_init();
313 sync_check_init(srv_max_n_threads);
314 os_thread_open();
315 }
316
317 static void TearDownTestCase() {
318 os_thread_close();
319 sync_check_close();
320 os_event_global_destroy();
321 }
322 };
323
324 TEST_F(ut0lock_free_hash, single_threaded) {
325 #ifdef HAVE_UT_CHRONO_T
326 ut_chrono_t chrono("single threaded");
327 #endif /* HAVE_UT_CHRONO_T */
328
329 #if defined(TEST_STD_MAP) || defined(TEST_STD_UNORDERED_MAP)
330 ut_hash_interface_t *hash = new std_hash_t();
331 #elif defined(TEST_TBB)
332 ut_hash_interface_t *hash = new tbb_hash_t();
333 #else
334 ut_hash_interface_t *hash = new ut_lock_free_hash_t(1048576, true);
335 #endif
336
337 const size_t n_elements = 16 * 1024;
338
339 hash_insert(hash, n_elements, 0);
340
341 hash_check_inserted(hash, n_elements, 0);
342
343 hash_delete(hash, n_elements, 0);
344
345 hash_check_deleted(hash, n_elements, 0);
346
347 hash_insert(hash, n_elements, 0);
348
349 hash_check_inserted(hash, n_elements, 0);
350
351 #if defined(HEAVY_TEST)
352 const size_t n_iter = 512;
353 #else
354 const size_t n_iter = 128 / 8;
355 #endif
356
357 for (size_t it = 0; it < n_iter; it++) {
358 /* Increment the values of some and decrement of others. */
359 for (size_t i = 0; i < n_elements; i++) {
360 const bool should_inc = i % 2 == 0;
361 const uint64_t key = key_gen(i, 0);
362
363 /* Inc/dec from 0 to 9 times, depending on 'i'. */
364 for (size_t j = 0; j < i % 10; j++) {
365 if (should_inc) {
366 hash->inc(key);
367 } else {
368 hash->dec(key);
369 }
370 }
371 }
372 }
373
374 /* Check that increment/decrement was done properly. */
375 for (size_t i = 0; i < n_elements; i++) {
376 const bool was_inc = i % 2 == 0;
377 const int64_t delta = (i % 10) * n_iter;
378
379 ASSERT_EQ(val_from_i(i) + (was_inc ? delta : -delta),
380 hash->get(key_gen(i, 0)));
381 }
382
383 hash_delete(hash, n_elements, 0);
384
385 hash_check_deleted(hash, n_elements, 0);
386
387 delete hash;
388 }
389
390 /** A thread's parameters. */
391 struct thread_params_t {
392 /** Common hash, accessed by many threads concurrently. */
393 ut_hash_interface_t *hash;
394
395 /** Thread id. Used to derive keys that are private to a given
396 thread, whose tuples are accessed only by that thread. */
397 uint64_t thread_id;
398
399 /** Number of common tuples (accessed by all threads) that are inserted
400 into the hash before starting the threads. */
401 size_t n_common;
402
403 /** Number of private, per-thread tuples to insert by each thread. */
404 size_t n_priv_per_thread;
405 };
406
407 /** Run a multi threaded test.
408 @param[in] label label used when printing the timing
409 @param[in] initial_hash_size initial number of cells in the hash
410 @param[in] n_common number of common tuples (accessed by
411 all threads) to insert into the hash before starting up all threads
412 @param[in] n_priv_per_thread number of private, per-thread tuples
413 to insert by each thread.
414 @param[in] n_threads number of threads to start. Overall
415 the hash will be filled with n_common + n_threads * n_priv_per_thread tuples
416 @param[in] thread_func function to fire up as a new thread */
417 template <typename F>
418 static void run_multi_threaded(const char *label, size_t initial_hash_size,
419 size_t n_common, size_t n_priv_per_thread,
420 size_t n_threads, F thread_func) {
421 #ifdef HAVE_UT_CHRONO_T
422 ut_chrono_t chrono(label);
423 #endif /* HAVE_UT_CHRONO_T */
424
425 ut_hash_interface_t *hash;
426
427 ut::detail::random_seed = 0;
428
429 #if defined(TEST_STD_MAP) || defined(TEST_STD_UNORDERED_MAP)
430 hash = new std_hash_t();
431 #elif defined(TEST_TBB)
432 hash = new tbb_hash_t();
433 #else
434 hash = new ut_lock_free_hash_t(initial_hash_size, true);
435 #endif
436
437 std::thread **threads = new std::thread *[n_threads];
438 thread_params_t *params = new thread_params_t[n_threads];
439
440 hash_insert(hash, n_common, 0);
441
442 for (uintptr_t i = 0; i < n_threads; i++) {
443 params[i].hash = hash;
444 /* Avoid thread_id == 0 because that will collide with the
445 shared tuples, thus use 'i + 1' instead of 'i'. */
446 params[i].thread_id = i + 1;
447 params[i].n_common = n_common;
448 params[i].n_priv_per_thread = n_priv_per_thread;
449
450 threads[i] = new std::thread(thread_func, &params[i]);
451 }
452
453 /* Wait for all threads to exit. */
454 for (uintptr_t i = 0; i < n_threads; i++) {
455 threads[i]->join();
456 delete threads[i];
457 }
458
459 hash_check_inserted(hash, n_common, 0);
460
461 #ifdef UT_HASH_IMPLEMENT_PRINT_STATS
462 hash->print_stats();
463 #endif /* UT_HASH_IMPLEMENT_PRINT_STATS */
464
465 delete[] params;
466 delete[] threads;
467
468 delete hash;
469 }
470
471 /** Hammer a common hash with inc(), dec() and set(), 100% writes.
472 The inc()/dec() performed on the common keys will net to 0 when this thread
473 ends. It also inserts some tuples with keys that are unique to this thread.
474 @param[in] p thread arguments */
475 void thread_0r100w(const thread_params_t *p) {
476 const uint64_t key_extra_bits = p->thread_id << 32;
477
478 hash_insert(p->hash, p->n_priv_per_thread, key_extra_bits);
479
480 hash_check_inserted(p->hash, p->n_priv_per_thread, key_extra_bits);
481
482 #if defined(HEAVY_TEST)
483 const size_t n_iter = 512 * 4096 / p->n_common;
484 #else
485 const size_t n_iter = 4096 / p->n_common;
486 #endif
487
488 for (size_t i = 0; i < n_iter; i++) {
489 for (size_t j = 0; j < p->n_common; j++) {
490 const uint64_t key = key_gen(j, 0);
491
492 p->hash->inc(key);
493 p->hash->inc(key);
494 p->hash->inc(key);
495
496 p->hash->dec(key);
497 p->hash->inc(key);
498
499 p->hash->dec(key);
500 p->hash->dec(key);
501 p->hash->dec(key);
502 }
503
504 for (size_t j = 0; j < p->n_priv_per_thread; j++) {
505 const uint64_t key = key_gen(j, key_extra_bits);
506
507 for (size_t k = 0; k < 4; k++) {
508 p->hash->inc(key);
509 p->hash->dec(key);
510 p->hash->inc(key);
511 p->hash->dec(key);
512 }
513 }
514 }
515
516 hash_check_inserted(p->hash, p->n_priv_per_thread, key_extra_bits);
517
518 hash_delete(p->hash, p->n_priv_per_thread, key_extra_bits);
519
520 hash_check_deleted(p->hash, p->n_priv_per_thread, key_extra_bits);
521 }
522
523 TEST_F(ut0lock_free_hash, multi_threaded_0r100w) {
524 run_multi_threaded(
525 "multi threaded, 0% read, 100% write, many keys" /* label */,
526 1024 * 32 /* initial hash size */, 4096 /* n_common */,
527 256 /* n_priv_per_thread */, 64 /* n_threads */,
528 thread_0r100w /* thr func */
529 );
530 }
531
532 TEST_F(ut0lock_free_hash, multi_threaded_0r100w_few_keys) {
533 run_multi_threaded(
534 "multi threaded, 0% read, 100% write, few keys" /* label */,
535 1024 * 32 /* initial hash size */, 16 /* n_common */,
536 0 /* n_priv_per_thread */, 64 /* n_threads */,
537 thread_0r100w /* thr func */
538 );
539 }
540
541 TEST_F(ut0lock_free_hash, multi_threaded_0r100w_grow) {
542 run_multi_threaded(
543 "multi threaded, 0% read, 100% write, arraygrow" /* label */,
544 1 /* initial hash size */, 4096 /* n_common */,
545 256 /* n_priv_per_thread */, 64, /* n_threads */
546 thread_0r100w /* thr func */
547 );
548 }
549
550 /** Hammer a common hash with get(), inc(), dec() and set(), 50% reads and
551 50% writes. The inc()/dec() performed on the common keys will net to 0 when
552 this thread ends. It also inserts some tuples with keys that are unique to
553 this thread.
554 @param[in] p thread arguments */
555 void thread_50r50w(const thread_params_t *p) {
556 const uint64_t key_extra_bits = p->thread_id << 32;
557
558 hash_insert(p->hash, p->n_priv_per_thread, key_extra_bits);
559
560 hash_check_inserted(p->hash, p->n_priv_per_thread, key_extra_bits);
561
562 #if defined(HEAVY_TEST)
563 const size_t n_iter = 512;
564 #else
565 const size_t n_iter = 1;
566 #endif
567
568 for (size_t i = 0; i < n_iter; i++) {
569 for (size_t j = 0; j < p->n_common; j++) {
570 const uint64_t key_write = key_gen(j, 0);
571 /* Make 1/4 of the reads access possibly nonexisting
572 tuples. */
573 const uint64_t key_read = key_gen(j + p->n_common / 4, 0);
574
575 p->hash->get(key_read);
576
577 p->hash->inc(key_write);
578 p->hash->get(key_read);
579 p->hash->inc(key_write);
580
581 p->hash->dec(key_write);
582 p->hash->get(key_read);
583 p->hash->dec(key_write);
584
585 p->hash->get(key_read);
586 }
587
588 for (size_t j = 0; j < p->n_priv_per_thread; j++) {
589 const uint64_t key_write = key_gen(j, key_extra_bits);
590 /* Make 1/4 of the reads access possibly nonexisting
591 tuples. */
592 const uint64_t key_read =
593 key_gen(j + p->n_priv_per_thread / 4, key_extra_bits);
594
595 for (size_t k = 0; k < 4; k++) {
596 p->hash->inc(key_write);
597 p->hash->get(key_read);
598 p->hash->dec(key_write);
599 p->hash->get(key_read);
600 }
601 }
602 }
603
604 hash_check_inserted(p->hash, p->n_priv_per_thread, key_extra_bits);
605
606 hash_delete(p->hash, p->n_priv_per_thread, key_extra_bits);
607
608 hash_check_deleted(p->hash, p->n_priv_per_thread, key_extra_bits);
609 }
610
611 TEST_F(ut0lock_free_hash, multi_threaded_50r50w) {
612 run_multi_threaded(
613 "multi threaded, 50% read, 50% write, many keys" /* label */,
614 1024 * 32 /* initial hash size */, 4096 /* n_common */,
615 256 /* n_priv_per_thread */, 64 /* n_threads */,
616 thread_50r50w /* thr func */
617 );
618 }
619
620 /** Hammer a commmon hash with get()s, 100% reads.
621 @param[in] p thread arguments */
622 void thread_100r0w(const thread_params_t *p) {
623 const uint64_t key_extra_bits = p->thread_id << 32;
624
625 hash_insert(p->hash, p->n_priv_per_thread, key_extra_bits);
626
627 hash_check_inserted(p->hash, p->n_priv_per_thread, key_extra_bits);
628
629 #if defined(HEAVY_TEST)
630 const size_t n_iter = 512;
631 #else
632 const size_t n_iter = 1;
633 #endif
634
635 for (size_t i = 0; i < n_iter; i++) {
636 for (size_t j = 0; j < p->n_common; j++) {
637 /* Make 1/4 of the reads access possibly nonexisting
638 tuples. */
639 const uint64_t key_read = key_gen(j + p->n_common / 4, 0);
640
641 p->hash->get(key_read);
642 p->hash->get(key_read);
643 p->hash->get(key_read);
644 p->hash->get(key_read);
645 p->hash->get(key_read);
646 p->hash->get(key_read);
647 p->hash->get(key_read);
648 p->hash->get(key_read);
649 }
650
651 for (size_t j = 0; j < p->n_priv_per_thread; j++) {
652 /* Make 1/4 of the reads access possibly nonexisting
653 tuples. */
654 const uint64_t key_read =
655 key_gen(j + p->n_priv_per_thread / 4, key_extra_bits);
656
657 for (size_t k = 0; k < 4; k++) {
658 p->hash->get(key_read);
659 p->hash->get(key_read);
660 p->hash->get(key_read);
661 p->hash->get(key_read);
662 }
663 }
664 }
665
666 hash_check_inserted(p->hash, p->n_priv_per_thread, key_extra_bits);
667
668 hash_delete(p->hash, p->n_priv_per_thread, key_extra_bits);
669
670 hash_check_deleted(p->hash, p->n_priv_per_thread, key_extra_bits);
671 }
672
673 TEST_F(ut0lock_free_hash, multi_threaded_100r0w) {
674 run_multi_threaded(
675 "multi threaded, 100% read, 0% write, many keys" /* label */,
676 1024 * 32 /* initial hash size */, 4096 /* n_common */,
677 256 /* n_priv_per_thread */, 64 /* n_threads */,
678 thread_100r0w /* thr func */
679 );
680 }
681 TEST_F(ut0lock_free_hash, too_relaxed) {
682 /* Tests race conditions between writer (the main thread) and readers.
683 The writer puts T*CAPACITY elements into the hashmap, which has a smallest
684 possible capacity (2k) of a single node, to force frequent allocation of new
685 nodes. If the capacity was smaller than 2k, then the hashmap would keep
686 doubling the size of new nodes until it reached 2k. Also, there's a heuristic
687 to not double the size if the old node contained lots of deleted elements,
688 thus our writer will mark all inserted elements as deleted. The goal of all
689 this is to execute node allocation logic as often as possible, while the
690 readers are busy calling get(), which will fail with NOT_FOUND for the
691 duration of whole test, until the writer finally calls set(T*CAPACITY,42).
692 Note that due to the way get() implements probing, it takes linear time when
693 a node is full of elements (even deleted), thus these get()s are slow.
694 */
695 constexpr uint64_t CAPACITY = 2048;
696 constexpr uint64_t T = 10000;
697 constexpr uint64_t READERS = 10;
698 ut_lock_free_hash_t hash_table{CAPACITY, false};
699 const auto reading = [&]() {
700 while (hash_table.get(T * CAPACITY) == ut_lock_free_hash_t::NOT_FOUND) {
701 // whatever
702 }
703 };
704 std::vector<std::thread> readers;
705 for (uint64_t i = 0; i < READERS; ++i) {
706 readers.emplace_back(reading);
707 }
708 for (uint64_t i = 0; i < T; ++i) {
709 for (uint64_t j = 0; j < CAPACITY; ++j) {
710 hash_table.set(i * CAPACITY + j, 17);
711 hash_table.del(i * CAPACITY + j);
712 }
713 }
714 hash_table.set(T * CAPACITY, 42);
715 for (auto &reader : readers) {
716 reader.join();
717 }
718 }
719 } // namespace innodb_lock_free_hash_unittest