AverageRoller_pvt.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef AverageRoller_pvt_H
  16. #define AverageRoller_pvt_H
  17. #include "util/AverageRoller.h"
  18. #include "util/events/EventBase.h"
  19. #include "util/Linker.h"
  20. Linker_require("util/AverageRoller.c")
  21. /** Used to represent the sum and entry count for a given second. */
  22. struct AverageRoller_SumAndEntryCount
  23. {
  24. /** Sum of all entries. */
  25. uint32_t sum;
  26. /** Number of entries. */
  27. uint32_t entryCount;
  28. };
  29. /**
  30. * A structure for helping calculate a rolling average over some number of seconds.
  31. * Each element in "seconds" array is responsable for 1 second and it is a circular array.
  32. */
  33. struct AverageRoller_pvt
  34. {
  35. struct AverageRoller pub;
  36. /** The average will be calculated over this number of seconds. */
  37. const uint32_t windowSeconds;
  38. /** The number of seconds since the epoch when the average was last updated. */
  39. uint32_t lastUpdateTime;
  40. /** The index of the array when the roller was last updated. */
  41. uint32_t lastUpdateIndex;
  42. /** The sum of every entry in the last windowSeconds seconds. */
  43. uint32_t sum;
  44. /** The total number of entries in the last windowSeconds seconds. */
  45. uint32_t entryCount;
  46. /** A stored value equal to the sum divided by the entry count. */
  47. uint32_t average;
  48. /** Means of getting the current time. */
  49. struct EventBase* eventBase;
  50. Identity
  51. /**
  52. * An array of entries containing sum and entry count for each second
  53. * in the seconds prior to the last update.
  54. * This array pretends to contain one entry but actually contans windowSeconds entries.
  55. */
  56. struct AverageRoller_SumAndEntryCount seconds[1];
  57. };
  58. #endif