Source file src/runtime/rss_linux.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
     6  
     7  import "internal/runtime/syscall/linux"
     8  
     9  var (
    10  	procSelfStatm = []byte("/proc/self/statm\x00")
    11  	// procSelfStatmFD is the file descriptor for reading /proc/self/statm
    12  	// to compute RSS. sysRSSInit opens it at startup and it remains open
    13  	// for the entire process lifetime. sysRSS rereads it from the
    14  	// beginning.
    15  	//
    16  	// We open the file at startup rather than on each call because sysRSS
    17  	// is called on every GC cycle, and opening files at unpredictable times
    18  	// during execution is a problem for some programs: sandboxed programs
    19  	// may install seccomp filters after startup that forbid unexpected
    20  	// opens, and file descriptor leak detection is difficult to implement
    21  	// in a stable way if the runtime opens new files periodically. See
    22  	// defaultGOMAXPROCSInit for another example of this pattern.
    23  	procSelfStatmFD = -1
    24  )
    25  
    26  func sysRSSInit() {
    27  	for {
    28  		fd, errno := linux.Open(&procSelfStatm[0], linux.O_RDONLY|linux.O_CLOEXEC, 0)
    29  		if errno == _EINTR {
    30  			continue
    31  		}
    32  		if errno == 0 {
    33  			procSelfStatmFD = fd
    34  		}
    35  		return
    36  	}
    37  }
    38  
    39  // sysRSS returns the current resident set size of this process and its
    40  // shared portion, both in bytes. A zero RSS means it is unavailable.
    41  // Linux's statm RSS accounting is asynchronous, so the result is approximate.
    42  func sysRSS() (rss, shared uint64, hasShared bool) {
    43  	fd := procSelfStatmFD
    44  	if fd < 0 {
    45  		return 0, 0, false
    46  	}
    47  
    48  	// We only need the first three fields of /proc/self/statm. Each is an
    49  	// unsigned long, so 20 decimal digits plus a trailing space per field
    50  	// is enough even on 64-bit systems.
    51  	var buf [3 * (20 + 1)]byte
    52  	for {
    53  		// Read from the beginning to get a fresh value.
    54  		n, errno := linux.Pread(fd, buf[:], 0)
    55  		if errno == _EINTR {
    56  			continue
    57  		}
    58  		if errno != 0 || n <= 0 {
    59  			return 0, 0, false
    60  		}
    61  		return parseProcStatmRSS(buf[:n], uint64(physPageSize))
    62  	}
    63  }
    64  
    65  // parseProcStatmRSS parses the first three fields of /proc/self/statm and
    66  // returns the resident and shared page counts converted to bytes.
    67  func parseProcStatmRSS(data []byte, pageSize uint64) (resident, shared uint64, ok bool) {
    68  	if pageSize == 0 {
    69  		return 0, 0, false
    70  	}
    71  
    72  	// Skip the first field. Its value is not needed, but it must contain at
    73  	// least one digit and be followed by a space.
    74  	i := 0
    75  	for i < len(data) && data[i] >= '0' && data[i] <= '9' {
    76  		i++
    77  	}
    78  	if i == 0 || i == len(data) || data[i] != ' ' {
    79  		return 0, 0, false
    80  	}
    81  	i++
    82  
    83  	for _, pages := range []*uint64{&resident, &shared} {
    84  		start := i
    85  		for i < len(data) && data[i] >= '0' && data[i] <= '9' {
    86  			digit := uint64(data[i] - '0')
    87  			if *pages > (^uint64(0)-digit)/10 {
    88  				return 0, 0, false
    89  			}
    90  			*pages = *pages*10 + digit
    91  			i++
    92  		}
    93  		if i == start || i == len(data) || data[i] != ' ' {
    94  			return 0, 0, false
    95  		}
    96  		i++
    97  	}
    98  	if shared > resident || resident > ^uint64(0)/pageSize {
    99  		return 0, 0, false
   100  	}
   101  	return resident * pageSize, shared * pageSize, true
   102  }
   103  

View as plain text