xref: /kernel/linux/linux-5.10/lib/win_minmax.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/**
38c2ecf20Sopenharmony_ci * lib/minmax.c: windowed min/max tracker
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Kathleen Nichols' algorithm for tracking the minimum (or maximum)
68c2ecf20Sopenharmony_ci * value of a data stream over some fixed time interval.  (E.g.,
78c2ecf20Sopenharmony_ci * the minimum RTT over the past five minutes.) It uses constant
88c2ecf20Sopenharmony_ci * space and constant time per update yet almost always delivers
98c2ecf20Sopenharmony_ci * the same minimum as an implementation that has to keep all the
108c2ecf20Sopenharmony_ci * data in the window.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * The algorithm keeps track of the best, 2nd best & 3rd best min
138c2ecf20Sopenharmony_ci * values, maintaining an invariant that the measurement time of
148c2ecf20Sopenharmony_ci * the n'th best >= n-1'th best. It also makes sure that the three
158c2ecf20Sopenharmony_ci * values are widely separated in the time window since that bounds
168c2ecf20Sopenharmony_ci * the worse case error when that data is monotonically increasing
178c2ecf20Sopenharmony_ci * over the window.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * Upon getting a new min, we can forget everything earlier because
208c2ecf20Sopenharmony_ci * it has no value - the new min is <= everything else in the window
218c2ecf20Sopenharmony_ci * by definition and it's the most recent. So we restart fresh on
228c2ecf20Sopenharmony_ci * every new min and overwrites 2nd & 3rd choices. The same property
238c2ecf20Sopenharmony_ci * holds for 2nd & 3rd best.
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci#include <linux/module.h>
268c2ecf20Sopenharmony_ci#include <linux/win_minmax.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* As time advances, update the 1st, 2nd, and 3rd choices. */
298c2ecf20Sopenharmony_cistatic u32 minmax_subwin_update(struct minmax *m, u32 win,
308c2ecf20Sopenharmony_ci				const struct minmax_sample *val)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	u32 dt = val->t - m->s[0].t;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	if (unlikely(dt > win)) {
358c2ecf20Sopenharmony_ci		/*
368c2ecf20Sopenharmony_ci		 * Passed entire window without a new val so make 2nd
378c2ecf20Sopenharmony_ci		 * choice the new val & 3rd choice the new 2nd choice.
388c2ecf20Sopenharmony_ci		 * we may have to iterate this since our 2nd choice
398c2ecf20Sopenharmony_ci		 * may also be outside the window (we checked on entry
408c2ecf20Sopenharmony_ci		 * that the third choice was in the window).
418c2ecf20Sopenharmony_ci		 */
428c2ecf20Sopenharmony_ci		m->s[0] = m->s[1];
438c2ecf20Sopenharmony_ci		m->s[1] = m->s[2];
448c2ecf20Sopenharmony_ci		m->s[2] = *val;
458c2ecf20Sopenharmony_ci		if (unlikely(val->t - m->s[0].t > win)) {
468c2ecf20Sopenharmony_ci			m->s[0] = m->s[1];
478c2ecf20Sopenharmony_ci			m->s[1] = m->s[2];
488c2ecf20Sopenharmony_ci			m->s[2] = *val;
498c2ecf20Sopenharmony_ci		}
508c2ecf20Sopenharmony_ci	} else if (unlikely(m->s[1].t == m->s[0].t) && dt > win/4) {
518c2ecf20Sopenharmony_ci		/*
528c2ecf20Sopenharmony_ci		 * We've passed a quarter of the window without a new val
538c2ecf20Sopenharmony_ci		 * so take a 2nd choice from the 2nd quarter of the window.
548c2ecf20Sopenharmony_ci		 */
558c2ecf20Sopenharmony_ci		m->s[2] = m->s[1] = *val;
568c2ecf20Sopenharmony_ci	} else if (unlikely(m->s[2].t == m->s[1].t) && dt > win/2) {
578c2ecf20Sopenharmony_ci		/*
588c2ecf20Sopenharmony_ci		 * We've passed half the window without finding a new val
598c2ecf20Sopenharmony_ci		 * so take a 3rd choice from the last half of the window
608c2ecf20Sopenharmony_ci		 */
618c2ecf20Sopenharmony_ci		m->s[2] = *val;
628c2ecf20Sopenharmony_ci	}
638c2ecf20Sopenharmony_ci	return m->s[0].v;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* Check if new measurement updates the 1st, 2nd or 3rd choice max. */
678c2ecf20Sopenharmony_ciu32 minmax_running_max(struct minmax *m, u32 win, u32 t, u32 meas)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	struct minmax_sample val = { .t = t, .v = meas };
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	if (unlikely(val.v >= m->s[0].v) ||	  /* found new max? */
728c2ecf20Sopenharmony_ci	    unlikely(val.t - m->s[2].t > win))	  /* nothing left in window? */
738c2ecf20Sopenharmony_ci		return minmax_reset(m, t, meas);  /* forget earlier samples */
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	if (unlikely(val.v >= m->s[1].v))
768c2ecf20Sopenharmony_ci		m->s[2] = m->s[1] = val;
778c2ecf20Sopenharmony_ci	else if (unlikely(val.v >= m->s[2].v))
788c2ecf20Sopenharmony_ci		m->s[2] = val;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	return minmax_subwin_update(m, win, &val);
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ciEXPORT_SYMBOL(minmax_running_max);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/* Check if new measurement updates the 1st, 2nd or 3rd choice min. */
858c2ecf20Sopenharmony_ciu32 minmax_running_min(struct minmax *m, u32 win, u32 t, u32 meas)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct minmax_sample val = { .t = t, .v = meas };
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	if (unlikely(val.v <= m->s[0].v) ||	  /* found new min? */
908c2ecf20Sopenharmony_ci	    unlikely(val.t - m->s[2].t > win))	  /* nothing left in window? */
918c2ecf20Sopenharmony_ci		return minmax_reset(m, t, meas);  /* forget earlier samples */
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	if (unlikely(val.v <= m->s[1].v))
948c2ecf20Sopenharmony_ci		m->s[2] = m->s[1] = val;
958c2ecf20Sopenharmony_ci	else if (unlikely(val.v <= m->s[2].v))
968c2ecf20Sopenharmony_ci		m->s[2] = val;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	return minmax_subwin_update(m, win, &val);
998c2ecf20Sopenharmony_ci}
100