Source file src/runtime/rss_linux_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  //go:build linux
     6  
     7  package runtime_test
     8  
     9  import (
    10  	. "runtime"
    11  	"testing"
    12  )
    13  
    14  func TestParseProcStatmRSS(t *testing.T) {
    15  	tests := []struct {
    16  		name       string
    17  		input      string
    18  		pageSize   uint64
    19  		wantRSS    uint64
    20  		wantShared uint64
    21  		ok         bool
    22  	}{
    23  		{name: "full", input: "100 25 10 5 0 20 0\n", pageSize: 4096, wantRSS: 25 * 4096, wantShared: 10 * 4096, ok: true},
    24  		{name: "minimum", input: "3 2 0 ", pageSize: 4096, wantRSS: 2 * 4096, ok: true},
    25  		{name: "extra fields", input: "5 3 2 5", pageSize: 2, wantRSS: 6, wantShared: 4, ok: true},
    26  		{name: "max width", input: "18446744073709551615 18446744073709551615 18446744073709551615 ", pageSize: 1, wantRSS: ^uint64(0), wantShared: ^uint64(0), ok: true},
    27  		{name: "leading whitespace", input: " 3 2 1 ", pageSize: 4096},
    28  		{name: "tab separator", input: "3\t2 1 ", pageSize: 4096},
    29  		{name: "empty", input: "", pageSize: 4096},
    30  		{name: "only first field", input: "3", pageSize: 4096},
    31  		{name: "missing resident", input: "3  1 ", pageSize: 4096},
    32  		{name: "missing shared", input: "3 2 ", pageSize: 4096},
    33  		{name: "invalid first field", input: "x 2 1 ", pageSize: 4096},
    34  		{name: "invalid resident", input: "3 2x 1 ", pageSize: 4096},
    35  		{name: "invalid shared", input: "3 2 1x ", pageSize: 4096},
    36  		{name: "negative first field", input: "-3 2 1 ", pageSize: 4096},
    37  		{name: "negative resident", input: "3 -2 1 ", pageSize: 4096},
    38  		{name: "negative shared", input: "3 2 -1 ", pageSize: 4096},
    39  		{name: "truncated shared", input: "3 2 1", pageSize: 4096},
    40  		{name: "large first field ignored", input: "18446744073709551616 1 1 ", pageSize: 1, wantRSS: 1, wantShared: 1, ok: true},
    41  		{name: "resident overflow", input: "1 18446744073709551616 1 ", pageSize: 1},
    42  		{name: "shared overflow", input: "1 1 18446744073709551616 ", pageSize: 1},
    43  		{name: "shared exceeds RSS", input: "3 1 2 ", pageSize: 4096},
    44  		{name: "multiplication overflow", input: "3 2 1 ", pageSize: 1 << 63},
    45  		{name: "zero page size", input: "3 2 1 ", pageSize: 0},
    46  	}
    47  
    48  	for _, tt := range tests {
    49  		t.Run(tt.name, func(t *testing.T) {
    50  			rss, shared, ok := ParseProcStatmRSS([]byte(tt.input), tt.pageSize)
    51  			if rss != tt.wantRSS || shared != tt.wantShared || ok != tt.ok {
    52  				t.Errorf("ParseProcStatmRSS(%q, %d) = (%d, %d, %v); want (%d, %d, %v)",
    53  					tt.input, tt.pageSize, rss, shared, ok, tt.wantRSS, tt.wantShared, tt.ok)
    54  			}
    55  		})
    56  	}
    57  }
    58  

View as plain text