latency_recorder.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // Date: 2014/09/22 11:57:43
  18. #ifndef BVAR_LATENCY_RECORDER_H
  19. #define BVAR_LATENCY_RECORDER_H
  20. #include "bvar/recorder.h"
  21. #include "bvar/reducer.h"
  22. #include "bvar/passive_status.h"
  23. #include "bvar/detail/percentile.h"
  24. namespace bvar {
  25. namespace detail {
  26. class Percentile;
  27. typedef Window<IntRecorder, SERIES_IN_SECOND> RecorderWindow;
  28. typedef Window<Maxer<int64_t>, SERIES_IN_SECOND> MaxWindow;
  29. typedef Window<Percentile, SERIES_IN_SECOND> PercentileWindow;
  30. // NOTE: Always use int64_t in the interfaces no matter what the impl. is.
  31. class CDF : public Variable {
  32. public:
  33. explicit CDF(PercentileWindow* w);
  34. ~CDF();
  35. void describe(std::ostream& os, bool quote_string) const override;
  36. int describe_series(std::ostream& os, const SeriesOptions& options) const override;
  37. private:
  38. PercentileWindow* _w;
  39. };
  40. // For mimic constructor inheritance.
  41. class LatencyRecorderBase {
  42. public:
  43. explicit LatencyRecorderBase(time_t window_size);
  44. time_t window_size() const { return _latency_window.window_size(); }
  45. protected:
  46. IntRecorder _latency;
  47. Maxer<int64_t> _max_latency;
  48. Percentile _latency_percentile;
  49. RecorderWindow _latency_window;
  50. MaxWindow _max_latency_window;
  51. PassiveStatus<int64_t> _count;
  52. PassiveStatus<int64_t> _qps;
  53. PercentileWindow _latency_percentile_window;
  54. PassiveStatus<int64_t> _latency_p1;
  55. PassiveStatus<int64_t> _latency_p2;
  56. PassiveStatus<int64_t> _latency_p3;
  57. PassiveStatus<int64_t> _latency_999; // 99.9%
  58. PassiveStatus<int64_t> _latency_9999; // 99.99%
  59. CDF _latency_cdf;
  60. PassiveStatus<Vector<int64_t, 4> > _latency_percentiles;
  61. };
  62. } // namespace detail
  63. // Specialized structure to record latency.
  64. // It's not a Variable, but it contains multiple bvar inside.
  65. class LatencyRecorder : public detail::LatencyRecorderBase {
  66. typedef detail::LatencyRecorderBase Base;
  67. public:
  68. LatencyRecorder() : Base(-1) {}
  69. explicit LatencyRecorder(time_t window_size) : Base(window_size) {}
  70. explicit LatencyRecorder(const butil::StringPiece& prefix) : Base(-1) {
  71. expose(prefix);
  72. }
  73. LatencyRecorder(const butil::StringPiece& prefix,
  74. time_t window_size) : Base(window_size) {
  75. expose(prefix);
  76. }
  77. LatencyRecorder(const butil::StringPiece& prefix1,
  78. const butil::StringPiece& prefix2) : Base(-1) {
  79. expose(prefix1, prefix2);
  80. }
  81. LatencyRecorder(const butil::StringPiece& prefix1,
  82. const butil::StringPiece& prefix2,
  83. time_t window_size) : Base(window_size) {
  84. expose(prefix1, prefix2);
  85. }
  86. ~LatencyRecorder() { hide(); }
  87. // Record the latency.
  88. LatencyRecorder& operator<<(int64_t latency);
  89. // Expose all internal variables using `prefix' as prefix.
  90. // Returns 0 on success, -1 otherwise.
  91. // Example:
  92. // LatencyRecorder rec;
  93. // rec.expose("foo_bar_write"); // foo_bar_write_latency
  94. // // foo_bar_write_max_latency
  95. // // foo_bar_write_count
  96. // // foo_bar_write_qps
  97. // rec.expose("foo_bar", "read"); // foo_bar_read_latency
  98. // // foo_bar_read_max_latency
  99. // // foo_bar_read_count
  100. // // foo_bar_read_qps
  101. int expose(const butil::StringPiece& prefix) {
  102. return expose(butil::StringPiece(), prefix);
  103. }
  104. int expose(const butil::StringPiece& prefix1,
  105. const butil::StringPiece& prefix2);
  106. // Hide all internal variables, called in dtor as well.
  107. void hide();
  108. // Get the average latency in recent |window_size| seconds
  109. // If |window_size| is absent, use the window_size to ctor.
  110. int64_t latency(time_t window_size) const
  111. { return _latency_window.get_value(window_size).get_average_int(); }
  112. int64_t latency() const
  113. { return _latency_window.get_value().get_average_int(); }
  114. // Get p1/p2/p3/99.9-ile latencies in recent window_size-to-ctor seconds.
  115. Vector<int64_t, 4> latency_percentiles() const;
  116. // Get the max latency in recent window_size-to-ctor seconds.
  117. int64_t max_latency() const { return _max_latency_window.get_value(); }
  118. // Get the total number of recorded latencies.
  119. int64_t count() const { return _latency.get_value().num; }
  120. // Get qps in recent |window_size| seconds. The `q' means latencies
  121. // recorded by operator<<().
  122. // If |window_size| is absent, use the window_size to ctor.
  123. int64_t qps(time_t window_size) const;
  124. int64_t qps() const { return _qps.get_value(); }
  125. // Get |ratio|-ile latency in recent |window_size| seconds
  126. // E.g. 0.99 means 99%-ile
  127. int64_t latency_percentile(double ratio) const;
  128. // Get name of a sub-bvar.
  129. const std::string& latency_name() const { return _latency_window.name(); }
  130. const std::string& latency_percentiles_name() const
  131. { return _latency_percentiles.name(); }
  132. const std::string& latency_cdf_name() const { return _latency_cdf.name(); }
  133. const std::string& max_latency_name() const
  134. { return _max_latency_window.name(); }
  135. const std::string& count_name() const { return _count.name(); }
  136. const std::string& qps_name() const { return _qps.name(); }
  137. };
  138. std::ostream& operator<<(std::ostream& os, const LatencyRecorder&);
  139. } // namespace bvar
  140. #endif //BVAR_LATENCY_RECORDER_H