dm-ps-service-time.c 8.9 KB

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