dm-service-time.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (C) 2007-2009 NEC Corporation. All Rights Reserved.
  3. *
  4. * Module Author: Kiyoshi Ueda
  5. *
  6. * This file is released under the GPL.
  7. *
  8. * Throughput oriented path selector.
  9. */
  10. #include "dm.h"
  11. #include "dm-path-selector.h"
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #define DM_MSG_PREFIX "multipath service-time"
  15. #define ST_MIN_IO 1
  16. #define ST_MAX_RELATIVE_THROUGHPUT 100
  17. #define ST_MAX_RELATIVE_THROUGHPUT_SHIFT 7
  18. #define ST_MAX_INFLIGHT_SIZE ((size_t)-1 >> ST_MAX_RELATIVE_THROUGHPUT_SHIFT)
  19. #define ST_VERSION "0.2.0"
  20. struct selector {
  21. struct list_head valid_paths;
  22. struct list_head failed_paths;
  23. };
  24. struct path_info {
  25. struct list_head list;
  26. struct dm_path *path;
  27. unsigned repeat_count;
  28. unsigned relative_throughput;
  29. atomic_t in_flight_size; /* Total size of in-flight I/Os */
  30. };
  31. static struct selector *alloc_selector(void)
  32. {
  33. struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
  34. if (s) {
  35. INIT_LIST_HEAD(&s->valid_paths);
  36. INIT_LIST_HEAD(&s->failed_paths);
  37. }
  38. return s;
  39. }
  40. static int st_create(struct path_selector *ps, unsigned argc, char **argv)
  41. {
  42. struct selector *s = alloc_selector();
  43. if (!s)
  44. return -ENOMEM;
  45. ps->context = s;
  46. return 0;
  47. }
  48. static void free_paths(struct list_head *paths)
  49. {
  50. struct path_info *pi, *next;
  51. list_for_each_entry_safe(pi, next, paths, list) {
  52. list_del(&pi->list);
  53. kfree(pi);
  54. }
  55. }
  56. static void st_destroy(struct path_selector *ps)
  57. {
  58. struct selector *s = ps->context;
  59. free_paths(&s->valid_paths);
  60. free_paths(&s->failed_paths);
  61. kfree(s);
  62. ps->context = NULL;
  63. }
  64. static int st_status(struct path_selector *ps, struct dm_path *path,
  65. status_type_t type, char *result, unsigned maxlen)
  66. {
  67. unsigned sz = 0;
  68. struct path_info *pi;
  69. if (!path)
  70. DMEMIT("0 ");
  71. else {
  72. pi = path->pscontext;
  73. switch (type) {
  74. case STATUSTYPE_INFO:
  75. DMEMIT("%d %u ", atomic_read(&pi->in_flight_size),
  76. pi->relative_throughput);
  77. break;
  78. case STATUSTYPE_TABLE:
  79. DMEMIT("%u %u ", pi->repeat_count,
  80. pi->relative_throughput);
  81. break;
  82. }
  83. }
  84. return sz;
  85. }
  86. static int st_add_path(struct path_selector *ps, struct dm_path *path,
  87. int argc, char **argv, char **error)
  88. {
  89. struct selector *s = ps->context;
  90. struct path_info *pi;
  91. unsigned repeat_count = ST_MIN_IO;
  92. unsigned relative_throughput = 1;
  93. char dummy;
  94. /*
  95. * Arguments: [<repeat_count> [<relative_throughput>]]
  96. * <repeat_count>: The number of I/Os before switching path.
  97. * If not given, default (ST_MIN_IO) is used.
  98. * <relative_throughput>: The relative throughput value of
  99. * the path among all paths in the path-group.
  100. * The valid range: 0-<ST_MAX_RELATIVE_THROUGHPUT>
  101. * If not given, minimum value '1' is used.
  102. * If '0' is given, the path isn't selected while
  103. * other paths having a positive value are
  104. * available.
  105. */
  106. if (argc > 2) {
  107. *error = "service-time ps: incorrect number of arguments";
  108. return -EINVAL;
  109. }
  110. if (argc && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
  111. *error = "service-time ps: invalid repeat count";
  112. return -EINVAL;
  113. }
  114. if ((argc == 2) &&
  115. (sscanf(argv[1], "%u%c", &relative_throughput, &dummy) != 1 ||
  116. relative_throughput > ST_MAX_RELATIVE_THROUGHPUT)) {
  117. *error = "service-time ps: invalid relative_throughput value";
  118. return -EINVAL;
  119. }
  120. /* allocate the path */
  121. pi = kmalloc(sizeof(*pi), GFP_KERNEL);
  122. if (!pi) {
  123. *error = "service-time ps: Error allocating path context";
  124. return -ENOMEM;
  125. }
  126. pi->path = path;
  127. pi->repeat_count = repeat_count;
  128. pi->relative_throughput = relative_throughput;
  129. atomic_set(&pi->in_flight_size, 0);
  130. path->pscontext = pi;
  131. list_add_tail(&pi->list, &s->valid_paths);
  132. return 0;
  133. }
  134. static void st_fail_path(struct path_selector *ps, struct dm_path *path)
  135. {
  136. struct selector *s = ps->context;
  137. struct path_info *pi = path->pscontext;
  138. list_move(&pi->list, &s->failed_paths);
  139. }
  140. static int st_reinstate_path(struct path_selector *ps, struct dm_path *path)
  141. {
  142. struct selector *s = ps->context;
  143. struct path_info *pi = path->pscontext;
  144. list_move_tail(&pi->list, &s->valid_paths);
  145. return 0;
  146. }
  147. /*
  148. * Compare the estimated service time of 2 paths, pi1 and pi2,
  149. * for the incoming I/O.
  150. *
  151. * Returns:
  152. * < 0 : pi1 is better
  153. * 0 : no difference between pi1 and pi2
  154. * > 0 : pi2 is better
  155. *
  156. * Description:
  157. * Basically, the service time is estimated by:
  158. * ('pi->in-flight-size' + 'incoming') / 'pi->relative_throughput'
  159. * To reduce the calculation, some optimizations are made.
  160. * (See comments inline)
  161. */
  162. static int st_compare_load(struct path_info *pi1, struct path_info *pi2,
  163. size_t incoming)
  164. {
  165. size_t sz1, sz2, st1, st2;
  166. sz1 = atomic_read(&pi1->in_flight_size);
  167. sz2 = atomic_read(&pi2->in_flight_size);
  168. /*
  169. * Case 1: Both have same throughput value. Choose less loaded path.
  170. */
  171. if (pi1->relative_throughput == pi2->relative_throughput)
  172. return sz1 - sz2;
  173. /*
  174. * Case 2a: Both have same load. Choose higher throughput path.
  175. * Case 2b: One path has no throughput value. Choose the other one.
  176. */
  177. if (sz1 == sz2 ||
  178. !pi1->relative_throughput || !pi2->relative_throughput)
  179. return pi2->relative_throughput - pi1->relative_throughput;
  180. /*
  181. * Case 3: Calculate service time. Choose faster path.
  182. * Service time using pi1:
  183. * st1 = (sz1 + incoming) / pi1->relative_throughput
  184. * Service time using pi2:
  185. * st2 = (sz2 + incoming) / pi2->relative_throughput
  186. *
  187. * To avoid the division, transform the expression to use
  188. * multiplication.
  189. * Because ->relative_throughput > 0 here, if st1 < st2,
  190. * the expressions below are the same meaning:
  191. * (sz1 + incoming) / pi1->relative_throughput <
  192. * (sz2 + incoming) / pi2->relative_throughput
  193. * (sz1 + incoming) * pi2->relative_throughput <
  194. * (sz2 + incoming) * pi1->relative_throughput
  195. * So use the later one.
  196. */
  197. sz1 += incoming;
  198. sz2 += incoming;
  199. if (unlikely(sz1 >= ST_MAX_INFLIGHT_SIZE ||
  200. sz2 >= ST_MAX_INFLIGHT_SIZE)) {
  201. /*
  202. * Size may be too big for multiplying pi->relative_throughput
  203. * and overflow.
  204. * To avoid the overflow and mis-selection, shift down both.
  205. */
  206. sz1 >>= ST_MAX_RELATIVE_THROUGHPUT_SHIFT;
  207. sz2 >>= ST_MAX_RELATIVE_THROUGHPUT_SHIFT;
  208. }
  209. st1 = sz1 * pi2->relative_throughput;
  210. st2 = sz2 * pi1->relative_throughput;
  211. if (st1 != st2)
  212. return st1 - st2;
  213. /*
  214. * Case 4: Service time is equal. Choose higher throughput path.
  215. */
  216. return pi2->relative_throughput - pi1->relative_throughput;
  217. }
  218. static struct dm_path *st_select_path(struct path_selector *ps,
  219. unsigned *repeat_count, size_t nr_bytes)
  220. {
  221. struct selector *s = ps->context;
  222. struct path_info *pi = NULL, *best = NULL;
  223. if (list_empty(&s->valid_paths))
  224. return NULL;
  225. /* Change preferred (first in list) path to evenly balance. */
  226. list_move_tail(s->valid_paths.next, &s->valid_paths);
  227. list_for_each_entry(pi, &s->valid_paths, list)
  228. if (!best || (st_compare_load(pi, best, nr_bytes) < 0))
  229. best = pi;
  230. if (!best)
  231. return NULL;
  232. *repeat_count = best->repeat_count;
  233. return best->path;
  234. }
  235. static int st_start_io(struct path_selector *ps, struct dm_path *path,
  236. size_t nr_bytes)
  237. {
  238. struct path_info *pi = path->pscontext;
  239. atomic_add(nr_bytes, &pi->in_flight_size);
  240. return 0;
  241. }
  242. static int st_end_io(struct path_selector *ps, struct dm_path *path,
  243. size_t nr_bytes)
  244. {
  245. struct path_info *pi = path->pscontext;
  246. atomic_sub(nr_bytes, &pi->in_flight_size);
  247. return 0;
  248. }
  249. static struct path_selector_type st_ps = {
  250. .name = "service-time",
  251. .module = THIS_MODULE,
  252. .table_args = 2,
  253. .info_args = 2,
  254. .create = st_create,
  255. .destroy = st_destroy,
  256. .status = st_status,
  257. .add_path = st_add_path,
  258. .fail_path = st_fail_path,
  259. .reinstate_path = st_reinstate_path,
  260. .select_path = st_select_path,
  261. .start_io = st_start_io,
  262. .end_io = st_end_io,
  263. };
  264. static int __init dm_st_init(void)
  265. {
  266. int r = dm_register_path_selector(&st_ps);
  267. if (r < 0)
  268. DMERR("register failed %d", r);
  269. DMINFO("version " ST_VERSION " loaded");
  270. return r;
  271. }
  272. static void __exit dm_st_exit(void)
  273. {
  274. int r = dm_unregister_path_selector(&st_ps);
  275. if (r < 0)
  276. DMERR("unregister failed %d", r);
  277. }
  278. module_init(dm_st_init);
  279. module_exit(dm_st_exit);
  280. MODULE_DESCRIPTION(DM_NAME " throughput oriented path selector");
  281. MODULE_AUTHOR("Kiyoshi Ueda <k-ueda@ct.jp.nec.com>");
  282. MODULE_LICENSE("GPL");