aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/prometheus/procfs/sysfs/vmstat_numa.go
blob: 038a0ee3bc149492dd5972b34332eaa5625aa6b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux
// +build linux

package sysfs

import (
	"bufio"
	"bytes"
	"fmt"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"

	"github.com/prometheus/procfs/internal/util"
)

var (
	nodePattern      = "devices/system/node/node[0-9]*"
	nodeNumberRegexp = regexp.MustCompile(`.*devices/system/node/node([0-9]*)`)
)

type VMStat struct {
	NrFreePages                uint64
	NrZoneInactiveAnon         uint64
	NrZoneActiveAnon           uint64
	NrZoneInactiveFile         uint64
	NrZoneActiveFile           uint64
	NrZoneUnevictable          uint64
	NrZoneWritePending         uint64
	NrMlock                    uint64
	NrPageTablePages           uint64
	NrKernelStack              uint64
	NrBounce                   uint64
	NrZspages                  uint64
	NrFreeCma                  uint64
	NumaHit                    uint64
	NumaMiss                   uint64
	NumaForeign                uint64
	NumaInterleave             uint64
	NumaLocal                  uint64
	NumaOther                  uint64
	NrInactiveAnon             uint64
	NrActiveAnon               uint64
	NrInactiveFile             uint64
	NrActiveFile               uint64
	NrUnevictable              uint64
	NrSlabReclaimable          uint64
	NrSlabUnreclaimable        uint64
	NrIsolatedAnon             uint64
	NrIsolatedFile             uint64
	WorkingsetNodes            uint64
	WorkingsetRefault          uint64
	WorkingsetActivate         uint64
	WorkingsetRestore          uint64
	WorkingsetNodereclaim      uint64
	NrAnonPages                uint64
	NrMapped                   uint64
	NrFilePages                uint64
	NrDirty                    uint64
	NrWriteback                uint64
	NrWritebackTemp            uint64
	NrShmem                    uint64
	NrShmemHugepages           uint64
	NrShmemPmdmapped           uint64
	NrFileHugepages            uint64
	NrFilePmdmapped            uint64
	NrAnonTransparentHugepages uint64
	NrVmscanWrite              uint64
	NrVmscanImmediateReclaim   uint64
	NrDirtied                  uint64
	NrWritten                  uint64
	NrKernelMiscReclaimable    uint64
	NrFollPinAcquired          uint64
	NrFollPinReleased          uint64
}

func (fs FS) VMStatNUMA() (map[int]VMStat, error) {
	m := make(map[int]VMStat)
	nodes, err := filepath.Glob(fs.sys.Path(nodePattern))
	if err != nil {
		return nil, err
	}

	for _, node := range nodes {
		nodeNumbers := nodeNumberRegexp.FindStringSubmatch(node)
		if len(nodeNumbers) != 2 {
			continue
		}
		nodeNumber, err := strconv.Atoi(nodeNumbers[1])
		if err != nil {
			return nil, err
		}
		file, err := util.ReadFileNoStat(filepath.Join(node, "vmstat"))
		if err != nil {
			return nil, err
		}
		nodeStats, err := parseVMStatNUMA(file)
		if err != nil {
			return nil, err
		}
		m[nodeNumber] = nodeStats
	}
	return m, nil
}

