Skip to content

Commit 5c470ff

Browse files
committed
Add support /sys/devices/system/node/nodeN/meminfo
1 parent affffdb commit 5c470ff

File tree

5 files changed

+333
-6
lines changed

5 files changed

+333
-6
lines changed

Diff for: sysfs/meminfo_numa.go

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// Copyright 2019 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build linux
15+
// +build linux
16+
17+
package sysfs
18+
19+
import (
20+
"bufio"
21+
"bytes"
22+
"fmt"
23+
"io"
24+
"path/filepath"
25+
"strconv"
26+
"strings"
27+
28+
"github.com/prometheus/procfs/internal/util"
29+
)
30+
31+
// Meminfo represents memory statistics for NUMA node
32+
type Meminfo struct {
33+
MemTotal uint64
34+
MemFree uint64
35+
MemUsed uint64
36+
SwapCached uint64
37+
Active uint64
38+
Inactive uint64
39+
ActiveAnon uint64
40+
InactiveAnon uint64
41+
ActiveFile uint64
42+
InactiveFile uint64
43+
Unevictable uint64
44+
Mlocked uint64
45+
Dirty uint64
46+
Writeback uint64
47+
FilePages uint64
48+
Mapped uint64
49+
AnonPages uint64
50+
Shmem uint64
51+
KernelStack uint64
52+
PageTables uint64
53+
NFS_Unstable uint64
54+
Bounce uint64
55+
WritebackTmp uint64
56+
KReclaimable uint64
57+
Slab uint64
58+
SReclaimable uint64
59+
SUnreclaim uint64
60+
AnonHugePages uint64
61+
ShmemHugePages uint64
62+
ShmemPmdMapped uint64
63+
FileHugePages uint64
64+
FilePmdMapped uint64
65+
HugePages_Total uint64
66+
HugePages_Free uint64
67+
HugePages_Surp uint64
68+
}
69+
70+
func (fs FS) MeminfoNUMA() (map[int]Meminfo, error) {
71+
m := make(map[int]Meminfo)
72+
nodes, err := filepath.Glob(fs.sys.Path(nodePattern))
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
for _, node := range nodes {
78+
nodeNumbers := nodeNumberRegexp.FindStringSubmatch(node)
79+
if len(nodeNumbers) != 2 {
80+
continue
81+
}
82+
nodeNumber, err := strconv.Atoi(nodeNumbers[1])
83+
if err != nil {
84+
return nil, err
85+
}
86+
b, err := util.ReadFileNoStat(filepath.Join(node, "meminfo"))
87+
if err != nil {
88+
return nil, err
89+
}
90+
meminfo, err := parseMeminfo(bytes.NewReader(b))
91+
if err != nil {
92+
return nil, err
93+
}
94+
m[nodeNumber] = meminfo
95+
}
96+
return m, nil
97+
}
98+
99+
func parseMeminfo(r io.Reader) (Meminfo, error) {
100+
var m Meminfo
101+
s := bufio.NewScanner(r)
102+
for s.Scan() {
103+
// Each line has at least a name and value; we ignore the unit.
104+
// A line example: "Node 0 MemTotal: 395936028 kB"
105+
fields := strings.Fields(s.Text())
106+
if len(fields) < 4 {
107+
return Meminfo{}, fmt.Errorf("malformed meminfo line: %q", s.Text())
108+
}
109+
110+
v, err := strconv.ParseUint(fields[3], 0, 64)
111+
if err != nil {
112+
return Meminfo{}, err
113+
}
114+
115+
switch fields[2] {
116+
case "MemTotal:":
117+
m.MemTotal = v
118+
case "MemFree:":
119+
m.MemFree = v
120+
case "MemUsed:":
121+
m.MemUsed = v
122+
case "SwapCached:":
123+
m.SwapCached = v
124+
case "Active:":
125+
m.Active = v
126+
case "Inactive:":
127+
m.Inactive = v
128+
case "Active(anon):":
129+
m.ActiveAnon = v
130+
case "Inactive(anon):":
131+
m.InactiveAnon = v
132+
case "Active(file):":
133+
m.ActiveFile = v
134+
case "Inactive(file):":
135+
m.InactiveFile = v
136+
case "Unevictable:":
137+
m.Unevictable = v
138+
case "Mlocked:":
139+
m.Mlocked = v
140+
case "Dirty:":
141+
m.Dirty = v
142+
case "Writeback:":
143+
m.Writeback = v
144+
case "FilePages:":
145+
m.FilePages = v
146+
case "Mapped:":
147+
m.Mapped = v
148+
case "AnonPages:":
149+
m.AnonPages = v
150+
case "Shmem:":
151+
m.Shmem = v
152+
case "KernelStack:":
153+
m.KernelStack = v
154+
case "PageTables:":
155+
m.PageTables = v
156+
case "NFS_Unstable:":
157+
m.NFS_Unstable = v
158+
case "Bounce:":
159+
m.Bounce = v
160+
case "WritebackTmp:":
161+
m.WritebackTmp = v
162+
case "KReclaimable:":
163+
m.KReclaimable = v
164+
case "Slab:":
165+
m.Slab = v
166+
case "SReclaimable:":
167+
m.SReclaimable = v
168+
case "SUnreclaim:":
169+
m.SUnreclaim = v
170+
case "AnonHugePages:":
171+
m.AnonHugePages = v
172+
case "ShmemHugePages:":
173+
m.ShmemHugePages = v
174+
case "ShmemPmdMapped:":
175+
m.ShmemPmdMapped = v
176+
case "FileHugePages:":
177+
m.FileHugePages = v
178+
case "FilePmdMapped:":
179+
m.FilePmdMapped = v
180+
case "HugePages_Total:":
181+
m.HugePages_Total = v
182+
case "HugePages_Free:":
183+
m.HugePages_Free = v
184+
case "HugePages_Surp:":
185+
m.HugePages_Surp = v
186+
}
187+
}
188+
189+
return m, nil
190+
}

