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.3.0"
25 struct list_head valid_paths
;
26 struct list_head failed_paths
;
31 struct list_head list
;
33 unsigned repeat_count
;
34 unsigned relative_throughput
;
35 atomic_t in_flight_size
; /* Total size of in-flight I/Os */
38 static struct selector
*alloc_selector(void)
40 struct selector
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
43 INIT_LIST_HEAD(&s
->valid_paths
);
44 INIT_LIST_HEAD(&s
->failed_paths
);
45 spin_lock_init(&s
->lock
);
51 static int st_create(struct path_selector
*ps
, unsigned argc
, char **argv
)
53 struct selector
*s
= alloc_selector();
62 static void free_paths(struct list_head
*paths
)
64 struct path_info
*pi
, *next
;
66 list_for_each_entry_safe(pi
, next
, paths
, list
) {
72 static void st_destroy(struct path_selector
*ps
)
74 struct selector
*s
= ps
->context
;
76 free_paths(&s
->valid_paths
);
77 free_paths(&s
->failed_paths
);
82 static int st_status(struct path_selector
*ps
, struct dm_path
*path
,
83 status_type_t type
, char *result
, unsigned maxlen
)
95 DMEMIT("%d %u ", atomic_read(&pi
->in_flight_size
),
96 pi
->relative_throughput
);
98 case STATUSTYPE_TABLE
:
99 DMEMIT("%u %u ", pi
->repeat_count
,
100 pi
->relative_throughput
);
108 static int st_add_path(struct path_selector
*ps
, struct dm_path
*path
,
109 int argc
, char **argv
, char **error
)
111 struct selector
*s
= ps
->context
;
112 struct path_info
*pi
;
113 unsigned repeat_count
= ST_MIN_IO
;
114 unsigned relative_throughput
= 1;
119 * Arguments: [<repeat_count> [<relative_throughput>]]
120 * <repeat_count>: The number of I/Os before switching path.
121 * If not given, default (ST_MIN_IO) is used.
122 * <relative_throughput>: The relative throughput value of
123 * the path among all paths in the path-group.
124 * The valid range: 0-<ST_MAX_RELATIVE_THROUGHPUT>
125 * If not given, minimum value '1' is used.
126 * If '0' is given, the path isn't selected while
127 * other paths having a positive value are
131 *error
= "service-time ps: incorrect number of arguments";
135 if (argc
&& (sscanf(argv
[0], "%u%c", &repeat_count
, &dummy
) != 1)) {
136 *error
= "service-time ps: invalid repeat count";
140 if (repeat_count
> 1) {
141 DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
146 (sscanf(argv
[1], "%u%c", &relative_throughput
, &dummy
) != 1 ||
147 relative_throughput
> ST_MAX_RELATIVE_THROUGHPUT
)) {
148 *error
= "service-time ps: invalid relative_throughput value";
152 /* allocate the path */
153 pi
= kmalloc(sizeof(*pi
), GFP_KERNEL
);
155 *error
= "service-time ps: Error allocating path context";
160 pi
->repeat_count
= repeat_count
;
161 pi
->relative_throughput
= relative_throughput
;
162 atomic_set(&pi
->in_flight_size
, 0);
164 path
->pscontext
= pi
;
166 spin_lock_irqsave(&s
->lock
, flags
);
167 list_add_tail(&pi
->list
, &s
->valid_paths
);
168 spin_unlock_irqrestore(&s
->lock
, flags
);
173 static void st_fail_path(struct path_selector
*ps
, struct dm_path
*path
)
175 struct selector
*s
= ps
->context
;
176 struct path_info
*pi
= path
->pscontext
;
179 spin_lock_irqsave(&s
->lock
, flags
);
180 list_move(&pi
->list
, &s
->failed_paths
);
181 spin_unlock_irqrestore(&s
->lock
, flags
);
184 static int st_reinstate_path(struct path_selector
*ps
, struct dm_path
*path
)
186 struct selector
*s
= ps
->context
;
187 struct path_info
*pi
= path
->pscontext
;
190 spin_lock_irqsave(&s
->lock
, flags
);
191 list_move_tail(&pi
->list
, &s
->valid_paths
);
192 spin_unlock_irqrestore(&s
->lock
, flags
);
198 * Compare the estimated service time of 2 paths, pi1 and pi2,
199 * for the incoming I/O.
202 * < 0 : pi1 is better
203 * 0 : no difference between pi1 and pi2
204 * > 0 : pi2 is better
207 * Basically, the service time is estimated by:
208 * ('pi->in-flight-size' + 'incoming') / 'pi->relative_throughput'
209 * To reduce the calculation, some optimizations are made.
210 * (See comments inline)
212 static int st_compare_load(struct path_info
*pi1
, struct path_info
*pi2
,
215 size_t sz1
, sz2
, st1
, st2
;
217 sz1
= atomic_read(&pi1
->in_flight_size
);
218 sz2
= atomic_read(&pi2
->in_flight_size
);
221 * Case 1: Both have same throughput value. Choose less loaded path.
223 if (pi1
->relative_throughput
== pi2
->relative_throughput
)
227 * Case 2a: Both have same load. Choose higher throughput path.
228 * Case 2b: One path has no throughput value. Choose the other one.
231 !pi1
->relative_throughput
|| !pi2
->relative_throughput
)
232 return pi2
->relative_throughput
- pi1
->relative_throughput
;
235 * Case 3: Calculate service time. Choose faster path.
236 * Service time using pi1:
237 * st1 = (sz1 + incoming) / pi1->relative_throughput
238 * Service time using pi2:
239 * st2 = (sz2 + incoming) / pi2->relative_throughput
241 * To avoid the division, transform the expression to use
243 * Because ->relative_throughput > 0 here, if st1 < st2,
244 * the expressions below are the same meaning:
245 * (sz1 + incoming) / pi1->relative_throughput <
246 * (sz2 + incoming) / pi2->relative_throughput
247 * (sz1 + incoming) * pi2->relative_throughput <
248 * (sz2 + incoming) * pi1->relative_throughput
249 * So use the later one.
253 if (unlikely(sz1
>= ST_MAX_INFLIGHT_SIZE
||
254 sz2
>= ST_MAX_INFLIGHT_SIZE
)) {
256 * Size may be too big for multiplying pi->relative_throughput
258 * To avoid the overflow and mis-selection, shift down both.
260 sz1
>>= ST_MAX_RELATIVE_THROUGHPUT_SHIFT
;
261 sz2
>>= ST_MAX_RELATIVE_THROUGHPUT_SHIFT
;
263 st1
= sz1
* pi2
->relative_throughput
;
264 st2
= sz2
* pi1
->relative_throughput
;
269 * Case 4: Service time is equal. Choose higher throughput path.
271 return pi2
->relative_throughput
- pi1
->relative_throughput
;
274 static struct dm_path
*st_select_path(struct path_selector
*ps
, size_t nr_bytes
)
276 struct selector
*s
= ps
->context
;
277 struct path_info
*pi
= NULL
, *best
= NULL
;
278 struct dm_path
*ret
= NULL
;
281 spin_lock_irqsave(&s
->lock
, flags
);
282 if (list_empty(&s
->valid_paths
))
285 list_for_each_entry(pi
, &s
->valid_paths
, list
)
286 if (!best
|| (st_compare_load(pi
, best
, nr_bytes
) < 0))
292 /* Move most recently used to least preferred to evenly balance. */
293 list_move_tail(&best
->list
, &s
->valid_paths
);
297 spin_unlock_irqrestore(&s
->lock
, flags
);
301 static int st_start_io(struct path_selector
*ps
, struct dm_path
*path
,
304 struct path_info
*pi
= path
->pscontext
;
306 atomic_add(nr_bytes
, &pi
->in_flight_size
);
311 static int st_end_io(struct path_selector
*ps
, struct dm_path
*path
,
312 size_t nr_bytes
, u64 start_time
)
314 struct path_info
*pi
= path
->pscontext
;
316 atomic_sub(nr_bytes
, &pi
->in_flight_size
);
321 static struct path_selector_type st_ps
= {
322 .name
= "service-time",
323 .module
= THIS_MODULE
,
327 .destroy
= st_destroy
,
329 .add_path
= st_add_path
,
330 .fail_path
= st_fail_path
,
331 .reinstate_path
= st_reinstate_path
,
332 .select_path
= st_select_path
,
333 .start_io
= st_start_io
,
337 static int __init
dm_st_init(void)
339 int r
= dm_register_path_selector(&st_ps
);
342 DMERR("register failed %d", r
);
344 DMINFO("version " ST_VERSION
" loaded");
349 static void __exit
dm_st_exit(void)
351 int r
= dm_unregister_path_selector(&st_ps
);
354 DMERR("unregister failed %d", r
);
357 module_init(dm_st_init
);
358 module_exit(dm_st_exit
);
360 MODULE_DESCRIPTION(DM_NAME
" throughput oriented path selector");
361 MODULE_AUTHOR("Kiyoshi Ueda <k-ueda@ct.jp.nec.com>");
362 MODULE_LICENSE("GPL");