diff options
| author | Jason A. Donenfeld <Jason@zx2c4.com> | 2024-09-29 04:24:00 +0200 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2024-09-30 15:38:26 +0000 |
| commit | 86781963cc2bf0d3dbd1eccebd8a2f080846f3ee (patch) | |
| tree | 11d8d0e67a904ab135900c6b7bc2f258857f0e81 /src/runtime | |
| parent | 327074551a2f22f2c0b8e444d1673c86f77ca745 (diff) | |
| download | go-86781963cc2bf0d3dbd1eccebd8a2f080846f3ee.tar.xz | |
runtime: align vgetrandom states to cache line
This prevents false sharing, which makes a large difference on machines
with several NUMA nodes, such as this dual socket server:
cpu: Intel(R) Xeon(R) Gold 6338 CPU @ 2.00GHz
│ sec/op │ sec/op vs base │
ParallelGetRandom-128 0.7944n ± 5% 0.4503n ± 0% -43.31% (p=0.000 n=10)
│ B/s │ B/s vs base │
ParallelGetRandom-128 4.690Gi ± 5% 8.272Gi ± 0% +76.38% (p=0.000 n=10)
Change-Id: Id4421e9a4c190b38aff0be4c59e9067b0a38ccd7
Reviewed-on: https://go-review.googlesource.com/c/go/+/616535
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Jason Donenfeld <Jason@zx2c4.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Diffstat (limited to 'src/runtime')
| -rw-r--r-- | src/runtime/vgetrandom_linux.go | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/runtime/vgetrandom_linux.go b/src/runtime/vgetrandom_linux.go index 1e8c8ceaf0..af0e9cad1e 100644 --- a/src/runtime/vgetrandom_linux.go +++ b/src/runtime/vgetrandom_linux.go @@ -6,7 +6,10 @@ package runtime -import "unsafe" +import ( + "internal/cpu" + "unsafe" +) func vgetrandom1(buf *byte, length uintptr, flags uint32, state uintptr, stateSize uintptr) int @@ -43,8 +46,9 @@ func vgetrandomGetState() uintptr { lock(&vgetrandomAlloc.statesLock) if len(vgetrandomAlloc.states) == 0 { num := uintptr(ncpu) // Just a reasonable size hint to start. - allocSize := (num*vgetrandomAlloc.stateSize + physPageSize - 1) &^ (physPageSize - 1) - num = (physPageSize / vgetrandomAlloc.stateSize) * (allocSize / physPageSize) + stateSizeCacheAligned := (vgetrandomAlloc.stateSize + cpu.CacheLineSize - 1) &^ (cpu.CacheLineSize - 1) + allocSize := (num*stateSizeCacheAligned + physPageSize - 1) &^ (physPageSize - 1) + num = (physPageSize / stateSizeCacheAligned) * (allocSize / physPageSize) p, err := mmap(nil, allocSize, vgetrandomAlloc.mmapProt, vgetrandomAlloc.mmapFlags, -1, 0) if err != 0 { unlock(&vgetrandomAlloc.statesLock) @@ -59,7 +63,7 @@ func vgetrandomGetState() uintptr { newBlock = (newBlock + physPageSize - 1) &^ (physPageSize - 1) } vgetrandomAlloc.states = append(vgetrandomAlloc.states, newBlock) - newBlock += vgetrandomAlloc.stateSize + newBlock += stateSizeCacheAligned } } state := vgetrandomAlloc.states[len(vgetrandomAlloc.states)-1] |
