Source file src/sync/counter_block.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
     6  
     7  import (
     8  	"internal/cpu"
     9  	"unsafe"
    10  )
    11  
    12  // cacheLineSize is used to prevent false sharing of cache lines.
    13  const cacheLineSize = max(unsafe.Sizeof(cpu.CacheLinePad{}), uintptr(64))
    14  
    15  // counterBlockLen is the number of uint64 values a block contains.
    16  // A block is sized to span exactly one cache line.
    17  const counterBlockLen = int(cacheLineSize / unsafe.Sizeof(uint64(0)))
    18  
    19  // counterBlock contains a collection of uint64 which fit into a cache line.
    20  // It is intended to be accessed by a single P.
    21  type counterBlock struct {
    22  	line [counterBlockLen]uint64
    23  }
    24  
    25  // counterBlocks holds one counterBlock per P. Element pID holds the counters
    26  // owned by P pID
    27  type counterBlocks []counterBlock
    28  
    29  var (
    30  	countBlockMu      Mutex
    31  	countBlocksActive counterBlocks
    32  	countBlocksIndex  int
    33  )
    34  
    35  // allocCounters returns an iterator of pointers to uint64 values of the provided length. Each uint64
    36  // is guaranteed to be located in a P-local cache line to minimize false sharing.
    37  func allocCounters(size int) func(yield func(*uint64) bool) {
    38  	countBlockMu.Lock()
    39  	defer countBlockMu.Unlock()
    40  
    41  	// A new counterBlocks is allocated when:
    42  	//   - size > len(countBlocksActive): the requested number of Ps has grown
    43  	//     (a nil countBlocksActive has length 0, so this also covers the
    44  	//     not-yet-allocated case).
    45  	//   - countBlocksIndex == counterBlockLen: every column of the current
    46  	//     block-set has been handed out.
    47  	if size > len(countBlocksActive) || countBlocksIndex == counterBlockLen {
    48  		countBlocksActive = newCounterBlocks(size)
    49  		countBlocksIndex = 0
    50  	}
    51  	iterCb := countBlocksActive
    52  	iterCbIndex := countBlocksIndex
    53  	countBlocksIndex++
    54  
    55  	return func(yield func(*uint64) bool) {
    56  		for pID := range size {
    57  			c := &iterCb[pID].line[iterCbIndex]
    58  			if !yield(c) {
    59  				return
    60  			}
    61  		}
    62  	}
    63  }
    64  
    65  // newCounterBlocks allocates size cache-line-aligned blocks, one per P.
    66  //
    67  // Because the runtime allocator guarantees only 8-byte pointer alignment for slice
    68  // backing arrays, an unaligned block array could straddle cache lines and cause
    69  // false sharing between adjacent Ps. To guarantee cache-line alignment,
    70  // newCounterBlocks over-allocates by one block and reslices from the first
    71  // cache line boundary.
    72  func newCounterBlocks(size int) counterBlocks {
    73  	// Allocate size+1 blocks so there is always at least one cache line aligned window of length size.
    74  	buf := make(counterBlocks, size+1)
    75  	// Compute the byte offset to the next cache line boundary.
    76  	off := -uintptr(unsafe.Pointer(&buf[0])) & (cacheLineSize - 1)
    77  	// Return a slice of length size starting at the cache line aligned address within buf.
    78  	return unsafe.Slice((*counterBlock)(unsafe.Add(unsafe.Pointer(&buf[0]), off)), size)
    79  }
    80  

View as plain text