Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Anticipatory & deadline i/o scheduler. |
| 3 | * |
| 4 | * Copyright (C) 2002 Jens Axboe <axboe@suse.de> |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 5 | * Nick Piggin <nickpiggin@yahoo.com.au> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #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 */ |
| 67 | enum as_io_states { |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 68 | AS_TASK_RUNNING=0, /* Process has not exited */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | AS_TASK_IOSTARTED, /* Process has started some IO */ |
| 70 | AS_TASK_IORUNNING, /* Process has completed some IO */ |
| 71 | }; |
| 72 | |
| 73 | enum anticipation_status { |
| 74 | ANTIC_OFF=0, /* Not anticipating (normal operation) */ |
| 75 | ANTIC_WAIT_REQ, /* The last read has not yet completed */ |
| 76 | ANTIC_WAIT_NEXT, /* Currently anticipating a request vs |
| 77 | last read (which has completed) */ |
| 78 | ANTIC_FINISHED, /* Anticipating but have found a candidate |
| 79 | * or timed out */ |
| 80 | }; |
| 81 | |
| 82 | struct 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 Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 95 | struct request *next_rq[2]; /* next in sort order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | sector_t last_sector[2]; /* last REQ_SYNC & REQ_ASYNC sectors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | unsigned long exit_prob; /* probability a task will exit while |
| 99 | being waited on */ |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 100 | unsigned long exit_no_coop; /* probablility an exited task will |
| 101 | not be part of a later cooperating |
| 102 | request */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | unsigned long new_ttime_total; /* mean thinktime on new proc */ |
| 104 | unsigned long new_ttime_mean; |
| 105 | u64 new_seek_total; /* mean seek on new proc */ |
| 106 | sector_t new_seek_mean; |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | /* |
| 134 | * per-request data. |
| 135 | */ |
| 136 | enum 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 Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 148 | #define RQ_IOC(rq) ((struct io_context *) (rq)->elevator_private) |
| 149 | #define RQ_STATE(rq) ((enum arq_state)(rq)->elevator_private2) |
| 150 | #define RQ_SET_STATE(rq, state) ((rq)->elevator_private2 = (void *) state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 152 | static atomic_t ioc_count = ATOMIC_INIT(0); |
| 153 | static struct completion *ioc_gone; |
| 154 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 155 | static void as_move_to_dispatch(struct as_data *ad, struct request *rq); |
Tejun Heo | ef9be1d | 2005-11-11 14:27:09 +0100 | [diff] [blame] | 156 | static void as_antic_stop(struct as_data *ad); |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | /* |
| 159 | * IO Context helper functions |
| 160 | */ |
| 161 | |
| 162 | /* Called to deallocate the as_io_context */ |
| 163 | static void free_as_io_context(struct as_io_context *aic) |
| 164 | { |
| 165 | kfree(aic); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 166 | if (atomic_dec_and_test(&ioc_count) && ioc_gone) |
| 167 | complete(ioc_gone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 170 | static void as_trim(struct io_context *ioc) |
| 171 | { |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 172 | if (ioc->aic) |
| 173 | free_as_io_context(ioc->aic); |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 174 | ioc->aic = NULL; |
| 175 | } |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | /* Called when the task exits */ |
| 178 | static void exit_as_io_context(struct as_io_context *aic) |
| 179 | { |
| 180 | WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state)); |
| 181 | clear_bit(AS_TASK_RUNNING, &aic->state); |
| 182 | } |
| 183 | |
| 184 | static struct as_io_context *alloc_as_io_context(void) |
| 185 | { |
| 186 | struct as_io_context *ret; |
| 187 | |
| 188 | ret = kmalloc(sizeof(*ret), GFP_ATOMIC); |
| 189 | if (ret) { |
| 190 | ret->dtor = free_as_io_context; |
| 191 | ret->exit = exit_as_io_context; |
| 192 | ret->state = 1 << AS_TASK_RUNNING; |
| 193 | atomic_set(&ret->nr_queued, 0); |
| 194 | atomic_set(&ret->nr_dispatched, 0); |
| 195 | spin_lock_init(&ret->lock); |
| 196 | ret->ttime_total = 0; |
| 197 | ret->ttime_samples = 0; |
| 198 | ret->ttime_mean = 0; |
| 199 | ret->seek_total = 0; |
| 200 | ret->seek_samples = 0; |
| 201 | ret->seek_mean = 0; |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 202 | atomic_inc(&ioc_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * If the current task has no AS IO context then create one and initialise it. |
| 210 | * Then take a ref on the task's io context and return it. |
| 211 | */ |
| 212 | static struct io_context *as_get_io_context(void) |
| 213 | { |
| 214 | struct io_context *ioc = get_io_context(GFP_ATOMIC); |
| 215 | if (ioc && !ioc->aic) { |
| 216 | ioc->aic = alloc_as_io_context(); |
| 217 | if (!ioc->aic) { |
| 218 | put_io_context(ioc); |
| 219 | ioc = NULL; |
| 220 | } |
| 221 | } |
| 222 | return ioc; |
| 223 | } |
| 224 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 225 | static void as_put_io_context(struct request *rq) |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 226 | { |
| 227 | struct as_io_context *aic; |
| 228 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 229 | if (unlikely(!RQ_IOC(rq))) |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 230 | return; |
| 231 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 232 | aic = RQ_IOC(rq)->aic; |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 233 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 234 | if (rq_is_sync(rq) && aic) { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 235 | spin_lock(&aic->lock); |
| 236 | set_bit(AS_TASK_IORUNNING, &aic->state); |
| 237 | aic->last_end_request = jiffies; |
| 238 | spin_unlock(&aic->lock); |
| 239 | } |
| 240 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 241 | put_io_context(RQ_IOC(rq)); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | * rb tree support functions |
| 246 | */ |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 247 | #define RQ_RB_ROOT(ad, rq) (&(ad)->sort_list[rq_is_sync((rq))]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 249 | static void as_add_rq_rb(struct as_data *ad, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 251 | struct request *alias; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 253 | while ((unlikely(alias = elv_rb_add(RQ_RB_ROOT(ad, rq), rq)))) { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 254 | as_move_to_dispatch(ad, alias); |
Tejun Heo | ef9be1d | 2005-11-11 14:27:09 +0100 | [diff] [blame] | 255 | as_antic_stop(ad); |
| 256 | } |
| 257 | } |
| 258 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 259 | static inline void as_del_rq_rb(struct as_data *ad, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 261 | elv_rb_del(RQ_RB_ROOT(ad, rq), rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* |
| 265 | * IO Scheduler proper |
| 266 | */ |
| 267 | |
| 268 | #define MAXBACK (1024 * 1024) /* |
| 269 | * Maximum distance the disk will go backward |
| 270 | * for a request. |
| 271 | */ |
| 272 | |
| 273 | #define BACK_PENALTY 2 |
| 274 | |
| 275 | /* |
| 276 | * as_choose_req selects the preferred one of two requests of the same data_dir |
| 277 | * ignoring time - eg. timeouts, which is the job of as_dispatch_request |
| 278 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 279 | static struct request * |
| 280 | as_choose_req(struct as_data *ad, struct request *rq1, struct request *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
| 282 | int data_dir; |
| 283 | sector_t last, s1, s2, d1, d2; |
| 284 | int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */ |
| 285 | const sector_t maxback = MAXBACK; |
| 286 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 287 | if (rq1 == NULL || rq1 == rq2) |
| 288 | return rq2; |
| 289 | if (rq2 == NULL) |
| 290 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 292 | data_dir = rq_is_sync(rq1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | last = ad->last_sector[data_dir]; |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 295 | s1 = rq1->sector; |
| 296 | s2 = rq2->sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 298 | BUG_ON(data_dir != rq_is_sync(rq2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | /* |
| 301 | * Strict one way elevator _except_ in the case where we allow |
| 302 | * short backward seeks which are biased as twice the cost of a |
| 303 | * similar forward seek. |
| 304 | */ |
| 305 | if (s1 >= last) |
| 306 | d1 = s1 - last; |
| 307 | else if (s1+maxback >= last) |
| 308 | d1 = (last - s1)*BACK_PENALTY; |
| 309 | else { |
| 310 | r1_wrap = 1; |
| 311 | d1 = 0; /* shut up, gcc */ |
| 312 | } |
| 313 | |
| 314 | if (s2 >= last) |
| 315 | d2 = s2 - last; |
| 316 | else if (s2+maxback >= last) |
| 317 | d2 = (last - s2)*BACK_PENALTY; |
| 318 | else { |
| 319 | r2_wrap = 1; |
| 320 | d2 = 0; |
| 321 | } |
| 322 | |
| 323 | /* Found required data */ |
| 324 | if (!r1_wrap && r2_wrap) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 325 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | else if (!r2_wrap && r1_wrap) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 327 | return rq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | else if (r1_wrap && r2_wrap) { |
| 329 | /* both behind the head */ |
| 330 | if (s1 <= s2) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 331 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | else |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 333 | return rq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Both requests in front of the head */ |
| 337 | if (d1 < d2) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 338 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | else if (d2 < d1) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 340 | return rq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | else { |
| 342 | if (s1 >= s2) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 343 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | else |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 345 | return rq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 350 | * as_find_next_rq finds the next request after @prev in elevator order. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | * this with as_choose_req form the basis for how the scheduler chooses |
| 352 | * what request to process next. Anticipation works on top of this. |
| 353 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 354 | static struct request * |
| 355 | as_find_next_rq(struct as_data *ad, struct request *last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | struct rb_node *rbnext = rb_next(&last->rb_node); |
| 358 | struct rb_node *rbprev = rb_prev(&last->rb_node); |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 359 | struct request *next = NULL, *prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 361 | BUG_ON(RB_EMPTY_NODE(&last->rb_node)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
| 363 | if (rbprev) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 364 | prev = rb_entry_rq(rbprev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | if (rbnext) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 367 | next = rb_entry_rq(rbnext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | else { |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 369 | const int data_dir = rq_is_sync(last); |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 370 | |
| 371 | rbnext = rb_first(&ad->sort_list[data_dir]); |
| 372 | if (rbnext && rbnext != &last->rb_node) |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 373 | next = rb_entry_rq(rbnext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 376 | return as_choose_req(ad, next, prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* |
| 380 | * anticipatory scheduling functions follow |
| 381 | */ |
| 382 | |
| 383 | /* |
| 384 | * as_antic_expired tells us when we have anticipated too long. |
| 385 | * The funny "absolute difference" math on the elapsed time is to handle |
| 386 | * jiffy wraps, and disks which have been idle for 0x80000000 jiffies. |
| 387 | */ |
| 388 | static int as_antic_expired(struct as_data *ad) |
| 389 | { |
| 390 | long delta_jif; |
| 391 | |
| 392 | delta_jif = jiffies - ad->antic_start; |
| 393 | if (unlikely(delta_jif < 0)) |
| 394 | delta_jif = -delta_jif; |
| 395 | if (delta_jif < ad->antic_expire) |
| 396 | return 0; |
| 397 | |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * as_antic_waitnext starts anticipating that a nice request will soon be |
| 403 | * submitted. See also as_antic_waitreq |
| 404 | */ |
| 405 | static void as_antic_waitnext(struct as_data *ad) |
| 406 | { |
| 407 | unsigned long timeout; |
| 408 | |
| 409 | BUG_ON(ad->antic_status != ANTIC_OFF |
| 410 | && ad->antic_status != ANTIC_WAIT_REQ); |
| 411 | |
| 412 | timeout = ad->antic_start + ad->antic_expire; |
| 413 | |
| 414 | mod_timer(&ad->antic_timer, timeout); |
| 415 | |
| 416 | ad->antic_status = ANTIC_WAIT_NEXT; |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * as_antic_waitreq starts anticipating. We don't start timing the anticipation |
| 421 | * until the request that we're anticipating on has finished. This means we |
| 422 | * are timing from when the candidate process wakes up hopefully. |
| 423 | */ |
| 424 | static void as_antic_waitreq(struct as_data *ad) |
| 425 | { |
| 426 | BUG_ON(ad->antic_status == ANTIC_FINISHED); |
| 427 | if (ad->antic_status == ANTIC_OFF) { |
| 428 | if (!ad->io_context || ad->ioc_finished) |
| 429 | as_antic_waitnext(ad); |
| 430 | else |
| 431 | ad->antic_status = ANTIC_WAIT_REQ; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * This is called directly by the functions in this file to stop anticipation. |
| 437 | * We kill the timer and schedule a call to the request_fn asap. |
| 438 | */ |
| 439 | static void as_antic_stop(struct as_data *ad) |
| 440 | { |
| 441 | int status = ad->antic_status; |
| 442 | |
| 443 | if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) { |
| 444 | if (status == ANTIC_WAIT_NEXT) |
| 445 | del_timer(&ad->antic_timer); |
| 446 | ad->antic_status = ANTIC_FINISHED; |
| 447 | /* see as_work_handler */ |
| 448 | kblockd_schedule_work(&ad->antic_work); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * as_antic_timeout is the timer function set by as_antic_waitnext. |
| 454 | */ |
| 455 | static void as_antic_timeout(unsigned long data) |
| 456 | { |
| 457 | struct request_queue *q = (struct request_queue *)data; |
| 458 | struct as_data *ad = q->elevator->elevator_data; |
| 459 | unsigned long flags; |
| 460 | |
| 461 | spin_lock_irqsave(q->queue_lock, flags); |
| 462 | if (ad->antic_status == ANTIC_WAIT_REQ |
| 463 | || ad->antic_status == ANTIC_WAIT_NEXT) { |
| 464 | struct as_io_context *aic = ad->io_context->aic; |
| 465 | |
| 466 | ad->antic_status = ANTIC_FINISHED; |
| 467 | kblockd_schedule_work(&ad->antic_work); |
| 468 | |
| 469 | if (aic->ttime_samples == 0) { |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 470 | /* process anticipated on has exited or timed out*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | ad->exit_prob = (7*ad->exit_prob + 256)/8; |
| 472 | } |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 473 | if (!test_bit(AS_TASK_RUNNING, &aic->state)) { |
| 474 | /* process not "saved" by a cooperating request */ |
| 475 | ad->exit_no_coop = (7*ad->exit_no_coop + 256)/8; |
| 476 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 479 | } |
| 480 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 481 | static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic, |
| 482 | unsigned long ttime) |
| 483 | { |
| 484 | /* fixed point: 1.0 == 1<<8 */ |
| 485 | if (aic->ttime_samples == 0) { |
| 486 | ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8; |
| 487 | ad->new_ttime_mean = ad->new_ttime_total / 256; |
| 488 | |
| 489 | ad->exit_prob = (7*ad->exit_prob)/8; |
| 490 | } |
| 491 | aic->ttime_samples = (7*aic->ttime_samples + 256) / 8; |
| 492 | aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8; |
| 493 | aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples; |
| 494 | } |
| 495 | |
| 496 | static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic, |
| 497 | sector_t sdist) |
| 498 | { |
| 499 | u64 total; |
| 500 | |
| 501 | if (aic->seek_samples == 0) { |
| 502 | ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8; |
| 503 | ad->new_seek_mean = ad->new_seek_total / 256; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Don't allow the seek distance to get too large from the |
| 508 | * odd fragment, pagein, etc |
| 509 | */ |
| 510 | if (aic->seek_samples <= 60) /* second&third seek */ |
| 511 | sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024); |
| 512 | else |
| 513 | sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64); |
| 514 | |
| 515 | aic->seek_samples = (7*aic->seek_samples + 256) / 8; |
| 516 | aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8; |
| 517 | total = aic->seek_total + (aic->seek_samples/2); |
| 518 | do_div(total, aic->seek_samples); |
| 519 | aic->seek_mean = (sector_t)total; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * as_update_iohist keeps a decaying histogram of IO thinktimes, and |
| 524 | * updates @aic->ttime_mean based on that. It is called when a new |
| 525 | * request is queued. |
| 526 | */ |
| 527 | static void as_update_iohist(struct as_data *ad, struct as_io_context *aic, |
| 528 | struct request *rq) |
| 529 | { |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 530 | int data_dir = rq_is_sync(rq); |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 531 | unsigned long thinktime = 0; |
| 532 | sector_t seek_dist; |
| 533 | |
| 534 | if (aic == NULL) |
| 535 | return; |
| 536 | |
| 537 | if (data_dir == REQ_SYNC) { |
| 538 | unsigned long in_flight = atomic_read(&aic->nr_queued) |
| 539 | + atomic_read(&aic->nr_dispatched); |
| 540 | spin_lock(&aic->lock); |
| 541 | if (test_bit(AS_TASK_IORUNNING, &aic->state) || |
| 542 | test_bit(AS_TASK_IOSTARTED, &aic->state)) { |
| 543 | /* Calculate read -> read thinktime */ |
| 544 | if (test_bit(AS_TASK_IORUNNING, &aic->state) |
| 545 | && in_flight == 0) { |
| 546 | thinktime = jiffies - aic->last_end_request; |
| 547 | thinktime = min(thinktime, MAX_THINKTIME-1); |
| 548 | } |
| 549 | as_update_thinktime(ad, aic, thinktime); |
| 550 | |
| 551 | /* Calculate read -> read seek distance */ |
| 552 | if (aic->last_request_pos < rq->sector) |
| 553 | seek_dist = rq->sector - aic->last_request_pos; |
| 554 | else |
| 555 | seek_dist = aic->last_request_pos - rq->sector; |
| 556 | as_update_seekdist(ad, aic, seek_dist); |
| 557 | } |
| 558 | aic->last_request_pos = rq->sector + rq->nr_sectors; |
| 559 | set_bit(AS_TASK_IOSTARTED, &aic->state); |
| 560 | spin_unlock(&aic->lock); |
| 561 | } |
| 562 | } |
| 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | /* |
| 565 | * as_close_req decides if one request is considered "close" to the |
| 566 | * previous one issued. |
| 567 | */ |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 568 | static int as_close_req(struct as_data *ad, struct as_io_context *aic, |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 569 | struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | { |
| 571 | unsigned long delay; /* milliseconds */ |
| 572 | sector_t last = ad->last_sector[ad->batch_data_dir]; |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 573 | sector_t next = rq->sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | sector_t delta; /* acceptable close offset (in sectors) */ |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 575 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished) |
| 578 | delay = 0; |
| 579 | else |
| 580 | delay = ((jiffies - ad->antic_start) * 1000) / HZ; |
| 581 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 582 | if (delay == 0) |
| 583 | delta = 8192; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | else if (delay <= 20 && delay <= ad->antic_expire) |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 585 | delta = 8192 << delay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | else |
| 587 | return 1; |
| 588 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 589 | if ((last <= next + (delta>>1)) && (next <= last + delta)) |
| 590 | return 1; |
| 591 | |
| 592 | if (last < next) |
| 593 | s = next - last; |
| 594 | else |
| 595 | s = last - next; |
| 596 | |
| 597 | if (aic->seek_samples == 0) { |
| 598 | /* |
| 599 | * Process has just started IO. Use past statistics to |
| 600 | * gauge success possibility |
| 601 | */ |
| 602 | if (ad->new_seek_mean > s) { |
| 603 | /* this request is better than what we're expecting */ |
| 604 | return 1; |
| 605 | } |
| 606 | |
| 607 | } else { |
| 608 | if (aic->seek_mean > s) { |
| 609 | /* this request is better than what we're expecting */ |
| 610 | return 1; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /* |
| 618 | * as_can_break_anticipation returns true if we have been anticipating this |
| 619 | * request. |
| 620 | * |
| 621 | * It also returns true if the process against which we are anticipating |
| 622 | * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to |
| 623 | * dispatch it ASAP, because we know that application will not be submitting |
| 624 | * any new reads. |
| 625 | * |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 626 | * If the task which has submitted the request has exited, break anticipation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | * |
| 628 | * If this task has queued some other IO, do not enter enticipation. |
| 629 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 630 | static int as_can_break_anticipation(struct as_data *ad, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | { |
| 632 | struct io_context *ioc; |
| 633 | struct as_io_context *aic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
| 635 | ioc = ad->io_context; |
| 636 | BUG_ON(!ioc); |
| 637 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 638 | if (rq && ioc == RQ_IOC(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | /* request from same process */ |
| 640 | return 1; |
| 641 | } |
| 642 | |
| 643 | if (ad->ioc_finished && as_antic_expired(ad)) { |
| 644 | /* |
| 645 | * In this situation status should really be FINISHED, |
| 646 | * however the timer hasn't had the chance to run yet. |
| 647 | */ |
| 648 | return 1; |
| 649 | } |
| 650 | |
| 651 | aic = ioc->aic; |
| 652 | if (!aic) |
| 653 | return 0; |
| 654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | if (atomic_read(&aic->nr_queued) > 0) { |
| 656 | /* process has more requests queued */ |
| 657 | return 1; |
| 658 | } |
| 659 | |
| 660 | if (atomic_read(&aic->nr_dispatched) > 0) { |
| 661 | /* process has more requests dispatched */ |
| 662 | return 1; |
| 663 | } |
| 664 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 665 | if (rq && rq_is_sync(rq) && as_close_req(ad, aic, rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | /* |
| 667 | * Found a close request that is not one of ours. |
| 668 | * |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 669 | * This makes close requests from another process update |
| 670 | * our IO history. Is generally useful when there are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | * two or more cooperating processes working in the same |
| 672 | * area. |
| 673 | */ |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 674 | if (!test_bit(AS_TASK_RUNNING, &aic->state)) { |
| 675 | if (aic->ttime_samples == 0) |
| 676 | ad->exit_prob = (7*ad->exit_prob + 256)/8; |
| 677 | |
| 678 | ad->exit_no_coop = (7*ad->exit_no_coop)/8; |
| 679 | } |
| 680 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 681 | as_update_iohist(ad, aic, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | return 1; |
| 683 | } |
| 684 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 685 | if (!test_bit(AS_TASK_RUNNING, &aic->state)) { |
| 686 | /* process anticipated on has exited */ |
| 687 | if (aic->ttime_samples == 0) |
| 688 | ad->exit_prob = (7*ad->exit_prob + 256)/8; |
| 689 | |
| 690 | if (ad->exit_no_coop > 128) |
| 691 | return 1; |
| 692 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
| 694 | if (aic->ttime_samples == 0) { |
| 695 | if (ad->new_ttime_mean > ad->antic_expire) |
| 696 | return 1; |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 697 | if (ad->exit_prob * ad->exit_no_coop > 128*256) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | return 1; |
| 699 | } else if (aic->ttime_mean > ad->antic_expire) { |
| 700 | /* the process thinks too much between requests */ |
| 701 | return 1; |
| 702 | } |
| 703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 708 | * as_can_anticipate indicates whether we should either run rq |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | * or keep anticipating a better request. |
| 710 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 711 | static int as_can_anticipate(struct as_data *ad, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | { |
| 713 | if (!ad->io_context) |
| 714 | /* |
| 715 | * Last request submitted was a write |
| 716 | */ |
| 717 | return 0; |
| 718 | |
| 719 | if (ad->antic_status == ANTIC_FINISHED) |
| 720 | /* |
| 721 | * Don't restart if we have just finished. Run the next request |
| 722 | */ |
| 723 | return 0; |
| 724 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 725 | if (as_can_break_anticipation(ad, rq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | /* |
| 727 | * This request is a good candidate. Don't keep anticipating, |
| 728 | * run it. |
| 729 | */ |
| 730 | return 0; |
| 731 | |
| 732 | /* |
| 733 | * OK from here, we haven't finished, and don't have a decent request! |
| 734 | * Status is either ANTIC_OFF so start waiting, |
| 735 | * ANTIC_WAIT_REQ so continue waiting for request to finish |
| 736 | * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | */ |
| 738 | |
| 739 | return 1; |
| 740 | } |
| 741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 743 | * as_update_rq must be called whenever a request (rq) is added to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | * the sort_list. This function keeps caches up to date, and checks if the |
| 745 | * request might be one we are "anticipating" |
| 746 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 747 | static void as_update_rq(struct as_data *ad, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 749 | const int data_dir = rq_is_sync(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 751 | /* keep the next_rq cache up to date */ |
| 752 | ad->next_rq[data_dir] = as_choose_req(ad, rq, ad->next_rq[data_dir]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
| 754 | /* |
| 755 | * have we been anticipating this request? |
| 756 | * or does it come from the same process as the one we are anticipating |
| 757 | * for? |
| 758 | */ |
| 759 | if (ad->antic_status == ANTIC_WAIT_REQ |
| 760 | || ad->antic_status == ANTIC_WAIT_NEXT) { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 761 | if (as_can_break_anticipation(ad, rq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | as_antic_stop(ad); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * Gathers timings and resizes the write batch automatically |
| 768 | */ |
| 769 | static void update_write_batch(struct as_data *ad) |
| 770 | { |
| 771 | unsigned long batch = ad->batch_expire[REQ_ASYNC]; |
| 772 | long write_time; |
| 773 | |
| 774 | write_time = (jiffies - ad->current_batch_expires) + batch; |
| 775 | if (write_time < 0) |
| 776 | write_time = 0; |
| 777 | |
| 778 | if (write_time > batch && !ad->write_batch_idled) { |
| 779 | if (write_time > batch * 3) |
| 780 | ad->write_batch_count /= 2; |
| 781 | else |
| 782 | ad->write_batch_count--; |
| 783 | } else if (write_time < batch && ad->current_write_count == 0) { |
| 784 | if (batch > write_time * 3) |
| 785 | ad->write_batch_count *= 2; |
| 786 | else |
| 787 | ad->write_batch_count++; |
| 788 | } |
| 789 | |
| 790 | if (ad->write_batch_count < 1) |
| 791 | ad->write_batch_count = 1; |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * as_completed_request is to be called when a request has completed and |
| 796 | * returned something to the requesting process, be it an error or data. |
| 797 | */ |
| 798 | static void as_completed_request(request_queue_t *q, struct request *rq) |
| 799 | { |
| 800 | struct as_data *ad = q->elevator->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
| 802 | WARN_ON(!list_empty(&rq->queuelist)); |
| 803 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 804 | if (RQ_STATE(rq) != AS_RQ_REMOVED) { |
| 805 | printk("rq->state %d\n", RQ_STATE(rq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | WARN_ON(1); |
| 807 | goto out; |
| 808 | } |
| 809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | if (ad->changed_batch && ad->nr_dispatched == 1) { |
| 811 | kblockd_schedule_work(&ad->antic_work); |
| 812 | ad->changed_batch = 0; |
| 813 | |
| 814 | if (ad->batch_data_dir == REQ_SYNC) |
| 815 | ad->new_batch = 1; |
| 816 | } |
| 817 | WARN_ON(ad->nr_dispatched == 0); |
| 818 | ad->nr_dispatched--; |
| 819 | |
| 820 | /* |
| 821 | * Start counting the batch from when a request of that direction is |
| 822 | * actually serviced. This should help devices with big TCQ windows |
| 823 | * and writeback caches |
| 824 | */ |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 825 | if (ad->new_batch && ad->batch_data_dir == rq_is_sync(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | update_write_batch(ad); |
| 827 | ad->current_batch_expires = jiffies + |
| 828 | ad->batch_expire[REQ_SYNC]; |
| 829 | ad->new_batch = 0; |
| 830 | } |
| 831 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 832 | if (ad->io_context == RQ_IOC(rq) && ad->io_context) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | ad->antic_start = jiffies; |
| 834 | ad->ioc_finished = 1; |
| 835 | if (ad->antic_status == ANTIC_WAIT_REQ) { |
| 836 | /* |
| 837 | * We were waiting on this request, now anticipate |
| 838 | * the next one |
| 839 | */ |
| 840 | as_antic_waitnext(ad); |
| 841 | } |
| 842 | } |
| 843 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 844 | as_put_io_context(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | out: |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 846 | RQ_SET_STATE(rq, AS_RQ_POSTSCHED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /* |
| 850 | * as_remove_queued_request removes a request from the pre dispatch queue |
| 851 | * without updating refcounts. It is expected the caller will drop the |
| 852 | * reference unless it replaces the request at somepart of the elevator |
| 853 | * (ie. the dispatch queue) |
| 854 | */ |
| 855 | static void as_remove_queued_request(request_queue_t *q, struct request *rq) |
| 856 | { |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 857 | const int data_dir = rq_is_sync(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | struct as_data *ad = q->elevator->elevator_data; |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 859 | struct io_context *ioc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 861 | WARN_ON(RQ_STATE(rq) != AS_RQ_QUEUED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 863 | ioc = RQ_IOC(rq); |
| 864 | if (ioc && ioc->aic) { |
| 865 | BUG_ON(!atomic_read(&ioc->aic->nr_queued)); |
| 866 | atomic_dec(&ioc->aic->nr_queued); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 870 | * Update the "next_rq" cache if we are about to remove its |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | * entry |
| 872 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 873 | if (ad->next_rq[data_dir] == rq) |
| 874 | ad->next_rq[data_dir] = as_find_next_rq(ad, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 876 | rq_fifo_clear(rq); |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 877 | as_del_rq_rb(ad, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | * as_fifo_expired returns 0 if there are no expired reads on the fifo, |
| 882 | * 1 otherwise. It is ratelimited so that we only perform the check once per |
| 883 | * `fifo_expire' interval. Otherwise a large number of expired requests |
| 884 | * would create a hopeless seekstorm. |
| 885 | * |
| 886 | * See as_antic_expired comment. |
| 887 | */ |
| 888 | static int as_fifo_expired(struct as_data *ad, int adir) |
| 889 | { |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 890 | struct request *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | long delta_jif; |
| 892 | |
| 893 | delta_jif = jiffies - ad->last_check_fifo[adir]; |
| 894 | if (unlikely(delta_jif < 0)) |
| 895 | delta_jif = -delta_jif; |
| 896 | if (delta_jif < ad->fifo_expire[adir]) |
| 897 | return 0; |
| 898 | |
| 899 | ad->last_check_fifo[adir] = jiffies; |
| 900 | |
| 901 | if (list_empty(&ad->fifo_list[adir])) |
| 902 | return 0; |
| 903 | |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 904 | rq = rq_entry_fifo(ad->fifo_list[adir].next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 906 | return time_after(jiffies, rq_fifo_time(rq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /* |
| 910 | * as_batch_expired returns true if the current batch has expired. A batch |
| 911 | * is a set of reads or a set of writes. |
| 912 | */ |
| 913 | static inline int as_batch_expired(struct as_data *ad) |
| 914 | { |
| 915 | if (ad->changed_batch || ad->new_batch) |
| 916 | return 0; |
| 917 | |
| 918 | if (ad->batch_data_dir == REQ_SYNC) |
| 919 | /* TODO! add a check so a complete fifo gets written? */ |
| 920 | return time_after(jiffies, ad->current_batch_expires); |
| 921 | |
| 922 | return time_after(jiffies, ad->current_batch_expires) |
| 923 | || ad->current_write_count == 0; |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * move an entry to dispatch queue |
| 928 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 929 | static void as_move_to_dispatch(struct as_data *ad, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | { |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 931 | const int data_dir = rq_is_sync(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 933 | BUG_ON(RB_EMPTY_NODE(&rq->rb_node)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | |
| 935 | as_antic_stop(ad); |
| 936 | ad->antic_status = ANTIC_OFF; |
| 937 | |
| 938 | /* |
| 939 | * This has to be set in order to be correctly updated by |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 940 | * as_find_next_rq |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | */ |
| 942 | ad->last_sector[data_dir] = rq->sector + rq->nr_sectors; |
| 943 | |
| 944 | if (data_dir == REQ_SYNC) { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 945 | struct io_context *ioc = RQ_IOC(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | /* In case we have to anticipate after this */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 947 | copy_io_context(&ad->io_context, &ioc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | } else { |
| 949 | if (ad->io_context) { |
| 950 | put_io_context(ad->io_context); |
| 951 | ad->io_context = NULL; |
| 952 | } |
| 953 | |
| 954 | if (ad->current_write_count != 0) |
| 955 | ad->current_write_count--; |
| 956 | } |
| 957 | ad->ioc_finished = 0; |
| 958 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 959 | ad->next_rq[data_dir] = as_find_next_rq(ad, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | |
| 961 | /* |
| 962 | * take it off the sort and fifo list, add to dispatch queue |
| 963 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | as_remove_queued_request(ad->q, rq); |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 965 | WARN_ON(RQ_STATE(rq) != AS_RQ_QUEUED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 967 | elv_dispatch_sort(ad->q, rq); |
| 968 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 969 | RQ_SET_STATE(rq, AS_RQ_DISPATCHED); |
| 970 | if (RQ_IOC(rq) && RQ_IOC(rq)->aic) |
| 971 | atomic_inc(&RQ_IOC(rq)->aic->nr_dispatched); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | ad->nr_dispatched++; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * as_dispatch_request selects the best request according to |
| 977 | * read/write expire, batch expire, etc, and moves it to the dispatch |
| 978 | * queue. Returns 1 if a request was found, 0 otherwise. |
| 979 | */ |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 980 | static int as_dispatch_request(request_queue_t *q, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 982 | struct as_data *ad = q->elevator->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]); |
| 984 | const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]); |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 985 | struct request *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 987 | if (unlikely(force)) { |
| 988 | /* |
| 989 | * Forced dispatch, accounting is useless. Reset |
| 990 | * accounting states and dump fifo_lists. Note that |
| 991 | * batch_data_dir is reset to REQ_SYNC to avoid |
| 992 | * screwing write batch accounting as write batch |
| 993 | * accounting occurs on W->R transition. |
| 994 | */ |
| 995 | int dispatched = 0; |
| 996 | |
| 997 | ad->batch_data_dir = REQ_SYNC; |
| 998 | ad->changed_batch = 0; |
| 999 | ad->new_batch = 0; |
| 1000 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1001 | while (ad->next_rq[REQ_SYNC]) { |
| 1002 | as_move_to_dispatch(ad, ad->next_rq[REQ_SYNC]); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1003 | dispatched++; |
| 1004 | } |
| 1005 | ad->last_check_fifo[REQ_SYNC] = jiffies; |
| 1006 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1007 | while (ad->next_rq[REQ_ASYNC]) { |
| 1008 | as_move_to_dispatch(ad, ad->next_rq[REQ_ASYNC]); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1009 | dispatched++; |
| 1010 | } |
| 1011 | ad->last_check_fifo[REQ_ASYNC] = jiffies; |
| 1012 | |
| 1013 | return dispatched; |
| 1014 | } |
| 1015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | /* Signal that the write batch was uncontended, so we can't time it */ |
| 1017 | if (ad->batch_data_dir == REQ_ASYNC && !reads) { |
| 1018 | if (ad->current_write_count == 0 || !writes) |
| 1019 | ad->write_batch_idled = 1; |
| 1020 | } |
| 1021 | |
| 1022 | if (!(reads || writes) |
| 1023 | || ad->antic_status == ANTIC_WAIT_REQ |
| 1024 | || ad->antic_status == ANTIC_WAIT_NEXT |
| 1025 | || ad->changed_batch) |
| 1026 | return 0; |
| 1027 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 1028 | if (!(reads && writes && as_batch_expired(ad))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | /* |
| 1030 | * batch is still running or no reads or no writes |
| 1031 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1032 | rq = ad->next_rq[ad->batch_data_dir]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
| 1034 | if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) { |
| 1035 | if (as_fifo_expired(ad, REQ_SYNC)) |
| 1036 | goto fifo_expired; |
| 1037 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1038 | if (as_can_anticipate(ad, rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | as_antic_waitreq(ad); |
| 1040 | return 0; |
| 1041 | } |
| 1042 | } |
| 1043 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1044 | if (rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | /* we have a "next request" */ |
| 1046 | if (reads && !writes) |
| 1047 | ad->current_batch_expires = |
| 1048 | jiffies + ad->batch_expire[REQ_SYNC]; |
| 1049 | goto dispatch_request; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * at this point we are not running a batch. select the appropriate |
| 1055 | * data direction (read / write) |
| 1056 | */ |
| 1057 | |
| 1058 | if (reads) { |
Jens Axboe | dd67d05 | 2006-06-21 09:36:18 +0200 | [diff] [blame] | 1059 | BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_SYNC])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | |
| 1061 | if (writes && ad->batch_data_dir == REQ_SYNC) |
| 1062 | /* |
| 1063 | * Last batch was a read, switch to writes |
| 1064 | */ |
| 1065 | goto dispatch_writes; |
| 1066 | |
| 1067 | if (ad->batch_data_dir == REQ_ASYNC) { |
| 1068 | WARN_ON(ad->new_batch); |
| 1069 | ad->changed_batch = 1; |
| 1070 | } |
| 1071 | ad->batch_data_dir = REQ_SYNC; |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1072 | rq = rq_entry_fifo(ad->fifo_list[REQ_SYNC].next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | ad->last_check_fifo[ad->batch_data_dir] = jiffies; |
| 1074 | goto dispatch_request; |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * the last batch was a read |
| 1079 | */ |
| 1080 | |
| 1081 | if (writes) { |
| 1082 | dispatch_writes: |
Jens Axboe | dd67d05 | 2006-06-21 09:36:18 +0200 | [diff] [blame] | 1083 | BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_ASYNC])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
| 1085 | if (ad->batch_data_dir == REQ_SYNC) { |
| 1086 | ad->changed_batch = 1; |
| 1087 | |
| 1088 | /* |
| 1089 | * new_batch might be 1 when the queue runs out of |
| 1090 | * reads. A subsequent submission of a write might |
| 1091 | * cause a change of batch before the read is finished. |
| 1092 | */ |
| 1093 | ad->new_batch = 0; |
| 1094 | } |
| 1095 | ad->batch_data_dir = REQ_ASYNC; |
| 1096 | ad->current_write_count = ad->write_batch_count; |
| 1097 | ad->write_batch_idled = 0; |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1098 | rq = ad->next_rq[ad->batch_data_dir]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | goto dispatch_request; |
| 1100 | } |
| 1101 | |
| 1102 | BUG(); |
| 1103 | return 0; |
| 1104 | |
| 1105 | dispatch_request: |
| 1106 | /* |
| 1107 | * If a request has expired, service it. |
| 1108 | */ |
| 1109 | |
| 1110 | if (as_fifo_expired(ad, ad->batch_data_dir)) { |
| 1111 | fifo_expired: |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1112 | rq = rq_entry_fifo(ad->fifo_list[ad->batch_data_dir].next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | if (ad->changed_batch) { |
| 1116 | WARN_ON(ad->new_batch); |
| 1117 | |
| 1118 | if (ad->nr_dispatched) |
| 1119 | return 0; |
| 1120 | |
| 1121 | if (ad->batch_data_dir == REQ_ASYNC) |
| 1122 | ad->current_batch_expires = jiffies + |
| 1123 | ad->batch_expire[REQ_ASYNC]; |
| 1124 | else |
| 1125 | ad->new_batch = 1; |
| 1126 | |
| 1127 | ad->changed_batch = 0; |
| 1128 | } |
| 1129 | |
| 1130 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1131 | * rq is the selected appropriate request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1133 | as_move_to_dispatch(ad, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
| 1135 | return 1; |
| 1136 | } |
| 1137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1139 | * add rq to rbtree and fifo |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | */ |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1141 | static void as_add_request(request_queue_t *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1143 | struct as_data *ad = q->elevator->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | int data_dir; |
| 1145 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1146 | RQ_SET_STATE(rq, AS_RQ_NEW); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1147 | |
Jens Axboe | 9e2585a | 2006-07-28 09:26:13 +0200 | [diff] [blame] | 1148 | data_dir = rq_is_sync(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1150 | rq->elevator_private = as_get_io_context(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1152 | if (RQ_IOC(rq)) { |
| 1153 | as_update_iohist(ad, RQ_IOC(rq)->aic, rq); |
| 1154 | atomic_inc(&RQ_IOC(rq)->aic->nr_queued); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1157 | as_add_rq_rb(ad, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | |
Tejun Heo | ef9be1d | 2005-11-11 14:27:09 +0100 | [diff] [blame] | 1159 | /* |
| 1160 | * set expire time (only used for reads) and add to fifo list |
| 1161 | */ |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 1162 | rq_set_fifo_time(rq, jiffies + ad->fifo_expire[data_dir]); |
| 1163 | list_add_tail(&rq->queuelist, &ad->fifo_list[data_dir]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1165 | as_update_rq(ad, rq); /* keep state machine up to date */ |
| 1166 | RQ_SET_STATE(rq, AS_RQ_QUEUED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1169 | static void as_activate_request(request_queue_t *q, struct request *rq) |
| 1170 | { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1171 | WARN_ON(RQ_STATE(rq) != AS_RQ_DISPATCHED); |
| 1172 | RQ_SET_STATE(rq, AS_RQ_REMOVED); |
| 1173 | if (RQ_IOC(rq) && RQ_IOC(rq)->aic) |
| 1174 | atomic_dec(&RQ_IOC(rq)->aic->nr_dispatched); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | static void as_deactivate_request(request_queue_t *q, struct request *rq) |
| 1178 | { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1179 | WARN_ON(RQ_STATE(rq) != AS_RQ_REMOVED); |
| 1180 | RQ_SET_STATE(rq, AS_RQ_DISPATCHED); |
| 1181 | if (RQ_IOC(rq) && RQ_IOC(rq)->aic) |
| 1182 | atomic_inc(&RQ_IOC(rq)->aic->nr_dispatched); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * as_queue_empty tells us if there are requests left in the device. It may |
| 1187 | * not be the case that a driver can get the next request even if the queue |
| 1188 | * is not empty - it is used in the block layer to check for plugging and |
| 1189 | * merging opportunities |
| 1190 | */ |
| 1191 | static int as_queue_empty(request_queue_t *q) |
| 1192 | { |
| 1193 | struct as_data *ad = q->elevator->elevator_data; |
| 1194 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1195 | return list_empty(&ad->fifo_list[REQ_ASYNC]) |
| 1196 | && list_empty(&ad->fifo_list[REQ_SYNC]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | static int |
| 1200 | as_merge(request_queue_t *q, struct request **req, struct bio *bio) |
| 1201 | { |
| 1202 | struct as_data *ad = q->elevator->elevator_data; |
| 1203 | sector_t rb_key = bio->bi_sector + bio_sectors(bio); |
| 1204 | struct request *__rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | |
| 1206 | /* |
| 1207 | * check for front merge |
| 1208 | */ |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 1209 | __rq = elv_rb_find(&ad->sort_list[bio_data_dir(bio)], rb_key); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 1210 | if (__rq && elv_rq_merge_ok(__rq, bio)) { |
| 1211 | *req = __rq; |
| 1212 | return ELEVATOR_FRONT_MERGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | return ELEVATOR_NO_MERGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 1218 | static void as_merged_request(request_queue_t *q, struct request *req, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | { |
| 1220 | struct as_data *ad = q->elevator->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
| 1222 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | * if the merge was a front merge, we need to reposition request |
| 1224 | */ |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 1225 | if (type == ELEVATOR_FRONT_MERGE) { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1226 | as_del_rq_rb(ad, req); |
| 1227 | as_add_rq_rb(ad, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | /* |
| 1229 | * Note! At this stage of this and the next function, our next |
| 1230 | * request may not be optimal - eg the request may have "grown" |
| 1231 | * behind the disk head. We currently don't bother adjusting. |
| 1232 | */ |
| 1233 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 1236 | static void as_merged_requests(request_queue_t *q, struct request *req, |
| 1237 | struct request *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1240 | * if next expires before rq, assign its expire time to arq |
| 1241 | * and move into next position (next will be deleted) in fifo |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | */ |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 1243 | if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) { |
| 1244 | if (time_before(rq_fifo_time(next), rq_fifo_time(req))) { |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1245 | struct io_context *rioc = RQ_IOC(req); |
| 1246 | struct io_context *nioc = RQ_IOC(next); |
| 1247 | |
Jens Axboe | d4f2f46 | 2006-07-13 09:12:14 +0200 | [diff] [blame] | 1248 | list_move(&req->queuelist, &next->queuelist); |
| 1249 | rq_set_fifo_time(req, rq_fifo_time(next)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | /* |
| 1251 | * Don't copy here but swap, because when anext is |
| 1252 | * removed below, it must contain the unused context |
| 1253 | */ |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1254 | swap_io_context(&rioc, &nioc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | * kill knowledge of next, this one is a goner |
| 1260 | */ |
| 1261 | as_remove_queued_request(q, next); |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1262 | as_put_io_context(next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1264 | RQ_SET_STATE(next, AS_RQ_MERGED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * This is executed in a "deferred" process context, by kblockd. It calls the |
| 1269 | * driver's request_fn so the driver can submit that request. |
| 1270 | * |
| 1271 | * IMPORTANT! This guy will reenter the elevator, so set up all queue global |
| 1272 | * state before calling, and don't rely on any state over calls. |
| 1273 | * |
| 1274 | * FIXME! dispatch queue is not a queue at all! |
| 1275 | */ |
| 1276 | static void as_work_handler(void *data) |
| 1277 | { |
| 1278 | struct request_queue *q = data; |
| 1279 | unsigned long flags; |
| 1280 | |
| 1281 | spin_lock_irqsave(q->queue_lock, flags); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1282 | if (!as_queue_empty(q)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | q->request_fn(q); |
| 1284 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 1285 | } |
| 1286 | |
Jens Axboe | cb78b28 | 2006-07-28 09:32:57 +0200 | [diff] [blame^] | 1287 | static int as_may_queue(request_queue_t *q, int rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | { |
| 1289 | int ret = ELV_MQUEUE_MAY; |
| 1290 | struct as_data *ad = q->elevator->elevator_data; |
| 1291 | struct io_context *ioc; |
| 1292 | if (ad->antic_status == ANTIC_WAIT_REQ || |
| 1293 | ad->antic_status == ANTIC_WAIT_NEXT) { |
| 1294 | ioc = as_get_io_context(); |
| 1295 | if (ad->io_context == ioc) |
| 1296 | ret = ELV_MQUEUE_MUST; |
| 1297 | put_io_context(ioc); |
| 1298 | } |
| 1299 | |
| 1300 | return ret; |
| 1301 | } |
| 1302 | |
| 1303 | static void as_exit_queue(elevator_t *e) |
| 1304 | { |
| 1305 | struct as_data *ad = e->elevator_data; |
| 1306 | |
| 1307 | del_timer_sync(&ad->antic_timer); |
| 1308 | kblockd_flush(); |
| 1309 | |
| 1310 | BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC])); |
| 1311 | BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC])); |
| 1312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | put_io_context(ad->io_context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | kfree(ad); |
| 1315 | } |
| 1316 | |
| 1317 | /* |
Jens Axboe | 8a8e674 | 2006-07-18 21:07:29 +0200 | [diff] [blame] | 1318 | * initialize elevator private data (as_data). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | */ |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 1320 | static void *as_init_queue(request_queue_t *q, elevator_t *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | { |
| 1322 | struct as_data *ad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 1324 | ad = kmalloc_node(sizeof(*ad), GFP_KERNEL, q->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | if (!ad) |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 1326 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | memset(ad, 0, sizeof(*ad)); |
| 1328 | |
| 1329 | ad->q = q; /* Identify what queue the data belongs to */ |
| 1330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | /* anticipatory scheduling helpers */ |
| 1332 | ad->antic_timer.function = as_antic_timeout; |
| 1333 | ad->antic_timer.data = (unsigned long)q; |
| 1334 | init_timer(&ad->antic_timer); |
| 1335 | INIT_WORK(&ad->antic_work, as_work_handler, q); |
| 1336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]); |
| 1338 | INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]); |
| 1339 | ad->sort_list[REQ_SYNC] = RB_ROOT; |
| 1340 | ad->sort_list[REQ_ASYNC] = RB_ROOT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | ad->fifo_expire[REQ_SYNC] = default_read_expire; |
| 1342 | ad->fifo_expire[REQ_ASYNC] = default_write_expire; |
| 1343 | ad->antic_expire = default_antic_expire; |
| 1344 | ad->batch_expire[REQ_SYNC] = default_read_batch_expire; |
| 1345 | ad->batch_expire[REQ_ASYNC] = default_write_batch_expire; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | |
| 1347 | ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC]; |
| 1348 | ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10; |
| 1349 | if (ad->write_batch_count < 2) |
| 1350 | ad->write_batch_count = 2; |
| 1351 | |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 1352 | return ad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | /* |
| 1356 | * sysfs parts below |
| 1357 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | |
| 1359 | static ssize_t |
| 1360 | as_var_show(unsigned int var, char *page) |
| 1361 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | return sprintf(page, "%d\n", var); |
| 1363 | } |
| 1364 | |
| 1365 | static ssize_t |
| 1366 | as_var_store(unsigned long *var, const char *page, size_t count) |
| 1367 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | char *p = (char *) page; |
| 1369 | |
Jens Axboe | c9b3ad6 | 2005-07-27 11:43:37 -0700 | [diff] [blame] | 1370 | *var = simple_strtoul(p, &p, 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | return count; |
| 1372 | } |
| 1373 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 1374 | static ssize_t est_time_show(elevator_t *e, char *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1376 | struct as_data *ad = e->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | int pos = 0; |
| 1378 | |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 1379 | pos += sprintf(page+pos, "%lu %% exit probability\n", |
| 1380 | 100*ad->exit_prob/256); |
| 1381 | pos += sprintf(page+pos, "%lu %% probability of exiting without a " |
| 1382 | "cooperating process submitting IO\n", |
| 1383 | 100*ad->exit_no_coop/256); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean); |
Nick Piggin | f5b3db0 | 2005-11-07 00:59:53 -0800 | [diff] [blame] | 1385 | pos += sprintf(page+pos, "%llu sectors new seek distance\n", |
| 1386 | (unsigned long long)ad->new_seek_mean); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | |
| 1388 | return pos; |
| 1389 | } |
| 1390 | |
| 1391 | #define SHOW_FUNCTION(__FUNC, __VAR) \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1392 | static ssize_t __FUNC(elevator_t *e, char *page) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | { \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1394 | struct as_data *ad = e->elevator_data; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | return as_var_show(jiffies_to_msecs((__VAR)), (page)); \ |
| 1396 | } |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 1397 | SHOW_FUNCTION(as_read_expire_show, ad->fifo_expire[REQ_SYNC]); |
| 1398 | SHOW_FUNCTION(as_write_expire_show, ad->fifo_expire[REQ_ASYNC]); |
| 1399 | SHOW_FUNCTION(as_antic_expire_show, ad->antic_expire); |
| 1400 | SHOW_FUNCTION(as_read_batch_expire_show, ad->batch_expire[REQ_SYNC]); |
| 1401 | SHOW_FUNCTION(as_write_batch_expire_show, ad->batch_expire[REQ_ASYNC]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | #undef SHOW_FUNCTION |
| 1403 | |
| 1404 | #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1405 | static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | { \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1407 | struct as_data *ad = e->elevator_data; \ |
| 1408 | int ret = as_var_store(__PTR, (page), count); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | if (*(__PTR) < (MIN)) \ |
| 1410 | *(__PTR) = (MIN); \ |
| 1411 | else if (*(__PTR) > (MAX)) \ |
| 1412 | *(__PTR) = (MAX); \ |
| 1413 | *(__PTR) = msecs_to_jiffies(*(__PTR)); \ |
| 1414 | return ret; \ |
| 1415 | } |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 1416 | STORE_FUNCTION(as_read_expire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX); |
| 1417 | STORE_FUNCTION(as_write_expire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX); |
| 1418 | STORE_FUNCTION(as_antic_expire_store, &ad->antic_expire, 0, INT_MAX); |
| 1419 | STORE_FUNCTION(as_read_batch_expire_store, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | &ad->batch_expire[REQ_SYNC], 0, INT_MAX); |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 1421 | STORE_FUNCTION(as_write_batch_expire_store, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | &ad->batch_expire[REQ_ASYNC], 0, INT_MAX); |
| 1423 | #undef STORE_FUNCTION |
| 1424 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 1425 | #define AS_ATTR(name) \ |
| 1426 | __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 1428 | static struct elv_fs_entry as_attrs[] = { |
| 1429 | __ATTR_RO(est_time), |
| 1430 | AS_ATTR(read_expire), |
| 1431 | AS_ATTR(write_expire), |
| 1432 | AS_ATTR(antic_expire), |
| 1433 | AS_ATTR(read_batch_expire), |
| 1434 | AS_ATTR(write_batch_expire), |
| 1435 | __ATTR_NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | }; |
| 1437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | static struct elevator_type iosched_as = { |
| 1439 | .ops = { |
| 1440 | .elevator_merge_fn = as_merge, |
| 1441 | .elevator_merged_fn = as_merged_request, |
| 1442 | .elevator_merge_req_fn = as_merged_requests, |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1443 | .elevator_dispatch_fn = as_dispatch_request, |
| 1444 | .elevator_add_req_fn = as_add_request, |
| 1445 | .elevator_activate_req_fn = as_activate_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | .elevator_deactivate_req_fn = as_deactivate_request, |
| 1447 | .elevator_queue_empty_fn = as_queue_empty, |
| 1448 | .elevator_completed_req_fn = as_completed_request, |
Jens Axboe | e37f346 | 2006-07-18 21:06:01 +0200 | [diff] [blame] | 1449 | .elevator_former_req_fn = elv_rb_former_request, |
| 1450 | .elevator_latter_req_fn = elv_rb_latter_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | .elevator_may_queue_fn = as_may_queue, |
| 1452 | .elevator_init_fn = as_init_queue, |
| 1453 | .elevator_exit_fn = as_exit_queue, |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 1454 | .trim = as_trim, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | }, |
| 1456 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1457 | .elevator_attrs = as_attrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | .elevator_name = "anticipatory", |
| 1459 | .elevator_owner = THIS_MODULE, |
| 1460 | }; |
| 1461 | |
| 1462 | static int __init as_init(void) |
| 1463 | { |
| 1464 | int ret; |
| 1465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | ret = elv_register(&iosched_as); |
| 1467 | if (!ret) { |
| 1468 | /* |
| 1469 | * don't allow AS to get unregistered, since we would have |
| 1470 | * to browse all tasks in the system and release their |
| 1471 | * as_io_context first |
| 1472 | */ |
| 1473 | __module_get(THIS_MODULE); |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | return ret; |
| 1478 | } |
| 1479 | |
| 1480 | static void __exit as_exit(void) |
| 1481 | { |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 1482 | DECLARE_COMPLETION(all_gone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | elv_unregister(&iosched_as); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 1484 | ioc_gone = &all_gone; |
OGAWA Hirofumi | fba8227 | 2006-04-18 09:44:06 +0200 | [diff] [blame] | 1485 | /* ioc_gone's update must be visible before reading ioc_count */ |
| 1486 | smp_wmb(); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 1487 | if (atomic_read(&ioc_count)) |
OGAWA Hirofumi | fba8227 | 2006-04-18 09:44:06 +0200 | [diff] [blame] | 1488 | wait_for_completion(ioc_gone); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 1489 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | module_init(as_init); |
| 1493 | module_exit(as_exit); |
| 1494 | |
| 1495 | MODULE_AUTHOR("Nick Piggin"); |
| 1496 | MODULE_LICENSE("GPL"); |
| 1497 | MODULE_DESCRIPTION("anticipatory IO scheduler"); |