1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright 2012 Google, Inc.
14 * We keep absolute totals of various statistics, and addionally a set of three
17 * Every so often, a timer goes off and rescales the rolling averages.
18 * accounting_rescale[] is how many times the timer has to go off before we
19 * rescale each set of numbers; that gets us half lives of 5 minutes, one hour,
22 * accounting_delay is how often the timer goes off - 22 times in 5 minutes,
23 * and accounting_weight is what we use to rescale:
25 * pow(31 / 32, 22) ~= 1/2
27 * So that we don't have to increment each set of numbers every time we (say)
28 * get a cache hit, we increment a single atomic_t in acc->collector, and when
29 * the rescale function runs it resets the atomic counter to 0 and adds its
30 * old value to each of the exported numbers.
32 * To reduce rounding error, the numbers in struct cache_stats are all
33 * stored left shifted by 16, and scaled back in the sysfs show() function.
36 static const unsigned int DAY_RESCALE
= 288;
37 static const unsigned int HOUR_RESCALE
= 12;
38 static const unsigned int FIVE_MINUTE_RESCALE
= 1;
39 static const unsigned int accounting_delay
= (HZ
* 300) / 22;
40 static const unsigned int accounting_weight
= 32;
42 /* sysfs reading/writing */
44 read_attribute(cache_hits
);
45 read_attribute(cache_misses
);
46 read_attribute(cache_bypass_hits
);
47 read_attribute(cache_bypass_misses
);
48 read_attribute(cache_hit_ratio
);
49 read_attribute(cache_readaheads
);
50 read_attribute(cache_miss_collisions
);
51 read_attribute(bypassed
);
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
);
63 sysfs_print(cache_hit_ratio
,
64 DIV_SAFE(var(cache_hits
) * 100,
65 var(cache_hits
) + var(cache_misses
)));
67 var_print(cache_readaheads
);
68 var_print(cache_miss_collisions
);
69 sysfs_hprint(bypassed
, var(sectors_bypassed
) << 9);
79 static void bch_stats_release(struct kobject
*k
)
83 static struct attribute
*bch_stats_files
[] = {
86 &sysfs_cache_bypass_hits
,
87 &sysfs_cache_bypass_misses
,
88 &sysfs_cache_hit_ratio
,
89 &sysfs_cache_readaheads
,
90 &sysfs_cache_miss_collisions
,
94 static KTYPE(bch_stats
);
96 int bch_cache_accounting_add_kobjs(struct cache_accounting
*acc
,
97 struct kobject
*parent
)
99 int ret
= kobject_add(&acc
->total
.kobj
, parent
,
101 ret
= ret
?: kobject_add(&acc
->five_minute
.kobj
, parent
,
102 "stats_five_minute");
103 ret
= ret
?: kobject_add(&acc
->hour
.kobj
, parent
,
105 ret
= ret
?: kobject_add(&acc
->day
.kobj
, parent
,
110 void bch_cache_accounting_clear(struct cache_accounting
*acc
)
112 acc
->total
.cache_hits
= 0;
113 acc
->total
.cache_misses
= 0;
114 acc
->total
.cache_bypass_hits
= 0;
115 acc
->total
.cache_bypass_misses
= 0;
116 acc
->total
.cache_readaheads
= 0;
117 acc
->total
.cache_miss_collisions
= 0;
118 acc
->total
.sectors_bypassed
= 0;
121 void bch_cache_accounting_destroy(struct cache_accounting
*acc
)
123 kobject_put(&acc
->total
.kobj
);
124 kobject_put(&acc
->five_minute
.kobj
);
125 kobject_put(&acc
->hour
.kobj
);
126 kobject_put(&acc
->day
.kobj
);
128 atomic_set(&acc
->closing
, 1);
129 if (del_timer_sync(&acc
->timer
))
130 closure_return(&acc
->cl
);
135 static void scale_stat(unsigned long *stat
)
137 *stat
= ewma_add(*stat
, 0, accounting_weight
, 0);
140 static void scale_stats(struct cache_stats
*stats
, unsigned long rescale_at
)
142 if (++stats
->rescale
== rescale_at
) {
144 scale_stat(&stats
->cache_hits
);
145 scale_stat(&stats
->cache_misses
);
146 scale_stat(&stats
->cache_bypass_hits
);
147 scale_stat(&stats
->cache_bypass_misses
);
148 scale_stat(&stats
->cache_readaheads
);
149 scale_stat(&stats
->cache_miss_collisions
);
150 scale_stat(&stats
->sectors_bypassed
);
154 static void scale_accounting(struct timer_list
*t
)
156 struct cache_accounting
*acc
= from_timer(acc
, t
, timer
);
158 #define move_stat(name) do { \
159 unsigned int t = atomic_xchg(&acc->collector.name, 0); \
161 acc->five_minute.name += t; \
162 acc->hour.name += t; \
163 acc->day.name += t; \
164 acc->total.name += t; \
167 move_stat(cache_hits
);
168 move_stat(cache_misses
);
169 move_stat(cache_bypass_hits
);
170 move_stat(cache_bypass_misses
);
171 move_stat(cache_readaheads
);
172 move_stat(cache_miss_collisions
);
173 move_stat(sectors_bypassed
);
175 scale_stats(&acc
->total
, 0);
176 scale_stats(&acc
->day
, DAY_RESCALE
);
177 scale_stats(&acc
->hour
, HOUR_RESCALE
);
178 scale_stats(&acc
->five_minute
, FIVE_MINUTE_RESCALE
);
180 acc
->timer
.expires
+= accounting_delay
;
182 if (!atomic_read(&acc
->closing
))
183 add_timer(&acc
->timer
);
185 closure_return(&acc
->cl
);
188 static void mark_cache_stats(struct cache_stat_collector
*stats
,
189 bool hit
, bool bypass
)
193 atomic_inc(&stats
->cache_hits
);
195 atomic_inc(&stats
->cache_misses
);
198 atomic_inc(&stats
->cache_bypass_hits
);
200 atomic_inc(&stats
->cache_bypass_misses
);
203 void bch_mark_cache_accounting(struct cache_set
*c
, struct bcache_device
*d
,
204 bool hit
, bool bypass
)
206 struct cached_dev
*dc
= container_of(d
, struct cached_dev
, disk
);
208 mark_cache_stats(&dc
->accounting
.collector
, hit
, bypass
);
209 mark_cache_stats(&c
->accounting
.collector
, hit
, bypass
);
212 void bch_mark_cache_readahead(struct cache_set
*c
, struct bcache_device
*d
)
214 struct cached_dev
*dc
= container_of(d
, struct cached_dev
, disk
);
216 atomic_inc(&dc
->accounting
.collector
.cache_readaheads
);
217 atomic_inc(&c
->accounting
.collector
.cache_readaheads
);
220 void bch_mark_cache_miss_collision(struct cache_set
*c
, struct bcache_device
*d
)
222 struct cached_dev
*dc
= container_of(d
, struct cached_dev
, disk
);
224 atomic_inc(&dc
->accounting
.collector
.cache_miss_collisions
);
225 atomic_inc(&c
->accounting
.collector
.cache_miss_collisions
);
228 void bch_mark_sectors_bypassed(struct cache_set
*c
, struct cached_dev
*dc
,
231 atomic_add(sectors
, &dc
->accounting
.collector
.sectors_bypassed
);
232 atomic_add(sectors
, &c
->accounting
.collector
.sectors_bypassed
);
235 void bch_cache_accounting_init(struct cache_accounting
*acc
,
236 struct closure
*parent
)
238 kobject_init(&acc
->total
.kobj
, &bch_stats_ktype
);
239 kobject_init(&acc
->five_minute
.kobj
, &bch_stats_ktype
);
240 kobject_init(&acc
->hour
.kobj
, &bch_stats_ktype
);
241 kobject_init(&acc
->day
.kobj
, &bch_stats_ktype
);
243 closure_init(&acc
->cl
, parent
);
244 timer_setup(&acc
->timer
, scale_accounting
, 0);
245 acc
->timer
.expires
= jiffies
+ accounting_delay
;
246 add_timer(&acc
->timer
);