Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / md / dm-ps-historical-service-time.c
blob186f91e2752c1862be6d23b038329b845581b611
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Historical Service Time
5 * Keeps a time-weighted exponential moving average of the historical
6 * service time. Estimates future service time based on the historical
7 * service time and the number of outstanding requests.
9 * Marks paths stale if they have not finished within hst *
10 * num_paths. If a path is stale and unused, we will send a single
11 * request to probe in case the path has improved. This situation
12 * generally arises if the path is so much worse than others that it
13 * will never have the best estimated service time, or if the entire
14 * multipath device is unused. If a path is stale and in use, limit the
15 * number of requests it can receive with the assumption that the path
16 * has become degraded.
18 * To avoid repeatedly calculating exponents for time weighting, times
19 * are split into HST_WEIGHT_COUNT buckets each (1 >> HST_BUCKET_SHIFT)
20 * ns, and the weighting is pre-calculated.
24 #include "dm.h"
25 #include "dm-path-selector.h"
27 #include <linux/blkdev.h>
28 #include <linux/slab.h>
29 #include <linux/module.h>
32 #define DM_MSG_PREFIX "multipath historical-service-time"
33 #define HST_MIN_IO 1
34 #define HST_VERSION "0.1.1"
36 #define HST_FIXED_SHIFT 10 /* 10 bits of decimal precision */
37 #define HST_FIXED_MAX (ULLONG_MAX >> HST_FIXED_SHIFT)
38 #define HST_FIXED_1 (1 << HST_FIXED_SHIFT)
39 #define HST_FIXED_95 972
40 #define HST_MAX_INFLIGHT HST_FIXED_1
41 #define HST_BUCKET_SHIFT 24 /* Buckets are ~ 16ms */
42 #define HST_WEIGHT_COUNT 64ULL
44 struct selector {
45 struct list_head valid_paths;
46 struct list_head failed_paths;
47 int valid_count;
48 spinlock_t lock;
50 unsigned int weights[HST_WEIGHT_COUNT];
51 unsigned int threshold_multiplier;
54 struct path_info {
55 struct list_head list;
56 struct dm_path *path;
57 unsigned int repeat_count;
59 spinlock_t lock;
61 u64 historical_service_time; /* Fixed point */
63 u64 stale_after;
64 u64 last_finish;
66 u64 outstanding;
69 /**
70 * fixed_power - compute: x^n, in O(log n) time
72 * @x: base of the power
73 * @frac_bits: fractional bits of @x
74 * @n: power to raise @x to.
76 * By exploiting the relation between the definition of the natural power
77 * function: x^n := x*x*...*x (x multiplied by itself for n times), and
78 * the binary encoding of numbers used by computers: n := \Sum n_i * 2^i,
79 * (where: n_i \elem {0, 1}, the binary vector representing n),
80 * we find: x^n := x^(\Sum n_i * 2^i) := \Prod x^(n_i * 2^i), which is
81 * of course trivially computable in O(log_2 n), the length of our binary
82 * vector.
84 * (see: kernel/sched/loadavg.c)
86 static u64 fixed_power(u64 x, unsigned int frac_bits, unsigned int n)
88 unsigned long result = 1UL << frac_bits;
90 if (n) {
91 for (;;) {
92 if (n & 1) {
93 result *= x;
94 result += 1UL << (frac_bits - 1);
95 result >>= frac_bits;
97 n >>= 1;
98 if (!n)
99 break;
100 x *= x;
101 x += 1UL << (frac_bits - 1);
102 x >>= frac_bits;
106 return result;
110 * Calculate the next value of an exponential moving average
111 * a_1 = a_0 * e + a * (1 - e)
113 * @last: [0, ULLONG_MAX >> HST_FIXED_SHIFT]
114 * @next: [0, ULLONG_MAX >> HST_FIXED_SHIFT]
115 * @weight: [0, HST_FIXED_1]
117 * Note:
118 * To account for multiple periods in the same calculation,
119 * a_n = a_0 * e^n + a * (1 - e^n),
120 * so call fixed_ema(last, next, pow(weight, N))
122 static u64 fixed_ema(u64 last, u64 next, u64 weight)
124 last *= weight;
125 last += next * (HST_FIXED_1 - weight);
126 last += 1ULL << (HST_FIXED_SHIFT - 1);
127 return last >> HST_FIXED_SHIFT;
130 static struct selector *alloc_selector(void)
132 struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
134 if (s) {
135 INIT_LIST_HEAD(&s->valid_paths);
136 INIT_LIST_HEAD(&s->failed_paths);
137 spin_lock_init(&s->lock);
138 s->valid_count = 0;
141 return s;
145 * Get the weight for a given time span.
147 static u64 hst_weight(struct path_selector *ps, u64 delta)
149 struct selector *s = ps->context;
150 int bucket = clamp(delta >> HST_BUCKET_SHIFT, 0ULL,
151 HST_WEIGHT_COUNT - 1);
153 return s->weights[bucket];
157 * Set up the weights array.
159 * weights[len-1] = 0
160 * weights[n] = base ^ (n + 1)
162 static void hst_set_weights(struct path_selector *ps, unsigned int base)
164 struct selector *s = ps->context;
165 int i;
167 if (base >= HST_FIXED_1)
168 return;
170 for (i = 0; i < HST_WEIGHT_COUNT - 1; i++)
171 s->weights[i] = fixed_power(base, HST_FIXED_SHIFT, i + 1);
172 s->weights[HST_WEIGHT_COUNT - 1] = 0;
175 static int hst_create(struct path_selector *ps, unsigned int argc, char **argv)
177 struct selector *s;
178 unsigned int base_weight = HST_FIXED_95;
179 unsigned int threshold_multiplier = 0;
180 char dummy;
183 * Arguments: [<base_weight> [<threshold_multiplier>]]
184 * <base_weight>: Base weight for ema [0, 1024) 10-bit fixed point. A
185 * value of 0 will completely ignore any history.
186 * If not given, default (HST_FIXED_95) is used.
187 * <threshold_multiplier>: Minimum threshold multiplier for paths to
188 * be considered different. That is, a path is
189 * considered different iff (p1 > N * p2) where p1
190 * is the path with higher service time. A threshold
191 * of 1 or 0 has no effect. Defaults to 0.
193 if (argc > 2)
194 return -EINVAL;
196 if (argc && (sscanf(argv[0], "%u%c", &base_weight, &dummy) != 1 ||
197 base_weight >= HST_FIXED_1)) {
198 return -EINVAL;
201 if (argc > 1 && (sscanf(argv[1], "%u%c",
202 &threshold_multiplier, &dummy) != 1)) {
203 return -EINVAL;
206 s = alloc_selector();
207 if (!s)
208 return -ENOMEM;
210 ps->context = s;
212 hst_set_weights(ps, base_weight);
213 s->threshold_multiplier = threshold_multiplier;
214 return 0;
217 static void free_paths(struct list_head *paths)
219 struct path_info *pi, *next;
221 list_for_each_entry_safe(pi, next, paths, list) {
222 list_del(&pi->list);
223 kfree(pi);
227 static void hst_destroy(struct path_selector *ps)
229 struct selector *s = ps->context;
231 free_paths(&s->valid_paths);
232 free_paths(&s->failed_paths);
233 kfree(s);
234 ps->context = NULL;
237 static int hst_status(struct path_selector *ps, struct dm_path *path,
238 status_type_t type, char *result, unsigned int maxlen)
240 unsigned int sz = 0;
241 struct path_info *pi;
243 if (!path) {
244 struct selector *s = ps->context;
246 DMEMIT("2 %u %u ", s->weights[0], s->threshold_multiplier);
247 } else {
248 pi = path->pscontext;
250 switch (type) {
251 case STATUSTYPE_INFO:
252 DMEMIT("%llu %llu %llu ", pi->historical_service_time,
253 pi->outstanding, pi->stale_after);
254 break;
255 case STATUSTYPE_TABLE:
256 DMEMIT("0 ");
257 break;
261 return sz;
264 static int hst_add_path(struct path_selector *ps, struct dm_path *path,
265 int argc, char **argv, char **error)
267 struct selector *s = ps->context;
268 struct path_info *pi;
269 unsigned int repeat_count = HST_MIN_IO;
270 char dummy;
271 unsigned long flags;
274 * Arguments: [<repeat_count>]
275 * <repeat_count>: The number of I/Os before switching path.
276 * If not given, default (HST_MIN_IO) is used.
278 if (argc > 1) {
279 *error = "historical-service-time ps: incorrect number of arguments";
280 return -EINVAL;
283 if (argc && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
284 *error = "historical-service-time ps: invalid repeat count";
285 return -EINVAL;
288 /* allocate the path */
289 pi = kmalloc(sizeof(*pi), GFP_KERNEL);
290 if (!pi) {
291 *error = "historical-service-time ps: Error allocating path context";
292 return -ENOMEM;
295 pi->path = path;
296 pi->repeat_count = repeat_count;
298 pi->historical_service_time = HST_FIXED_1;
300 spin_lock_init(&pi->lock);
301 pi->outstanding = 0;
303 pi->stale_after = 0;
304 pi->last_finish = 0;
306 path->pscontext = pi;
308 spin_lock_irqsave(&s->lock, flags);
309 list_add_tail(&pi->list, &s->valid_paths);
310 s->valid_count++;
311 spin_unlock_irqrestore(&s->lock, flags);
313 return 0;
316 static void hst_fail_path(struct path_selector *ps, struct dm_path *path)
318 struct selector *s = ps->context;
319 struct path_info *pi = path->pscontext;
320 unsigned long flags;
322 spin_lock_irqsave(&s->lock, flags);
323 list_move(&pi->list, &s->failed_paths);
324 s->valid_count--;
325 spin_unlock_irqrestore(&s->lock, flags);
328 static int hst_reinstate_path(struct path_selector *ps, struct dm_path *path)
330 struct selector *s = ps->context;
331 struct path_info *pi = path->pscontext;
332 unsigned long flags;
334 spin_lock_irqsave(&s->lock, flags);
335 list_move_tail(&pi->list, &s->valid_paths);
336 s->valid_count++;
337 spin_unlock_irqrestore(&s->lock, flags);
339 return 0;
342 static void hst_fill_compare(struct path_info *pi, u64 *hst,
343 u64 *out, u64 *stale)
345 unsigned long flags;
347 spin_lock_irqsave(&pi->lock, flags);
348 *hst = pi->historical_service_time;
349 *out = pi->outstanding;
350 *stale = pi->stale_after;
351 spin_unlock_irqrestore(&pi->lock, flags);
355 * Compare the estimated service time of 2 paths, pi1 and pi2,
356 * for the incoming I/O.
358 * Returns:
359 * < 0 : pi1 is better
360 * 0 : no difference between pi1 and pi2
361 * > 0 : pi2 is better
364 static long long hst_compare(struct path_info *pi1, struct path_info *pi2,
365 u64 time_now, struct path_selector *ps)
367 struct selector *s = ps->context;
368 u64 hst1, hst2;
369 long long out1, out2, stale1, stale2;
370 int pi2_better, over_threshold;
372 hst_fill_compare(pi1, &hst1, &out1, &stale1);
373 hst_fill_compare(pi2, &hst2, &out2, &stale2);
375 /* Check here if estimated latency for two paths are too similar.
376 * If this is the case, we skip extra calculation and just compare
377 * outstanding requests. In this case, any unloaded paths will
378 * be preferred.
380 if (hst1 > hst2)
381 over_threshold = hst1 > (s->threshold_multiplier * hst2);
382 else
383 over_threshold = hst2 > (s->threshold_multiplier * hst1);
385 if (!over_threshold)
386 return out1 - out2;
389 * If an unloaded path is stale, choose it. If both paths are unloaded,
390 * choose path that is the most stale.
391 * (If one path is loaded, choose the other)
393 if ((!out1 && stale1 < time_now) || (!out2 && stale2 < time_now) ||
394 (!out1 && !out2))
395 return (!out2 * stale1) - (!out1 * stale2);
397 /* Compare estimated service time. If outstanding is the same, we
398 * don't need to multiply
400 if (out1 == out2) {
401 pi2_better = hst1 > hst2;
402 } else {
403 /* Potential overflow with out >= 1024 */
404 if (unlikely(out1 >= HST_MAX_INFLIGHT ||
405 out2 >= HST_MAX_INFLIGHT)) {
406 /* If over 1023 in-flights, we may overflow if hst
407 * is at max. (With this shift we still overflow at
408 * 1048576 in-flights, which is high enough).
410 hst1 >>= HST_FIXED_SHIFT;
411 hst2 >>= HST_FIXED_SHIFT;
413 pi2_better = (1 + out1) * hst1 > (1 + out2) * hst2;
416 /* In the case that the 'winner' is stale, limit to equal usage. */
417 if (pi2_better) {
418 if (stale2 < time_now)
419 return out1 - out2;
420 return 1;
422 if (stale1 < time_now)
423 return out1 - out2;
424 return -1;
427 static struct dm_path *hst_select_path(struct path_selector *ps,
428 size_t nr_bytes)
430 struct selector *s = ps->context;
431 struct path_info *pi = NULL, *best = NULL;
432 u64 time_now = sched_clock();
433 struct dm_path *ret = NULL;
434 unsigned long flags;
436 spin_lock_irqsave(&s->lock, flags);
437 if (list_empty(&s->valid_paths))
438 goto out;
440 list_for_each_entry(pi, &s->valid_paths, list) {
441 if (!best || (hst_compare(pi, best, time_now, ps) < 0))
442 best = pi;
445 if (!best)
446 goto out;
448 /* Move last used path to end (least preferred in case of ties) */
449 list_move_tail(&best->list, &s->valid_paths);
451 ret = best->path;
453 out:
454 spin_unlock_irqrestore(&s->lock, flags);
455 return ret;
458 static int hst_start_io(struct path_selector *ps, struct dm_path *path,
459 size_t nr_bytes)
461 struct path_info *pi = path->pscontext;
462 unsigned long flags;
464 spin_lock_irqsave(&pi->lock, flags);
465 pi->outstanding++;
466 spin_unlock_irqrestore(&pi->lock, flags);
468 return 0;
471 static u64 path_service_time(struct path_info *pi, u64 start_time)
473 u64 sched_now = ktime_get_ns();
475 /* if a previous disk request has finished after this IO was
476 * sent to the hardware, pretend the submission happened
477 * serially.
479 if (time_after64(pi->last_finish, start_time))
480 start_time = pi->last_finish;
482 pi->last_finish = sched_now;
483 if (time_before64(sched_now, start_time))
484 return 0;
486 return sched_now - start_time;
489 static int hst_end_io(struct path_selector *ps, struct dm_path *path,
490 size_t nr_bytes, u64 start_time)
492 struct path_info *pi = path->pscontext;
493 struct selector *s = ps->context;
494 unsigned long flags;
495 u64 st;
497 spin_lock_irqsave(&pi->lock, flags);
499 st = path_service_time(pi, start_time);
500 pi->outstanding--;
501 pi->historical_service_time =
502 fixed_ema(pi->historical_service_time,
503 min(st * HST_FIXED_1, HST_FIXED_MAX),
504 hst_weight(ps, st));
507 * On request end, mark path as fresh. If a path hasn't
508 * finished any requests within the fresh period, the estimated
509 * service time is considered too optimistic and we limit the
510 * maximum requests on that path.
512 pi->stale_after = pi->last_finish +
513 (s->valid_count * (pi->historical_service_time >> HST_FIXED_SHIFT));
515 spin_unlock_irqrestore(&pi->lock, flags);
517 return 0;
520 static struct path_selector_type hst_ps = {
521 .name = "historical-service-time",
522 .module = THIS_MODULE,
523 .table_args = 1,
524 .info_args = 3,
525 .create = hst_create,
526 .destroy = hst_destroy,
527 .status = hst_status,
528 .add_path = hst_add_path,
529 .fail_path = hst_fail_path,
530 .reinstate_path = hst_reinstate_path,
531 .select_path = hst_select_path,
532 .start_io = hst_start_io,
533 .end_io = hst_end_io,
536 static int __init dm_hst_init(void)
538 int r = dm_register_path_selector(&hst_ps);
540 if (r < 0)
541 DMERR("register failed %d", r);
543 DMINFO("version " HST_VERSION " loaded");
545 return r;
548 static void __exit dm_hst_exit(void)
550 int r = dm_unregister_path_selector(&hst_ps);
552 if (r < 0)
553 DMERR("unregister failed %d", r);
556 module_init(dm_hst_init);
557 module_exit(dm_hst_exit);
559 MODULE_DESCRIPTION(DM_NAME " measured service time oriented path selector");
560 MODULE_AUTHOR("Khazhismel Kumykov <khazhy@google.com>");
561 MODULE_LICENSE("GPL");