// Atomically decreases a given *system* memory stat. Same comments as // mSysStatInc apply. //go:nosplit func mSysStatDec(sysStat *uint64, n uintptr) { if sys.BigEndian != 0 { atomic.Xadd64(sysStat, -int64(n)) return } if val := atomic.Xadduintptr((*uintptr)(unsafe.Pointer(sysStat)), uintptr(-int64(n))); val+n < n { print("runtime: stat underflow: val ", val, ", n ", n, "\n") exit(2) } }
// Atomically increases a given *system* memory stat. We are counting on this // stat never overflowing a uintptr, so this function must only be used for // system memory stats. // // The current implementation for little endian architectures is based on // xadduintptr(), which is less than ideal: xadd64() should really be used. // Using xadduintptr() is a stop-gap solution until arm supports xadd64() that // doesn't use locks. (Locks are a problem as they require a valid G, which // restricts their useability.) // // A side-effect of using xadduintptr() is that we need to check for // overflow errors. //go:nosplit func mSysStatInc(sysStat *uint64, n uintptr) { if _BigEndian != 0 { atomic.Xadd64(sysStat, int64(n)) return } if val := atomic.Xadduintptr((*uintptr)(unsafe.Pointer(sysStat)), n); val < n { print("runtime: stat overflow: val ", val, ", n ", n, "\n") exit(2) } }
func TestXadduintptr(t *testing.T) { const N = 20 const iter = 100000 inc := uintptr(100) total := uintptr(0) runParallel(N, iter, func() { atomic.Xadduintptr(&total, inc) }) if want := uintptr(N * iter * inc); want != total { t.Fatalf("xadduintpr error, want %d, got %d", want, total) } total = 0 runParallel(N, iter, func() { atomic.Xadduintptr(&total, inc) atomic.Xadduintptr(&total, uintptr(-int64(inc))) }) if total != 0 { t.Fatalf("xadduintpr total error, want %d, got %d", 0, total) } }
// Tests that xadduintptr correctly updates 64-bit values. The place where // we actually do so is mstats.go, functions mSysStat{Inc,Dec}. func TestXadduintptrOnUint64(t *testing.T) { if sys.BigEndian != 0 { // On big endian architectures, we never use xadduintptr to update // 64-bit values and hence we skip the test. (Note that functions // mSysStat{Inc,Dec} in mstats.go have explicit checks for // big-endianness.) t.Skip("skip xadduintptr on big endian architecture") } const inc = 100 val := uint64(0) atomic.Xadduintptr((*uintptr)(unsafe.Pointer(&val)), inc) if inc != val { t.Fatalf("xadduintptr should increase lower-order bits, want %d, got %d", inc, val) } }