stats.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * bcache stats code
  3. *
  4. * Copyright 2012 Google, Inc.
  5. */
  6. #include "bcache.h"
  7. #include "stats.h"
  8. #include "btree.h"
  9. #include "sysfs.h"
  10. /*
  11. * We keep absolute totals of various statistics, and addionally a set of three
  12. * rolling averages.
  13. *
  14. * Every so often, a timer goes off and rescales the rolling averages.
  15. * accounting_rescale[] is how many times the timer has to go off before we
  16. * rescale each set of numbers; that gets us half lives of 5 minutes, one hour,
  17. * and one day.
  18. *
  19. * accounting_delay is how often the timer goes off - 22 times in 5 minutes,
  20. * and accounting_weight is what we use to rescale:
  21. *
  22. * pow(31 / 32, 22) ~= 1/2
  23. *
  24. * So that we don't have to increment each set of numbers every time we (say)
  25. * get a cache hit, we increment a single atomic_t in acc->collector, and when
  26. * the rescale function runs it resets the atomic counter to 0 and adds its
  27. * old value to each of the exported numbers.
  28. *
  29. * To reduce rounding error, the numbers in struct cache_stats are all
  30. * stored left shifted by 16, and scaled back in the sysfs show() function.
  31. */
  32. static const unsigned DAY_RESCALE = 288;
  33. static const unsigned HOUR_RESCALE = 12;
  34. static const unsigned FIVE_MINUTE_RESCALE = 1;
  35. static const unsigned accounting_delay = (HZ * 300) / 22;
  36. static const unsigned accounting_weight = 32;
  37. /* sysfs reading/writing */
  38. read_attribute(cache_hits);
  39. read_attribute(cache_misses);
  40. read_attribute(cache_bypass_hits);
  41. read_attribute(cache_bypass_misses);
  42. read_attribute(cache_hit_ratio);
  43. read_attribute(cache_readaheads);
  44. read_attribute(cache_miss_collisions);
  45. read_attribute(bypassed);
  46. SHOW(bch_stats)
  47. {
  48. struct cache_stats *s =
  49. container_of(kobj, struct cache_stats, kobj);
  50. #define var(stat) (s->stat >> 16)
  51. var_print(cache_hits);
  52. var_print(cache_misses);
  53. var_print(cache_bypass_hits);
  54. var_print(cache_bypass_misses);
  55. sysfs_print(cache_hit_ratio,
  56. DIV_SAFE(var(cache_hits) * 100,
  57. var(cache_hits) + var(cache_misses)));
  58. var_print(cache_readaheads);
  59. var_print(cache_miss_collisions);
  60. sysfs_hprint(bypassed, var(sectors_bypassed) << 9);
  61. #undef var
  62. return 0;
  63. }
  64. STORE(bch_stats)
  65. {
  66. return size;
  67. }
  68. static void bch_stats_release(struct kobject *k)
  69. {
  70. }
  71. static struct attribute *bch_stats_files[] = {
  72. &sysfs_cache_hits,
  73. &sysfs_cache_misses,
  74. &sysfs_cache_bypass_hits,
  75. &sysfs_cache_bypass_misses,
  76. &sysfs_cache_hit_ratio,
  77. &sysfs_cache_readaheads,
  78. &sysfs_cache_miss_collisions,
  79. &sysfs_bypassed,
  80. NULL
  81. };
  82. static KTYPE(bch_stats);
  83. int bch_cache_accounting_add_kobjs(struct cache_accounting *acc,
  84. struct kobject *parent)
  85. {
  86. int ret = kobject_add(&acc->total.kobj, parent,
  87. "stats_total");
  88. ret = ret ?: kobject_add(&acc->five_minute.kobj, parent,
  89. "stats_five_minute");
  90. ret = ret ?: kobject_add(&acc->hour.kobj, parent,
  91. "stats_hour");
  92. ret = ret ?: kobject_add(&acc->day.kobj, parent,
  93. "stats_day");
  94. return ret;
  95. }
  96. void bch_cache_accounting_clear(struct cache_accounting *acc)
  97. {
  98. memset(&acc->total.cache_hits,
  99. 0,
  100. sizeof(unsigned long) * 7);
  101. }
  102. void bch_cache_accounting_destroy(struct cache_accounting *acc)
  103. {
  104. kobject_put(&acc->total.kobj);
  105. kobject_put(&acc->five_minute.kobj);
  106. kobject_put(&acc->hour.kobj);
  107. kobject_put(&acc->day.kobj);
  108. atomic_set(&acc->closing, 1);
  109. if (del_timer_sync(&acc->timer))
  110. closure_return(&acc->cl);
  111. }
  112. /* EWMA scaling */
  113. static void scale_stat(unsigned long *stat)
  114. {
  115. *stat = ewma_add(*stat, 0, accounting_weight, 0);
  116. }
  117. static void scale_stats(struct cache_stats *stats, unsigned long rescale_at)
  118. {
  119. if (++stats->rescale == rescale_at) {
  120. stats->rescale = 0;
  121. scale_stat(&stats->cache_hits);
  122. scale_stat(&stats->cache_misses);
  123. scale_stat(&stats->cache_bypass_hits);
  124. scale_stat(&stats->cache_bypass_misses);
  125. scale_stat(&stats->cache_readaheads);
  126. scale_stat(&stats->cache_miss_collisions);
  127. scale_stat(&stats->sectors_bypassed);
  128. }
  129. }
  130. static void scale_accounting(unsigned long data)
  131. {
  132. struct cache_accounting *acc = (struct cache_accounting *) data;
  133. #define move_stat(name) do { \
  134. unsigned t = atomic_xchg(&acc->collector.name, 0); \
  135. t <<= 16; \
  136. acc->five_minute.name += t; \
  137. acc->hour.name += t; \
  138. acc->day.name += t; \
  139. acc->total.name += t; \
  140. } while (0)
  141. move_stat(cache_hits);
  142. move_stat(cache_misses);
  143. move_stat(cache_bypass_hits);
  144. move_stat(cache_bypass_misses);
  145. move_stat(cache_readaheads);
  146. move_stat(cache_miss_collisions);
  147. move_stat(sectors_bypassed);
  148. scale_stats(&acc->total, 0);
  149. scale_stats(&acc->day, DAY_RESCALE);
  150. scale_stats(&acc->hour, HOUR_RESCALE);
  151. scale_stats(&acc->five_minute, FIVE_MINUTE_RESCALE);
  152. acc->timer.expires += accounting_delay;
  153. if (!atomic_read(&acc->closing))
  154. add_timer(&acc->timer);
  155. else
  156. closure_return(&acc->cl);
  157. }
  158. static void mark_cache_stats(struct cache_stat_collector *stats,
  159. bool hit, bool bypass)
  160. {
  161. if (!bypass)
  162. if (hit)
  163. atomic_inc(&stats->cache_hits);
  164. else
  165. atomic_inc(&stats->cache_misses);
  166. else
  167. if (hit)
  168. atomic_inc(&stats->cache_bypass_hits);
  169. else
  170. atomic_inc(&stats->cache_bypass_misses);
  171. }
  172. void bch_mark_cache_accounting(struct cache_set *c, struct bcache_device *d,
  173. bool hit, bool bypass)
  174. {
  175. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  176. mark_cache_stats(&dc->accounting.collector, hit, bypass);
  177. mark_cache_stats(&c->accounting.collector, hit, bypass);
  178. }
  179. void bch_mark_cache_readahead(struct cache_set *c, struct bcache_device *d)
  180. {
  181. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  182. atomic_inc(&dc->accounting.collector.cache_readaheads);
  183. atomic_inc(&c->accounting.collector.cache_readaheads);
  184. }
  185. void bch_mark_cache_miss_collision(struct cache_set *c, struct bcache_device *d)
  186. {
  187. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  188. atomic_inc(&dc->accounting.collector.cache_miss_collisions);
  189. atomic_inc(&c->accounting.collector.cache_miss_collisions);
  190. }
  191. void bch_mark_sectors_bypassed(struct cache_set *c, struct cached_dev *dc,
  192. int sectors)
  193. {
  194. atomic_add(sectors, &dc->accounting.collector.sectors_bypassed);
  195. atomic_add(sectors, &c->accounting.collector.sectors_bypassed);
  196. }
  197. void bch_cache_accounting_init(struct cache_accounting *acc,
  198. struct closure *parent)
  199. {
  200. kobject_init(&acc->total.kobj, &bch_stats_ktype);
  201. kobject_init(&acc->five_minute.kobj, &bch_stats_ktype);
  202. kobject_init(&acc->hour.kobj, &bch_stats_ktype);
  203. kobject_init(&acc->day.kobj, &bch_stats_ktype);
  204. closure_init(&acc->cl, parent);
  205. init_timer(&acc->timer);
  206. acc->timer.expires = jiffies + accounting_delay;
  207. acc->timer.data = (unsigned long) acc;
  208. acc->timer.function = scale_accounting;
  209. add_timer(&acc->timer);
  210. }