lsquic_minmax.h revision 06b2a236
1/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc.  See LICENSE. */
2#ifndef LSQUIC_MINMAX_H
3#define LSQUIC_MINMAX_H
4
5/* Windowed min/max tracker by Kathleen Nichols.
6 *
7 * Based on Google code released under BSD license here:
8 *  https://groups.google.com/forum/#!topic/bbr-dev/3RTgkzi5ZD8
9 */
10
11
12struct minmax_sample
13{
14    uint64_t    time;
15    uint64_t    value;
16};
17
18struct minmax
19{
20    uint64_t                window;
21    struct minmax_sample    samples[3];
22};
23
24#define minmax_get_idx(minmax_, idx_) ((minmax_)->samples[idx_].value)
25
26#define minmax_get(minmax_) minmax_get_idx(minmax_, 0)
27
28#define minmax_reset(minmax_, sample_) do {                             \
29    (minmax_)->samples[0] = (minmax_)->samples[1]                       \
30        = (minmax_)->samples[2] = (sample_);                            \
31} while (0)
32
33#define minmax_init(minmax_, window_) do {                              \
34    (minmax_)->window = (window_);                                      \
35    minmax_reset(minmax_, ((struct minmax_sample) { 0, 0, }));          \
36} while (0)
37
38void lsquic_minmax_update_min(struct minmax *, uint64_t now, uint64_t meas);
39void lsquic_minmax_update_max(struct minmax *, uint64_t now, uint64_t meas);
40
41#define minmax_upmin lsquic_minmax_update_min
42#define minmax_upmax lsquic_minmax_update_max
43
44#endif
45