1 // SPDX-License-Identifier: GPL-2.0 1 2 /* 3 * lib/minmax.c: windowed min/max tracker 4 * 5 * Kathleen Nichols' algorithm for tracking th 6 * value of a data stream over some fixed time 7 * the minimum RTT over the past five minutes. 8 * space and constant time per update yet almo 9 * the same minimum as an implementation that 10 * data in the window. 11 * 12 * The algorithm keeps track of the best, 2nd 13 * values, maintaining an invariant that the m 14 * the n'th best >= n-1'th best. It also makes 15 * values are widely separated in the time win 16 * the worse case error when that data is mono 17 * over the window. 18 * 19 * Upon getting a new min, we can forget every 20 * it has no value - the new min is <= everyth 21 * by definition and it's the most recent. So 22 * every new min and overwrites 2nd & 3rd choi 23 * holds for 2nd & 3rd best. 24 */ 25 #include <linux/module.h> 26 #include <linux/win_minmax.h> 27 28 /* As time advances, update the 1st, 2nd, and 29 static u32 minmax_subwin_update(struct minmax 30 const struct m 31 { 32 u32 dt = val->t - m->s[0].t; 33 34 if (unlikely(dt > win)) { 35 /* 36 * Passed entire window withou 37 * choice the new val & 3rd ch 38 * we may have to iterate this 39 * may also be outside the win 40 * that the third choice was i 41 */ 42 m->s[0] = m->s[1]; 43 m->s[1] = m->s[2]; 44 m->s[2] = *val; 45 if (unlikely(val->t - m->s[0]. 46 m->s[0] = m->s[1]; 47 m->s[1] = m->s[2]; 48 m->s[2] = *val; 49 } 50 } else if (unlikely(m->s[1].t == m->s[ 51 /* 52 * We've passed a quarter of t 53 * so take a 2nd choice from t 54 */ 55 m->s[2] = m->s[1] = *val; 56 } else if (unlikely(m->s[2].t == m->s[ 57 /* 58 * We've passed half the windo 59 * so take a 3rd choice from t 60 */ 61 m->s[2] = *val; 62 } 63 return m->s[0].v; 64 } 65 66 /* Check if new measurement updates the 1st, 2 67 u32 minmax_running_max(struct minmax *m, u32 w 68 { 69 struct minmax_sample val = { .t = t, . 70 71 if (unlikely(val.v >= m->s[0].v) || 72 unlikely(val.t - m->s[2].t > win)) 73 return minmax_reset(m, t, meas 74 75 if (unlikely(val.v >= m->s[1].v)) 76 m->s[2] = m->s[1] = val; 77 else if (unlikely(val.v >= m->s[2].v)) 78 m->s[2] = val; 79 80 return minmax_subwin_update(m, win, &v 81 } 82 EXPORT_SYMBOL(minmax_running_max); 83 84 /* Check if new measurement updates the 1st, 2 85 u32 minmax_running_min(struct minmax *m, u32 w 86 { 87 struct minmax_sample val = { .t = t, . 88 89 if (unlikely(val.v <= m->s[0].v) || 90 unlikely(val.t - m->s[2].t > win)) 91 return minmax_reset(m, t, meas 92 93 if (unlikely(val.v <= m->s[1].v)) 94 m->s[2] = m->s[1] = val; 95 else if (unlikely(val.v <= m->s[2].v)) 96 m->s[2] = val; 97 98 return minmax_subwin_update(m, win, &v 99 } 100
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.