win_minmax.c 3.4 KB

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