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