blob: 134545b544c5fe6e422bfc4ebdaaacacc142305c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Anticipatory & deadline i/o scheduler.
3 *
4 * Copyright (C) 2002 Jens Axboe <axboe@suse.de>
Nick Pigginf5b3db02005-11-07 00:59:53 -08005 * Nick Piggin <nickpiggin@yahoo.com.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/init.h>
16#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 */
67enum as_io_states {
Nick Pigginf5b3db02005-11-07 00:59:53 -080068 AS_TASK_RUNNING=0, /* Process has not exited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 AS_TASK_IOSTARTED, /* Process has started some IO */
70 AS_TASK_IORUNNING, /* Process has completed some IO */
71};
72
73enum 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
82struct 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 Torvalds1da177e2005-04-16 15:20:36 -070097
98 unsigned long exit_prob; /* probability a task will exit while
99 being waited on */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800100 unsigned long exit_no_coop; /* probablility an exited task will
101 not be part of a later cooperating
102 request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 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 */
139enum 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
151struct as_rq {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct request *request;
153
154 struct io_context *io_context; /* The submitting task */
155
156 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * 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
168static kmem_cache_t *arq_pool;
169
Al Viro334e94d2006-03-18 15:05:53 -0500170static atomic_t ioc_count = ATOMIC_INIT(0);
171static struct completion *ioc_gone;
172
Tejun Heoef9be1d2005-11-11 14:27:09 +0100173static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq);
174static void as_antic_stop(struct as_data *ad);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/*
177 * IO Context helper functions
178 */
179
180/* Called to deallocate the as_io_context */
181static void free_as_io_context(struct as_io_context *aic)
182{
183 kfree(aic);
Al Viro334e94d2006-03-18 15:05:53 -0500184 if (atomic_dec_and_test(&ioc_count) && ioc_gone)
185 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Al Viroe17a9482006-03-18 13:21:20 -0500188static void as_trim(struct io_context *ioc)
189{
Al Viro334e94d2006-03-18 15:05:53 -0500190 if (ioc->aic)
191 free_as_io_context(ioc->aic);
Al Viroe17a9482006-03-18 13:21:20 -0500192 ioc->aic = NULL;
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/* Called when the task exits */
196static 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
202static 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 Viro334e94d2006-03-18 15:05:53 -0500220 atomic_inc(&ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
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 */
230static 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 Axboeb4878f22005-10-20 16:42:29 +0200243static 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 Torvalds1da177e2005-04-16 15:20:36 -0700262/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * rb tree support functions
264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265#define ARQ_RB_ROOT(ad, arq) (&(ad)->sort_list[(arq)->is_sync])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Jens Axboee37f3462006-07-18 21:06:01 +0200267static void as_add_arq_rb(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Jens Axboee37f3462006-07-18 21:06:01 +0200269 struct as_rq *arq = RQ_DATA(rq);
270 struct request *alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Jens Axboee37f3462006-07-18 21:06:01 +0200272 while ((unlikely(alias = elv_rb_add(ARQ_RB_ROOT(ad, arq), rq)))) {
273 as_move_to_dispatch(ad, RQ_DATA(alias));
Tejun Heoef9be1d2005-11-11 14:27:09 +0100274 as_antic_stop(ad);
275 }
276}
277
Jens Axboee37f3462006-07-18 21:06:01 +0200278static inline void as_del_arq_rb(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Jens Axboee37f3462006-07-18 21:06:01 +0200280 elv_rb_del(ARQ_RB_ROOT(ad, RQ_DATA(rq)), rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
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 */
298static struct as_rq *
299as_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 Axboee37f3462006-07-18 21:06:01 +0200373static struct as_rq *as_find_next_arq(struct as_data *ad, struct as_rq *arq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Jens Axboee37f3462006-07-18 21:06:01 +0200375 struct request *last = arq->request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct rb_node *rbnext = rb_next(&last->rb_node);
377 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboee37f3462006-07-18 21:06:01 +0200378 struct as_rq *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Jens Axboee37f3462006-07-18 21:06:01 +0200380 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if (rbprev)
Jens Axboee37f3462006-07-18 21:06:01 +0200383 prev = RQ_DATA(rb_entry_rq(rbprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (rbnext)
Jens Axboee37f3462006-07-18 21:06:01 +0200386 next = RQ_DATA(rb_entry_rq(rbnext));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 else {
Jens Axboee37f3462006-07-18 21:06:01 +0200388 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 Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
Jens Axboee37f3462006-07-18 21:06:01 +0200395 return as_choose_req(ad, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
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 */
407static 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 */
424static 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 */
443static 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 */
458static 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 */
474static 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 Pigginf5b3db02005-11-07 00:59:53 -0800489 /* process anticipated on has exited or timed out*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 ad->exit_prob = (7*ad->exit_prob + 256)/8;
491 }
Nick Pigginf5b3db02005-11-07 00:59:53 -0800492 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 Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 spin_unlock_irqrestore(q->queue_lock, flags);
498}
499
Nick Pigginf5b3db02005-11-07 00:59:53 -0800500static 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
515static 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 */
546static 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 Torvalds1da177e2005-04-16 15:20:36 -0700584/*
585 * as_close_req decides if one request is considered "close" to the
586 * previous one issued.
587 */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800588static int as_close_req(struct as_data *ad, struct as_io_context *aic,
589 struct as_rq *arq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
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 Pigginf5b3db02005-11-07 00:59:53 -0800595 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
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 Pigginf5b3db02005-11-07 00:59:53 -0800602 if (delay == 0)
603 delta = 8192;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 else if (delay <= 20 && delay <= ad->antic_expire)
Nick Pigginf5b3db02005-11-07 00:59:53 -0800605 delta = 8192 << delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 else
607 return 1;
608
Nick Pigginf5b3db02005-11-07 00:59:53 -0800609 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 Torvalds1da177e2005-04-16 15:20:36 -0700635}
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 Pigginf5b3db02005-11-07 00:59:53 -0800646 * If the task which has submitted the request has exited, break anticipation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 *
648 * If this task has queued some other IO, do not enter enticipation.
649 */
650static 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 Torvalds1da177e2005-04-16 15:20:36 -0700654
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 Torvalds1da177e2005-04-16 15:20:36 -0700675 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 Pigginf5b3db02005-11-07 00:59:53 -0800685 if (arq && arq->is_sync == REQ_SYNC && as_close_req(ad, aic, arq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /*
687 * Found a close request that is not one of ours.
688 *
Nick Pigginf5b3db02005-11-07 00:59:53 -0800689 * This makes close requests from another process update
690 * our IO history. Is generally useful when there are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * two or more cooperating processes working in the same
692 * area.
693 */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800694 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 Torvalds1da177e2005-04-16 15:20:36 -0700702 return 1;
703 }
704
Nick Pigginf5b3db02005-11-07 00:59:53 -0800705 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 Torvalds1da177e2005-04-16 15:20:36 -0700713
714 if (aic->ttime_samples == 0) {
715 if (ad->new_ttime_mean > ad->antic_expire)
716 return 1;
Nick Pigginf5b3db02005-11-07 00:59:53 -0800717 if (ad->exit_prob * ad->exit_no_coop > 128*256)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 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 Torvalds1da177e2005-04-16 15:20:36 -0700724 return 0;
725}
726
727/*
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200728 * as_can_anticipate indicates whether we should either run arq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * or keep anticipating a better request.
730 */
731static 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 Torvalds1da177e2005-04-16 15:20:36 -0700757 */
758
759 return 1;
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/*
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 */
767static 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 */
789static 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 */
818static 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 Torvalds1da177e2005-04-16 15:20:36 -0700825 if (arq->state != AS_RQ_REMOVED) {
826 printk("arq->state %d\n", arq->state);
827 WARN_ON(1);
828 goto out;
829 }
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 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 Axboeb4878f22005-10-20 16:42:29 +0200865 as_put_io_context(arq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866out:
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 */
876static 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 Axboee37f3462006-07-18 21:06:01 +0200897 as_del_arq_rb(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 * 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 */
908static 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 */
933static 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 */
949static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq)
950{
951 struct request *rq = arq->request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 const int data_dir = arq->is_sync;
953
Jens Axboee37f3462006-07-18 21:06:01 +0200954 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
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 Torvalds1da177e2005-04-16 15:20:36 -0700984 as_remove_queued_request(ad->q, rq);
985 WARN_ON(arq->state != AS_RQ_QUEUED);
986
Jens Axboeb4878f22005-10-20 16:42:29 +0200987 elv_dispatch_sort(ad->q, rq);
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 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 Axboeb4878f22005-10-20 16:42:29 +02001000static int as_dispatch_request(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Jens Axboeb4878f22005-10-20 16:42:29 +02001002 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 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 Axboeb4878f22005-10-20 16:42:29 +02001007 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 Torvalds1da177e2005-04-16 15:20:36 -07001036 /* 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 Pigginf5b3db02005-11-07 00:59:53 -08001048 if (!(reads && writes && as_batch_expired(ad))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /*
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 Axboedd67d052006-06-21 09:36:18 +02001079 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_SYNC]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
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) {
1102dispatch_writes:
Jens Axboedd67d052006-06-21 09:36:18 +02001103 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_ASYNC]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
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
1125dispatch_request:
1126 /*
1127 * If a request has expired, service it.
1128 */
1129
1130 if (as_fifo_expired(ad, ad->batch_data_dir)) {
1131fifo_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 Torvalds1da177e2005-04-16 15:20:36 -07001159/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 * add arq to rbtree and fifo
1161 */
Jens Axboeb4878f22005-10-20 16:42:29 +02001162static void as_add_request(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Jens Axboeb4878f22005-10-20 16:42:29 +02001164 struct as_data *ad = q->elevator->elevator_data;
1165 struct as_rq *arq = RQ_DATA(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 int data_dir;
1167
Jens Axboeb4878f22005-10-20 16:42:29 +02001168 arq->state = AS_RQ_NEW;
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (rq_data_dir(arq->request) == READ
Jens Axboe4aff5e22006-08-10 08:44:47 +02001171 || (arq->request->cmd_flags & REQ_RW_SYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 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 Axboee37f3462006-07-18 21:06:01 +02001184 as_add_arq_rb(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Tejun Heoef9be1d2005-11-11 14:27:09 +01001186 /*
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 Torvalds1da177e2005-04-16 15:20:36 -07001191
Tejun Heoef9be1d2005-11-11 14:27:09 +01001192 as_update_arq(ad, arq); /* keep state machine up to date */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 arq->state = AS_RQ_QUEUED;
1194}
1195
Jens Axboeb4878f22005-10-20 16:42:29 +02001196static 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 Torvalds1da177e2005-04-16 15:20:36 -07001206static void as_deactivate_request(request_queue_t *q, struct request *rq)
1207{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 struct as_rq *arq = RQ_DATA(rq);
1209
Jens Axboeb4878f22005-10-20 16:42:29 +02001210 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 Torvalds1da177e2005-04-16 15:20:36 -07001214}
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 */
1222static int as_queue_empty(request_queue_t *q)
1223{
1224 struct as_data *ad = q->elevator->elevator_data;
1225
Jens Axboeb4878f22005-10-20 16:42:29 +02001226 return list_empty(&ad->fifo_list[REQ_ASYNC])
1227 && list_empty(&ad->fifo_list[REQ_SYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230static int
1231as_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 Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 /*
1238 * check for front merge
1239 */
Jens Axboee37f3462006-07-18 21:06:01 +02001240 __rq = elv_rb_find(&ad->sort_list[bio_data_dir(bio)], rb_key);
Jens Axboe98170642006-07-28 09:23:08 +02001241 if (__rq && elv_rq_merge_ok(__rq, bio)) {
1242 *req = __rq;
1243 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245
1246 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Jens Axboee37f3462006-07-18 21:06:01 +02001249static void as_merged_request(request_queue_t *q, struct request *req, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 * if the merge was a front merge, we need to reposition request
1255 */
Jens Axboee37f3462006-07-18 21:06:01 +02001256 if (type == ELEVATOR_FRONT_MERGE) {
1257 as_del_arq_rb(ad, req);
1258 as_add_arq_rb(ad, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
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 Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Nick Pigginf5b3db02005-11-07 00:59:53 -08001267static void as_merged_requests(request_queue_t *q, struct request *req,
1268 struct request *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 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 Torvalds1da177e2005-04-16 15:20:36 -07001276 /*
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 Torvalds1da177e2005-04-16 15:20:36 -07001293 * kill knowledge of next, this one is a goner
1294 */
1295 as_remove_queued_request(q, next);
Jens Axboeb4878f22005-10-20 16:42:29 +02001296 as_put_io_context(anext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
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 */
1310static 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 Axboeb4878f22005-10-20 16:42:29 +02001316 if (!as_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 q->request_fn(q);
1318 spin_unlock_irqrestore(q->queue_lock, flags);
1319}
1320
1321static 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 Axboeb4878f22005-10-20 16:42:29 +02001331 if (unlikely(arq->state != AS_RQ_POSTSCHED &&
1332 arq->state != AS_RQ_PRESCHED &&
1333 arq->state != AS_RQ_MERGED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 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 Axboe22e2c502005-06-27 10:55:12 +02001342static int as_set_request(request_queue_t *q, struct request *rq,
Al Viro8267e262005-10-21 03:20:53 -04001343 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
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 Torvalds1da177e2005-04-16 15:20:36 -07001350 arq->request = rq;
1351 arq->state = AS_RQ_PRESCHED;
1352 arq->io_context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 INIT_LIST_HEAD(&arq->fifo);
1354 rq->elevator_private = arq;
1355 return 0;
1356 }
1357
1358 return 1;
1359}
1360
Jens Axboe22e2c502005-06-27 10:55:12 +02001361static int as_may_queue(request_queue_t *q, int rw, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
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
1377static 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 Torvalds1da177e2005-04-16 15:20:36 -07001389 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 Axboebc1c1162006-06-08 08:49:06 +02001396static void *as_init_queue(request_queue_t *q, elevator_t *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 struct as_data *ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 if (!arq_pool)
Jens Axboebc1c1162006-06-08 08:49:06 +02001401 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Christoph Lameter19460892005-06-23 00:08:19 -07001403 ad = kmalloc_node(sizeof(*ad), GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (!ad)
Jens Axboebc1c1162006-06-08 08:49:06 +02001405 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 memset(ad, 0, sizeof(*ad));
1407
1408 ad->q = q; /* Identify what queue the data belongs to */
1409
Christoph Lameter19460892005-06-23 00:08:19 -07001410 ad->arq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
1411 mempool_free_slab, arq_pool, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (!ad->arq_pool) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 kfree(ad);
Jens Axboebc1c1162006-06-08 08:49:06 +02001414 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
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 Torvalds1da177e2005-04-16 15:20:36 -07001423 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 Torvalds1da177e2005-04-16 15:20:36 -07001427 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 Torvalds1da177e2005-04-16 15:20:36 -07001432
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 Axboebc1c1162006-06-08 08:49:06 +02001438 return ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
1441/*
1442 * sysfs parts below
1443 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445static ssize_t
1446as_var_show(unsigned int var, char *page)
1447{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return sprintf(page, "%d\n", var);
1449}
1450
1451static ssize_t
1452as_var_store(unsigned long *var, const char *page, size_t count)
1453{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 char *p = (char *) page;
1455
Jens Axboec9b3ad62005-07-27 11:43:37 -07001456 *var = simple_strtoul(p, &p, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return count;
1458}
1459
Al Viroe572ec72006-03-18 22:27:18 -05001460static ssize_t est_time_show(elevator_t *e, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Al Viro3d1ab402006-03-18 18:35:43 -05001462 struct as_data *ad = e->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 int pos = 0;
1464
Nick Pigginf5b3db02005-11-07 00:59:53 -08001465 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 Torvalds1da177e2005-04-16 15:20:36 -07001470 pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
Nick Pigginf5b3db02005-11-07 00:59:53 -08001471 pos += sprintf(page+pos, "%llu sectors new seek distance\n",
1472 (unsigned long long)ad->new_seek_mean);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 return pos;
1475}
1476
1477#define SHOW_FUNCTION(__FUNC, __VAR) \
Al Viro3d1ab402006-03-18 18:35:43 -05001478static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{ \
Al Viro3d1ab402006-03-18 18:35:43 -05001480 struct as_data *ad = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1482}
Al Viroe572ec72006-03-18 22:27:18 -05001483SHOW_FUNCTION(as_read_expire_show, ad->fifo_expire[REQ_SYNC]);
1484SHOW_FUNCTION(as_write_expire_show, ad->fifo_expire[REQ_ASYNC]);
1485SHOW_FUNCTION(as_antic_expire_show, ad->antic_expire);
1486SHOW_FUNCTION(as_read_batch_expire_show, ad->batch_expire[REQ_SYNC]);
1487SHOW_FUNCTION(as_write_batch_expire_show, ad->batch_expire[REQ_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488#undef SHOW_FUNCTION
1489
1490#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
Al Viro3d1ab402006-03-18 18:35:43 -05001491static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{ \
Al Viro3d1ab402006-03-18 18:35:43 -05001493 struct as_data *ad = e->elevator_data; \
1494 int ret = as_var_store(__PTR, (page), count); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 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 Viroe572ec72006-03-18 22:27:18 -05001502STORE_FUNCTION(as_read_expire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
1503STORE_FUNCTION(as_write_expire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
1504STORE_FUNCTION(as_antic_expire_store, &ad->antic_expire, 0, INT_MAX);
1505STORE_FUNCTION(as_read_batch_expire_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
Al Viroe572ec72006-03-18 22:27:18 -05001507STORE_FUNCTION(as_write_batch_expire_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
1509#undef STORE_FUNCTION
1510
Al Viroe572ec72006-03-18 22:27:18 -05001511#define AS_ATTR(name) \
1512 __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Al Viroe572ec72006-03-18 22:27:18 -05001514static 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 Torvalds1da177e2005-04-16 15:20:36 -07001522};
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524static 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 Axboeb4878f22005-10-20 16:42:29 +02001529 .elevator_dispatch_fn = as_dispatch_request,
1530 .elevator_add_req_fn = as_add_request,
1531 .elevator_activate_req_fn = as_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 .elevator_deactivate_req_fn = as_deactivate_request,
1533 .elevator_queue_empty_fn = as_queue_empty,
1534 .elevator_completed_req_fn = as_completed_request,
Jens Axboee37f3462006-07-18 21:06:01 +02001535 .elevator_former_req_fn = elv_rb_former_request,
1536 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 .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 Viroe17a9482006-03-18 13:21:20 -05001542 .trim = as_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 },
1544
Al Viro3d1ab402006-03-18 18:35:43 -05001545 .elevator_attrs = as_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 .elevator_name = "anticipatory",
1547 .elevator_owner = THIS_MODULE,
1548};
1549
1550static 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
1574static void __exit as_exit(void)
1575{
Al Viro334e94d2006-03-18 15:05:53 -05001576 DECLARE_COMPLETION(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 elv_unregister(&iosched_as);
Al Viro334e94d2006-03-18 15:05:53 -05001578 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02001579 /* ioc_gone's update must be visible before reading ioc_count */
1580 smp_wmb();
Al Viro334e94d2006-03-18 15:05:53 -05001581 if (atomic_read(&ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02001582 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05001583 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08001584 kmem_cache_destroy(arq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
1587module_init(as_init);
1588module_exit(as_exit);
1589
1590MODULE_AUTHOR("Nick Piggin");
1591MODULE_LICENSE("GPL");
1592MODULE_DESCRIPTION("anticipatory IO scheduler");