stats.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 int DAY_RESCALE = 288;
  34. static const unsigned int HOUR_RESCALE = 12;
  35. static const unsigned int FIVE_MINUTE_RESCALE = 1;
  36. static const unsigned int accounting_delay = (HZ * 300) / 22;
  37. static const unsigned int 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. acc->total.cache_hits = 0;
  100. acc->total.cache_misses = 0;
  101. acc->total.cache_bypass_hits = 0;
  102. acc->total.cache_bypass_misses = 0;
  103. acc->total.cache_readaheads = 0;
  104. acc->total.cache_miss_collisions = 0;
  105. acc->total.sectors_bypassed = 0;
  106. }
  107. void bch_cache_accounting_destroy(struct cache_accounting *acc)
  108. {
  109. kobject_put(&acc->total.kobj);
  110. kobject_put(&acc->five_minute.kobj);
  111. kobject_put(&acc->hour.kobj);
  112. kobject_put(&acc->day.kobj);
  113. atomic_set(&acc->closing, 1);
  114. if (del_timer_sync(&acc->timer))
  115. closure_return(&acc->cl);
  116. }
  117. /* EWMA scaling */
  118. static void scale_stat(unsigned long *stat)
  119. {
  120. *stat = ewma_add(*stat, 0, accounting_weight, 0);
  121. }
  122. static void scale_stats(struct cache_stats *stats, unsigned long rescale_at)
  123. {
  124. if (++stats->rescale == rescale_at) {
  125. stats->rescale = 0;
  126. scale_stat(&stats->cache_hits);
  127. scale_stat(&stats->cache_misses);
  128. scale_stat(&stats->cache_bypass_hits);
  129. scale_stat(&stats->cache_bypass_misses);
  130. scale_stat(&stats->cache_readaheads);
  131. scale_stat(&stats->cache_miss_collisions);
  132. scale_stat(&stats->sectors_bypassed);
  133. }
  134. }
  135. static void scale_accounting(struct timer_list *t)
  136. {
  137. struct cache_accounting *acc = from_timer(acc, t, timer);
  138. #define move_stat(name) do { \
  139. unsigned int t = atomic_xchg(&acc->collector.name, 0); \
  140. t <<= 16; \
  141. acc->five_minute.name += t; \
  142. acc->hour.name += t; \
  143. acc->day.name += t; \
  144. acc->total.name += t; \
  145. } while (0)
  146. move_stat(cache_hits);
  147. move_stat(cache_misses);
  148. move_stat(cache_bypass_hits);
  149. move_stat(cache_bypass_misses);
  150. move_stat(cache_readaheads);
  151. move_stat(cache_miss_collisions);
  152. move_stat(sectors_bypassed);
  153. scale_stats(&acc->total, 0);
  154. scale_stats(&acc->day, DAY_RESCALE);
  155. scale_stats(&acc->hour, HOUR_RESCALE);
  156. scale_stats(&acc->five_minute, FIVE_MINUTE_RESCALE);
  157. acc->timer.expires += accounting_delay;
  158. if (!atomic_read(&acc->closing))
  159. add_timer(&acc->timer);
  160. else
  161. closure_return(&acc->cl);
  162. }
  163. static void mark_cache_stats(struct cache_stat_collector *stats,
  164. bool hit, bool bypass)
  165. {
  166. if (!bypass)
  167. if (hit)
  168. atomic_inc(&stats->cache_hits);
  169. else
  170. atomic_inc(&stats->cache_misses);
  171. else
  172. if (hit)
  173. atomic_inc(&stats->cache_bypass_hits);
  174. else
  175. atomic_inc(&stats->cache_bypass_misses);
  176. }
  177. void bch_mark_cache_accounting(struct cache_set *c, struct bcache_device *d,
  178. bool hit, bool bypass)
  179. {
  180. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  181. mark_cache_stats(&dc->accounting.collector, hit, bypass);
  182. mark_cache_stats(&c->accounting.collector, hit, bypass);
  183. }
  184. void bch_mark_cache_readahead(struct cache_set *c, struct bcache_device *d)
  185. {
  186. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  187. atomic_inc(&dc->accounting.collector.cache_readaheads);
  188. atomic_inc(&c->accounting.collector.cache_readaheads);
  189. }
  190. void bch_mark_cache_miss_collision(struct cache_set *c, struct bcache_device *d)
  191. {
  192. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  193. atomic_inc(&dc->accounting.collector.cache_miss_collisions);
  194. atomic_inc(&c->accounting.collector.cache_miss_collisions);
  195. }
  196. void bch_mark_sectors_bypassed(struct cache_set *c, struct cached_dev *dc,
  197. int sectors)
  198. {
  199. atomic_add(sectors, &dc->accounting.collector.sectors_bypassed);
  200. atomic_add(sectors, &c->accounting.collector.sectors_bypassed);
  201. }
  202. void bch_cache_accounting_init(struct cache_accounting *acc,
  203. struct closure *parent)
  204. {
  205. kobject_init(&acc->total.kobj, &bch_stats_ktype);
  206. kobject_init(&acc->five_minute.kobj, &bch_stats_ktype);
  207. kobject_init(&acc->hour.kobj, &bch_stats_ktype);
  208. kobject_init(&acc->day.kobj, &bch_stats_ktype);
  209. closure_init(&acc->cl, parent);
  210. timer_setup(&acc->timer, scale_accounting, 0);
  211. acc->timer.expires = jiffies + accounting_delay;
  212. add_timer(&acc->timer);
  213. }