Meter.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_METER_HPP
  14. #define ZT_METER_HPP
  15. #include "Constants.hpp"
  16. #include "Mutex.hpp"
  17. #define ZT_METER_HISTORY_LENGTH 4
  18. #define ZT_METER_HISTORY_TICK_DURATION 1000
  19. namespace ZeroTier {
  20. /**
  21. * Transfer rate meter (thread-safe)
  22. */
  23. class Meter
  24. {
  25. public:
  26. ZT_ALWAYS_INLINE Meter()
  27. {
  28. for(int i=0;i<ZT_METER_HISTORY_LENGTH;++i)
  29. _history[i] = 0.0;
  30. _ts = 0;
  31. _count = 0;
  32. }
  33. ZT_ALWAYS_INLINE Meter(const Meter &m) { *this = m; }
  34. ZT_ALWAYS_INLINE Meter &operator=(const Meter &m)
  35. {
  36. m._lock.lock();
  37. for(int i=0;i<ZT_METER_HISTORY_LENGTH;++i)
  38. _history[i] = m._history[i];
  39. _ts = m._ts;
  40. _count = m._count;
  41. m._lock.unlock();
  42. return *this;
  43. }
  44. template<typename I>
  45. ZT_ALWAYS_INLINE void log(const int64_t now,I count)
  46. {
  47. _lock.lock();
  48. const int64_t since = now - _ts;
  49. if (since >= ZT_METER_HISTORY_TICK_DURATION) {
  50. _ts = now;
  51. for(int i=1;i<ZT_METER_HISTORY_LENGTH;++i)
  52. _history[i-1] = _history[i];
  53. _history[ZT_METER_HISTORY_LENGTH-1] = (double)_count / ((double)since / 1000.0);
  54. _count = 0;
  55. }
  56. _count += (uint64_t)count;
  57. _lock.unlock();
  58. }
  59. ZT_ALWAYS_INLINE double perSecond(const int64_t now) const
  60. {
  61. double r = 0.0,n = 0.0;
  62. _lock.lock();
  63. const int64_t since = (now - _ts);
  64. if (since >= ZT_METER_HISTORY_TICK_DURATION) {
  65. r += (double)_count / ((double)since / 1000.0);
  66. n += 1.0;
  67. }
  68. for(int i=0;i<ZT_METER_HISTORY_LENGTH;++i) {
  69. r += _history[i];
  70. n += 1.0;
  71. }
  72. _lock.unlock();
  73. return r / n;
  74. }
  75. private:
  76. double _history[ZT_METER_HISTORY_LENGTH];
  77. int64_t _ts;
  78. uint64_t _count;
  79. Mutex _lock;
  80. };
  81. } // namespace ZeroTier
  82. #endif