| 1 |
// slotstate_test.go - unit tests, concurrency tests, and microbenchmarks for slotState |
| 2 |
// |
| 3 |
// (c) 2024 Sudhi Herle <[email protected]> |
| 4 |
// |
| 5 |
// Copyright 2024- Sudhi Herle <sw-at-herle-dot-net> |
| 6 |
// License: BSD-2-Clause |
| 7 |
// |
| 8 |
// If you need a commercial license for this work, please contact |
| 9 |
// the author. |
| 10 |
// |
| 11 |
// This software does not come with any express or implied |
| 12 |
// warranty; it is provided "as is". No claim is made to its |
| 13 |
// suitability for any purpose. |
| 14 |
|
| 15 |
package sieve |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"math/rand" |
| 20 |
"runtime" |
| 21 |
"sync" |
| 22 |
"sync/atomic" |
| 23 |
"testing" |
| 24 |
) |
| 25 |
|
| 26 |
// ========================================================================= |
| 27 |
// Construction |
| 28 |
// ========================================================================= |
| 29 |
|
| 30 |
func TestSlotState_NewK1(t *testing.T) { |
| 31 |
ss := newSlotState(100, 1) |
| 32 |
if ss.vbits != 1 { |
| 33 |
t.Fatalf("vbits=%d, want 1", ss.vbits) |
| 34 |
} |
| 35 |
if ss.vmask != 1 { |
| 36 |
t.Fatalf("vmask=%d, want 1", ss.vmask) |
| 37 |
} |
| 38 |
if ss.maxVisit != 1 { |
| 39 |
t.Fatalf("maxVisit=%d, want 1", ss.maxVisit) |
| 40 |
} |
| 41 |
if len(ss.words) != 100 { |
| 42 |
t.Fatalf("len(words)=%d, want 100", len(ss.words)) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func TestSlotState_NewKValues(t *testing.T) { |
| 47 |
tests := []struct { |
| 48 |
k int |
| 49 |
wantBits uint |
| 50 |
wantMask uint64 |
| 51 |
wantMax uint64 |
| 52 |
}{ |
| 53 |
{1, 1, 1, 1}, |
| 54 |
{2, 2, 3, 2}, // 2 bits, mask=3, but saturates at 2 not 3 |
| 55 |
{3, 2, 3, 3}, |
| 56 |
{4, 3, 7, 4}, |
| 57 |
{7, 3, 7, 7}, |
| 58 |
{8, 4, 15, 8}, |
| 59 |
{15, 4, 15, 15}, |
| 60 |
{63, 6, 63, 63}, |
| 61 |
} |
| 62 |
|
| 63 |
for _, tt := range tests { |
| 64 |
t.Run(fmt.Sprintf("k=%d", tt.k), func(t *testing.T) { |
| 65 |
ss := newSlotState(4, tt.k) |
| 66 |
if ss.vbits != tt.wantBits { |
| 67 |
t.Errorf("vbits=%d, want %d", ss.vbits, tt.wantBits) |
| 68 |
} |
| 69 |
if ss.vmask != tt.wantMask { |
| 70 |
t.Errorf("vmask=%d, want %d", ss.vmask, tt.wantMask) |
| 71 |
} |
| 72 |
if ss.maxVisit != tt.wantMax { |
| 73 |
t.Errorf("maxVisit=%d, want %d", ss.maxVisit, tt.wantMax) |
| 74 |
} |
| 75 |
}) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
func TestSlotState_NewKClamped(t *testing.T) { |
| 80 |
for _, k := range []int{0, -1, -100} { |
| 81 |
ss := newSlotState(8, k) |
| 82 |
if ss.maxVisit != 1 { |
| 83 |
t.Fatalf("k=%d: maxVisit=%d, want 1 (clamped)", k, ss.maxVisit) |
| 84 |
} |
| 85 |
if ss.vbits != 1 { |
| 86 |
t.Fatalf("k=%d: vbits=%d, want 1", k, ss.vbits) |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// ========================================================================= |
| 92 |
// Lock / Unlock basics |
| 93 |
// ========================================================================= |
| 94 |
|
| 95 |
func TestSlotState_LockUnlock(t *testing.T) { |
| 96 |
ss := newSlotState(4, 1) |
| 97 |
|
| 98 |
ss.Lock(0) |
| 99 |
w := atomic.LoadUint64(&ss.words[0]) |
| 100 |
if w&_LockBit == 0 { |
| 101 |
t.Fatal("Lock: lock bit not set") |
| 102 |
} |
| 103 |
|
| 104 |
ss.Unlock(0) |
| 105 |
w = atomic.LoadUint64(&ss.words[0]) |
| 106 |
if w&_LockBit != 0 { |
| 107 |
t.Fatal("Unlock: lock bit still set") |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func TestSlotState_Lock_DoesNotAffectVisited(t *testing.T) { |
| 112 |
ss := newSlotState(4, 3) |
| 113 |
|
| 114 |
// Mark to counter=2 |
| 115 |
ss.LockAndMark(0) |
| 116 |
ss.Unlock(0) |
| 117 |
ss.LockAndMark(0) |
| 118 |
ss.Unlock(0) |
| 119 |
|
| 120 |
// Plain Lock/Unlock should not change the counter |
| 121 |
ss.Lock(0) |
| 122 |
ss.Unlock(0) |
| 123 |
|
| 124 |
w := atomic.LoadUint64(&ss.words[0]) |
| 125 |
counter := w & ss.vmask |
| 126 |
if counter != 2 { |
| 127 |
t.Fatalf("Lock/Unlock changed counter: got %d, want 2", counter) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// ========================================================================= |
| 132 |
// LockAndMark |
| 133 |
// ========================================================================= |
| 134 |
|
| 135 |
func TestSlotState_LockAndMark_K1(t *testing.T) { |
| 136 |
ss := newSlotState(4, 1) |
| 137 |
|
| 138 |
if ss.IsVisited(0) { |
| 139 |
t.Fatal("should not be visited initially") |
| 140 |
} |
| 141 |
|
| 142 |
ss.LockAndMark(0) |
| 143 |
|
| 144 |
// While locked: both bits set |
| 145 |
w := atomic.LoadUint64(&ss.words[0]) |
| 146 |
if w&_LockBit == 0 { |
| 147 |
t.Fatal("lock bit not set") |
| 148 |
} |
| 149 |
if w&1 == 0 { |
| 150 |
t.Fatal("visited bit not set") |
| 151 |
} |
| 152 |
|
| 153 |
ss.Unlock(0) |
| 154 |
|
| 155 |
// After unlock: visited preserved, lock cleared |
| 156 |
if !ss.IsVisited(0) { |
| 157 |
t.Fatal("Unlock should preserve visited bit") |
| 158 |
} |
| 159 |
w = atomic.LoadUint64(&ss.words[0]) |
| 160 |
if w&_LockBit != 0 { |
| 161 |
t.Fatal("Unlock should clear lock bit") |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func TestSlotState_LockAndMark_K1_Idempotent(t *testing.T) { |
| 166 |
ss := newSlotState(4, 1) |
| 167 |
|
| 168 |
// Multiple marks on k=1: bit stays 1 |
| 169 |
for i := 0; i < 20; i++ { |
| 170 |
ss.LockAndMark(0) |
| 171 |
ss.Unlock(0) |
| 172 |
} |
| 173 |
|
| 174 |
w := atomic.LoadUint64(&ss.words[0]) |
| 175 |
if w != 1 { // visited=1, lock=0 |
| 176 |
t.Fatalf("k=1 after 20 marks: word=%#x, want 0x1", w) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
func TestSlotState_LockAndMark_K3_Saturation(t *testing.T) { |
| 181 |
ss := newSlotState(4, 3) |
| 182 |
|
| 183 |
// Increment 10 times, should saturate at 3 |
| 184 |
for i := 0; i < 10; i++ { |
| 185 |
ss.LockAndMark(0) |
| 186 |
ss.Unlock(0) |
| 187 |
} |
| 188 |
|
| 189 |
w := atomic.LoadUint64(&ss.words[0]) |
| 190 |
counter := w & ss.vmask |
| 191 |
if counter != 3 { |
| 192 |
t.Fatalf("k=3 after 10 marks: counter=%d, want 3", counter) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
// K=2 uses 2-bit field (vmask=3) but must saturate at 2, not 3. |
| 197 |
func TestSlotState_LockAndMark_K2_SaturatesAtMaxVisitNotMask(t *testing.T) { |
| 198 |
ss := newSlotState(4, 2) |
| 199 |
|
| 200 |
for i := 0; i < 20; i++ { |
| 201 |
ss.LockAndMark(0) |
| 202 |
ss.Unlock(0) |
| 203 |
} |
| 204 |
|
| 205 |
w := atomic.LoadUint64(&ss.words[0]) |
| 206 |
counter := w & ss.vmask |
| 207 |
if counter != 2 { |
| 208 |
t.Fatalf("k=2: counter=%d, want 2 (maxVisit, not vmask=%d)", counter, ss.vmask) |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
func TestSlotState_LockAndMark_K7_StepByStep(t *testing.T) { |
| 213 |
ss := newSlotState(4, 7) |
| 214 |
|
| 215 |
for want := uint64(1); want <= 7; want++ { |
| 216 |
ss.LockAndMark(0) |
| 217 |
ss.Unlock(0) |
| 218 |
w := atomic.LoadUint64(&ss.words[0]) |
| 219 |
got := w & ss.vmask |
| 220 |
if got != want { |
| 221 |
t.Fatalf("after %d marks: counter=%d, want %d", want, got, want) |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
// One more: should stay at 7 |
| 226 |
ss.LockAndMark(0) |
| 227 |
ss.Unlock(0) |
| 228 |
w := atomic.LoadUint64(&ss.words[0]) |
| 229 |
if w&ss.vmask != 7 { |
| 230 |
t.Fatalf("after saturation: counter=%d, want 7", w&ss.vmask) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
// ========================================================================= |
| 235 |
// IsVisited |
| 236 |
// ========================================================================= |
| 237 |
|
| 238 |
func TestSlotState_IsVisited(t *testing.T) { |
| 239 |
ss := newSlotState(4, 3) |
| 240 |
|
| 241 |
if ss.IsVisited(0) { |
| 242 |
t.Fatal("should not be visited initially") |
| 243 |
} |
| 244 |
|
| 245 |
ss.LockAndMark(0) |
| 246 |
ss.Unlock(0) |
| 247 |
if !ss.IsVisited(0) { |
| 248 |
t.Fatal("should be visited after mark") |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
// ========================================================================= |
| 253 |
// Clear |
| 254 |
// ========================================================================= |
| 255 |
|
| 256 |
func TestSlotState_Clear_K1(t *testing.T) { |
| 257 |
ss := newSlotState(4, 1) |
| 258 |
|
| 259 |
ss.LockAndMark(0) |
| 260 |
ss.Unlock(0) |
| 261 |
if !ss.IsVisited(0) { |
| 262 |
t.Fatal("expected visited after mark") |
| 263 |
} |
| 264 |
|
| 265 |
ss.Clear(0) |
| 266 |
if ss.IsVisited(0) { |
| 267 |
t.Fatal("expected not visited after clear") |
| 268 |
} |
| 269 |
|
| 270 |
// Word should be fully zero |
| 271 |
w := atomic.LoadUint64(&ss.words[0]) |
| 272 |
if w != 0 { |
| 273 |
t.Fatalf("word=%#x after clear, want 0", w) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
func TestSlotState_Clear_K3_DecrementsByOne(t *testing.T) { |
| 278 |
ss := newSlotState(4, 3) |
| 279 |
|
| 280 |
// Saturate at 3 |
| 281 |
for i := 0; i < 5; i++ { |
| 282 |
ss.LockAndMark(0) |
| 283 |
ss.Unlock(0) |
| 284 |
} |
| 285 |
|
| 286 |
for want := uint64(2); ; want-- { |
| 287 |
ss.Clear(0) |
| 288 |
w := atomic.LoadUint64(&ss.words[0]) |
| 289 |
got := w & ss.vmask |
| 290 |
if got != want { |
| 291 |
t.Fatalf("Clear: counter=%d, want %d", got, want) |
| 292 |
} |
| 293 |
if !ss.IsVisited(0) == (want > 0) { |
| 294 |
t.Fatalf("Clear: IsVisited=%v, want %v", ss.IsVisited(0), want > 0) |
| 295 |
} |
| 296 |
if want == 0 { |
| 297 |
break |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
func TestSlotState_Clear_AlreadyZero(t *testing.T) { |
| 303 |
ss := newSlotState(4, 3) |
| 304 |
|
| 305 |
// Clear on zero counter: no-op |
| 306 |
ss.Clear(0) |
| 307 |
w := atomic.LoadUint64(&ss.words[0]) |
| 308 |
if w != 0 { |
| 309 |
t.Fatalf("Clear on zero: word=%#x, want 0", w) |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
// Clear uses CAS; verify it works while the lock bit is held by someone else. |
| 314 |
// The CAS includes the lock bit in the expected value, so it will succeed |
| 315 |
// as long as the lock state doesn't change between Load and CAS. |
| 316 |
func TestSlotState_Clear_WhileLocked(t *testing.T) { |
| 317 |
ss := newSlotState(4, 3) |
| 318 |
|
| 319 |
// Mark to 3 |
| 320 |
for i := 0; i < 3; i++ { |
| 321 |
ss.LockAndMark(0) |
| 322 |
ss.Unlock(0) |
| 323 |
} |
| 324 |
|
| 325 |
// Hold the lock, Clear from another goroutine |
| 326 |
ss.Lock(0) |
| 327 |
|
| 328 |
done := make(chan struct{}) |
| 329 |
go func() { |
| 330 |
ss.Clear(0) // CAS on _LockBit|3 → _LockBit|2 |
| 331 |
close(done) |
| 332 |
}() |
| 333 |
|
| 334 |
// Give the Clear goroutine time to execute |
| 335 |
runtime.Gosched() |
| 336 |
runtime.Gosched() |
| 337 |
|
| 338 |
ss.Unlock(0) |
| 339 |
<-done |
| 340 |
|
| 341 |
w := atomic.LoadUint64(&ss.words[0]) |
| 342 |
counter := w & ss.vmask |
| 343 |
if counter != 2 { |
| 344 |
t.Fatalf("Clear while locked: counter=%d, want 2", counter) |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
// ========================================================================= |
| 349 |
// LockAndReset |
| 350 |
// ========================================================================= |
| 351 |
|
| 352 |
func TestSlotState_LockAndReset(t *testing.T) { |
| 353 |
ss := newSlotState(4, 3) |
| 354 |
|
| 355 |
// Mark to 3 |
| 356 |
for i := 0; i < 5; i++ { |
| 357 |
ss.LockAndMark(0) |
| 358 |
ss.Unlock(0) |
| 359 |
} |
| 360 |
if !ss.IsVisited(0) { |
| 361 |
t.Fatal("should be visited before reset") |
| 362 |
} |
| 363 |
|
| 364 |
ss.LockAndReset(0) |
| 365 |
|
| 366 |
// Lock held, visited cleared |
| 367 |
w := atomic.LoadUint64(&ss.words[0]) |
| 368 |
if w != _LockBit { |
| 369 |
t.Fatalf("LockAndReset: word=%#x, want %#x (lock bit only)", w, _LockBit) |
| 370 |
} |
| 371 |
|
| 372 |
ss.Unlock(0) |
| 373 |
|
| 374 |
if ss.IsVisited(0) { |
| 375 |
t.Fatal("visited should be cleared after LockAndReset") |
| 376 |
} |
| 377 |
w = atomic.LoadUint64(&ss.words[0]) |
| 378 |
if w != 0 { |
| 379 |
t.Fatalf("after Unlock: word=%#x, want 0", w) |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
// LockAndReset must wait if the lock is already held. |
| 384 |
func TestSlotState_LockAndReset_WaitsForLock(t *testing.T) { |
| 385 |
ss := newSlotState(4, 1) |
| 386 |
|
| 387 |
ss.LockAndMark(0) |
| 388 |
// Lock is held. LockAndReset in another goroutine must block. |
| 389 |
|
| 390 |
var resetDone atomic.Bool |
| 391 |
go func() { |
| 392 |
ss.LockAndReset(0) |
| 393 |
resetDone.Store(true) |
| 394 |
ss.Unlock(0) |
| 395 |
}() |
| 396 |
|
| 397 |
// Yield a few times — reset goroutine should still be spinning |
| 398 |
for i := 0; i < 10; i++ { |
| 399 |
runtime.Gosched() |
| 400 |
} |
| 401 |
if resetDone.Load() { |
| 402 |
t.Fatal("LockAndReset returned while lock was held") |
| 403 |
} |
| 404 |
|
| 405 |
// Release the lock |
| 406 |
ss.Unlock(0) |
| 407 |
|
| 408 |
// Wait for the reset goroutine |
| 409 |
for i := 0; i < 1000; i++ { |
| 410 |
if resetDone.Load() { |
| 411 |
break |
| 412 |
} |
| 413 |
runtime.Gosched() |
| 414 |
} |
| 415 |
if !resetDone.Load() { |
| 416 |
t.Fatal("LockAndReset never completed") |
| 417 |
} |
| 418 |
|
| 419 |
// After LockAndReset + Unlock: visited should be cleared |
| 420 |
if ss.IsVisited(0) { |
| 421 |
t.Fatal("visited should be cleared after LockAndReset") |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
// ========================================================================= |
| 426 |
// Reset / ResetAll |
| 427 |
// ========================================================================= |
| 428 |
|
| 429 |
func TestSlotState_Reset(t *testing.T) { |
| 430 |
ss := newSlotState(4, 3) |
| 431 |
|
| 432 |
ss.LockAndMark(0) |
| 433 |
ss.Unlock(0) |
| 434 |
|
| 435 |
ss.Reset(0) |
| 436 |
w := atomic.LoadUint64(&ss.words[0]) |
| 437 |
if w != 0 { |
| 438 |
t.Fatalf("Reset: word=%#x, want 0", w) |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
func TestSlotState_ResetAll(t *testing.T) { |
| 443 |
ss := newSlotState(8, 3) |
| 444 |
|
| 445 |
for i := int32(0); i < 8; i++ { |
| 446 |
ss.LockAndMark(i) |
| 447 |
ss.Unlock(i) |
| 448 |
} |
| 449 |
|
| 450 |
ss.ResetAll() |
| 451 |
|
| 452 |
for i := int32(0); i < 8; i++ { |
| 453 |
w := atomic.LoadUint64(&ss.words[i]) |
| 454 |
if w != 0 { |
| 455 |
t.Fatalf("ResetAll: word[%d]=%#x, want 0", i, w) |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
// ========================================================================= |
| 461 |
// Slot independence |
| 462 |
// ========================================================================= |
| 463 |
|
| 464 |
func TestSlotState_SlotsAreIndependent(t *testing.T) { |
| 465 |
ss := newSlotState(4, 3) |
| 466 |
|
| 467 |
// Mark slot 0 to saturation |
| 468 |
for i := 0; i < 5; i++ { |
| 469 |
ss.LockAndMark(0) |
| 470 |
ss.Unlock(0) |
| 471 |
} |
| 472 |
|
| 473 |
// Mark slot 1 once |
| 474 |
ss.LockAndMark(1) |
| 475 |
ss.Unlock(1) |
| 476 |
|
| 477 |
// Slot 2 and 3 untouched |
| 478 |
if ss.IsVisited(2) || ss.IsVisited(3) { |
| 479 |
t.Fatal("untouched slots should not be visited") |
| 480 |
} |
| 481 |
|
| 482 |
w0 := atomic.LoadUint64(&ss.words[0]) |
| 483 |
w1 := atomic.LoadUint64(&ss.words[1]) |
| 484 |
if w0&ss.vmask != 3 { |
| 485 |
t.Fatalf("slot 0: counter=%d, want 3", w0&ss.vmask) |
| 486 |
} |
| 487 |
if w1&ss.vmask != 1 { |
| 488 |
t.Fatalf("slot 1: counter=%d, want 1", w1&ss.vmask) |
| 489 |
} |
| 490 |
|
| 491 |
// Clear slot 0; slot 1 unaffected |
| 492 |
ss.Clear(0) |
| 493 |
w1 = atomic.LoadUint64(&ss.words[1]) |
| 494 |
if w1&ss.vmask != 1 { |
| 495 |
t.Fatalf("slot 1 changed after clearing slot 0: counter=%d", w1&ss.vmask) |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
// ========================================================================= |
| 500 |
// Concurrency correctness tests |
| 501 |
// ========================================================================= |
| 502 |
|
| 503 |
// Verify that Lock provides mutual exclusion using a shared counter. |
| 504 |
func TestSlotState_MutualExclusion_Lock(t *testing.T) { |
| 505 |
ss := newSlotState(4, 1) |
| 506 |
const goroutines = 50 |
| 507 |
const iters = 10000 |
| 508 |
|
| 509 |
var counter int64 |
| 510 |
var wg sync.WaitGroup |
| 511 |
wg.Add(goroutines) |
| 512 |
for g := 0; g < goroutines; g++ { |
| 513 |
go func() { |
| 514 |
defer wg.Done() |
| 515 |
for i := 0; i < iters; i++ { |
| 516 |
ss.Lock(0) |
| 517 |
c := counter |
| 518 |
c++ |
| 519 |
counter = c |
| 520 |
ss.Unlock(0) |
| 521 |
} |
| 522 |
}() |
| 523 |
} |
| 524 |
wg.Wait() |
| 525 |
|
| 526 |
want := int64(goroutines * iters) |
| 527 |
if counter != want { |
| 528 |
t.Fatalf("mutual exclusion violated: counter=%d, want %d", counter, want) |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
// Verify that LockAndMark provides mutual exclusion. |
| 533 |
func TestSlotState_MutualExclusion_LockAndMark(t *testing.T) { |
| 534 |
ss := newSlotState(4, 1) |
| 535 |
const goroutines = 50 |
| 536 |
const iters = 10000 |
| 537 |
|
| 538 |
var counter int64 |
| 539 |
var wg sync.WaitGroup |
| 540 |
wg.Add(goroutines) |
| 541 |
for g := 0; g < goroutines; g++ { |
| 542 |
go func() { |
| 543 |
defer wg.Done() |
| 544 |
for i := 0; i < iters; i++ { |
| 545 |
ss.LockAndMark(0) |
| 546 |
c := counter |
| 547 |
c++ |
| 548 |
counter = c |
| 549 |
ss.Unlock(0) |
| 550 |
} |
| 551 |
}() |
| 552 |
} |
| 553 |
wg.Wait() |
| 554 |
|
| 555 |
want := int64(goroutines * iters) |
| 556 |
if counter != want { |
| 557 |
t.Fatalf("mutual exclusion violated: counter=%d, want %d", counter, want) |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
// Verify that LockAndMark k>1 provides mutual exclusion. The k>1 path |
| 562 |
// uses a CAS loop for the counter increment; verify it doesn't break |
| 563 |
// the lock's exclusion guarantee. |
| 564 |
func TestSlotState_MutualExclusion_LockAndMark_K3(t *testing.T) { |
| 565 |
ss := newSlotState(4, 3) |
| 566 |
const goroutines = 50 |
| 567 |
const iters = 10000 |
| 568 |
|
| 569 |
var counter int64 |
| 570 |
var wg sync.WaitGroup |
| 571 |
wg.Add(goroutines) |
| 572 |
for g := 0; g < goroutines; g++ { |
| 573 |
go func() { |
| 574 |
defer wg.Done() |
| 575 |
for i := 0; i < iters; i++ { |
| 576 |
ss.LockAndMark(0) |
| 577 |
c := counter |
| 578 |
c++ |
| 579 |
counter = c |
| 580 |
ss.Unlock(0) |
| 581 |
} |
| 582 |
}() |
| 583 |
} |
| 584 |
wg.Wait() |
| 585 |
|
| 586 |
want := int64(goroutines * iters) |
| 587 |
if counter != want { |
| 588 |
t.Fatalf("mutual exclusion violated: counter=%d, want %d", counter, want) |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
// Concurrent LockAndMark on the same slot: final state must have |
| 593 |
// lock cleared and counter at the correct saturation value. |
| 594 |
func TestSlotState_ConcurrentMark_K1(t *testing.T) { |
| 595 |
ss := newSlotState(4, 1) |
| 596 |
const goroutines = 100 |
| 597 |
const iters = 5000 |
| 598 |
|
| 599 |
var wg sync.WaitGroup |
| 600 |
wg.Add(goroutines) |
| 601 |
for g := 0; g < goroutines; g++ { |
| 602 |
go func() { |
| 603 |
defer wg.Done() |
| 604 |
for i := 0; i < iters; i++ { |
| 605 |
ss.LockAndMark(0) |
| 606 |
ss.Unlock(0) |
| 607 |
} |
| 608 |
}() |
| 609 |
} |
| 610 |
wg.Wait() |
| 611 |
|
| 612 |
w := atomic.LoadUint64(&ss.words[0]) |
| 613 |
if w&_LockBit != 0 { |
| 614 |
t.Fatal("lock bit set after all goroutines finished") |
| 615 |
} |
| 616 |
if w&ss.vmask != 1 { |
| 617 |
t.Fatalf("k=1: counter=%d, want 1", w&ss.vmask) |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
func TestSlotState_ConcurrentMark_K3(t *testing.T) { |
| 622 |
ss := newSlotState(4, 3) |
| 623 |
const goroutines = 100 |
| 624 |
const iters = 5000 |
| 625 |
|
| 626 |
var wg sync.WaitGroup |
| 627 |
wg.Add(goroutines) |
| 628 |
for g := 0; g < goroutines; g++ { |
| 629 |
go func() { |
| 630 |
defer wg.Done() |
| 631 |
for i := 0; i < iters; i++ { |
| 632 |
ss.LockAndMark(0) |
| 633 |
ss.Unlock(0) |
| 634 |
} |
| 635 |
}() |
| 636 |
} |
| 637 |
wg.Wait() |
| 638 |
|
| 639 |
w := atomic.LoadUint64(&ss.words[0]) |
| 640 |
if w&_LockBit != 0 { |
| 641 |
t.Fatal("lock bit set after all goroutines finished") |
| 642 |
} |
| 643 |
if w&ss.vmask != 3 { |
| 644 |
t.Fatalf("k=3: counter=%d, want 3", w&ss.vmask) |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
// LockAndMark racing with Clear on the same slot. This exercises: |
| 649 |
// - The CAS loop in Clear (fails when LockAndMark flips the lock bit) |
| 650 |
// - The CAS loop in LockAndMark k>1 (fails when Clear decrements the counter) |
| 651 |
func TestSlotState_ConcurrentMarkAndClear_K3(t *testing.T) { |
| 652 |
ss := newSlotState(4, 3) |
| 653 |
const goroutines = 20 |
| 654 |
const iters = 10000 |
| 655 |
|
| 656 |
var wg sync.WaitGroup |
| 657 |
|
| 658 |
// Markers |
| 659 |
wg.Add(goroutines) |
| 660 |
for g := 0; g < goroutines; g++ { |
| 661 |
go func() { |
| 662 |
defer wg.Done() |
| 663 |
for i := 0; i < iters; i++ { |
| 664 |
ss.LockAndMark(0) |
| 665 |
ss.Unlock(0) |
| 666 |
} |
| 667 |
}() |
| 668 |
} |
| 669 |
|
| 670 |
// Clearers (as eviction would do) |
| 671 |
wg.Add(goroutines / 2) |
| 672 |
for g := 0; g < goroutines/2; g++ { |
| 673 |
go func() { |
| 674 |
defer wg.Done() |
| 675 |
for i := 0; i < iters; i++ { |
| 676 |
ss.Clear(0) |
| 677 |
} |
| 678 |
}() |
| 679 |
} |
| 680 |
|
| 681 |
wg.Wait() |
| 682 |
|
| 683 |
w := atomic.LoadUint64(&ss.words[0]) |
| 684 |
if w&_LockBit != 0 { |
| 685 |
t.Fatal("lock bit set after all goroutines finished") |
| 686 |
} |
| 687 |
counter := w & ss.vmask |
| 688 |
if counter > ss.maxVisit { |
| 689 |
t.Fatalf("counter=%d exceeds maxVisit=%d", counter, ss.maxVisit) |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
// LockAndReset racing with LockAndMark. This is the newNode() + Get() race: |
| 694 |
// a stale Get holds the lock while newNode calls LockAndReset. |
| 695 |
func TestSlotState_ConcurrentLockAndReset_WithMark(t *testing.T) { |
| 696 |
ss := newSlotState(4, 3) |
| 697 |
const goroutines = 20 |
| 698 |
const iters = 5000 |
| 699 |
|
| 700 |
var wg sync.WaitGroup |
| 701 |
|
| 702 |
// Markers (simulating Get hot path) |
| 703 |
wg.Add(goroutines) |
| 704 |
for g := 0; g < goroutines; g++ { |
| 705 |
go func() { |
| 706 |
defer wg.Done() |
| 707 |
for i := 0; i < iters; i++ { |
| 708 |
ss.LockAndMark(0) |
| 709 |
ss.Unlock(0) |
| 710 |
} |
| 711 |
}() |
| 712 |
} |
| 713 |
|
| 714 |
// Resetters (simulating newNode) |
| 715 |
resetCount := goroutines / 4 |
| 716 |
wg.Add(resetCount) |
| 717 |
for g := 0; g < resetCount; g++ { |
| 718 |
go func() { |
| 719 |
defer wg.Done() |
| 720 |
for i := 0; i < iters/10; i++ { |
| 721 |
ss.LockAndReset(0) |
| 722 |
// While holding lock after reset: word must be _LockBit only |
| 723 |
w := atomic.LoadUint64(&ss.words[0]) |
| 724 |
if w != _LockBit { |
| 725 |
t.Errorf("LockAndReset: word=%#x, want %#x", w, _LockBit) |
| 726 |
ss.Unlock(0) |
| 727 |
return |
| 728 |
} |
| 729 |
ss.Unlock(0) |
| 730 |
} |
| 731 |
}() |
| 732 |
} |
| 733 |
|
| 734 |
wg.Wait() |
| 735 |
|
| 736 |
w := atomic.LoadUint64(&ss.words[0]) |
| 737 |
if w&_LockBit != 0 { |
| 738 |
t.Fatal("lock bit set after all goroutines finished") |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
// Multiple slots under concurrent access: verify no cross-slot interference. |
| 743 |
func TestSlotState_ConcurrentMultipleSlots(t *testing.T) { |
| 744 |
const nslots = 16 |
| 745 |
ss := newSlotState(nslots, 1) |
| 746 |
const goroutines = 64 |
| 747 |
const iters = 5000 |
| 748 |
|
| 749 |
counters := make([]int64, nslots) |
| 750 |
|
| 751 |
var wg sync.WaitGroup |
| 752 |
wg.Add(goroutines) |
| 753 |
for g := 0; g < goroutines; g++ { |
| 754 |
go func(id int) { |
| 755 |
defer wg.Done() |
| 756 |
slot := int32(id % nslots) |
| 757 |
for i := 0; i < iters; i++ { |
| 758 |
ss.Lock(slot) |
| 759 |
counters[slot]++ |
| 760 |
ss.Unlock(slot) |
| 761 |
} |
| 762 |
}(g) |
| 763 |
} |
| 764 |
wg.Wait() |
| 765 |
|
| 766 |
for slot := 0; slot < nslots; slot++ { |
| 767 |
assigned := 0 |
| 768 |
for g := 0; g < goroutines; g++ { |
| 769 |
if g%nslots == slot { |
| 770 |
assigned++ |
| 771 |
} |
| 772 |
} |
| 773 |
want := int64(assigned * iters) |
| 774 |
if counters[slot] != want { |
| 775 |
t.Errorf("slot %d: counter=%d, want %d", slot, counters[slot], want) |
| 776 |
} |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
// Realistic mixed workload: mostly LockAndMark+Unlock with occasional |
| 781 |
// Clear and LockAndReset, across many slots. |
| 782 |
func TestSlotState_ConcurrentMixedWorkload(t *testing.T) { |
| 783 |
const nslots = 64 |
| 784 |
ss := newSlotState(nslots, 3) |
| 785 |
const goroutines = 32 |
| 786 |
const iters = 20000 |
| 787 |
|
| 788 |
var wg sync.WaitGroup |
| 789 |
wg.Add(goroutines) |
| 790 |
for g := 0; g < goroutines; g++ { |
| 791 |
go func(id int) { |
| 792 |
defer wg.Done() |
| 793 |
r := rand.New(rand.NewSource(int64(id))) |
| 794 |
for i := 0; i < iters; i++ { |
| 795 |
idx := int32(r.Intn(nslots)) |
| 796 |
op := r.Intn(20) |
| 797 |
switch { |
| 798 |
case op < 14: // 70% LockAndMark (Get path) |
| 799 |
ss.LockAndMark(idx) |
| 800 |
ss.Unlock(idx) |
| 801 |
case op < 17: // 15% Clear (eviction scan) |
| 802 |
ss.Clear(idx) |
| 803 |
case op < 19: // 10% Lock+Unlock (remove path) |
| 804 |
ss.Lock(idx) |
| 805 |
ss.Unlock(idx) |
| 806 |
default: // 5% LockAndReset (newNode path) |
| 807 |
ss.LockAndReset(idx) |
| 808 |
ss.Unlock(idx) |
| 809 |
} |
| 810 |
} |
| 811 |
}(g) |
| 812 |
} |
| 813 |
wg.Wait() |
| 814 |
|
| 815 |
// All locks should be released |
| 816 |
for i := int32(0); i < nslots; i++ { |
| 817 |
w := atomic.LoadUint64(&ss.words[i]) |
| 818 |
if w&_LockBit != 0 { |
| 819 |
t.Errorf("slot %d: lock bit still set", i) |
| 820 |
} |
| 821 |
if w&ss.vmask > ss.maxVisit { |
| 822 |
t.Errorf("slot %d: counter=%d exceeds maxVisit=%d", i, w&ss.vmask, ss.maxVisit) |
| 823 |
} |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
// ========================================================================= |
| 828 |
// Benchmarks — uncontended (single-thread baseline) |
| 829 |
// ========================================================================= |
| 830 |
|
| 831 |
// The hot Get() path: LockAndMark + Unlock, k=1. |
| 832 |
func BenchmarkSlotState_LockAndMark_Uncontended_K1(b *testing.B) { |
| 833 |
ss := newSlotState(1024, 1) |
| 834 |
b.ResetTimer() |
| 835 |
for i := 0; i < b.N; i++ { |
| 836 |
idx := int32(i & 1023) |
| 837 |
ss.LockAndMark(idx) |
| 838 |
ss.Unlock(idx) |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
// The hot Get() path for SIEVE-k: LockAndMark + Unlock, k=3. |
| 843 |
func BenchmarkSlotState_LockAndMark_Uncontended_K3(b *testing.B) { |
| 844 |
ss := newSlotState(1024, 3) |
| 845 |
b.ResetTimer() |
| 846 |
for i := 0; i < b.N; i++ { |
| 847 |
idx := int32(i & 1023) |
| 848 |
ss.LockAndMark(idx) |
| 849 |
ss.Unlock(idx) |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
// Plain Lock + Unlock (the remove() path). |
| 854 |
func BenchmarkSlotState_Lock_Uncontended(b *testing.B) { |
| 855 |
ss := newSlotState(1024, 1) |
| 856 |
b.ResetTimer() |
| 857 |
for i := 0; i < b.N; i++ { |
| 858 |
idx := int32(i & 1023) |
| 859 |
ss.Lock(idx) |
| 860 |
ss.Unlock(idx) |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
// Lock-free read path (eviction scan check). |
| 865 |
func BenchmarkSlotState_IsVisited_Uncontended(b *testing.B) { |
| 866 |
ss := newSlotState(1024, 1) |
| 867 |
// Mark half the slots |
| 868 |
for i := int32(0); i < 512; i++ { |
| 869 |
ss.LockAndMark(i) |
| 870 |
ss.Unlock(i) |
| 871 |
} |
| 872 |
b.ResetTimer() |
| 873 |
for i := 0; i < b.N; i++ { |
| 874 |
ss.IsVisited(int32(i & 1023)) |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
// Clear CAS loop (eviction scan decrement). |
| 879 |
func BenchmarkSlotState_Clear_Uncontended(b *testing.B) { |
| 880 |
ss := newSlotState(1024, 3) |
| 881 |
// Pre-mark everything to counter=3 |
| 882 |
for i := int32(0); i < 1024; i++ { |
| 883 |
for j := 0; j < 3; j++ { |
| 884 |
ss.LockAndMark(i) |
| 885 |
ss.Unlock(i) |
| 886 |
} |
| 887 |
} |
| 888 |
b.ResetTimer() |
| 889 |
for i := 0; i < b.N; i++ { |
| 890 |
idx := int32(i & 1023) |
| 891 |
ss.Clear(idx) |
| 892 |
// Re-mark so we don't drain the counter |
| 893 |
ss.LockAndMark(idx) |
| 894 |
ss.Unlock(idx) |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
// LockAndReset + Unlock (the newNode() path). |
| 899 |
func BenchmarkSlotState_LockAndReset_Uncontended(b *testing.B) { |
| 900 |
ss := newSlotState(1024, 3) |
| 901 |
b.ResetTimer() |
| 902 |
for i := 0; i < b.N; i++ { |
| 903 |
idx := int32(i & 1023) |
| 904 |
ss.LockAndReset(idx) |
| 905 |
ss.Unlock(idx) |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
// ========================================================================= |
| 910 |
// Benchmarks — contended (parallel) |
| 911 |
// ========================================================================= |
| 912 |
|
| 913 |
// Worst case: all goroutines hammer the same slot. k=1. |
| 914 |
func BenchmarkSlotState_LockAndMark_Contended_K1_SameSlot(b *testing.B) { |
| 915 |
ss := newSlotState(1024, 1) |
| 916 |
b.ResetTimer() |
| 917 |
b.RunParallel(func(pb *testing.PB) { |
| 918 |
for pb.Next() { |
| 919 |
ss.LockAndMark(0) |
| 920 |
ss.Unlock(0) |
| 921 |
} |
| 922 |
}) |
| 923 |
} |
| 924 |
|
| 925 |
// Worst case: same slot, k=3 (Or + CAS path). |
| 926 |
func BenchmarkSlotState_LockAndMark_Contended_K3_SameSlot(b *testing.B) { |
| 927 |
ss := newSlotState(1024, 3) |
| 928 |
b.ResetTimer() |
| 929 |
b.RunParallel(func(pb *testing.PB) { |
| 930 |
for pb.Next() { |
| 931 |
ss.LockAndMark(0) |
| 932 |
ss.Unlock(0) |
| 933 |
} |
| 934 |
}) |
| 935 |
} |
| 936 |
|
| 937 |
// Plain Lock contention on same slot. |
| 938 |
func BenchmarkSlotState_Lock_Contended_SameSlot(b *testing.B) { |
| 939 |
ss := newSlotState(1024, 1) |
| 940 |
b.ResetTimer() |
| 941 |
b.RunParallel(func(pb *testing.PB) { |
| 942 |
for pb.Next() { |
| 943 |
ss.Lock(0) |
| 944 |
ss.Unlock(0) |
| 945 |
} |
| 946 |
}) |
| 947 |
} |
| 948 |
|
| 949 |
// Contention scaling: vary the number of hot slots. |
| 950 |
// Contention probability = 1/hotSlots per pair of goroutines. |
| 951 |
func BenchmarkSlotState_LockAndMark_ContendedScaling_K1(b *testing.B) { |
| 952 |
for _, hotSlots := range []int{1, 4, 16, 64, 256, 1024, 8192} { |
| 953 |
b.Run(fmt.Sprintf("Slots_%d", hotSlots), func(b *testing.B) { |
| 954 |
ss := newSlotState(8192, 1) |
| 955 |
b.ResetTimer() |
| 956 |
b.RunParallel(func(pb *testing.PB) { |
| 957 |
r := rand.New(rand.NewSource(rand.Int63())) |
| 958 |
for pb.Next() { |
| 959 |
idx := int32(r.Intn(hotSlots)) |
| 960 |
ss.LockAndMark(idx) |
| 961 |
ss.Unlock(idx) |
| 962 |
} |
| 963 |
}) |
| 964 |
}) |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
// Same scaling test for k=3 to show the CAS overhead under contention. |
| 969 |
func BenchmarkSlotState_LockAndMark_ContendedScaling_K3(b *testing.B) { |
| 970 |
for _, hotSlots := range []int{1, 4, 16, 64, 256, 1024, 8192} { |
| 971 |
b.Run(fmt.Sprintf("Slots_%d", hotSlots), func(b *testing.B) { |
| 972 |
ss := newSlotState(8192, 3) |
| 973 |
b.ResetTimer() |
| 974 |
b.RunParallel(func(pb *testing.PB) { |
| 975 |
r := rand.New(rand.NewSource(rand.Int63())) |
| 976 |
for pb.Next() { |
| 977 |
idx := int32(r.Intn(hotSlots)) |
| 978 |
ss.LockAndMark(idx) |
| 979 |
ss.Unlock(idx) |
| 980 |
} |
| 981 |
}) |
| 982 |
}) |
| 983 |
} |
| 984 |
} |
| 985 |
|
| 986 |
// IsVisited is lock-free; verify it scales linearly. |
| 987 |
func BenchmarkSlotState_IsVisited_Parallel(b *testing.B) { |
| 988 |
const nslots = 8192 |
| 989 |
ss := newSlotState(nslots, 1) |
| 990 |
for i := int32(0); i < int32(nslots); i += 2 { |
| 991 |
ss.LockAndMark(i) |
| 992 |
ss.Unlock(i) |
| 993 |
} |
| 994 |
b.ResetTimer() |
| 995 |
b.RunParallel(func(pb *testing.PB) { |
| 996 |
r := rand.New(rand.NewSource(rand.Int63())) |
| 997 |
for pb.Next() { |
| 998 |
ss.IsVisited(int32(r.Intn(nslots))) |
| 999 |
} |
| 1000 |
}) |
| 1001 |
} |
| 1002 |
|
| 1003 |
// Clear under contention with concurrent LockAndMark (eviction vs Get). |
| 1004 |
func BenchmarkSlotState_Clear_Contended_K3(b *testing.B) { |
| 1005 |
const nslots = 1024 |
| 1006 |
ss := newSlotState(nslots, 3) |
| 1007 |
// Pre-fill |
| 1008 |
for i := int32(0); i < nslots; i++ { |
| 1009 |
for j := 0; j < 3; j++ { |
| 1010 |
ss.LockAndMark(i) |
| 1011 |
ss.Unlock(i) |
| 1012 |
} |
| 1013 |
} |
| 1014 |
b.ResetTimer() |
| 1015 |
b.RunParallel(func(pb *testing.PB) { |
| 1016 |
r := rand.New(rand.NewSource(rand.Int63())) |
| 1017 |
for pb.Next() { |
| 1018 |
idx := int32(r.Intn(nslots)) |
| 1019 |
if r.Intn(2) == 0 { |
| 1020 |
ss.LockAndMark(idx) |
| 1021 |
ss.Unlock(idx) |
| 1022 |
} else { |
| 1023 |
ss.Clear(idx) |
| 1024 |
} |
| 1025 |
} |
| 1026 |
}) |
| 1027 |
} |
| 1028 |
|
| 1029 |
// Realistic mixed workload: 90% LockAndMark, 10% Clear. |
| 1030 |
func BenchmarkSlotState_MixedWorkload_Parallel(b *testing.B) { |
| 1031 |
const nslots = 8192 |
| 1032 |
ss := newSlotState(nslots, 1) |
| 1033 |
b.ResetTimer() |
| 1034 |
b.RunParallel(func(pb *testing.PB) { |
| 1035 |
r := rand.New(rand.NewSource(rand.Int63())) |
| 1036 |
for pb.Next() { |
| 1037 |
idx := int32(r.Intn(nslots)) |
| 1038 |
if r.Intn(10) < 9 { |
| 1039 |
ss.LockAndMark(idx) |
| 1040 |
ss.Unlock(idx) |
| 1041 |
} else { |
| 1042 |
ss.Clear(idx) |
| 1043 |
} |
| 1044 |
} |
| 1045 |
}) |
| 1046 |
} |