Back to home page

LXR

 
 

    


0001 /**
0002  * lib/minmax.c: windowed min/max tracker
0003  *
0004  * Kathleen Nichols' algorithm for tracking the minimum (or maximum)
0005  * value of a data stream over some fixed time interval.  (E.g.,
0006  * the minimum RTT over the past five minutes.) It uses constant
0007  * space and constant time per update yet almost always delivers
0008  * the same minimum as an implementation that has to keep all the
0009  * data in the window.
0010  *
0011  * The algorithm keeps track of the best, 2nd best & 3rd best min
0012  * values, maintaining an invariant that the measurement time of
0013  * the n'th best >= n-1'th best. It also makes sure that the three
0014  * values are widely separated in the time window since that bounds
0015  * the worse case error when that data is monotonically increasing
0016  * over the window.
0017  *
0018  * Upon getting a new min, we can forget everything earlier because
0019  * it has no value - the new min is <= everything else in the window
0020  * by definition and it's the most recent. So we restart fresh on
0021  * every new min and overwrites 2nd & 3rd choices. The same property
0022  * holds for 2nd & 3rd best.
0023  */
0024 #include <linux/module.h>
0025 #include <linux/win_minmax.h>
0026 
0027 /* As time advances, update the 1st, 2nd, and 3rd choices. */
0028 static u32 minmax_subwin_update(struct minmax *m, u32 win,
0029                 const struct minmax_sample *val)
0030 {
0031     u32 dt = val->t - m->s[0].t;
0032 
0033     if (unlikely(dt > win)) {
0034         /*
0035          * Passed entire window without a new val so make 2nd
0036          * choice the new val & 3rd choice the new 2nd choice.
0037          * we may have to iterate this since our 2nd choice
0038          * may also be outside the window (we checked on entry
0039          * that the third choice was in the window).
0040          */
0041         m->s[0] = m->s[1];
0042         m->s[1] = m->s[2];
0043         m->s[2] = *val;
0044         if (unlikely(val->t - m->s[0].t > win)) {
0045             m->s[0] = m->s[1];
0046             m->s[1] = m->s[2];
0047             m->s[2] = *val;
0048         }
0049     } else if (unlikely(m->s[1].t == m->s[0].t) && dt > win/4) {
0050         /*
0051          * We've passed a quarter of the window without a new val
0052          * so take a 2nd choice from the 2nd quarter of the window.
0053          */
0054         m->s[2] = m->s[1] = *val;
0055     } else if (unlikely(m->s[2].t == m->s[1].t) && dt > win/2) {
0056         /*
0057          * We've passed half the window without finding a new val
0058          * so take a 3rd choice from the last half of the window
0059          */
0060         m->s[2] = *val;
0061     }
0062     return m->s[0].v;
0063 }
0064 
0065 /* Check if new measurement updates the 1st, 2nd or 3rd choice max. */
0066 u32 minmax_running_max(struct minmax *m, u32 win, u32 t, u32 meas)
0067 {
0068     struct minmax_sample val = { .t = t, .v = meas };
0069 
0070     if (unlikely(val.v >= m->s[0].v) ||   /* found new max? */
0071         unlikely(val.t - m->s[2].t > win))    /* nothing left in window? */
0072         return minmax_reset(m, t, meas);  /* forget earlier samples */
0073 
0074     if (unlikely(val.v >= m->s[1].v))
0075         m->s[2] = m->s[1] = val;
0076     else if (unlikely(val.v >= m->s[2].v))
0077         m->s[2] = val;
0078 
0079     return minmax_subwin_update(m, win, &val);
0080 }
0081 EXPORT_SYMBOL(minmax_running_max);
0082 
0083 /* Check if new measurement updates the 1st, 2nd or 3rd choice min. */
0084 u32 minmax_running_min(struct minmax *m, u32 win, u32 t, u32 meas)
0085 {
0086     struct minmax_sample val = { .t = t, .v = meas };
0087 
0088     if (unlikely(val.v <= m->s[0].v) ||   /* found new min? */
0089         unlikely(val.t - m->s[2].t > win))    /* nothing left in window? */
0090         return minmax_reset(m, t, meas);  /* forget earlier samples */
0091 
0092     if (unlikely(val.v <= m->s[1].v))
0093         m->s[2] = m->s[1] = val;
0094     else if (unlikely(val.v <= m->s[2].v))
0095         m->s[2] = val;
0096 
0097     return minmax_subwin_update(m, win, &val);
0098 }