as-iosched.c 38 KB

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