2 * Anticipatory & deadline i/o scheduler.
4 * Copyright (C) 2002 Jens Axboe <axboe@kernel.dk>
5 * Nick Piggin <nickpiggin@yahoo.com.au>
8 #include <linux/kernel.h>
10 #include <linux/blkdev.h>
11 #include <linux/elevator.h>
12 #include <linux/bio.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/compiler.h>
17 #include <linux/rbtree.h>
18 #include <linux/interrupt.h>
24 * See Documentation/block/as-iosched.txt
28 * max time before a read is submitted.
30 #define default_read_expire (HZ / 8)
33 * ditto for writes, these limits are not hard, even
34 * if the disk is capable of satisfying them.
36 #define default_write_expire (HZ / 4)
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.
42 #define default_read_batch_expire (HZ / 2)
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...
50 #define default_write_batch_expire (HZ / 8)
53 * max time we may wait to anticipate a read (default around 6ms)
55 #define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
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.
64 #define MAX_THINKTIME (HZ/50UL)
66 /* Bits in as_io_context.state */
68 AS_TASK_RUNNING
=0, /* Process has not exited */
69 AS_TASK_IOSTARTED
, /* Process has started some IO */
70 AS_TASK_IORUNNING
, /* Process has completed some IO */
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
87 struct request_queue
*q
; /* the "owner" queue */
90 * requests (as_rq s) are present on both sort_list and fifo_list
92 struct rb_root sort_list
[2];
93 struct list_head fifo_list
[2];
95 struct request
*next_rq
[2]; /* next in sort order */
96 sector_t last_sector
[2]; /* last REQ_SYNC & REQ_ASYNC sectors */
98 unsigned long exit_prob
; /* probability a task will exit while
100 unsigned long exit_no_coop
; /* probablility an exited task will
101 not be part of a later cooperating
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
;
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? */
117 enum anticipation_status antic_status
;
118 unsigned long antic_start
; /* jiffies: when it started */
119 struct timer_list antic_timer
; /* anticipatory scheduling timer */
120 struct work_struct antic_work
; /* Deferred unplugging */
121 struct io_context
*io_context
; /* Identify the expected process */
122 int ioc_finished
; /* IO associated with io_context is finished */
126 * settings that change how the i/o scheduler behaves
128 unsigned long fifo_expire
[2];
129 unsigned long batch_expire
[2];
130 unsigned long antic_expire
;
137 AS_RQ_NEW
=0, /* New - not referenced and not on any lists */
138 AS_RQ_QUEUED
, /* In the request queue. It belongs to the
140 AS_RQ_DISPATCHED
, /* On the dispatch list. It belongs to the
142 AS_RQ_PRESCHED
, /* Debug poisoning for requests being used */
145 AS_RQ_POSTSCHED
, /* when they shouldn't be */
148 #define RQ_IOC(rq) ((struct io_context *) (rq)->elevator_private)
149 #define RQ_STATE(rq) ((enum arq_state)(rq)->elevator_private2)
150 #define RQ_SET_STATE(rq, state) ((rq)->elevator_private2 = (void *) state)
152 static DEFINE_PER_CPU(unsigned long, ioc_count
);
153 static struct completion
*ioc_gone
;
155 static void as_move_to_dispatch(struct as_data
*ad
, struct request
*rq
);
156 static void as_antic_stop(struct as_data
*ad
);
159 * IO Context helper functions
162 /* Called to deallocate the as_io_context */
163 static void free_as_io_context(struct as_io_context
*aic
)
166 elv_ioc_count_dec(ioc_count
);
167 if (ioc_gone
&& !elv_ioc_count_read(ioc_count
))
171 static void as_trim(struct io_context
*ioc
)
174 free_as_io_context(ioc
->aic
);
178 /* Called when the task exits */
179 static void exit_as_io_context(struct as_io_context
*aic
)
181 WARN_ON(!test_bit(AS_TASK_RUNNING
, &aic
->state
));
182 clear_bit(AS_TASK_RUNNING
, &aic
->state
);
185 static struct as_io_context
*alloc_as_io_context(void)
187 struct as_io_context
*ret
;
189 ret
= kmalloc(sizeof(*ret
), GFP_ATOMIC
);
191 ret
->dtor
= free_as_io_context
;
192 ret
->exit
= exit_as_io_context
;
193 ret
->state
= 1 << AS_TASK_RUNNING
;
194 atomic_set(&ret
->nr_queued
, 0);
195 atomic_set(&ret
->nr_dispatched
, 0);
196 spin_lock_init(&ret
->lock
);
197 ret
->ttime_total
= 0;
198 ret
->ttime_samples
= 0;
201 ret
->seek_samples
= 0;
203 elv_ioc_count_inc(ioc_count
);
210 * If the current task has no AS IO context then create one and initialise it.
211 * Then take a ref on the task's io context and return it.
213 static struct io_context
*as_get_io_context(int node
)
215 struct io_context
*ioc
= get_io_context(GFP_ATOMIC
, node
);
216 if (ioc
&& !ioc
->aic
) {
217 ioc
->aic
= alloc_as_io_context();
226 static void as_put_io_context(struct request
*rq
)
228 struct as_io_context
*aic
;
230 if (unlikely(!RQ_IOC(rq
)))
233 aic
= RQ_IOC(rq
)->aic
;
235 if (rq_is_sync(rq
) && aic
) {
236 spin_lock(&aic
->lock
);
237 set_bit(AS_TASK_IORUNNING
, &aic
->state
);
238 aic
->last_end_request
= jiffies
;
239 spin_unlock(&aic
->lock
);
242 put_io_context(RQ_IOC(rq
));
246 * rb tree support functions
248 #define RQ_RB_ROOT(ad, rq) (&(ad)->sort_list[rq_is_sync((rq))])
250 static void as_add_rq_rb(struct as_data
*ad
, struct request
*rq
)
252 struct request
*alias
;
254 while ((unlikely(alias
= elv_rb_add(RQ_RB_ROOT(ad
, rq
), rq
)))) {
255 as_move_to_dispatch(ad
, alias
);
260 static inline void as_del_rq_rb(struct as_data
*ad
, struct request
*rq
)
262 elv_rb_del(RQ_RB_ROOT(ad
, rq
), rq
);
266 * IO Scheduler proper
269 #define MAXBACK (1024 * 1024) /*
270 * Maximum distance the disk will go backward
274 #define BACK_PENALTY 2
277 * as_choose_req selects the preferred one of two requests of the same data_dir
278 * ignoring time - eg. timeouts, which is the job of as_dispatch_request
280 static struct request
*
281 as_choose_req(struct as_data
*ad
, struct request
*rq1
, struct request
*rq2
)
284 sector_t last
, s1
, s2
, d1
, d2
;
285 int r1_wrap
=0, r2_wrap
=0; /* requests are behind the disk head */
286 const sector_t maxback
= MAXBACK
;
288 if (rq1
== NULL
|| rq1
== rq2
)
293 data_dir
= rq_is_sync(rq1
);
295 last
= ad
->last_sector
[data_dir
];
299 BUG_ON(data_dir
!= rq_is_sync(rq2
));
302 * Strict one way elevator _except_ in the case where we allow
303 * short backward seeks which are biased as twice the cost of a
304 * similar forward seek.
308 else if (s1
+maxback
>= last
)
309 d1
= (last
- s1
)*BACK_PENALTY
;
312 d1
= 0; /* shut up, gcc */
317 else if (s2
+maxback
>= last
)
318 d2
= (last
- s2
)*BACK_PENALTY
;
324 /* Found required data */
325 if (!r1_wrap
&& r2_wrap
)
327 else if (!r2_wrap
&& r1_wrap
)
329 else if (r1_wrap
&& r2_wrap
) {
330 /* both behind the head */
337 /* Both requests in front of the head */
351 * as_find_next_rq finds the next request after @prev in elevator order.
352 * this with as_choose_req form the basis for how the scheduler chooses
353 * what request to process next. Anticipation works on top of this.
355 static struct request
*
356 as_find_next_rq(struct as_data
*ad
, struct request
*last
)
358 #if 0 // mask by Victor Yu. 02-12-2007
359 struct rb_node
*rbnext
= rb_next(&last
->rb_node
);
360 struct rb_node
*rbprev
= rb_prev(&last
->rb_node
);
362 struct rb_node
*rbnext
= rb_next(&last
->u
.rb_node
);
363 struct rb_node
*rbprev
= rb_prev(&last
->u
.rb_node
);
365 struct request
*next
= NULL
, *prev
= NULL
;
367 #if 0 // mask by Victor Yu. 02-12-2007
368 BUG_ON(RB_EMPTY_NODE(&last
->rb_node
));
370 BUG_ON(RB_EMPTY_NODE(&last
->u
.rb_node
));
374 prev
= rb_entry_rq(rbprev
);
377 next
= rb_entry_rq(rbnext
);
379 const int data_dir
= rq_is_sync(last
);
381 rbnext
= rb_first(&ad
->sort_list
[data_dir
]);
382 #if 0 // mask by Victor Yu. 02-12-2007
383 if (rbnext
&& rbnext
!= &last
->rb_node
)
385 if (rbnext
&& rbnext
!= &last
->u
.rb_node
)
387 next
= rb_entry_rq(rbnext
);
390 return as_choose_req(ad
, next
, prev
);
394 * anticipatory scheduling functions follow
398 * as_antic_expired tells us when we have anticipated too long.
399 * The funny "absolute difference" math on the elapsed time is to handle
400 * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
402 static int as_antic_expired(struct as_data
*ad
)
406 delta_jif
= jiffies
- ad
->antic_start
;
407 if (unlikely(delta_jif
< 0))
408 delta_jif
= -delta_jif
;
409 if (delta_jif
< ad
->antic_expire
)
416 * as_antic_waitnext starts anticipating that a nice request will soon be
417 * submitted. See also as_antic_waitreq
419 static void as_antic_waitnext(struct as_data
*ad
)
421 unsigned long timeout
;
423 BUG_ON(ad
->antic_status
!= ANTIC_OFF
424 && ad
->antic_status
!= ANTIC_WAIT_REQ
);
426 timeout
= ad
->antic_start
+ ad
->antic_expire
;
428 mod_timer(&ad
->antic_timer
, timeout
);
430 ad
->antic_status
= ANTIC_WAIT_NEXT
;
434 * as_antic_waitreq starts anticipating. We don't start timing the anticipation
435 * until the request that we're anticipating on has finished. This means we
436 * are timing from when the candidate process wakes up hopefully.
438 static void as_antic_waitreq(struct as_data
*ad
)
440 BUG_ON(ad
->antic_status
== ANTIC_FINISHED
);
441 if (ad
->antic_status
== ANTIC_OFF
) {
442 if (!ad
->io_context
|| ad
->ioc_finished
)
443 as_antic_waitnext(ad
);
445 ad
->antic_status
= ANTIC_WAIT_REQ
;
450 * This is called directly by the functions in this file to stop anticipation.
451 * We kill the timer and schedule a call to the request_fn asap.
453 static void as_antic_stop(struct as_data
*ad
)
455 int status
= ad
->antic_status
;
457 if (status
== ANTIC_WAIT_REQ
|| status
== ANTIC_WAIT_NEXT
) {
458 if (status
== ANTIC_WAIT_NEXT
)
459 del_timer(&ad
->antic_timer
);
460 ad
->antic_status
= ANTIC_FINISHED
;
461 /* see as_work_handler */
462 kblockd_schedule_work(&ad
->antic_work
);
467 * as_antic_timeout is the timer function set by as_antic_waitnext.
469 static void as_antic_timeout(unsigned long data
)
471 struct request_queue
*q
= (struct request_queue
*)data
;
472 struct as_data
*ad
= q
->elevator
->elevator_data
;
475 spin_lock_irqsave(q
->queue_lock
, flags
);
476 if (ad
->antic_status
== ANTIC_WAIT_REQ
477 || ad
->antic_status
== ANTIC_WAIT_NEXT
) {
478 struct as_io_context
*aic
= ad
->io_context
->aic
;
480 ad
->antic_status
= ANTIC_FINISHED
;
481 kblockd_schedule_work(&ad
->antic_work
);
483 if (aic
->ttime_samples
== 0) {
484 /* process anticipated on has exited or timed out*/
485 ad
->exit_prob
= (7*ad
->exit_prob
+ 256)/8;
487 if (!test_bit(AS_TASK_RUNNING
, &aic
->state
)) {
488 /* process not "saved" by a cooperating request */
489 ad
->exit_no_coop
= (7*ad
->exit_no_coop
+ 256)/8;
492 spin_unlock_irqrestore(q
->queue_lock
, flags
);
495 static void as_update_thinktime(struct as_data
*ad
, struct as_io_context
*aic
,
498 /* fixed point: 1.0 == 1<<8 */
499 if (aic
->ttime_samples
== 0) {
500 ad
->new_ttime_total
= (7*ad
->new_ttime_total
+ 256*ttime
) / 8;
501 ad
->new_ttime_mean
= ad
->new_ttime_total
/ 256;
503 ad
->exit_prob
= (7*ad
->exit_prob
)/8;
505 aic
->ttime_samples
= (7*aic
->ttime_samples
+ 256) / 8;
506 aic
->ttime_total
= (7*aic
->ttime_total
+ 256*ttime
) / 8;
507 aic
->ttime_mean
= (aic
->ttime_total
+ 128) / aic
->ttime_samples
;
510 static void as_update_seekdist(struct as_data
*ad
, struct as_io_context
*aic
,
515 if (aic
->seek_samples
== 0) {
516 ad
->new_seek_total
= (7*ad
->new_seek_total
+ 256*(u64
)sdist
)/8;
517 ad
->new_seek_mean
= ad
->new_seek_total
/ 256;
521 * Don't allow the seek distance to get too large from the
522 * odd fragment, pagein, etc
524 if (aic
->seek_samples
<= 60) /* second&third seek */
525 sdist
= min(sdist
, (aic
->seek_mean
* 4) + 2*1024*1024);
527 sdist
= min(sdist
, (aic
->seek_mean
* 4) + 2*1024*64);
529 aic
->seek_samples
= (7*aic
->seek_samples
+ 256) / 8;
530 aic
->seek_total
= (7*aic
->seek_total
+ (u64
)256*sdist
) / 8;
531 total
= aic
->seek_total
+ (aic
->seek_samples
/2);
532 do_div(total
, aic
->seek_samples
);
533 aic
->seek_mean
= (sector_t
)total
;
537 * as_update_iohist keeps a decaying histogram of IO thinktimes, and
538 * updates @aic->ttime_mean based on that. It is called when a new
541 static void as_update_iohist(struct as_data
*ad
, struct as_io_context
*aic
,
544 int data_dir
= rq_is_sync(rq
);
545 unsigned long thinktime
= 0;
551 if (data_dir
== REQ_SYNC
) {
552 unsigned long in_flight
= atomic_read(&aic
->nr_queued
)
553 + atomic_read(&aic
->nr_dispatched
);
554 spin_lock(&aic
->lock
);
555 if (test_bit(AS_TASK_IORUNNING
, &aic
->state
) ||
556 test_bit(AS_TASK_IOSTARTED
, &aic
->state
)) {
557 /* Calculate read -> read thinktime */
558 if (test_bit(AS_TASK_IORUNNING
, &aic
->state
)
560 thinktime
= jiffies
- aic
->last_end_request
;
561 thinktime
= min(thinktime
, MAX_THINKTIME
-1);
563 as_update_thinktime(ad
, aic
, thinktime
);
565 /* Calculate read -> read seek distance */
566 if (aic
->last_request_pos
< rq
->sector
)
567 seek_dist
= rq
->sector
- aic
->last_request_pos
;
569 seek_dist
= aic
->last_request_pos
- rq
->sector
;
570 as_update_seekdist(ad
, aic
, seek_dist
);
572 aic
->last_request_pos
= rq
->sector
+ rq
->nr_sectors
;
573 set_bit(AS_TASK_IOSTARTED
, &aic
->state
);
574 spin_unlock(&aic
->lock
);
579 * as_close_req decides if one request is considered "close" to the
580 * previous one issued.
582 static int as_close_req(struct as_data
*ad
, struct as_io_context
*aic
,
585 unsigned long delay
; /* milliseconds */
586 sector_t last
= ad
->last_sector
[ad
->batch_data_dir
];
587 sector_t next
= rq
->sector
;
588 sector_t delta
; /* acceptable close offset (in sectors) */
591 if (ad
->antic_status
== ANTIC_OFF
|| !ad
->ioc_finished
)
594 delay
= ((jiffies
- ad
->antic_start
) * 1000) / HZ
;
598 else if (delay
<= 20 && delay
<= ad
->antic_expire
)
599 delta
= 8192 << delay
;
603 if ((last
<= next
+ (delta
>>1)) && (next
<= last
+ delta
))
611 if (aic
->seek_samples
== 0) {
613 * Process has just started IO. Use past statistics to
614 * gauge success possibility
616 if (ad
->new_seek_mean
> s
) {
617 /* this request is better than what we're expecting */
622 if (aic
->seek_mean
> s
) {
623 /* this request is better than what we're expecting */
632 * as_can_break_anticipation returns true if we have been anticipating this
635 * It also returns true if the process against which we are anticipating
636 * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
637 * dispatch it ASAP, because we know that application will not be submitting
640 * If the task which has submitted the request has exited, break anticipation.
642 * If this task has queued some other IO, do not enter enticipation.
644 static int as_can_break_anticipation(struct as_data
*ad
, struct request
*rq
)
646 struct io_context
*ioc
;
647 struct as_io_context
*aic
;
649 ioc
= ad
->io_context
;
652 if (rq
&& ioc
== RQ_IOC(rq
)) {
653 /* request from same process */
657 if (ad
->ioc_finished
&& as_antic_expired(ad
)) {
659 * In this situation status should really be FINISHED,
660 * however the timer hasn't had the chance to run yet.
669 if (atomic_read(&aic
->nr_queued
) > 0) {
670 /* process has more requests queued */
674 if (atomic_read(&aic
->nr_dispatched
) > 0) {
675 /* process has more requests dispatched */
679 if (rq
&& rq_is_sync(rq
) && as_close_req(ad
, aic
, rq
)) {
681 * Found a close request that is not one of ours.
683 * This makes close requests from another process update
684 * our IO history. Is generally useful when there are
685 * two or more cooperating processes working in the same
688 if (!test_bit(AS_TASK_RUNNING
, &aic
->state
)) {
689 if (aic
->ttime_samples
== 0)
690 ad
->exit_prob
= (7*ad
->exit_prob
+ 256)/8;
692 ad
->exit_no_coop
= (7*ad
->exit_no_coop
)/8;
695 as_update_iohist(ad
, aic
, rq
);
699 if (!test_bit(AS_TASK_RUNNING
, &aic
->state
)) {
700 /* process anticipated on has exited */
701 if (aic
->ttime_samples
== 0)
702 ad
->exit_prob
= (7*ad
->exit_prob
+ 256)/8;
704 if (ad
->exit_no_coop
> 128)
708 if (aic
->ttime_samples
== 0) {
709 if (ad
->new_ttime_mean
> ad
->antic_expire
)
711 if (ad
->exit_prob
* ad
->exit_no_coop
> 128*256)
713 } else if (aic
->ttime_mean
> ad
->antic_expire
) {
714 /* the process thinks too much between requests */
722 * as_can_anticipate indicates whether we should either run rq
723 * or keep anticipating a better request.
725 static int as_can_anticipate(struct as_data
*ad
, struct request
*rq
)
729 * Last request submitted was a write
733 if (ad
->antic_status
== ANTIC_FINISHED
)
735 * Don't restart if we have just finished. Run the next request
739 if (as_can_break_anticipation(ad
, rq
))
741 * This request is a good candidate. Don't keep anticipating,
747 * OK from here, we haven't finished, and don't have a decent request!
748 * Status is either ANTIC_OFF so start waiting,
749 * ANTIC_WAIT_REQ so continue waiting for request to finish
750 * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
757 * as_update_rq must be called whenever a request (rq) is added to
758 * the sort_list. This function keeps caches up to date, and checks if the
759 * request might be one we are "anticipating"
761 static void as_update_rq(struct as_data
*ad
, struct request
*rq
)
763 const int data_dir
= rq_is_sync(rq
);
765 /* keep the next_rq cache up to date */
766 ad
->next_rq
[data_dir
] = as_choose_req(ad
, rq
, ad
->next_rq
[data_dir
]);
769 * have we been anticipating this request?
770 * or does it come from the same process as the one we are anticipating
773 if (ad
->antic_status
== ANTIC_WAIT_REQ
774 || ad
->antic_status
== ANTIC_WAIT_NEXT
) {
775 if (as_can_break_anticipation(ad
, rq
))
781 * Gathers timings and resizes the write batch automatically
783 static void update_write_batch(struct as_data
*ad
)
785 unsigned long batch
= ad
->batch_expire
[REQ_ASYNC
];
788 write_time
= (jiffies
- ad
->current_batch_expires
) + batch
;
792 if (write_time
> batch
&& !ad
->write_batch_idled
) {
793 if (write_time
> batch
* 3)
794 ad
->write_batch_count
/= 2;
796 ad
->write_batch_count
--;
797 } else if (write_time
< batch
&& ad
->current_write_count
== 0) {
798 if (batch
> write_time
* 3)
799 ad
->write_batch_count
*= 2;
801 ad
->write_batch_count
++;
804 if (ad
->write_batch_count
< 1)
805 ad
->write_batch_count
= 1;
809 * as_completed_request is to be called when a request has completed and
810 * returned something to the requesting process, be it an error or data.
812 static void as_completed_request(request_queue_t
*q
, struct request
*rq
)
814 struct as_data
*ad
= q
->elevator
->elevator_data
;
816 WARN_ON(!list_empty(&rq
->queuelist
));
818 if (RQ_STATE(rq
) != AS_RQ_REMOVED
) {
819 printk("rq->state %d\n", RQ_STATE(rq
));
824 if (ad
->changed_batch
&& ad
->nr_dispatched
== 1) {
825 kblockd_schedule_work(&ad
->antic_work
);
826 ad
->changed_batch
= 0;
828 if (ad
->batch_data_dir
== REQ_SYNC
)
831 WARN_ON(ad
->nr_dispatched
== 0);
835 * Start counting the batch from when a request of that direction is
836 * actually serviced. This should help devices with big TCQ windows
837 * and writeback caches
839 if (ad
->new_batch
&& ad
->batch_data_dir
== rq_is_sync(rq
)) {
840 update_write_batch(ad
);
841 ad
->current_batch_expires
= jiffies
+
842 ad
->batch_expire
[REQ_SYNC
];
846 if (ad
->io_context
== RQ_IOC(rq
) && ad
->io_context
) {
847 ad
->antic_start
= jiffies
;
848 ad
->ioc_finished
= 1;
849 if (ad
->antic_status
== ANTIC_WAIT_REQ
) {
851 * We were waiting on this request, now anticipate
854 as_antic_waitnext(ad
);
858 as_put_io_context(rq
);
860 RQ_SET_STATE(rq
, AS_RQ_POSTSCHED
);
864 * as_remove_queued_request removes a request from the pre dispatch queue
865 * without updating refcounts. It is expected the caller will drop the
866 * reference unless it replaces the request at somepart of the elevator
867 * (ie. the dispatch queue)
869 static void as_remove_queued_request(request_queue_t
*q
, struct request
*rq
)
871 const int data_dir
= rq_is_sync(rq
);
872 struct as_data
*ad
= q
->elevator
->elevator_data
;
873 struct io_context
*ioc
;
875 WARN_ON(RQ_STATE(rq
) != AS_RQ_QUEUED
);
878 if (ioc
&& ioc
->aic
) {
879 BUG_ON(!atomic_read(&ioc
->aic
->nr_queued
));
880 atomic_dec(&ioc
->aic
->nr_queued
);
884 * Update the "next_rq" cache if we are about to remove its
887 if (ad
->next_rq
[data_dir
] == rq
)
888 ad
->next_rq
[data_dir
] = as_find_next_rq(ad
, rq
);
891 as_del_rq_rb(ad
, rq
);
895 * as_fifo_expired returns 0 if there are no expired reads on the fifo,
896 * 1 otherwise. It is ratelimited so that we only perform the check once per
897 * `fifo_expire' interval. Otherwise a large number of expired requests
898 * would create a hopeless seekstorm.
900 * See as_antic_expired comment.
902 static int as_fifo_expired(struct as_data
*ad
, int adir
)
907 delta_jif
= jiffies
- ad
->last_check_fifo
[adir
];
908 if (unlikely(delta_jif
< 0))
909 delta_jif
= -delta_jif
;
910 if (delta_jif
< ad
->fifo_expire
[adir
])
913 ad
->last_check_fifo
[adir
] = jiffies
;
915 if (list_empty(&ad
->fifo_list
[adir
]))
918 rq
= rq_entry_fifo(ad
->fifo_list
[adir
].next
);
920 return time_after(jiffies
, rq_fifo_time(rq
));
924 * as_batch_expired returns true if the current batch has expired. A batch
925 * is a set of reads or a set of writes.
927 static inline int as_batch_expired(struct as_data
*ad
)
929 if (ad
->changed_batch
|| ad
->new_batch
)
932 if (ad
->batch_data_dir
== REQ_SYNC
)
933 /* TODO! add a check so a complete fifo gets written? */
934 return time_after(jiffies
, ad
->current_batch_expires
);
936 return time_after(jiffies
, ad
->current_batch_expires
)
937 || ad
->current_write_count
== 0;
941 * move an entry to dispatch queue
943 static void as_move_to_dispatch(struct as_data
*ad
, struct request
*rq
)
945 const int data_dir
= rq_is_sync(rq
);
947 #if 0 // mask by Victor Yu. 02-12-2007
948 BUG_ON(RB_EMPTY_NODE(&rq
->rb_node
));
950 BUG_ON(RB_EMPTY_NODE(&rq
->u
.rb_node
));
954 ad
->antic_status
= ANTIC_OFF
;
957 * This has to be set in order to be correctly updated by
960 ad
->last_sector
[data_dir
] = rq
->sector
+ rq
->nr_sectors
;
962 if (data_dir
== REQ_SYNC
) {
963 struct io_context
*ioc
= RQ_IOC(rq
);
964 /* In case we have to anticipate after this */
965 copy_io_context(&ad
->io_context
, &ioc
);
967 if (ad
->io_context
) {
968 put_io_context(ad
->io_context
);
969 ad
->io_context
= NULL
;
972 if (ad
->current_write_count
!= 0)
973 ad
->current_write_count
--;
975 ad
->ioc_finished
= 0;
977 ad
->next_rq
[data_dir
] = as_find_next_rq(ad
, rq
);
980 * take it off the sort and fifo list, add to dispatch queue
982 as_remove_queued_request(ad
->q
, rq
);
983 WARN_ON(RQ_STATE(rq
) != AS_RQ_QUEUED
);
985 elv_dispatch_sort(ad
->q
, rq
);
987 RQ_SET_STATE(rq
, AS_RQ_DISPATCHED
);
988 if (RQ_IOC(rq
) && RQ_IOC(rq
)->aic
)
989 atomic_inc(&RQ_IOC(rq
)->aic
->nr_dispatched
);
994 * as_dispatch_request selects the best request according to
995 * read/write expire, batch expire, etc, and moves it to the dispatch
996 * queue. Returns 1 if a request was found, 0 otherwise.
998 static int as_dispatch_request(request_queue_t
*q
, int force
)
1000 struct as_data
*ad
= q
->elevator
->elevator_data
;
1001 const int reads
= !list_empty(&ad
->fifo_list
[REQ_SYNC
]);
1002 const int writes
= !list_empty(&ad
->fifo_list
[REQ_ASYNC
]);
1005 if (unlikely(force
)) {
1007 * Forced dispatch, accounting is useless. Reset
1008 * accounting states and dump fifo_lists. Note that
1009 * batch_data_dir is reset to REQ_SYNC to avoid
1010 * screwing write batch accounting as write batch
1011 * accounting occurs on W->R transition.
1015 ad
->batch_data_dir
= REQ_SYNC
;
1016 ad
->changed_batch
= 0;
1019 while (ad
->next_rq
[REQ_SYNC
]) {
1020 as_move_to_dispatch(ad
, ad
->next_rq
[REQ_SYNC
]);
1023 ad
->last_check_fifo
[REQ_SYNC
] = jiffies
;
1025 while (ad
->next_rq
[REQ_ASYNC
]) {
1026 as_move_to_dispatch(ad
, ad
->next_rq
[REQ_ASYNC
]);
1029 ad
->last_check_fifo
[REQ_ASYNC
] = jiffies
;
1034 /* Signal that the write batch was uncontended, so we can't time it */
1035 if (ad
->batch_data_dir
== REQ_ASYNC
&& !reads
) {
1036 if (ad
->current_write_count
== 0 || !writes
)
1037 ad
->write_batch_idled
= 1;
1040 if (!(reads
|| writes
)
1041 || ad
->antic_status
== ANTIC_WAIT_REQ
1042 || ad
->antic_status
== ANTIC_WAIT_NEXT
1043 || ad
->changed_batch
)
1046 if (!(reads
&& writes
&& as_batch_expired(ad
))) {
1048 * batch is still running or no reads or no writes
1050 rq
= ad
->next_rq
[ad
->batch_data_dir
];
1052 if (ad
->batch_data_dir
== REQ_SYNC
&& ad
->antic_expire
) {
1053 if (as_fifo_expired(ad
, REQ_SYNC
))
1056 if (as_can_anticipate(ad
, rq
)) {
1057 as_antic_waitreq(ad
);
1063 /* we have a "next request" */
1064 if (reads
&& !writes
)
1065 ad
->current_batch_expires
=
1066 jiffies
+ ad
->batch_expire
[REQ_SYNC
];
1067 goto dispatch_request
;
1072 * at this point we are not running a batch. select the appropriate
1073 * data direction (read / write)
1077 BUG_ON(RB_EMPTY_ROOT(&ad
->sort_list
[REQ_SYNC
]));
1079 if (writes
&& ad
->batch_data_dir
== REQ_SYNC
)
1081 * Last batch was a read, switch to writes
1083 goto dispatch_writes
;
1085 if (ad
->batch_data_dir
== REQ_ASYNC
) {
1086 WARN_ON(ad
->new_batch
);
1087 ad
->changed_batch
= 1;
1089 ad
->batch_data_dir
= REQ_SYNC
;
1090 rq
= rq_entry_fifo(ad
->fifo_list
[REQ_SYNC
].next
);
1091 ad
->last_check_fifo
[ad
->batch_data_dir
] = jiffies
;
1092 goto dispatch_request
;
1096 * the last batch was a read
1101 BUG_ON(RB_EMPTY_ROOT(&ad
->sort_list
[REQ_ASYNC
]));
1103 if (ad
->batch_data_dir
== REQ_SYNC
) {
1104 ad
->changed_batch
= 1;
1107 * new_batch might be 1 when the queue runs out of
1108 * reads. A subsequent submission of a write might
1109 * cause a change of batch before the read is finished.
1113 ad
->batch_data_dir
= REQ_ASYNC
;
1114 ad
->current_write_count
= ad
->write_batch_count
;
1115 ad
->write_batch_idled
= 0;
1116 rq
= ad
->next_rq
[ad
->batch_data_dir
];
1117 goto dispatch_request
;
1125 * If a request has expired, service it.
1128 if (as_fifo_expired(ad
, ad
->batch_data_dir
)) {
1130 rq
= rq_entry_fifo(ad
->fifo_list
[ad
->batch_data_dir
].next
);
1133 if (ad
->changed_batch
) {
1134 WARN_ON(ad
->new_batch
);
1136 if (ad
->nr_dispatched
)
1139 if (ad
->batch_data_dir
== REQ_ASYNC
)
1140 ad
->current_batch_expires
= jiffies
+
1141 ad
->batch_expire
[REQ_ASYNC
];
1145 ad
->changed_batch
= 0;
1149 * rq is the selected appropriate request.
1151 as_move_to_dispatch(ad
, rq
);
1157 * add rq to rbtree and fifo
1159 static void as_add_request(request_queue_t
*q
, struct request
*rq
)
1161 struct as_data
*ad
= q
->elevator
->elevator_data
;
1164 RQ_SET_STATE(rq
, AS_RQ_NEW
);
1166 data_dir
= rq_is_sync(rq
);
1168 rq
->elevator_private
= as_get_io_context(q
->node
);
1171 as_update_iohist(ad
, RQ_IOC(rq
)->aic
, rq
);
1172 atomic_inc(&RQ_IOC(rq
)->aic
->nr_queued
);
1175 as_add_rq_rb(ad
, rq
);
1178 * set expire time (only used for reads) and add to fifo list
1180 rq_set_fifo_time(rq
, jiffies
+ ad
->fifo_expire
[data_dir
]);
1181 list_add_tail(&rq
->queuelist
, &ad
->fifo_list
[data_dir
]);
1183 as_update_rq(ad
, rq
); /* keep state machine up to date */
1184 RQ_SET_STATE(rq
, AS_RQ_QUEUED
);
1187 static void as_activate_request(request_queue_t
*q
, struct request
*rq
)
1189 WARN_ON(RQ_STATE(rq
) != AS_RQ_DISPATCHED
);
1190 RQ_SET_STATE(rq
, AS_RQ_REMOVED
);
1191 if (RQ_IOC(rq
) && RQ_IOC(rq
)->aic
)
1192 atomic_dec(&RQ_IOC(rq
)->aic
->nr_dispatched
);
1195 static void as_deactivate_request(request_queue_t
*q
, struct request
*rq
)
1197 WARN_ON(RQ_STATE(rq
) != AS_RQ_REMOVED
);
1198 RQ_SET_STATE(rq
, AS_RQ_DISPATCHED
);
1199 if (RQ_IOC(rq
) && RQ_IOC(rq
)->aic
)
1200 atomic_inc(&RQ_IOC(rq
)->aic
->nr_dispatched
);
1204 * as_queue_empty tells us if there are requests left in the device. It may
1205 * not be the case that a driver can get the next request even if the queue
1206 * is not empty - it is used in the block layer to check for plugging and
1207 * merging opportunities
1209 static int as_queue_empty(request_queue_t
*q
)
1211 struct as_data
*ad
= q
->elevator
->elevator_data
;
1213 return list_empty(&ad
->fifo_list
[REQ_ASYNC
])
1214 && list_empty(&ad
->fifo_list
[REQ_SYNC
]);
1218 as_merge(request_queue_t
*q
, struct request
**req
, struct bio
*bio
)
1220 struct as_data
*ad
= q
->elevator
->elevator_data
;
1221 sector_t rb_key
= bio
->bi_sector
+ bio_sectors(bio
);
1222 struct request
*__rq
;
1225 * check for front merge
1227 __rq
= elv_rb_find(&ad
->sort_list
[bio_data_dir(bio
)], rb_key
);
1228 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
1230 return ELEVATOR_FRONT_MERGE
;
1233 return ELEVATOR_NO_MERGE
;
1236 static void as_merged_request(request_queue_t
*q
, struct request
*req
, int type
)
1238 struct as_data
*ad
= q
->elevator
->elevator_data
;
1241 * if the merge was a front merge, we need to reposition request
1243 if (type
== ELEVATOR_FRONT_MERGE
) {
1244 as_del_rq_rb(ad
, req
);
1245 as_add_rq_rb(ad
, req
);
1247 * Note! At this stage of this and the next function, our next
1248 * request may not be optimal - eg the request may have "grown"
1249 * behind the disk head. We currently don't bother adjusting.
1254 static void as_merged_requests(request_queue_t
*q
, struct request
*req
,
1255 struct request
*next
)
1258 * if next expires before rq, assign its expire time to arq
1259 * and move into next position (next will be deleted) in fifo
1261 if (!list_empty(&req
->queuelist
) && !list_empty(&next
->queuelist
)) {
1262 if (time_before(rq_fifo_time(next
), rq_fifo_time(req
))) {
1263 struct io_context
*rioc
= RQ_IOC(req
);
1264 struct io_context
*nioc
= RQ_IOC(next
);
1266 list_move(&req
->queuelist
, &next
->queuelist
);
1267 rq_set_fifo_time(req
, rq_fifo_time(next
));
1269 * Don't copy here but swap, because when anext is
1270 * removed below, it must contain the unused context
1272 swap_io_context(&rioc
, &nioc
);
1277 * kill knowledge of next, this one is a goner
1279 as_remove_queued_request(q
, next
);
1280 as_put_io_context(next
);
1282 RQ_SET_STATE(next
, AS_RQ_MERGED
);
1286 * This is executed in a "deferred" process context, by kblockd. It calls the
1287 * driver's request_fn so the driver can submit that request.
1289 * IMPORTANT! This guy will reenter the elevator, so set up all queue global
1290 * state before calling, and don't rely on any state over calls.
1292 * FIXME! dispatch queue is not a queue at all!
1294 static void as_work_handler(void *data
)
1296 struct request_queue
*q
= data
;
1297 unsigned long flags
;
1299 spin_lock_irqsave(q
->queue_lock
, flags
);
1300 blk_start_queueing(q
);
1301 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1304 static int as_may_queue(request_queue_t
*q
, int rw
)
1306 int ret
= ELV_MQUEUE_MAY
;
1307 struct as_data
*ad
= q
->elevator
->elevator_data
;
1308 struct io_context
*ioc
;
1309 if (ad
->antic_status
== ANTIC_WAIT_REQ
||
1310 ad
->antic_status
== ANTIC_WAIT_NEXT
) {
1311 ioc
= as_get_io_context(q
->node
);
1312 if (ad
->io_context
== ioc
)
1313 ret
= ELV_MQUEUE_MUST
;
1314 put_io_context(ioc
);
1320 static void as_exit_queue(elevator_t
*e
)
1322 struct as_data
*ad
= e
->elevator_data
;
1324 del_timer_sync(&ad
->antic_timer
);
1327 BUG_ON(!list_empty(&ad
->fifo_list
[REQ_SYNC
]));
1328 BUG_ON(!list_empty(&ad
->fifo_list
[REQ_ASYNC
]));
1330 put_io_context(ad
->io_context
);
1335 * initialize elevator private data (as_data).
1337 static void *as_init_queue(request_queue_t
*q
, elevator_t
*e
)
1341 ad
= kmalloc_node(sizeof(*ad
), GFP_KERNEL
, q
->node
);
1344 memset(ad
, 0, sizeof(*ad
));
1346 ad
->q
= q
; /* Identify what queue the data belongs to */
1348 /* anticipatory scheduling helpers */
1349 ad
->antic_timer
.function
= as_antic_timeout
;
1350 ad
->antic_timer
.data
= (unsigned long)q
;
1351 init_timer(&ad
->antic_timer
);
1352 INIT_WORK(&ad
->antic_work
, as_work_handler
, q
);
1354 INIT_LIST_HEAD(&ad
->fifo_list
[REQ_SYNC
]);
1355 INIT_LIST_HEAD(&ad
->fifo_list
[REQ_ASYNC
]);
1356 ad
->sort_list
[REQ_SYNC
] = RB_ROOT
;
1357 ad
->sort_list
[REQ_ASYNC
] = RB_ROOT
;
1358 ad
->fifo_expire
[REQ_SYNC
] = default_read_expire
;
1359 ad
->fifo_expire
[REQ_ASYNC
] = default_write_expire
;
1360 ad
->antic_expire
= default_antic_expire
;
1361 ad
->batch_expire
[REQ_SYNC
] = default_read_batch_expire
;
1362 ad
->batch_expire
[REQ_ASYNC
] = default_write_batch_expire
;
1364 ad
->current_batch_expires
= jiffies
+ ad
->batch_expire
[REQ_SYNC
];
1365 ad
->write_batch_count
= ad
->batch_expire
[REQ_ASYNC
] / 10;
1366 if (ad
->write_batch_count
< 2)
1367 ad
->write_batch_count
= 2;
1377 as_var_show(unsigned int var
, char *page
)
1379 return sprintf(page
, "%d\n", var
);
1383 as_var_store(unsigned long *var
, const char *page
, size_t count
)
1385 char *p
= (char *) page
;
1387 *var
= simple_strtoul(p
, &p
, 10);
1391 static ssize_t
est_time_show(elevator_t
*e
, char *page
)
1393 struct as_data
*ad
= e
->elevator_data
;
1396 pos
+= sprintf(page
+pos
, "%lu %% exit probability\n",
1397 100*ad
->exit_prob
/256);
1398 pos
+= sprintf(page
+pos
, "%lu %% probability of exiting without a "
1399 "cooperating process submitting IO\n",
1400 100*ad
->exit_no_coop
/256);
1401 pos
+= sprintf(page
+pos
, "%lu ms new thinktime\n", ad
->new_ttime_mean
);
1402 pos
+= sprintf(page
+pos
, "%llu sectors new seek distance\n",
1403 (unsigned long long)ad
->new_seek_mean
);
1408 #define SHOW_FUNCTION(__FUNC, __VAR) \
1409 static ssize_t __FUNC(elevator_t *e, char *page) \
1411 struct as_data *ad = e->elevator_data; \
1412 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1414 SHOW_FUNCTION(as_read_expire_show
, ad
->fifo_expire
[REQ_SYNC
]);
1415 SHOW_FUNCTION(as_write_expire_show
, ad
->fifo_expire
[REQ_ASYNC
]);
1416 SHOW_FUNCTION(as_antic_expire_show
, ad
->antic_expire
);
1417 SHOW_FUNCTION(as_read_batch_expire_show
, ad
->batch_expire
[REQ_SYNC
]);
1418 SHOW_FUNCTION(as_write_batch_expire_show
, ad
->batch_expire
[REQ_ASYNC
]);
1419 #undef SHOW_FUNCTION
1421 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
1422 static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
1424 struct as_data *ad = e->elevator_data; \
1425 int ret = as_var_store(__PTR, (page), count); \
1426 if (*(__PTR) < (MIN)) \
1428 else if (*(__PTR) > (MAX)) \
1430 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1433 STORE_FUNCTION(as_read_expire_store
, &ad
->fifo_expire
[REQ_SYNC
], 0, INT_MAX
);
1434 STORE_FUNCTION(as_write_expire_store
, &ad
->fifo_expire
[REQ_ASYNC
], 0, INT_MAX
);
1435 STORE_FUNCTION(as_antic_expire_store
, &ad
->antic_expire
, 0, INT_MAX
);
1436 STORE_FUNCTION(as_read_batch_expire_store
,
1437 &ad
->batch_expire
[REQ_SYNC
], 0, INT_MAX
);
1438 STORE_FUNCTION(as_write_batch_expire_store
,
1439 &ad
->batch_expire
[REQ_ASYNC
], 0, INT_MAX
);
1440 #undef STORE_FUNCTION
1442 #define AS_ATTR(name) \
1443 __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store)
1445 static struct elv_fs_entry as_attrs
[] = {
1446 __ATTR_RO(est_time
),
1447 AS_ATTR(read_expire
),
1448 AS_ATTR(write_expire
),
1449 AS_ATTR(antic_expire
),
1450 AS_ATTR(read_batch_expire
),
1451 AS_ATTR(write_batch_expire
),
1455 static struct elevator_type iosched_as
= {
1457 .elevator_merge_fn
= as_merge
,
1458 .elevator_merged_fn
= as_merged_request
,
1459 .elevator_merge_req_fn
= as_merged_requests
,
1460 .elevator_dispatch_fn
= as_dispatch_request
,
1461 .elevator_add_req_fn
= as_add_request
,
1462 .elevator_activate_req_fn
= as_activate_request
,
1463 .elevator_deactivate_req_fn
= as_deactivate_request
,
1464 .elevator_queue_empty_fn
= as_queue_empty
,
1465 .elevator_completed_req_fn
= as_completed_request
,
1466 .elevator_former_req_fn
= elv_rb_former_request
,
1467 .elevator_latter_req_fn
= elv_rb_latter_request
,
1468 .elevator_may_queue_fn
= as_may_queue
,
1469 .elevator_init_fn
= as_init_queue
,
1470 .elevator_exit_fn
= as_exit_queue
,
1474 .elevator_attrs
= as_attrs
,
1475 .elevator_name
= "anticipatory",
1476 .elevator_owner
= THIS_MODULE
,
1479 static int __init
as_init(void)
1483 ret
= elv_register(&iosched_as
);
1486 * don't allow AS to get unregistered, since we would have
1487 * to browse all tasks in the system and release their
1488 * as_io_context first
1490 __module_get(THIS_MODULE
);
1497 static void __exit
as_exit(void)
1499 DECLARE_COMPLETION_ONSTACK(all_gone
);
1500 elv_unregister(&iosched_as
);
1501 ioc_gone
= &all_gone
;
1502 /* ioc_gone's update must be visible before reading ioc_count */
1504 if (elv_ioc_count_read(ioc_count
))
1505 wait_for_completion(ioc_gone
);
1509 module_init(as_init
);
1510 module_exit(as_exit
);
1512 MODULE_AUTHOR("Nick Piggin");
1513 MODULE_LICENSE("GPL");
1514 MODULE_DESCRIPTION("anticipatory IO scheduler");