2 * Copyright (C) 2007-2009 NEC Corporation. All Rights Reserved.
4 * Module Author: Kiyoshi Ueda
6 * This file is released under the GPL.
8 * Throughput oriented path selector.
12 #include "dm-path-selector.h"
14 #include <linux/slab.h>
15 #include <linux/module.h>
17 #define DM_MSG_PREFIX "multipath service-time"
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"
25 struct list_head valid_paths
;
26 struct list_head failed_paths
;
30 struct list_head list
;
32 unsigned repeat_count
;
33 unsigned relative_throughput
;
34 atomic_t in_flight_size
; /* Total size of in-flight I/Os */
37 static struct selector
*alloc_selector(void)
39 struct selector
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
42 INIT_LIST_HEAD(&s
->valid_paths
);
43 INIT_LIST_HEAD(&s
->failed_paths
);
49 static int st_create(struct path_selector
*ps
, unsigned argc
, char **argv
)
51 struct selector
*s
= alloc_selector();
60 static void free_paths(struct list_head
*paths
)
62 struct path_info
*pi
, *next
;
64 list_for_each_entry_safe(pi
, next
, paths
, list
) {
70 static void st_destroy(struct path_selector
*ps
)
72 struct selector
*s
= ps
->context
;
74 free_paths(&s
->valid_paths
);
75 free_paths(&s
->failed_paths
);
80 static int st_status(struct path_selector
*ps
, struct dm_path
*path
,
81 status_type_t type
, char *result
, unsigned maxlen
)
93 DMEMIT("%d %u ", atomic_read(&pi
->in_flight_size
),
94 pi
->relative_throughput
);
96 case STATUSTYPE_TABLE
:
97 DMEMIT("%u %u ", pi
->repeat_count
,
98 pi
->relative_throughput
);
106 static int st_add_path(struct path_selector
*ps
, struct dm_path
*path
,
107 int argc
, char **argv
, char **error
)
109 struct selector
*s
= ps
->context
;
110 struct path_info
*pi
;
111 unsigned repeat_count
= ST_MIN_IO
;
112 unsigned relative_throughput
= 1;
116 * Arguments: [<repeat_count> [<relative_throughput>]]
117 * <repeat_count>: The number of I/Os before switching path.
118 * If not given, default (ST_MIN_IO) is used.
119 * <relative_throughput>: The relative throughput value of
120 * the path among all paths in the path-group.
121 * The valid range: 0-<ST_MAX_RELATIVE_THROUGHPUT>
122 * If not given, minimum value '1' is used.
123 * If '0' is given, the path isn't selected while
124 * other paths having a positive value are
128 *error
= "service-time ps: incorrect number of arguments";
132 if (argc
&& (sscanf(argv
[0], "%u%c", &repeat_count
, &dummy
) != 1)) {
133 *error
= "service-time ps: invalid repeat count";
138 (sscanf(argv
[1], "%u%c", &relative_throughput
, &dummy
) != 1 ||
139 relative_throughput
> ST_MAX_RELATIVE_THROUGHPUT
)) {
140 *error
= "service-time ps: invalid relative_throughput value";
144 /* allocate the path */
145 pi
= kmalloc(sizeof(*pi
), GFP_KERNEL
);
147 *error
= "service-time ps: Error allocating path context";
152 pi
->repeat_count
= repeat_count
;
153 pi
->relative_throughput
= relative_throughput
;
154 atomic_set(&pi
->in_flight_size
, 0);
156 path
->pscontext
= pi
;
158 list_add_tail(&pi
->list
, &s
->valid_paths
);
163 static void st_fail_path(struct path_selector
*ps
, struct dm_path
*path
)
165 struct selector
*s
= ps
->context
;
166 struct path_info
*pi
= path
->pscontext
;
168 list_move(&pi
->list
, &s
->failed_paths
);
171 static int st_reinstate_path(struct path_selector
*ps
, struct dm_path
*path
)
173 struct selector
*s
= ps
->context
;
174 struct path_info
*pi
= path
->pscontext
;
176 list_move_tail(&pi
->list
, &s
->valid_paths
);
182 * Compare the estimated service time of 2 paths, pi1 and pi2,
183 * for the incoming I/O.
186 * < 0 : pi1 is better
187 * 0 : no difference between pi1 and pi2
188 * > 0 : pi2 is better
191 * Basically, the service time is estimated by:
192 * ('pi->in-flight-size' + 'incoming') / 'pi->relative_throughput'
193 * To reduce the calculation, some optimizations are made.
194 * (See comments inline)
196 static int st_compare_load(struct path_info
*pi1
, struct path_info
*pi2
,
199 size_t sz1
, sz2
, st1
, st2
;
201 sz1
= atomic_read(&pi1
->in_flight_size
);
202 sz2
= atomic_read(&pi2
->in_flight_size
);
205 * Case 1: Both have same throughput value. Choose less loaded path.
207 if (pi1
->relative_throughput
== pi2
->relative_throughput
)
211 * Case 2a: Both have same load. Choose higher throughput path.
212 * Case 2b: One path has no throughput value. Choose the other one.
215 !pi1
->relative_throughput
|| !pi2
->relative_throughput
)
216 return pi2
->relative_throughput
- pi1
->relative_throughput
;
219 * Case 3: Calculate service time. Choose faster path.
220 * Service time using pi1:
221 * st1 = (sz1 + incoming) / pi1->relative_throughput
222 * Service time using pi2:
223 * st2 = (sz2 + incoming) / pi2->relative_throughput
225 * To avoid the division, transform the expression to use
227 * Because ->relative_throughput > 0 here, if st1 < st2,
228 * the expressions below are the same meaning:
229 * (sz1 + incoming) / pi1->relative_throughput <
230 * (sz2 + incoming) / pi2->relative_throughput
231 * (sz1 + incoming) * pi2->relative_throughput <
232 * (sz2 + incoming) * pi1->relative_throughput
233 * So use the later one.
237 if (unlikely(sz1
>= ST_MAX_INFLIGHT_SIZE
||
238 sz2
>= ST_MAX_INFLIGHT_SIZE
)) {
240 * Size may be too big for multiplying pi->relative_throughput
242 * To avoid the overflow and mis-selection, shift down both.
244 sz1
>>= ST_MAX_RELATIVE_THROUGHPUT_SHIFT
;
245 sz2
>>= ST_MAX_RELATIVE_THROUGHPUT_SHIFT
;
247 st1
= sz1
* pi2
->relative_throughput
;
248 st2
= sz2
* pi1
->relative_throughput
;
253 * Case 4: Service time is equal. Choose higher throughput path.
255 return pi2
->relative_throughput
- pi1
->relative_throughput
;
258 static struct dm_path
*st_select_path(struct path_selector
*ps
,
259 unsigned *repeat_count
, size_t nr_bytes
)
261 struct selector
*s
= ps
->context
;
262 struct path_info
*pi
= NULL
, *best
= NULL
;
264 if (list_empty(&s
->valid_paths
))
267 /* Change preferred (first in list) path to evenly balance. */
268 list_move_tail(s
->valid_paths
.next
, &s
->valid_paths
);
270 list_for_each_entry(pi
, &s
->valid_paths
, list
)
271 if (!best
|| (st_compare_load(pi
, best
, nr_bytes
) < 0))
277 *repeat_count
= best
->repeat_count
;
282 static int st_start_io(struct path_selector
*ps
, struct dm_path
*path
,
285 struct path_info
*pi
= path
->pscontext
;
287 atomic_add(nr_bytes
, &pi
->in_flight_size
);
292 static int st_end_io(struct path_selector
*ps
, struct dm_path
*path
,
295 struct path_info
*pi
= path
->pscontext
;
297 atomic_sub(nr_bytes
, &pi
->in_flight_size
);
302 static struct path_selector_type st_ps
= {
303 .name
= "service-time",
304 .module
= THIS_MODULE
,
308 .destroy
= st_destroy
,
310 .add_path
= st_add_path
,
311 .fail_path
= st_fail_path
,
312 .reinstate_path
= st_reinstate_path
,
313 .select_path
= st_select_path
,
314 .start_io
= st_start_io
,
318 static int __init
dm_st_init(void)
320 int r
= dm_register_path_selector(&st_ps
);
323 DMERR("register failed %d", r
);
325 DMINFO("version " ST_VERSION
" loaded");
330 static void __exit
dm_st_exit(void)
332 int r
= dm_unregister_path_selector(&st_ps
);
335 DMERR("unregister failed %d", r
);
338 module_init(dm_st_init
);
339 module_exit(dm_st_exit
);
341 MODULE_DESCRIPTION(DM_NAME
" throughput oriented path selector");
342 MODULE_AUTHOR("Kiyoshi Ueda <k-ueda@ct.jp.nec.com>");
343 MODULE_LICENSE("GPL");