| 1 | /* Copyright (c) 2008, 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 | /** |
| 25 | @file |
| 26 | |
| 27 | Unit tests for lock-free algorithms of mysys |
| 28 | */ |
| 29 | |
| 30 | #include "my_config.h" |
| 31 | |
| 32 | #include <gtest/gtest.h> |
| 33 | #include <stddef.h> |
| 34 | #include <sys/types.h> |
| 35 | |
| 36 | #include "lf.h" |
| 37 | #include "my_byteorder.h" |
| 38 | #include "my_compiler.h" |
| 39 | #include "my_inttypes.h" |
| 40 | #include "my_sys.h" |
| 41 | #include "my_systime.h" |
| 42 | #include "my_thread.h" |
| 43 | #include "mysql/components/services/bits/my_thread_bits.h" |
| 44 | |
| 45 | namespace mysys_lf_unittest { |
| 46 | |
| 47 | #include "unittest/gunit/thr_template.cc" |
| 48 | |
| 49 | std::atomic<int32> inserts{0}; |
| 50 | std::atomic<int32> N{0}; |
| 51 | LF_ALLOCATOR lf_allocator; |
| 52 | LF_HASH lf_hash; |
| 53 | |
| 54 | int with_my_thread_init = 0; |
| 55 | |
| 56 | /* |
| 57 | pin allocator - alloc and release an element in a loop |
| 58 | */ |
| 59 | extern "C" void *test_lf_pinbox(void *arg) { |
| 60 | int m = *(int *)arg; |
| 61 | LF_PINS *pins; |
| 62 | |
| 63 | if (with_my_thread_init) my_thread_init(); |
| 64 | |
| 65 | pins = lf_pinbox_get_pins(&lf_allocator.pinbox); |
| 66 | |
| 67 | for (; m; m--) { |
| 68 | lf_pinbox_put_pins(pins); |
| 69 | pins = lf_pinbox_get_pins(&lf_allocator.pinbox); |
| 70 | } |
| 71 | lf_pinbox_put_pins(pins); |
| 72 | |
| 73 | if (with_my_thread_init) my_thread_end(); |
| 74 | |
| 75 | return nullptr; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | thread local data area, allocated using lf_alloc. |
| 80 | union is required to enforce the minimum required element size (sizeof(ptr)) |
| 81 | */ |
| 82 | union TLA { |
| 83 | std::atomic<int32> data; |
| 84 | void *not_used; |
| 85 | }; |
| 86 | |
| 87 | // SUPPRESS_UBSAN: integer overflow when generating random data. |
| 88 | extern "C" void *test_lf_alloc(void *arg) SUPPRESS_UBSAN; |
| 89 | extern "C" void *test_lf_alloc(void *arg) { |
| 90 | int m = (*(int *)arg) / 2; |
| 91 | int32 x, y = 0; |
| 92 | LF_PINS *pins; |
| 93 | |
| 94 | if (with_my_thread_init) my_thread_init(); |
| 95 | |
| 96 | pins = lf_pinbox_get_pins(&lf_allocator.pinbox); |
| 97 | |
| 98 | for (x = ((int)(intptr)(&m)); m; m--) { |
| 99 | TLA *node1, *node2; |
| 100 | x = (x * m + 0x87654321) & INT_MAX32; |
| 101 | node1 = (TLA *)lf_alloc_new(pins); |
| 102 | node1->data = x; |
| 103 | y += node1->data; |
| 104 | node1->data = 0; |
| 105 | node2 = (TLA *)lf_alloc_new(pins); |
| 106 | node2->data = x; |
| 107 | y -= node2->data; |
| 108 | node2->data = 0; |
| 109 | lf_pinbox_free(pins, node1); |
| 110 | lf_pinbox_free(pins, node2); |
| 111 | } |
| 112 | lf_pinbox_put_pins(pins); |
| 113 | |
| 114 | bad += y; |
| 115 | |
| 116 | if (--N == 0) { |
| 117 | #ifdef MY_LF_EXTRA_DEBUG |
| 118 | bad |= lf_allocator.mallocs - lf_alloc_pool_count(&lf_allocator); |
| 119 | #endif |
| 120 | } |
| 121 | |
| 122 | if (with_my_thread_init) my_thread_end(); |
| 123 | return nullptr; |
| 124 | } |
| 125 | |
| 126 | const int N_TLH = 1000; |
| 127 | extern "C" void *test_lf_hash(void *arg) { |
| 128 | int m = (*(int *)arg) / (2 * N_TLH); |
| 129 | int32 x, y, z, sum = 0, ins = 0; |
| 130 | LF_PINS *pins; |
| 131 | |
| 132 | if (with_my_thread_init) my_thread_init(); |
| 133 | |
| 134 | pins = lf_hash_get_pins(&lf_hash); |
| 135 | |
| 136 | for (x = ((int)(intptr)(&m)); m; m--) { |
| 137 | int i; |
| 138 | y = x; |
| 139 | for (i = 0; i < N_TLH; i++) { |
| 140 | x = (x * (m + i) + 0x87654321) & INT_MAX32; |
| 141 | z = (x < 0) ? -x : x; |
| 142 | if (lf_hash_insert(&lf_hash, pins, &z)) { |
| 143 | sum += z; |
| 144 | ins++; |
| 145 | } |
| 146 | } |
| 147 | for (i = 0; i < N_TLH; i++) { |
| 148 | y = (y * (m + i) + 0x87654321) & INT_MAX32; |
| 149 | z = (y < 0) ? -y : y; |
| 150 | if (lf_hash_delete(&lf_hash, pins, (uchar *)&z, sizeof(z))) sum -= z; |
| 151 | } |
| 152 | } |
| 153 | lf_hash_put_pins(pins); |
| 154 | |
| 155 | bad += sum; |
| 156 | inserts += ins; |
| 157 | |
| 158 | if (--N == 0) { |
| 159 | bad |= lf_hash.count; |
| 160 | } |
| 161 | |
| 162 | if (with_my_thread_init) my_thread_end(); |
| 163 | return nullptr; |
| 164 | } |
| 165 | |
| 166 | void do_tests() { |
| 167 | lf_alloc_init(&lf_allocator, sizeof(TLA), offsetof(TLA, not_used)); |
| 168 | lf_hash_init(&lf_hash, sizeof(int), LF_HASH_UNIQUE, 0, sizeof(int), nullptr, |
| 169 | &my_charset_bin); |
| 170 | |
| 171 | with_my_thread_init = 1; |
| 172 | test_concurrently("lf_pinbox (with my_thread_init)", test_lf_pinbox, |
| 173 | N = THREADS, CYCLES); |
| 174 | test_concurrently("lf_alloc (with my_thread_init)", test_lf_alloc, |
| 175 | N = THREADS, CYCLES); |
| 176 | test_concurrently("lf_hash (with my_thread_init)", test_lf_hash, N = THREADS, |
| 177 | CYCLES / 10); |
| 178 | |
| 179 | with_my_thread_init = 0; |
| 180 | test_concurrently("lf_pinbox (without my_thread_init)", test_lf_pinbox, |
| 181 | N = THREADS, CYCLES); |
| 182 | test_concurrently("lf_alloc (without my_thread_init)", test_lf_alloc, |
| 183 | N = THREADS, CYCLES); |
| 184 | test_concurrently("lf_hash (without my_thread_init)", test_lf_hash, |
| 185 | N = THREADS, CYCLES / 10); |
| 186 | |
| 187 | lf_hash_destroy(&lf_hash); |
| 188 | lf_alloc_destroy(&lf_allocator); |
| 189 | } |
| 190 | |
| 191 | TEST(Mysys, LockFree) { |
| 192 | my_thread_attr_init(&thr_attr); |
| 193 | my_thread_attr_setdetachstate(&thr_attr, MY_THREAD_CREATE_DETACHED); |
| 194 | |
| 195 | do_tests(); |
| 196 | |
| 197 | my_thread_attr_destroy(&thr_attr); |
| 198 | } |
| 199 | |
| 200 | extern "C" { |
| 201 | |
| 202 | static uint test_hash(const LF_HASH *, const uchar *key, size_t length) { |
| 203 | if (length < sizeof(uint32)) |
| 204 | return 0; |
| 205 | else { |
| 206 | /* We use ulongget() to avoid potential problems with alignment. */ |
| 207 | return ulongget(key); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | static int test_match(const uchar *arg, void *match_arg [[maybe_unused]]) { |
| 212 | /* |
| 213 | Unlike keys passed to hash function memory passed to match |
| 214 | functions are always correctly aligned. |
| 215 | */ |
| 216 | return *reinterpret_cast<const uint32 *>(arg) & 0x100; |
| 217 | } |
| 218 | |
| 219 | } /* extern "C" */ |
| 220 | |
| 221 | /** |
| 222 | Glass box test for lf_hash_random_match() function. |
| 223 | */ |
| 224 | TEST(Mysys, LFHashRandomMatch) { |
| 225 | LF_HASH hash; |
| 226 | LF_PINS *pins; |
| 227 | uint32 val, *fnd, *null_fnd = nullptr; |
| 228 | int rc; |
| 229 | |
| 230 | lf_hash_init2(&hash, sizeof(uint32), LF_HASH_UNIQUE, 0, sizeof(int), nullptr, |
| 231 | &my_charset_bin, &test_hash, nullptr, nullptr, nullptr); |
| 232 | /* Right after initialization hash is expected to be empty */ |
| 233 | EXPECT_EQ(0, hash.count.load()); |
| 234 | |
| 235 | pins = lf_hash_get_pins(&hash); |
| 236 | |
| 237 | /* We should not be able to find anything in empty hash. */ |
| 238 | fnd = static_cast<uint32 *>( |
| 239 | lf_hash_random_match(&hash, pins, &test_match, 0, nullptr)); |
| 240 | EXPECT_EQ(null_fnd, fnd); |
| 241 | lf_hash_search_unpin(pins); |
| 242 | |
| 243 | /* Insert a few non-matching values. */ |
| 244 | for (val = 0; val < 4; ++val) { |
| 245 | rc = lf_hash_insert(&hash, pins, &val); |
| 246 | EXPECT_EQ(0, rc); |
| 247 | } |
| 248 | EXPECT_EQ(4, hash.count.load()); |
| 249 | |
| 250 | /* Search still should return nothing. */ |
| 251 | fnd = static_cast<uint32 *>( |
| 252 | lf_hash_random_match(&hash, pins, &test_match, 0, nullptr)); |
| 253 | EXPECT_EQ(null_fnd, fnd); |
| 254 | lf_hash_search_unpin(pins); |
| 255 | |
| 256 | /* Even if we start from different bucket/hash value. */ |
| 257 | fnd = static_cast<uint32 *>( |
| 258 | lf_hash_random_match(&hash, pins, &test_match, 3, nullptr)); |
| 259 | EXPECT_EQ(null_fnd, fnd); |
| 260 | lf_hash_search_unpin(pins); |
| 261 | |
| 262 | /* Insert matching record which also will be last in split-ordered list. */ |
| 263 | val = 3 + 0x100; |
| 264 | rc = lf_hash_insert(&hash, pins, &val); |
| 265 | EXPECT_EQ(0, rc); |
| 266 | |
| 267 | /* |
| 268 | We should be able to find this record when we start searching |
| 269 | from bucket #0. |
| 270 | */ |
| 271 | fnd = static_cast<uint32 *>( |
| 272 | lf_hash_random_match(&hash, pins, &test_match, 0, nullptr)); |
| 273 | EXPECT_NE(null_fnd, fnd); |
| 274 | EXPECT_EQ(val, *fnd); |
| 275 | lf_hash_search_unpin(pins); |
| 276 | |
| 277 | /* |
| 278 | Also when searching from buckets #2 and #5, which don't have dummy nodes and |
| 279 | lists associated yet. |
| 280 | */ |
| 281 | fnd = static_cast<uint32 *>( |
| 282 | lf_hash_random_match(&hash, pins, &test_match, 2, nullptr)); |
| 283 | EXPECT_NE(null_fnd, fnd); |
| 284 | EXPECT_EQ(val, *fnd); |
| 285 | lf_hash_search_unpin(pins); |
| 286 | fnd = static_cast<uint *>( |
| 287 | lf_hash_random_match(&hash, pins, &test_match, 5, nullptr)); |
| 288 | EXPECT_NE(null_fnd, fnd); |
| 289 | EXPECT_EQ(val, *fnd); |
| 290 | lf_hash_search_unpin(pins); |
| 291 | |
| 292 | /* Naturally, it should also be reachable from its native bucket #3. */ |
| 293 | fnd = static_cast<uint32 *>( |
| 294 | lf_hash_random_match(&hash, pins, &test_match, 3, nullptr)); |
| 295 | EXPECT_NE(null_fnd, fnd); |
| 296 | EXPECT_EQ(val, *fnd); |
| 297 | lf_hash_search_unpin(pins); |
| 298 | |
| 299 | /* |
| 300 | If random value provided identifies element which should reside later |
| 301 | than the matching element in split-ordered list it should NOT be normally |
| 302 | reachable even if random value corresponds to the same bucket (i.e. #3). |
| 303 | This allows to avoid bias towards elements at the start of the bucket list. |
| 304 | |
| 305 | But in this particular case it will be reachable because we have only one |
| 306 | matching element in the hash and we restart our search from the head of |
| 307 | split-ordered list if no matching elements were found at the tail. |
| 308 | Such restart is necessary to avoid bias against elements at the start of |
| 309 | the split-ordered list. |
| 310 | */ |
| 311 | fnd = static_cast<uint32 *>(lf_hash_random_match(&hash, pins, &test_match, |
| 312 | 3 + 0x10 + 0x100, nullptr)); |
| 313 | EXPECT_NE(null_fnd, fnd); |
| 314 | EXPECT_EQ(val, *fnd); |
| 315 | lf_hash_search_unpin(pins); |
| 316 | |
| 317 | /* Let us add non-matching record to the end of the split-ordered list. */ |
| 318 | val = 7; |
| 319 | rc = lf_hash_insert(&hash, pins, &val); |
| 320 | EXPECT_EQ(0, rc); |
| 321 | |
| 322 | /* |
| 323 | Our matching record will be reachable from its bucket #7 as well, |
| 324 | since our search wraps around from the tail of the list to its head. |
| 325 | */ |
| 326 | fnd = static_cast<uint32 *>( |
| 327 | lf_hash_random_match(&hash, pins, &test_match, 7, nullptr)); |
| 328 | EXPECT_NE(null_fnd, fnd); |
| 329 | val = 3 + 0x100; |
| 330 | EXPECT_EQ(val, *fnd); |
| 331 | lf_hash_search_unpin(pins); |
| 332 | |
| 333 | /* Now let us add one more matching record to bucket #4. */ |
| 334 | val = 4 + 0x100; |
| 335 | rc = lf_hash_insert(&hash, pins, &val); |
| 336 | EXPECT_EQ(0, rc); |
| 337 | |
| 338 | /* This record should be reachable from buckets #0 and #4. */ |
| 339 | fnd = static_cast<uint32 *>( |
| 340 | lf_hash_random_match(&hash, pins, &test_match, 0, nullptr)); |
| 341 | EXPECT_NE(null_fnd, fnd); |
| 342 | EXPECT_EQ(val, *fnd); |
| 343 | lf_hash_search_unpin(pins); |
| 344 | |
| 345 | fnd = static_cast<uint32 *>( |
| 346 | lf_hash_random_match(&hash, pins, &test_match, 4, nullptr)); |
| 347 | EXPECT_NE(null_fnd, fnd); |
| 348 | EXPECT_EQ(val, *fnd); |
| 349 | lf_hash_search_unpin(pins); |
| 350 | |
| 351 | /* But not from bucket #3. We will find another record instead. */ |
| 352 | fnd = static_cast<uint32 *>( |
| 353 | lf_hash_random_match(&hash, pins, &test_match, 3, nullptr)); |
| 354 | EXPECT_NE(null_fnd, fnd); |
| 355 | EXPECT_NE(val, *fnd); |
| 356 | lf_hash_search_unpin(pins); |
| 357 | |
| 358 | /* |
| 359 | Thanks to search wrapping around from tail to the head of the list, |
| 360 | our record will be reachable from bucket #7. |
| 361 | */ |
| 362 | fnd = static_cast<uint32 *>( |
| 363 | lf_hash_random_match(&hash, pins, &test_match, 7, nullptr)); |
| 364 | EXPECT_NE(null_fnd, fnd); |
| 365 | EXPECT_EQ(val, *fnd); |
| 366 | lf_hash_search_unpin(pins); |
| 367 | |
| 368 | /* |
| 369 | To avoid bias towards elements at the start of the bucket list this record |
| 370 | should not be reachable even from its native bucket #4, if random number |
| 371 | identifies higher element. Instead we should get another matching element. |
| 372 | */ |
| 373 | fnd = static_cast<uint32 *>(lf_hash_random_match(&hash, pins, &test_match, |
| 374 | 4 + 0x10 + 0x100, nullptr)); |
| 375 | EXPECT_NE(null_fnd, fnd); |
| 376 | EXPECT_NE(val, *fnd); |
| 377 | lf_hash_search_unpin(pins); |
| 378 | |
| 379 | lf_hash_put_pins(pins); |
| 380 | lf_hash_destroy(&hash); |
| 381 | } |
| 382 | } // namespace mysys_lf_unittest |