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.

327 lines
9.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. //
  9. #include "system_wrappers/include/metrics.h"
  10. #include <algorithm>
  11. #include "rtc_base/critical_section.h"
  12. #include "rtc_base/thread_annotations.h"
  13. // Default implementation of histogram methods for WebRTC clients that do not
  14. // want to provide their own implementation.
  15. namespace webrtc {
  16. namespace metrics {
  17. class Histogram;
  18. namespace {
  19. // Limit for the maximum number of sample values that can be stored.
  20. // TODO(asapersson): Consider using bucket count (and set up
  21. // linearly/exponentially spaced buckets) if samples are logged more frequently.
  22. const int kMaxSampleMapSize = 300;
  23. class RtcHistogram {
  24. public:
  25. RtcHistogram(const std::string& name, int min, int max, int bucket_count)
  26. : min_(min), max_(max), info_(name, min, max, bucket_count) {
  27. RTC_DCHECK_GT(bucket_count, 0);
  28. }
  29. void Add(int sample) {
  30. sample = std::min(sample, max_);
  31. sample = std::max(sample, min_ - 1); // Underflow bucket.
  32. rtc::CritScope cs(&crit_);
  33. if (info_.samples.size() == kMaxSampleMapSize &&
  34. info_.samples.find(sample) == info_.samples.end()) {
  35. return;
  36. }
  37. ++info_.samples[sample];
  38. }
  39. // Returns a copy (or nullptr if there are no samples) and clears samples.
  40. std::unique_ptr<SampleInfo> GetAndReset() {
  41. rtc::CritScope cs(&crit_);
  42. if (info_.samples.empty())
  43. return nullptr;
  44. SampleInfo* copy =
  45. new SampleInfo(info_.name, info_.min, info_.max, info_.bucket_count);
  46. std::swap(info_.samples, copy->samples);
  47. return std::unique_ptr<SampleInfo>(copy);
  48. }
  49. const std::string& name() const { return info_.name; }
  50. // Functions only for testing.
  51. void Reset() {
  52. rtc::CritScope cs(&crit_);
  53. info_.samples.clear();
  54. }
  55. int NumEvents(int sample) const {
  56. rtc::CritScope cs(&crit_);
  57. const auto it = info_.samples.find(sample);
  58. return (it == info_.samples.end()) ? 0 : it->second;
  59. }
  60. int NumSamples() const {
  61. int num_samples = 0;
  62. rtc::CritScope cs(&crit_);
  63. for (const auto& sample : info_.samples) {
  64. num_samples += sample.second;
  65. }
  66. return num_samples;
  67. }
  68. int MinSample() const {
  69. rtc::CritScope cs(&crit_);
  70. return (info_.samples.empty()) ? -1 : info_.samples.begin()->first;
  71. }
  72. std::map<int, int> Samples() const {
  73. rtc::CritScope cs(&crit_);
  74. return info_.samples;
  75. }
  76. private:
  77. rtc::CriticalSection crit_;
  78. const int min_;
  79. const int max_;
  80. SampleInfo info_ RTC_GUARDED_BY(crit_);
  81. RTC_DISALLOW_COPY_AND_ASSIGN(RtcHistogram);
  82. };
  83. class RtcHistogramMap {
  84. public:
  85. RtcHistogramMap() {}
  86. ~RtcHistogramMap() {}
  87. Histogram* GetCountsHistogram(const std::string& name,
  88. int min,
  89. int max,
  90. int bucket_count) {
  91. rtc::CritScope cs(&crit_);
  92. const auto& it = map_.find(name);
  93. if (it != map_.end())
  94. return reinterpret_cast<Histogram*>(it->second.get());
  95. RtcHistogram* hist = new RtcHistogram(name, min, max, bucket_count);
  96. map_[name].reset(hist);
  97. return reinterpret_cast<Histogram*>(hist);
  98. }
  99. Histogram* GetEnumerationHistogram(const std::string& name, int boundary) {
  100. rtc::CritScope cs(&crit_);
  101. const auto& it = map_.find(name);
  102. if (it != map_.end())
  103. return reinterpret_cast<Histogram*>(it->second.get());
  104. RtcHistogram* hist = new RtcHistogram(name, 1, boundary, boundary + 1);
  105. map_[name].reset(hist);
  106. return reinterpret_cast<Histogram*>(hist);
  107. }
  108. void GetAndReset(
  109. std::map<std::string, std::unique_ptr<SampleInfo>>* histograms) {
  110. rtc::CritScope cs(&crit_);
  111. for (const auto& kv : map_) {
  112. std::unique_ptr<SampleInfo> info = kv.second->GetAndReset();
  113. if (info)
  114. histograms->insert(std::make_pair(kv.first, std::move(info)));
  115. }
  116. }
  117. // Functions only for testing.
  118. void Reset() {
  119. rtc::CritScope cs(&crit_);
  120. for (const auto& kv : map_)
  121. kv.second->Reset();
  122. }
  123. int NumEvents(const std::string& name, int sample) const {
  124. rtc::CritScope cs(&crit_);
  125. const auto& it = map_.find(name);
  126. return (it == map_.end()) ? 0 : it->second->NumEvents(sample);
  127. }
  128. int NumSamples(const std::string& name) const {
  129. rtc::CritScope cs(&crit_);
  130. const auto& it = map_.find(name);
  131. return (it == map_.end()) ? 0 : it->second->NumSamples();
  132. }
  133. int MinSample(const std::string& name) const {
  134. rtc::CritScope cs(&crit_);
  135. const auto& it = map_.find(name);
  136. return (it == map_.end()) ? -1 : it->second->MinSample();
  137. }
  138. std::map<int, int> Samples(const std::string& name) const {
  139. rtc::CritScope cs(&crit_);
  140. const auto& it = map_.find(name);
  141. return (it == map_.end()) ? std::map<int, int>() : it->second->Samples();
  142. }
  143. private:
  144. rtc::CriticalSection crit_;
  145. std::map<std::string, std::unique_ptr<RtcHistogram>> map_
  146. RTC_GUARDED_BY(crit_);
  147. RTC_DISALLOW_COPY_AND_ASSIGN(RtcHistogramMap);
  148. };
  149. // RtcHistogramMap is allocated upon call to Enable().
  150. // The histogram getter functions, which return pointer values to the histograms
  151. // in the map, are cached in WebRTC. Therefore, this memory is not freed by the
  152. // application (the memory will be reclaimed by the OS).
  153. static RtcHistogramMap* volatile g_rtc_histogram_map = nullptr;
  154. void CreateMap() {
  155. RtcHistogramMap* map = rtc::AtomicOps::AcquireLoadPtr(&g_rtc_histogram_map);
  156. if (map == nullptr) {
  157. RtcHistogramMap* new_map = new RtcHistogramMap();
  158. RtcHistogramMap* old_map = rtc::AtomicOps::CompareAndSwapPtr(
  159. &g_rtc_histogram_map, static_cast<RtcHistogramMap*>(nullptr), new_map);
  160. if (old_map != nullptr)
  161. delete new_map;
  162. }
  163. }
  164. // Set the first time we start using histograms. Used to make sure Enable() is
  165. // not called thereafter.
  166. #if RTC_DCHECK_IS_ON
  167. static volatile int g_rtc_histogram_called = 0;
  168. #endif
  169. // Gets the map (or nullptr).
  170. RtcHistogramMap* GetMap() {
  171. #if RTC_DCHECK_IS_ON
  172. rtc::AtomicOps::ReleaseStore(&g_rtc_histogram_called, 1);
  173. #endif
  174. return g_rtc_histogram_map;
  175. }
  176. } // namespace
  177. #ifndef WEBRTC_EXCLUDE_METRICS_DEFAULT
  178. // Implementation of histogram methods in
  179. // webrtc/system_wrappers/interface/metrics.h.
  180. // Histogram with exponentially spaced buckets.
  181. // Creates (or finds) histogram.
  182. // The returned histogram pointer is cached (and used for adding samples in
  183. // subsequent calls).
  184. Histogram* HistogramFactoryGetCounts(const std::string& name,
  185. int min,
  186. int max,
  187. int bucket_count) {
  188. // TODO(asapersson): Alternative implementation will be needed if this
  189. // histogram type should be truly exponential.
  190. return HistogramFactoryGetCountsLinear(name, min, max, bucket_count);
  191. }
  192. // Histogram with linearly spaced buckets.
  193. // Creates (or finds) histogram.
  194. // The returned histogram pointer is cached (and used for adding samples in
  195. // subsequent calls).
  196. Histogram* HistogramFactoryGetCountsLinear(const std::string& name,
  197. int min,
  198. int max,
  199. int bucket_count) {
  200. RtcHistogramMap* map = GetMap();
  201. if (!map)
  202. return nullptr;
  203. return map->GetCountsHistogram(name, min, max, bucket_count);
  204. }
  205. // Histogram with linearly spaced buckets.
  206. // Creates (or finds) histogram.
  207. // The returned histogram pointer is cached (and used for adding samples in
  208. // subsequent calls).
  209. Histogram* HistogramFactoryGetEnumeration(const std::string& name,
  210. int boundary) {
  211. RtcHistogramMap* map = GetMap();
  212. if (!map)
  213. return nullptr;
  214. return map->GetEnumerationHistogram(name, boundary);
  215. }
  216. // Our default implementation reuses the non-sparse histogram.
  217. Histogram* SparseHistogramFactoryGetEnumeration(const std::string& name,
  218. int boundary) {
  219. return HistogramFactoryGetEnumeration(name, boundary);
  220. }
  221. // Fast path. Adds |sample| to cached |histogram_pointer|.
  222. void HistogramAdd(Histogram* histogram_pointer, int sample) {
  223. RtcHistogram* ptr = reinterpret_cast<RtcHistogram*>(histogram_pointer);
  224. ptr->Add(sample);
  225. }
  226. #endif // WEBRTC_EXCLUDE_METRICS_DEFAULT
  227. SampleInfo::SampleInfo(const std::string& name,
  228. int min,
  229. int max,
  230. size_t bucket_count)
  231. : name(name), min(min), max(max), bucket_count(bucket_count) {}
  232. SampleInfo::~SampleInfo() {}
  233. // Implementation of global functions in metrics.h.
  234. void Enable() {
  235. RTC_DCHECK(g_rtc_histogram_map == nullptr);
  236. #if RTC_DCHECK_IS_ON
  237. RTC_DCHECK_EQ(0, rtc::AtomicOps::AcquireLoad(&g_rtc_histogram_called));
  238. #endif
  239. CreateMap();
  240. }
  241. void GetAndReset(
  242. std::map<std::string, std::unique_ptr<SampleInfo>>* histograms) {
  243. histograms->clear();
  244. RtcHistogramMap* map = GetMap();
  245. if (map)
  246. map->GetAndReset(histograms);
  247. }
  248. void Reset() {
  249. RtcHistogramMap* map = GetMap();
  250. if (map)
  251. map->Reset();
  252. }
  253. int NumEvents(const std::string& name, int sample) {
  254. RtcHistogramMap* map = GetMap();
  255. return map ? map->NumEvents(name, sample) : 0;
  256. }
  257. int NumSamples(const std::string& name) {
  258. RtcHistogramMap* map = GetMap();
  259. return map ? map->NumSamples(name) : 0;
  260. }
  261. int MinSample(const std::string& name) {
  262. RtcHistogramMap* map = GetMap();
  263. return map ? map->MinSample(name) : -1;
  264. }
  265. std::map<int, int> Samples(const std::string& name) {
  266. RtcHistogramMap* map = GetMap();
  267. return map ? map->Samples(name) : std::map<int, int>();
  268. }
  269. } // namespace metrics
  270. } // namespace webrtc