1
2
3
4
5
6
7
8#include "bcache.h"
9#include "stats.h"
10#include "btree.h"
11#include "sysfs.h"
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36static const unsigned DAY_RESCALE = 288;
37static const unsigned HOUR_RESCALE = 12;
38static const unsigned FIVE_MINUTE_RESCALE = 1;
39static const unsigned accounting_delay = (HZ * 300) / 22;
40static const unsigned accounting_weight = 32;
41
42
43
44read_attribute(cache_hits);
45read_attribute(cache_misses);
46read_attribute(cache_bypass_hits);
47read_attribute(cache_bypass_misses);
48read_attribute(cache_hit_ratio);
49read_attribute(cache_readaheads);
50read_attribute(cache_miss_collisions);
51read_attribute(bypassed);
52
53SHOW(bch_stats)
54{
55 struct cache_stats *s =
56 container_of(kobj, struct cache_stats, kobj);
57#define var(stat) (s->stat >> 16)
58 var_print(cache_hits);
59 var_print(cache_misses);
60 var_print(cache_bypass_hits);
61 var_print(cache_bypass_misses);
62
63 sysfs_print(cache_hit_ratio,
64 DIV_SAFE(var(cache_hits) * 100,
65 var(cache_hits) + var(cache_misses)));
66
67 var_print(cache_readaheads);
68 var_print(cache_miss_collisions);
69 sysfs_hprint(bypassed, var(sectors_bypassed) << 9);
70#undef var
71 return 0;
72}
73
74STORE(bch_stats)
75{
76 return size;
77}
78
79static void bch_stats_release(struct kobject *k)
80{
81}
82
83static struct attribute *bch_stats_files[] = {
84 &sysfs_cache_hits,
85 &sysfs_cache_misses,
86 &sysfs_cache_bypass_hits,
87 &sysfs_cache_bypass_misses,
88 &sysfs_cache_hit_ratio,
89 &sysfs_cache_readaheads,
90 &sysfs_cache_miss_collisions,
91 &sysfs_bypassed,
92 NULL
93};
94static KTYPE(bch_stats);
95
96int bch_cache_accounting_add_kobjs(struct cache_accounting *acc,
97 struct kobject *parent)
98{
99 int ret = kobject_add(&acc->total.kobj, parent,
100 "stats_total");
101 ret = ret ?: kobject_add(&acc->five_minute.kobj, parent,
102 "stats_five_minute");
103 ret = ret ?: kobject_add(&acc->hour.kobj, parent,
104 "stats_hour");
105 ret = ret ?: kobject_add(&acc->day.kobj, parent,
106 "stats_day");
107 return ret;
108}
109
110void bch_cache_accounting_clear(struct cache_accounting *acc)
111{
112 memset(&acc->total.cache_hits,
113 0,
114 sizeof(unsigned long) * 7);
115}
116
117void bch_cache_accounting_destroy(struct cache_accounting *acc)
118{
119 kobject_put(&acc->total.kobj);
120 kobject_put(&acc->five_minute.kobj);
121 kobject_put(&acc->hour.kobj);
122 kobject_put(&acc->day.kobj);
123
124 atomic_set(&acc->closing, 1);
125 if (del_timer_sync(&acc->timer))
126 closure_return(&acc->cl);
127}
128
129
130
131static void scale_stat(unsigned long *stat)
132{
133 *stat = ewma_add(*stat, 0, accounting_weight, 0);
134}
135
136static void scale_stats(struct cache_stats *stats, unsigned long rescale_at)
137{
138 if (++stats->rescale == rescale_at) {
139 stats->rescale = 0;
140 scale_stat(&stats->cache_hits);
141 scale_stat(&stats->cache_misses);
142 scale_stat(&stats->cache_bypass_hits);
143 scale_stat(&stats->cache_bypass_misses);
144 scale_stat(&stats->cache_readaheads);
145 scale_stat(&stats->cache_miss_collisions);
146 scale_stat(&stats->sectors_bypassed);
147 }
148}
149
150static void scale_accounting(unsigned long data)
151{
152 struct cache_accounting *acc = (struct cache_accounting *) data;
153
154#define move_stat(name) do { \
155 unsigned t = atomic_xchg(&acc->collector.name, 0); \
156 t <<= 16; \
157 acc->five_minute.name += t; \
158 acc->hour.name += t; \
159 acc->day.name += t; \
160 acc->total.name += t; \
161} while (0)
162
163 move_stat(cache_hits);
164 move_stat(cache_misses);
165 move_stat(cache_bypass_hits);
166 move_stat(cache_bypass_misses);
167 move_stat(cache_readaheads);
168 move_stat(cache_miss_collisions);
169 move_stat(sectors_bypassed);
170
171 scale_stats(&acc->total, 0);
172 scale_stats(&acc->day, DAY_RESCALE);
173 scale_stats(&acc->hour, HOUR_RESCALE);
174 scale_stats(&acc->five_minute, FIVE_MINUTE_RESCALE);
175
176 acc->timer.expires += accounting_delay;
177
178 if (!atomic_read(&acc->closing))
179 add_timer(&acc->timer);
180 else
181 closure_return(&acc->cl);
182}
183
184static void mark_cache_stats(struct cache_stat_collector *stats,
185 bool hit, bool bypass)
186{
187 if (!bypass)
188 if (hit)
189 atomic_inc(&stats->cache_hits);
190 else
191 atomic_inc(&stats->cache_misses);
192 else
193 if (hit)
194 atomic_inc(&stats->cache_bypass_hits);
195 else
196 atomic_inc(&stats->cache_bypass_misses);
197}
198
199void bch_mark_cache_accounting(struct cache_set *c, struct bcache_device *d,
200 bool hit, bool bypass)
201{
202 struct cached_dev *dc = container_of(d, struct cached_dev, disk);
203 mark_cache_stats(&dc->accounting.collector, hit, bypass);
204 mark_cache_stats(&c->accounting.collector, hit, bypass);
205}
206
207void bch_mark_cache_readahead(struct cache_set *c, struct bcache_device *d)
208{
209 struct cached_dev *dc = container_of(d, struct cached_dev, disk);
210 atomic_inc(&dc->accounting.collector.cache_readaheads);
211 atomic_inc(&c->accounting.collector.cache_readaheads);
212}
213
214void bch_mark_cache_miss_collision(struct cache_set *c, struct bcache_device *d)
215{
216 struct cached_dev *dc = container_of(d, struct cached_dev, disk);
217 atomic_inc(&dc->accounting.collector.cache_miss_collisions);
218 atomic_inc(&c->accounting.collector.cache_miss_collisions);
219}
220
221void bch_mark_sectors_bypassed(struct cache_set *c, struct cached_dev *dc,
222 int sectors)
223{
224 atomic_add(sectors, &dc->accounting.collector.sectors_bypassed);
225 atomic_add(sectors, &c->accounting.collector.sectors_bypassed);
226}
227
228void bch_cache_accounting_init(struct cache_accounting *acc,
229 struct closure *parent)
230{
231 kobject_init(&acc->total.kobj, &bch_stats_ktype);
232 kobject_init(&acc->five_minute.kobj, &bch_stats_ktype);
233 kobject_init(&acc->hour.kobj, &bch_stats_ktype);
234 kobject_init(&acc->day.kobj, &bch_stats_ktype);
235
236 closure_init(&acc->cl, parent);
237 init_timer(&acc->timer);
238 acc->timer.expires = jiffies + accounting_delay;
239 acc->timer.data = (unsigned long) acc;
240 acc->timer.function = scale_accounting;
241 add_timer(&acc->timer);
242}
243