~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/win_minmax.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /lib/win_minmax.c (Version linux-6.12-rc7) and /lib/win_minmax.c (Version linux-4.9.337)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php