Source file
src/runtime/rss_linux.go
1
2
3
4
5 package runtime
6
7 import "internal/runtime/syscall/linux"
8
9 var (
10 procSelfStatm = []byte("/proc/self/statm\x00")
11
12
13
14
15
16
17
18
19
20
21
22
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
40
41
42 func sysRSS() (rss, shared uint64, hasShared bool) {
43 fd := procSelfStatmFD
44 if fd < 0 {
45 return 0, 0, false
46 }
47
48
49
50
51 var buf [3 * (20 + 1)]byte
52 for {
53
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
66
67 func parseProcStatmRSS(data []byte, pageSize uint64) (resident, shared uint64, ok bool) {
68 if pageSize == 0 {
69 return 0, 0, false
70 }
71
72
73
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