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