jacobson_karels.h 1009 B

1234567891011121314151617181920212223242526272829303132333435
  1. /* Licensed under LGPLv2.1+ - see LICENSE file for details */
  2. #ifndef CCAN_JACOBSON_KARELS_H
  3. #define CCAN_JACOBSON_KARELS_H
  4. #include "config.h"
  5. #include <ccan/minmax/minmax.h>
  6. #define JACOBSON_KARELS(_name, _type, _a1, _a2, _b1, _b2, _g, _k) \
  7. struct _name##_state { \
  8. _type rtt, variance; \
  9. }; \
  10. static inline void _name##_init(struct _name##_state *s, \
  11. _type rtt0, _type var0) \
  12. { \
  13. s->rtt = rtt0; \
  14. s->variance = var0; \
  15. } \
  16. static inline void _name##_update(struct _name##_state *s, _type sample) \
  17. { \
  18. _type diff = sample - s->rtt; \
  19. s->rtt += (_a2) * diff / ((_a1) + (_a2)); \
  20. diff = (diff < 0) ? -diff : diff; \
  21. s->variance = ((_b1)*s->variance + (_b2) * diff) \
  22. / ((_b1) + (_b2)); \
  23. } \
  24. static inline _type _name##_timeout(struct _name##_state *s, \
  25. _type tmin, _type tmax) \
  26. { \
  27. return clamp((_g) * s->rtt + (_k)*s->variance, tmin, tmax); \
  28. }
  29. JACOBSON_KARELS(jacobson_karels, unsigned long, 7, 1, 3, 1, 1, 4)
  30. #endif /* CCAN_JACOBSON_KARELS_H */