]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcache/stats.c
bcache in userspace; userspace fsck
[bcachefs-tools-debian] / libbcache / stats.c
1 /*
2  * bcache stats code
3  *
4  * Copyright 2012 Google, Inc.
5  */
6
7 #include "bcache.h"
8 #include "stats.h"
9 #include "sysfs.h"
10
11 /*
12  * We keep absolute totals of various statistics, and addionally a set of three
13  * rolling averages.
14  *
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,
18  * and one day.
19  *
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:
22  *
23  * pow(31 / 32, 22) ~= 1/2
24  *
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.
29  *
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.
32  */
33
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;
39
40 /* sysfs reading/writing */
41
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);
54
55 SHOW(bch_stats)
56 {
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);
64
65         sysfs_print(cache_hit_ratio,
66                     DIV_SAFE(var(cache_hits) * 100,
67                              var(cache_hits) + var(cache_misses)));
68
69         var_print(cache_readaheads);
70         var_print(cache_miss_collisions);
71
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);
76
77         sysfs_print(foreground_write_ratio,
78                     DIV_SAFE(var(foreground_write_sectors) * 100,
79                              var(foreground_write_sectors) +
80                              var(gc_write_sectors)));
81 #undef var
82         return 0;
83 }
84
85 STORE(bch_stats)
86 {
87         return size;
88 }
89
90 static void bch_stats_release(struct kobject *k)
91 {
92 }
93
94 static struct attribute *bch_stats_files[] = {
95         &sysfs_cache_hits,
96         &sysfs_cache_misses,
97         &sysfs_cache_bypass_hits,
98         &sysfs_cache_bypass_misses,
99         &sysfs_cache_hit_ratio,
100         &sysfs_cache_readaheads,
101         &sysfs_cache_miss_collisions,
102         &sysfs_bypassed,
103         &sysfs_foreground_write_ratio,
104         &sysfs_foreground_writes,
105         &sysfs_gc_writes,
106         &sysfs_discards,
107         NULL
108 };
109 static KTYPE(bch_stats);
110
111 int bch_cache_accounting_add_kobjs(struct cache_accounting *acc,
112                                    struct kobject *parent)
113 {
114         int ret = kobject_add(&acc->total.kobj, parent,
115                               "stats_total");
116         ret = ret ?: kobject_add(&acc->five_minute.kobj, parent,
117                                  "stats_five_minute");
118         ret = ret ?: kobject_add(&acc->hour.kobj, parent,
119                                  "stats_hour");
120         ret = ret ?: kobject_add(&acc->day.kobj, parent,
121                                  "stats_day");
122         return ret;
123 }
124
125 void bch_cache_accounting_clear(struct cache_accounting *acc)
126 {
127         memset(&acc->total.cache_hits,
128                0,
129                sizeof(unsigned long) * 9);
130 }
131
132 void bch_cache_accounting_destroy(struct cache_accounting *acc)
133 {
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);
138
139         atomic_set(&acc->closing, 1);
140         if (del_timer_sync(&acc->timer))
141                 closure_return(&acc->cl);
142 }
143
144 /* EWMA scaling */
145
146 static void scale_stat(unsigned long *stat)
147 {
148         *stat =  ewma_add(*stat, 0, accounting_weight);
149 }
150
151 static void scale_stats(struct cache_stats *stats, unsigned long rescale_at)
152 {
153         if (++stats->rescale == rescale_at) {
154                 stats->rescale = 0;
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);
165         }
166 }
167
168 static void scale_accounting(unsigned long data)
169 {
170         struct cache_accounting *acc = (struct cache_accounting *) data;
171
172 #define move_stat(name) do {                                            \
173         unsigned t = atomic_xchg(&acc->collector.name, 0);              \
174         t <<= 16;                                                       \
175         acc->five_minute.name += t;                                     \
176         acc->hour.name += t;                                            \
177         acc->day.name += t;                                             \
178         acc->total.name += t;                                           \
179 } while (0)
180
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);
191
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);
196
197         acc->timer.expires += accounting_delay;
198
199         if (!atomic_read(&acc->closing))
200                 add_timer(&acc->timer);
201         else
202                 closure_return(&acc->cl);
203 }
204
205 void bch_cache_accounting_init(struct cache_accounting *acc,
206                                struct closure *parent)
207 {
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);
212
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);
219 }