You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
14 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //
  2. // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license
  5. // that can be found in the LICENSE file in the root of the source
  6. // tree. An additional intellectual property rights grant can be found
  7. // in the file PATENTS. All contributing project authors may
  8. // be found in the AUTHORS file in the root of the source tree.
  9. //
  10. #ifndef SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
  11. #define SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
  12. #include <stddef.h>
  13. #include <map>
  14. #include <memory>
  15. #include <string>
  16. #include "rtc_base/atomic_ops.h"
  17. #include "rtc_base/checks.h"
  18. // Macros for allowing WebRTC clients (e.g. Chrome) to gather and aggregate
  19. // statistics.
  20. //
  21. // Histogram for counters.
  22. // RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count);
  23. //
  24. // Histogram for enumerators.
  25. // The boundary should be above the max enumerator sample.
  26. // RTC_HISTOGRAM_ENUMERATION(name, sample, boundary);
  27. //
  28. //
  29. // The macros use the methods HistogramFactoryGetCounts,
  30. // HistogramFactoryGetEnumeration and HistogramAdd.
  31. //
  32. // By default WebRTC provides implementations of the aforementioned methods
  33. // that can be found in system_wrappers/source/metrics.cc. If clients want to
  34. // provide a custom version, they will have to:
  35. //
  36. // 1. Compile WebRTC defining the preprocessor macro
  37. // WEBRTC_EXCLUDE_METRICS_DEFAULT (if GN is used this can be achieved
  38. // by setting the GN arg rtc_exclude_metrics_default to true).
  39. // 2. Provide implementations of:
  40. // Histogram* webrtc::metrics::HistogramFactoryGetCounts(
  41. // const std::string& name, int sample, int min, int max,
  42. // int bucket_count);
  43. // Histogram* webrtc::metrics::HistogramFactoryGetEnumeration(
  44. // const std::string& name, int sample, int boundary);
  45. // void webrtc::metrics::HistogramAdd(
  46. // Histogram* histogram_pointer, const std::string& name, int sample);
  47. //
  48. // Example usage:
  49. //
  50. // RTC_HISTOGRAM_COUNTS("WebRTC.Video.NacksSent", nacks_sent, 1, 100000, 100);
  51. //
  52. // enum Types {
  53. // kTypeX,
  54. // kTypeY,
  55. // kBoundary,
  56. // };
  57. //
  58. // RTC_HISTOGRAM_ENUMERATION("WebRTC.Types", kTypeX, kBoundary);
  59. //
  60. // NOTE: It is recommended to do the Chromium review for modifications to
  61. // histograms.xml before new metrics are committed to WebRTC.
  62. // Macros for adding samples to a named histogram.
  63. // Histogram for counters (exponentially spaced buckets).
  64. #define RTC_HISTOGRAM_COUNTS_100(name, sample) \
  65. RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50)
  66. #define RTC_HISTOGRAM_COUNTS_200(name, sample) \
  67. RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50)
  68. #define RTC_HISTOGRAM_COUNTS_500(name, sample) \
  69. RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50)
  70. #define RTC_HISTOGRAM_COUNTS_1000(name, sample) \
  71. RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50)
  72. #define RTC_HISTOGRAM_COUNTS_10000(name, sample) \
  73. RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50)
  74. #define RTC_HISTOGRAM_COUNTS_100000(name, sample) \
  75. RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50)
  76. #define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
  77. RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
  78. webrtc::metrics::HistogramFactoryGetCounts( \
  79. name, min, max, bucket_count))
  80. #define RTC_HISTOGRAM_COUNTS_LINEAR(name, sample, min, max, bucket_count) \
  81. RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
  82. webrtc::metrics::HistogramFactoryGetCountsLinear( \
  83. name, min, max, bucket_count))
  84. // Slow metrics: pointer to metric is acquired at each call and is not cached.
  85. //
  86. #define RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample) \
  87. RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100, 50)
  88. #define RTC_HISTOGRAM_COUNTS_SPARSE_200(name, sample) \
  89. RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 200, 50)
  90. #define RTC_HISTOGRAM_COUNTS_SPARSE_500(name, sample) \
  91. RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 500, 50)
  92. #define RTC_HISTOGRAM_COUNTS_SPARSE_1000(name, sample) \
  93. RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 1000, 50)
  94. #define RTC_HISTOGRAM_COUNTS_SPARSE_10000(name, sample) \
  95. RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 10000, 50)
  96. #define RTC_HISTOGRAM_COUNTS_SPARSE_100000(name, sample) \
  97. RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, 1, 100000, 50)
  98. #define RTC_HISTOGRAM_COUNTS_SPARSE(name, sample, min, max, bucket_count) \
  99. RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, \
  100. webrtc::metrics::HistogramFactoryGetCounts( \
  101. name, min, max, bucket_count))
  102. // Histogram for percentage (evenly spaced buckets).
  103. #define RTC_HISTOGRAM_PERCENTAGE_SPARSE(name, sample) \
  104. RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 101)
  105. // Histogram for booleans.
  106. #define RTC_HISTOGRAM_BOOLEAN_SPARSE(name, sample) \
  107. RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, 2)
  108. // Histogram for enumerators (evenly spaced buckets).
  109. // |boundary| should be above the max enumerator sample.
  110. //
  111. // TODO(qingsi): Refactor the default implementation given by RtcHistogram,
  112. // which is already sparse, and remove the boundary argument from the macro.
  113. #define RTC_HISTOGRAM_ENUMERATION_SPARSE(name, sample, boundary) \
  114. RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \
  115. name, sample, \
  116. webrtc::metrics::SparseHistogramFactoryGetEnumeration(name, boundary))
  117. // Histogram for percentage (evenly spaced buckets).
  118. #define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
  119. RTC_HISTOGRAM_ENUMERATION(name, sample, 101)
  120. // Histogram for booleans.
  121. #define RTC_HISTOGRAM_BOOLEAN(name, sample) \
  122. RTC_HISTOGRAM_ENUMERATION(name, sample, 2)
  123. // Histogram for enumerators (evenly spaced buckets).
  124. // |boundary| should be above the max enumerator sample.
  125. #define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
  126. RTC_HISTOGRAM_COMMON_BLOCK_SLOW( \
  127. name, sample, \
  128. webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
  129. // The name of the histogram should not vary.
  130. // TODO(asapersson): Consider changing string to const char*.
  131. #define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
  132. factory_get_invocation) \
  133. do { \
  134. static webrtc::metrics::Histogram* atomic_histogram_pointer = nullptr; \
  135. webrtc::metrics::Histogram* histogram_pointer = \
  136. rtc::AtomicOps::AcquireLoadPtr(&atomic_histogram_pointer); \
  137. if (!histogram_pointer) { \
  138. histogram_pointer = factory_get_invocation; \
  139. webrtc::metrics::Histogram* prev_pointer = \
  140. rtc::AtomicOps::CompareAndSwapPtr( \
  141. &atomic_histogram_pointer, \
  142. static_cast<webrtc::metrics::Histogram*>(nullptr), \
  143. histogram_pointer); \
  144. RTC_DCHECK(prev_pointer == nullptr || \
  145. prev_pointer == histogram_pointer); \
  146. } \
  147. if (histogram_pointer) { \
  148. webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
  149. } \
  150. } while (0)
  151. // The histogram is constructed/found for each call.
  152. // May be used for histograms with infrequent updates.`
  153. #define RTC_HISTOGRAM_COMMON_BLOCK_SLOW(name, sample, factory_get_invocation) \
  154. do { \
  155. webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \
  156. if (histogram_pointer) { \
  157. webrtc::metrics::HistogramAdd(histogram_pointer, sample); \
  158. } \
  159. } while (0)
  160. // Helper macros.
  161. // Macros for calling a histogram with varying name (e.g. when using a metric
  162. // in different modes such as real-time vs screenshare). Fast, because pointer
  163. // is cached. |index| should be different for different names. Allowed |index|
  164. // values are 0, 1, and 2.
  165. #define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \
  166. RTC_HISTOGRAMS_COMMON(index, name, sample, \
  167. RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50))
  168. #define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \
  169. RTC_HISTOGRAMS_COMMON(index, name, sample, \
  170. RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50))
  171. #define RTC_HISTOGRAMS_COUNTS_500(index, name, sample) \
  172. RTC_HISTOGRAMS_COMMON(index, name, sample, \
  173. RTC_HISTOGRAM_COUNTS(name, sample, 1, 500, 50))
  174. #define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \
  175. RTC_HISTOGRAMS_COMMON(index, name, sample, \
  176. RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50))
  177. #define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \
  178. RTC_HISTOGRAMS_COMMON(index, name, sample, \
  179. RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50))
  180. #define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \
  181. RTC_HISTOGRAMS_COMMON(index, name, sample, \
  182. RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50))
  183. #define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
  184. RTC_HISTOGRAMS_COMMON(index, name, sample, \
  185. RTC_HISTOGRAM_ENUMERATION(name, sample, boundary))
  186. #define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \
  187. RTC_HISTOGRAMS_COMMON(index, name, sample, \
  188. RTC_HISTOGRAM_PERCENTAGE(name, sample))
  189. #define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \
  190. do { \
  191. switch (index) { \
  192. case 0: \
  193. macro_invocation; \
  194. break; \
  195. case 1: \
  196. macro_invocation; \
  197. break; \
  198. case 2: \
  199. macro_invocation; \
  200. break; \
  201. default: \
  202. RTC_NOTREACHED(); \
  203. } \
  204. } while (0)
  205. namespace webrtc {
  206. namespace metrics {
  207. // Time that should have elapsed for stats that are gathered once per call.
  208. enum { kMinRunTimeInSeconds = 10 };
  209. class Histogram;
  210. // Functions for getting pointer to histogram (constructs or finds the named
  211. // histogram).
  212. // Get histogram for counters.
  213. Histogram* HistogramFactoryGetCounts(const std::string& name,
  214. int min,
  215. int max,
  216. int bucket_count);
  217. // Get histogram for counters with linear bucket spacing.
  218. Histogram* HistogramFactoryGetCountsLinear(const std::string& name,
  219. int min,
  220. int max,
  221. int bucket_count);
  222. // Get histogram for enumerators.
  223. // |boundary| should be above the max enumerator sample.
  224. Histogram* HistogramFactoryGetEnumeration(const std::string& name,
  225. int boundary);
  226. // Get sparse histogram for enumerators.
  227. // |boundary| should be above the max enumerator sample.
  228. Histogram* SparseHistogramFactoryGetEnumeration(const std::string& name,
  229. int boundary);
  230. // Function for adding a |sample| to a histogram.
  231. void HistogramAdd(Histogram* histogram_pointer, int sample);
  232. struct SampleInfo {
  233. SampleInfo(const std::string& name, int min, int max, size_t bucket_count);
  234. ~SampleInfo();
  235. const std::string name;
  236. const int min;
  237. const int max;
  238. const size_t bucket_count;
  239. std::map<int, int> samples; // <value, # of events>
  240. };
  241. // Enables collection of samples.
  242. // This method should be called before any other call into webrtc.
  243. void Enable();
  244. // Gets histograms and clears all samples.
  245. void GetAndReset(
  246. std::map<std::string, std::unique_ptr<SampleInfo>>* histograms);
  247. // Functions below are mainly for testing.
  248. // Clears all samples.
  249. void Reset();
  250. // Returns the number of times the |sample| has been added to the histogram.
  251. int NumEvents(const std::string& name, int sample);
  252. // Returns the total number of added samples to the histogram.
  253. int NumSamples(const std::string& name);
  254. // Returns the minimum sample value (or -1 if the histogram has no samples).
  255. int MinSample(const std::string& name);
  256. // Returns a map with keys the samples with at least one event and values the
  257. // number of events for that sample.
  258. std::map<int, int> Samples(const std::string& name);
  259. } // namespace metrics
  260. } // namespace webrtc
  261. #endif // SYSTEM_WRAPPERS_INCLUDE_METRICS_H_