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