as-iosched.c 38 KB

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