func parseVMStatNUMA(r []byte) (VMStat, error) {
	var (
		vmStat  = VMStat{}
		scanner = bufio.NewScanner(bytes.NewReader(r))
	)

	for scanner.Scan() {
		line := strings.TrimSpace(scanner.Text())
		if line == "" {
			continue
		}
		parts := strings.Fields(line)
		if len(parts) != 2 {
			return vmStat, fmt.Errorf("line scan did not return 2 fields: %s", line)
		}

		fv, err := strconv.ParseUint(parts[1], 10, 64)
		if err != nil {
			return vmStat, fmt.Errorf("invalid value in vmstat: %w", err)
		}
		switch parts[0] {
		case "nr_free_pages":
			vmStat.NrFreePages = fv
		case "nr_zone_inactive_anon":
			vmStat.NrZoneInactiveAnon = fv
		case "nr_zone_active_anon":
			vmStat.NrZoneActiveAnon = fv
		case "nr_zone_inactive_file":
			vmStat.NrZoneInactiveFile = fv
		case "nr_zone_active_file":
			vmStat.NrZoneActiveFile = fv
		case "nr_zone_unevictable":
			vmStat.NrZoneUnevictable = fv
		case "nr_zone_write_pending":
			vmStat.NrZoneWritePending = fv
		case "nr_mlock":
			vmStat.NrMlock = fv
		case "nr_page_table_pages":
			vmStat.NrPageTablePages = fv
		case "nr_kernel_stack":
			vmStat.NrKernelStack = fv
		case "nr_bounce":
			vmStat.NrBounce = fv
		case "nr_zspages":
			vmStat.NrZspages = fv
		case "nr_free_cma":
			vmStat.NrFreeCma = fv
		case "numa_hit":
			vmStat.NumaHit = fv
		case "numa_miss":
			vmStat.NumaMiss = fv
		case "numa_foreign":
			vmStat.NumaForeign = fv
		case "numa_interleave":
			vmStat.NumaInterleave = fv
		case "numa_local":
			vmStat.NumaLocal = fv
		case "numa_other":
			vmStat.NumaOther = fv
		case "nr_inactive_anon":
			vmStat.NrInactiveAnon = fv
		case "nr_active_anon":
			vmStat.NrActiveAnon = fv
		case "nr_inactive_file":
			vmStat.NrInactiveFile = fv
		case "nr_active_file":
			vmStat.NrActiveFile = fv
		case "nr_unevictable":
			vmStat.NrUnevictable = fv
		case "nr_slab_reclaimable":
			vmStat.NrSlabReclaimable = fv
		case "nr_slab_unreclaimable":
			vmStat.NrSlabUnreclaimable = fv
		case "nr_isolated_anon":
			vmStat.NrIsolatedAnon = fv
		case "nr_isolated_file":
			vmStat.NrIsolatedFile = fv
		case "workingset_nodes":
			vmStat.WorkingsetNodes = fv
		case "workingset_refault":
			vmStat.WorkingsetRefault = fv
		case "workingset_activate":
			vmStat.WorkingsetActivate = fv
		case "workingset_restore":
			vmStat.WorkingsetRestore = fv
		case "workingset_nodereclaim":
			vmStat.WorkingsetNodereclaim = fv
		case "nr_anon_pages":
			vmStat.NrAnonPages = fv
		case "nr_mapped":
			vmStat.NrMapped = fv
		case "nr_file_pages":
			vmStat.NrFilePages = fv
		case "nr_dirty":
			vmStat.NrDirty = fv
		case "nr_writeback":
			vmStat.NrWriteback = fv
		case "nr_writeback_temp":
			vmStat.NrWritebackTemp = fv
		case "nr_shmem":
			vmStat.NrShmem = fv
		case "nr_shmem_hugepages":
			vmStat.NrShmemHugepages = fv
		case "nr_shmem_pmdmapped":
			vmStat.NrShmemPmdmapped = fv
		case "nr_file_hugepages":
			vmStat.NrFileHugepages = fv
		case "nr_file_pmdmapped":
			vmStat.NrFilePmdmapped = fv
		case "nr_anon_transparent_hugepages":
			vmStat.NrAnonTransparentHugepages = fv
		case "nr_vmscan_write":
			vmStat.NrVmscanWrite = fv
		case "nr_vmscan_immediate_reclaim":
			vmStat.NrVmscanImmediateReclaim = fv
		case "nr_dirtied":
			vmStat.NrDirtied = fv
		case "nr_written":
			vmStat.NrWritten = fv
		case "nr_kernel_misc_reclaimable":
			vmStat.NrKernelMiscReclaimable = fv
		case "nr_foll_pin_acquired":
			vmStat.NrFollPinAcquired = fv
		case "nr_foll_pin_released":
			vmStat.NrFollPinReleased = fv
		}

	}
	return vmStat, scanner.Err()
}