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