blk-cgroup-rwstat.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* SPDX-License-Identifier: GPL-2.0
  2. *
  3. * Legacy blkg rwstat helpers enabled by CONFIG_BLK_CGROUP_RWSTAT.
  4. * Do not use in new code.
  5. */
  6. #include "blk-cgroup-rwstat.h"
  7. int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp)
  8. {
  9. int i, ret;
  10. for (i = 0; i < BLKG_RWSTAT_NR; i++) {
  11. ret = percpu_counter_init(&rwstat->cpu_cnt[i], 0, gfp);
  12. if (ret) {
  13. while (--i >= 0)
  14. percpu_counter_destroy(&rwstat->cpu_cnt[i]);
  15. return ret;
  16. }
  17. atomic64_set(&rwstat->aux_cnt[i], 0);
  18. }
  19. return 0;
  20. }
  21. EXPORT_SYMBOL_GPL(blkg_rwstat_init);
  22. void blkg_rwstat_exit(struct blkg_rwstat *rwstat)
  23. {
  24. int i;
  25. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  26. percpu_counter_destroy(&rwstat->cpu_cnt[i]);
  27. }
  28. EXPORT_SYMBOL_GPL(blkg_rwstat_exit);
  29. /**
  30. * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
  31. * @sf: seq_file to print to
  32. * @pd: policy private data of interest
  33. * @rwstat: rwstat to print
  34. *
  35. * Print @rwstat to @sf for the device assocaited with @pd.
  36. */
  37. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  38. const struct blkg_rwstat_sample *rwstat)
  39. {
  40. static const char *rwstr[] = {
  41. [BLKG_RWSTAT_READ] = "Read",
  42. [BLKG_RWSTAT_WRITE] = "Write",
  43. [BLKG_RWSTAT_SYNC] = "Sync",
  44. [BLKG_RWSTAT_ASYNC] = "Async",
  45. [BLKG_RWSTAT_DISCARD] = "Discard",
  46. };
  47. const char *dname = blkg_dev_name(pd->blkg);
  48. u64 v;
  49. int i;
  50. if (!dname)
  51. return 0;
  52. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  53. seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
  54. rwstat->cnt[i]);
  55. v = rwstat->cnt[BLKG_RWSTAT_READ] +
  56. rwstat->cnt[BLKG_RWSTAT_WRITE] +
  57. rwstat->cnt[BLKG_RWSTAT_DISCARD];
  58. seq_printf(sf, "%s Total %llu\n", dname, v);
  59. return v;
  60. }
  61. EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);
  62. /**
  63. * blkg_prfill_rwstat - prfill callback for blkg_rwstat
  64. * @sf: seq_file to print to
  65. * @pd: policy private data of interest
  66. * @off: offset to the blkg_rwstat in @pd
  67. *
  68. * prfill callback for printing a blkg_rwstat.
  69. */
  70. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  71. int off)
  72. {
  73. struct blkg_rwstat_sample rwstat = { };
  74. blkg_rwstat_read((void *)pd + off, &rwstat);
  75. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  76. }
  77. EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
  78. /**
  79. * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
  80. * @blkg: blkg of interest
  81. * @pol: blkcg_policy which contains the blkg_rwstat
  82. * @off: offset to the blkg_rwstat in blkg_policy_data or @blkg
  83. * @sum: blkg_rwstat_sample structure containing the results
  84. *
  85. * Collect the blkg_rwstat specified by @blkg, @pol and @off and all its
  86. * online descendants and their aux counts. The caller must be holding the
  87. * queue lock for online tests.
  88. *
  89. * If @pol is NULL, blkg_rwstat is at @off bytes into @blkg; otherwise, it
  90. * is at @off bytes into @blkg's blkg_policy_data of the policy.
  91. */
  92. void blkg_rwstat_recursive_sum(struct blkcg_gq *blkg, struct blkcg_policy *pol,
  93. int off, struct blkg_rwstat_sample *sum)
  94. {
  95. struct blkcg_gq *pos_blkg;
  96. struct cgroup_subsys_state *pos_css;
  97. unsigned int i;
  98. lockdep_assert_held(&blkg->q->queue_lock);
  99. memset(sum, 0, sizeof(*sum));
  100. rcu_read_lock();
  101. blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
  102. struct blkg_rwstat *rwstat;
  103. if (!pos_blkg->online)
  104. continue;
  105. if (pol)
  106. rwstat = (void *)blkg_to_pd(pos_blkg, pol) + off;
  107. else
  108. rwstat = (void *)pos_blkg + off;
  109. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  110. sum->cnt[i] += blkg_rwstat_read_counter(rwstat, i);
  111. }
  112. rcu_read_unlock();
  113. }
  114. EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);