blob: 7a12cf6ee1d35e212a6d26007a1eb2057c189357 [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 *
Jens Axboe0fe23472006-09-04 15:41:16 +02004 * Copyright (C) 2002 Jens Axboe <axboe@kernel.dk>
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/*
21 * See Documentation/block/as-iosched.txt
22 */
23
24/*
25 * max time before a read is submitted.
26 */
27#define default_read_expire (HZ / 8)
28
29/*
30 * ditto for writes, these limits are not hard, even
31 * if the disk is capable of satisfying them.
32 */
33#define default_write_expire (HZ / 4)
34
35/*
36 * read_batch_expire describes how long we will allow a stream of reads to
37 * persist before looking to see whether it is time to switch over to writes.
38 */
39#define default_read_batch_expire (HZ / 2)
40
41/*
42 * write_batch_expire describes how long we want a stream of writes to run for.
43 * This is not a hard limit, but a target we set for the auto-tuning thingy.
44 * See, the problem is: we can send a lot of writes to disk cache / TCQ in
45 * a short amount of time...
46 */
47#define default_write_batch_expire (HZ / 8)
48
49/*
50 * max time we may wait to anticipate a read (default around 6ms)
51 */
52#define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
53
54/*
55 * Keep track of up to 20ms thinktimes. We can go as big as we like here,
56 * however huge values tend to interfere and not decay fast enough. A program
57 * might be in a non-io phase of operation. Waiting on user input for example,
58 * or doing a lengthy computation. A small penalty can be justified there, and
59 * will still catch out those processes that constantly have large thinktimes.
60 */
61#define MAX_THINKTIME (HZ/50UL)
62
63/* Bits in as_io_context.state */
64enum as_io_states {
Nick Pigginf5b3db02005-11-07 00:59:53 -080065 AS_TASK_RUNNING=0, /* Process has not exited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 AS_TASK_IOSTARTED, /* Process has started some IO */
67 AS_TASK_IORUNNING, /* Process has completed some IO */
68};
69
70enum anticipation_status {
71 ANTIC_OFF=0, /* Not anticipating (normal operation) */
72 ANTIC_WAIT_REQ, /* The last read has not yet completed */
73 ANTIC_WAIT_NEXT, /* Currently anticipating a request vs
74 last read (which has completed) */
75 ANTIC_FINISHED, /* Anticipating but have found a candidate
76 * or timed out */
77};
78
79struct as_data {
80 /*
81 * run time data
82 */
83
84 struct request_queue *q; /* the "owner" queue */
85
86 /*
87 * requests (as_rq s) are present on both sort_list and fifo_list
88 */
89 struct rb_root sort_list[2];
90 struct list_head fifo_list[2];
91
Jens Axboe8a8e6742006-07-18 21:07:29 +020092 struct request *next_rq[2]; /* next in sort order */
Jens Axboe1d6bfbd2009-04-08 11:02:08 +020093 sector_t last_sector[2]; /* last SYNC & ASYNC sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 unsigned long exit_prob; /* probability a task will exit while
96 being waited on */
Nick Pigginf5b3db02005-11-07 00:59:53 -080097 unsigned long exit_no_coop; /* probablility an exited task will
98 not be part of a later cooperating
99 request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 unsigned long new_ttime_total; /* mean thinktime on new proc */
101 unsigned long new_ttime_mean;
102 u64 new_seek_total; /* mean seek on new proc */
103 sector_t new_seek_mean;
104
105 unsigned long current_batch_expires;
106 unsigned long last_check_fifo[2];
107 int changed_batch; /* 1: waiting for old batch to end */
108 int new_batch; /* 1: waiting on first read complete */
Jens Axboe1d6bfbd2009-04-08 11:02:08 +0200109 int batch_data_dir; /* current batch SYNC / ASYNC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int write_batch_count; /* max # of reqs in a write batch */
111 int current_write_count; /* how many requests left this batch */
112 int write_batch_idled; /* has the write batch gone idle? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 enum anticipation_status antic_status;
115 unsigned long antic_start; /* jiffies: when it started */
116 struct timer_list antic_timer; /* anticipatory scheduling timer */
117 struct work_struct antic_work; /* Deferred unplugging */
118 struct io_context *io_context; /* Identify the expected process */
119 int ioc_finished; /* IO associated with io_context is finished */
120 int nr_dispatched;
121
122 /*
123 * settings that change how the i/o scheduler behaves
124 */
125 unsigned long fifo_expire[2];
126 unsigned long batch_expire[2];
127 unsigned long antic_expire;
128};
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * per-request data.
132 */
133enum arq_state {
134 AS_RQ_NEW=0, /* New - not referenced and not on any lists */
135 AS_RQ_QUEUED, /* In the request queue. It belongs to the
136 scheduler */
137 AS_RQ_DISPATCHED, /* On the dispatch list. It belongs to the
138 driver now */
139 AS_RQ_PRESCHED, /* Debug poisoning for requests being used */
140 AS_RQ_REMOVED,
141 AS_RQ_MERGED,
142 AS_RQ_POSTSCHED, /* when they shouldn't be */
143};
144
Jens Axboe8a8e6742006-07-18 21:07:29 +0200145#define RQ_IOC(rq) ((struct io_context *) (rq)->elevator_private)
146#define RQ_STATE(rq) ((enum arq_state)(rq)->elevator_private2)
147#define RQ_SET_STATE(rq, state) ((rq)->elevator_private2 = (void *) state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Jens Axboee4313dd2006-07-19 05:10:01 +0200149static DEFINE_PER_CPU(unsigned long, ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -0500150static struct completion *ioc_gone;
Jens Axboe863fddc2008-05-29 09:35:22 +0200151static DEFINE_SPINLOCK(ioc_gone_lock);
Al Viro334e94d2006-03-18 15:05:53 -0500152
Jens Axboe8a8e6742006-07-18 21:07:29 +0200153static void as_move_to_dispatch(struct as_data *ad, struct request *rq);
Tejun Heoef9be1d2005-11-11 14:27:09 +0100154static void as_antic_stop(struct as_data *ad);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/*
157 * IO Context helper functions
158 */
159
160/* Called to deallocate the as_io_context */
161static void free_as_io_context(struct as_io_context *aic)
162{
163 kfree(aic);
Jens Axboee4313dd2006-07-19 05:10:01 +0200164 elv_ioc_count_dec(ioc_count);
Jens Axboe863fddc2008-05-29 09:35:22 +0200165 if (ioc_gone) {
166 /*
167 * AS scheduler is exiting, grab exit lock and check
168 * the pending io context count. If it hits zero,
169 * complete ioc_gone and set it back to NULL.
170 */
171 spin_lock(&ioc_gone_lock);
172 if (ioc_gone && !elv_ioc_count_read(ioc_count)) {
173 complete(ioc_gone);
174 ioc_gone = NULL;
175 }
176 spin_unlock(&ioc_gone_lock);
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Al Viroe17a9482006-03-18 13:21:20 -0500180static void as_trim(struct io_context *ioc)
181{
Jens Axboe8bdd3f82008-02-01 09:44:28 +0100182 spin_lock_irq(&ioc->lock);
Al Viro334e94d2006-03-18 15:05:53 -0500183 if (ioc->aic)
184 free_as_io_context(ioc->aic);
Al Viroe17a9482006-03-18 13:21:20 -0500185 ioc->aic = NULL;
Jens Axboe8bdd3f82008-02-01 09:44:28 +0100186 spin_unlock_irq(&ioc->lock);
Al Viroe17a9482006-03-18 13:21:20 -0500187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* Called when the task exits */
190static void exit_as_io_context(struct as_io_context *aic)
191{
192 WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
193 clear_bit(AS_TASK_RUNNING, &aic->state);
194}
195
196static struct as_io_context *alloc_as_io_context(void)
197{
198 struct as_io_context *ret;
199
200 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
201 if (ret) {
202 ret->dtor = free_as_io_context;
203 ret->exit = exit_as_io_context;
204 ret->state = 1 << AS_TASK_RUNNING;
205 atomic_set(&ret->nr_queued, 0);
206 atomic_set(&ret->nr_dispatched, 0);
207 spin_lock_init(&ret->lock);
208 ret->ttime_total = 0;
209 ret->ttime_samples = 0;
210 ret->ttime_mean = 0;
211 ret->seek_total = 0;
212 ret->seek_samples = 0;
213 ret->seek_mean = 0;
Jens Axboee4313dd2006-07-19 05:10:01 +0200214 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217 return ret;
218}
219
220/*
221 * If the current task has no AS IO context then create one and initialise it.
222 * Then take a ref on the task's io context and return it.
223 */
Jens Axboeb5deef92006-07-19 23:39:40 +0200224static struct io_context *as_get_io_context(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Jens Axboeb5deef92006-07-19 23:39:40 +0200226 struct io_context *ioc = get_io_context(GFP_ATOMIC, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (ioc && !ioc->aic) {
228 ioc->aic = alloc_as_io_context();
229 if (!ioc->aic) {
230 put_io_context(ioc);
231 ioc = NULL;
232 }
233 }
234 return ioc;
235}
236
Jens Axboe8a8e6742006-07-18 21:07:29 +0200237static void as_put_io_context(struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +0200238{
239 struct as_io_context *aic;
240
Jens Axboe8a8e6742006-07-18 21:07:29 +0200241 if (unlikely(!RQ_IOC(rq)))
Jens Axboeb4878f22005-10-20 16:42:29 +0200242 return;
243
Jens Axboe8a8e6742006-07-18 21:07:29 +0200244 aic = RQ_IOC(rq)->aic;
Jens Axboeb4878f22005-10-20 16:42:29 +0200245
Jens Axboe8a8e6742006-07-18 21:07:29 +0200246 if (rq_is_sync(rq) && aic) {
Jens Axboe8bdd3f82008-02-01 09:44:28 +0100247 unsigned long flags;
248
249 spin_lock_irqsave(&aic->lock, flags);
Jens Axboeb4878f22005-10-20 16:42:29 +0200250 set_bit(AS_TASK_IORUNNING, &aic->state);
251 aic->last_end_request = jiffies;
Jens Axboe8bdd3f82008-02-01 09:44:28 +0100252 spin_unlock_irqrestore(&aic->lock, flags);
Jens Axboeb4878f22005-10-20 16:42:29 +0200253 }
254
Jens Axboe8a8e6742006-07-18 21:07:29 +0200255 put_io_context(RQ_IOC(rq));
Jens Axboeb4878f22005-10-20 16:42:29 +0200256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * rb tree support functions
260 */
Jens Axboe9e2585a2006-07-28 09:26:13 +0200261#define RQ_RB_ROOT(ad, rq) (&(ad)->sort_list[rq_is_sync((rq))])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Jens Axboe8a8e6742006-07-18 21:07:29 +0200263static void as_add_rq_rb(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Jens Axboee37f3462006-07-18 21:06:01 +0200265 struct request *alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Jens Axboe9e2585a2006-07-28 09:26:13 +0200267 while ((unlikely(alias = elv_rb_add(RQ_RB_ROOT(ad, rq), rq)))) {
Jens Axboe8a8e6742006-07-18 21:07:29 +0200268 as_move_to_dispatch(ad, alias);
Tejun Heoef9be1d2005-11-11 14:27:09 +0100269 as_antic_stop(ad);
270 }
271}
272
Jens Axboe8a8e6742006-07-18 21:07:29 +0200273static inline void as_del_rq_rb(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Jens Axboe9e2585a2006-07-28 09:26:13 +0200275 elv_rb_del(RQ_RB_ROOT(ad, rq), rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/*
279 * IO Scheduler proper
280 */
281
282#define MAXBACK (1024 * 1024) /*
283 * Maximum distance the disk will go backward
284 * for a request.
285 */
286
287#define BACK_PENALTY 2
288
289/*
290 * as_choose_req selects the preferred one of two requests of the same data_dir
291 * ignoring time - eg. timeouts, which is the job of as_dispatch_request
292 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200293static struct request *
294as_choose_req(struct as_data *ad, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 int data_dir;
297 sector_t last, s1, s2, d1, d2;
298 int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */
299 const sector_t maxback = MAXBACK;
300
Jens Axboe8a8e6742006-07-18 21:07:29 +0200301 if (rq1 == NULL || rq1 == rq2)
302 return rq2;
303 if (rq2 == NULL)
304 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Jens Axboe8a8e6742006-07-18 21:07:29 +0200306 data_dir = rq_is_sync(rq1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 last = ad->last_sector[data_dir];
Tejun Heo83096eb2009-05-07 22:24:39 +0900309 s1 = blk_rq_pos(rq1);
310 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Jens Axboe8a8e6742006-07-18 21:07:29 +0200312 BUG_ON(data_dir != rq_is_sync(rq2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /*
315 * Strict one way elevator _except_ in the case where we allow
316 * short backward seeks which are biased as twice the cost of a
317 * similar forward seek.
318 */
319 if (s1 >= last)
320 d1 = s1 - last;
321 else if (s1+maxback >= last)
322 d1 = (last - s1)*BACK_PENALTY;
323 else {
324 r1_wrap = 1;
325 d1 = 0; /* shut up, gcc */
326 }
327
328 if (s2 >= last)
329 d2 = s2 - last;
330 else if (s2+maxback >= last)
331 d2 = (last - s2)*BACK_PENALTY;
332 else {
333 r2_wrap = 1;
334 d2 = 0;
335 }
336
337 /* Found required data */
338 if (!r1_wrap && r2_wrap)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200339 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 else if (!r2_wrap && r1_wrap)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200341 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 else if (r1_wrap && r2_wrap) {
343 /* both behind the head */
344 if (s1 <= s2)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200345 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 else
Jens Axboe8a8e6742006-07-18 21:07:29 +0200347 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
350 /* Both requests in front of the head */
351 if (d1 < d2)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200352 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 else if (d2 < d1)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200354 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 else {
356 if (s1 >= s2)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200357 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 else
Jens Axboe8a8e6742006-07-18 21:07:29 +0200359 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361}
362
363/*
Jens Axboe8a8e6742006-07-18 21:07:29 +0200364 * as_find_next_rq finds the next request after @prev in elevator order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * this with as_choose_req form the basis for how the scheduler chooses
366 * what request to process next. Anticipation works on top of this.
367 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200368static struct request *
369as_find_next_rq(struct as_data *ad, struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct rb_node *rbnext = rb_next(&last->rb_node);
372 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe8a8e6742006-07-18 21:07:29 +0200373 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Jens Axboee37f3462006-07-18 21:06:01 +0200375 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (rbprev)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200378 prev = rb_entry_rq(rbprev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (rbnext)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200381 next = rb_entry_rq(rbnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 else {
Jens Axboe9e2585a2006-07-28 09:26:13 +0200383 const int data_dir = rq_is_sync(last);
Jens Axboee37f3462006-07-18 21:06:01 +0200384
385 rbnext = rb_first(&ad->sort_list[data_dir]);
386 if (rbnext && rbnext != &last->rb_node)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200387 next = rb_entry_rq(rbnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Jens Axboee37f3462006-07-18 21:06:01 +0200390 return as_choose_req(ad, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393/*
394 * anticipatory scheduling functions follow
395 */
396
397/*
398 * as_antic_expired tells us when we have anticipated too long.
399 * The funny "absolute difference" math on the elapsed time is to handle
400 * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
401 */
402static int as_antic_expired(struct as_data *ad)
403{
404 long delta_jif;
405
406 delta_jif = jiffies - ad->antic_start;
407 if (unlikely(delta_jif < 0))
408 delta_jif = -delta_jif;
409 if (delta_jif < ad->antic_expire)
410 return 0;
411
412 return 1;
413}
414
415/*
416 * as_antic_waitnext starts anticipating that a nice request will soon be
417 * submitted. See also as_antic_waitreq
418 */
419static void as_antic_waitnext(struct as_data *ad)
420{
421 unsigned long timeout;
422
423 BUG_ON(ad->antic_status != ANTIC_OFF
424 && ad->antic_status != ANTIC_WAIT_REQ);
425
426 timeout = ad->antic_start + ad->antic_expire;
427
428 mod_timer(&ad->antic_timer, timeout);
429
430 ad->antic_status = ANTIC_WAIT_NEXT;
431}
432
433/*
434 * as_antic_waitreq starts anticipating. We don't start timing the anticipation
435 * until the request that we're anticipating on has finished. This means we
436 * are timing from when the candidate process wakes up hopefully.
437 */
438static void as_antic_waitreq(struct as_data *ad)
439{
440 BUG_ON(ad->antic_status == ANTIC_FINISHED);
441 if (ad->antic_status == ANTIC_OFF) {
442 if (!ad->io_context || ad->ioc_finished)
443 as_antic_waitnext(ad);
444 else
445 ad->antic_status = ANTIC_WAIT_REQ;
446 }
447}
448
449/*
450 * This is called directly by the functions in this file to stop anticipation.
451 * We kill the timer and schedule a call to the request_fn asap.
452 */
453static void as_antic_stop(struct as_data *ad)
454{
455 int status = ad->antic_status;
456
457 if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
458 if (status == ANTIC_WAIT_NEXT)
459 del_timer(&ad->antic_timer);
460 ad->antic_status = ANTIC_FINISHED;
461 /* see as_work_handler */
Jens Axboe18887ad2008-07-28 13:08:45 +0200462 kblockd_schedule_work(ad->q, &ad->antic_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464}
465
466/*
467 * as_antic_timeout is the timer function set by as_antic_waitnext.
468 */
469static void as_antic_timeout(unsigned long data)
470{
471 struct request_queue *q = (struct request_queue *)data;
472 struct as_data *ad = q->elevator->elevator_data;
473 unsigned long flags;
474
475 spin_lock_irqsave(q->queue_lock, flags);
476 if (ad->antic_status == ANTIC_WAIT_REQ
477 || ad->antic_status == ANTIC_WAIT_NEXT) {
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100478 struct as_io_context *aic;
479 spin_lock(&ad->io_context->lock);
480 aic = ad->io_context->aic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 ad->antic_status = ANTIC_FINISHED;
Jens Axboe18887ad2008-07-28 13:08:45 +0200483 kblockd_schedule_work(q, &ad->antic_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (aic->ttime_samples == 0) {
Nick Pigginf5b3db02005-11-07 00:59:53 -0800486 /* process anticipated on has exited or timed out*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 ad->exit_prob = (7*ad->exit_prob + 256)/8;
488 }
Nick Pigginf5b3db02005-11-07 00:59:53 -0800489 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
490 /* process not "saved" by a cooperating request */
491 ad->exit_no_coop = (7*ad->exit_no_coop + 256)/8;
492 }
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100493 spin_unlock(&ad->io_context->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495 spin_unlock_irqrestore(q->queue_lock, flags);
496}
497
Nick Pigginf5b3db02005-11-07 00:59:53 -0800498static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic,
499 unsigned long ttime)
500{
501 /* fixed point: 1.0 == 1<<8 */
502 if (aic->ttime_samples == 0) {
503 ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
504 ad->new_ttime_mean = ad->new_ttime_total / 256;
505
506 ad->exit_prob = (7*ad->exit_prob)/8;
507 }
508 aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
509 aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
510 aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
511}
512
513static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic,
514 sector_t sdist)
515{
516 u64 total;
517
518 if (aic->seek_samples == 0) {
519 ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
520 ad->new_seek_mean = ad->new_seek_total / 256;
521 }
522
523 /*
524 * Don't allow the seek distance to get too large from the
525 * odd fragment, pagein, etc
526 */
527 if (aic->seek_samples <= 60) /* second&third seek */
528 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
529 else
530 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
531
532 aic->seek_samples = (7*aic->seek_samples + 256) / 8;
533 aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
534 total = aic->seek_total + (aic->seek_samples/2);
535 do_div(total, aic->seek_samples);
536 aic->seek_mean = (sector_t)total;
537}
538
539/*
540 * as_update_iohist keeps a decaying histogram of IO thinktimes, and
541 * updates @aic->ttime_mean based on that. It is called when a new
542 * request is queued.
543 */
544static void as_update_iohist(struct as_data *ad, struct as_io_context *aic,
545 struct request *rq)
546{
Jens Axboe9e2585a2006-07-28 09:26:13 +0200547 int data_dir = rq_is_sync(rq);
Nick Pigginf5b3db02005-11-07 00:59:53 -0800548 unsigned long thinktime = 0;
549 sector_t seek_dist;
550
551 if (aic == NULL)
552 return;
553
Jens Axboe1d6bfbd2009-04-08 11:02:08 +0200554 if (data_dir == BLK_RW_SYNC) {
Nick Pigginf5b3db02005-11-07 00:59:53 -0800555 unsigned long in_flight = atomic_read(&aic->nr_queued)
556 + atomic_read(&aic->nr_dispatched);
557 spin_lock(&aic->lock);
558 if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
559 test_bit(AS_TASK_IOSTARTED, &aic->state)) {
560 /* Calculate read -> read thinktime */
561 if (test_bit(AS_TASK_IORUNNING, &aic->state)
562 && in_flight == 0) {
563 thinktime = jiffies - aic->last_end_request;
564 thinktime = min(thinktime, MAX_THINKTIME-1);
565 }
566 as_update_thinktime(ad, aic, thinktime);
567
568 /* Calculate read -> read seek distance */
Tejun Heo83096eb2009-05-07 22:24:39 +0900569 if (aic->last_request_pos < blk_rq_pos(rq))
570 seek_dist = blk_rq_pos(rq) -
571 aic->last_request_pos;
Nick Pigginf5b3db02005-11-07 00:59:53 -0800572 else
Tejun Heo83096eb2009-05-07 22:24:39 +0900573 seek_dist = aic->last_request_pos -
574 blk_rq_pos(rq);
Nick Pigginf5b3db02005-11-07 00:59:53 -0800575 as_update_seekdist(ad, aic, seek_dist);
576 }
Tejun Heo83096eb2009-05-07 22:24:39 +0900577 aic->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Nick Pigginf5b3db02005-11-07 00:59:53 -0800578 set_bit(AS_TASK_IOSTARTED, &aic->state);
579 spin_unlock(&aic->lock);
580 }
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/*
584 * as_close_req decides if one request is considered "close" to the
585 * previous one issued.
586 */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800587static int as_close_req(struct as_data *ad, struct as_io_context *aic,
Jens Axboe8a8e6742006-07-18 21:07:29 +0200588 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Nick Pigginc6a632a2007-05-08 00:26:34 -0700590 unsigned long delay; /* jiffies */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 sector_t last = ad->last_sector[ad->batch_data_dir];
Tejun Heo83096eb2009-05-07 22:24:39 +0900592 sector_t next = blk_rq_pos(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 sector_t delta; /* acceptable close offset (in sectors) */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800594 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
597 delay = 0;
598 else
Nick Pigginc6a632a2007-05-08 00:26:34 -0700599 delay = jiffies - ad->antic_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Nick Pigginf5b3db02005-11-07 00:59:53 -0800601 if (delay == 0)
602 delta = 8192;
Nick Pigginc6a632a2007-05-08 00:26:34 -0700603 else if (delay <= (20 * HZ / 1000) && delay <= ad->antic_expire)
Nick Pigginf5b3db02005-11-07 00:59:53 -0800604 delta = 8192 << delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 else
606 return 1;
607
Nick Pigginf5b3db02005-11-07 00:59:53 -0800608 if ((last <= next + (delta>>1)) && (next <= last + delta))
609 return 1;
610
611 if (last < next)
612 s = next - last;
613 else
614 s = last - next;
615
616 if (aic->seek_samples == 0) {
617 /*
618 * Process has just started IO. Use past statistics to
619 * gauge success possibility
620 */
621 if (ad->new_seek_mean > s) {
622 /* this request is better than what we're expecting */
623 return 1;
624 }
625
626 } else {
627 if (aic->seek_mean > s) {
628 /* this request is better than what we're expecting */
629 return 1;
630 }
631 }
632
633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636/*
637 * as_can_break_anticipation returns true if we have been anticipating this
638 * request.
639 *
640 * It also returns true if the process against which we are anticipating
641 * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
642 * dispatch it ASAP, because we know that application will not be submitting
643 * any new reads.
644 *
Nick Pigginf5b3db02005-11-07 00:59:53 -0800645 * If the task which has submitted the request has exited, break anticipation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 *
647 * If this task has queued some other IO, do not enter enticipation.
648 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200649static int as_can_break_anticipation(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 struct io_context *ioc;
652 struct as_io_context *aic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 ioc = ad->io_context;
655 BUG_ON(!ioc);
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100656 spin_lock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Jens Axboe8a8e6742006-07-18 21:07:29 +0200658 if (rq && ioc == RQ_IOC(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /* request from same process */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100660 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return 1;
662 }
663
664 if (ad->ioc_finished && as_antic_expired(ad)) {
665 /*
666 * In this situation status should really be FINISHED,
667 * however the timer hasn't had the chance to run yet.
668 */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100669 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 1;
671 }
672
673 aic = ioc->aic;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100674 if (!aic) {
675 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return 0;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (atomic_read(&aic->nr_queued) > 0) {
680 /* process has more requests queued */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100681 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 1;
683 }
684
685 if (atomic_read(&aic->nr_dispatched) > 0) {
686 /* process has more requests dispatched */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100687 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return 1;
689 }
690
Jens Axboe8a8e6742006-07-18 21:07:29 +0200691 if (rq && rq_is_sync(rq) && as_close_req(ad, aic, rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /*
693 * Found a close request that is not one of ours.
694 *
Nick Pigginf5b3db02005-11-07 00:59:53 -0800695 * This makes close requests from another process update
696 * our IO history. Is generally useful when there are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * two or more cooperating processes working in the same
698 * area.
699 */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800700 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
701 if (aic->ttime_samples == 0)
702 ad->exit_prob = (7*ad->exit_prob + 256)/8;
703
704 ad->exit_no_coop = (7*ad->exit_no_coop)/8;
705 }
706
Jens Axboe8a8e6742006-07-18 21:07:29 +0200707 as_update_iohist(ad, aic, rq);
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100708 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 1;
710 }
711
Nick Pigginf5b3db02005-11-07 00:59:53 -0800712 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
713 /* process anticipated on has exited */
714 if (aic->ttime_samples == 0)
715 ad->exit_prob = (7*ad->exit_prob + 256)/8;
716
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100717 if (ad->exit_no_coop > 128) {
718 spin_unlock(&ioc->lock);
Nick Pigginf5b3db02005-11-07 00:59:53 -0800719 return 1;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100720 }
Nick Pigginf5b3db02005-11-07 00:59:53 -0800721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (aic->ttime_samples == 0) {
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100724 if (ad->new_ttime_mean > ad->antic_expire) {
725 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 1;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100727 }
728 if (ad->exit_prob * ad->exit_no_coop > 128*256) {
729 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return 1;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 } else if (aic->ttime_mean > ad->antic_expire) {
733 /* the process thinks too much between requests */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100734 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 1;
736 }
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100737 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return 0;
739}
740
741/*
Jens Axboe8a8e6742006-07-18 21:07:29 +0200742 * as_can_anticipate indicates whether we should either run rq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * or keep anticipating a better request.
744 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200745static int as_can_anticipate(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Jens Axboef7d7b7a2008-09-25 11:37:50 +0200747#if 0 /* disable for now, we need to check tag level as well */
Jens Axboea68bbddba2008-09-24 13:03:33 +0200748 /*
749 * SSD device without seek penalty, disable idling
750 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +0200751 if (blk_queue_nonrot(ad->q)) axman
Jens Axboea68bbddba2008-09-24 13:03:33 +0200752 return 0;
Jens Axboef7d7b7a2008-09-25 11:37:50 +0200753#endif
Jens Axboea68bbddba2008-09-24 13:03:33 +0200754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (!ad->io_context)
756 /*
757 * Last request submitted was a write
758 */
759 return 0;
760
761 if (ad->antic_status == ANTIC_FINISHED)
762 /*
763 * Don't restart if we have just finished. Run the next request
764 */
765 return 0;
766
Jens Axboe8a8e6742006-07-18 21:07:29 +0200767 if (as_can_break_anticipation(ad, rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /*
769 * This request is a good candidate. Don't keep anticipating,
770 * run it.
771 */
772 return 0;
773
774 /*
775 * OK from here, we haven't finished, and don't have a decent request!
776 * Status is either ANTIC_OFF so start waiting,
777 * ANTIC_WAIT_REQ so continue waiting for request to finish
778 * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 */
780
781 return 1;
782}
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/*
Jens Axboe8a8e6742006-07-18 21:07:29 +0200785 * as_update_rq must be called whenever a request (rq) is added to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 * the sort_list. This function keeps caches up to date, and checks if the
787 * request might be one we are "anticipating"
788 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200789static void as_update_rq(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Jens Axboe8a8e6742006-07-18 21:07:29 +0200791 const int data_dir = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Jens Axboe8a8e6742006-07-18 21:07:29 +0200793 /* keep the next_rq cache up to date */
794 ad->next_rq[data_dir] = as_choose_req(ad, rq, ad->next_rq[data_dir]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /*
797 * have we been anticipating this request?
798 * or does it come from the same process as the one we are anticipating
799 * for?
800 */
801 if (ad->antic_status == ANTIC_WAIT_REQ
802 || ad->antic_status == ANTIC_WAIT_NEXT) {
Jens Axboe8a8e6742006-07-18 21:07:29 +0200803 if (as_can_break_anticipation(ad, rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 as_antic_stop(ad);
805 }
806}
807
808/*
809 * Gathers timings and resizes the write batch automatically
810 */
811static void update_write_batch(struct as_data *ad)
812{
Jens Axboe1d6bfbd2009-04-08 11:02:08 +0200813 unsigned long batch = ad->batch_expire[BLK_RW_ASYNC];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 long write_time;
815
816 write_time = (jiffies - ad->current_batch_expires) + batch;
817 if (write_time < 0)
818 write_time = 0;
819
820 if (write_time > batch && !ad->write_batch_idled) {
821 if (write_time > batch * 3)
822 ad->write_batch_count /= 2;
823 else
824 ad->write_batch_count--;
825 } else if (write_time < batch && ad->current_write_count == 0) {
826 if (batch > write_time * 3)
827 ad->write_batch_count *= 2;
828 else
829 ad->write_batch_count++;
830 }
831
832 if (ad->write_batch_count < 1)
833 ad->write_batch_count = 1;
834}
835
836/*
837 * as_completed_request is to be called when a request has completed and
838 * returned something to the requesting process, be it an error or data.
839 */
Jens Axboe165125e2007-07-24 09:28:11 +0200840static void as_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 WARN_ON(!list_empty(&rq->queuelist));
845
Jens Axboe8a8e6742006-07-18 21:07:29 +0200846 if (RQ_STATE(rq) != AS_RQ_REMOVED) {
Arjan van de Ven12e00362008-07-25 19:45:38 -0700847 WARN(1, "rq->state %d\n", RQ_STATE(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 goto out;
849 }
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (ad->changed_batch && ad->nr_dispatched == 1) {
Divyesh Shahd585d0b2008-06-16 18:37:08 +0200852 ad->current_batch_expires = jiffies +
853 ad->batch_expire[ad->batch_data_dir];
Jens Axboe18887ad2008-07-28 13:08:45 +0200854 kblockd_schedule_work(q, &ad->antic_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 ad->changed_batch = 0;
856
Jens Axboe1d6bfbd2009-04-08 11:02:08 +0200857 if (ad->batch_data_dir == BLK_RW_SYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ad->new_batch = 1;
859 }
860 WARN_ON(ad->nr_dispatched == 0);
861 ad->nr_dispatched--;
862
863 /*
864 * Start counting the batch from when a request of that direction is
865 * actually serviced. This should help devices with big TCQ windows
866 * and writeback caches
867 */
Jens Axboe9e2585a2006-07-28 09:26:13 +0200868 if (ad->new_batch && ad->batch_data_dir == rq_is_sync(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 update_write_batch(ad);
870 ad->current_batch_expires = jiffies +
Jens Axboe1d6bfbd2009-04-08 11:02:08 +0200871 ad->batch_expire[BLK_RW_SYNC];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 ad->new_batch = 0;
873 }
874
Jens Axboe8a8e6742006-07-18 21:07:29 +0200875 if (ad->io_context == RQ_IOC(rq) && ad->io_context) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 ad->antic_start = jiffies;
877 ad->ioc_finished = 1;
878 if (ad->antic_status == ANTIC_WAIT_REQ) {
879 /*
880 * We were waiting on this request, now anticipate
881 * the next one
882 */
883 as_antic_waitnext(ad);
884 }
885 }
886
Jens Axboe8a8e6742006-07-18 21:07:29 +0200887 as_put_io_context(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888out:
Jens Axboe8a8e6742006-07-18 21:07:29 +0200889 RQ_SET_STATE(rq, AS_RQ_POSTSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892/*
893 * as_remove_queued_request removes a request from the pre dispatch queue
894 * without updating refcounts. It is expected the caller will drop the
895 * reference unless it replaces the request at somepart of the elevator
896 * (ie. the dispatch queue)
897 */
Jens Axboe165125e2007-07-24 09:28:11 +0200898static void as_remove_queued_request(struct request_queue *q,
899 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Jens Axboe9e2585a2006-07-28 09:26:13 +0200901 const int data_dir = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 struct as_data *ad = q->elevator->elevator_data;
Jens Axboe8a8e6742006-07-18 21:07:29 +0200903 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Jens Axboe8a8e6742006-07-18 21:07:29 +0200905 WARN_ON(RQ_STATE(rq) != AS_RQ_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Jens Axboe8a8e6742006-07-18 21:07:29 +0200907 ioc = RQ_IOC(rq);
908 if (ioc && ioc->aic) {
909 BUG_ON(!atomic_read(&ioc->aic->nr_queued));
910 atomic_dec(&ioc->aic->nr_queued);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912
913 /*
Jens Axboe8a8e6742006-07-18 21:07:29 +0200914 * Update the "next_rq" cache if we are about to remove its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * entry
916 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200917 if (ad->next_rq[data_dir] == rq)
918 ad->next_rq[data_dir] = as_find_next_rq(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Jens Axboed4f2f462006-07-13 09:12:14 +0200920 rq_fifo_clear(rq);
Jens Axboe8a8e6742006-07-18 21:07:29 +0200921 as_del_rq_rb(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924/*
Aaron Carroll8896f3c2007-12-05 21:06:50 +1100925 * as_fifo_expired returns 0 if there are no expired requests on the fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 * 1 otherwise. It is ratelimited so that we only perform the check once per
927 * `fifo_expire' interval. Otherwise a large number of expired requests
928 * would create a hopeless seekstorm.
929 *
930 * See as_antic_expired comment.
931 */
932static int as_fifo_expired(struct as_data *ad, int adir)
933{
Jens Axboed4f2f462006-07-13 09:12:14 +0200934 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 long delta_jif;
936
937 delta_jif = jiffies - ad->last_check_fifo[adir];
938 if (unlikely(delta_jif < 0))
939 delta_jif = -delta_jif;
940 if (delta_jif < ad->fifo_expire[adir])
941 return 0;
942
943 ad->last_check_fifo[adir] = jiffies;
944
945 if (list_empty(&ad->fifo_list[adir]))
946 return 0;
947
Jens Axboed4f2f462006-07-13 09:12:14 +0200948 rq = rq_entry_fifo(ad->fifo_list[adir].next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Jens Axboed4f2f462006-07-13 09:12:14 +0200950 return time_after(jiffies, rq_fifo_time(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953/*
954 * as_batch_expired returns true if the current batch has expired. A batch
955 * is a set of reads or a set of writes.
956 */
957static inline int as_batch_expired(struct as_data *ad)
958{
959 if (ad->changed_batch || ad->new_batch)
960 return 0;
961
Jens Axboe1d6bfbd2009-04-08 11:02:08 +0200962 if (ad->batch_data_dir == BLK_RW_SYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /* TODO! add a check so a complete fifo gets written? */
964 return time_after(jiffies, ad->current_batch_expires);
965
966 return time_after(jiffies, ad->current_batch_expires)
967 || ad->current_write_count == 0;
968}
969
970/*
971 * move an entry to dispatch queue
972 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200973static void as_move_to_dispatch(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Jens Axboe9e2585a2006-07-28 09:26:13 +0200975 const int data_dir = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Jens Axboee37f3462006-07-18 21:06:01 +0200977 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 as_antic_stop(ad);
980 ad->antic_status = ANTIC_OFF;
981
982 /*
983 * This has to be set in order to be correctly updated by
Jens Axboe8a8e6742006-07-18 21:07:29 +0200984 * as_find_next_rq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900986 ad->last_sector[data_dir] = blk_rq_pos(rq) + blk_rq_sectors(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Jens Axboe1d6bfbd2009-04-08 11:02:08 +0200988 if (data_dir == BLK_RW_SYNC) {
Jens Axboe8a8e6742006-07-18 21:07:29 +0200989 struct io_context *ioc = RQ_IOC(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /* In case we have to anticipate after this */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200991 copy_io_context(&ad->io_context, &ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 } else {
993 if (ad->io_context) {
994 put_io_context(ad->io_context);
995 ad->io_context = NULL;
996 }
997
998 if (ad->current_write_count != 0)
999 ad->current_write_count--;
1000 }
1001 ad->ioc_finished = 0;
1002
Jens Axboe8a8e6742006-07-18 21:07:29 +02001003 ad->next_rq[data_dir] = as_find_next_rq(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 /*
1006 * take it off the sort and fifo list, add to dispatch queue
1007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 as_remove_queued_request(ad->q, rq);
Jens Axboe8a8e6742006-07-18 21:07:29 +02001009 WARN_ON(RQ_STATE(rq) != AS_RQ_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Jens Axboeb4878f22005-10-20 16:42:29 +02001011 elv_dispatch_sort(ad->q, rq);
1012
Jens Axboe8a8e6742006-07-18 21:07:29 +02001013 RQ_SET_STATE(rq, AS_RQ_DISPATCHED);
1014 if (RQ_IOC(rq) && RQ_IOC(rq)->aic)
1015 atomic_inc(&RQ_IOC(rq)->aic->nr_dispatched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 ad->nr_dispatched++;
1017}
1018
1019/*
1020 * as_dispatch_request selects the best request according to
1021 * read/write expire, batch expire, etc, and moves it to the dispatch
1022 * queue. Returns 1 if a request was found, 0 otherwise.
1023 */
Jens Axboe165125e2007-07-24 09:28:11 +02001024static int as_dispatch_request(struct request_queue *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Jens Axboeb4878f22005-10-20 16:42:29 +02001026 struct as_data *ad = q->elevator->elevator_data;
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001027 const int reads = !list_empty(&ad->fifo_list[BLK_RW_SYNC]);
1028 const int writes = !list_empty(&ad->fifo_list[BLK_RW_ASYNC]);
Jens Axboe8a8e6742006-07-18 21:07:29 +02001029 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Jens Axboeb4878f22005-10-20 16:42:29 +02001031 if (unlikely(force)) {
1032 /*
1033 * Forced dispatch, accounting is useless. Reset
1034 * accounting states and dump fifo_lists. Note that
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001035 * batch_data_dir is reset to BLK_RW_SYNC to avoid
Jens Axboeb4878f22005-10-20 16:42:29 +02001036 * screwing write batch accounting as write batch
1037 * accounting occurs on W->R transition.
1038 */
1039 int dispatched = 0;
1040
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001041 ad->batch_data_dir = BLK_RW_SYNC;
Jens Axboeb4878f22005-10-20 16:42:29 +02001042 ad->changed_batch = 0;
1043 ad->new_batch = 0;
1044
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001045 while (ad->next_rq[BLK_RW_SYNC]) {
1046 as_move_to_dispatch(ad, ad->next_rq[BLK_RW_SYNC]);
Jens Axboeb4878f22005-10-20 16:42:29 +02001047 dispatched++;
1048 }
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001049 ad->last_check_fifo[BLK_RW_SYNC] = jiffies;
Jens Axboeb4878f22005-10-20 16:42:29 +02001050
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001051 while (ad->next_rq[BLK_RW_ASYNC]) {
1052 as_move_to_dispatch(ad, ad->next_rq[BLK_RW_ASYNC]);
Jens Axboeb4878f22005-10-20 16:42:29 +02001053 dispatched++;
1054 }
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001055 ad->last_check_fifo[BLK_RW_ASYNC] = jiffies;
Jens Axboeb4878f22005-10-20 16:42:29 +02001056
1057 return dispatched;
1058 }
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /* Signal that the write batch was uncontended, so we can't time it */
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001061 if (ad->batch_data_dir == BLK_RW_ASYNC && !reads) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (ad->current_write_count == 0 || !writes)
1063 ad->write_batch_idled = 1;
1064 }
1065
1066 if (!(reads || writes)
1067 || ad->antic_status == ANTIC_WAIT_REQ
1068 || ad->antic_status == ANTIC_WAIT_NEXT
1069 || ad->changed_batch)
1070 return 0;
1071
Nick Pigginf5b3db02005-11-07 00:59:53 -08001072 if (!(reads && writes && as_batch_expired(ad))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /*
1074 * batch is still running or no reads or no writes
1075 */
Jens Axboe8a8e6742006-07-18 21:07:29 +02001076 rq = ad->next_rq[ad->batch_data_dir];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001078 if (ad->batch_data_dir == BLK_RW_SYNC && ad->antic_expire) {
1079 if (as_fifo_expired(ad, BLK_RW_SYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 goto fifo_expired;
1081
Jens Axboe8a8e6742006-07-18 21:07:29 +02001082 if (as_can_anticipate(ad, rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 as_antic_waitreq(ad);
1084 return 0;
1085 }
1086 }
1087
Jens Axboe8a8e6742006-07-18 21:07:29 +02001088 if (rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* we have a "next request" */
1090 if (reads && !writes)
1091 ad->current_batch_expires =
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001092 jiffies + ad->batch_expire[BLK_RW_SYNC];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto dispatch_request;
1094 }
1095 }
1096
1097 /*
1098 * at this point we are not running a batch. select the appropriate
1099 * data direction (read / write)
1100 */
1101
1102 if (reads) {
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001103 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[BLK_RW_SYNC]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001105 if (writes && ad->batch_data_dir == BLK_RW_SYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /*
1107 * Last batch was a read, switch to writes
1108 */
1109 goto dispatch_writes;
1110
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001111 if (ad->batch_data_dir == BLK_RW_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 WARN_ON(ad->new_batch);
1113 ad->changed_batch = 1;
1114 }
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001115 ad->batch_data_dir = BLK_RW_SYNC;
1116 rq = rq_entry_fifo(ad->fifo_list[BLK_RW_SYNC].next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 ad->last_check_fifo[ad->batch_data_dir] = jiffies;
1118 goto dispatch_request;
1119 }
1120
1121 /*
1122 * the last batch was a read
1123 */
1124
1125 if (writes) {
1126dispatch_writes:
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001127 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[BLK_RW_ASYNC]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001129 if (ad->batch_data_dir == BLK_RW_SYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 ad->changed_batch = 1;
1131
1132 /*
1133 * new_batch might be 1 when the queue runs out of
1134 * reads. A subsequent submission of a write might
1135 * cause a change of batch before the read is finished.
1136 */
1137 ad->new_batch = 0;
1138 }
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001139 ad->batch_data_dir = BLK_RW_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 ad->current_write_count = ad->write_batch_count;
1141 ad->write_batch_idled = 0;
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001142 rq = rq_entry_fifo(ad->fifo_list[BLK_RW_ASYNC].next);
1143 ad->last_check_fifo[BLK_RW_ASYNC] = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 goto dispatch_request;
1145 }
1146
1147 BUG();
1148 return 0;
1149
1150dispatch_request:
1151 /*
1152 * If a request has expired, service it.
1153 */
1154
1155 if (as_fifo_expired(ad, ad->batch_data_dir)) {
1156fifo_expired:
Jens Axboe8a8e6742006-07-18 21:07:29 +02001157 rq = rq_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
1160 if (ad->changed_batch) {
1161 WARN_ON(ad->new_batch);
1162
1163 if (ad->nr_dispatched)
1164 return 0;
1165
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001166 if (ad->batch_data_dir == BLK_RW_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 ad->current_batch_expires = jiffies +
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001168 ad->batch_expire[BLK_RW_ASYNC];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 else
1170 ad->new_batch = 1;
1171
1172 ad->changed_batch = 0;
1173 }
1174
1175 /*
Jens Axboe8a8e6742006-07-18 21:07:29 +02001176 * rq is the selected appropriate request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
Jens Axboe8a8e6742006-07-18 21:07:29 +02001178 as_move_to_dispatch(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 return 1;
1181}
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183/*
Jens Axboe8a8e6742006-07-18 21:07:29 +02001184 * add rq to rbtree and fifo
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 */
Jens Axboe165125e2007-07-24 09:28:11 +02001186static void as_add_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Jens Axboeb4878f22005-10-20 16:42:29 +02001188 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int data_dir;
1190
Jens Axboe8a8e6742006-07-18 21:07:29 +02001191 RQ_SET_STATE(rq, AS_RQ_NEW);
Jens Axboeb4878f22005-10-20 16:42:29 +02001192
Jens Axboe9e2585a2006-07-28 09:26:13 +02001193 data_dir = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Jens Axboeb5deef92006-07-19 23:39:40 +02001195 rq->elevator_private = as_get_io_context(q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Jens Axboe8a8e6742006-07-18 21:07:29 +02001197 if (RQ_IOC(rq)) {
1198 as_update_iohist(ad, RQ_IOC(rq)->aic, rq);
1199 atomic_inc(&RQ_IOC(rq)->aic->nr_queued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201
Jens Axboe8a8e6742006-07-18 21:07:29 +02001202 as_add_rq_rb(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Tejun Heoef9be1d2005-11-11 14:27:09 +01001204 /*
Aaron Carroll8896f3c2007-12-05 21:06:50 +11001205 * set expire time and add to fifo list
Tejun Heoef9be1d2005-11-11 14:27:09 +01001206 */
Jens Axboed4f2f462006-07-13 09:12:14 +02001207 rq_set_fifo_time(rq, jiffies + ad->fifo_expire[data_dir]);
1208 list_add_tail(&rq->queuelist, &ad->fifo_list[data_dir]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Jens Axboe8a8e6742006-07-18 21:07:29 +02001210 as_update_rq(ad, rq); /* keep state machine up to date */
1211 RQ_SET_STATE(rq, AS_RQ_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Jens Axboe165125e2007-07-24 09:28:11 +02001214static void as_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001215{
Jens Axboe8a8e6742006-07-18 21:07:29 +02001216 WARN_ON(RQ_STATE(rq) != AS_RQ_DISPATCHED);
1217 RQ_SET_STATE(rq, AS_RQ_REMOVED);
1218 if (RQ_IOC(rq) && RQ_IOC(rq)->aic)
1219 atomic_dec(&RQ_IOC(rq)->aic->nr_dispatched);
Jens Axboeb4878f22005-10-20 16:42:29 +02001220}
1221
Jens Axboe165125e2007-07-24 09:28:11 +02001222static void as_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Jens Axboe8a8e6742006-07-18 21:07:29 +02001224 WARN_ON(RQ_STATE(rq) != AS_RQ_REMOVED);
1225 RQ_SET_STATE(rq, AS_RQ_DISPATCHED);
1226 if (RQ_IOC(rq) && RQ_IOC(rq)->aic)
1227 atomic_inc(&RQ_IOC(rq)->aic->nr_dispatched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230/*
1231 * as_queue_empty tells us if there are requests left in the device. It may
1232 * not be the case that a driver can get the next request even if the queue
1233 * is not empty - it is used in the block layer to check for plugging and
1234 * merging opportunities
1235 */
Jens Axboe165125e2007-07-24 09:28:11 +02001236static int as_queue_empty(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 struct as_data *ad = q->elevator->elevator_data;
1239
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001240 return list_empty(&ad->fifo_list[BLK_RW_ASYNC])
1241 && list_empty(&ad->fifo_list[BLK_RW_SYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244static int
Jens Axboe165125e2007-07-24 09:28:11 +02001245as_merge(struct request_queue *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 struct as_data *ad = q->elevator->elevator_data;
1248 sector_t rb_key = bio->bi_sector + bio_sectors(bio);
1249 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /*
1252 * check for front merge
1253 */
Jens Axboee37f3462006-07-18 21:06:01 +02001254 __rq = elv_rb_find(&ad->sort_list[bio_data_dir(bio)], rb_key);
Jens Axboe98170642006-07-28 09:23:08 +02001255 if (__rq && elv_rq_merge_ok(__rq, bio)) {
1256 *req = __rq;
1257 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
1260 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Jens Axboe165125e2007-07-24 09:28:11 +02001263static void as_merged_request(struct request_queue *q, struct request *req,
1264 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 * if the merge was a front merge, we need to reposition request
1270 */
Jens Axboee37f3462006-07-18 21:06:01 +02001271 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe8a8e6742006-07-18 21:07:29 +02001272 as_del_rq_rb(ad, req);
1273 as_add_rq_rb(ad, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /*
1275 * Note! At this stage of this and the next function, our next
1276 * request may not be optimal - eg the request may have "grown"
1277 * behind the disk head. We currently don't bother adjusting.
1278 */
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Jens Axboe165125e2007-07-24 09:28:11 +02001282static void as_merged_requests(struct request_queue *q, struct request *req,
Nick Pigginf5b3db02005-11-07 00:59:53 -08001283 struct request *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /*
Jens Axboe8a8e6742006-07-18 21:07:29 +02001286 * if next expires before rq, assign its expire time to arq
1287 * and move into next position (next will be deleted) in fifo
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 */
Jens Axboed4f2f462006-07-13 09:12:14 +02001289 if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
1290 if (time_before(rq_fifo_time(next), rq_fifo_time(req))) {
1291 list_move(&req->queuelist, &next->queuelist);
1292 rq_set_fifo_time(req, rq_fifo_time(next));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 }
1295
1296 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 * kill knowledge of next, this one is a goner
1298 */
1299 as_remove_queued_request(q, next);
Jens Axboe8a8e6742006-07-18 21:07:29 +02001300 as_put_io_context(next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Jens Axboe8a8e6742006-07-18 21:07:29 +02001302 RQ_SET_STATE(next, AS_RQ_MERGED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
1305/*
1306 * This is executed in a "deferred" process context, by kblockd. It calls the
1307 * driver's request_fn so the driver can submit that request.
1308 *
1309 * IMPORTANT! This guy will reenter the elevator, so set up all queue global
1310 * state before calling, and don't rely on any state over calls.
1311 *
1312 * FIXME! dispatch queue is not a queue at all!
1313 */
David Howells65f27f32006-11-22 14:55:48 +00001314static void as_work_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
David Howells65f27f32006-11-22 14:55:48 +00001316 struct as_data *ad = container_of(work, struct as_data, antic_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Tejun Heoa7f55792009-04-23 11:05:17 +09001318 blk_run_queue(ad->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Jens Axboe165125e2007-07-24 09:28:11 +02001321static int as_may_queue(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 int ret = ELV_MQUEUE_MAY;
1324 struct as_data *ad = q->elevator->elevator_data;
1325 struct io_context *ioc;
1326 if (ad->antic_status == ANTIC_WAIT_REQ ||
1327 ad->antic_status == ANTIC_WAIT_NEXT) {
Jens Axboeb5deef92006-07-19 23:39:40 +02001328 ioc = as_get_io_context(q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (ad->io_context == ioc)
1330 ret = ELV_MQUEUE_MUST;
1331 put_io_context(ioc);
1332 }
1333
1334 return ret;
1335}
1336
Jens Axboeb374d182008-10-31 10:05:07 +01001337static void as_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 struct as_data *ad = e->elevator_data;
1340
1341 del_timer_sync(&ad->antic_timer);
Cheng Renquan64d01dc2008-12-03 12:41:39 +01001342 cancel_work_sync(&ad->antic_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001344 BUG_ON(!list_empty(&ad->fifo_list[BLK_RW_SYNC]));
1345 BUG_ON(!list_empty(&ad->fifo_list[BLK_RW_ASYNC]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 put_io_context(ad->io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 kfree(ad);
1349}
1350
1351/*
Jens Axboe8a8e6742006-07-18 21:07:29 +02001352 * initialize elevator private data (as_data).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
Jens Axboe165125e2007-07-24 09:28:11 +02001354static void *as_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 struct as_data *ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Christoph Lameter94f60302007-07-17 04:03:29 -07001358 ad = kmalloc_node(sizeof(*ad), GFP_KERNEL | __GFP_ZERO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (!ad)
Jens Axboebc1c1162006-06-08 08:49:06 +02001360 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 ad->q = q; /* Identify what queue the data belongs to */
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /* anticipatory scheduling helpers */
1365 ad->antic_timer.function = as_antic_timeout;
1366 ad->antic_timer.data = (unsigned long)q;
1367 init_timer(&ad->antic_timer);
David Howells65f27f32006-11-22 14:55:48 +00001368 INIT_WORK(&ad->antic_work, as_work_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001370 INIT_LIST_HEAD(&ad->fifo_list[BLK_RW_SYNC]);
1371 INIT_LIST_HEAD(&ad->fifo_list[BLK_RW_ASYNC]);
1372 ad->sort_list[BLK_RW_SYNC] = RB_ROOT;
1373 ad->sort_list[BLK_RW_ASYNC] = RB_ROOT;
1374 ad->fifo_expire[BLK_RW_SYNC] = default_read_expire;
1375 ad->fifo_expire[BLK_RW_ASYNC] = default_write_expire;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 ad->antic_expire = default_antic_expire;
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001377 ad->batch_expire[BLK_RW_SYNC] = default_read_batch_expire;
1378 ad->batch_expire[BLK_RW_ASYNC] = default_write_batch_expire;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001380 ad->current_batch_expires = jiffies + ad->batch_expire[BLK_RW_SYNC];
1381 ad->write_batch_count = ad->batch_expire[BLK_RW_ASYNC] / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (ad->write_batch_count < 2)
1383 ad->write_batch_count = 2;
1384
Jens Axboebc1c1162006-06-08 08:49:06 +02001385 return ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388/*
1389 * sysfs parts below
1390 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392static ssize_t
1393as_var_show(unsigned int var, char *page)
1394{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return sprintf(page, "%d\n", var);
1396}
1397
1398static ssize_t
1399as_var_store(unsigned long *var, const char *page, size_t count)
1400{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 char *p = (char *) page;
1402
Jens Axboec9b3ad62005-07-27 11:43:37 -07001403 *var = simple_strtoul(p, &p, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return count;
1405}
1406
Jens Axboeb374d182008-10-31 10:05:07 +01001407static ssize_t est_time_show(struct elevator_queue *e, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Al Viro3d1ab402006-03-18 18:35:43 -05001409 struct as_data *ad = e->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 int pos = 0;
1411
Nick Pigginf5b3db02005-11-07 00:59:53 -08001412 pos += sprintf(page+pos, "%lu %% exit probability\n",
1413 100*ad->exit_prob/256);
1414 pos += sprintf(page+pos, "%lu %% probability of exiting without a "
1415 "cooperating process submitting IO\n",
1416 100*ad->exit_no_coop/256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
Nick Pigginf5b3db02005-11-07 00:59:53 -08001418 pos += sprintf(page+pos, "%llu sectors new seek distance\n",
1419 (unsigned long long)ad->new_seek_mean);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 return pos;
1422}
1423
1424#define SHOW_FUNCTION(__FUNC, __VAR) \
Jens Axboeb374d182008-10-31 10:05:07 +01001425static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{ \
Al Viro3d1ab402006-03-18 18:35:43 -05001427 struct as_data *ad = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1429}
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001430SHOW_FUNCTION(as_read_expire_show, ad->fifo_expire[BLK_RW_SYNC]);
1431SHOW_FUNCTION(as_write_expire_show, ad->fifo_expire[BLK_RW_ASYNC]);
Al Viroe572ec72006-03-18 22:27:18 -05001432SHOW_FUNCTION(as_antic_expire_show, ad->antic_expire);
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001433SHOW_FUNCTION(as_read_batch_expire_show, ad->batch_expire[BLK_RW_SYNC]);
1434SHOW_FUNCTION(as_write_batch_expire_show, ad->batch_expire[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435#undef SHOW_FUNCTION
1436
1437#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
Jens Axboeb374d182008-10-31 10:05:07 +01001438static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{ \
Al Viro3d1ab402006-03-18 18:35:43 -05001440 struct as_data *ad = e->elevator_data; \
1441 int ret = as_var_store(__PTR, (page), count); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (*(__PTR) < (MIN)) \
1443 *(__PTR) = (MIN); \
1444 else if (*(__PTR) > (MAX)) \
1445 *(__PTR) = (MAX); \
1446 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1447 return ret; \
1448}
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001449STORE_FUNCTION(as_read_expire_store, &ad->fifo_expire[BLK_RW_SYNC], 0, INT_MAX);
1450STORE_FUNCTION(as_write_expire_store,
1451 &ad->fifo_expire[BLK_RW_ASYNC], 0, INT_MAX);
Al Viroe572ec72006-03-18 22:27:18 -05001452STORE_FUNCTION(as_antic_expire_store, &ad->antic_expire, 0, INT_MAX);
1453STORE_FUNCTION(as_read_batch_expire_store,
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001454 &ad->batch_expire[BLK_RW_SYNC], 0, INT_MAX);
Al Viroe572ec72006-03-18 22:27:18 -05001455STORE_FUNCTION(as_write_batch_expire_store,
Jens Axboe1d6bfbd2009-04-08 11:02:08 +02001456 &ad->batch_expire[BLK_RW_ASYNC], 0, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457#undef STORE_FUNCTION
1458
Al Viroe572ec72006-03-18 22:27:18 -05001459#define AS_ATTR(name) \
1460 __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Al Viroe572ec72006-03-18 22:27:18 -05001462static struct elv_fs_entry as_attrs[] = {
1463 __ATTR_RO(est_time),
1464 AS_ATTR(read_expire),
1465 AS_ATTR(write_expire),
1466 AS_ATTR(antic_expire),
1467 AS_ATTR(read_batch_expire),
1468 AS_ATTR(write_batch_expire),
1469 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470};
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472static struct elevator_type iosched_as = {
1473 .ops = {
1474 .elevator_merge_fn = as_merge,
1475 .elevator_merged_fn = as_merged_request,
1476 .elevator_merge_req_fn = as_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02001477 .elevator_dispatch_fn = as_dispatch_request,
1478 .elevator_add_req_fn = as_add_request,
1479 .elevator_activate_req_fn = as_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 .elevator_deactivate_req_fn = as_deactivate_request,
1481 .elevator_queue_empty_fn = as_queue_empty,
1482 .elevator_completed_req_fn = as_completed_request,
Jens Axboee37f3462006-07-18 21:06:01 +02001483 .elevator_former_req_fn = elv_rb_former_request,
1484 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 .elevator_may_queue_fn = as_may_queue,
1486 .elevator_init_fn = as_init_queue,
1487 .elevator_exit_fn = as_exit_queue,
Al Viroe17a9482006-03-18 13:21:20 -05001488 .trim = as_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 },
1490
Al Viro3d1ab402006-03-18 18:35:43 -05001491 .elevator_attrs = as_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 .elevator_name = "anticipatory",
1493 .elevator_owner = THIS_MODULE,
1494};
1495
1496static int __init as_init(void)
1497{
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01001498 elv_register(&iosched_as);
1499
1500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
1503static void __exit as_exit(void)
1504{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001505 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 elv_unregister(&iosched_as);
Al Viro334e94d2006-03-18 15:05:53 -05001507 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02001508 /* ioc_gone's update must be visible before reading ioc_count */
1509 smp_wmb();
Jens Axboee4313dd2006-07-19 05:10:01 +02001510 if (elv_ioc_count_read(ioc_count))
Jens Axboe863fddc2008-05-29 09:35:22 +02001511 wait_for_completion(&all_gone);
Al Viro334e94d2006-03-18 15:05:53 -05001512 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515module_init(as_init);
1516module_exit(as_exit);
1517
1518MODULE_AUTHOR("Nick Piggin");
1519MODULE_LICENSE("GPL");
1520MODULE_DESCRIPTION("anticipatory IO scheduler");