blk-cgroup.h 11 KB

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