win_minmax.c 3.3 KB

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