blk-cgroup.h 11 KB

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