Kbps.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Kbps_H
  16. #define Kbps_H
  17. #include "util/Assert.h"
  18. #include <stdint.h>
  19. // Must be multiples of 2
  20. #define Kbps_WINDOW_SIZE 8
  21. // Must be a multiple of 1024 (1 second)
  22. #define Kbps_TIMESPAN 1024
  23. #define Kbps_WINDOW_SH 3 // log2(Kbps_WINDOW_SIZE)
  24. #define Kbps_TIMESPAN_SH 10 // log2(Kbps_TIMESPAN)
  25. struct Kbps
  26. {
  27. /** bookkeeping */
  28. uint32_t window[Kbps_WINDOW_SIZE];
  29. /** Millisecond time of last message. */
  30. uint32_t lastTime;
  31. /** KiloBytes per timeslot. */
  32. uint32_t currentBpt;
  33. };
  34. #define Kbps_accumulate_NO_PACKET 0xffffffff
  35. static inline uint32_t Kbps_accumulate(struct Kbps* ctx, uint32_t now, uint32_t packetSize)
  36. {
  37. // now >> 10-3 == now / 1024 * 8 (eighth second Kbps_WINDOW_SIZE)
  38. uint32_t xnow = now;
  39. if ((xnow - ctx->lastTime) >> 31) { xnow = ctx->lastTime; }
  40. int slot = (xnow >> (Kbps_TIMESPAN_SH - Kbps_WINDOW_SH) ) % Kbps_WINDOW_SIZE;
  41. int lslot = ( ctx->lastTime >> (Kbps_TIMESPAN_SH - Kbps_WINDOW_SH) ) % Kbps_WINDOW_SIZE;
  42. if (xnow - ctx->lastTime > Kbps_TIMESPAN ||
  43. ((lslot == slot) && (xnow - ctx->lastTime > (Kbps_TIMESPAN >> 1))))
  44. {
  45. for (int i = 0; i < Kbps_WINDOW_SIZE; i++) {
  46. ctx->currentBpt -= ctx->window[i];
  47. ctx->window[i] = 0;
  48. }
  49. Assert_true(!ctx->currentBpt);
  50. } else if (lslot != slot) {
  51. for (int i = lslot + 1; ; i++) {
  52. i %= Kbps_WINDOW_SIZE;
  53. ctx->currentBpt -= ctx->window[i];
  54. ctx->window[i] = 0;
  55. if (i == slot) { break; }
  56. }
  57. }
  58. // Make sure it didn't go under zero and roll over
  59. Assert_true(!(ctx->currentBpt >> 31));
  60. if (packetSize != Kbps_accumulate_NO_PACKET) {
  61. ctx->window[slot] += packetSize;
  62. ctx->currentBpt += packetSize;
  63. }
  64. ctx->lastTime = now;
  65. // /= 2 ** Kbps_TIMESPAN_SH --> bytes per timespan to bytes per millisecond
  66. // *= 2 ** 10 --> bytes per millisecond to bytes per second
  67. // *= 2 ** 3 --> bytes per second to bits per second
  68. // /= 2 ** 10 --> bits per second to kbits per second
  69. int32_t x = Kbps_TIMESPAN_SH - 10 - 3 + 10;
  70. if (x < 0) {
  71. return ctx->currentBpt << (-x);
  72. } else {
  73. return ctx->currentBpt >> x;
  74. }
  75. }
  76. #endif