Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 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 | |
| 16 | #include <linux/cgroup.h> |
| 17 | |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 18 | #if defined(CONFIG_BLK_CGROUP) || defined(CONFIG_BLK_CGROUP_MODULE) |
| 19 | |
| 20 | #ifndef CONFIG_BLK_CGROUP |
| 21 | /* When blk-cgroup is a module, its subsys_id isn't a compile-time constant */ |
| 22 | extern struct cgroup_subsys blkio_subsys; |
| 23 | #define blkio_subsys_id blkio_subsys.subsys_id |
| 24 | #endif |
Jens Axboe | 2f5ea47 | 2009-12-03 21:06:43 +0100 | [diff] [blame] | 25 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 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, |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 37 | /* Number of IOs merged */ |
| 38 | BLKIO_STAT_MERGED, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 39 | /* Number of IOs queued up */ |
| 40 | BLKIO_STAT_QUEUED, |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 41 | /* All the single valued stats go below this */ |
| 42 | BLKIO_STAT_TIME, |
| 43 | BLKIO_STAT_SECTORS, |
| 44 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 45 | BLKIO_STAT_AVG_QUEUE_SIZE, |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 46 | BLKIO_STAT_IDLE_TIME, |
| 47 | BLKIO_STAT_EMPTY_TIME, |
| 48 | BLKIO_STAT_GROUP_WAIT_TIME, |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 49 | BLKIO_STAT_DEQUEUE |
| 50 | #endif |
| 51 | }; |
| 52 | |
| 53 | enum stat_sub_type { |
| 54 | BLKIO_STAT_READ = 0, |
| 55 | BLKIO_STAT_WRITE, |
| 56 | BLKIO_STAT_SYNC, |
| 57 | BLKIO_STAT_ASYNC, |
| 58 | BLKIO_STAT_TOTAL |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 61 | /* blkg state flags */ |
| 62 | enum blkg_state_flags { |
| 63 | BLKG_waiting = 0, |
| 64 | BLKG_idling, |
| 65 | BLKG_empty, |
| 66 | }; |
| 67 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 68 | struct blkio_cgroup { |
| 69 | struct cgroup_subsys_state css; |
| 70 | unsigned int weight; |
| 71 | spinlock_t lock; |
| 72 | struct hlist_head blkg_list; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame^] | 73 | struct list_head policy_list; /* list of blkio_policy_node */ |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 74 | }; |
| 75 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 76 | struct blkio_group_stats { |
| 77 | /* total disk time and nr sectors dispatched by this group */ |
| 78 | uint64_t time; |
| 79 | uint64_t sectors; |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 80 | uint64_t stat_arr[BLKIO_STAT_QUEUED + 1][BLKIO_STAT_TOTAL]; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 81 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 82 | /* Sum of number of IOs queued across all samples */ |
| 83 | uint64_t avg_queue_size_sum; |
| 84 | /* Count of samples taken for average */ |
| 85 | uint64_t avg_queue_size_samples; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 86 | /* How many times this group has been removed from service tree */ |
| 87 | unsigned long dequeue; |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 88 | |
| 89 | /* Total time spent waiting for it to be assigned a timeslice. */ |
| 90 | uint64_t group_wait_time; |
| 91 | uint64_t start_group_wait_time; |
| 92 | |
| 93 | /* Time spent idling for this blkio_group */ |
| 94 | uint64_t idle_time; |
| 95 | uint64_t start_idle_time; |
| 96 | /* |
| 97 | * Total time when we have requests queued and do not contain the |
| 98 | * current active queue. |
| 99 | */ |
| 100 | uint64_t empty_time; |
| 101 | uint64_t start_empty_time; |
| 102 | uint16_t flags; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 103 | #endif |
| 104 | }; |
| 105 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 106 | struct blkio_group { |
| 107 | /* An rcu protected unique identifier for the group */ |
| 108 | void *key; |
| 109 | struct hlist_node blkcg_node; |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 110 | unsigned short blkcg_id; |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 111 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 112 | /* Store cgroup path */ |
| 113 | char path[128]; |
| 114 | #endif |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 115 | /* The device MKDEV(major, minor), this group has been created for */ |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 116 | dev_t dev; |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 117 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 118 | /* Need to serialize the stats in the case of reset/update */ |
| 119 | spinlock_t stats_lock; |
| 120 | struct blkio_group_stats stats; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 121 | }; |
| 122 | |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame^] | 123 | struct blkio_policy_node { |
| 124 | struct list_head node; |
| 125 | dev_t dev; |
| 126 | unsigned int weight; |
| 127 | }; |
| 128 | |
| 129 | extern unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg, |
| 130 | dev_t dev); |
| 131 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 132 | typedef void (blkio_unlink_group_fn) (void *key, struct blkio_group *blkg); |
| 133 | typedef void (blkio_update_group_weight_fn) (struct blkio_group *blkg, |
| 134 | unsigned int weight); |
| 135 | |
| 136 | struct blkio_policy_ops { |
| 137 | blkio_unlink_group_fn *blkio_unlink_group_fn; |
| 138 | blkio_update_group_weight_fn *blkio_update_group_weight_fn; |
| 139 | }; |
| 140 | |
| 141 | struct blkio_policy_type { |
| 142 | struct list_head list; |
| 143 | struct blkio_policy_ops ops; |
| 144 | }; |
| 145 | |
| 146 | /* Blkio controller policy registration */ |
| 147 | extern void blkio_policy_register(struct blkio_policy_type *); |
| 148 | extern void blkio_policy_unregister(struct blkio_policy_type *); |
| 149 | |
Jens Axboe | 2f5ea47 | 2009-12-03 21:06:43 +0100 | [diff] [blame] | 150 | #else |
| 151 | |
| 152 | struct blkio_group { |
| 153 | }; |
| 154 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 155 | struct blkio_policy_type { |
| 156 | }; |
| 157 | |
| 158 | static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { } |
| 159 | static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { } |
| 160 | |
Jens Axboe | 2f5ea47 | 2009-12-03 21:06:43 +0100 | [diff] [blame] | 161 | #endif |
| 162 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 163 | #define BLKIO_WEIGHT_MIN 100 |
| 164 | #define BLKIO_WEIGHT_MAX 1000 |
| 165 | #define BLKIO_WEIGHT_DEFAULT 500 |
| 166 | |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 167 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 168 | static inline char *blkg_path(struct blkio_group *blkg) |
| 169 | { |
| 170 | return blkg->path; |
| 171 | } |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 172 | void blkiocg_update_set_active_queue_stats(struct blkio_group *blkg); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 173 | void blkiocg_update_dequeue_stats(struct blkio_group *blkg, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 174 | unsigned long dequeue); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 175 | void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg); |
| 176 | void blkiocg_update_idle_time_stats(struct blkio_group *blkg); |
| 177 | void blkiocg_set_start_empty_time(struct blkio_group *blkg, bool ignore); |
| 178 | |
| 179 | #define BLKG_FLAG_FNS(name) \ |
| 180 | static inline void blkio_mark_blkg_##name( \ |
| 181 | struct blkio_group_stats *stats) \ |
| 182 | { \ |
| 183 | stats->flags |= (1 << BLKG_##name); \ |
| 184 | } \ |
| 185 | static inline void blkio_clear_blkg_##name( \ |
| 186 | struct blkio_group_stats *stats) \ |
| 187 | { \ |
| 188 | stats->flags &= ~(1 << BLKG_##name); \ |
| 189 | } \ |
| 190 | static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \ |
| 191 | { \ |
| 192 | return (stats->flags & (1 << BLKG_##name)) != 0; \ |
| 193 | } \ |
| 194 | |
| 195 | BLKG_FLAG_FNS(waiting) |
| 196 | BLKG_FLAG_FNS(idling) |
| 197 | BLKG_FLAG_FNS(empty) |
| 198 | #undef BLKG_FLAG_FNS |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 199 | #else |
| 200 | static inline char *blkg_path(struct blkio_group *blkg) { return NULL; } |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 201 | static inline void blkiocg_update_set_active_queue_stats( |
| 202 | struct blkio_group *blkg) {} |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 203 | static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg, |
| 204 | unsigned long dequeue) {} |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 205 | static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg) |
| 206 | {} |
| 207 | static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg) {} |
| 208 | static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg, |
| 209 | bool ignore) {} |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 210 | #endif |
| 211 | |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 212 | #if defined(CONFIG_BLK_CGROUP) || defined(CONFIG_BLK_CGROUP_MODULE) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 213 | extern struct blkio_cgroup blkio_root_cgroup; |
| 214 | extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup); |
| 215 | extern void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 216 | struct blkio_group *blkg, void *key, dev_t dev); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 217 | extern int blkiocg_del_blkio_group(struct blkio_group *blkg); |
| 218 | extern struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, |
| 219 | void *key); |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 220 | void blkio_group_init(struct blkio_group *blkg); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 221 | void blkiocg_update_timeslice_used(struct blkio_group *blkg, |
| 222 | unsigned long time); |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 223 | void blkiocg_update_dispatch_stats(struct blkio_group *blkg, uint64_t bytes, |
| 224 | bool direction, bool sync); |
| 225 | void blkiocg_update_completion_stats(struct blkio_group *blkg, |
| 226 | uint64_t start_time, uint64_t io_start_time, bool direction, bool sync); |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 227 | void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction, |
| 228 | bool sync); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 229 | void blkiocg_update_request_add_stats(struct blkio_group *blkg, |
| 230 | struct blkio_group *curr_blkg, bool direction, bool sync); |
| 231 | void blkiocg_update_request_remove_stats(struct blkio_group *blkg, |
| 232 | bool direction, bool sync); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 233 | #else |
Jens Axboe | 2f5ea47 | 2009-12-03 21:06:43 +0100 | [diff] [blame] | 234 | struct cgroup; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 235 | static inline struct blkio_cgroup * |
| 236 | cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; } |
| 237 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 238 | static inline void blkio_group_init(struct blkio_group *blkg) {} |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 239 | static inline void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg, |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 240 | struct blkio_group *blkg, void *key, dev_t dev) {} |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 241 | |
| 242 | static inline int |
| 243 | blkiocg_del_blkio_group(struct blkio_group *blkg) { return 0; } |
| 244 | |
| 245 | static inline struct blkio_group * |
| 246 | blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key) { return NULL; } |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 247 | static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg, |
Divyesh Shah | 9a0785b | 2010-04-01 15:01:04 -0700 | [diff] [blame] | 248 | unsigned long time) {} |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 249 | static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg, |
| 250 | uint64_t bytes, bool direction, bool sync) {} |
| 251 | static inline void blkiocg_update_completion_stats(struct blkio_group *blkg, |
| 252 | uint64_t start_time, uint64_t io_start_time, bool direction, |
| 253 | bool sync) {} |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 254 | static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg, |
| 255 | bool direction, bool sync) {} |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 256 | static inline void blkiocg_update_request_add_stats(struct blkio_group *blkg, |
| 257 | struct blkio_group *curr_blkg, bool direction, bool sync) {} |
| 258 | static inline void blkiocg_update_request_remove_stats(struct blkio_group *blkg, |
| 259 | bool direction, bool sync) {} |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 260 | #endif |
| 261 | #endif /* _BLK_CGROUP_H */ |