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