Diff for: sysfs/meminfo_numa_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2017 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build linux
15+
// +build linux
16+
17+
package sysfs
18+
19+
import (
20+
"testing"
21+
)
22+
23+
func TestMeminfoNUMA(t *testing.T) {
24+
fs, err := NewFS(sysTestFixtures)
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
29+
meminfo, err := fs.MeminfoNUMA()
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
if want, got := uint64(133), meminfo[1].HugePages_Total; want != got {
35+
t.Errorf("want meminfo stat HugePages_Total value %d, got %d", want, got)
36+
}
37+
38+
if want, got := uint64(134), meminfo[1].HugePages_Free; want != got {
39+
t.Errorf("want meminfo stat HugePages_Free value %d, got %d", want, got)
40+
}
41+
}

Diff for: sysfs/numa.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2017 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build linux
15+
// +build linux
16+
17+
package sysfs
18+
19+
import "regexp"
20+
21+
var (
22+
nodePattern = "devices/system/node/node[0-9]*"
23+
nodeNumberRegexp = regexp.MustCompile(`.*devices/system/node/node([0-9]*)`)
24+
)

Diff for: sysfs/vmstat_numa.go

-6
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,12 @@ import (
2121
"bytes"
2222
"fmt"
2323
"path/filepath"
24-
"regexp"
2524
"strconv"
2625
"strings"
2726

2827
"github.com/prometheus/procfs/internal/util"
2928
)
3029

