Source file src/runtime/rss_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 runtime_test
     6  
     7  import (
     8  	"os"
     9  	"runtime"
    10  	"testing"
    11  )
    12  
    13  func requireSysRSS(t *testing.T) {
    14  	t.Helper()
    15  	switch runtime.GOOS {
    16  	case "darwin":
    17  	case "linux":
    18  		if _, err := os.Stat("/proc/self/statm"); err != nil {
    19  			t.Skipf("SysRSS unavailable: %v", err)
    20  		}
    21  	default:
    22  		t.Skipf("SysRSS unsupported on %s/%s", runtime.GOOS, runtime.GOARCH)
    23  	}
    24  }
    25  
    26  func TestSysRSS(t *testing.T) {
    27  	requireSysRSS(t)
    28  	bytes, shared, hasShared := runtime.SysRSS()
    29  	if bytes == 0 {
    30  		t.Fatalf("SysRSS unavailable on supported platform %s/%s", runtime.GOOS, runtime.GOARCH)
    31  	}
    32  	if runtime.GOOS == "linux" && !hasShared {
    33  		t.Error("SysRSS did not report shared RSS on Linux")
    34  	}
    35  	if shared > bytes || !hasShared && shared != 0 {
    36  		t.Errorf("SysRSS returned inconsistent counts: rss=%d, shared=%d, hasShared=%t", bytes, shared, hasShared)
    37  	}
    38  	t.Logf("SysRSS = %d bytes (%d MiB), shared = %d bytes", bytes, bytes>>20, shared)
    39  }
    40  
    41  func TestSysRSSNoAlloc(t *testing.T) {
    42  	requireSysRSS(t)
    43  	if rss, _, _ := runtime.SysRSS(); rss == 0 {
    44  		t.Fatalf("SysRSS unavailable on supported platform %s/%s", runtime.GOOS, runtime.GOARCH)
    45  	}
    46  	allocs := testing.AllocsPerRun(100, func() {
    47  		runtime.SysRSS()
    48  	})
    49  	if allocs != 0 {
    50  		t.Errorf("SysRSS allocated %v objects per call; want 0", allocs)
    51  	}
    52  }
    53  

View as plain text