blob: 4c6fafbba9330bf6d785efc7e511b93c936f998f [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
20#define REQ_SYNC 1
21#define REQ_ASYNC 0
22
23/*
24 * See Documentation/block/as-iosched.txt
25 */
26
27/*
28 * max time before a read is submitted.
29 */
30#define default_read_expire (HZ / 8)
31
32/*
33 * ditto for writes, these limits are not hard, even
34 * if the disk is capable of satisfying them.
35 */
36#define default_write_expire (HZ / 4)
37
38/*
39 * read_batch_expire describes how long we will allow a stream of reads to
40 * persist before looking to see whether it is time to switch over to writes.
41 */
42#define default_read_batch_expire (HZ / 2)
43
44/*
45 * write_batch_expire describes how long we want a stream of writes to run for.
46 * This is not a hard limit, but a target we set for the auto-tuning thingy.
47 * See, the problem is: we can send a lot of writes to disk cache / TCQ in
48 * a short amount of time...
49 */
50#define default_write_batch_expire (HZ / 8)
51
52/*
53 * max time we may wait to anticipate a read (default around 6ms)
54 */
55#define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
56
57/*
58 * Keep track of up to 20ms thinktimes. We can go as big as we like here,
59 * however huge values tend to interfere and not decay fast enough. A program
60 * might be in a non-io phase of operation. Waiting on user input for example,
61 * or doing a lengthy computation. A small penalty can be justified there, and
62 * will still catch out those processes that constantly have large thinktimes.
63 */
64#define MAX_THINKTIME (HZ/50UL)
65
66/* Bits in as_io_context.state */
67enum as_io_states {
Nick Pigginf5b3db02005-11-07 00:59:53 -080068 AS_TASK_RUNNING=0, /* Process has not exited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 AS_TASK_IOSTARTED, /* Process has started some IO */
70 AS_TASK_IORUNNING, /* Process has completed some IO */
71};
72
73enum anticipation_status {
74 ANTIC_OFF=0, /* Not anticipating (normal operation) */
75 ANTIC_WAIT_REQ, /* The last read has not yet completed */
76 ANTIC_WAIT_NEXT, /* Currently anticipating a request vs
77 last read (which has completed) */
78 ANTIC_FINISHED, /* Anticipating but have found a candidate
79 * or timed out */
80};
81
82struct as_data {
83 /*
84 * run time data
85 */
86
87 struct request_queue *q; /* the "owner" queue */
88
89 /*
90 * requests (as_rq s) are present on both sort_list and fifo_list
91 */
92 struct rb_root sort_list[2];
93 struct list_head fifo_list[2];
94
Jens Axboe8a8e6742006-07-18 21:07:29 +020095 struct request *next_rq[2]; /* next in sort order */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 sector_t last_sector[2]; /* last REQ_SYNC & REQ_ASYNC sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 unsigned long exit_prob; /* probability a task will exit while
99 being waited on */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800100 unsigned long exit_no_coop; /* probablility an exited task will
101 not be part of a later cooperating
102 request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long new_ttime_total; /* mean thinktime on new proc */
104 unsigned long new_ttime_mean;
105 u64 new_seek_total; /* mean seek on new proc */
106 sector_t new_seek_mean;
107
108 unsigned long current_batch_expires;
109 unsigned long last_check_fifo[2];
110 int changed_batch; /* 1: waiting for old batch to end */
111 int new_batch; /* 1: waiting on first read complete */
112 int batch_data_dir; /* current batch REQ_SYNC / REQ_ASYNC */
113 int write_batch_count; /* max # of reqs in a write batch */
114 int current_write_count; /* how many requests left this batch */
115 int write_batch_idled; /* has the write batch gone idle? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
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 */
123 int nr_dispatched;
124
125 /*
126 * settings that change how the i/o scheduler behaves
127 */
128 unsigned long fifo_expire[2];
129 unsigned long batch_expire[2];
130 unsigned long antic_expire;
131};
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
134 * per-request data.
135 */
136enum arq_state {
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
139 scheduler */
140 AS_RQ_DISPATCHED, /* On the dispatch list. It belongs to the
141 driver now */
142 AS_RQ_PRESCHED, /* Debug poisoning for requests being used */
143 AS_RQ_REMOVED,
144 AS_RQ_MERGED,
145 AS_RQ_POSTSCHED, /* when they shouldn't be */
146};
147
Jens Axboe8a8e6742006-07-18 21:07:29 +0200148#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Jens Axboee4313dd2006-07-19 05:10:01 +0200152static DEFINE_PER_CPU(unsigned long, ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -0500153static struct completion *ioc_gone;
Jens Axboe863fddc2008-05-29 09:35:22 +0200154static DEFINE_SPINLOCK(ioc_gone_lock);
Al Viro334e94d2006-03-18 15:05:53 -0500155
Jens Axboe8a8e6742006-07-18 21:07:29 +0200156static void as_move_to_dispatch(struct as_data *ad, struct request *rq);
Tejun Heoef9be1d2005-11-11 14:27:09 +0100157static void as_antic_stop(struct as_data *ad);
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
160 * IO Context helper functions
161 */
162
163/* Called to deallocate the as_io_context */
164static void free_as_io_context(struct as_io_context *aic)
165{
166 kfree(aic);
Jens Axboee4313dd2006-07-19 05:10:01 +0200167 elv_ioc_count_dec(ioc_count);
Jens Axboe863fddc2008-05-29 09:35:22 +0200168 if (ioc_gone) {
169 /*
170 * AS scheduler is exiting, grab exit lock and check
171 * the pending io context count. If it hits zero,
172 * complete ioc_gone and set it back to NULL.
173 */
174 spin_lock(&ioc_gone_lock);
175 if (ioc_gone && !elv_ioc_count_read(ioc_count)) {
176 complete(ioc_gone);
177 ioc_gone = NULL;
178 }
179 spin_unlock(&ioc_gone_lock);
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Al Viroe17a9482006-03-18 13:21:20 -0500183static void as_trim(struct io_context *ioc)
184{
Jens Axboe8bdd3f82008-02-01 09:44:28 +0100185 spin_lock_irq(&ioc->lock);
Al Viro334e94d2006-03-18 15:05:53 -0500186 if (ioc->aic)
187 free_as_io_context(ioc->aic);
Al Viroe17a9482006-03-18 13:21:20 -0500188 ioc->aic = NULL;
Jens Axboe8bdd3f82008-02-01 09:44:28 +0100189 spin_unlock_irq(&ioc->lock);
Al Viroe17a9482006-03-18 13:21:20 -0500190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/* Called when the task exits */
193static void exit_as_io_context(struct as_io_context *aic)
194{
195 WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
196 clear_bit(AS_TASK_RUNNING, &aic->state);
197}
198
199static struct as_io_context *alloc_as_io_context(void)
200{
201 struct as_io_context *ret;
202
203 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
204 if (ret) {
205 ret->dtor = free_as_io_context;
206 ret->exit = exit_as_io_context;
207 ret->state = 1 << AS_TASK_RUNNING;
208 atomic_set(&ret->nr_queued, 0);
209 atomic_set(&ret->nr_dispatched, 0);
210 spin_lock_init(&ret->lock);
211 ret->ttime_total = 0;
212 ret->ttime_samples = 0;
213 ret->ttime_mean = 0;
214 ret->seek_total = 0;
215 ret->seek_samples = 0;
216 ret->seek_mean = 0;
Jens Axboee4313dd2006-07-19 05:10:01 +0200217 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
220 return ret;
221}
222
223/*
224 * If the current task has no AS IO context then create one and initialise it.
225 * Then take a ref on the task's io context and return it.
226 */
Jens Axboeb5deef92006-07-19 23:39:40 +0200227static struct io_context *as_get_io_context(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Jens Axboeb5deef92006-07-19 23:39:40 +0200229 struct io_context *ioc = get_io_context(GFP_ATOMIC, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (ioc && !ioc->aic) {
231 ioc->aic = alloc_as_io_context();
232 if (!ioc->aic) {
233 put_io_context(ioc);
234 ioc = NULL;
235 }
236 }
237 return ioc;
238}
239
Jens Axboe8a8e6742006-07-18 21:07:29 +0200240static void as_put_io_context(struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +0200241{
242 struct as_io_context *aic;
243
Jens Axboe8a8e6742006-07-18 21:07:29 +0200244 if (unlikely(!RQ_IOC(rq)))
Jens Axboeb4878f22005-10-20 16:42:29 +0200245 return;
246
Jens Axboe8a8e6742006-07-18 21:07:29 +0200247 aic = RQ_IOC(rq)->aic;
Jens Axboeb4878f22005-10-20 16:42:29 +0200248
Jens Axboe8a8e6742006-07-18 21:07:29 +0200249 if (rq_is_sync(rq) && aic) {
Jens Axboe8bdd3f82008-02-01 09:44:28 +0100250 unsigned long flags;
251
252 spin_lock_irqsave(&aic->lock, flags);
Jens Axboeb4878f22005-10-20 16:42:29 +0200253 set_bit(AS_TASK_IORUNNING, &aic->state);
254 aic->last_end_request = jiffies;
Jens Axboe8bdd3f82008-02-01 09:44:28 +0100255 spin_unlock_irqrestore(&aic->lock, flags);
Jens Axboeb4878f22005-10-20 16:42:29 +0200256 }
257
Jens Axboe8a8e6742006-07-18 21:07:29 +0200258 put_io_context(RQ_IOC(rq));
Jens Axboeb4878f22005-10-20 16:42:29 +0200259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 * rb tree support functions
263 */
Jens Axboe9e2585a2006-07-28 09:26:13 +0200264#define RQ_RB_ROOT(ad, rq) (&(ad)->sort_list[rq_is_sync((rq))])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Jens Axboe8a8e6742006-07-18 21:07:29 +0200266static void as_add_rq_rb(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Jens Axboee37f3462006-07-18 21:06:01 +0200268 struct request *alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jens Axboe9e2585a2006-07-28 09:26:13 +0200270 while ((unlikely(alias = elv_rb_add(RQ_RB_ROOT(ad, rq), rq)))) {
Jens Axboe8a8e6742006-07-18 21:07:29 +0200271 as_move_to_dispatch(ad, alias);
Tejun Heoef9be1d2005-11-11 14:27:09 +0100272 as_antic_stop(ad);
273 }
274}
275
Jens Axboe8a8e6742006-07-18 21:07:29 +0200276static inline void as_del_rq_rb(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Jens Axboe9e2585a2006-07-28 09:26:13 +0200278 elv_rb_del(RQ_RB_ROOT(ad, rq), rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/*
282 * IO Scheduler proper
283 */
284
285#define MAXBACK (1024 * 1024) /*
286 * Maximum distance the disk will go backward
287 * for a request.
288 */
289
290#define BACK_PENALTY 2
291
292/*
293 * as_choose_req selects the preferred one of two requests of the same data_dir
294 * ignoring time - eg. timeouts, which is the job of as_dispatch_request
295 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200296static struct request *
297as_choose_req(struct as_data *ad, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 int data_dir;
300 sector_t last, s1, s2, d1, d2;
301 int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */
302 const sector_t maxback = MAXBACK;
303
Jens Axboe8a8e6742006-07-18 21:07:29 +0200304 if (rq1 == NULL || rq1 == rq2)
305 return rq2;
306 if (rq2 == NULL)
307 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Jens Axboe8a8e6742006-07-18 21:07:29 +0200309 data_dir = rq_is_sync(rq1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 last = ad->last_sector[data_dir];
Jens Axboe8a8e6742006-07-18 21:07:29 +0200312 s1 = rq1->sector;
313 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Jens Axboe8a8e6742006-07-18 21:07:29 +0200315 BUG_ON(data_dir != rq_is_sync(rq2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /*
318 * Strict one way elevator _except_ in the case where we allow
319 * short backward seeks which are biased as twice the cost of a
320 * similar forward seek.
321 */
322 if (s1 >= last)
323 d1 = s1 - last;
324 else if (s1+maxback >= last)
325 d1 = (last - s1)*BACK_PENALTY;
326 else {
327 r1_wrap = 1;
328 d1 = 0; /* shut up, gcc */
329 }
330
331 if (s2 >= last)
332 d2 = s2 - last;
333 else if (s2+maxback >= last)
334 d2 = (last - s2)*BACK_PENALTY;
335 else {
336 r2_wrap = 1;
337 d2 = 0;
338 }
339
340 /* Found required data */
341 if (!r1_wrap && r2_wrap)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200342 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 else if (!r2_wrap && r1_wrap)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200344 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 else if (r1_wrap && r2_wrap) {
346 /* both behind the head */
347 if (s1 <= s2)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200348 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 else
Jens Axboe8a8e6742006-07-18 21:07:29 +0200350 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
353 /* Both requests in front of the head */
354 if (d1 < d2)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200355 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 else if (d2 < d1)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200357 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 else {
359 if (s1 >= s2)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200360 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 else
Jens Axboe8a8e6742006-07-18 21:07:29 +0200362 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364}
365
366/*
Jens Axboe8a8e6742006-07-18 21:07:29 +0200367 * as_find_next_rq finds the next request after @prev in elevator order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * this with as_choose_req form the basis for how the scheduler chooses
369 * what request to process next. Anticipation works on top of this.
370 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200371static struct request *
372as_find_next_rq(struct as_data *ad, struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct rb_node *rbnext = rb_next(&last->rb_node);
375 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe8a8e6742006-07-18 21:07:29 +0200376 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Jens Axboee37f3462006-07-18 21:06:01 +0200378 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (rbprev)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200381 prev = rb_entry_rq(rbprev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 if (rbnext)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200384 next = rb_entry_rq(rbnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 else {
Jens Axboe9e2585a2006-07-28 09:26:13 +0200386 const int data_dir = rq_is_sync(last);
Jens Axboee37f3462006-07-18 21:06:01 +0200387
388 rbnext = rb_first(&ad->sort_list[data_dir]);
389 if (rbnext && rbnext != &last->rb_node)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200390 next = rb_entry_rq(rbnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
Jens Axboee37f3462006-07-18 21:06:01 +0200393 return as_choose_req(ad, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/*
397 * anticipatory scheduling functions follow
398 */
399
400/*
401 * as_antic_expired tells us when we have anticipated too long.
402 * The funny "absolute difference" math on the elapsed time is to handle
403 * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
404 */
405static int as_antic_expired(struct as_data *ad)
406{
407 long delta_jif;
408
409 delta_jif = jiffies - ad->antic_start;
410 if (unlikely(delta_jif < 0))
411 delta_jif = -delta_jif;
412 if (delta_jif < ad->antic_expire)
413 return 0;
414
415 return 1;
416}
417
418/*
419 * as_antic_waitnext starts anticipating that a nice request will soon be
420 * submitted. See also as_antic_waitreq
421 */
422static void as_antic_waitnext(struct as_data *ad)
423{
424 unsigned long timeout;
425
426 BUG_ON(ad->antic_status != ANTIC_OFF
427 && ad->antic_status != ANTIC_WAIT_REQ);
428
429 timeout = ad->antic_start + ad->antic_expire;
430
431 mod_timer(&ad->antic_timer, timeout);
432
433 ad->antic_status = ANTIC_WAIT_NEXT;
434}
435
436/*
437 * as_antic_waitreq starts anticipating. We don't start timing the anticipation
438 * until the request that we're anticipating on has finished. This means we
439 * are timing from when the candidate process wakes up hopefully.
440 */
441static void as_antic_waitreq(struct as_data *ad)
442{
443 BUG_ON(ad->antic_status == ANTIC_FINISHED);
444 if (ad->antic_status == ANTIC_OFF) {
445 if (!ad->io_context || ad->ioc_finished)
446 as_antic_waitnext(ad);
447 else
448 ad->antic_status = ANTIC_WAIT_REQ;
449 }
450}
451
452/*
453 * This is called directly by the functions in this file to stop anticipation.
454 * We kill the timer and schedule a call to the request_fn asap.
455 */
456static void as_antic_stop(struct as_data *ad)
457{
458 int status = ad->antic_status;
459
460 if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
461 if (status == ANTIC_WAIT_NEXT)
462 del_timer(&ad->antic_timer);
463 ad->antic_status = ANTIC_FINISHED;
464 /* see as_work_handler */
Jens Axboe18887ad2008-07-28 13:08:45 +0200465 kblockd_schedule_work(ad->q, &ad->antic_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467}
468
469/*
470 * as_antic_timeout is the timer function set by as_antic_waitnext.
471 */
472static void as_antic_timeout(unsigned long data)
473{
474 struct request_queue *q = (struct request_queue *)data;
475 struct as_data *ad = q->elevator->elevator_data;
476 unsigned long flags;
477
478 spin_lock_irqsave(q->queue_lock, flags);
479 if (ad->antic_status == ANTIC_WAIT_REQ
480 || ad->antic_status == ANTIC_WAIT_NEXT) {
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100481 struct as_io_context *aic;
482 spin_lock(&ad->io_context->lock);
483 aic = ad->io_context->aic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 ad->antic_status = ANTIC_FINISHED;
Jens Axboe18887ad2008-07-28 13:08:45 +0200486 kblockd_schedule_work(q, &ad->antic_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (aic->ttime_samples == 0) {
Nick Pigginf5b3db02005-11-07 00:59:53 -0800489 /* process anticipated on has exited or timed out*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 ad->exit_prob = (7*ad->exit_prob + 256)/8;
491 }
Nick Pigginf5b3db02005-11-07 00:59:53 -0800492 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
493 /* process not "saved" by a cooperating request */
494 ad->exit_no_coop = (7*ad->exit_no_coop + 256)/8;
495 }
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100496 spin_unlock(&ad->io_context->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 spin_unlock_irqrestore(q->queue_lock, flags);
499}
500
Nick Pigginf5b3db02005-11-07 00:59:53 -0800501static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic,
502 unsigned long ttime)
503{
504 /* fixed point: 1.0 == 1<<8 */
505 if (aic->ttime_samples == 0) {
506 ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
507 ad->new_ttime_mean = ad->new_ttime_total / 256;
508
509 ad->exit_prob = (7*ad->exit_prob)/8;
510 }
511 aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
512 aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
513 aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
514}
515
516static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic,
517 sector_t sdist)
518{
519 u64 total;
520
521 if (aic->seek_samples == 0) {
522 ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
523 ad->new_seek_mean = ad->new_seek_total / 256;
524 }
525
526 /*
527 * Don't allow the seek distance to get too large from the
528 * odd fragment, pagein, etc
529 */
530 if (aic->seek_samples <= 60) /* second&third seek */
531 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
532 else
533 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
534
535 aic->seek_samples = (7*aic->seek_samples + 256) / 8;
536 aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
537 total = aic->seek_total + (aic->seek_samples/2);
538 do_div(total, aic->seek_samples);
539 aic->seek_mean = (sector_t)total;
540}
541
542/*
543 * as_update_iohist keeps a decaying histogram of IO thinktimes, and
544 * updates @aic->ttime_mean based on that. It is called when a new
545 * request is queued.
546 */
547static void as_update_iohist(struct as_data *ad, struct as_io_context *aic,
548 struct request *rq)
549{
Jens Axboe9e2585a2006-07-28 09:26:13 +0200550 int data_dir = rq_is_sync(rq);
Nick Pigginf5b3db02005-11-07 00:59:53 -0800551 unsigned long thinktime = 0;
552 sector_t seek_dist;
553
554 if (aic == NULL)
555 return;
556
557 if (data_dir == REQ_SYNC) {
558 unsigned long in_flight = atomic_read(&aic->nr_queued)
559 + atomic_read(&aic->nr_dispatched);
560 spin_lock(&aic->lock);
561 if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
562 test_bit(AS_TASK_IOSTARTED, &aic->state)) {
563 /* Calculate read -> read thinktime */
564 if (test_bit(AS_TASK_IORUNNING, &aic->state)
565 && in_flight == 0) {
566 thinktime = jiffies - aic->last_end_request;
567 thinktime = min(thinktime, MAX_THINKTIME-1);
568 }
569 as_update_thinktime(ad, aic, thinktime);
570
571 /* Calculate read -> read seek distance */
572 if (aic->last_request_pos < rq->sector)
573 seek_dist = rq->sector - aic->last_request_pos;
574 else
575 seek_dist = aic->last_request_pos - rq->sector;
576 as_update_seekdist(ad, aic, seek_dist);
577 }
578 aic->last_request_pos = rq->sector + rq->nr_sectors;
579 set_bit(AS_TASK_IOSTARTED, &aic->state);
580 spin_unlock(&aic->lock);
581 }
582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/*
585 * as_close_req decides if one request is considered "close" to the
586 * previous one issued.
587 */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800588static int as_close_req(struct as_data *ad, struct as_io_context *aic,
Jens Axboe8a8e6742006-07-18 21:07:29 +0200589 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Nick Pigginc6a632a2007-05-08 00:26:34 -0700591 unsigned long delay; /* jiffies */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 sector_t last = ad->last_sector[ad->batch_data_dir];
Jens Axboe8a8e6742006-07-18 21:07:29 +0200593 sector_t next = rq->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 sector_t delta; /* acceptable close offset (in sectors) */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800595 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
598 delay = 0;
599 else
Nick Pigginc6a632a2007-05-08 00:26:34 -0700600 delay = jiffies - ad->antic_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Nick Pigginf5b3db02005-11-07 00:59:53 -0800602 if (delay == 0)
603 delta = 8192;
Nick Pigginc6a632a2007-05-08 00:26:34 -0700604 else if (delay <= (20 * HZ / 1000) && delay <= ad->antic_expire)
Nick Pigginf5b3db02005-11-07 00:59:53 -0800605 delta = 8192 << delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 else
607 return 1;
608
Nick Pigginf5b3db02005-11-07 00:59:53 -0800609 if ((last <= next + (delta>>1)) && (next <= last + delta))
610 return 1;
611
612 if (last < next)
613 s = next - last;
614 else
615 s = last - next;
616
617 if (aic->seek_samples == 0) {
618 /*
619 * Process has just started IO. Use past statistics to
620 * gauge success possibility
621 */
622 if (ad->new_seek_mean > s) {
623 /* this request is better than what we're expecting */
624 return 1;
625 }
626
627 } else {
628 if (aic->seek_mean > s) {
629 /* this request is better than what we're expecting */
630 return 1;
631 }
632 }
633
634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637/*
638 * as_can_break_anticipation returns true if we have been anticipating this
639 * request.
640 *
641 * It also returns true if the process against which we are anticipating
642 * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
643 * dispatch it ASAP, because we know that application will not be submitting
644 * any new reads.
645 *
Nick Pigginf5b3db02005-11-07 00:59:53 -0800646 * If the task which has submitted the request has exited, break anticipation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 *
648 * If this task has queued some other IO, do not enter enticipation.
649 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200650static int as_can_break_anticipation(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 struct io_context *ioc;
653 struct as_io_context *aic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 ioc = ad->io_context;
656 BUG_ON(!ioc);
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100657 spin_lock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Jens Axboe8a8e6742006-07-18 21:07:29 +0200659 if (rq && ioc == RQ_IOC(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* request from same process */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100661 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 1;
663 }
664
665 if (ad->ioc_finished && as_antic_expired(ad)) {
666 /*
667 * In this situation status should really be FINISHED,
668 * however the timer hasn't had the chance to run yet.
669 */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100670 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return 1;
672 }
673
674 aic = ioc->aic;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100675 if (!aic) {
676 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return 0;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (atomic_read(&aic->nr_queued) > 0) {
681 /* process has more requests queued */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100682 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 1;
684 }
685
686 if (atomic_read(&aic->nr_dispatched) > 0) {
687 /* process has more requests dispatched */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100688 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 1;
690 }
691
Jens Axboe8a8e6742006-07-18 21:07:29 +0200692 if (rq && rq_is_sync(rq) && as_close_req(ad, aic, rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /*
694 * Found a close request that is not one of ours.
695 *
Nick Pigginf5b3db02005-11-07 00:59:53 -0800696 * This makes close requests from another process update
697 * our IO history. Is generally useful when there are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 * two or more cooperating processes working in the same
699 * area.
700 */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800701 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
702 if (aic->ttime_samples == 0)
703 ad->exit_prob = (7*ad->exit_prob + 256)/8;
704
705 ad->exit_no_coop = (7*ad->exit_no_coop)/8;
706 }
707
Jens Axboe8a8e6742006-07-18 21:07:29 +0200708 as_update_iohist(ad, aic, rq);
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100709 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return 1;
711 }
712
Nick Pigginf5b3db02005-11-07 00:59:53 -0800713 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
714 /* process anticipated on has exited */
715 if (aic->ttime_samples == 0)
716 ad->exit_prob = (7*ad->exit_prob + 256)/8;
717
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100718 if (ad->exit_no_coop > 128) {
719 spin_unlock(&ioc->lock);
Nick Pigginf5b3db02005-11-07 00:59:53 -0800720 return 1;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100721 }
Nick Pigginf5b3db02005-11-07 00:59:53 -0800722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if (aic->ttime_samples == 0) {
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100725 if (ad->new_ttime_mean > ad->antic_expire) {
726 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return 1;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100728 }
729 if (ad->exit_prob * ad->exit_no_coop > 128*256) {
730 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return 1;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 } else if (aic->ttime_mean > ad->antic_expire) {
734 /* the process thinks too much between requests */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100735 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return 1;
737 }
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100738 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return 0;
740}
741
742/*
Jens Axboe8a8e6742006-07-18 21:07:29 +0200743 * as_can_anticipate indicates whether we should either run rq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * or keep anticipating a better request.
745 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200746static int as_can_anticipate(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Jens Axboea68bbdd2008-09-24 13:03:33 +0200748 /*
749 * SSD device without seek penalty, disable idling
750 */
751 if (blk_queue_nonrot(ad->q))
752 return 0;
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (!ad->io_context)
755 /*
756 * Last request submitted was a write
757 */
758 return 0;
759
760 if (ad->antic_status == ANTIC_FINISHED)
761 /*
762 * Don't restart if we have just finished. Run the next request
763 */
764 return 0;
765
Jens Axboe8a8e6742006-07-18 21:07:29 +0200766 if (as_can_break_anticipation(ad, rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /*
768 * This request is a good candidate. Don't keep anticipating,
769 * run it.
770 */
771 return 0;
772
773 /*
774 * OK from here, we haven't finished, and don't have a decent request!
775 * Status is either ANTIC_OFF so start waiting,
776 * ANTIC_WAIT_REQ so continue waiting for request to finish
777 * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
779
780 return 1;
781}
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783/*
Jens Axboe8a8e6742006-07-18 21:07:29 +0200784 * as_update_rq must be called whenever a request (rq) is added to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 * the sort_list. This function keeps caches up to date, and checks if the
786 * request might be one we are "anticipating"
787 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200788static void as_update_rq(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Jens Axboe8a8e6742006-07-18 21:07:29 +0200790 const int data_dir = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Jens Axboe8a8e6742006-07-18 21:07:29 +0200792 /* keep the next_rq cache up to date */
793 ad->next_rq[data_dir] = as_choose_req(ad, rq, ad->next_rq[data_dir]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 /*
796 * have we been anticipating this request?
797 * or does it come from the same process as the one we are anticipating
798 * for?
799 */
800 if (ad->antic_status == ANTIC_WAIT_REQ
801 || ad->antic_status == ANTIC_WAIT_NEXT) {
Jens Axboe8a8e6742006-07-18 21:07:29 +0200802 if (as_can_break_anticipation(ad, rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 as_antic_stop(ad);
804 }
805}
806
807/*
808 * Gathers timings and resizes the write batch automatically
809 */
810static void update_write_batch(struct as_data *ad)
811{
812 unsigned long batch = ad->batch_expire[REQ_ASYNC];
813 long write_time;
814
815 write_time = (jiffies - ad->current_batch_expires) + batch;
816 if (write_time < 0)
817 write_time = 0;
818
819 if (write_time > batch && !ad->write_batch_idled) {
820 if (write_time > batch * 3)
821 ad->write_batch_count /= 2;
822 else
823 ad->write_batch_count--;
824 } else if (write_time < batch && ad->current_write_count == 0) {
825 if (batch > write_time * 3)
826 ad->write_batch_count *= 2;
827 else
828 ad->write_batch_count++;
829 }
830
831 if (ad->write_batch_count < 1)
832 ad->write_batch_count = 1;
833}
834
835/*
836 * as_completed_request is to be called when a request has completed and
837 * returned something to the requesting process, be it an error or data.
838 */
Jens Axboe165125e2007-07-24 09:28:11 +0200839static void as_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 WARN_ON(!list_empty(&rq->queuelist));
844
Jens Axboe8a8e6742006-07-18 21:07:29 +0200845 if (RQ_STATE(rq) != AS_RQ_REMOVED) {
Arjan van de Ven12e00362008-07-25 19:45:38 -0700846 WARN(1, "rq->state %d\n", RQ_STATE(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto out;
848 }
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (ad->changed_batch && ad->nr_dispatched == 1) {
Divyesh Shahd585d0b2008-06-16 18:37:08 +0200851 ad->current_batch_expires = jiffies +
852 ad->batch_expire[ad->batch_data_dir];
Jens Axboe18887ad2008-07-28 13:08:45 +0200853 kblockd_schedule_work(q, &ad->antic_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 ad->changed_batch = 0;
855
856 if (ad->batch_data_dir == REQ_SYNC)
857 ad->new_batch = 1;
858 }
859 WARN_ON(ad->nr_dispatched == 0);
860 ad->nr_dispatched--;
861
862 /*
863 * Start counting the batch from when a request of that direction is
864 * actually serviced. This should help devices with big TCQ windows
865 * and writeback caches
866 */
Jens Axboe9e2585a2006-07-28 09:26:13 +0200867 if (ad->new_batch && ad->batch_data_dir == rq_is_sync(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 update_write_batch(ad);
869 ad->current_batch_expires = jiffies +
870 ad->batch_expire[REQ_SYNC];
871 ad->new_batch = 0;
872 }
873
Jens Axboe8a8e6742006-07-18 21:07:29 +0200874 if (ad->io_context == RQ_IOC(rq) && ad->io_context) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 ad->antic_start = jiffies;
876 ad->ioc_finished = 1;
877 if (ad->antic_status == ANTIC_WAIT_REQ) {
878 /*
879 * We were waiting on this request, now anticipate
880 * the next one
881 */
882 as_antic_waitnext(ad);
883 }
884 }
885
Jens Axboe8a8e6742006-07-18 21:07:29 +0200886 as_put_io_context(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887out:
Jens Axboe8a8e6742006-07-18 21:07:29 +0200888 RQ_SET_STATE(rq, AS_RQ_POSTSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891/*
892 * as_remove_queued_request removes a request from the pre dispatch queue
893 * without updating refcounts. It is expected the caller will drop the
894 * reference unless it replaces the request at somepart of the elevator
895 * (ie. the dispatch queue)
896 */
Jens Axboe165125e2007-07-24 09:28:11 +0200897static void as_remove_queued_request(struct request_queue *q,
898 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Jens Axboe9e2585a2006-07-28 09:26:13 +0200900 const int data_dir = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 struct as_data *ad = q->elevator->elevator_data;
Jens Axboe8a8e6742006-07-18 21:07:29 +0200902 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Jens Axboe8a8e6742006-07-18 21:07:29 +0200904 WARN_ON(RQ_STATE(rq) != AS_RQ_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Jens Axboe8a8e6742006-07-18 21:07:29 +0200906 ioc = RQ_IOC(rq);
907 if (ioc && ioc->aic) {
908 BUG_ON(!atomic_read(&ioc->aic->nr_queued));
909 atomic_dec(&ioc->aic->nr_queued);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
912 /*
Jens Axboe8a8e6742006-07-18 21:07:29 +0200913 * Update the "next_rq" cache if we are about to remove its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 * entry
915 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200916 if (ad->next_rq[data_dir] == rq)
917 ad->next_rq[data_dir] = as_find_next_rq(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Jens Axboed4f2f462006-07-13 09:12:14 +0200919 rq_fifo_clear(rq);
Jens Axboe8a8e6742006-07-18 21:07:29 +0200920 as_del_rq_rb(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923/*
Aaron Carroll8896f3c2007-12-05 21:06:50 +1100924 * as_fifo_expired returns 0 if there are no expired requests on the fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 * 1 otherwise. It is ratelimited so that we only perform the check once per
926 * `fifo_expire' interval. Otherwise a large number of expired requests
927 * would create a hopeless seekstorm.
928 *
929 * See as_antic_expired comment.
930 */
931static int as_fifo_expired(struct as_data *ad, int adir)
932{
Jens Axboed4f2f462006-07-13 09:12:14 +0200933 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 long delta_jif;
935
936 delta_jif = jiffies - ad->last_check_fifo[adir];
937 if (unlikely(delta_jif < 0))
938 delta_jif = -delta_jif;
939 if (delta_jif < ad->fifo_expire[adir])
940 return 0;
941
942 ad->last_check_fifo[adir] = jiffies;
943
944 if (list_empty(&ad->fifo_list[adir]))
945 return 0;
946
Jens Axboed4f2f462006-07-13 09:12:14 +0200947 rq = rq_entry_fifo(ad->fifo_list[adir].next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Jens Axboed4f2f462006-07-13 09:12:14 +0200949 return time_after(jiffies, rq_fifo_time(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952/*
953 * as_batch_expired returns true if the current batch has expired. A batch
954 * is a set of reads or a set of writes.
955 */
956static inline int as_batch_expired(struct as_data *ad)
957{
958 if (ad->changed_batch || ad->new_batch)
959 return 0;
960
961 if (ad->batch_data_dir == REQ_SYNC)
962 /* TODO! add a check so a complete fifo gets written? */
963 return time_after(jiffies, ad->current_batch_expires);
964
965 return time_after(jiffies, ad->current_batch_expires)
966 || ad->current_write_count == 0;
967}
968
969/*
970 * move an entry to dispatch queue
971 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200972static void as_move_to_dispatch(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Jens Axboe9e2585a2006-07-28 09:26:13 +0200974 const int data_dir = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Jens Axboee37f3462006-07-18 21:06:01 +0200976 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 as_antic_stop(ad);
979 ad->antic_status = ANTIC_OFF;
980
981 /*
982 * This has to be set in order to be correctly updated by
Jens Axboe8a8e6742006-07-18 21:07:29 +0200983 * as_find_next_rq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 */
985 ad->last_sector[data_dir] = rq->sector + rq->nr_sectors;
986
987 if (data_dir == REQ_SYNC) {
Jens Axboe8a8e6742006-07-18 21:07:29 +0200988 struct io_context *ioc = RQ_IOC(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /* In case we have to anticipate after this */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200990 copy_io_context(&ad->io_context, &ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 } else {
992 if (ad->io_context) {
993 put_io_context(ad->io_context);
994 ad->io_context = NULL;
995 }
996
997 if (ad->current_write_count != 0)
998 ad->current_write_count--;
999 }
1000 ad->ioc_finished = 0;
1001
Jens Axboe8a8e6742006-07-18 21:07:29 +02001002 ad->next_rq[data_dir] = as_find_next_rq(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 /*
1005 * take it off the sort and fifo list, add to dispatch queue
1006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 as_remove_queued_request(ad->q, rq);
Jens Axboe8a8e6742006-07-18 21:07:29 +02001008 WARN_ON(RQ_STATE(rq) != AS_RQ_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Jens Axboeb4878f22005-10-20 16:42:29 +02001010 elv_dispatch_sort(ad->q, rq);
1011
Jens Axboe8a8e6742006-07-18 21:07:29 +02001012 RQ_SET_STATE(rq, AS_RQ_DISPATCHED);
1013 if (RQ_IOC(rq) && RQ_IOC(rq)->aic)
1014 atomic_inc(&RQ_IOC(rq)->aic->nr_dispatched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 ad->nr_dispatched++;
1016}
1017
1018/*
1019 * as_dispatch_request selects the best request according to
1020 * read/write expire, batch expire, etc, and moves it to the dispatch
1021 * queue. Returns 1 if a request was found, 0 otherwise.
1022 */
Jens Axboe165125e2007-07-24 09:28:11 +02001023static int as_dispatch_request(struct request_queue *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Jens Axboeb4878f22005-10-20 16:42:29 +02001025 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]);
1027 const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]);
Jens Axboe8a8e6742006-07-18 21:07:29 +02001028 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Jens Axboeb4878f22005-10-20 16:42:29 +02001030 if (unlikely(force)) {
1031 /*
1032 * Forced dispatch, accounting is useless. Reset
1033 * accounting states and dump fifo_lists. Note that
1034 * batch_data_dir is reset to REQ_SYNC to avoid
1035 * screwing write batch accounting as write batch
1036 * accounting occurs on W->R transition.
1037 */
1038 int dispatched = 0;
1039
1040 ad->batch_data_dir = REQ_SYNC;
1041 ad->changed_batch = 0;
1042 ad->new_batch = 0;
1043
Jens Axboe8a8e6742006-07-18 21:07:29 +02001044 while (ad->next_rq[REQ_SYNC]) {
1045 as_move_to_dispatch(ad, ad->next_rq[REQ_SYNC]);
Jens Axboeb4878f22005-10-20 16:42:29 +02001046 dispatched++;
1047 }
1048 ad->last_check_fifo[REQ_SYNC] = jiffies;
1049
Jens Axboe8a8e6742006-07-18 21:07:29 +02001050 while (ad->next_rq[REQ_ASYNC]) {
1051 as_move_to_dispatch(ad, ad->next_rq[REQ_ASYNC]);
Jens Axboeb4878f22005-10-20 16:42:29 +02001052 dispatched++;
1053 }
1054 ad->last_check_fifo[REQ_ASYNC] = jiffies;
1055
1056 return dispatched;
1057 }
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /* Signal that the write batch was uncontended, so we can't time it */
1060 if (ad->batch_data_dir == REQ_ASYNC && !reads) {
1061 if (ad->current_write_count == 0 || !writes)
1062 ad->write_batch_idled = 1;
1063 }
1064
1065 if (!(reads || writes)
1066 || ad->antic_status == ANTIC_WAIT_REQ
1067 || ad->antic_status == ANTIC_WAIT_NEXT
1068 || ad->changed_batch)
1069 return 0;
1070
Nick Pigginf5b3db02005-11-07 00:59:53 -08001071 if (!(reads && writes && as_batch_expired(ad))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /*
1073 * batch is still running or no reads or no writes
1074 */
Jens Axboe8a8e6742006-07-18 21:07:29 +02001075 rq = ad->next_rq[ad->batch_data_dir];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) {
1078 if (as_fifo_expired(ad, REQ_SYNC))
1079 goto fifo_expired;
1080
Jens Axboe8a8e6742006-07-18 21:07:29 +02001081 if (as_can_anticipate(ad, rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 as_antic_waitreq(ad);
1083 return 0;
1084 }
1085 }
1086
Jens Axboe8a8e6742006-07-18 21:07:29 +02001087 if (rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /* we have a "next request" */
1089 if (reads && !writes)
1090 ad->current_batch_expires =
1091 jiffies + ad->batch_expire[REQ_SYNC];
1092 goto dispatch_request;
1093 }
1094 }
1095
1096 /*
1097 * at this point we are not running a batch. select the appropriate
1098 * data direction (read / write)
1099 */
1100
1101 if (reads) {
Jens Axboedd67d052006-06-21 09:36:18 +02001102 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_SYNC]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 if (writes && ad->batch_data_dir == REQ_SYNC)
1105 /*
1106 * Last batch was a read, switch to writes
1107 */
1108 goto dispatch_writes;
1109
1110 if (ad->batch_data_dir == REQ_ASYNC) {
1111 WARN_ON(ad->new_batch);
1112 ad->changed_batch = 1;
1113 }
1114 ad->batch_data_dir = REQ_SYNC;
Jens Axboe8a8e6742006-07-18 21:07:29 +02001115 rq = rq_entry_fifo(ad->fifo_list[REQ_SYNC].next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 ad->last_check_fifo[ad->batch_data_dir] = jiffies;
1117 goto dispatch_request;
1118 }
1119
1120 /*
1121 * the last batch was a read
1122 */
1123
1124 if (writes) {
1125dispatch_writes:
Jens Axboedd67d052006-06-21 09:36:18 +02001126 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_ASYNC]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (ad->batch_data_dir == REQ_SYNC) {
1129 ad->changed_batch = 1;
1130
1131 /*
1132 * new_batch might be 1 when the queue runs out of
1133 * reads. A subsequent submission of a write might
1134 * cause a change of batch before the read is finished.
1135 */
1136 ad->new_batch = 0;
1137 }
1138 ad->batch_data_dir = REQ_ASYNC;
1139 ad->current_write_count = ad->write_batch_count;
1140 ad->write_batch_idled = 0;
Aaron Carroll49565122007-12-05 21:07:07 +11001141 rq = rq_entry_fifo(ad->fifo_list[REQ_ASYNC].next);
1142 ad->last_check_fifo[REQ_ASYNC] = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 goto dispatch_request;
1144 }
1145
1146 BUG();
1147 return 0;
1148
1149dispatch_request:
1150 /*
1151 * If a request has expired, service it.
1152 */
1153
1154 if (as_fifo_expired(ad, ad->batch_data_dir)) {
1155fifo_expired:
Jens Axboe8a8e6742006-07-18 21:07:29 +02001156 rq = rq_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158
1159 if (ad->changed_batch) {
1160 WARN_ON(ad->new_batch);
1161
1162 if (ad->nr_dispatched)
1163 return 0;
1164
1165 if (ad->batch_data_dir == REQ_ASYNC)
1166 ad->current_batch_expires = jiffies +
1167 ad->batch_expire[REQ_ASYNC];
1168 else
1169 ad->new_batch = 1;
1170
1171 ad->changed_batch = 0;
1172 }
1173
1174 /*
Jens Axboe8a8e6742006-07-18 21:07:29 +02001175 * rq is the selected appropriate request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
Jens Axboe8a8e6742006-07-18 21:07:29 +02001177 as_move_to_dispatch(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 return 1;
1180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182/*
Jens Axboe8a8e6742006-07-18 21:07:29 +02001183 * add rq to rbtree and fifo
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 */
Jens Axboe165125e2007-07-24 09:28:11 +02001185static void as_add_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Jens Axboeb4878f22005-10-20 16:42:29 +02001187 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 int data_dir;
1189
Jens Axboe8a8e6742006-07-18 21:07:29 +02001190 RQ_SET_STATE(rq, AS_RQ_NEW);
Jens Axboeb4878f22005-10-20 16:42:29 +02001191
Jens Axboe9e2585a2006-07-28 09:26:13 +02001192 data_dir = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Jens Axboeb5deef92006-07-19 23:39:40 +02001194 rq->elevator_private = as_get_io_context(q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Jens Axboe8a8e6742006-07-18 21:07:29 +02001196 if (RQ_IOC(rq)) {
1197 as_update_iohist(ad, RQ_IOC(rq)->aic, rq);
1198 atomic_inc(&RQ_IOC(rq)->aic->nr_queued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200
Jens Axboe8a8e6742006-07-18 21:07:29 +02001201 as_add_rq_rb(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Tejun Heoef9be1d2005-11-11 14:27:09 +01001203 /*
Aaron Carroll8896f3c2007-12-05 21:06:50 +11001204 * set expire time and add to fifo list
Tejun Heoef9be1d2005-11-11 14:27:09 +01001205 */
Jens Axboed4f2f462006-07-13 09:12:14 +02001206 rq_set_fifo_time(rq, jiffies + ad->fifo_expire[data_dir]);
1207 list_add_tail(&rq->queuelist, &ad->fifo_list[data_dir]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Jens Axboe8a8e6742006-07-18 21:07:29 +02001209 as_update_rq(ad, rq); /* keep state machine up to date */
1210 RQ_SET_STATE(rq, AS_RQ_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
Jens Axboe165125e2007-07-24 09:28:11 +02001213static void as_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001214{
Jens Axboe8a8e6742006-07-18 21:07:29 +02001215 WARN_ON(RQ_STATE(rq) != AS_RQ_DISPATCHED);
1216 RQ_SET_STATE(rq, AS_RQ_REMOVED);
1217 if (RQ_IOC(rq) && RQ_IOC(rq)->aic)
1218 atomic_dec(&RQ_IOC(rq)->aic->nr_dispatched);
Jens Axboeb4878f22005-10-20 16:42:29 +02001219}
1220
Jens Axboe165125e2007-07-24 09:28:11 +02001221static void as_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Jens Axboe8a8e6742006-07-18 21:07:29 +02001223 WARN_ON(RQ_STATE(rq) != AS_RQ_REMOVED);
1224 RQ_SET_STATE(rq, AS_RQ_DISPATCHED);
1225 if (RQ_IOC(rq) && RQ_IOC(rq)->aic)
1226 atomic_inc(&RQ_IOC(rq)->aic->nr_dispatched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
1229/*
1230 * as_queue_empty tells us if there are requests left in the device. It may
1231 * not be the case that a driver can get the next request even if the queue
1232 * is not empty - it is used in the block layer to check for plugging and
1233 * merging opportunities
1234 */
Jens Axboe165125e2007-07-24 09:28:11 +02001235static int as_queue_empty(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 struct as_data *ad = q->elevator->elevator_data;
1238
Jens Axboeb4878f22005-10-20 16:42:29 +02001239 return list_empty(&ad->fifo_list[REQ_ASYNC])
1240 && list_empty(&ad->fifo_list[REQ_SYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243static int
Jens Axboe165125e2007-07-24 09:28:11 +02001244as_merge(struct request_queue *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct as_data *ad = q->elevator->elevator_data;
1247 sector_t rb_key = bio->bi_sector + bio_sectors(bio);
1248 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /*
1251 * check for front merge
1252 */
Jens Axboee37f3462006-07-18 21:06:01 +02001253 __rq = elv_rb_find(&ad->sort_list[bio_data_dir(bio)], rb_key);
Jens Axboe98170642006-07-28 09:23:08 +02001254 if (__rq && elv_rq_merge_ok(__rq, bio)) {
1255 *req = __rq;
1256 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258
1259 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
Jens Axboe165125e2007-07-24 09:28:11 +02001262static void as_merged_request(struct request_queue *q, struct request *req,
1263 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 * if the merge was a front merge, we need to reposition request
1269 */
Jens Axboee37f3462006-07-18 21:06:01 +02001270 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe8a8e6742006-07-18 21:07:29 +02001271 as_del_rq_rb(ad, req);
1272 as_add_rq_rb(ad, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /*
1274 * Note! At this stage of this and the next function, our next
1275 * request may not be optimal - eg the request may have "grown"
1276 * behind the disk head. We currently don't bother adjusting.
1277 */
1278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
Jens Axboe165125e2007-07-24 09:28:11 +02001281static void as_merged_requests(struct request_queue *q, struct request *req,
Nick Pigginf5b3db02005-11-07 00:59:53 -08001282 struct request *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /*
Jens Axboe8a8e6742006-07-18 21:07:29 +02001285 * if next expires before rq, assign its expire time to arq
1286 * and move into next position (next will be deleted) in fifo
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 */
Jens Axboed4f2f462006-07-13 09:12:14 +02001288 if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
1289 if (time_before(rq_fifo_time(next), rq_fifo_time(req))) {
1290 list_move(&req->queuelist, &next->queuelist);
1291 rq_set_fifo_time(req, rq_fifo_time(next));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293 }
1294
1295 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 * kill knowledge of next, this one is a goner
1297 */
1298 as_remove_queued_request(q, next);
Jens Axboe8a8e6742006-07-18 21:07:29 +02001299 as_put_io_context(next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Jens Axboe8a8e6742006-07-18 21:07:29 +02001301 RQ_SET_STATE(next, AS_RQ_MERGED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
1304/*
1305 * This is executed in a "deferred" process context, by kblockd. It calls the
1306 * driver's request_fn so the driver can submit that request.
1307 *
1308 * IMPORTANT! This guy will reenter the elevator, so set up all queue global
1309 * state before calling, and don't rely on any state over calls.
1310 *
1311 * FIXME! dispatch queue is not a queue at all!
1312 */
David Howells65f27f32006-11-22 14:55:48 +00001313static void as_work_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
David Howells65f27f32006-11-22 14:55:48 +00001315 struct as_data *ad = container_of(work, struct as_data, antic_work);
1316 struct request_queue *q = ad->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 unsigned long flags;
1318
1319 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboedc72ef42006-07-20 14:54:05 +02001320 blk_start_queueing(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 spin_unlock_irqrestore(q->queue_lock, flags);
1322}
1323
Jens Axboe165125e2007-07-24 09:28:11 +02001324static int as_may_queue(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 int ret = ELV_MQUEUE_MAY;
1327 struct as_data *ad = q->elevator->elevator_data;
1328 struct io_context *ioc;
1329 if (ad->antic_status == ANTIC_WAIT_REQ ||
1330 ad->antic_status == ANTIC_WAIT_NEXT) {
Jens Axboeb5deef92006-07-19 23:39:40 +02001331 ioc = as_get_io_context(q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (ad->io_context == ioc)
1333 ret = ELV_MQUEUE_MUST;
1334 put_io_context(ioc);
1335 }
1336
1337 return ret;
1338}
1339
1340static void as_exit_queue(elevator_t *e)
1341{
1342 struct as_data *ad = e->elevator_data;
1343
1344 del_timer_sync(&ad->antic_timer);
Andrew Morton19a75d82007-05-09 02:33:56 -07001345 kblockd_flush_work(&ad->antic_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
1348 BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 put_io_context(ad->io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 kfree(ad);
1352}
1353
1354/*
Jens Axboe8a8e6742006-07-18 21:07:29 +02001355 * initialize elevator private data (as_data).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 */
Jens Axboe165125e2007-07-24 09:28:11 +02001357static void *as_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
1359 struct as_data *ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Christoph Lameter94f60302007-07-17 04:03:29 -07001361 ad = kmalloc_node(sizeof(*ad), GFP_KERNEL | __GFP_ZERO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (!ad)
Jens Axboebc1c1162006-06-08 08:49:06 +02001363 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 ad->q = q; /* Identify what queue the data belongs to */
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* anticipatory scheduling helpers */
1368 ad->antic_timer.function = as_antic_timeout;
1369 ad->antic_timer.data = (unsigned long)q;
1370 init_timer(&ad->antic_timer);
David Howells65f27f32006-11-22 14:55:48 +00001371 INIT_WORK(&ad->antic_work, as_work_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
1374 INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
1375 ad->sort_list[REQ_SYNC] = RB_ROOT;
1376 ad->sort_list[REQ_ASYNC] = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 ad->fifo_expire[REQ_SYNC] = default_read_expire;
1378 ad->fifo_expire[REQ_ASYNC] = default_write_expire;
1379 ad->antic_expire = default_antic_expire;
1380 ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
1381 ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
1384 ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
1385 if (ad->write_batch_count < 2)
1386 ad->write_batch_count = 2;
1387
Jens Axboebc1c1162006-06-08 08:49:06 +02001388 return ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391/*
1392 * sysfs parts below
1393 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395static ssize_t
1396as_var_show(unsigned int var, char *page)
1397{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return sprintf(page, "%d\n", var);
1399}
1400
1401static ssize_t
1402as_var_store(unsigned long *var, const char *page, size_t count)
1403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 char *p = (char *) page;
1405
Jens Axboec9b3ad62005-07-27 11:43:37 -07001406 *var = simple_strtoul(p, &p, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return count;
1408}
1409
Al Viroe572ec72006-03-18 22:27:18 -05001410static ssize_t est_time_show(elevator_t *e, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Al Viro3d1ab402006-03-18 18:35:43 -05001412 struct as_data *ad = e->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 int pos = 0;
1414
Nick Pigginf5b3db02005-11-07 00:59:53 -08001415 pos += sprintf(page+pos, "%lu %% exit probability\n",
1416 100*ad->exit_prob/256);
1417 pos += sprintf(page+pos, "%lu %% probability of exiting without a "
1418 "cooperating process submitting IO\n",
1419 100*ad->exit_no_coop/256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
Nick Pigginf5b3db02005-11-07 00:59:53 -08001421 pos += sprintf(page+pos, "%llu sectors new seek distance\n",
1422 (unsigned long long)ad->new_seek_mean);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 return pos;
1425}
1426
1427#define SHOW_FUNCTION(__FUNC, __VAR) \
Al Viro3d1ab402006-03-18 18:35:43 -05001428static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{ \
Al Viro3d1ab402006-03-18 18:35:43 -05001430 struct as_data *ad = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1432}
Al Viroe572ec72006-03-18 22:27:18 -05001433SHOW_FUNCTION(as_read_expire_show, ad->fifo_expire[REQ_SYNC]);
1434SHOW_FUNCTION(as_write_expire_show, ad->fifo_expire[REQ_ASYNC]);
1435SHOW_FUNCTION(as_antic_expire_show, ad->antic_expire);
1436SHOW_FUNCTION(as_read_batch_expire_show, ad->batch_expire[REQ_SYNC]);
1437SHOW_FUNCTION(as_write_batch_expire_show, ad->batch_expire[REQ_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438#undef SHOW_FUNCTION
1439
1440#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
Al Viro3d1ab402006-03-18 18:35:43 -05001441static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{ \
Al Viro3d1ab402006-03-18 18:35:43 -05001443 struct as_data *ad = e->elevator_data; \
1444 int ret = as_var_store(__PTR, (page), count); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (*(__PTR) < (MIN)) \
1446 *(__PTR) = (MIN); \
1447 else if (*(__PTR) > (MAX)) \
1448 *(__PTR) = (MAX); \
1449 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1450 return ret; \
1451}
Al Viroe572ec72006-03-18 22:27:18 -05001452STORE_FUNCTION(as_read_expire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
1453STORE_FUNCTION(as_write_expire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
1454STORE_FUNCTION(as_antic_expire_store, &ad->antic_expire, 0, INT_MAX);
1455STORE_FUNCTION(as_read_batch_expire_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
Al Viroe572ec72006-03-18 22:27:18 -05001457STORE_FUNCTION(as_write_batch_expire_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
1459#undef STORE_FUNCTION
1460
Al Viroe572ec72006-03-18 22:27:18 -05001461#define AS_ATTR(name) \
1462 __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Al Viroe572ec72006-03-18 22:27:18 -05001464static struct elv_fs_entry as_attrs[] = {
1465 __ATTR_RO(est_time),
1466 AS_ATTR(read_expire),
1467 AS_ATTR(write_expire),
1468 AS_ATTR(antic_expire),
1469 AS_ATTR(read_batch_expire),
1470 AS_ATTR(write_batch_expire),
1471 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472};
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474static struct elevator_type iosched_as = {
1475 .ops = {
1476 .elevator_merge_fn = as_merge,
1477 .elevator_merged_fn = as_merged_request,
1478 .elevator_merge_req_fn = as_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02001479 .elevator_dispatch_fn = as_dispatch_request,
1480 .elevator_add_req_fn = as_add_request,
1481 .elevator_activate_req_fn = as_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 .elevator_deactivate_req_fn = as_deactivate_request,
1483 .elevator_queue_empty_fn = as_queue_empty,
1484 .elevator_completed_req_fn = as_completed_request,
Jens Axboee37f3462006-07-18 21:06:01 +02001485 .elevator_former_req_fn = elv_rb_former_request,
1486 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 .elevator_may_queue_fn = as_may_queue,
1488 .elevator_init_fn = as_init_queue,
1489 .elevator_exit_fn = as_exit_queue,
Al Viroe17a9482006-03-18 13:21:20 -05001490 .trim = as_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 },
1492
Al Viro3d1ab402006-03-18 18:35:43 -05001493 .elevator_attrs = as_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 .elevator_name = "anticipatory",
1495 .elevator_owner = THIS_MODULE,
1496};
1497
1498static int __init as_init(void)
1499{
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01001500 elv_register(&iosched_as);
1501
1502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
1505static void __exit as_exit(void)
1506{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001507 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 elv_unregister(&iosched_as);
Al Viro334e94d2006-03-18 15:05:53 -05001509 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02001510 /* ioc_gone's update must be visible before reading ioc_count */
1511 smp_wmb();
Jens Axboee4313dd2006-07-19 05:10:01 +02001512 if (elv_ioc_count_read(ioc_count))
Jens Axboe863fddc2008-05-29 09:35:22 +02001513 wait_for_completion(&all_gone);
Al Viro334e94d2006-03-18 15:05:53 -05001514 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
1517module_init(as_init);
1518module_exit(as_exit);
1519
1520MODULE_AUTHOR("Nick Piggin");
1521MODULE_LICENSE("GPL");
1522MODULE_DESCRIPTION("anticipatory IO scheduler");