Skip to content

Commit 1b7c641

Browse files
dongjiang1989SuperQ
authored andcommitted
feat: Add /sys/fs/btrfs/<FSID>/commit_stats statistics (prometheus#580)
* Add commit_stats statistics --------- Signed-off-by: dongjiang1989 <[email protected]> Co-authored-by: Ben Kochie <[email protected]>
1 parent 00f0f1f commit 1b7c641

File tree

4 files changed

+116
-14
lines changed

4 files changed

+116
-14
lines changed

btrfs/btrfs.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Stats struct {
2525
NodeSize uint64
2626
QuotaOverride uint64
2727
SectorSize uint64
28+
CommitStats CommitStats
2829
}
2930

3031
// Allocation contains allocation statistics for data, metadata and system data.
@@ -65,3 +66,12 @@ type LayoutUsage struct {
6566
type Device struct {
6667
Size uint64
6768
}
69+
70+
// Number of commits and various time related statistics.
71+
// See Linux fs/btrfs/sysfs.c with 6.x version.
72+
type CommitStats struct {
73+
Commits uint64
74+
LastCommitMs uint64
75+
MaxCommitMs uint64
76+
TotalCommitMs uint64
77+
}

btrfs/get.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package btrfs
1515

1616
import (
17+
"bufio"
18+
"fmt"
1719
"os"
1820
"path"
1921
"path/filepath"
@@ -245,6 +247,62 @@ func (r *reader) readFilesystemStats() (s *Stats) {
245247
Metadata: r.readAllocationStats("allocation/metadata"),
246248
System: r.readAllocationStats("allocation/system"),
247249
},
250+
251+
// Read commit stats data
252+
CommitStats: r.readCommitStats("commit_stats"),
248253
}
249254
return
250255
}
256+
257+
// readCommitStats returns the commit_stats information for commit stats metrics.
258+
func (r *reader) readCommitStats(p string) CommitStats {
259+
stats := CommitStats{}
260+
261+
f, err := os.Open(path.Join(r.path, p))
262+
if err != nil {
263+
// if commit_stats not found. maybe btrfs version < 6.0
264+
if !os.IsNotExist(err) {
265+
r.err = err
266+
}
267+
return stats
268+
}
269+
defer f.Close()
270+
271+
scanner := bufio.NewScanner(f)
272+
273+
for scanner.Scan() {
274+
line := scanner.Text()
275+
parts := strings.Fields(scanner.Text())
276+
// require <key> <value>
277+
if len(parts) != 2 {
278+
r.err = fmt.Errorf("invalid commit_stats line %q", line)
279+
return stats
280+
}
281+
282+
value, err := strconv.ParseUint(parts[1], 10, 64)
283+
if err != nil {
284+
r.err = fmt.Errorf("error parsing commit_stats line: %w", err)
285+
return stats
286+
}
287+
288+
switch metricName := parts[0]; metricName {
289+
case "commits":
290+
stats.Commits = value
291+
case "last_commit_ms":
292+
stats.LastCommitMs = value
293+
case "max_commit_ms":
294+
stats.MaxCommitMs = value
295+
case "total_commit_ms":
296+
stats.TotalCommitMs = value
297+
default:
298+
continue
299+
}
300+
}
301+
302+
if err := scanner.Err(); err != nil {
303+
r.err = fmt.Errorf("error scanning commit_stats file: %w", err)
304+
return stats
305+
}
306+
307+
return stats
308+
}

btrfs/get_test.go

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type testVector struct {
1919
uuid, label string
2020
devices, features int
2121
data, meta, system alloc
22+
commitstats commit
2223
}
2324

2425
type alloc struct {
@@ -27,6 +28,13 @@ type alloc struct {
2728
ratio float64
2829
}
2930

31+
type commit struct {
32+
commits uint64
33+
lastCommitMs uint64
34+
maxCommitMs uint64
35+
totalCommitMs uint64
36+
}
37+
3038
func TestFSBtrfsStats(t *testing.T) {
3139
btrfs, err := NewFS("testdata/fixtures/sys")
3240
if err != nil {
@@ -39,22 +47,24 @@ func TestFSBtrfsStats(t *testing.T) {
3947

4048
tests := []testVector{
4149
{
42-
uuid: "0abb23a9-579b-43e6-ad30-227ef47fcb9d",
43-
label: "fixture",
44-
devices: 2,
45-
features: 4,
46-
data: alloc{"raid0", 2147483648, 1},
47-
meta: alloc{"raid1", 1073741824, 2},
48-
system: alloc{"raid1", 8388608, 2},
50+
uuid: "0abb23a9-579b-43e6-ad30-227ef47fcb9d",
51+
label: "fixture",
52+
devices: 2,
53+
features: 4,
54+
data: alloc{"raid0", 2147483648, 1},
55+
meta: alloc{"raid1", 1073741824, 2},
56+
system: alloc{"raid1", 8388608, 2},
57+
commitstats: commit{258051, 1000, 51462, 47836090},
4958
},
5059
{
51-
uuid: "7f07c59f-6136-449c-ab87-e1cf2328731b",
52-
label: "",
53-
devices: 4,
54-
features: 5,
55-
data: alloc{"raid5", 644087808, 4. / 3.},
56-
meta: alloc{"raid6", 429391872, 4. / 2.},
57-
system: alloc{"raid6", 16777216, 4. / 2.},
60+
uuid: "7f07c59f-6136-449c-ab87-e1cf2328731b",
61+
label: "",
62+
devices: 4,
63+
features: 5,
64+
data: alloc{"raid5", 644087808, 4. / 3.},
65+
meta: alloc{"raid6", 429391872, 4. / 2.},
66+
system: alloc{"raid6", 16777216, 4. / 2.},
67+
commitstats: commit{0, 0, 0, 0},
5868
},
5969
}
6070

@@ -98,5 +108,21 @@ func TestFSBtrfsStats(t *testing.T) {
98108
if want, got := tt.system.ratio, stats[i].Allocation.System.Layouts[tt.system.layout].Ratio; want != got {
99109
t.Errorf("fs %q unexpected system ratio:\nwant: %f\nhave: %f", tt.uuid, want, got)
100110
}
111+
112+
if want, got := tt.commitstats.commits, stats[i].CommitStats.Commits; want != got {
113+
t.Errorf("fs %q unexpected commit stats commits:\nwant: %d\nhave: %d", tt.uuid, want, got)
114+
}
115+
116+
if want, got := tt.commitstats.lastCommitMs, stats[i].CommitStats.LastCommitMs; want != got {
117+
t.Errorf("fs %q unexpected commit stats last_commit_ms:\nwant: %d\nhave: %d", tt.uuid, want, got)
118+
}
119+
120+
if want, got := tt.commitstats.maxCommitMs, stats[i].CommitStats.MaxCommitMs; want != got {
121+
t.Errorf("fs %q unexpected commit stats max_commit_ms:\nwant: %d\nhave: %d", tt.uuid, want, got)
122+
}
123+
124+
if want, got := tt.commitstats.totalCommitMs, stats[i].CommitStats.TotalCommitMs; want != got {
125+
t.Errorf("fs %q unexpected commit stats total_commit_ms:\nwant: %d\nhave: %d", tt.uuid, want, got)
126+
}
101127
}
102128
}

testdata/fixtures.ttar

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14230,6 +14230,14 @@ Lines: 1
1423014230
4096
1423114231
Mode: 444
1423214232
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
14233+
Path: fixtures/sys/fs/btrfs/0abb23a9-579b-43e6-ad30-227ef47fcb9d/commit_stats
14234+
Lines: 4
14235+
commits 258051
14236+
last_commit_ms 1000
14237+
max_commit_ms 51462
14238+
total_commit_ms 47836090EOF
14239+
Mode: 644
14240+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1423314241
Directory: fixtures/sys/fs/btrfs/0abb23a9-579b-43e6-ad30-227ef47fcb9d/devices
1423414242
Mode: 755
1423514243
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

0 commit comments

Comments
 (0)