1
0

Kbps.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/Constant.h"
  18. // Must be multiples of 2
  19. #define Kbps_WINDOW_SIZE 8
  20. // Must be a multiple of 1024 (1 second)
  21. #define Kbps_TIMESPAN 1024
  22. #define Kbps_WINDOW_SH Constant_log2(Kbps_WINDOW_SIZE)
  23. #define Kbps_TIMESPAN_SH Constant_log2(Kbps_TIMESPAN)
  24. struct Kbps
  25. {
  26. /** bookkeeping */
  27. uint32_t window[Kbps_WINDOW_SIZE];
  28. /** Millisecond time of last message. */
  29. uint32_t lastTime;
  30. /** KiloBytes per timeslot. */
  31. uint32_t currentBpt;
  32. };
  33. #define Kbps_accumulate_NO_PACKET 0xffffffff
  34. static inline uint32_t Kbps_accumulate(struct Kbps* ctx, uint32_t now, uint32_t packetSize)
  35. {
  36. // now >> 10-3 == now / 1024 * 8 (eighth second Kbps_WINDOW_SIZE)
  37. uint32_t xnow = now;
  38. if ((xnow - ctx->lastTime) >> 31) { xnow = ctx->lastTime; }
  39. int slot = (xnow >> (Kbps_TIMESPAN_SH - Kbps_WINDOW_SH) ) % Kbps_WINDOW_SIZE;
  40. int lslot = ( ctx->lastTime >> (Kbps_TIMESPAN_SH - Kbps_WINDOW_SH) ) % Kbps_WINDOW_SIZE;
  41. if (xnow - ctx->lastTime > Kbps_TIMESPAN ||
  42. ((lslot == slot) && (xnow - ctx->lastTime > (Kbps_TIMESPAN >> 1))))
  43. {
  44. for (int i = 0; i < Kbps_WINDOW_SIZE; i++) {
  45. ctx->currentBpt -= ctx->window[i];
  46. ctx->window[i] = 0;
  47. }
  48. Assert_true(!ctx->currentBpt);
  49. } else if (lslot != slot) {
  50. for (int i = lslot + 1; ; i++) {
  51. i %= Kbps_WINDOW_SIZE;
  52. ctx->currentBpt -= ctx->window[i];
  53. ctx->window[i] = 0;
  54. if (i == slot) { break; }
  55. }
  56. }
  57. // Make sure it didn't go under zero and roll over
  58. Assert_true(!(ctx->currentBpt >> 31));
  59. if (packetSize != Kbps_accumulate_NO_PACKET) {
  60. ctx->window[slot] += packetSize;
  61. ctx->currentBpt += packetSize;
  62. }
  63. ctx->lastTime = now;
  64. // /= 2 ** Kbps_TIMESPAN_SH --> bytes per timespan to bytes per millisecond
  65. // *= 2 ** 10 --> bytes per millisecond to bytes per second
  66. // *= 2 ** 3 --> bytes per second to bits per second
  67. // /= 2 ** 10 --> bits per second to kbits per second
  68. return (
  69. ctx->currentBpt <?js
  70. var x = (Number(Kbps_TIMESPAN_SH) - 10 - 3 + 10);
  71. return ( ((x) < 0) ? "<<" : ">>" ) + " " + Math.abs(x);
  72. ?>
  73. );
  74. }
  75. #endif