^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * lib/minmax.c: windowed min/max tracker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Kathleen Nichols' algorithm for tracking the minimum (or maximum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * value of a data stream over some fixed time interval. (E.g.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * the minimum RTT over the past five minutes.) It uses constant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * space and constant time per update yet almost always delivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * the same minimum as an implementation that has to keep all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * data in the window.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * The algorithm keeps track of the best, 2nd best & 3rd best min
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * values, maintaining an invariant that the measurement time of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * the n'th best >= n-1'th best. It also makes sure that the three
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * values are widely separated in the time window since that bounds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * the worse case error when that data is monotonically increasing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * over the window.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Upon getting a new min, we can forget everything earlier because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * it has no value - the new min is <= everything else in the window
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * by definition and it's the most recent. So we restart fresh on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * every new min and overwrites 2nd & 3rd choices. The same property
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * holds for 2nd & 3rd best.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/win_minmax.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* As time advances, update the 1st, 2nd, and 3rd choices. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static u32 minmax_subwin_update(struct minmax *m, u32 win,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) const struct minmax_sample *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) u32 dt = val->t - m->s[0].t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (unlikely(dt > win)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Passed entire window without a new val so make 2nd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * choice the new val & 3rd choice the new 2nd choice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * we may have to iterate this since our 2nd choice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * may also be outside the window (we checked on entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * that the third choice was in the window).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) m->s[0] = m->s[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) m->s[1] = m->s[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) m->s[2] = *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (unlikely(val->t - m->s[0].t > win)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) m->s[0] = m->s[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) m->s[1] = m->s[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) m->s[2] = *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) } else if (unlikely(m->s[1].t == m->s[0].t) && dt > win/4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * We've passed a quarter of the window without a new val
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * so take a 2nd choice from the 2nd quarter of the window.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) m->s[2] = m->s[1] = *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) } else if (unlikely(m->s[2].t == m->s[1].t) && dt > win/2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * We've passed half the window without finding a new val
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * so take a 3rd choice from the last half of the window
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) m->s[2] = *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return m->s[0].v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* Check if new measurement updates the 1st, 2nd or 3rd choice max. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) u32 minmax_running_max(struct minmax *m, u32 win, u32 t, u32 meas)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct minmax_sample val = { .t = t, .v = meas };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (unlikely(val.v >= m->s[0].v) || /* found new max? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unlikely(val.t - m->s[2].t > win)) /* nothing left in window? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return minmax_reset(m, t, meas); /* forget earlier samples */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (unlikely(val.v >= m->s[1].v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) m->s[2] = m->s[1] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) else if (unlikely(val.v >= m->s[2].v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) m->s[2] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return minmax_subwin_update(m, win, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) EXPORT_SYMBOL(minmax_running_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* Check if new measurement updates the 1st, 2nd or 3rd choice min. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) u32 minmax_running_min(struct minmax *m, u32 win, u32 t, u32 meas)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct minmax_sample val = { .t = t, .v = meas };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (unlikely(val.v <= m->s[0].v) || /* found new min? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) unlikely(val.t - m->s[2].t > win)) /* nothing left in window? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return minmax_reset(m, t, meas); /* forget earlier samples */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (unlikely(val.v <= m->s[1].v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) m->s[2] = m->s[1] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) else if (unlikely(val.v <= m->s[2].v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) m->s[2] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return minmax_subwin_update(m, win, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }