Counters.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2018-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 <string>
  18. #include <folly/Function.h>
  19. #include <folly/Optional.h>
  20. #include <folly/Range.h>
  21. namespace folly {
  22. namespace io {
  23. enum class CodecType;
  24. } // namespace io
  25. enum class CompressionCounterKey {
  26. BYTES_BEFORE_COMPRESSION = 0,
  27. BYTES_AFTER_COMPRESSION = 1,
  28. BYTES_BEFORE_DECOMPRESSION = 2,
  29. BYTES_AFTER_DECOMPRESSION = 3,
  30. COMPRESSIONS = 4,
  31. DECOMPRESSIONS = 5,
  32. COMPRESSION_MILLISECONDS = 6,
  33. DECOMPRESSION_MILLISECONDS = 7,
  34. };
  35. enum class CompressionCounterType {
  36. AVG = 0,
  37. SUM = 1,
  38. };
  39. /**
  40. * This functions is an extension point when FOLLY_HAVE_WEAK_SYMBOLS is true.
  41. * There is a default no-op implementation provided which can be overrided by
  42. * linking in a library which provides its own definition.
  43. *
  44. * @param codecType The type of the codec for this counter.
  45. * @param codecName The name of the codec for this counter. If the codecName
  46. * is empty it should be defaulted using the codecType.
  47. * @param level Optionally the level used to construct the codec.
  48. * @param key The key of the counter.
  49. * @param counterType The type of the counter.
  50. * @returns A function to increment the counter for the given key and
  51. * type. It may be an empty folly::Function.
  52. */
  53. folly::Function<void(double)> makeCompressionCounterHandler(
  54. folly::io::CodecType codecType,
  55. folly::StringPiece codecName,
  56. folly::Optional<int> level,
  57. CompressionCounterKey key,
  58. CompressionCounterType counterType);
  59. namespace detail {
  60. /// Wrapper around the makeCompressionCounterHandler() extension point.
  61. class CompressionCounter {
  62. public:
  63. CompressionCounter() {}
  64. CompressionCounter(
  65. folly::io::CodecType codecType,
  66. folly::StringPiece codecName,
  67. folly::Optional<int> level,
  68. CompressionCounterKey key,
  69. CompressionCounterType counterType) {
  70. initialize_ = [=]() {
  71. return makeCompressionCounterHandler(
  72. codecType, codecName, level, key, counterType);
  73. };
  74. DCHECK(!initialize_.hasAllocatedMemory());
  75. }
  76. void operator+=(double sum) {
  77. performLazyInit();
  78. if (increment_) {
  79. increment_(sum);
  80. }
  81. }
  82. void operator++() {
  83. *this += 1.0;
  84. }
  85. void operator++(int) {
  86. *this += 1.0;
  87. }
  88. bool hasImplementation() {
  89. performLazyInit();
  90. return static_cast<bool>(increment_);
  91. }
  92. private:
  93. void performLazyInit() {
  94. if (!initialized_) {
  95. initialized_ = true;
  96. increment_ = initialize_();
  97. initialize_ = {};
  98. }
  99. }
  100. bool initialized_{false};
  101. folly::Function<folly::Function<void(double)>()> initialize_;
  102. folly::Function<void(double)> increment_;
  103. };
  104. } // namespace detail
  105. } // namespace folly