dm-service-time.c 8.2 KB

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