aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/prometheus/procfs/xfs/xfs.go
blob: 79bba6565987d61ca4b5fa1ec3437bb123a6db7d (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2017 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.

// Package xfs provides access to statistics exposed by the XFS filesystem.
package xfs

import (
	"os"
	"path/filepath"
	"strings"

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

// Stats contains XFS filesystem runtime statistics, parsed from
// /proc/fs/xfs/stat.
//
// The names and meanings of each statistic were taken from
// http://xfs.org/index.php/Runtime_Stats and xfs_stats.h in the Linux
// kernel source. Most counters are uint32s (same data types used in
// xfs_stats.h), but some of the "extended precision stats" are uint64s.
type Stats struct {
	// The name of the filesystem used to source these statistics.
	// If empty, this indicates aggregated statistics for all XFS
	// filesystems on the host.
	Name string

	ExtentAllocation   ExtentAllocationStats
	AllocationBTree    BTreeStats
	BlockMapping       BlockMappingStats
	BlockMapBTree      BTreeStats
	DirectoryOperation DirectoryOperationStats
	Transaction        TransactionStats
	InodeOperation     InodeOperationStats
	LogOperation       LogOperationStats
	ReadWrite          ReadWriteStats
	AttributeOperation AttributeOperationStats
	InodeClustering    InodeClusteringStats
	Vnode              VnodeStats
	Buffer             BufferStats
	ExtendedPrecision  ExtendedPrecisionStats
	Xstrat             XstratStats            // xstrat
	PushAil            PushAilStats           // push_ail
	Debug              DebugStats             // debug
	QuotaManager       QuotaManagerStats      // qm
	BtreeAllocBlocks2  BtreeAllocBlocks2Stats // abtb2
	BtreeAllocContig2  BtreeAllocContig2Stats // abtc2
	BtreeBlockMap2     BtreeBlockMap2Stats    // bmbt2
	BtreeInode2        BtreeInode2Stats       // ibt2
}

// ExtentAllocationStats contains statistics regarding XFS extent allocations.
type ExtentAllocationStats struct {
	ExtentsAllocated uint32
	BlocksAllocated  uint32
	ExtentsFreed     uint32
	BlocksFreed      uint32
}

// BTreeStats contains statistics regarding an XFS internal B-tree.
type BTreeStats struct {
	Lookups         uint32
	Compares        uint32
	RecordsInserted uint32
	RecordsDeleted  uint32
}

// BlockMappingStats contains statistics regarding XFS block maps.
type BlockMappingStats struct {
	Reads                uint32
	Writes               uint32
	Unmaps               uint32
	ExtentListInsertions uint32
	ExtentListDeletions  uint32
	ExtentListLookups    uint32
	ExtentListCompares   uint32
}

// DirectoryOperationStats contains statistics regarding XFS directory entries.
type DirectoryOperationStats struct {
	Lookups  uint32
	Creates  uint32
	Removes  uint32
	Getdents uint32
}

// TransactionStats contains statistics regarding XFS metadata transactions.
type TransactionStats struct {
	Sync  uint32
	Async uint32
	Empty uint32
}

// InodeOperationStats contains statistics regarding XFS inode operations.
type InodeOperationStats struct {
	Attempts        uint32
	Found           uint32
	Recycle         uint32
	Missed          uint32
	Duplicate       uint32
	Reclaims        uint32
	AttributeChange uint32
}

// LogOperationStats contains statistics regarding the XFS log buffer.
type LogOperationStats struct {
	Writes            uint32
	Blocks            uint32
	NoInternalBuffers uint32
	Force             uint32
	ForceSleep        uint32
}

// ReadWriteStats contains statistics regarding the number of read and write
// system calls for XFS filesystems.
type ReadWriteStats struct {
	Write uint32
	Read  uint32
}

// AttributeOperationStats contains statistics regarding manipulation of
// XFS extended file attributes.
type AttributeOperationStats struct {
	Get    uint32
	Set    uint32
	Remove uint32
	List   uint32
}

// InodeClusteringStats contains statistics regarding XFS inode clustering
// operations.
type InodeClusteringStats struct {
	Iflush     uint32
	Flush      uint32
	FlushInode uint32
}

// VnodeStats contains statistics regarding XFS vnode operations.
type VnodeStats struct {
	Active   uint32
	Allocate uint32
	Get      uint32
	Hold     uint32
	Release  uint32
	Reclaim  uint32
	Remove   uint32
	Free     uint32
}

// BufferStats contains statistics regarding XFS read/write I/O buffers.
type BufferStats struct {
	Get             uint32
	Create          uint32
	GetLocked       uint32
	GetLockedWaited uint32
	BusyLocked      uint32
	MissLocked      uint32
	PageRetries     uint32
	PageFound       uint32
	GetRead         uint32
}

// ExtendedPrecisionStats contains high precision counters used to track the
// total number of bytes read, written, or flushed, during XFS operations.
type ExtendedPrecisionStats struct {
	FlushBytes uint64
	WriteBytes uint64
	ReadBytes  uint64
}

// PushAilStats contains statistics on tail-pushing operations.
type PushAilStats struct {
	TryLogspace   uint32
	SleepLogspace uint32
	Pushes        uint32
	Success       uint32
	PushBuf       uint32
	Pinned        uint32
	Locked        uint32
	Flushing      uint32
	Restarts      uint32
	Flush         uint32
}

// QuotaManagerStats contain statistics regarding quota processing.
type QuotaManagerStats struct {
	Reclaims      uint32
	ReclaimMisses uint32
	DquoteDups    uint32
	CacheMisses   uint32
	CacheHits     uint32
	Wants         uint32
	ShakeReclaims uint32
	InactReclaims uint32
	Unused        uint32
}

// XstratStats contains statistics regarding bytes processed by the XFS daemon.
type XstratStats struct {
	Quick uint32
	Split uint32
}

// DebugStats indicate if XFS debugging is enabled.
type DebugStats struct {
	Enabled uint32
}

// BtreeAllocBlocks2Stats contains statistics on B-Tree v2 allocations.
type BtreeAllocBlocks2Stats struct {
	Lookup    uint32
	Compare   uint32
	Insrec    uint32
	Delrec    uint32
	NewRoot   uint32
	KillRoot  uint32
	Increment uint32
	Decrement uint32
	Lshift    uint32
	Rshift    uint32
	Split     uint32
	Join      uint32
	Alloc     uint32
	Free      uint32
	Moves     uint32
}

// BtreeAllocContig2Stats contain statistics on B-tree v2 free-space-by-size record operations.
type BtreeAllocContig2Stats struct {
	Lookup    uint32
	Compare   uint32
	Insrec    uint32
	Delrec    uint32
	NewRoot   uint32
	KillRoot  uint32
	Increment uint32
	Decrement uint32
	Lshift    uint32
	Rshift    uint32
	Split     uint32
	Join      uint32
	Alloc     uint32
	Free      uint32
	Moves     uint32
}

// BtreeBlockMap2Stats contain statistics on B-tree v2 block map operations.
type BtreeBlockMap2Stats struct {
	Lookup    uint32
	Compare   uint32
	Insrec    uint32
	Delrec    uint32
	NewRoot   uint32
	KillRoot  uint32
	Increment uint32
	Decrement uint32
	Lshift    uint32
	Rshift    uint32
	Split     uint32
	Join      uint32
	Alloc     uint32
	Free      uint32
	Moves     uint32
}

// BtreeInode2Stats contain statistics on B-tree v2 inode allocations.
type BtreeInode2Stats struct {
	Lookup    uint32
	Compare   uint32
	Insrec    uint32
	Delrec    uint32
	NewRoot   uint32
	KillRoot  uint32
	Increment uint32
	Decrement uint32
	Lshift    uint32
	Rshift    uint32
	Split     uint32
	Join      uint32
	Alloc     uint32
	Free      uint32
	Moves     uint32
}

// FS represents the pseudo-filesystems proc and sys, which provides an interface to
// kernel data structures.
type FS struct {
	proc *fs.FS
	sys  *fs.FS
}

// NewDefaultFS returns a new XFS handle using the default proc and sys mountPoints.
// It will error if either of the mounts point can't be read.
func NewDefaultFS() (FS, error) {
	return NewFS(fs.DefaultProcMountPoint, fs.DefaultSysMountPoint)
}

// NewFS returns a new XFS handle using the given proc and sys mountPoints. It will error
// if either of the mounts point can't be read.
func NewFS(procMountPoint string, sysMountPoint string) (FS, error) {
	if strings.TrimSpace(procMountPoint) == "" {
		procMountPoint = fs.DefaultProcMountPoint
	}
	procfs, err := fs.NewFS(procMountPoint)
	if err != nil {
		return FS{}, err
	}
	if strings.TrimSpace(sysMountPoint) == "" {
		sysMountPoint = fs.DefaultSysMountPoint
	}
	sysfs, err := fs.NewFS(sysMountPoint)
	if err != nil {
		return FS{}, err
	}
	return FS{&procfs, &sysfs}, nil
}

// ProcStat retrieves XFS filesystem runtime statistics
// from proc/fs/xfs/stat given the profs mount point.
func (fs FS) ProcStat() (*Stats, error) {
	f, err := os.Open(fs.proc.Path("fs/xfs/stat"))
	if err != nil {
		return nil, err
	}
	defer f.Close()

	return ParseStats(f)
}

// SysStats retrieves XFS filesystem runtime statistics for each mounted XFS
// filesystem.  Only available on kernel 4.4+.  On older kernels, an empty
// slice of *xfs.Stats will be returned.
func (fs FS) SysStats() ([]*Stats, error) {
	matches, err := filepath.Glob(fs.sys.Path("fs/xfs/*/stats/stats"))
	if err != nil {
		return nil, err
	}

	stats := make([]*Stats, 0, len(matches))
	for _, m := range matches {
		f, err := os.Open(m)
		if err != nil {
			return nil, err
		}

		// "*" used in glob above indicates the name of the filesystem.
		name := filepath.Base(filepath.Dir(filepath.Dir(m)))

		// File must be closed after parsing, regardless of success or
		// failure.  Defer is not used because of the loop.
		s, err := ParseStats(f)
		_ = f.Close()
		if err != nil {
			return nil, err
		}

		s.Name = name
		stats = append(stats, s)
	}

	return stats, nil
}