blk-cgroup.h 10 KB

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