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