analysisOfTheCausesOfTheFluctuation.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Interface for controlling IO bandwidth on a request queue
  3. *
  4. * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/bio.h>
  10. #include <linux/blktrace_api.h>
  11. #include "blk-cgroup.h"
  12. #include "blk.h"
  13. /* Max dispatch from a group in 1 round */
  14. static int throtl_grp_quantum = 8;
  15. /* Total max dispatch from all groups in one round */
  16. static int throtl_quantum = 32;
  17. /* Throttling is performed over 100ms slice and after that slice is renewed */
  18. static unsigned long throtl_slice = HZ/10; /* 100 ms */
  19. static struct blkcg_policy blkcg_policy_throtl;
  20. /* A workqueue to queue throttle related work */
  21. static struct workqueue_struct *kthrotld_workqueue;
  22. static void throtl_schedule_delayed_work(struct throtl_data *td,
  23. unsigned long delay);
  24. struct throtl_rb_root {
  25. struct rb_root rb;
  26. struct rb_node *left;
  27. unsigned int count;
  28. unsigned long min_disptime;
  29. };
  30. #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
  31. .count = 0, .min_disptime = 0}
  32. #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
  33. /* Per-cpu group stats */
  34. struct tg_stats_cpu {
  35. /* total bytes transferred */
  36. struct blkg_rwstat service_bytes;
  37. /* total IOs serviced, post merge */
  38. struct blkg_rwstat serviced;
  39. };
  40. struct throtl_grp {
  41. /* must be the first member */
  42. struct blkg_policy_data pd;
  43. /* active throtl group service_tree member */
  44. struct rb_node rb_node;
  45. /*
  46. * Dispatch time in jiffies. This is the estimated time when group
  47. * will unthrottle and is ready to dispatch more bio. It is used as
  48. * key to sort active groups in service tree.
  49. */
  50. unsigned long disptime;
  51. unsigned int flags;
  52. /* Two lists for READ and WRITE */
  53. struct bio_list bio_lists[2];
  54. /* Number of queued bios on READ and WRITE lists */
  55. unsigned int nr_queued[2];
  56. /* bytes per second rate limits */
  57. uint64_t bps[2];
  58. /* IOPS limits */
  59. unsigned int iops[2];
  60. /* Number of bytes disptached in current slice */
  61. uint64_t bytes_disp[2];
  62. /* Number of bio's dispatched in current slice */
  63. unsigned int io_disp[2];
  64. /* When did we start a new slice */
  65. unsigned long slice_start[2];
  66. unsigned long slice_end[2];
  67. /* Some throttle limits got updated for the group */
  68. int limits_changed;
  69. /* Per cpu stats pointer */
  70. struct tg_stats_cpu __percpu *stats_cpu;
  71. /* List of tgs waiting for per cpu stats memory to be allocated */
  72. struct list_head stats_alloc_node;
  73. };
  74. struct throtl_data
  75. {
  76. /* service tree for active throtl groups */
  77. struct throtl_rb_root tg_service_tree;
  78. struct request_queue *queue;
  79. /* Total Number of queued bios on READ and WRITE lists */
  80. unsigned int nr_queued[2];
  81. /*
  82. * number of total undestroyed groups
  83. */
  84. unsigned int nr_undestroyed_grps;
  85. /* Work for dispatching throttled bios */
  86. struct delayed_work throtl_work;
  87. int limits_changed;
  88. };
  89. /* list and work item to allocate percpu group stats */
  90. static DEFINE_SPINLOCK(tg_stats_alloc_lock);
  91. static LIST_HEAD(tg_stats_alloc_list);
  92. static void tg_stats_alloc_fn(struct work_struct *);
  93. static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
  94. static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
  95. {
  96. return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
  97. }
  98. static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
  99. {
  100. return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
  101. }
  102. static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
  103. {
  104. return pd_to_blkg(&tg->pd);
  105. }
  106. static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
  107. {
  108. return blkg_to_tg(td->queue->root_blkg);
  109. }
  110. enum tg_state_flags {
  111. THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
  112. };
  113. #define THROTL_TG_FNS(name) \
  114. static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
  115. { \
  116. (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
  117. } \
  118. static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
  119. { \
  120. (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
  121. } \
  122. static inline int throtl_tg_##name(const struct throtl_grp *tg) \
  123. { \
  124. return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
  125. }
  126. THROTL_TG_FNS(on_rr);
  127. #define throtl_log_tg(td, tg, fmt, args...) do { \
  128. char __pbuf[128]; \
  129. \
  130. blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \
  131. blk_add_trace_msg((td)->queue, "throtl %s " fmt, __pbuf, ##args); \
  132. } while (0)
  133. #define throtl_log(td, fmt, args...) \
  134. blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
  135. static inline unsigned int total_nr_queued(struct throtl_data *td)
  136. {
  137. return td->nr_queued[0] + td->nr_queued[1];
  138. }
  139. /*
  140. * Worker for allocating per cpu stat for tgs. This is scheduled on the
  141. * system_wq once there are some groups on the alloc_list waiting for
  142. * allocation.
  143. */
  144. static void tg_stats_alloc_fn(struct work_struct *work)
  145. {
  146. static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
  147. struct delayed_work *dwork = to_delayed_work(work);
  148. bool empty = false;
  149. alloc_stats:
  150. if (!stats_cpu) {
  151. stats_cpu = alloc_percpu(struct tg_stats_cpu);
  152. if (!stats_cpu) {
  153. /* allocation failed, try again after some time */
  154. schedule_delayed_work(dwork, msecs_to_jiffies(10));
  155. return;
  156. }
  157. }
  158. spin_lock_irq(&tg_stats_alloc_lock);
  159. if (!list_empty(&tg_stats_alloc_list)) {
  160. struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
  161. struct throtl_grp,
  162. stats_alloc_node);
  163. swap(tg->stats_cpu, stats_cpu);
  164. list_del_init(&tg->stats_alloc_node);
  165. }
  166. empty = list_empty(&tg_stats_alloc_list);
  167. spin_unlock_irq(&tg_stats_alloc_lock);
  168. if (!empty)
  169. goto alloc_stats;
  170. }
  171. static void throtl_pd_init(struct blkcg_gq *blkg)
  172. {
  173. struct throtl_grp *tg = blkg_to_tg(blkg);
  174. unsigned long flags;
  175. RB_CLEAR_NODE(&tg->rb_node);
  176. bio_list_init(&tg->bio_lists[0]);
  177. bio_list_init(&tg->bio_lists[1]);
  178. tg->limits_changed = false;
  179. tg->bps[READ] = -1;
  180. tg->bps[WRITE] = -1;
  181. tg->iops[READ] = -1;
  182. tg->iops[WRITE] = -1;
  183. /*
  184. * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
  185. * but percpu allocator can't be called from IO path. Queue tg on
  186. * tg_stats_alloc_list and allocate from work item.
  187. */
  188. spin_lock_irqsave(&tg_stats_alloc_lock, flags);
  189. list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
  190. schedule_delayed_work(&tg_stats_alloc_work, 0);
  191. spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
  192. }
  193. static void throtl_pd_exit(struct blkcg_gq *blkg)
  194. {
  195. struct throtl_grp *tg = blkg_to_tg(blkg);
  196. unsigned long flags;
  197. spin_lock_irqsave(&tg_stats_alloc_lock, flags);
  198. list_del_init(&tg->stats_alloc_node);
  199. spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
  200. free_percpu(tg->stats_cpu);
  201. }
  202. static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
  203. {
  204. struct throtl_grp *tg = blkg_to_tg(blkg);
  205. int cpu;
  206. if (tg->stats_cpu == NULL)
  207. return;
  208. for_each_possible_cpu(cpu) {
  209. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  210. blkg_rwstat_reset(&sc->service_bytes);
  211. blkg_rwstat_reset(&sc->serviced);
  212. }
  213. }
  214. static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
  215. struct blkcg *blkcg)
  216. {
  217. /*
  218. * This is the common case when there are no blkcgs. Avoid lookup
  219. * in this case
  220. */
  221. if (blkcg == &blkcg_root)
  222. return td_root_tg(td);
  223. return blkg_to_tg(blkg_lookup(blkcg, td->queue));
  224. }
  225. static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
  226. struct blkcg *blkcg)
  227. {
  228. struct request_queue *q = td->queue;
  229. struct throtl_grp *tg = NULL;
  230. /*
  231. * This is the common case when there are no blkcgs. Avoid lookup
  232. * in this case
  233. */
  234. if (blkcg == &blkcg_root) {
  235. tg = td_root_tg(td);
  236. } else {
  237. struct blkcg_gq *blkg;
  238. blkg = blkg_lookup_create(blkcg, q);
  239. /* if %NULL and @q is alive, fall back to root_tg */
  240. if (!IS_ERR(blkg))
  241. tg = blkg_to_tg(blkg);
  242. else if (!blk_queue_dying(q))
  243. tg = td_root_tg(td);
  244. }
  245. return tg;
  246. }
  247. static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
  248. {
  249. /* Service tree is empty */
  250. if (!root->count)
  251. return NULL;
  252. if (!root->left)
  253. root->left = rb_first(&root->rb);
  254. if (root->left)
  255. return rb_entry_tg(root->left);