blk-cgroup.h 11 KB

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