-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbcachestat
More file actions
executable file
·150 lines (133 loc) · 4.43 KB
/
Copy pathbcachestat
File metadata and controls
executable file
·150 lines (133 loc) · 4.43 KB
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
#! /usr/bin/env perl6
use v6.d;
use Term::ReadKey;
use JSON::Fast;
our &BOLD = sub (*@s) {
"\e[1m{@s.join('')}\e[0m"
}
our &RED = sub (*@s) {
"\e[31m{@s.join('')}\e[0m"
}
our &RESET = sub (*@s) {
"\e[0m{@s.join('')}\e[0m"
}
&BOLD = &RED = &RESET = sub (Stringy $s) { $s } unless $*OUT.t;
sub cache(\seq) { seq.cache }
my %cache-mapping;
sub is-bcache-dev($dev) {
"/sys/block/$dev/bcache/cache".IO.e
}
sub bcache-devs() {
%cache-mapping := {};
gather for dir('/sys/block/')».basename -> $dev {
if is-bcache-dev($dev) {
%cache-mapping{"/sys/block/$dev/bcache/cache".IO.resolve.basename}.push: $dev;
$dev.take;
}
}
}
sub bcache-caches() {
gather for dir('/sys/fs/bcache/') { .basename.take if .basename.contains('-') }
}
my @stats =
'cache state', -> $dev { slurp("/sys/block/$dev/bcache/state").chomp },
'cache mode', -> $dev { slurp("/sys/block/$dev/bcache/cache_mode").chomp ~~ /'[' (\w+) ']'/; $0.Str },
'dirty data', -> $dev { slurp("/sys/block/$dev/bcache/dirty_data").chomp },
'keep dirty', -> $dev { slurp("/sys/block/$dev/bcache/writeback_percent").chomp ~ '%' },
'devices', -> $dev { dir("/sys/block/$dev/slaves/")».basename.chomp.join(' ') },
'device', -> $cache {
my @devs;
for 0..∞ -> $i {
my $path = "/sys/fs/bcache/$cache/cache{$i}".IO;
last unless $path.e;
@devs.push: $path.resolve.Str.split('/')[*-2]
}
@devs.join(' ')
},
'serving', -> $cache { quietly %cache-mapping{$cache}.join(' ') || 'detached' },
'hit ratio', -> $cache {
slurp("/sys/fs/bcache/$cache/stats_total/cache_hit_ratio").chomp ~ '%',
slurp("/sys/fs/bcache/$cache/stats_day/cache_hit_ratio").chomp ~ '%/d',
slurp("/sys/fs/bcache/$cache/stats_hour/cache_hit_ratio").chomp ~ '%/h',
slurp("/sys/fs/bcache/$cache/stats_five_minute/cache_hit_ratio").chomp ~ '%/5min'
},
'bypassed', -> $cache {
slurp("/sys/fs/bcache/$cache/stats_total/bypassed").chomp,
slurp("/sys/fs/bcache/$cache/stats_day/bypassed").chomp ~ '/d',
slurp("/sys/fs/bcache/$cache/stats_hour/bypassed").chomp ~ '/h',
slurp("/sys/fs/bcache/$cache/stats_five_minute/bypassed").chomp ~ '/5min'
},
'cache available', -> $cache {
slurp("/sys/fs/bcache/$cache/cache_available_percent").chomp ~ '%'
},
'written to cache', -> $cache { slurp("/sys/fs/bcache/$cache/cache0/written").chomp },
'btree written', -> $cache { slurp("/sys/fs/bcache/$cache/cache0/btree_written").chomp },
;
my %stats = do for @stats -> $key, &value {
slip $key.subst(' ', '-', :g), &value
}
multi sub MAIN(Bool :$text!) {
my @lines;
for bcache-devs() -> $dev {
with $dev {
@lines.push: BOLD $dev ~ ':';
for @stats -> $name, &f {
next unless &f.signature.params».name eq '$dev';
@lines.push: "\t" ~ $name ~ ': ' ~ .&f;
}
}
}
for bcache-caches() -> $cache {
@lines.push: BOLD $cache ~ ':';
with $cache {
for @stats -> $name, &f {
next unless &f.signature.params».name eq '$cache';
@lines.push: "\t" ~ $name ~ ': ' ~ .&f;
}
}
}
@lines.join("\n")
}
multi sub MAIN(Bool :$perl!) {
my $top;
for bcache-devs() -> $dev {
my %devs;
with $dev {
for @stats -> $name, &f {
next unless &f.signature.params».name eq '$dev';
%devs.push: $name => .&f;
}
}
$top.push: $dev => %devs;
}
for bcache-caches() -> $cache {
my %caches;
with $cache {
for @stats -> $name, &f {
next unless &f.signature.params».name eq '$cache';
%caches.push: $name => .&f;
}
}
$top.push: $cache => %caches;
}
$top
}
constant NOP = Supplier.new;
multi sub MAIN($delay?, Bool :$json) {
put '"bcache-stat": [' if $json;
if $delay {
react {
whenever Supply.interval($delay) {
put $json ?? to-json(MAIN(:perl)) ~ ',' !! MAIN(:text);
put "";
$*OUT.flush;
}
whenever $*OUT.t ?? key-pressed(:!echo) !! NOP {
when 'q' | 'Q' { done }
}
}
} else {
put $json ?? to-json(MAIN(:perl)) !! MAIN(:text);
}
put ']' if $json;
}