blob: 612d640963006a45a677571b93aa2e182a8e5d09 [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;
154
Jens Axboe8a8e6742006-07-18 21:07:29 +0200155static void as_move_to_dispatch(struct as_data *ad, struct request *rq);
Tejun Heoef9be1d2005-11-11 14:27:09 +0100156static void as_antic_stop(struct as_data *ad);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
159 * IO Context helper functions
160 */
161
162/* Called to deallocate the as_io_context */
163static void free_as_io_context(struct as_io_context *aic)
164{
165 kfree(aic);
Jens Axboee4313dd2006-07-19 05:10:01 +0200166 elv_ioc_count_dec(ioc_count);
167 if (ioc_gone && !elv_ioc_count_read(ioc_count))
Al Viro334e94d2006-03-18 15:05:53 -0500168 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Al Viroe17a9482006-03-18 13:21:20 -0500171static void as_trim(struct io_context *ioc)
172{
Jens Axboe8bdd3f82008-02-01 09:44:28 +0100173 spin_lock_irq(&ioc->lock);
Al Viro334e94d2006-03-18 15:05:53 -0500174 if (ioc->aic)
175 free_as_io_context(ioc->aic);
Al Viroe17a9482006-03-18 13:21:20 -0500176 ioc->aic = NULL;
Jens Axboe8bdd3f82008-02-01 09:44:28 +0100177 spin_unlock_irq(&ioc->lock);
Al Viroe17a9482006-03-18 13:21:20 -0500178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/* Called when the task exits */
181static void exit_as_io_context(struct as_io_context *aic)
182{
183 WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
184 clear_bit(AS_TASK_RUNNING, &aic->state);
185}
186
187static struct as_io_context *alloc_as_io_context(void)
188{
189 struct as_io_context *ret;
190
191 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
192 if (ret) {
193 ret->dtor = free_as_io_context;
194 ret->exit = exit_as_io_context;
195 ret->state = 1 << AS_TASK_RUNNING;
196 atomic_set(&ret->nr_queued, 0);
197 atomic_set(&ret->nr_dispatched, 0);
198 spin_lock_init(&ret->lock);
199 ret->ttime_total = 0;
200 ret->ttime_samples = 0;
201 ret->ttime_mean = 0;
202 ret->seek_total = 0;
203 ret->seek_samples = 0;
204 ret->seek_mean = 0;
Jens Axboee4313dd2006-07-19 05:10:01 +0200205 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
208 return ret;
209}
210
211/*
212 * If the current task has no AS IO context then create one and initialise it.
213 * Then take a ref on the task's io context and return it.
214 */
Jens Axboeb5deef92006-07-19 23:39:40 +0200215static struct io_context *as_get_io_context(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Jens Axboeb5deef92006-07-19 23:39:40 +0200217 struct io_context *ioc = get_io_context(GFP_ATOMIC, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (ioc && !ioc->aic) {
219 ioc->aic = alloc_as_io_context();
220 if (!ioc->aic) {
221 put_io_context(ioc);
222 ioc = NULL;
223 }
224 }
225 return ioc;
226}
227
Jens Axboe8a8e6742006-07-18 21:07:29 +0200228static void as_put_io_context(struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +0200229{
230 struct as_io_context *aic;
231
Jens Axboe8a8e6742006-07-18 21:07:29 +0200232 if (unlikely(!RQ_IOC(rq)))
Jens Axboeb4878f22005-10-20 16:42:29 +0200233 return;
234
Jens Axboe8a8e6742006-07-18 21:07:29 +0200235 aic = RQ_IOC(rq)->aic;
Jens Axboeb4878f22005-10-20 16:42:29 +0200236
Jens Axboe8a8e6742006-07-18 21:07:29 +0200237 if (rq_is_sync(rq) && aic) {
Jens Axboe8bdd3f82008-02-01 09:44:28 +0100238 unsigned long flags;
239
240 spin_lock_irqsave(&aic->lock, flags);
Jens Axboeb4878f22005-10-20 16:42:29 +0200241 set_bit(AS_TASK_IORUNNING, &aic->state);
242 aic->last_end_request = jiffies;
Jens Axboe8bdd3f82008-02-01 09:44:28 +0100243 spin_unlock_irqrestore(&aic->lock, flags);
Jens Axboeb4878f22005-10-20 16:42:29 +0200244 }
245
Jens Axboe8a8e6742006-07-18 21:07:29 +0200246 put_io_context(RQ_IOC(rq));
Jens Axboeb4878f22005-10-20 16:42:29 +0200247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 * rb tree support functions
251 */
Jens Axboe9e2585a2006-07-28 09:26:13 +0200252#define RQ_RB_ROOT(ad, rq) (&(ad)->sort_list[rq_is_sync((rq))])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Jens Axboe8a8e6742006-07-18 21:07:29 +0200254static void as_add_rq_rb(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Jens Axboee37f3462006-07-18 21:06:01 +0200256 struct request *alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Jens Axboe9e2585a2006-07-28 09:26:13 +0200258 while ((unlikely(alias = elv_rb_add(RQ_RB_ROOT(ad, rq), rq)))) {
Jens Axboe8a8e6742006-07-18 21:07:29 +0200259 as_move_to_dispatch(ad, alias);
Tejun Heoef9be1d2005-11-11 14:27:09 +0100260 as_antic_stop(ad);
261 }
262}
263
Jens Axboe8a8e6742006-07-18 21:07:29 +0200264static inline void as_del_rq_rb(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Jens Axboe9e2585a2006-07-28 09:26:13 +0200266 elv_rb_del(RQ_RB_ROOT(ad, rq), rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269/*
270 * IO Scheduler proper
271 */
272
273#define MAXBACK (1024 * 1024) /*
274 * Maximum distance the disk will go backward
275 * for a request.
276 */
277
278#define BACK_PENALTY 2
279
280/*
281 * as_choose_req selects the preferred one of two requests of the same data_dir
282 * ignoring time - eg. timeouts, which is the job of as_dispatch_request
283 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200284static struct request *
285as_choose_req(struct as_data *ad, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 int data_dir;
288 sector_t last, s1, s2, d1, d2;
289 int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */
290 const sector_t maxback = MAXBACK;
291
Jens Axboe8a8e6742006-07-18 21:07:29 +0200292 if (rq1 == NULL || rq1 == rq2)
293 return rq2;
294 if (rq2 == NULL)
295 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Jens Axboe8a8e6742006-07-18 21:07:29 +0200297 data_dir = rq_is_sync(rq1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 last = ad->last_sector[data_dir];
Jens Axboe8a8e6742006-07-18 21:07:29 +0200300 s1 = rq1->sector;
301 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Jens Axboe8a8e6742006-07-18 21:07:29 +0200303 BUG_ON(data_dir != rq_is_sync(rq2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /*
306 * Strict one way elevator _except_ in the case where we allow
307 * short backward seeks which are biased as twice the cost of a
308 * similar forward seek.
309 */
310 if (s1 >= last)
311 d1 = s1 - last;
312 else if (s1+maxback >= last)
313 d1 = (last - s1)*BACK_PENALTY;
314 else {
315 r1_wrap = 1;
316 d1 = 0; /* shut up, gcc */
317 }
318
319 if (s2 >= last)
320 d2 = s2 - last;
321 else if (s2+maxback >= last)
322 d2 = (last - s2)*BACK_PENALTY;
323 else {
324 r2_wrap = 1;
325 d2 = 0;
326 }
327
328 /* Found required data */
329 if (!r1_wrap && r2_wrap)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200330 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 else if (!r2_wrap && r1_wrap)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200332 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 else if (r1_wrap && r2_wrap) {
334 /* both behind the head */
335 if (s1 <= s2)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200336 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 else
Jens Axboe8a8e6742006-07-18 21:07:29 +0200338 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
341 /* Both requests in front of the head */
342 if (d1 < d2)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200343 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 else if (d2 < d1)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200345 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 else {
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
354/*
Jens Axboe8a8e6742006-07-18 21:07:29 +0200355 * as_find_next_rq finds the next request after @prev in elevator order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * this with as_choose_req form the basis for how the scheduler chooses
357 * what request to process next. Anticipation works on top of this.
358 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200359static struct request *
360as_find_next_rq(struct as_data *ad, struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct rb_node *rbnext = rb_next(&last->rb_node);
363 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe8a8e6742006-07-18 21:07:29 +0200364 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Jens Axboee37f3462006-07-18 21:06:01 +0200366 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if (rbprev)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200369 prev = rb_entry_rq(rbprev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 if (rbnext)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200372 next = rb_entry_rq(rbnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 else {
Jens Axboe9e2585a2006-07-28 09:26:13 +0200374 const int data_dir = rq_is_sync(last);
Jens Axboee37f3462006-07-18 21:06:01 +0200375
376 rbnext = rb_first(&ad->sort_list[data_dir]);
377 if (rbnext && rbnext != &last->rb_node)
Jens Axboe8a8e6742006-07-18 21:07:29 +0200378 next = rb_entry_rq(rbnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Jens Axboee37f3462006-07-18 21:06:01 +0200381 return as_choose_req(ad, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384/*
385 * anticipatory scheduling functions follow
386 */
387
388/*
389 * as_antic_expired tells us when we have anticipated too long.
390 * The funny "absolute difference" math on the elapsed time is to handle
391 * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
392 */
393static int as_antic_expired(struct as_data *ad)
394{
395 long delta_jif;
396
397 delta_jif = jiffies - ad->antic_start;
398 if (unlikely(delta_jif < 0))
399 delta_jif = -delta_jif;
400 if (delta_jif < ad->antic_expire)
401 return 0;
402
403 return 1;
404}
405
406/*
407 * as_antic_waitnext starts anticipating that a nice request will soon be
408 * submitted. See also as_antic_waitreq
409 */
410static void as_antic_waitnext(struct as_data *ad)
411{
412 unsigned long timeout;
413
414 BUG_ON(ad->antic_status != ANTIC_OFF
415 && ad->antic_status != ANTIC_WAIT_REQ);
416
417 timeout = ad->antic_start + ad->antic_expire;
418
419 mod_timer(&ad->antic_timer, timeout);
420
421 ad->antic_status = ANTIC_WAIT_NEXT;
422}
423
424/*
425 * as_antic_waitreq starts anticipating. We don't start timing the anticipation
426 * until the request that we're anticipating on has finished. This means we
427 * are timing from when the candidate process wakes up hopefully.
428 */
429static void as_antic_waitreq(struct as_data *ad)
430{
431 BUG_ON(ad->antic_status == ANTIC_FINISHED);
432 if (ad->antic_status == ANTIC_OFF) {
433 if (!ad->io_context || ad->ioc_finished)
434 as_antic_waitnext(ad);
435 else
436 ad->antic_status = ANTIC_WAIT_REQ;
437 }
438}
439
440/*
441 * This is called directly by the functions in this file to stop anticipation.
442 * We kill the timer and schedule a call to the request_fn asap.
443 */
444static void as_antic_stop(struct as_data *ad)
445{
446 int status = ad->antic_status;
447
448 if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
449 if (status == ANTIC_WAIT_NEXT)
450 del_timer(&ad->antic_timer);
451 ad->antic_status = ANTIC_FINISHED;
452 /* see as_work_handler */
453 kblockd_schedule_work(&ad->antic_work);
454 }
455}
456
457/*
458 * as_antic_timeout is the timer function set by as_antic_waitnext.
459 */
460static void as_antic_timeout(unsigned long data)
461{
462 struct request_queue *q = (struct request_queue *)data;
463 struct as_data *ad = q->elevator->elevator_data;
464 unsigned long flags;
465
466 spin_lock_irqsave(q->queue_lock, flags);
467 if (ad->antic_status == ANTIC_WAIT_REQ
468 || ad->antic_status == ANTIC_WAIT_NEXT) {
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100469 struct as_io_context *aic;
470 spin_lock(&ad->io_context->lock);
471 aic = ad->io_context->aic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 ad->antic_status = ANTIC_FINISHED;
474 kblockd_schedule_work(&ad->antic_work);
475
476 if (aic->ttime_samples == 0) {
Nick Pigginf5b3db02005-11-07 00:59:53 -0800477 /* process anticipated on has exited or timed out*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 ad->exit_prob = (7*ad->exit_prob + 256)/8;
479 }
Nick Pigginf5b3db02005-11-07 00:59:53 -0800480 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
481 /* process not "saved" by a cooperating request */
482 ad->exit_no_coop = (7*ad->exit_no_coop + 256)/8;
483 }
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100484 spin_unlock(&ad->io_context->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 spin_unlock_irqrestore(q->queue_lock, flags);
487}
488
Nick Pigginf5b3db02005-11-07 00:59:53 -0800489static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic,
490 unsigned long ttime)
491{
492 /* fixed point: 1.0 == 1<<8 */
493 if (aic->ttime_samples == 0) {
494 ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
495 ad->new_ttime_mean = ad->new_ttime_total / 256;
496
497 ad->exit_prob = (7*ad->exit_prob)/8;
498 }
499 aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
500 aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
501 aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
502}
503
504static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic,
505 sector_t sdist)
506{
507 u64 total;
508
509 if (aic->seek_samples == 0) {
510 ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
511 ad->new_seek_mean = ad->new_seek_total / 256;
512 }
513
514 /*
515 * Don't allow the seek distance to get too large from the
516 * odd fragment, pagein, etc
517 */
518 if (aic->seek_samples <= 60) /* second&third seek */
519 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
520 else
521 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
522
523 aic->seek_samples = (7*aic->seek_samples + 256) / 8;
524 aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
525 total = aic->seek_total + (aic->seek_samples/2);
526 do_div(total, aic->seek_samples);
527 aic->seek_mean = (sector_t)total;
528}
529
530/*
531 * as_update_iohist keeps a decaying histogram of IO thinktimes, and
532 * updates @aic->ttime_mean based on that. It is called when a new
533 * request is queued.
534 */
535static void as_update_iohist(struct as_data *ad, struct as_io_context *aic,
536 struct request *rq)
537{
Jens Axboe9e2585a2006-07-28 09:26:13 +0200538 int data_dir = rq_is_sync(rq);
Nick Pigginf5b3db02005-11-07 00:59:53 -0800539 unsigned long thinktime = 0;
540 sector_t seek_dist;
541
542 if (aic == NULL)
543 return;
544
545 if (data_dir == REQ_SYNC) {
546 unsigned long in_flight = atomic_read(&aic->nr_queued)
547 + atomic_read(&aic->nr_dispatched);
548 spin_lock(&aic->lock);
549 if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
550 test_bit(AS_TASK_IOSTARTED, &aic->state)) {
551 /* Calculate read -> read thinktime */
552 if (test_bit(AS_TASK_IORUNNING, &aic->state)
553 && in_flight == 0) {
554 thinktime = jiffies - aic->last_end_request;
555 thinktime = min(thinktime, MAX_THINKTIME-1);
556 }
557 as_update_thinktime(ad, aic, thinktime);
558
559 /* Calculate read -> read seek distance */
560 if (aic->last_request_pos < rq->sector)
561 seek_dist = rq->sector - aic->last_request_pos;
562 else
563 seek_dist = aic->last_request_pos - rq->sector;
564 as_update_seekdist(ad, aic, seek_dist);
565 }
566 aic->last_request_pos = rq->sector + rq->nr_sectors;
567 set_bit(AS_TASK_IOSTARTED, &aic->state);
568 spin_unlock(&aic->lock);
569 }
570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/*
573 * as_close_req decides if one request is considered "close" to the
574 * previous one issued.
575 */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800576static int as_close_req(struct as_data *ad, struct as_io_context *aic,
Jens Axboe8a8e6742006-07-18 21:07:29 +0200577 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Nick Pigginc6a632a2007-05-08 00:26:34 -0700579 unsigned long delay; /* jiffies */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 sector_t last = ad->last_sector[ad->batch_data_dir];
Jens Axboe8a8e6742006-07-18 21:07:29 +0200581 sector_t next = rq->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 sector_t delta; /* acceptable close offset (in sectors) */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800583 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
586 delay = 0;
587 else
Nick Pigginc6a632a2007-05-08 00:26:34 -0700588 delay = jiffies - ad->antic_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Nick Pigginf5b3db02005-11-07 00:59:53 -0800590 if (delay == 0)
591 delta = 8192;
Nick Pigginc6a632a2007-05-08 00:26:34 -0700592 else if (delay <= (20 * HZ / 1000) && delay <= ad->antic_expire)
Nick Pigginf5b3db02005-11-07 00:59:53 -0800593 delta = 8192 << delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 else
595 return 1;
596
Nick Pigginf5b3db02005-11-07 00:59:53 -0800597 if ((last <= next + (delta>>1)) && (next <= last + delta))
598 return 1;
599
600 if (last < next)
601 s = next - last;
602 else
603 s = last - next;
604
605 if (aic->seek_samples == 0) {
606 /*
607 * Process has just started IO. Use past statistics to
608 * gauge success possibility
609 */
610 if (ad->new_seek_mean > s) {
611 /* this request is better than what we're expecting */
612 return 1;
613 }
614
615 } else {
616 if (aic->seek_mean > s) {
617 /* this request is better than what we're expecting */
618 return 1;
619 }
620 }
621
622 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625/*
626 * as_can_break_anticipation returns true if we have been anticipating this
627 * request.
628 *
629 * It also returns true if the process against which we are anticipating
630 * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
631 * dispatch it ASAP, because we know that application will not be submitting
632 * any new reads.
633 *
Nick Pigginf5b3db02005-11-07 00:59:53 -0800634 * If the task which has submitted the request has exited, break anticipation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 *
636 * If this task has queued some other IO, do not enter enticipation.
637 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200638static int as_can_break_anticipation(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct io_context *ioc;
641 struct as_io_context *aic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 ioc = ad->io_context;
644 BUG_ON(!ioc);
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100645 spin_lock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Jens Axboe8a8e6742006-07-18 21:07:29 +0200647 if (rq && ioc == RQ_IOC(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* request from same process */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100649 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 1;
651 }
652
653 if (ad->ioc_finished && as_antic_expired(ad)) {
654 /*
655 * In this situation status should really be FINISHED,
656 * however the timer hasn't had the chance to run yet.
657 */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100658 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return 1;
660 }
661
662 aic = ioc->aic;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100663 if (!aic) {
664 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return 0;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (atomic_read(&aic->nr_queued) > 0) {
669 /* process has more requests queued */
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 if (atomic_read(&aic->nr_dispatched) > 0) {
675 /* process has more requests dispatched */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100676 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return 1;
678 }
679
Jens Axboe8a8e6742006-07-18 21:07:29 +0200680 if (rq && rq_is_sync(rq) && as_close_req(ad, aic, rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /*
682 * Found a close request that is not one of ours.
683 *
Nick Pigginf5b3db02005-11-07 00:59:53 -0800684 * This makes close requests from another process update
685 * our IO history. Is generally useful when there are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 * two or more cooperating processes working in the same
687 * area.
688 */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800689 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
690 if (aic->ttime_samples == 0)
691 ad->exit_prob = (7*ad->exit_prob + 256)/8;
692
693 ad->exit_no_coop = (7*ad->exit_no_coop)/8;
694 }
695
Jens Axboe8a8e6742006-07-18 21:07:29 +0200696 as_update_iohist(ad, aic, rq);
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100697 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return 1;
699 }
700
Nick Pigginf5b3db02005-11-07 00:59:53 -0800701 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
702 /* process anticipated on has exited */
703 if (aic->ttime_samples == 0)
704 ad->exit_prob = (7*ad->exit_prob + 256)/8;
705
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100706 if (ad->exit_no_coop > 128) {
707 spin_unlock(&ioc->lock);
Nick Pigginf5b3db02005-11-07 00:59:53 -0800708 return 1;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100709 }
Nick Pigginf5b3db02005-11-07 00:59:53 -0800710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 if (aic->ttime_samples == 0) {
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100713 if (ad->new_ttime_mean > ad->antic_expire) {
714 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return 1;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100716 }
717 if (ad->exit_prob * ad->exit_no_coop > 128*256) {
718 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 1;
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 } else if (aic->ttime_mean > ad->antic_expire) {
722 /* the process thinks too much between requests */
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100723 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return 1;
725 }
Jens Axboe521f3bbd2008-01-21 20:03:12 +0100726 spin_unlock(&ioc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return 0;
728}
729
730/*
Jens Axboe8a8e6742006-07-18 21:07:29 +0200731 * as_can_anticipate indicates whether we should either run rq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * or keep anticipating a better request.
733 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200734static int as_can_anticipate(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 if (!ad->io_context)
737 /*
738 * Last request submitted was a write
739 */
740 return 0;
741
742 if (ad->antic_status == ANTIC_FINISHED)
743 /*
744 * Don't restart if we have just finished. Run the next request
745 */
746 return 0;
747
Jens Axboe8a8e6742006-07-18 21:07:29 +0200748 if (as_can_break_anticipation(ad, rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /*
750 * This request is a good candidate. Don't keep anticipating,
751 * run it.
752 */
753 return 0;
754
755 /*
756 * OK from here, we haven't finished, and don't have a decent request!
757 * Status is either ANTIC_OFF so start waiting,
758 * ANTIC_WAIT_REQ so continue waiting for request to finish
759 * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
761
762 return 1;
763}
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/*
Jens Axboe8a8e6742006-07-18 21:07:29 +0200766 * as_update_rq must be called whenever a request (rq) is added to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 * the sort_list. This function keeps caches up to date, and checks if the
768 * request might be one we are "anticipating"
769 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200770static void as_update_rq(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Jens Axboe8a8e6742006-07-18 21:07:29 +0200772 const int data_dir = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Jens Axboe8a8e6742006-07-18 21:07:29 +0200774 /* keep the next_rq cache up to date */
775 ad->next_rq[data_dir] = as_choose_req(ad, rq, ad->next_rq[data_dir]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /*
778 * have we been anticipating this request?
779 * or does it come from the same process as the one we are anticipating
780 * for?
781 */
782 if (ad->antic_status == ANTIC_WAIT_REQ
783 || ad->antic_status == ANTIC_WAIT_NEXT) {
Jens Axboe8a8e6742006-07-18 21:07:29 +0200784 if (as_can_break_anticipation(ad, rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 as_antic_stop(ad);
786 }
787}
788
789/*
790 * Gathers timings and resizes the write batch automatically
791 */
792static void update_write_batch(struct as_data *ad)
793{
794 unsigned long batch = ad->batch_expire[REQ_ASYNC];
795 long write_time;
796
797 write_time = (jiffies - ad->current_batch_expires) + batch;
798 if (write_time < 0)
799 write_time = 0;
800
801 if (write_time > batch && !ad->write_batch_idled) {
802 if (write_time > batch * 3)
803 ad->write_batch_count /= 2;
804 else
805 ad->write_batch_count--;
806 } else if (write_time < batch && ad->current_write_count == 0) {
807 if (batch > write_time * 3)
808 ad->write_batch_count *= 2;
809 else
810 ad->write_batch_count++;
811 }
812
813 if (ad->write_batch_count < 1)
814 ad->write_batch_count = 1;
815}
816
817/*
818 * as_completed_request is to be called when a request has completed and
819 * returned something to the requesting process, be it an error or data.
820 */
Jens Axboe165125e2007-07-24 09:28:11 +0200821static void as_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 WARN_ON(!list_empty(&rq->queuelist));
826
Jens Axboe8a8e6742006-07-18 21:07:29 +0200827 if (RQ_STATE(rq) != AS_RQ_REMOVED) {
828 printk("rq->state %d\n", RQ_STATE(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 WARN_ON(1);
830 goto out;
831 }
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (ad->changed_batch && ad->nr_dispatched == 1) {
834 kblockd_schedule_work(&ad->antic_work);
835 ad->changed_batch = 0;
836
837 if (ad->batch_data_dir == REQ_SYNC)
838 ad->new_batch = 1;
839 }
840 WARN_ON(ad->nr_dispatched == 0);
841 ad->nr_dispatched--;
842
843 /*
844 * Start counting the batch from when a request of that direction is
845 * actually serviced. This should help devices with big TCQ windows
846 * and writeback caches
847 */
Jens Axboe9e2585a2006-07-28 09:26:13 +0200848 if (ad->new_batch && ad->batch_data_dir == rq_is_sync(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 update_write_batch(ad);
850 ad->current_batch_expires = jiffies +
851 ad->batch_expire[REQ_SYNC];
852 ad->new_batch = 0;
853 }
854
Jens Axboe8a8e6742006-07-18 21:07:29 +0200855 if (ad->io_context == RQ_IOC(rq) && ad->io_context) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 ad->antic_start = jiffies;
857 ad->ioc_finished = 1;
858 if (ad->antic_status == ANTIC_WAIT_REQ) {
859 /*
860 * We were waiting on this request, now anticipate
861 * the next one
862 */
863 as_antic_waitnext(ad);
864 }
865 }
866
Jens Axboe8a8e6742006-07-18 21:07:29 +0200867 as_put_io_context(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868out:
Jens Axboe8a8e6742006-07-18 21:07:29 +0200869 RQ_SET_STATE(rq, AS_RQ_POSTSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872/*
873 * as_remove_queued_request removes a request from the pre dispatch queue
874 * without updating refcounts. It is expected the caller will drop the
875 * reference unless it replaces the request at somepart of the elevator
876 * (ie. the dispatch queue)
877 */
Jens Axboe165125e2007-07-24 09:28:11 +0200878static void as_remove_queued_request(struct request_queue *q,
879 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Jens Axboe9e2585a2006-07-28 09:26:13 +0200881 const int data_dir = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct as_data *ad = q->elevator->elevator_data;
Jens Axboe8a8e6742006-07-18 21:07:29 +0200883 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Jens Axboe8a8e6742006-07-18 21:07:29 +0200885 WARN_ON(RQ_STATE(rq) != AS_RQ_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Jens Axboe8a8e6742006-07-18 21:07:29 +0200887 ioc = RQ_IOC(rq);
888 if (ioc && ioc->aic) {
889 BUG_ON(!atomic_read(&ioc->aic->nr_queued));
890 atomic_dec(&ioc->aic->nr_queued);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892
893 /*
Jens Axboe8a8e6742006-07-18 21:07:29 +0200894 * Update the "next_rq" cache if we are about to remove its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 * entry
896 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200897 if (ad->next_rq[data_dir] == rq)
898 ad->next_rq[data_dir] = as_find_next_rq(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Jens Axboed4f2f462006-07-13 09:12:14 +0200900 rq_fifo_clear(rq);
Jens Axboe8a8e6742006-07-18 21:07:29 +0200901 as_del_rq_rb(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
904/*
Aaron Carroll8896f3c2007-12-05 21:06:50 +1100905 * as_fifo_expired returns 0 if there are no expired requests on the fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 * 1 otherwise. It is ratelimited so that we only perform the check once per
907 * `fifo_expire' interval. Otherwise a large number of expired requests
908 * would create a hopeless seekstorm.
909 *
910 * See as_antic_expired comment.
911 */
912static int as_fifo_expired(struct as_data *ad, int adir)
913{
Jens Axboed4f2f462006-07-13 09:12:14 +0200914 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 long delta_jif;
916
917 delta_jif = jiffies - ad->last_check_fifo[adir];
918 if (unlikely(delta_jif < 0))
919 delta_jif = -delta_jif;
920 if (delta_jif < ad->fifo_expire[adir])
921 return 0;
922
923 ad->last_check_fifo[adir] = jiffies;
924
925 if (list_empty(&ad->fifo_list[adir]))
926 return 0;
927
Jens Axboed4f2f462006-07-13 09:12:14 +0200928 rq = rq_entry_fifo(ad->fifo_list[adir].next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Jens Axboed4f2f462006-07-13 09:12:14 +0200930 return time_after(jiffies, rq_fifo_time(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933/*
934 * as_batch_expired returns true if the current batch has expired. A batch
935 * is a set of reads or a set of writes.
936 */
937static inline int as_batch_expired(struct as_data *ad)
938{
939 if (ad->changed_batch || ad->new_batch)
940 return 0;
941
942 if (ad->batch_data_dir == REQ_SYNC)
943 /* TODO! add a check so a complete fifo gets written? */
944 return time_after(jiffies, ad->current_batch_expires);
945
946 return time_after(jiffies, ad->current_batch_expires)
947 || ad->current_write_count == 0;
948}
949
950/*
951 * move an entry to dispatch queue
952 */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200953static void as_move_to_dispatch(struct as_data *ad, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Jens Axboe9e2585a2006-07-28 09:26:13 +0200955 const int data_dir = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Jens Axboee37f3462006-07-18 21:06:01 +0200957 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 as_antic_stop(ad);
960 ad->antic_status = ANTIC_OFF;
961
962 /*
963 * This has to be set in order to be correctly updated by
Jens Axboe8a8e6742006-07-18 21:07:29 +0200964 * as_find_next_rq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
966 ad->last_sector[data_dir] = rq->sector + rq->nr_sectors;
967
968 if (data_dir == REQ_SYNC) {
Jens Axboe8a8e6742006-07-18 21:07:29 +0200969 struct io_context *ioc = RQ_IOC(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* In case we have to anticipate after this */
Jens Axboe8a8e6742006-07-18 21:07:29 +0200971 copy_io_context(&ad->io_context, &ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 } else {
973 if (ad->io_context) {
974 put_io_context(ad->io_context);
975 ad->io_context = NULL;
976 }
977
978 if (ad->current_write_count != 0)
979 ad->current_write_count--;
980 }
981 ad->ioc_finished = 0;
982
Jens Axboe8a8e6742006-07-18 21:07:29 +0200983 ad->next_rq[data_dir] = as_find_next_rq(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 /*
986 * take it off the sort and fifo list, add to dispatch queue
987 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 as_remove_queued_request(ad->q, rq);
Jens Axboe8a8e6742006-07-18 21:07:29 +0200989 WARN_ON(RQ_STATE(rq) != AS_RQ_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Jens Axboeb4878f22005-10-20 16:42:29 +0200991 elv_dispatch_sort(ad->q, rq);
992
Jens Axboe8a8e6742006-07-18 21:07:29 +0200993 RQ_SET_STATE(rq, AS_RQ_DISPATCHED);
994 if (RQ_IOC(rq) && RQ_IOC(rq)->aic)
995 atomic_inc(&RQ_IOC(rq)->aic->nr_dispatched);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 ad->nr_dispatched++;
997}
998
999/*
1000 * as_dispatch_request selects the best request according to
1001 * read/write expire, batch expire, etc, and moves it to the dispatch
1002 * queue. Returns 1 if a request was found, 0 otherwise.
1003 */
Jens Axboe165125e2007-07-24 09:28:11 +02001004static int as_dispatch_request(struct request_queue *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Jens Axboeb4878f22005-10-20 16:42:29 +02001006 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]);
1008 const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]);
Jens Axboe8a8e6742006-07-18 21:07:29 +02001009 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Jens Axboeb4878f22005-10-20 16:42:29 +02001011 if (unlikely(force)) {
1012 /*
1013 * Forced dispatch, accounting is useless. Reset
1014 * accounting states and dump fifo_lists. Note that
1015 * batch_data_dir is reset to REQ_SYNC to avoid
1016 * screwing write batch accounting as write batch
1017 * accounting occurs on W->R transition.
1018 */
1019 int dispatched = 0;
1020
1021 ad->batch_data_dir = REQ_SYNC;
1022 ad->changed_batch = 0;
1023 ad->new_batch = 0;
1024
Jens Axboe8a8e6742006-07-18 21:07:29 +02001025 while (ad->next_rq[REQ_SYNC]) {
1026 as_move_to_dispatch(ad, ad->next_rq[REQ_SYNC]);
Jens Axboeb4878f22005-10-20 16:42:29 +02001027 dispatched++;
1028 }
1029 ad->last_check_fifo[REQ_SYNC] = jiffies;
1030
Jens Axboe8a8e6742006-07-18 21:07:29 +02001031 while (ad->next_rq[REQ_ASYNC]) {
1032 as_move_to_dispatch(ad, ad->next_rq[REQ_ASYNC]);
Jens Axboeb4878f22005-10-20 16:42:29 +02001033 dispatched++;
1034 }
1035 ad->last_check_fifo[REQ_ASYNC] = jiffies;
1036
1037 return dispatched;
1038 }
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* Signal that the write batch was uncontended, so we can't time it */
1041 if (ad->batch_data_dir == REQ_ASYNC && !reads) {
1042 if (ad->current_write_count == 0 || !writes)
1043 ad->write_batch_idled = 1;
1044 }
1045
1046 if (!(reads || writes)
1047 || ad->antic_status == ANTIC_WAIT_REQ
1048 || ad->antic_status == ANTIC_WAIT_NEXT
1049 || ad->changed_batch)
1050 return 0;
1051
Nick Pigginf5b3db02005-11-07 00:59:53 -08001052 if (!(reads && writes && as_batch_expired(ad))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /*
1054 * batch is still running or no reads or no writes
1055 */
Jens Axboe8a8e6742006-07-18 21:07:29 +02001056 rq = ad->next_rq[ad->batch_data_dir];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) {
1059 if (as_fifo_expired(ad, REQ_SYNC))
1060 goto fifo_expired;
1061
Jens Axboe8a8e6742006-07-18 21:07:29 +02001062 if (as_can_anticipate(ad, rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 as_antic_waitreq(ad);
1064 return 0;
1065 }
1066 }
1067
Jens Axboe8a8e6742006-07-18 21:07:29 +02001068 if (rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* we have a "next request" */
1070 if (reads && !writes)
1071 ad->current_batch_expires =
1072 jiffies + ad->batch_expire[REQ_SYNC];
1073 goto dispatch_request;
1074 }
1075 }
1076
1077 /*
1078 * at this point we are not running a batch. select the appropriate
1079 * data direction (read / write)
1080 */
1081
1082 if (reads) {
Jens Axboedd67d052006-06-21 09:36:18 +02001083 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_SYNC]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (writes && ad->batch_data_dir == REQ_SYNC)
1086 /*
1087 * Last batch was a read, switch to writes
1088 */
1089 goto dispatch_writes;
1090
1091 if (ad->batch_data_dir == REQ_ASYNC) {
1092 WARN_ON(ad->new_batch);
1093 ad->changed_batch = 1;
1094 }
1095 ad->batch_data_dir = REQ_SYNC;
Jens Axboe8a8e6742006-07-18 21:07:29 +02001096 rq = rq_entry_fifo(ad->fifo_list[REQ_SYNC].next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 ad->last_check_fifo[ad->batch_data_dir] = jiffies;
1098 goto dispatch_request;
1099 }
1100
1101 /*
1102 * the last batch was a read
1103 */
1104
1105 if (writes) {
1106dispatch_writes:
Jens Axboedd67d052006-06-21 09:36:18 +02001107 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_ASYNC]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 if (ad->batch_data_dir == REQ_SYNC) {
1110 ad->changed_batch = 1;
1111
1112 /*
1113 * new_batch might be 1 when the queue runs out of
1114 * reads. A subsequent submission of a write might
1115 * cause a change of batch before the read is finished.
1116 */
1117 ad->new_batch = 0;
1118 }
1119 ad->batch_data_dir = REQ_ASYNC;
1120 ad->current_write_count = ad->write_batch_count;
1121 ad->write_batch_idled = 0;
Aaron Carroll49565122007-12-05 21:07:07 +11001122 rq = rq_entry_fifo(ad->fifo_list[REQ_ASYNC].next);
1123 ad->last_check_fifo[REQ_ASYNC] = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 goto dispatch_request;
1125 }
1126
1127 BUG();
1128 return 0;
1129
1130dispatch_request:
1131 /*
1132 * If a request has expired, service it.
1133 */
1134
1135 if (as_fifo_expired(ad, ad->batch_data_dir)) {
1136fifo_expired:
Jens Axboe8a8e6742006-07-18 21:07:29 +02001137 rq = rq_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139
1140 if (ad->changed_batch) {
1141 WARN_ON(ad->new_batch);
1142
1143 if (ad->nr_dispatched)
1144 return 0;
1145
1146 if (ad->batch_data_dir == REQ_ASYNC)
1147 ad->current_batch_expires = jiffies +
1148 ad->batch_expire[REQ_ASYNC];
1149 else
1150 ad->new_batch = 1;
1151
1152 ad->changed_batch = 0;
1153 }
1154
1155 /*
Jens Axboe8a8e6742006-07-18 21:07:29 +02001156 * rq is the selected appropriate request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 */
Jens Axboe8a8e6742006-07-18 21:07:29 +02001158 as_move_to_dispatch(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 return 1;
1161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/*
Jens Axboe8a8e6742006-07-18 21:07:29 +02001164 * add rq to rbtree and fifo
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 */
Jens Axboe165125e2007-07-24 09:28:11 +02001166static void as_add_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Jens Axboeb4878f22005-10-20 16:42:29 +02001168 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int data_dir;
1170
Jens Axboe8a8e6742006-07-18 21:07:29 +02001171 RQ_SET_STATE(rq, AS_RQ_NEW);
Jens Axboeb4878f22005-10-20 16:42:29 +02001172
Jens Axboe9e2585a2006-07-28 09:26:13 +02001173 data_dir = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Jens Axboeb5deef92006-07-19 23:39:40 +02001175 rq->elevator_private = as_get_io_context(q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Jens Axboe8a8e6742006-07-18 21:07:29 +02001177 if (RQ_IOC(rq)) {
1178 as_update_iohist(ad, RQ_IOC(rq)->aic, rq);
1179 atomic_inc(&RQ_IOC(rq)->aic->nr_queued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
Jens Axboe8a8e6742006-07-18 21:07:29 +02001182 as_add_rq_rb(ad, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Tejun Heoef9be1d2005-11-11 14:27:09 +01001184 /*
Aaron Carroll8896f3c2007-12-05 21:06:50 +11001185 * set expire time and add to fifo list
Tejun Heoef9be1d2005-11-11 14:27:09 +01001186 */
Jens Axboed4f2f462006-07-13 09:12:14 +02001187 rq_set_fifo_time(rq, jiffies + ad->fifo_expire[data_dir]);
1188 list_add_tail(&rq->queuelist, &ad->fifo_list[data_dir]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Jens Axboe8a8e6742006-07-18 21:07:29 +02001190 as_update_rq(ad, rq); /* keep state machine up to date */
1191 RQ_SET_STATE(rq, AS_RQ_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Jens Axboe165125e2007-07-24 09:28:11 +02001194static void as_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001195{
Jens Axboe8a8e6742006-07-18 21:07:29 +02001196 WARN_ON(RQ_STATE(rq) != AS_RQ_DISPATCHED);
1197 RQ_SET_STATE(rq, AS_RQ_REMOVED);
1198 if (RQ_IOC(rq) && RQ_IOC(rq)->aic)
1199 atomic_dec(&RQ_IOC(rq)->aic->nr_dispatched);
Jens Axboeb4878f22005-10-20 16:42:29 +02001200}
1201
Jens Axboe165125e2007-07-24 09:28:11 +02001202static void as_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Jens Axboe8a8e6742006-07-18 21:07:29 +02001204 WARN_ON(RQ_STATE(rq) != AS_RQ_REMOVED);
1205 RQ_SET_STATE(rq, AS_RQ_DISPATCHED);
1206 if (RQ_IOC(rq) && RQ_IOC(rq)->aic)
1207 atomic_inc(&RQ_IOC(rq)->aic->nr_dispatched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210/*
1211 * as_queue_empty tells us if there are requests left in the device. It may
1212 * not be the case that a driver can get the next request even if the queue
1213 * is not empty - it is used in the block layer to check for plugging and
1214 * merging opportunities
1215 */
Jens Axboe165125e2007-07-24 09:28:11 +02001216static int as_queue_empty(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 struct as_data *ad = q->elevator->elevator_data;
1219
Jens Axboeb4878f22005-10-20 16:42:29 +02001220 return list_empty(&ad->fifo_list[REQ_ASYNC])
1221 && list_empty(&ad->fifo_list[REQ_SYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224static int
Jens Axboe165125e2007-07-24 09:28:11 +02001225as_merge(struct request_queue *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 struct as_data *ad = q->elevator->elevator_data;
1228 sector_t rb_key = bio->bi_sector + bio_sectors(bio);
1229 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /*
1232 * check for front merge
1233 */
Jens Axboee37f3462006-07-18 21:06:01 +02001234 __rq = elv_rb_find(&ad->sort_list[bio_data_dir(bio)], rb_key);
Jens Axboe98170642006-07-28 09:23:08 +02001235 if (__rq && elv_rq_merge_ok(__rq, bio)) {
1236 *req = __rq;
1237 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
1240 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Jens Axboe165125e2007-07-24 09:28:11 +02001243static void as_merged_request(struct request_queue *q, struct request *req,
1244 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 * if the merge was a front merge, we need to reposition request
1250 */
Jens Axboee37f3462006-07-18 21:06:01 +02001251 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe8a8e6742006-07-18 21:07:29 +02001252 as_del_rq_rb(ad, req);
1253 as_add_rq_rb(ad, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /*
1255 * Note! At this stage of this and the next function, our next
1256 * request may not be optimal - eg the request may have "grown"
1257 * behind the disk head. We currently don't bother adjusting.
1258 */
1259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
Jens Axboe165125e2007-07-24 09:28:11 +02001262static void as_merged_requests(struct request_queue *q, struct request *req,
Nick Pigginf5b3db02005-11-07 00:59:53 -08001263 struct request *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 /*
Jens Axboe8a8e6742006-07-18 21:07:29 +02001266 * if next expires before rq, assign its expire time to arq
1267 * and move into next position (next will be deleted) in fifo
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 */
Jens Axboed4f2f462006-07-13 09:12:14 +02001269 if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
1270 if (time_before(rq_fifo_time(next), rq_fifo_time(req))) {
Jens Axboe8a8e6742006-07-18 21:07:29 +02001271 struct io_context *rioc = RQ_IOC(req);
1272 struct io_context *nioc = RQ_IOC(next);
1273
Jens Axboed4f2f462006-07-13 09:12:14 +02001274 list_move(&req->queuelist, &next->queuelist);
1275 rq_set_fifo_time(req, rq_fifo_time(next));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /*
1277 * Don't copy here but swap, because when anext is
1278 * removed below, it must contain the unused context
1279 */
Jens Axboe149a0512008-01-29 22:25:18 +01001280 if (rioc != nioc) {
1281 double_spin_lock(&rioc->lock, &nioc->lock,
1282 rioc < nioc);
1283 swap_io_context(&rioc, &nioc);
1284 double_spin_unlock(&rioc->lock, &nioc->lock,
1285 rioc < nioc);
1286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288 }
1289
1290 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 * kill knowledge of next, this one is a goner
1292 */
1293 as_remove_queued_request(q, next);
Jens Axboe8a8e6742006-07-18 21:07:29 +02001294 as_put_io_context(next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Jens Axboe8a8e6742006-07-18 21:07:29 +02001296 RQ_SET_STATE(next, AS_RQ_MERGED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
1299/*
1300 * This is executed in a "deferred" process context, by kblockd. It calls the
1301 * driver's request_fn so the driver can submit that request.
1302 *
1303 * IMPORTANT! This guy will reenter the elevator, so set up all queue global
1304 * state before calling, and don't rely on any state over calls.
1305 *
1306 * FIXME! dispatch queue is not a queue at all!
1307 */
David Howells65f27f32006-11-22 14:55:48 +00001308static void as_work_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
David Howells65f27f32006-11-22 14:55:48 +00001310 struct as_data *ad = container_of(work, struct as_data, antic_work);
1311 struct request_queue *q = ad->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 unsigned long flags;
1313
1314 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboedc72ef42006-07-20 14:54:05 +02001315 blk_start_queueing(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 spin_unlock_irqrestore(q->queue_lock, flags);
1317}
1318
Jens Axboe165125e2007-07-24 09:28:11 +02001319static int as_may_queue(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 int ret = ELV_MQUEUE_MAY;
1322 struct as_data *ad = q->elevator->elevator_data;
1323 struct io_context *ioc;
1324 if (ad->antic_status == ANTIC_WAIT_REQ ||
1325 ad->antic_status == ANTIC_WAIT_NEXT) {
Jens Axboeb5deef92006-07-19 23:39:40 +02001326 ioc = as_get_io_context(q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (ad->io_context == ioc)
1328 ret = ELV_MQUEUE_MUST;
1329 put_io_context(ioc);
1330 }
1331
1332 return ret;
1333}
1334
1335static void as_exit_queue(elevator_t *e)
1336{
1337 struct as_data *ad = e->elevator_data;
1338
1339 del_timer_sync(&ad->antic_timer);
Andrew Morton19a75d82007-05-09 02:33:56 -07001340 kblockd_flush_work(&ad->antic_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
1343 BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 put_io_context(ad->io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 kfree(ad);
1347}
1348
1349/*
Jens Axboe8a8e6742006-07-18 21:07:29 +02001350 * initialize elevator private data (as_data).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 */
Jens Axboe165125e2007-07-24 09:28:11 +02001352static void *as_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 struct as_data *ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Christoph Lameter94f60302007-07-17 04:03:29 -07001356 ad = kmalloc_node(sizeof(*ad), GFP_KERNEL | __GFP_ZERO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (!ad)
Jens Axboebc1c1162006-06-08 08:49:06 +02001358 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 ad->q = q; /* Identify what queue the data belongs to */
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /* anticipatory scheduling helpers */
1363 ad->antic_timer.function = as_antic_timeout;
1364 ad->antic_timer.data = (unsigned long)q;
1365 init_timer(&ad->antic_timer);
David Howells65f27f32006-11-22 14:55:48 +00001366 INIT_WORK(&ad->antic_work, as_work_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
1369 INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
1370 ad->sort_list[REQ_SYNC] = RB_ROOT;
1371 ad->sort_list[REQ_ASYNC] = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 ad->fifo_expire[REQ_SYNC] = default_read_expire;
1373 ad->fifo_expire[REQ_ASYNC] = default_write_expire;
1374 ad->antic_expire = default_antic_expire;
1375 ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
1376 ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
1379 ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
1380 if (ad->write_batch_count < 2)
1381 ad->write_batch_count = 2;
1382
Jens Axboebc1c1162006-06-08 08:49:06 +02001383 return ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386/*
1387 * sysfs parts below
1388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390static ssize_t
1391as_var_show(unsigned int var, char *page)
1392{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return sprintf(page, "%d\n", var);
1394}
1395
1396static ssize_t
1397as_var_store(unsigned long *var, const char *page, size_t count)
1398{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 char *p = (char *) page;
1400
Jens Axboec9b3ad62005-07-27 11:43:37 -07001401 *var = simple_strtoul(p, &p, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return count;
1403}
1404
Al Viroe572ec72006-03-18 22:27:18 -05001405static ssize_t est_time_show(elevator_t *e, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Al Viro3d1ab402006-03-18 18:35:43 -05001407 struct as_data *ad = e->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int pos = 0;
1409
Nick Pigginf5b3db02005-11-07 00:59:53 -08001410 pos += sprintf(page+pos, "%lu %% exit probability\n",
1411 100*ad->exit_prob/256);
1412 pos += sprintf(page+pos, "%lu %% probability of exiting without a "
1413 "cooperating process submitting IO\n",
1414 100*ad->exit_no_coop/256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
Nick Pigginf5b3db02005-11-07 00:59:53 -08001416 pos += sprintf(page+pos, "%llu sectors new seek distance\n",
1417 (unsigned long long)ad->new_seek_mean);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 return pos;
1420}
1421
1422#define SHOW_FUNCTION(__FUNC, __VAR) \
Al Viro3d1ab402006-03-18 18:35:43 -05001423static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{ \
Al Viro3d1ab402006-03-18 18:35:43 -05001425 struct as_data *ad = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1427}
Al Viroe572ec72006-03-18 22:27:18 -05001428SHOW_FUNCTION(as_read_expire_show, ad->fifo_expire[REQ_SYNC]);
1429SHOW_FUNCTION(as_write_expire_show, ad->fifo_expire[REQ_ASYNC]);
1430SHOW_FUNCTION(as_antic_expire_show, ad->antic_expire);
1431SHOW_FUNCTION(as_read_batch_expire_show, ad->batch_expire[REQ_SYNC]);
1432SHOW_FUNCTION(as_write_batch_expire_show, ad->batch_expire[REQ_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433#undef SHOW_FUNCTION
1434
1435#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
Al Viro3d1ab402006-03-18 18:35:43 -05001436static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{ \
Al Viro3d1ab402006-03-18 18:35:43 -05001438 struct as_data *ad = e->elevator_data; \
1439 int ret = as_var_store(__PTR, (page), count); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (*(__PTR) < (MIN)) \
1441 *(__PTR) = (MIN); \
1442 else if (*(__PTR) > (MAX)) \
1443 *(__PTR) = (MAX); \
1444 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1445 return ret; \
1446}
Al Viroe572ec72006-03-18 22:27:18 -05001447STORE_FUNCTION(as_read_expire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
1448STORE_FUNCTION(as_write_expire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
1449STORE_FUNCTION(as_antic_expire_store, &ad->antic_expire, 0, INT_MAX);
1450STORE_FUNCTION(as_read_batch_expire_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
Al Viroe572ec72006-03-18 22:27:18 -05001452STORE_FUNCTION(as_write_batch_expire_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
1454#undef STORE_FUNCTION
1455
Al Viroe572ec72006-03-18 22:27:18 -05001456#define AS_ATTR(name) \
1457 __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Al Viroe572ec72006-03-18 22:27:18 -05001459static struct elv_fs_entry as_attrs[] = {
1460 __ATTR_RO(est_time),
1461 AS_ATTR(read_expire),
1462 AS_ATTR(write_expire),
1463 AS_ATTR(antic_expire),
1464 AS_ATTR(read_batch_expire),
1465 AS_ATTR(write_batch_expire),
1466 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467};
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469static struct elevator_type iosched_as = {
1470 .ops = {
1471 .elevator_merge_fn = as_merge,
1472 .elevator_merged_fn = as_merged_request,
1473 .elevator_merge_req_fn = as_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02001474 .elevator_dispatch_fn = as_dispatch_request,
1475 .elevator_add_req_fn = as_add_request,
1476 .elevator_activate_req_fn = as_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 .elevator_deactivate_req_fn = as_deactivate_request,
1478 .elevator_queue_empty_fn = as_queue_empty,
1479 .elevator_completed_req_fn = as_completed_request,
Jens Axboee37f3462006-07-18 21:06:01 +02001480 .elevator_former_req_fn = elv_rb_former_request,
1481 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 .elevator_may_queue_fn = as_may_queue,
1483 .elevator_init_fn = as_init_queue,
1484 .elevator_exit_fn = as_exit_queue,
Al Viroe17a9482006-03-18 13:21:20 -05001485 .trim = as_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 },
1487
Al Viro3d1ab402006-03-18 18:35:43 -05001488 .elevator_attrs = as_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 .elevator_name = "anticipatory",
1490 .elevator_owner = THIS_MODULE,
1491};
1492
1493static int __init as_init(void)
1494{
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01001495 elv_register(&iosched_as);
1496
1497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500static void __exit as_exit(void)
1501{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001502 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 elv_unregister(&iosched_as);
Al Viro334e94d2006-03-18 15:05:53 -05001504 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02001505 /* ioc_gone's update must be visible before reading ioc_count */
1506 smp_wmb();
Jens Axboee4313dd2006-07-19 05:10:01 +02001507 if (elv_ioc_count_read(ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02001508 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05001509 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
1512module_init(as_init);
1513module_exit(as_exit);
1514
1515MODULE_AUTHOR("Nick Piggin");
1516MODULE_LICENSE("GPL");
1517MODULE_DESCRIPTION("anticipatory IO scheduler");