MultiLevelTimeSeries-defs.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2013-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <folly/stats/MultiLevelTimeSeries.h>
  18. #include <glog/logging.h>
  19. namespace folly {
  20. template <typename VT, typename CT>
  21. MultiLevelTimeSeries<VT, CT>::MultiLevelTimeSeries(
  22. size_t nBuckets,
  23. size_t nLevels,
  24. const Duration levelDurations[])
  25. : cachedTime_(), cachedSum_(0), cachedCount_(0) {
  26. CHECK_GT(nLevels, 0u);
  27. CHECK(levelDurations);
  28. levels_.reserve(nLevels);
  29. for (size_t i = 0; i < nLevels; ++i) {
  30. if (levelDurations[i] == Duration(0)) {
  31. CHECK_EQ(i, nLevels - 1);
  32. } else if (i > 0) {
  33. CHECK(levelDurations[i - 1] < levelDurations[i]);
  34. }
  35. levels_.emplace_back(nBuckets, levelDurations[i]);
  36. }
  37. }
  38. template <typename VT, typename CT>
  39. MultiLevelTimeSeries<VT, CT>::MultiLevelTimeSeries(
  40. size_t nBuckets,
  41. std::initializer_list<Duration> durations)
  42. : cachedTime_(), cachedSum_(0), cachedCount_(0) {
  43. CHECK_GT(durations.size(), 0u);
  44. levels_.reserve(durations.size());
  45. size_t i = 0;
  46. Duration prev{0};
  47. for (auto dur : durations) {
  48. if (dur == Duration(0)) {
  49. CHECK_EQ(i, durations.size() - 1);
  50. } else if (i > 0) {
  51. CHECK(prev < dur);
  52. }
  53. levels_.emplace_back(nBuckets, dur);
  54. prev = dur;
  55. i++;
  56. }
  57. }
  58. template <typename VT, typename CT>
  59. void MultiLevelTimeSeries<VT, CT>::addValue(
  60. TimePoint now,
  61. const ValueType& val) {
  62. addValueAggregated(now, val, 1);
  63. }
  64. template <typename VT, typename CT>
  65. void MultiLevelTimeSeries<VT, CT>::addValue(
  66. TimePoint now,
  67. const ValueType& val,
  68. uint64_t times) {
  69. addValueAggregated(now, val * ValueType(times), times);
  70. }
  71. template <typename VT, typename CT>
  72. void MultiLevelTimeSeries<VT, CT>::addValueAggregated(
  73. TimePoint now,
  74. const ValueType& total,
  75. uint64_t nsamples) {
  76. if (cachedTime_ != now) {
  77. flush();
  78. cachedTime_ = now;
  79. }
  80. cachedSum_ += total;
  81. cachedCount_ += nsamples;
  82. }
  83. template <typename VT, typename CT>
  84. void MultiLevelTimeSeries<VT, CT>::update(TimePoint now) {
  85. flush();
  86. for (size_t i = 0; i < levels_.size(); ++i) {
  87. levels_[i].update(now);
  88. }
  89. }
  90. template <typename VT, typename CT>
  91. void MultiLevelTimeSeries<VT, CT>::flush() {
  92. // update all the underlying levels
  93. if (cachedCount_ > 0) {
  94. for (size_t i = 0; i < levels_.size(); ++i) {
  95. levels_[i].addValueAggregated(cachedTime_, cachedSum_, cachedCount_);
  96. }
  97. cachedCount_ = 0;
  98. cachedSum_ = 0;
  99. }
  100. }
  101. template <typename VT, typename CT>
  102. void MultiLevelTimeSeries<VT, CT>::clear() {
  103. for (auto& level : levels_) {
  104. level.clear();
  105. }
  106. cachedTime_ = TimePoint();
  107. cachedSum_ = 0;
  108. cachedCount_ = 0;
  109. }
  110. } // namespace folly