Source file src/sync/counter_stripe_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package sync_test
     6  
     7  import (
     8  	"runtime"
     9  	"sync"
    10  	"testing"
    11  	"unsafe"
    12  )
    13  
    14  // TestStripeShardsOnDistinctCacheLines checks the property the stripe exists
    15  // to provide: every P's shard of a counter is a distinct, stable pointer that
    16  // lives on its own cache line, so Adds on different Ps don't false-share.
    17  func TestStripeShardsOnDistinctCacheLines(t *testing.T) {
    18  	s := sync.NewStripe()
    19  	owner := make(map[uintptr]int) // cache line -> pID
    20  	for pID, p := range s.Block() {
    21  		if p == nil {
    22  			t.Fatalf("shard %d is nil", pID)
    23  		}
    24  		if got := s.Get(pID); got != p {
    25  			t.Errorf("Get(%d) = %p, want stable pointer %p", pID, got, p)
    26  		}
    27  		line := uintptr(unsafe.Pointer(p)) / sync.CacheLineSize
    28  		if other, ok := owner[line]; ok {
    29  			t.Errorf("shards for P %d and P %d share cache line %#x", other, pID, line*sync.CacheLineSize)
    30  		}
    31  		owner[line] = pID
    32  	}
    33  }
    34  
    35  func TestStripeGrow(t *testing.T) {
    36  	if runtime.GOARCH == "wasm" {
    37  		t.Skip("skipping on wasm: GOMAXPROCS cannot exceed 1")
    38  	}
    39  	initCap := runtime.GOMAXPROCS(0)
    40  	t.Cleanup(func() {
    41  		runtime.GOMAXPROCS(initCap)
    42  	})
    43  
    44  	s := sync.NewStripe()
    45  	if got := s.Cap(); got != initCap {
    46  		t.Fatalf("initial cap = %d, want %d", got, initCap)
    47  	}
    48  
    49  	// Snapshot the initial shard pointers to verify prefix preservation on grow.
    50  	origPtrs := append([]*uint64(nil), s.Block()...)
    51  
    52  	for idx := range initCap {
    53  		sync.Runtime_procPin()
    54  		n := s.Get(idx)
    55  		*n += 2
    56  		sync.Runtime_procUnpin()
    57  	}
    58  
    59  	// Double GOMAXPROCS so that an out-of-range pID triggers real stripe growth.
    60  	updCap := initCap * 2
    61  	runtime.GOMAXPROCS(updCap)
    62  
    63  	for idx := range updCap {
    64  		sync.Runtime_procPin()
    65  		n := s.Get(idx)
    66  		*n += 2
    67  		sync.Runtime_procUnpin()
    68  	}
    69  
    70  	if got := s.Cap(); got != updCap {
    71  		t.Fatalf("cap after grow = %d, want %d", got, updCap)
    72  	}
    73  
    74  	// Verify existing shard pointers were preserved and new shards were allocated.
    75  	grownBlock := s.Block()
    76  	for i := range initCap {
    77  		if grownBlock[i] != origPtrs[i] {
    78  			t.Errorf("shard %d pointer changed across grow: got %p, want %p", i, grownBlock[i], origPtrs[i])
    79  		}
    80  	}
    81  	for i := initCap; i < updCap; i++ {
    82  		if grownBlock[i] == nil {
    83  			t.Errorf("newly grown shard %d is nil", i)
    84  		}
    85  	}
    86  
    87  	var got uint64
    88  	for idx := range s.Cap() {
    89  		sync.Runtime_procPin()
    90  		got += *s.Get(idx)
    91  		sync.Runtime_procUnpin()
    92  	}
    93  	want := uint64(initCap*2 + updCap*2)
    94  	if got != want {
    95  		t.Errorf("Total sum of values want %d, got %d", want, got)
    96  	}
    97  
    98  	// Verify shrinking GOMAXPROCS does not shrink the stripe.
    99  	runtime.GOMAXPROCS(initCap)
   100  	s.Grow()
   101  	if got := s.Cap(); got != updCap {
   102  		t.Errorf("cap after GOMAXPROCS shrink = %d, want %d (must not shrink)", got, updCap)
   103  	}
   104  }
   105  

View as plain text