zen-iosched.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Zen IO scheduler
  3. * Primarily based on Noop, deadline, and SIO IO schedulers.
  4. *
  5. * Copyright (C) 2012 Brandon Berhent <bbedward@gmail.com>
  6. *
  7. * FCFS, dispatches are back-inserted, deadlines ensure fairness.
  8. * Should work best with devices where there is no travel delay.
  9. */
  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. enum zen_data_dir { ASYNC, SYNC };
  17. static const int sync_expire = HZ / 2; /* max time before a sync is submitted. */
  18. static const int async_expire = 5 * HZ; /* ditto for async, these limits are SOFT! */
  19. static const int fifo_batch = 16;
  20. struct zen_data {
  21. /* Runtime Data */
  22. /* Requests are only present on fifo_list */
  23. struct list_head fifo_list[2];
  24. unsigned int batching; /* number of sequential requests made */
  25. /* tunables */
  26. int fifo_expire[2];
  27. int fifo_batch;
  28. };
  29. static inline struct zen_data *
  30. zen_get_data(struct request_queue *q) {
  31. return q->elevator->elevator_data;
  32. }
  33. static void zen_dispatch(struct zen_data *, struct request *);
  34. static void
  35. zen_merged_requests(struct request_queue *q, struct request *req,
  36. struct request *next)
  37. {
  38. /*
  39. * if next expires before rq, assign its expire time to arq
  40. * and move into next position (next will be deleted) in fifo
  41. */
  42. if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
  43. if (time_before(rq_fifo_time(next), rq_fifo_time(req))) {
  44. list_move(&req->queuelist, &next->queuelist);
  45. rq_set_fifo_time(req, rq_fifo_time(next));
  46. }
  47. }
  48. /* next request is gone */
  49. rq_fifo_clear(next);
  50. }
  51. static void zen_add_request(struct request_queue *q, struct request *rq)
  52. {
  53. struct zen_data *zdata = zen_get_data(q);
  54. const int sync = rq_is_sync(rq);
  55. if (zdata->fifo_expire[sync]) {
  56. rq_set_fifo_time(rq, jiffies + zdata->fifo_expire[sync]);
  57. list_add_tail(&rq->queuelist, &zdata->fifo_list[sync]);
  58. }
  59. }
  60. static void zen_dispatch(struct zen_data *zdata, struct request *rq)
  61. {
  62. /* Remove request from list and dispatch it */
  63. rq_fifo_clear(rq);
  64. elv_dispatch_add_tail(rq->q, rq);
  65. /* Increment # of sequential requests */
  66. zdata->batching++;
  67. }
  68. /*
  69. * get the first expired request in direction ddir
  70. */
  71. static struct request *
  72. zen_expired_request(struct zen_data *zdata, int ddir)
  73. {
  74. struct request *rq;
  75. if (list_empty(&zdata->fifo_list[ddir]))
  76. return NULL;
  77. rq = rq_entry_fifo(zdata->fifo_list[ddir].next);
  78. if (time_after_eq(jiffies, rq_fifo_time(rq)))
  79. return rq;
  80. return NULL;
  81. }
  82. /*
  83. * zen_check_fifo returns 0 if there are no expired requests on the fifo,
  84. * otherwise it returns the next expired request
  85. */
  86. static struct request *
  87. zen_check_fifo(struct zen_data *zdata)
  88. {
  89. struct request *rq_sync = zen_expired_request(zdata, SYNC);
  90. struct request *rq_async = zen_expired_request(zdata, ASYNC);
  91. if (rq_async && rq_sync) {
  92. if (time_after(rq_fifo_time(rq_async), rq_fifo_time(rq_sync)))
  93. return rq_sync;
  94. } else if (rq_sync) {
  95. return rq_sync;
  96. } else if (rq_async) {
  97. return rq_async;
  98. }
  99. return 0;
  100. }
  101. static struct request *
  102. zen_choose_request(struct zen_data *zdata)
  103. {
  104. /*
  105. * Retrieve request from available fifo list.
  106. * Synchronous requests have priority over asynchronous.
  107. */
  108. if (!list_empty(&zdata->fifo_list[SYNC]))
  109. return rq_entry_fifo(zdata->fifo_list[SYNC].next);
  110. if (!list_empty(&zdata->fifo_list[ASYNC]))
  111. return rq_entry_fifo(zdata->fifo_list[ASYNC].next);
  112. return NULL;
  113. }
  114. static int zen_dispatch_requests(struct request_queue *q, int force)
  115. {
  116. struct zen_data *zdata = zen_get_data(q);
  117. struct request *rq = NULL;
  118. /* Check for and issue expired requests */
  119. if (zdata->batching > zdata->fifo_batch) {
  120. zdata->batching = 0;
  121. rq = zen_check_fifo(zdata);
  122. }
  123. if (!rq) {
  124. rq = zen_choose_request(zdata);
  125. if (!rq)
  126. return 0;
  127. }
  128. zen_dispatch(zdata, rq);
  129. return 1;
  130. }
  131. static void *zen_init_queue(struct request_queue *q)
  132. {
  133. struct zen_data *zdata;
  134. zdata = kmalloc_node(sizeof(*zdata), GFP_KERNEL, q->node);
  135. if (!zdata)
  136. return NULL;
  137. INIT_LIST_HEAD(&zdata->fifo_list[SYNC]);
  138. INIT_LIST_HEAD(&zdata->fifo_list[ASYNC]);
  139. zdata->fifo_expire[SYNC] = sync_expire;
  140. zdata->fifo_expire[ASYNC] = async_expire;
  141. zdata->fifo_batch = fifo_batch;
  142. return zdata;
  143. }
  144. static void zen_exit_queue(struct elevator_queue *e)
  145. {
  146. struct zen_data *zdata = e->elevator_data;
  147. BUG_ON(!list_empty(&zdata->fifo_list[SYNC]));
  148. BUG_ON(!list_empty(&zdata->fifo_list[ASYNC]));
  149. kfree(zdata);
  150. }
  151. /* Sysfs */
  152. static ssize_t
  153. zen_var_show(int var, char *page)
  154. {
  155. return sprintf(page, "%d\n", var);
  156. }
  157. static ssize_t
  158. zen_var_store(int *var, const char *page, size_t count)
  159. {
  160. *var = simple_strtol(page, NULL, 10);
  161. return count;
  162. }
  163. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  164. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  165. { \
  166. struct zen_data *zdata = e->elevator_data; \
  167. int __data = __VAR; \
  168. if (__CONV) \
  169. __data = jiffies_to_msecs(__data); \
  170. return zen_var_show(__data, (page)); \
  171. }
  172. SHOW_FUNCTION(zen_sync_expire_show, zdata->fifo_expire[SYNC], 1);
  173. SHOW_FUNCTION(zen_async_expire_show, zdata->fifo_expire[ASYNC], 1);
  174. SHOW_FUNCTION(zen_fifo_batch_show, zdata->fifo_batch, 0);
  175. #undef SHOW_FUNCTION
  176. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  177. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  178. { \
  179. struct zen_data *zdata = e->elevator_data; \
  180. int __data; \
  181. int ret = zen_var_store(&__data, (page), count); \
  182. if (__data < (MIN)) \
  183. __data = (MIN); \
  184. else if (__data > (MAX)) \
  185. __data = (MAX); \
  186. if (__CONV) \
  187. *(__PTR) = msecs_to_jiffies(__data); \
  188. else \
  189. *(__PTR) = __data; \
  190. return ret; \
  191. }
  192. STORE_FUNCTION(zen_sync_expire_store, &zdata->fifo_expire[SYNC], 0, INT_MAX, 1);
  193. STORE_FUNCTION(zen_async_expire_store, &zdata->fifo_expire[ASYNC], 0, INT_MAX, 1);
  194. STORE_FUNCTION(zen_fifo_batch_store, &zdata->fifo_batch, 0, INT_MAX, 0);
  195. #undef STORE_FUNCTION
  196. #define DD_ATTR(name) \
  197. __ATTR(name, S_IRUGO|S_IWUSR, zen_##name##_show, \
  198. zen_##name##_store)
  199. static struct elv_fs_entry zen_attrs[] = {
  200. DD_ATTR(sync_expire),
  201. DD_ATTR(async_expire),
  202. DD_ATTR(fifo_batch),
  203. __ATTR_NULL
  204. };
  205. static struct elevator_type iosched_zen = {
  206. .ops = {
  207. .elevator_merge_req_fn = zen_merged_requests,
  208. .elevator_dispatch_fn = zen_dispatch_requests,
  209. .elevator_add_req_fn = zen_add_request,
  210. .elevator_former_req_fn = elv_rb_former_request,
  211. .elevator_latter_req_fn = elv_rb_latter_request,
  212. .elevator_init_fn = zen_init_queue,
  213. .elevator_exit_fn = zen_exit_queue,
  214. },
  215. .elevator_attrs = zen_attrs,
  216. .elevator_name = "zen",
  217. .elevator_owner = THIS_MODULE,
  218. };
  219. static int __init zen_init(void)
  220. {
  221. return elv_register(&iosched_zen);
  222. }
  223. static void __exit zen_exit(void)
  224. {
  225. elv_unregister(&iosched_zen);
  226. }
  227. module_init(zen_init);
  228. module_exit(zen_exit);
  229. MODULE_AUTHOR("Brandon Berhent");
  230. MODULE_LICENSE("GPL");
  231. MODULE_DESCRIPTION("Zen IO scheduler");
  232. MODULE_VERSION("1.1");