blk-cgroup.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. enum blkio_policy_id {
  17. BLKIO_POLICY_PROP = 0, /* Proportional Bandwidth division */
  18. };
  19. #if defined(CONFIG_BLK_CGROUP) || defined(CONFIG_BLK_CGROUP_MODULE)
  20. #ifndef CONFIG_BLK_CGROUP
  21. /* When blk-cgroup is a module, its subsys_id isn't a compile-time constant */
  22. extern struct cgroup_subsys blkio_subsys;
  23. #define blkio_subsys_id blkio_subsys.subsys_id
  24. #endif
  25. enum stat_type {
  26. /* Total time spent (in ns) between request dispatch to the driver and
  27. * request completion for IOs doen by this cgroup. This may not be
  28. * accurate when NCQ is turned on. */
  29. BLKIO_STAT_SERVICE_TIME = 0,
  30. /* Total bytes transferred */
  31. BLKIO_STAT_SERVICE_BYTES,
  32. /* Total IOs serviced, post merge */
  33. BLKIO_STAT_SERVICED,
  34. /* Total time spent waiting in scheduler queue in ns */
  35. BLKIO_STAT_WAIT_TIME,
  36. /* Number of IOs merged */
  37. BLKIO_STAT_MERGED,
  38. /* Number of IOs queued up */
  39. BLKIO_STAT_QUEUED,
  40. /* All the single valued stats go below this */
  41. BLKIO_STAT_TIME,
  42. BLKIO_STAT_SECTORS,
  43. #ifdef CONFIG_DEBUG_BLK_CGROUP
  44. BLKIO_STAT_AVG_QUEUE_SIZE,
  45. BLKIO_STAT_IDLE_TIME,
  46. BLKIO_STAT_EMPTY_TIME,
  47. BLKIO_STAT_GROUP_WAIT_TIME,
  48. BLKIO_STAT_DEQUEUE
  49. #endif
  50. };
  51. enum stat_sub_type {
  52. BLKIO_STAT_READ = 0,
  53. BLKIO_STAT_WRITE,
  54. BLKIO_STAT_SYNC,
  55. BLKIO_STAT_ASYNC,
  56. BLKIO_STAT_TOTAL
  57. };
  58. /* blkg state flags */
  59. enum blkg_state_flags {
  60. BLKG_waiting = 0,
  61. BLKG_idling,
  62. BLKG_empty,
  63. };
  64. /* cgroup files owned by proportional weight policy */
  65. enum blkcg_file_name_prop {
  66. BLKIO_PROP_weight = 1,
  67. BLKIO_PROP_weight_device,
  68. BLKIO_PROP_io_service_bytes,
  69. BLKIO_PROP_io_serviced,
  70. BLKIO_PROP_time,
  71. BLKIO_PROP_sectors,
  72. BLKIO_PROP_io_service_time,
  73. BLKIO_PROP_io_wait_time,
  74. BLKIO_PROP_io_merged,
  75. BLKIO_PROP_io_queued,
  76. BLKIO_PROP_avg_queue_size,
  77. BLKIO_PROP_group_wait_time,
  78. BLKIO_PROP_idle_time,
  79. BLKIO_PROP_empty_time,
  80. BLKIO_PROP_dequeue,
  81. };
  82. struct blkio_cgroup {
  83. struct cgroup_subsys_state css;
  84. unsigned int weight;
  85. spinlock_t lock;
  86. struct hlist_head blkg_list;
  87. struct list_head policy_list; /* list of blkio_policy_node */
  88. };
  89. struct blkio_group_stats {
  90. /* total disk time and nr sectors dispatched by this group */
  91. uint64_t time;
  92. uint64_t sectors;
  93. uint64_t stat_arr[BLKIO_STAT_QUEUED + 1][BLKIO_STAT_TOTAL];
  94. #ifdef CONFIG_DEBUG_BLK_CGROUP
  95. /* Sum of number of IOs queued across all samples */
  96. uint64_t avg_queue_size_sum;
  97. /* Count of samples taken for average */
  98. uint64_t avg_queue_size_samples;
  99. /* How many times this group has been removed from service tree */
  100. unsigned long dequeue;
  101. /* Total time spent waiting for it to be assigned a timeslice. */
  102. uint64_t group_wait_time;
  103. uint64_t start_group_wait_time;
  104. /* Time spent idling for this blkio_group */
  105. uint64_t idle_time;
  106. uint64_t start_idle_time;
  107. /*
  108. * Total time when we have requests queued and do not contain the
  109. * current active queue.
  110. */
  111. uint64_t empty_time;
  112. uint64_t start_empty_time;
  113. uint16_t flags;
  114. #endif
  115. };
  116. struct blkio_group {
  117. /* An rcu protected unique identifier for the group */
  118. void *key;
  119. struct hlist_node blkcg_node;
  120. unsigned short blkcg_id;
  121. /* Store cgroup path */
  122. char path[128];
  123. /* The device MKDEV(major, minor), this group has been created for */
  124. dev_t dev;
  125. /* policy which owns this blk group */
  126. enum blkio_policy_id plid;
  127. /* Need to serialize the stats in the case of reset/update */
  128. spinlock_t stats_lock;
  129. struct blkio_group_stats stats;
  130. };
  131. struct blkio_policy_node {
  132. struct list_head node;
  133. dev_t dev;
  134. unsigned int weight;
  135. /* This node belongs to max bw policy or porportional weight policy */
  136. enum blkio_policy_id plid;
  137. /* cgroup file to which this rule belongs to */
  138. int fileid;
  139. };
  140. extern unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
  141. dev_t dev);
  142. typedef void (blkio_unlink_group_fn) (void *key, struct blkio_group *blkg);
  143. typedef void (blkio_update_group_weight_fn) (struct blkio_group *blkg,
  144. unsigned int weight);
  145. struct blkio_policy_ops {
  146. blkio_unlink_group_fn *blkio_unlink_group_fn;
  147. blkio_update_group_weight_fn *blkio_update_group_weight_fn;
  148. };
  149. struct blkio_policy_type {
  150. struct list_head list;
  151. struct blkio_policy_ops ops;
  152. enum blkio_policy_id plid;
  153. };
  154. /* Blkio controller policy registration */
  155. extern void blkio_policy_register(struct blkio_policy_type *);
  156. extern void blkio_policy_unregister(struct blkio_policy_type *);
  157. static inline char *blkg_path(struct blkio_group *blkg)
  158. {
  159. return blkg->path;
  160. }
  161. #else
  162. struct blkio_group {
  163. };
  164. struct blkio_policy_type {
  165. };
  166. static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
  167. static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
  168. static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
  169. #endif
  170. #define BLKIO_WEIGHT_MIN 100
  171. #define BLKIO_WEIGHT_MAX 1000
  172. #define BLKIO_WEIGHT_DEFAULT 500
  173. #ifdef CONFIG_DEBUG_BLK_CGROUP
  174. void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg);
  175. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  176. unsigned long dequeue);
  177. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg);
  178. void blkiocg_update_idle_time_stats(struct blkio_group *blkg);
  179. void blkiocg_set_start_empty_time(struct blkio_group *blkg);
  180. #define BLKG_FLAG_FNS(name) \
  181. static inline void blkio_mark_blkg_##name( \
  182. struct blkio_group_stats *stats) \
  183. { \
  184. stats->flags |= (1 << BLKG_##name); \
  185. } \
  186. static inline void blkio_clear_blkg_##name( \
  187. struct blkio_group_stats *stats) \
  188. { \
  189. stats->flags &= ~(1 << BLKG_##name); \
  190. } \
  191. static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
  192. { \
  193. return (stats->flags & (1 << BLKG_##name)) != 0; \
  194. } \
  195. BLKG_FLAG_FNS(waiting)
  196. BLKG_FLAG_FNS(idling)
  197. BLKG_FLAG_FNS(empty)
  198. #undef BLKG_FLAG_FNS
  199. #else
  200. static inline void blkiocg_update_avg_queue_size_stats(
  201. struct blkio_group *blkg) {}
  202. static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  203. unsigned long dequeue) {}
  204. static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
  205. {}
  206. static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg) {}
  207. static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg) {}
  208. #endif
  209. #if defined(CONFIG_BLK_CGROUP) || defined(CONFIG_BLK_CGROUP_MODULE)
  210. extern struct blkio_cgroup blkio_root_cgroup;
  211. extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
  212. extern void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
  213. struct blkio_group *blkg, void *key, dev_t dev,
  214. enum blkio_policy_id plid);
  215. extern int blkiocg_del_blkio_group(struct blkio_group *blkg);
  216. extern struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg,
  217. void *key);
  218. void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  219. unsigned long time);
  220. void blkiocg_update_dispatch_stats(struct blkio_group *blkg, uint64_t bytes,
  221. bool direction, bool sync);
  222. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  223. uint64_t start_time, uint64_t io_start_time, bool direction, bool sync);
  224. void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
  225. bool sync);
  226. void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  227. struct blkio_group *curr_blkg, bool direction, bool sync);
  228. void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  229. bool direction, bool sync);
  230. #else
  231. struct cgroup;
  232. static inline struct blkio_cgroup *
  233. cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
  234. static inline void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
  235. struct blkio_group *blkg, void *key, dev_t dev,
  236. enum blkio_policy_id plid) {}
  237. static inline int
  238. blkiocg_del_blkio_group(struct blkio_group *blkg) { return 0; }
  239. static inline struct blkio_group *
  240. blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key) { return NULL; }
  241. static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  242. unsigned long time) {}
  243. static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  244. uint64_t bytes, bool direction, bool sync) {}
  245. static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
  246. uint64_t start_time, uint64_t io_start_time, bool direction,
  247. bool sync) {}
  248. static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  249. bool direction, bool sync) {}
  250. static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  251. struct blkio_group *curr_blkg, bool direction, bool sync) {}
  252. static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  253. bool direction, bool sync) {}
  254. #endif
  255. #endif /* _BLK_CGROUP_H */