31-
var (
32-
nodePattern = "devices/system/node/node[0-9]*"
33-
nodeNumberRegexp = regexp.MustCompile(`.*devices/system/node/node([0-9]*)`)
34-
)
35-
3630
type VMStat struct {
3731
NrFreePages uint64
3832
NrZoneInactiveAnon uint64

Diff for: testdata/fixtures.ttar

+78
Original file line numberDiff line numberDiff line change
@@ -12727,6 +12727,45 @@ Mode: 775
1272712727
Directory: fixtures/sys/devices/system/node/node1
1272812728
Mode: 755
1272912729
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
12730+
Path: fixtures/sys/devices/system/node/node1/meminfo
12731+
Lines: 35
12732+
Node 1 MemTotal: 101 kB
12733+
Node 1 MemFree: 102 kB
12734+
Node 1 MemUsed: 103 kB
12735+
Node 1 SwapCached: 104 kB
12736+
Node 1 Active: 105 kB
12737+
Node 1 Inactive: 106 kB
12738+
Node 1 Active(anon): 107 kB
12739+
Node 1 Inactive(anon): 108 kB
12740+
Node 1 Active(file): 109 kB
12741+
Node 1 Inactive(file): 110 kB
12742+
Node 1 Unevictable: 111 kB
12743+
Node 1 Mlocked: 112 kB
12744+
Node 1 Dirty: 113 kB
12745+
Node 1 Writeback: 114 kB
12746+
Node 1 FilePages: 115 kB
12747+
Node 1 Mapped: 116 kB
12748+
Node 1 AnonPages: 117 kB
12749+
Node 1 Shmem: 118 kB
12750+
Node 1 KernelStack: 119 kB
12751+
Node 1 PageTables: 120 kB
12752+
Node 1 NFS_Unstable: 121 kB
12753+
Node 1 Bounce: 122 kB
12754+
Node 1 WritebackTmp: 123 kB
12755+
Node 1 KReclaimable: 124 kB
12756+
Node 1 Slab: 125 kB
12757+
Node 1 SReclaimable: 126 kB
12758+
Node 1 SUnreclaim: 127 kB
12759+
Node 1 AnonHugePages: 128 kB
12760+
Node 1 ShmemHugePages: 129 kB
12761+
Node 1 ShmemPmdMapped: 130 kB
12762+
Node 1 FileHugePages: 131 kB
12763+
Node 1 FilePmdMapped: 132 kB
12764+
Node 1 HugePages_Total: 133
12765+
Node 1 HugePages_Free: 134
12766+
Node 1 HugePages_Surp: 135EOF
12767+
Mode: 644
12768+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1273012769
Path: fixtures/sys/devices/system/node/node1/vmstat
1273112770
Lines: 6
1273212771
nr_free_pages 1
@@ -12740,6 +12779,45 @@ Mode: 644
1274012779
Directory: fixtures/sys/devices/system/node/node2
1274112780
Mode: 755
1274212781
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
12782+
Path: fixtures/sys/devices/system/node/node2/meminfo
12783+
Lines: 35
12784+
Node 2 MemTotal: 201 kB
12785+
Node 2 MemFree: 202 kB
12786+
Node 2 MemUsed: 203 kB
12787+
Node 2 SwapCached: 204 kB
12788+
Node 2 Active: 205 kB
12789+
Node 2 Inactive: 206 kB
12790+
Node 2 Active(anon): 207 kB
12791+
Node 2 Inactive(anon): 208 kB
12792+
Node 2 Active(file): 209 kB
12793+
Node 2 Inactive(file): 210 kB
12794+
Node 2 Unevictable: 211 kB
12795+
Node 2 Mlocked: 212 kB
12796+
Node 2 Dirty: 213 kB
12797+
Node 2 Writeback: 214 kB
12798+
Node 2 FilePages: 215 kB
12799+
Node 2 Mapped: 216 kB
12800+
Node 2 AnonPages: 217 kB
12801+
Node 2 Shmem: 218 kB
12802+
Node 2 KernelStack: 219 kB
12803+
Node 2 PageTables: 220 kB
12804+
Node 2 NFS_Unstable: 221 kB
12805+
Node 2 Bounce: 222 kB
12806+
Node 2 WritebackTmp: 223 kB
12807+
Node 2 KReclaimable: 224 kB
12808+
Node 2 Slab: 225 kB
12809+
Node 2 SReclaimable: 226 kB
12810+
Node 2 SUnreclaim: 227 kB
12811+
Node 2 AnonHugePages: 228 kB
12812+
Node 2 ShmemHugePages: 229 kB
12813+
Node 2 ShmemPmdMapped: 230 kB
12814+
Node 2 FileHugePages: 231 kB
12815+
Node 2 FilePmdMapped: 232 kB
12816+
Node 2 HugePages_Total: 233
12817+
Node 2 HugePages_Free: 234
12818+
Node 2 HugePages_Surp: 235EOF
12819+
Mode: 644
12820+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1274312821
Path: fixtures/sys/devices/system/node/node2/vmstat
1274412822
Lines: 6
1274512823
nr_free_pages 7

0 commit comments

Comments
 (0)