stats.c 6.6 KB

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