4 * Copyright 2012 Google, Inc.
12 * We keep absolute totals of various statistics, and addionally a set of three
15 * Every so often, a timer goes off and rescales the rolling averages.
16 * accounting_rescale[] is how many times the timer has to go off before we
17 * rescale each set of numbers; that gets us half lives of 5 minutes, one hour,
20 * accounting_delay is how often the timer goes off - 22 times in 5 minutes,
21 * and accounting_weight is what we use to rescale:
23 * pow(31 / 32, 22) ~= 1/2
25 * So that we don't have to increment each set of numbers every time we (say)
26 * get a cache hit, we increment a single atomic_t in acc->collector, and when
27 * the rescale function runs it resets the atomic counter to 0 and adds its
28 * old value to each of the exported numbers.
30 * To reduce rounding error, the numbers in struct cache_stats are all
31 * stored left shifted by 16, and scaled back in the sysfs show() function.
34 static const unsigned DAY_RESCALE = 288;
35 static const unsigned HOUR_RESCALE = 12;
36 static const unsigned FIVE_MINUTE_RESCALE = 1;
37 static const unsigned accounting_delay = (HZ * 300) / 22;
38 static const unsigned accounting_weight = 5;
40 /* sysfs reading/writing */
42 read_attribute(cache_hits);
43 read_attribute(cache_misses);
44 read_attribute(cache_bypass_hits);
45 read_attribute(cache_bypass_misses);
46 read_attribute(cache_hit_ratio);
47 read_attribute(cache_readaheads);
48 read_attribute(cache_miss_collisions);
49 read_attribute(bypassed);
50 read_attribute(foreground_write_ratio);
51 read_attribute(foreground_writes);
52 read_attribute(gc_writes);
53 read_attribute(discards);
57 struct cache_stats *s =
58 container_of(kobj, struct cache_stats, kobj);
59 #define var(stat) (s->stat >> 16)
60 var_print(cache_hits);
61 var_print(cache_misses);
62 var_print(cache_bypass_hits);
63 var_print(cache_bypass_misses);
65 sysfs_print(cache_hit_ratio,
66 DIV_SAFE(var(cache_hits) * 100,
67 var(cache_hits) + var(cache_misses)));
69 var_print(cache_readaheads);
70 var_print(cache_miss_collisions);
72 sysfs_hprint(bypassed, var(sectors_bypassed) << 9);
73 sysfs_hprint(foreground_writes, var(foreground_write_sectors) << 9);
74 sysfs_hprint(gc_writes, var(gc_write_sectors) << 9);
75 sysfs_hprint(discards, var(discard_sectors) << 9);
77 sysfs_print(foreground_write_ratio,
78 DIV_SAFE(var(foreground_write_sectors) * 100,
79 var(foreground_write_sectors) +
80 var(gc_write_sectors)));
90 static void bch_stats_release(struct kobject *k)
94 static struct attribute *bch_stats_files[] = {
97 &sysfs_cache_bypass_hits,
98 &sysfs_cache_bypass_misses,
99 &sysfs_cache_hit_ratio,
100 &sysfs_cache_readaheads,
101 &sysfs_cache_miss_collisions,
103 &sysfs_foreground_write_ratio,
104 &sysfs_foreground_writes,
109 static KTYPE(bch_stats);
111 int bch_cache_accounting_add_kobjs(struct cache_accounting *acc,
112 struct kobject *parent)
114 int ret = kobject_add(&acc->total.kobj, parent,
116 ret = ret ?: kobject_add(&acc->five_minute.kobj, parent,
117 "stats_five_minute");
118 ret = ret ?: kobject_add(&acc->hour.kobj, parent,
120 ret = ret ?: kobject_add(&acc->day.kobj, parent,
125 void bch_cache_accounting_clear(struct cache_accounting *acc)
127 memset(&acc->total.cache_hits,
129 sizeof(unsigned long) * 9);
132 void bch_cache_accounting_destroy(struct cache_accounting *acc)
134 kobject_put(&acc->total.kobj);
135 kobject_put(&acc->five_minute.kobj);
136 kobject_put(&acc->hour.kobj);
137 kobject_put(&acc->day.kobj);
139 atomic_set(&acc->closing, 1);
140 if (del_timer_sync(&acc->timer))
141 closure_return(&acc->cl);
146 static void scale_stat(unsigned long *stat)
148 *stat = ewma_add(*stat, 0, accounting_weight);
151 static void scale_stats(struct cache_stats *stats, unsigned long rescale_at)
153 if (++stats->rescale == rescale_at) {
155 scale_stat(&stats->cache_hits);
156 scale_stat(&stats->cache_misses);
157 scale_stat(&stats->cache_bypass_hits);
158 scale_stat(&stats->cache_bypass_misses);
159 scale_stat(&stats->cache_readaheads);
160 scale_stat(&stats->cache_miss_collisions);
161 scale_stat(&stats->sectors_bypassed);
162 scale_stat(&stats->foreground_write_sectors);
163 scale_stat(&stats->gc_write_sectors);
164 scale_stat(&stats->discard_sectors);
168 static void scale_accounting(unsigned long data)
170 struct cache_accounting *acc = (struct cache_accounting *) data;
172 #define move_stat(name) do { \
173 unsigned t = atomic_xchg(&acc->collector.name, 0); \
175 acc->five_minute.name += t; \
176 acc->hour.name += t; \
177 acc->day.name += t; \
178 acc->total.name += t; \
181 move_stat(cache_hits);
182 move_stat(cache_misses);
183 move_stat(cache_bypass_hits);
184 move_stat(cache_bypass_misses);
185 move_stat(cache_readaheads);
186 move_stat(cache_miss_collisions);
187 move_stat(sectors_bypassed);
188 move_stat(foreground_write_sectors);
189 move_stat(gc_write_sectors);
190 move_stat(discard_sectors);
192 scale_stats(&acc->total, 0);
193 scale_stats(&acc->day, DAY_RESCALE);
194 scale_stats(&acc->hour, HOUR_RESCALE);
195 scale_stats(&acc->five_minute, FIVE_MINUTE_RESCALE);
197 acc->timer.expires += accounting_delay;
199 if (!atomic_read(&acc->closing))
200 add_timer(&acc->timer);
202 closure_return(&acc->cl);
205 void bch_cache_accounting_init(struct cache_accounting *acc,
206 struct closure *parent)
208 kobject_init(&acc->total.kobj, &bch_stats_ktype);
209 kobject_init(&acc->five_minute.kobj, &bch_stats_ktype);
210 kobject_init(&acc->hour.kobj, &bch_stats_ktype);
211 kobject_init(&acc->day.kobj, &bch_stats_ktype);
213 closure_init(&acc->cl, parent);
214 init_timer(&acc->timer);
215 acc->timer.expires = jiffies + accounting_delay;
216 acc->timer.data = (unsigned long) acc;
217 acc->timer.function = scale_accounting;
218 add_timer(&acc->timer);