blk-cgroup.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #ifndef _BLK_CGROUP_H
  2. #define _BLK_CGROUP_H
  3. /*
  4. * Common Block IO controller cgroup interface
  5. *
  6. * Based on ideas and code from CFQ, CFS and BFQ:
  7. * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
  8. *
  9. * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
  10. * Paolo Valente <paolo.valente@unimore.it>
  11. *
  12. * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
  13. * Nauman Rafique <nauman@google.com>
  14. */
  15. #include <linux/cgroup.h>
  16. #if defined(CONFIG_BLK_CGROUP) || defined(CONFIG_BLK_CGROUP_MODULE)
  17. #ifndef CONFIG_BLK_CGROUP
  18. /* When blk-cgroup is a module, its subsys_id isn't a compile-time constant */
  19. extern struct cgroup_subsys blkio_subsys;
  20. #define blkio_subsys_id blkio_subsys.subsys_id
  21. #endif
  22. enum stat_type {
  23. /* Total time spent (in ns) between request dispatch to the driver and
  24. * request completion for IOs doen by this cgroup. This may not be
  25. * accurate when NCQ is turned on. */
  26. BLKIO_STAT_SERVICE_TIME = 0,
  27. /* Total bytes transferred */
  28. BLKIO_STAT_SERVICE_BYTES,
  29. /* Total IOs serviced, post merge */
  30. BLKIO_STAT_SERVICED,
  31. /* Total time spent waiting in scheduler queue in ns */
  32. BLKIO_STAT_WAIT_TIME,
  33. /* Number of IOs merged */
  34. BLKIO_STAT_MERGED,
  35. /* Number of IOs queued up */
  36. BLKIO_STAT_QUEUED,
  37. /* All the single valued stats go below this */
  38. BLKIO_STAT_TIME,
  39. BLKIO_STAT_SECTORS,
  40. #ifdef CONFIG_DEBUG_BLK_CGROUP
  41. BLKIO_STAT_AVG_QUEUE_SIZE,
  42. BLKIO_STAT_IDLE_TIME,
  43. BLKIO_STAT_EMPTY_TIME,
  44. BLKIO_STAT_GROUP_WAIT_TIME,
  45. BLKIO_STAT_DEQUEUE
  46. #endif
  47. };
  48. enum stat_sub_type {
  49. BLKIO_STAT_READ = 0,
  50. BLKIO_STAT_WRITE,
  51. BLKIO_STAT_SYNC,
  52. BLKIO_STAT_ASYNC,
  53. BLKIO_STAT_TOTAL
  54. };
  55. /* blkg state flags */
  56. enum blkg_state_flags {
  57. BLKG_waiting = 0,
  58. BLKG_idling,
  59. BLKG_empty,
  60. };
  61. struct blkio_cgroup {
  62. struct cgroup_subsys_state css;
  63. unsigned int weight;
  64. spinlock_t lock;
  65. struct hlist_head blkg_list;
  66. };
  67. struct blkio_group_stats {
  68. /* total disk time and nr sectors dispatched by this group */
  69. uint64_t time;
  70. uint64_t sectors;
  71. uint64_t stat_arr[BLKIO_STAT_QUEUED + 1][BLKIO_STAT_TOTAL];
  72. #ifdef CONFIG_DEBUG_BLK_CGROUP
  73. /* Sum of number of IOs queued across all samples */
  74. uint64_t avg_queue_size_sum;
  75. /* Count of samples taken for average */
  76. uint64_t avg_queue_size_samples;
  77. /* How many times this group has been removed from service tree */
  78. unsigned long dequeue;
  79. /* Total time spent waiting for it to be assigned a timeslice. */
  80. uint64_t group_wait_time;
  81. uint64_t start_group_wait_time;
  82. /* Time spent idling for this blkio_group */
  83. uint64_t idle_time;
  84. uint64_t start_idle_time;
  85. /*
  86. * Total time when we have requests queued and do not contain the
  87. * current active queue.
  88. */
  89. uint64_t empty_time;
  90. uint64_t start_empty_time;
  91. uint16_t flags;
  92. #endif
  93. };
  94. struct blkio_group {
  95. /* An rcu protected unique identifier for the group */
  96. void *key;
  97. struct hlist_node blkcg_node;
  98. unsigned short blkcg_id;
  99. #ifdef CONFIG_DEBUG_BLK_CGROUP
  100. /* Store cgroup path */
  101. char path[128];
  102. #endif
  103. /* The device MKDEV(major, minor), this group has been created for */
  104. dev_t dev;
  105. /* Need to serialize the stats in the case of reset/update */
  106. spinlock_t stats_lock;
  107. struct blkio_group_stats stats;
  108. };
  109. typedef void (blkio_unlink_group_fn) (void *key, struct blkio_group *blkg);
  110. typedef void (blkio_update_group_weight_fn) (struct blkio_group *blkg,
  111. unsigned int weight);
  112. struct blkio_policy_ops {
  113. blkio_unlink_group_fn *blkio_unlink_group_fn;
  114. blkio_update_group_weight_fn *blkio_update_group_weight_fn;
  115. };
  116. struct blkio_policy_type {
  117. struct list_head list;
  118. struct blkio_policy_ops ops;
  119. };
  120. /* Blkio controller policy registration */
  121. extern void blkio_policy_register(struct blkio_policy_type *);
  122. extern void blkio_policy_unregister(struct blkio_policy_type *);
  123. #else
  124. struct blkio_group {
  125. };
  126. struct blkio_policy_type {
  127. };
  128. static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
  129. static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
  130. #endif
  131. #define BLKIO_WEIGHT_MIN 100
  132. #define BLKIO_WEIGHT_MAX 1000
  133. #define BLKIO_WEIGHT_DEFAULT 500
  134. #ifdef CONFIG_DEBUG_BLK_CGROUP
  135. static inline char *blkg_path(struct blkio_group *blkg)
  136. {
  137. return blkg->path;
  138. }
  139. void blkiocg_update_set_active_queue_stats(struct blkio_group *blkg);
  140. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  141. unsigned long dequeue);
  142. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg);
  143. void blkiocg_update_idle_time_stats(struct blkio_group *blkg);
  144. void blkiocg_set_start_empty_time(struct blkio_group *blkg, bool ignore);
  145. #define BLKG_FLAG_FNS(name) \
  146. static inline void blkio_mark_blkg_##name( \
  147. struct blkio_group_stats *stats) \
  148. { \
  149. stats->flags |= (1 << BLKG_##name); \
  150. } \
  151. static inline void blkio_clear_blkg_##name( \
  152. struct blkio_group_stats *stats) \
  153. { \
  154. stats->flags &= ~(1 << BLKG_##name); \
  155. } \
  156. static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
  157. { \
  158. return (stats->flags & (1 << BLKG_##name)) != 0; \
  159. } \
  160. BLKG_FLAG_FNS(waiting)
  161. BLKG_FLAG_FNS(idling)
  162. BLKG_FLAG_FNS(empty)
  163. #undef BLKG_FLAG_FNS
  164. #else
  165. static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
  166. static inline void blkiocg_update_set_active_queue_stats(
  167. struct blkio_group *blkg) {}
  168. static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  169. unsigned long dequeue) {}
  170. static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
  171. {}
  172. static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg) {}
  173. static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  174. bool ignore) {}
  175. #endif
  176. #if defined(CONFIG_BLK_CGROUP) || defined(CONFIG_BLK_CGROUP_MODULE)
  177. extern struct blkio_cgroup blkio_root_cgroup;
  178. extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
  179. extern void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
  180. struct blkio_group *blkg, void *key, dev_t dev);
  181. extern int blkiocg_del_blkio_group(struct blkio_group *blkg);
  182. extern struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg,
  183. void *key);
  184. void blkio_group_init(struct blkio_group *blkg);
  185. void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  186. unsigned long time);
  187. void blkiocg_update_dispatch_stats(struct blkio_group *blkg, uint64_t bytes,
  188. bool direction, bool sync);
  189. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  190. uint64_t start_time, uint64_t io_start_time, bool direction, bool sync);
  191. void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
  192. bool sync);
  193. void blkiocg_update_request_add_stats(struct blkio_group *blkg,
  194. struct blkio_group *curr_blkg, bool direction, bool sync);
  195. void blkiocg_update_request_remove_stats(struct blkio_group *blkg,
  196. bool direction, bool sync);
  197. #else
  198. struct cgroup;
  199. static inline struct blkio_cgroup *
  200. cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
  201. static inline void blkio_group_init(struct blkio_group *blkg) {}
  202. static inline void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
  203. struct blkio_group *blkg, void *key, dev_t dev) {}
  204. static inline int
  205. blkiocg_del_blkio_group(struct blkio_group *blkg) { return 0; }
  206. static inline struct blkio_group *
  207. blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key) { return NULL; }
  208. static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  209. unsigned long time) {}
  210. static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  211. uint64_t bytes, bool direction, bool sync) {}
  212. static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
  213. uint64_t start_time, uint64_t io_start_time, bool direction,
  214. bool sync) {}
  215. static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  216. bool direction, bool sync) {}
  217. static inline void blkiocg_update_request_add_stats(struct blkio_group *blkg,
  218. struct blkio_group *curr_blkg, bool direction, bool sync) {}
  219. static inline void blkiocg_update_request_remove_stats(struct blkio_group *blkg,
  220. bool direction, bool sync) {}
  221. #endif
  222. #endif /* _BLK_CGROUP_H */