blk-cgroup.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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_conf {
  140. unsigned int weight;
  141. unsigned int iops[2];
  142. u64 bps[2];
  143. };
  144. struct blkio_group {
  145. /* Pointer to the associated request_queue, RCU protected */
  146. struct request_queue __rcu *q;
  147. struct hlist_node blkcg_node;
  148. unsigned short blkcg_id;
  149. /* Store cgroup path */
  150. char path[128];
  151. /* The device MKDEV(major, minor), this group has been created for */
  152. dev_t dev;
  153. /* policy which owns this blk group */
  154. enum blkio_policy_id plid;
  155. /* Configuration */
  156. struct blkio_group_conf conf;
  157. /* Need to serialize the stats in the case of reset/update */
  158. spinlock_t stats_lock;
  159. struct blkio_group_stats stats;
  160. /* Per cpu stats pointer */
  161. struct blkio_group_stats_cpu __percpu *stats_cpu;
  162. };
  163. struct blkio_policy_node {
  164. struct list_head node;
  165. dev_t dev;
  166. /* This node belongs to max bw policy or porportional weight policy */
  167. enum blkio_policy_id plid;
  168. /* cgroup file to which this rule belongs to */
  169. int fileid;
  170. union {
  171. unsigned int weight;
  172. /*
  173. * Rate read/write in terms of bytes per second
  174. * Whether this rate represents read or write is determined
  175. * by file type "fileid".
  176. */
  177. u64 bps;
  178. unsigned int iops;
  179. } val;
  180. };
  181. extern unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
  182. dev_t dev);
  183. extern uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg,
  184. dev_t dev);
  185. extern uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg,
  186. dev_t dev);
  187. extern unsigned int blkcg_get_read_iops(struct blkio_cgroup *blkcg,
  188. dev_t dev);
  189. extern unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg,
  190. dev_t dev);
  191. typedef struct blkio_group *(blkio_alloc_group_fn)(struct request_queue *q,
  192. struct blkio_cgroup *blkcg);
  193. typedef void (blkio_link_group_fn)(struct request_queue *q,
  194. struct blkio_group *blkg);
  195. typedef void (blkio_unlink_group_fn)(struct request_queue *q,
  196. struct blkio_group *blkg);
  197. typedef bool (blkio_clear_queue_fn)(struct request_queue *q);
  198. typedef void (blkio_update_group_weight_fn)(struct request_queue *q,
  199. struct blkio_group *blkg, unsigned int weight);
  200. typedef void (blkio_update_group_read_bps_fn)(struct request_queue *q,
  201. struct blkio_group *blkg, u64 read_bps);
  202. typedef void (blkio_update_group_write_bps_fn)(struct request_queue *q,
  203. struct blkio_group *blkg, u64 write_bps);
  204. typedef void (blkio_update_group_read_iops_fn)(struct request_queue *q,
  205. struct blkio_group *blkg, unsigned int read_iops);
  206. typedef void (blkio_update_group_write_iops_fn)(struct request_queue *q,
  207. struct blkio_group *blkg, unsigned int write_iops);
  208. struct blkio_policy_ops {
  209. blkio_alloc_group_fn *blkio_alloc_group_fn;
  210. blkio_link_group_fn *blkio_link_group_fn;
  211. blkio_unlink_group_fn *blkio_unlink_group_fn;
  212. blkio_clear_queue_fn *blkio_clear_queue_fn;
  213. blkio_update_group_weight_fn *blkio_update_group_weight_fn;
  214. blkio_update_group_read_bps_fn *blkio_update_group_read_bps_fn;
  215. blkio_update_group_write_bps_fn *blkio_update_group_write_bps_fn;
  216. blkio_update_group_read_iops_fn *blkio_update_group_read_iops_fn;
  217. blkio_update_group_write_iops_fn *blkio_update_group_write_iops_fn;
  218. };
  219. struct blkio_policy_type {
  220. struct list_head list;
  221. struct blkio_policy_ops ops;
  222. enum blkio_policy_id plid;
  223. };
  224. /* Blkio controller policy registration */
  225. extern void blkio_policy_register(struct blkio_policy_type *);
  226. extern void blkio_policy_unregister(struct blkio_policy_type *);
  227. extern void blkg_destroy_all(struct request_queue *q);
  228. static inline char *blkg_path(struct blkio_group *blkg)
  229. {
  230. return blkg->path;
  231. }
  232. #else
  233. struct blkio_group {
  234. };
  235. struct blkio_policy_type {
  236. };
  237. static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
  238. static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
  239. static inline void blkg_destroy_all(struct request_queue *q) { }
  240. static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
  241. #endif
  242. #define BLKIO_WEIGHT_MIN 10
  243. #define BLKIO_WEIGHT_MAX 1000
  244. #define BLKIO_WEIGHT_DEFAULT 500
  245. #ifdef CONFIG_DEBUG_BLK_CGROUP
  246. void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg);
  247. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  248. unsigned long dequeue);
  249. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg);
  250. void blkiocg_update_idle_time_stats(struct blkio_group *blkg);
  251. void blkiocg_set_start_empty_time(struct blkio_group *blkg);
  252. #define BLKG_FLAG_FNS(name) \
  253. static inline void blkio_mark_blkg_##name( \
  254. struct blkio_group_stats *stats) \
  255. { \
  256. stats->flags |= (1 << BLKG_##name); \
  257. } \
  258. static inline void blkio_clear_blkg_##name( \
  259. struct blkio_group_stats *stats) \
  260. { \
  261. stats->flags &= ~(1 << BLKG_##name); \
  262. } \
  263. static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
  264. { \
  265. return (stats->flags & (1 << BLKG_##name)) != 0; \
  266. } \
  267. BLKG_FLAG_FNS(waiting)
  268. BLKG_FLAG_FNS(idling)
  269. BLKG_FLAG_FNS(empty)
  270. #undef BLKG_FLAG_FNS
  271. #else
  272. static inline void blkiocg_update_avg_queue_size_stats(
  273. struct blkio_group *blkg) {}
  274. static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  275. unsigned long dequeue) {}
  276. static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
  277. {}
  278. static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg) {}
  279. static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg) {}
  280. #endif
  281. #ifdef CONFIG_BLK_CGROUP
  282. extern struct blkio_cgroup blkio_root_cgroup;
  283. extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
  284. extern struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk);
  285. extern int blkiocg_del_blkio_group(struct blkio_group *blkg);
  286. extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  287. struct request_queue *q,
  288. enum blkio_policy_id plid);
  289. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  290. struct request_queue *q,
  291. enum blkio_policy_id plid,
  292. bool for_root);
  293. void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  294. unsigned long time,
  295. unsigned long unaccounted_time);
  296. void blkiocg_update_dispatch_stats(struct blkio_group *blkg, uint64_t bytes,
  297. bool direction, bool sync);
  298. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  299. uint64_t start_time, uint64_t io_start_time, bool direction, bool sync);
  300. void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
  301. bool sync);
  302. void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  303. struct blkio_group *curr_blkg, bool direction, bool sync);
  304. void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  305. bool direction, bool sync);
  306. #else
  307. struct cgroup;
  308. static inline struct blkio_cgroup *
  309. cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
  310. static inline struct blkio_cgroup *
  311. task_blkio_cgroup(struct task_struct *tsk) { return NULL; }
  312. static inline int
  313. blkiocg_del_blkio_group(struct blkio_group *blkg) { return 0; }
  314. static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  315. void *key) { return NULL; }
  316. static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  317. unsigned long time,
  318. unsigned long unaccounted_time)
  319. {}
  320. static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  321. uint64_t bytes, bool direction, bool sync) {}
  322. static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
  323. uint64_t start_time, uint64_t io_start_time, bool direction,
  324. bool sync) {}
  325. static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  326. bool direction, bool sync) {}
  327. static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  328. struct blkio_group *curr_blkg, bool direction, bool sync) {}
  329. static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  330. bool direction, bool sync) {}
  331. #endif
  332. #endif /* _BLK_CGROUP_H */