Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Anticipatory & deadline i/o scheduler. |
| 3 | * |
Jens Axboe | 0fe2347 | 2006-09-04 15:41:16 +0200 | [diff] [blame] | 4 | * Copyright (C) 2002 Jens Axboe <axboe@kernel.dk> |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 5 | * Nick Piggin <nickpiggin@yahoo.com.au> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | */ |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/elevator.h> |
| 12 | #include <linux/bio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/rbtree.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | |
| 20 | #define REQ_SYNC 1 |
| 21 | #define REQ_ASYNC 0 |
| 22 | |
| 23 | /* |
| 24 | * See Documentation/block/as-iosched.txt |
| 25 | */ |
| 26 | |
| 27 | /* |
| 28 | * max time before a read is submitted. |
| 29 | */ |
| 30 | #define default_read_expire (HZ / 8) |
| 31 | |
| 32 | /* |
| 33 | * ditto for writes, these limits are not hard, even |
| 34 | * if the disk is capable of satisfying them. |
| 35 | */ |
| 36 | #define default_write_expire (HZ / 4) |
| 37 | |
| 38 | /* |
| 39 | * read_batch_expire describes how long we will allow a stream of reads to |
| 40 | * persist before looking to see whether it is time to switch over to writes. |
| 41 | */ |
| 42 | #define default_read_batch_expire (HZ / 2) |
| 43 | |
| 44 | /* |
| 45 | * write_batch_expire describes how long we want a stream of writes to run for. |
| 46 | * This is not a hard limit, but a target we set for the auto-tuning thingy. |
| 47 | * See, the problem is: we can send a lot of writes to disk cache / TCQ in |
| 48 | * a short amount of time... |
| 49 | */ |
| 50 | #define default_write_batch_expire (HZ / 8) |
| 51 | |
| 52 | /* |
| 53 | * max time we may wait to anticipate a read (default around 6ms) |
| 54 | */ |
| 55 | #define default_antic_expire ((HZ / 150) ? HZ / 150 : 1) |
| 56 | |
| 57 | /* |
| 58 | * Keep track of up to 20ms thinktimes. We can go as big as we like here, |
| 59 | * however huge values tend to interfere and not decay fast enough. A program |
| 60 | * might be in a non-io phase of operation. Waiting on user input for example, |
| 61 | * or doing a lengthy computation. A small penalty can be justified there, and |
| 62 | * will still catch out those processes that constantly have large thinktimes. |
| 63 | */ |
| 64 | #define MAX_THINKTIME (HZ/50UL) |
| 65 | |
| 66 | /* Bits in as_io_context.state */ |
| 67 | enum as_io_states { |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 68 | AS_TASK_RUNNING=0, /* Process has not exited */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | AS_TASK_IOSTARTED, /* Process has started some IO */ |
| 70 | AS_TASK_IORUNNING, /* Process has completed some IO */ |
| 71 | }; |
| 72 | |
| 73 | enum anticipation_status { |
| 74 | ANTIC_OFF=0, /* Not anticipating (normal operation) */ |
| 75 | ANTIC_WAIT_REQ, /* The last read has not yet completed */ |
| 76 | ANTIC_WAIT_NEXT, /* Currently anticipating a request vs |
| 77 | last read (which has completed) */ |
| 78 | ANTIC_FINISHED, /* Anticipating but have found a candidate |
| 79 | * or timed out */ |
| 80 | }; |
| 81 | |
| 82 | struct as_data { |
| 83 | /* |
| 84 | * run time data |
| 85 | */ |
| 86 | |
| 87 | struct request_queue *q; /* the "owner" queue */ |
| 88 | |
| 89 | /* |
| 90 | * requests (as_rq s) are present on both sort_list and fifo_list |
| 91 | */ |
| 92 | struct rb_root sort_list[2]; |
| 93 | struct list_head fifo_list[2]; |
| 94 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 95 | struct request *next_rq[2]; /* next in sort order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | sector_t last_sector[2]; /* last REQ_SYNC & REQ_ASYNC sectors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | unsigned long exit_prob; /* probability a task will exit while |
| 99 | being waited on */ |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 100 | unsigned long exit_no_coop; /* probablility an exited task will |
| 101 | not be part of a later cooperating |
| 102 | request */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | unsigned long new_ttime_total; /* mean thinktime on new proc */ |
| 104 | unsigned long new_ttime_mean; |
| 105 | u64 new_seek_total; /* mean seek on new proc */ |
| 106 | sector_t new_seek_mean; |
| 107 | |
| 108 | unsigned long current_batch_expires; |
| 109 | unsigned long last_check_fifo[2]; |
| 110 | int changed_batch; /* 1: waiting for old batch to end */ |
| 111 | int new_batch; /* 1: waiting on first read complete */ |
| 112 | int batch_data_dir; /* current batch REQ_SYNC / REQ_ASYNC */ |
| 113 | int write_batch_count; /* max # of reqs in a write batch */ |
| 114 | int current_write_count; /* how many requests left this batch */ |
| 115 | int write_batch_idled; /* has the write batch gone idle? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | enum anticipation_status antic_status; |
| 118 | unsigned long antic_start; /* jiffies: when it started */ |
| 119 | struct timer_list antic_timer; /* anticipatory scheduling timer */ |
| 120 | struct work_struct antic_work; /* Deferred unplugging */ |
| 121 | struct io_context *io_context; /* Identify the expected process */ |
| 122 | int ioc_finished; /* IO associated with io_context is finished */ |
| 123 | int nr_dispatched; |
| 124 | |
| 125 | /* |
| 126 | * settings that change how the i/o scheduler behaves |
| 127 | */ |
| 128 | unsigned long fifo_expire[2]; |
| 129 | unsigned long batch_expire[2]; |
| 130 | unsigned long antic_expire; |
| 131 | }; |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | /* |
| 134 | * per-request data. |
| 135 | */ |
| 136 | enum arq_state { |
| 137 | AS_RQ_NEW=0, /* New - not referenced and not on any lists */ |
| 138 | AS_RQ_QUEUED, /* In the request queue. It belongs to the |
| 139 | scheduler */ |
| 140 | AS_RQ_DISPATCHED, /* On the dispatch list. It belongs to the |
| 141 | driver now */ |
| 142 | AS_RQ_PRESCHED, /* Debug poisoning for requests being used */ |
| 143 | AS_RQ_REMOVED, |
| 144 | AS_RQ_MERGED, |
| 145 | AS_RQ_POSTSCHED, /* when they shouldn't be */ |
| 146 | }; |
| 147 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 148 | #define RQ_IOC(rq) ((struct io_context *) (rq)->elevator_private) |
| 149 | #define RQ_STATE(rq) ((enum arq_state)(rq)->elevator_private2) |
| 150 | #define RQ_SET_STATE(rq, state) ((rq)->elevator_private2 = (void *) state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Jens Axboe | e4313dd | 2006-07-19 05:10:01 +0200 | [diff] [blame] | 152 | static DEFINE_PER_CPU(unsigned long, ioc_count); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 153 | static struct completion *ioc_gone; |
| 154 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 155 | static void as_move_to_dispatch(struct as_data *ad, struct request *rq); |
Tejun Heo | ef9be1d | 2005-11-11 14:27:09 +0100 | [diff] [blame] | 156 | static void as_antic_stop(struct as_data *ad); |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | /* |
| 159 | * IO Context helper functions |
| 160 | */ |
| 161 | |
| 162 | /* Called to deallocate the as_io_context */ |
| 163 | static void free_as_io_context(struct as_io_context *aic) |
| 164 | { |
| 165 | kfree(aic); |
Jens Axboe | e4313dd | 2006-07-19 05:10:01 +0200 | [diff] [blame] | 166 | elv_ioc_count_dec(ioc_count); |
| 167 | if (ioc_gone && !elv_ioc_count_read(ioc_count)) |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 168 | complete(ioc_gone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 171 | static void as_trim(struct io_context *ioc) |
| 172 | { |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 173 | spin_lock(&ioc->lock); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 174 | if (ioc->aic) |
| 175 | free_as_io_context(ioc->aic); |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 176 | ioc->aic = NULL; |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 177 | spin_unlock(&ioc->lock); |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* Called when the task exits */ |
| 181 | static void exit_as_io_context(struct as_io_context *aic) |
| 182 | { |
| 183 | WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state)); |
| 184 | clear_bit(AS_TASK_RUNNING, &aic->state); |
| 185 | } |
| 186 | |
| 187 | static struct as_io_context *alloc_as_io_context(void) |
| 188 | { |
| 189 | struct as_io_context *ret; |
| 190 | |
| 191 | ret = kmalloc(sizeof(*ret), GFP_ATOMIC); |
| 192 | if (ret) { |
| 193 | ret->dtor = free_as_io_context; |
| 194 | ret->exit = exit_as_io_context; |
| 195 | ret->state = 1 << AS_TASK_RUNNING; |
| 196 | atomic_set(&ret->nr_queued, 0); |
| 197 | atomic_set(&ret->nr_dispatched, 0); |
| 198 | spin_lock_init(&ret->lock); |
| 199 | ret->ttime_total = 0; |
| 200 | ret->ttime_samples = 0; |
| 201 | ret->ttime_mean = 0; |
| 202 | ret->seek_total = 0; |
| 203 | ret->seek_samples = 0; |
| 204 | ret->seek_mean = 0; |
Jens Axboe | e4313dd | 2006-07-19 05:10:01 +0200 | [diff] [blame] | 205 | elv_ioc_count_inc(ioc_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * If the current task has no AS IO context then create one and initialise it. |
| 213 | * Then take a ref on the task's io context and return it. |
| 214 | */ |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame] | 215 | static struct io_context *as_get_io_context(int node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame] | 217 | struct io_context *ioc = get_io_context(GFP_ATOMIC, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (ioc && !ioc->aic) { |
| 219 | ioc->aic = alloc_as_io_context(); |
| 220 | if (!ioc->aic) { |
| 221 | put_io_context(ioc); |
| 222 | ioc = NULL; |
| 223 | } |
| 224 | } |
| 225 | return ioc; |
| 226 | } |
| 227 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 228 | static void as_put_io_context(struct request *rq) |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 229 | { |
| 230 | struct as_io_context *aic; |
| 231 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 232 | if (unlikely(!RQ_IOC(rq))) |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 233 | return; |
| 234 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 235 | aic = RQ_IOC(rq)->aic; |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 236 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 237 | if (rq_is_sync(rq) && aic) { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 238 | spin_lock(&aic->lock); |
| 239 | set_bit(AS_TASK_IORUNNING, &aic->state); |
| 240 | aic->last_end_request = jiffies; |
| 241 | spin_unlock(&aic->lock); |
| 242 | } |
| 243 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 244 | put_io_context(RQ_IOC(rq)); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | * rb tree support functions |
| 249 | */ |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 250 | #define RQ_RB_ROOT(ad, rq) (&(ad)->sort_list[rq_is_sync((rq))]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 252 | static void as_add_rq_rb(struct as_data *ad, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 254 | struct request *alias; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 256 | while ((unlikely(alias = elv_rb_add(RQ_RB_ROOT(ad, rq), rq)))) { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 257 | as_move_to_dispatch(ad, alias); |
Tejun Heo | ef9be1d | 2005-11-11 14:27:09 +0100 | [diff] [blame] | 258 | as_antic_stop(ad); |
| 259 | } |
| 260 | } |
| 261 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 262 | static inline void as_del_rq_rb(struct as_data *ad, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 264 | elv_rb_del(RQ_RB_ROOT(ad, rq), rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* |
| 268 | * IO Scheduler proper |
| 269 | */ |
| 270 | |
| 271 | #define MAXBACK (1024 * 1024) /* |
| 272 | * Maximum distance the disk will go backward |
| 273 | * for a request. |
| 274 | */ |
| 275 | |
| 276 | #define BACK_PENALTY 2 |
| 277 | |
| 278 | /* |
| 279 | * as_choose_req selects the preferred one of two requests of the same data_dir |
| 280 | * ignoring time - eg. timeouts, which is the job of as_dispatch_request |
| 281 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 282 | static struct request * |
| 283 | as_choose_req(struct as_data *ad, struct request *rq1, struct request *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
| 285 | int data_dir; |
| 286 | sector_t last, s1, s2, d1, d2; |
| 287 | int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */ |
| 288 | const sector_t maxback = MAXBACK; |
| 289 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 290 | if (rq1 == NULL || rq1 == rq2) |
| 291 | return rq2; |
| 292 | if (rq2 == NULL) |
| 293 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 295 | data_dir = rq_is_sync(rq1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
| 297 | last = ad->last_sector[data_dir]; |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 298 | s1 = rq1->sector; |
| 299 | s2 = rq2->sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 301 | BUG_ON(data_dir != rq_is_sync(rq2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * Strict one way elevator _except_ in the case where we allow |
| 305 | * short backward seeks which are biased as twice the cost of a |
| 306 | * similar forward seek. |
| 307 | */ |
| 308 | if (s1 >= last) |
| 309 | d1 = s1 - last; |
| 310 | else if (s1+maxback >= last) |
| 311 | d1 = (last - s1)*BACK_PENALTY; |
| 312 | else { |
| 313 | r1_wrap = 1; |
| 314 | d1 = 0; /* shut up, gcc */ |
| 315 | } |
| 316 | |
| 317 | if (s2 >= last) |
| 318 | d2 = s2 - last; |
| 319 | else if (s2+maxback >= last) |
| 320 | d2 = (last - s2)*BACK_PENALTY; |
| 321 | else { |
| 322 | r2_wrap = 1; |
| 323 | d2 = 0; |
| 324 | } |
| 325 | |
| 326 | /* Found required data */ |
| 327 | if (!r1_wrap && r2_wrap) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 328 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | else if (!r2_wrap && r1_wrap) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 330 | return rq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | else if (r1_wrap && r2_wrap) { |
| 332 | /* both behind the head */ |
| 333 | if (s1 <= s2) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 334 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | else |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 336 | return rq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* Both requests in front of the head */ |
| 340 | if (d1 < d2) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 341 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | else if (d2 < d1) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 343 | return rq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | else { |
| 345 | if (s1 >= s2) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 346 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | else |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 348 | return rq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
| 352 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 353 | * as_find_next_rq finds the next request after @prev in elevator order. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | * this with as_choose_req form the basis for how the scheduler chooses |
| 355 | * what request to process next. Anticipation works on top of this. |
| 356 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 357 | static struct request * |
| 358 | as_find_next_rq(struct as_data *ad, struct request *last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | struct rb_node *rbnext = rb_next(&last->rb_node); |
| 361 | struct rb_node *rbprev = rb_prev(&last->rb_node); |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 362 | struct request *next = NULL, *prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 364 | BUG_ON(RB_EMPTY_NODE(&last->rb_node)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | if (rbprev) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 367 | prev = rb_entry_rq(rbprev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | if (rbnext) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 370 | next = rb_entry_rq(rbnext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | else { |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 372 | const int data_dir = rq_is_sync(last); |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 373 | |
| 374 | rbnext = rb_first(&ad->sort_list[data_dir]); |
| 375 | if (rbnext && rbnext != &last->rb_node) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 376 | next = rb_entry_rq(rbnext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 379 | return as_choose_req(ad, next, prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /* |
| 383 | * anticipatory scheduling functions follow |
| 384 | */ |
| 385 | |
| 386 | /* |
| 387 | * as_antic_expired tells us when we have anticipated too long. |
| 388 | * The funny "absolute difference" math on the elapsed time is to handle |
| 389 | * jiffy wraps, and disks which have been idle for 0x80000000 jiffies. |
| 390 | */ |
| 391 | static int as_antic_expired(struct as_data *ad) |
| 392 | { |
| 393 | long delta_jif; |
| 394 | |
| 395 | delta_jif = jiffies - ad->antic_start; |
| 396 | if (unlikely(delta_jif < 0)) |
| 397 | delta_jif = -delta_jif; |
| 398 | if (delta_jif < ad->antic_expire) |
| 399 | return 0; |
| 400 | |
| 401 | return 1; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * as_antic_waitnext starts anticipating that a nice request will soon be |
| 406 | * submitted. See also as_antic_waitreq |
| 407 | */ |
| 408 | static void as_antic_waitnext(struct as_data *ad) |
| 409 | { |
| 410 | unsigned long timeout; |
| 411 | |
| 412 | BUG_ON(ad->antic_status != ANTIC_OFF |
| 413 | && ad->antic_status != ANTIC_WAIT_REQ); |
| 414 | |
| 415 | timeout = ad->antic_start + ad->antic_expire; |
| 416 | |
| 417 | mod_timer(&ad->antic_timer, timeout); |
| 418 | |
| 419 | ad->antic_status = ANTIC_WAIT_NEXT; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * as_antic_waitreq starts anticipating. We don't start timing the anticipation |
| 424 | * until the request that we're anticipating on has finished. This means we |
| 425 | * are timing from when the candidate process wakes up hopefully. |
| 426 | */ |
| 427 | static void as_antic_waitreq(struct as_data *ad) |
| 428 | { |
| 429 | BUG_ON(ad->antic_status == ANTIC_FINISHED); |
| 430 | if (ad->antic_status == ANTIC_OFF) { |
| 431 | if (!ad->io_context || ad->ioc_finished) |
| 432 | as_antic_waitnext(ad); |
| 433 | else |
| 434 | ad->antic_status = ANTIC_WAIT_REQ; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * This is called directly by the functions in this file to stop anticipation. |
| 440 | * We kill the timer and schedule a call to the request_fn asap. |
| 441 | */ |
| 442 | static void as_antic_stop(struct as_data *ad) |
| 443 | { |
| 444 | int status = ad->antic_status; |
| 445 | |
| 446 | if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) { |
| 447 | if (status == ANTIC_WAIT_NEXT) |
| 448 | del_timer(&ad->antic_timer); |
| 449 | ad->antic_status = ANTIC_FINISHED; |
| 450 | /* see as_work_handler */ |
| 451 | kblockd_schedule_work(&ad->antic_work); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * as_antic_timeout is the timer function set by as_antic_waitnext. |
| 457 | */ |
| 458 | static void as_antic_timeout(unsigned long data) |
| 459 | { |
| 460 | struct request_queue *q = (struct request_queue *)data; |
| 461 | struct as_data *ad = q->elevator->elevator_data; |
| 462 | unsigned long flags; |
| 463 | |
| 464 | spin_lock_irqsave(q->queue_lock, flags); |
| 465 | if (ad->antic_status == ANTIC_WAIT_REQ |
| 466 | || ad->antic_status == ANTIC_WAIT_NEXT) { |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 467 | struct as_io_context *aic; |
| 468 | spin_lock(&ad->io_context->lock); |
| 469 | aic = ad->io_context->aic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | ad->antic_status = ANTIC_FINISHED; |
| 472 | kblockd_schedule_work(&ad->antic_work); |
| 473 | |
| 474 | if (aic->ttime_samples == 0) { |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 475 | /* process anticipated on has exited or timed out*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | ad->exit_prob = (7*ad->exit_prob + 256)/8; |
| 477 | } |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 478 | if (!test_bit(AS_TASK_RUNNING, &aic->state)) { |
| 479 | /* process not "saved" by a cooperating request */ |
| 480 | ad->exit_no_coop = (7*ad->exit_no_coop + 256)/8; |
| 481 | } |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 482 | spin_unlock(&ad->io_context->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
| 484 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 485 | } |
| 486 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 487 | static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic, |
| 488 | unsigned long ttime) |
| 489 | { |
| 490 | /* fixed point: 1.0 == 1<<8 */ |
| 491 | if (aic->ttime_samples == 0) { |
| 492 | ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8; |
| 493 | ad->new_ttime_mean = ad->new_ttime_total / 256; |
| 494 | |
| 495 | ad->exit_prob = (7*ad->exit_prob)/8; |
| 496 | } |
| 497 | aic->ttime_samples = (7*aic->ttime_samples + 256) / 8; |
| 498 | aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8; |
| 499 | aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples; |
| 500 | } |
| 501 | |
| 502 | static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic, |
| 503 | sector_t sdist) |
| 504 | { |
| 505 | u64 total; |
| 506 | |
| 507 | if (aic->seek_samples == 0) { |
| 508 | ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8; |
| 509 | ad->new_seek_mean = ad->new_seek_total / 256; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * Don't allow the seek distance to get too large from the |
| 514 | * odd fragment, pagein, etc |
| 515 | */ |
| 516 | if (aic->seek_samples <= 60) /* second&third seek */ |
| 517 | sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024); |
| 518 | else |
| 519 | sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64); |
| 520 | |
| 521 | aic->seek_samples = (7*aic->seek_samples + 256) / 8; |
| 522 | aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8; |
| 523 | total = aic->seek_total + (aic->seek_samples/2); |
| 524 | do_div(total, aic->seek_samples); |
| 525 | aic->seek_mean = (sector_t)total; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * as_update_iohist keeps a decaying histogram of IO thinktimes, and |
| 530 | * updates @aic->ttime_mean based on that. It is called when a new |
| 531 | * request is queued. |
| 532 | */ |
| 533 | static void as_update_iohist(struct as_data *ad, struct as_io_context *aic, |
| 534 | struct request *rq) |
| 535 | { |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 536 | int data_dir = rq_is_sync(rq); |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 537 | unsigned long thinktime = 0; |
| 538 | sector_t seek_dist; |
| 539 | |
| 540 | if (aic == NULL) |
| 541 | return; |
| 542 | |
| 543 | if (data_dir == REQ_SYNC) { |
| 544 | unsigned long in_flight = atomic_read(&aic->nr_queued) |
| 545 | + atomic_read(&aic->nr_dispatched); |
| 546 | spin_lock(&aic->lock); |
| 547 | if (test_bit(AS_TASK_IORUNNING, &aic->state) || |
| 548 | test_bit(AS_TASK_IOSTARTED, &aic->state)) { |
| 549 | /* Calculate read -> read thinktime */ |
| 550 | if (test_bit(AS_TASK_IORUNNING, &aic->state) |
| 551 | && in_flight == 0) { |
| 552 | thinktime = jiffies - aic->last_end_request; |
| 553 | thinktime = min(thinktime, MAX_THINKTIME-1); |
| 554 | } |
| 555 | as_update_thinktime(ad, aic, thinktime); |
| 556 | |
| 557 | /* Calculate read -> read seek distance */ |
| 558 | if (aic->last_request_pos < rq->sector) |
| 559 | seek_dist = rq->sector - aic->last_request_pos; |
| 560 | else |
| 561 | seek_dist = aic->last_request_pos - rq->sector; |
| 562 | as_update_seekdist(ad, aic, seek_dist); |
| 563 | } |
| 564 | aic->last_request_pos = rq->sector + rq->nr_sectors; |
| 565 | set_bit(AS_TASK_IOSTARTED, &aic->state); |
| 566 | spin_unlock(&aic->lock); |
| 567 | } |
| 568 | } |
| 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | /* |
| 571 | * as_close_req decides if one request is considered "close" to the |
| 572 | * previous one issued. |
| 573 | */ |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 574 | static int as_close_req(struct as_data *ad, struct as_io_context *aic, |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 575 | struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | { |
Nick Piggin | c6a632a | 2007-05-08 00:26:34 -0700 | [diff] [blame] | 577 | unsigned long delay; /* jiffies */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | sector_t last = ad->last_sector[ad->batch_data_dir]; |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 579 | sector_t next = rq->sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | sector_t delta; /* acceptable close offset (in sectors) */ |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 581 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
| 583 | if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished) |
| 584 | delay = 0; |
| 585 | else |
Nick Piggin | c6a632a | 2007-05-08 00:26:34 -0700 | [diff] [blame] | 586 | delay = jiffies - ad->antic_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 588 | if (delay == 0) |
| 589 | delta = 8192; |
Nick Piggin | c6a632a | 2007-05-08 00:26:34 -0700 | [diff] [blame] | 590 | else if (delay <= (20 * HZ / 1000) && delay <= ad->antic_expire) |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 591 | delta = 8192 << delay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | else |
| 593 | return 1; |
| 594 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 595 | if ((last <= next + (delta>>1)) && (next <= last + delta)) |
| 596 | return 1; |
| 597 | |
| 598 | if (last < next) |
| 599 | s = next - last; |
| 600 | else |
| 601 | s = last - next; |
| 602 | |
| 603 | if (aic->seek_samples == 0) { |
| 604 | /* |
| 605 | * Process has just started IO. Use past statistics to |
| 606 | * gauge success possibility |
| 607 | */ |
| 608 | if (ad->new_seek_mean > s) { |
| 609 | /* this request is better than what we're expecting */ |
| 610 | return 1; |
| 611 | } |
| 612 | |
| 613 | } else { |
| 614 | if (aic->seek_mean > s) { |
| 615 | /* this request is better than what we're expecting */ |
| 616 | return 1; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /* |
| 624 | * as_can_break_anticipation returns true if we have been anticipating this |
| 625 | * request. |
| 626 | * |
| 627 | * It also returns true if the process against which we are anticipating |
| 628 | * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to |
| 629 | * dispatch it ASAP, because we know that application will not be submitting |
| 630 | * any new reads. |
| 631 | * |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 632 | * If the task which has submitted the request has exited, break anticipation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | * |
| 634 | * If this task has queued some other IO, do not enter enticipation. |
| 635 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 636 | static int as_can_break_anticipation(struct as_data *ad, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
| 638 | struct io_context *ioc; |
| 639 | struct as_io_context *aic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | ioc = ad->io_context; |
| 642 | BUG_ON(!ioc); |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 643 | spin_lock(&ioc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 645 | if (rq && ioc == RQ_IOC(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | /* request from same process */ |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 647 | spin_unlock(&ioc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | return 1; |
| 649 | } |
| 650 | |
| 651 | if (ad->ioc_finished && as_antic_expired(ad)) { |
| 652 | /* |
| 653 | * In this situation status should really be FINISHED, |
| 654 | * however the timer hasn't had the chance to run yet. |
| 655 | */ |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 656 | spin_unlock(&ioc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | return 1; |
| 658 | } |
| 659 | |
| 660 | aic = ioc->aic; |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 661 | if (!aic) { |
| 662 | spin_unlock(&ioc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | return 0; |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 664 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | if (atomic_read(&aic->nr_queued) > 0) { |
| 667 | /* process has more requests queued */ |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 668 | spin_unlock(&ioc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | return 1; |
| 670 | } |
| 671 | |
| 672 | if (atomic_read(&aic->nr_dispatched) > 0) { |
| 673 | /* process has more requests dispatched */ |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 674 | spin_unlock(&ioc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | return 1; |
| 676 | } |
| 677 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 678 | if (rq && rq_is_sync(rq) && as_close_req(ad, aic, rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | /* |
| 680 | * Found a close request that is not one of ours. |
| 681 | * |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 682 | * This makes close requests from another process update |
| 683 | * our IO history. Is generally useful when there are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | * two or more cooperating processes working in the same |
| 685 | * area. |
| 686 | */ |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 687 | if (!test_bit(AS_TASK_RUNNING, &aic->state)) { |
| 688 | if (aic->ttime_samples == 0) |
| 689 | ad->exit_prob = (7*ad->exit_prob + 256)/8; |
| 690 | |
| 691 | ad->exit_no_coop = (7*ad->exit_no_coop)/8; |
| 692 | } |
| 693 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 694 | as_update_iohist(ad, aic, rq); |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 695 | spin_unlock(&ioc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | return 1; |
| 697 | } |
| 698 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 699 | if (!test_bit(AS_TASK_RUNNING, &aic->state)) { |
| 700 | /* process anticipated on has exited */ |
| 701 | if (aic->ttime_samples == 0) |
| 702 | ad->exit_prob = (7*ad->exit_prob + 256)/8; |
| 703 | |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 704 | if (ad->exit_no_coop > 128) { |
| 705 | spin_unlock(&ioc->lock); |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 706 | return 1; |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 707 | } |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 708 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
| 710 | if (aic->ttime_samples == 0) { |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 711 | if (ad->new_ttime_mean > ad->antic_expire) { |
| 712 | spin_unlock(&ioc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | return 1; |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 714 | } |
| 715 | if (ad->exit_prob * ad->exit_no_coop > 128*256) { |
| 716 | spin_unlock(&ioc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | return 1; |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | } else if (aic->ttime_mean > ad->antic_expire) { |
| 720 | /* the process thinks too much between requests */ |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 721 | spin_unlock(&ioc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | return 1; |
| 723 | } |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 724 | spin_unlock(&ioc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 729 | * as_can_anticipate indicates whether we should either run rq |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | * or keep anticipating a better request. |
| 731 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 732 | static int as_can_anticipate(struct as_data *ad, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | { |
| 734 | if (!ad->io_context) |
| 735 | /* |
| 736 | * Last request submitted was a write |
| 737 | */ |
| 738 | return 0; |
| 739 | |
| 740 | if (ad->antic_status == ANTIC_FINISHED) |
| 741 | /* |
| 742 | * Don't restart if we have just finished. Run the next request |
| 743 | */ |
| 744 | return 0; |
| 745 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 746 | if (as_can_break_anticipation(ad, rq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | /* |
| 748 | * This request is a good candidate. Don't keep anticipating, |
| 749 | * run it. |
| 750 | */ |
| 751 | return 0; |
| 752 | |
| 753 | /* |
| 754 | * OK from here, we haven't finished, and don't have a decent request! |
| 755 | * Status is either ANTIC_OFF so start waiting, |
| 756 | * ANTIC_WAIT_REQ so continue waiting for request to finish |
| 757 | * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | */ |
| 759 | |
| 760 | return 1; |
| 761 | } |
| 762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 764 | * as_update_rq must be called whenever a request (rq) is added to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | * the sort_list. This function keeps caches up to date, and checks if the |
| 766 | * request might be one we are "anticipating" |
| 767 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 768 | static void as_update_rq(struct as_data *ad, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 770 | const int data_dir = rq_is_sync(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 772 | /* keep the next_rq cache up to date */ |
| 773 | ad->next_rq[data_dir] = as_choose_req(ad, rq, ad->next_rq[data_dir]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
| 775 | /* |
| 776 | * have we been anticipating this request? |
| 777 | * or does it come from the same process as the one we are anticipating |
| 778 | * for? |
| 779 | */ |
| 780 | if (ad->antic_status == ANTIC_WAIT_REQ |
| 781 | || ad->antic_status == ANTIC_WAIT_NEXT) { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 782 | if (as_can_break_anticipation(ad, rq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | as_antic_stop(ad); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * Gathers timings and resizes the write batch automatically |
| 789 | */ |
| 790 | static void update_write_batch(struct as_data *ad) |
| 791 | { |
| 792 | unsigned long batch = ad->batch_expire[REQ_ASYNC]; |
| 793 | long write_time; |
| 794 | |
| 795 | write_time = (jiffies - ad->current_batch_expires) + batch; |
| 796 | if (write_time < 0) |
| 797 | write_time = 0; |
| 798 | |
| 799 | if (write_time > batch && !ad->write_batch_idled) { |
| 800 | if (write_time > batch * 3) |
| 801 | ad->write_batch_count /= 2; |
| 802 | else |
| 803 | ad->write_batch_count--; |
| 804 | } else if (write_time < batch && ad->current_write_count == 0) { |
| 805 | if (batch > write_time * 3) |
| 806 | ad->write_batch_count *= 2; |
| 807 | else |
| 808 | ad->write_batch_count++; |
| 809 | } |
| 810 | |
| 811 | if (ad->write_batch_count < 1) |
| 812 | ad->write_batch_count = 1; |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * as_completed_request is to be called when a request has completed and |
| 817 | * returned something to the requesting process, be it an error or data. |
| 818 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 819 | static void as_completed_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
| 821 | struct as_data *ad = q->elevator->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
| 823 | WARN_ON(!list_empty(&rq->queuelist)); |
| 824 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 825 | if (RQ_STATE(rq) != AS_RQ_REMOVED) { |
| 826 | printk("rq->state %d\n", RQ_STATE(rq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | WARN_ON(1); |
| 828 | goto out; |
| 829 | } |
| 830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | if (ad->changed_batch && ad->nr_dispatched == 1) { |
| 832 | kblockd_schedule_work(&ad->antic_work); |
| 833 | ad->changed_batch = 0; |
| 834 | |
| 835 | if (ad->batch_data_dir == REQ_SYNC) |
| 836 | ad->new_batch = 1; |
| 837 | } |
| 838 | WARN_ON(ad->nr_dispatched == 0); |
| 839 | ad->nr_dispatched--; |
| 840 | |
| 841 | /* |
| 842 | * Start counting the batch from when a request of that direction is |
| 843 | * actually serviced. This should help devices with big TCQ windows |
| 844 | * and writeback caches |
| 845 | */ |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 846 | if (ad->new_batch && ad->batch_data_dir == rq_is_sync(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | update_write_batch(ad); |
| 848 | ad->current_batch_expires = jiffies + |
| 849 | ad->batch_expire[REQ_SYNC]; |
| 850 | ad->new_batch = 0; |
| 851 | } |
| 852 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 853 | if (ad->io_context == RQ_IOC(rq) && ad->io_context) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | ad->antic_start = jiffies; |
| 855 | ad->ioc_finished = 1; |
| 856 | if (ad->antic_status == ANTIC_WAIT_REQ) { |
| 857 | /* |
| 858 | * We were waiting on this request, now anticipate |
| 859 | * the next one |
| 860 | */ |
| 861 | as_antic_waitnext(ad); |
| 862 | } |
| 863 | } |
| 864 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 865 | as_put_io_context(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | out: |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 867 | RQ_SET_STATE(rq, AS_RQ_POSTSCHED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | /* |
| 871 | * as_remove_queued_request removes a request from the pre dispatch queue |
| 872 | * without updating refcounts. It is expected the caller will drop the |
| 873 | * reference unless it replaces the request at somepart of the elevator |
| 874 | * (ie. the dispatch queue) |
| 875 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 876 | static void as_remove_queued_request(struct request_queue *q, |
| 877 | struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | { |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 879 | const int data_dir = rq_is_sync(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | struct as_data *ad = q->elevator->elevator_data; |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 881 | struct io_context *ioc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 883 | WARN_ON(RQ_STATE(rq) != AS_RQ_QUEUED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 885 | ioc = RQ_IOC(rq); |
| 886 | if (ioc && ioc->aic) { |
| 887 | BUG_ON(!atomic_read(&ioc->aic->nr_queued)); |
| 888 | atomic_dec(&ioc->aic->nr_queued); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 892 | * Update the "next_rq" cache if we are about to remove its |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | * entry |
| 894 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 895 | if (ad->next_rq[data_dir] == rq) |
| 896 | ad->next_rq[data_dir] = as_find_next_rq(ad, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 898 | rq_fifo_clear(rq); |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 899 | as_del_rq_rb(ad, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /* |
Aaron Carroll | 8896f3c | 2007-12-05 21:06:50 +1100 | [diff] [blame] | 903 | * as_fifo_expired returns 0 if there are no expired requests on the fifo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | * 1 otherwise. It is ratelimited so that we only perform the check once per |
| 905 | * `fifo_expire' interval. Otherwise a large number of expired requests |
| 906 | * would create a hopeless seekstorm. |
| 907 | * |
| 908 | * See as_antic_expired comment. |
| 909 | */ |
| 910 | static int as_fifo_expired(struct as_data *ad, int adir) |
| 911 | { |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 912 | struct request *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | long delta_jif; |
| 914 | |
| 915 | delta_jif = jiffies - ad->last_check_fifo[adir]; |
| 916 | if (unlikely(delta_jif < 0)) |
| 917 | delta_jif = -delta_jif; |
| 918 | if (delta_jif < ad->fifo_expire[adir]) |
| 919 | return 0; |
| 920 | |
| 921 | ad->last_check_fifo[adir] = jiffies; |
| 922 | |
| 923 | if (list_empty(&ad->fifo_list[adir])) |
| 924 | return 0; |
| 925 | |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 926 | rq = rq_entry_fifo(ad->fifo_list[adir].next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 928 | return time_after(jiffies, rq_fifo_time(rq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /* |
| 932 | * as_batch_expired returns true if the current batch has expired. A batch |
| 933 | * is a set of reads or a set of writes. |
| 934 | */ |
| 935 | static inline int as_batch_expired(struct as_data *ad) |
| 936 | { |
| 937 | if (ad->changed_batch || ad->new_batch) |
| 938 | return 0; |
| 939 | |
| 940 | if (ad->batch_data_dir == REQ_SYNC) |
| 941 | /* TODO! add a check so a complete fifo gets written? */ |
| 942 | return time_after(jiffies, ad->current_batch_expires); |
| 943 | |
| 944 | return time_after(jiffies, ad->current_batch_expires) |
| 945 | || ad->current_write_count == 0; |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * move an entry to dispatch queue |
| 950 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 951 | static void as_move_to_dispatch(struct as_data *ad, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | { |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 953 | const int data_dir = rq_is_sync(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 955 | BUG_ON(RB_EMPTY_NODE(&rq->rb_node)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | |
| 957 | as_antic_stop(ad); |
| 958 | ad->antic_status = ANTIC_OFF; |
| 959 | |
| 960 | /* |
| 961 | * This has to be set in order to be correctly updated by |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 962 | * as_find_next_rq |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | */ |
| 964 | ad->last_sector[data_dir] = rq->sector + rq->nr_sectors; |
| 965 | |
| 966 | if (data_dir == REQ_SYNC) { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 967 | struct io_context *ioc = RQ_IOC(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | /* In case we have to anticipate after this */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 969 | copy_io_context(&ad->io_context, &ioc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | } else { |
| 971 | if (ad->io_context) { |
| 972 | put_io_context(ad->io_context); |
| 973 | ad->io_context = NULL; |
| 974 | } |
| 975 | |
| 976 | if (ad->current_write_count != 0) |
| 977 | ad->current_write_count--; |
| 978 | } |
| 979 | ad->ioc_finished = 0; |
| 980 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 981 | ad->next_rq[data_dir] = as_find_next_rq(ad, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | |
| 983 | /* |
| 984 | * take it off the sort and fifo list, add to dispatch queue |
| 985 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | as_remove_queued_request(ad->q, rq); |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 987 | WARN_ON(RQ_STATE(rq) != AS_RQ_QUEUED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 989 | elv_dispatch_sort(ad->q, rq); |
| 990 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 991 | RQ_SET_STATE(rq, AS_RQ_DISPATCHED); |
| 992 | if (RQ_IOC(rq) && RQ_IOC(rq)->aic) |
| 993 | atomic_inc(&RQ_IOC(rq)->aic->nr_dispatched); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | ad->nr_dispatched++; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * as_dispatch_request selects the best request according to |
| 999 | * read/write expire, batch expire, etc, and moves it to the dispatch |
| 1000 | * queue. Returns 1 if a request was found, 0 otherwise. |
| 1001 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1002 | static int as_dispatch_request(struct request_queue *q, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1004 | struct as_data *ad = q->elevator->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]); |
| 1006 | const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]); |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1007 | struct request *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1009 | if (unlikely(force)) { |
| 1010 | /* |
| 1011 | * Forced dispatch, accounting is useless. Reset |
| 1012 | * accounting states and dump fifo_lists. Note that |
| 1013 | * batch_data_dir is reset to REQ_SYNC to avoid |
| 1014 | * screwing write batch accounting as write batch |
| 1015 | * accounting occurs on W->R transition. |
| 1016 | */ |
| 1017 | int dispatched = 0; |
| 1018 | |
| 1019 | ad->batch_data_dir = REQ_SYNC; |
| 1020 | ad->changed_batch = 0; |
| 1021 | ad->new_batch = 0; |
| 1022 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1023 | while (ad->next_rq[REQ_SYNC]) { |
| 1024 | as_move_to_dispatch(ad, ad->next_rq[REQ_SYNC]); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1025 | dispatched++; |
| 1026 | } |
| 1027 | ad->last_check_fifo[REQ_SYNC] = jiffies; |
| 1028 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1029 | while (ad->next_rq[REQ_ASYNC]) { |
| 1030 | as_move_to_dispatch(ad, ad->next_rq[REQ_ASYNC]); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1031 | dispatched++; |
| 1032 | } |
| 1033 | ad->last_check_fifo[REQ_ASYNC] = jiffies; |
| 1034 | |
| 1035 | return dispatched; |
| 1036 | } |
| 1037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | /* Signal that the write batch was uncontended, so we can't time it */ |
| 1039 | if (ad->batch_data_dir == REQ_ASYNC && !reads) { |
| 1040 | if (ad->current_write_count == 0 || !writes) |
| 1041 | ad->write_batch_idled = 1; |
| 1042 | } |
| 1043 | |
| 1044 | if (!(reads || writes) |
| 1045 | || ad->antic_status == ANTIC_WAIT_REQ |
| 1046 | || ad->antic_status == ANTIC_WAIT_NEXT |
| 1047 | || ad->changed_batch) |
| 1048 | return 0; |
| 1049 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 1050 | if (!(reads && writes && as_batch_expired(ad))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | /* |
| 1052 | * batch is still running or no reads or no writes |
| 1053 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1054 | rq = ad->next_rq[ad->batch_data_dir]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
| 1056 | if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) { |
| 1057 | if (as_fifo_expired(ad, REQ_SYNC)) |
| 1058 | goto fifo_expired; |
| 1059 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1060 | if (as_can_anticipate(ad, rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | as_antic_waitreq(ad); |
| 1062 | return 0; |
| 1063 | } |
| 1064 | } |
| 1065 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1066 | if (rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | /* we have a "next request" */ |
| 1068 | if (reads && !writes) |
| 1069 | ad->current_batch_expires = |
| 1070 | jiffies + ad->batch_expire[REQ_SYNC]; |
| 1071 | goto dispatch_request; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * at this point we are not running a batch. select the appropriate |
| 1077 | * data direction (read / write) |
| 1078 | */ |
| 1079 | |
| 1080 | if (reads) { |
Jens Axboe | dd67d05 | 2006-06-21 09:36:18 +0200 | [diff] [blame] | 1081 | BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_SYNC])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
| 1083 | if (writes && ad->batch_data_dir == REQ_SYNC) |
| 1084 | /* |
| 1085 | * Last batch was a read, switch to writes |
| 1086 | */ |
| 1087 | goto dispatch_writes; |
| 1088 | |
| 1089 | if (ad->batch_data_dir == REQ_ASYNC) { |
| 1090 | WARN_ON(ad->new_batch); |
| 1091 | ad->changed_batch = 1; |
| 1092 | } |
| 1093 | ad->batch_data_dir = REQ_SYNC; |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1094 | rq = rq_entry_fifo(ad->fifo_list[REQ_SYNC].next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | ad->last_check_fifo[ad->batch_data_dir] = jiffies; |
| 1096 | goto dispatch_request; |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * the last batch was a read |
| 1101 | */ |
| 1102 | |
| 1103 | if (writes) { |
| 1104 | dispatch_writes: |
Jens Axboe | dd67d05 | 2006-06-21 09:36:18 +0200 | [diff] [blame] | 1105 | BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_ASYNC])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
| 1107 | if (ad->batch_data_dir == REQ_SYNC) { |
| 1108 | ad->changed_batch = 1; |
| 1109 | |
| 1110 | /* |
| 1111 | * new_batch might be 1 when the queue runs out of |
| 1112 | * reads. A subsequent submission of a write might |
| 1113 | * cause a change of batch before the read is finished. |
| 1114 | */ |
| 1115 | ad->new_batch = 0; |
| 1116 | } |
| 1117 | ad->batch_data_dir = REQ_ASYNC; |
| 1118 | ad->current_write_count = ad->write_batch_count; |
| 1119 | ad->write_batch_idled = 0; |
Aaron Carroll | 4956512 | 2007-12-05 21:07:07 +1100 | [diff] [blame] | 1120 | rq = rq_entry_fifo(ad->fifo_list[REQ_ASYNC].next); |
| 1121 | ad->last_check_fifo[REQ_ASYNC] = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | goto dispatch_request; |
| 1123 | } |
| 1124 | |
| 1125 | BUG(); |
| 1126 | return 0; |
| 1127 | |
| 1128 | dispatch_request: |
| 1129 | /* |
| 1130 | * If a request has expired, service it. |
| 1131 | */ |
| 1132 | |
| 1133 | if (as_fifo_expired(ad, ad->batch_data_dir)) { |
| 1134 | fifo_expired: |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1135 | rq = rq_entry_fifo(ad->fifo_list[ad->batch_data_dir].next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | if (ad->changed_batch) { |
| 1139 | WARN_ON(ad->new_batch); |
| 1140 | |
| 1141 | if (ad->nr_dispatched) |
| 1142 | return 0; |
| 1143 | |
| 1144 | if (ad->batch_data_dir == REQ_ASYNC) |
| 1145 | ad->current_batch_expires = jiffies + |
| 1146 | ad->batch_expire[REQ_ASYNC]; |
| 1147 | else |
| 1148 | ad->new_batch = 1; |
| 1149 | |
| 1150 | ad->changed_batch = 0; |
| 1151 | } |
| 1152 | |
| 1153 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1154 | * rq is the selected appropriate request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1156 | as_move_to_dispatch(ad, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
| 1158 | return 1; |
| 1159 | } |
| 1160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1162 | * add rq to rbtree and fifo |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1164 | static void as_add_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1166 | struct as_data *ad = q->elevator->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | int data_dir; |
| 1168 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1169 | RQ_SET_STATE(rq, AS_RQ_NEW); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1170 | |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 1171 | data_dir = rq_is_sync(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame] | 1173 | rq->elevator_private = as_get_io_context(q->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1175 | if (RQ_IOC(rq)) { |
| 1176 | as_update_iohist(ad, RQ_IOC(rq)->aic, rq); |
| 1177 | atomic_inc(&RQ_IOC(rq)->aic->nr_queued); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1180 | as_add_rq_rb(ad, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | |
Tejun Heo | ef9be1d | 2005-11-11 14:27:09 +0100 | [diff] [blame] | 1182 | /* |
Aaron Carroll | 8896f3c | 2007-12-05 21:06:50 +1100 | [diff] [blame] | 1183 | * set expire time and add to fifo list |
Tejun Heo | ef9be1d | 2005-11-11 14:27:09 +0100 | [diff] [blame] | 1184 | */ |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 1185 | rq_set_fifo_time(rq, jiffies + ad->fifo_expire[data_dir]); |
| 1186 | list_add_tail(&rq->queuelist, &ad->fifo_list[data_dir]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1188 | as_update_rq(ad, rq); /* keep state machine up to date */ |
| 1189 | RQ_SET_STATE(rq, AS_RQ_QUEUED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1192 | static void as_activate_request(struct request_queue *q, struct request *rq) |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1193 | { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1194 | WARN_ON(RQ_STATE(rq) != AS_RQ_DISPATCHED); |
| 1195 | RQ_SET_STATE(rq, AS_RQ_REMOVED); |
| 1196 | if (RQ_IOC(rq) && RQ_IOC(rq)->aic) |
| 1197 | atomic_dec(&RQ_IOC(rq)->aic->nr_dispatched); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1198 | } |
| 1199 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1200 | static void as_deactivate_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1202 | WARN_ON(RQ_STATE(rq) != AS_RQ_REMOVED); |
| 1203 | RQ_SET_STATE(rq, AS_RQ_DISPATCHED); |
| 1204 | if (RQ_IOC(rq) && RQ_IOC(rq)->aic) |
| 1205 | atomic_inc(&RQ_IOC(rq)->aic->nr_dispatched); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | /* |
| 1209 | * as_queue_empty tells us if there are requests left in the device. It may |
| 1210 | * not be the case that a driver can get the next request even if the queue |
| 1211 | * is not empty - it is used in the block layer to check for plugging and |
| 1212 | * merging opportunities |
| 1213 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1214 | static int as_queue_empty(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | { |
| 1216 | struct as_data *ad = q->elevator->elevator_data; |
| 1217 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1218 | return list_empty(&ad->fifo_list[REQ_ASYNC]) |
| 1219 | && list_empty(&ad->fifo_list[REQ_SYNC]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | static int |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1223 | as_merge(struct request_queue *q, struct request **req, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | { |
| 1225 | struct as_data *ad = q->elevator->elevator_data; |
| 1226 | sector_t rb_key = bio->bi_sector + bio_sectors(bio); |
| 1227 | struct request *__rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | |
| 1229 | /* |
| 1230 | * check for front merge |
| 1231 | */ |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 1232 | __rq = elv_rb_find(&ad->sort_list[bio_data_dir(bio)], rb_key); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 1233 | if (__rq && elv_rq_merge_ok(__rq, bio)) { |
| 1234 | *req = __rq; |
| 1235 | return ELEVATOR_FRONT_MERGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | return ELEVATOR_NO_MERGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1241 | static void as_merged_request(struct request_queue *q, struct request *req, |
| 1242 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | { |
| 1244 | struct as_data *ad = q->elevator->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
| 1246 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | * if the merge was a front merge, we need to reposition request |
| 1248 | */ |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 1249 | if (type == ELEVATOR_FRONT_MERGE) { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1250 | as_del_rq_rb(ad, req); |
| 1251 | as_add_rq_rb(ad, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | /* |
| 1253 | * Note! At this stage of this and the next function, our next |
| 1254 | * request may not be optimal - eg the request may have "grown" |
| 1255 | * behind the disk head. We currently don't bother adjusting. |
| 1256 | */ |
| 1257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1260 | static void as_merged_requests(struct request_queue *q, struct request *req, |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 1261 | struct request *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1264 | * if next expires before rq, assign its expire time to arq |
| 1265 | * and move into next position (next will be deleted) in fifo |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | */ |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 1267 | if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) { |
| 1268 | if (time_before(rq_fifo_time(next), rq_fifo_time(req))) { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1269 | struct io_context *rioc = RQ_IOC(req); |
| 1270 | struct io_context *nioc = RQ_IOC(next); |
| 1271 | |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 1272 | list_move(&req->queuelist, &next->queuelist); |
| 1273 | rq_set_fifo_time(req, rq_fifo_time(next)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | /* |
| 1275 | * Don't copy here but swap, because when anext is |
| 1276 | * removed below, it must contain the unused context |
| 1277 | */ |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 1278 | double_spin_lock(&rioc->lock, &nioc->lock, rioc < nioc); |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1279 | swap_io_context(&rioc, &nioc); |
Jens Axboe | 521f3bbd | 2008-01-21 20:03:12 +0100 | [diff] [blame] | 1280 | double_spin_unlock(&rioc->lock, &nioc->lock, rioc < nioc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | * kill knowledge of next, this one is a goner |
| 1286 | */ |
| 1287 | as_remove_queued_request(q, next); |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1288 | as_put_io_context(next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1290 | RQ_SET_STATE(next, AS_RQ_MERGED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * This is executed in a "deferred" process context, by kblockd. It calls the |
| 1295 | * driver's request_fn so the driver can submit that request. |
| 1296 | * |
| 1297 | * IMPORTANT! This guy will reenter the elevator, so set up all queue global |
| 1298 | * state before calling, and don't rely on any state over calls. |
| 1299 | * |
| 1300 | * FIXME! dispatch queue is not a queue at all! |
| 1301 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1302 | static void as_work_handler(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1304 | struct as_data *ad = container_of(work, struct as_data, antic_work); |
| 1305 | struct request_queue *q = ad->q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | unsigned long flags; |
| 1307 | |
| 1308 | spin_lock_irqsave(q->queue_lock, flags); |
Jens Axboe | dc72ef4 | 2006-07-20 14:54:05 +0200 | [diff] [blame] | 1309 | blk_start_queueing(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 1311 | } |
| 1312 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1313 | static int as_may_queue(struct request_queue *q, int rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | { |
| 1315 | int ret = ELV_MQUEUE_MAY; |
| 1316 | struct as_data *ad = q->elevator->elevator_data; |
| 1317 | struct io_context *ioc; |
| 1318 | if (ad->antic_status == ANTIC_WAIT_REQ || |
| 1319 | ad->antic_status == ANTIC_WAIT_NEXT) { |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame] | 1320 | ioc = as_get_io_context(q->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | if (ad->io_context == ioc) |
| 1322 | ret = ELV_MQUEUE_MUST; |
| 1323 | put_io_context(ioc); |
| 1324 | } |
| 1325 | |
| 1326 | return ret; |
| 1327 | } |
| 1328 | |
| 1329 | static void as_exit_queue(elevator_t *e) |
| 1330 | { |
| 1331 | struct as_data *ad = e->elevator_data; |
| 1332 | |
| 1333 | del_timer_sync(&ad->antic_timer); |
Andrew Morton | 19a75d8 | 2007-05-09 02:33:56 -0700 | [diff] [blame] | 1334 | kblockd_flush_work(&ad->antic_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
| 1336 | BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC])); |
| 1337 | BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC])); |
| 1338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | put_io_context(ad->io_context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | kfree(ad); |
| 1341 | } |
| 1342 | |
| 1343 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1344 | * initialize elevator private data (as_data). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1346 | static void *as_init_queue(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | { |
| 1348 | struct as_data *ad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 1350 | ad = kmalloc_node(sizeof(*ad), GFP_KERNEL | __GFP_ZERO, q->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | if (!ad) |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 1352 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | |
| 1354 | ad->q = q; /* Identify what queue the data belongs to */ |
| 1355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | /* anticipatory scheduling helpers */ |
| 1357 | ad->antic_timer.function = as_antic_timeout; |
| 1358 | ad->antic_timer.data = (unsigned long)q; |
| 1359 | init_timer(&ad->antic_timer); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1360 | INIT_WORK(&ad->antic_work, as_work_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]); |
| 1363 | INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]); |
| 1364 | ad->sort_list[REQ_SYNC] = RB_ROOT; |
| 1365 | ad->sort_list[REQ_ASYNC] = RB_ROOT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | ad->fifo_expire[REQ_SYNC] = default_read_expire; |
| 1367 | ad->fifo_expire[REQ_ASYNC] = default_write_expire; |
| 1368 | ad->antic_expire = default_antic_expire; |
| 1369 | ad->batch_expire[REQ_SYNC] = default_read_batch_expire; |
| 1370 | ad->batch_expire[REQ_ASYNC] = default_write_batch_expire; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
| 1372 | ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC]; |
| 1373 | ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10; |
| 1374 | if (ad->write_batch_count < 2) |
| 1375 | ad->write_batch_count = 2; |
| 1376 | |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 1377 | return ad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | /* |
| 1381 | * sysfs parts below |
| 1382 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | |
| 1384 | static ssize_t |
| 1385 | as_var_show(unsigned int var, char *page) |
| 1386 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | return sprintf(page, "%d\n", var); |
| 1388 | } |
| 1389 | |
| 1390 | static ssize_t |
| 1391 | as_var_store(unsigned long *var, const char *page, size_t count) |
| 1392 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | char *p = (char *) page; |
| 1394 | |
Jens Axboe | c9b3ad6 | 2005-07-27 11:43:37 -0700 | [diff] [blame] | 1395 | *var = simple_strtoul(p, &p, 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | return count; |
| 1397 | } |
| 1398 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 1399 | static ssize_t est_time_show(elevator_t *e, char *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1401 | struct as_data *ad = e->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | int pos = 0; |
| 1403 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 1404 | pos += sprintf(page+pos, "%lu %% exit probability\n", |
| 1405 | 100*ad->exit_prob/256); |
| 1406 | pos += sprintf(page+pos, "%lu %% probability of exiting without a " |
| 1407 | "cooperating process submitting IO\n", |
| 1408 | 100*ad->exit_no_coop/256); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean); |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 1410 | pos += sprintf(page+pos, "%llu sectors new seek distance\n", |
| 1411 | (unsigned long long)ad->new_seek_mean); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
| 1413 | return pos; |
| 1414 | } |
| 1415 | |
| 1416 | #define SHOW_FUNCTION(__FUNC, __VAR) \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1417 | static ssize_t __FUNC(elevator_t *e, char *page) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | { \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1419 | struct as_data *ad = e->elevator_data; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | return as_var_show(jiffies_to_msecs((__VAR)), (page)); \ |
| 1421 | } |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 1422 | SHOW_FUNCTION(as_read_expire_show, ad->fifo_expire[REQ_SYNC]); |
| 1423 | SHOW_FUNCTION(as_write_expire_show, ad->fifo_expire[REQ_ASYNC]); |
| 1424 | SHOW_FUNCTION(as_antic_expire_show, ad->antic_expire); |
| 1425 | SHOW_FUNCTION(as_read_batch_expire_show, ad->batch_expire[REQ_SYNC]); |
| 1426 | SHOW_FUNCTION(as_write_batch_expire_show, ad->batch_expire[REQ_ASYNC]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | #undef SHOW_FUNCTION |
| 1428 | |
| 1429 | #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1430 | static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | { \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1432 | struct as_data *ad = e->elevator_data; \ |
| 1433 | int ret = as_var_store(__PTR, (page), count); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | if (*(__PTR) < (MIN)) \ |
| 1435 | *(__PTR) = (MIN); \ |
| 1436 | else if (*(__PTR) > (MAX)) \ |
| 1437 | *(__PTR) = (MAX); \ |
| 1438 | *(__PTR) = msecs_to_jiffies(*(__PTR)); \ |
| 1439 | return ret; \ |
| 1440 | } |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 1441 | STORE_FUNCTION(as_read_expire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX); |
| 1442 | STORE_FUNCTION(as_write_expire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX); |
| 1443 | STORE_FUNCTION(as_antic_expire_store, &ad->antic_expire, 0, INT_MAX); |
| 1444 | STORE_FUNCTION(as_read_batch_expire_store, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | &ad->batch_expire[REQ_SYNC], 0, INT_MAX); |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 1446 | STORE_FUNCTION(as_write_batch_expire_store, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | &ad->batch_expire[REQ_ASYNC], 0, INT_MAX); |
| 1448 | #undef STORE_FUNCTION |
| 1449 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 1450 | #define AS_ATTR(name) \ |
| 1451 | __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 1453 | static struct elv_fs_entry as_attrs[] = { |
| 1454 | __ATTR_RO(est_time), |
| 1455 | AS_ATTR(read_expire), |
| 1456 | AS_ATTR(write_expire), |
| 1457 | AS_ATTR(antic_expire), |
| 1458 | AS_ATTR(read_batch_expire), |
| 1459 | AS_ATTR(write_batch_expire), |
| 1460 | __ATTR_NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | }; |
| 1462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | static struct elevator_type iosched_as = { |
| 1464 | .ops = { |
| 1465 | .elevator_merge_fn = as_merge, |
| 1466 | .elevator_merged_fn = as_merged_request, |
| 1467 | .elevator_merge_req_fn = as_merged_requests, |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1468 | .elevator_dispatch_fn = as_dispatch_request, |
| 1469 | .elevator_add_req_fn = as_add_request, |
| 1470 | .elevator_activate_req_fn = as_activate_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | .elevator_deactivate_req_fn = as_deactivate_request, |
| 1472 | .elevator_queue_empty_fn = as_queue_empty, |
| 1473 | .elevator_completed_req_fn = as_completed_request, |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 1474 | .elevator_former_req_fn = elv_rb_former_request, |
| 1475 | .elevator_latter_req_fn = elv_rb_latter_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | .elevator_may_queue_fn = as_may_queue, |
| 1477 | .elevator_init_fn = as_init_queue, |
| 1478 | .elevator_exit_fn = as_exit_queue, |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 1479 | .trim = as_trim, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | }, |
| 1481 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1482 | .elevator_attrs = as_attrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | .elevator_name = "anticipatory", |
| 1484 | .elevator_owner = THIS_MODULE, |
| 1485 | }; |
| 1486 | |
| 1487 | static int __init as_init(void) |
| 1488 | { |
Adrian Bunk | 2fdd82b | 2007-12-12 18:51:56 +0100 | [diff] [blame] | 1489 | elv_register(&iosched_as); |
| 1490 | |
| 1491 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | static void __exit as_exit(void) |
| 1495 | { |
Peter Zijlstra | 6e9a473 | 2006-09-30 23:28:10 -0700 | [diff] [blame] | 1496 | DECLARE_COMPLETION_ONSTACK(all_gone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | elv_unregister(&iosched_as); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 1498 | ioc_gone = &all_gone; |
OGAWA Hirofumi | fba8227 | 2006-04-18 09:44:06 +0200 | [diff] [blame] | 1499 | /* ioc_gone's update must be visible before reading ioc_count */ |
| 1500 | smp_wmb(); |
Jens Axboe | e4313dd | 2006-07-19 05:10:01 +0200 | [diff] [blame] | 1501 | if (elv_ioc_count_read(ioc_count)) |
OGAWA Hirofumi | fba8227 | 2006-04-18 09:44:06 +0200 | [diff] [blame] | 1502 | wait_for_completion(ioc_gone); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 1503 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | module_init(as_init); |
| 1507 | module_exit(as_exit); |
| 1508 | |
| 1509 | MODULE_AUTHOR("Nick Piggin"); |
| 1510 | MODULE_LICENSE("GPL"); |
| 1511 | MODULE_DESCRIPTION("anticipatory IO scheduler"); |