// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. // // Use of this source code is governed by a BSD-style license // that can be found in the LICENSE file in the root of the source // tree. An additional intellectual property rights grant can be found // in the file PATENTS. All contributing project authors may // be found in the AUTHORS file in the root of the source tree. // #include "system_wrappers/include/metrics.h" #include <algorithm> #include "absl/strings/string_view.h" #include "rtc_base/string_utils.h" #include "rtc_base/synchronization/mutex.h" #include "rtc_base/thread_annotations.h" // Default implementation of histogram methods for WebRTC clients that do not // want to provide their own implementation. namespace webrtc { namespace metrics { class Histogram; namespace { // Limit for the maximum number of sample values that can be stored. // TODO(asapersson): Consider using bucket count (and set up // linearly/exponentially spaced buckets) if samples are logged more frequently. const int kMaxSampleMapSize = …; class RtcHistogram { … }; class RtcHistogramMap { … }; // RtcHistogramMap is allocated upon call to Enable(). // The histogram getter functions, which return pointer values to the histograms // in the map, are cached in WebRTC. Therefore, this memory is not freed by the // application (the memory will be reclaimed by the OS). static std::atomic<RtcHistogramMap*> g_rtc_histogram_map(nullptr); void CreateMap() { … } // Set the first time we start using histograms. Used to make sure Enable() is // not called thereafter. #if RTC_DCHECK_IS_ON static std::atomic<int> g_rtc_histogram_called(0); #endif // Gets the map (or nullptr). RtcHistogramMap* GetMap() { … } } // namespace #ifndef WEBRTC_EXCLUDE_METRICS_DEFAULT // Implementation of histogram methods in // webrtc/system_wrappers/interface/metrics.h. // Histogram with exponentially spaced buckets. // Creates (or finds) histogram. // The returned histogram pointer is cached (and used for adding samples in // subsequent calls). Histogram* HistogramFactoryGetCounts(absl::string_view name, int min, int max, int bucket_count) { // TODO(asapersson): Alternative implementation will be needed if this // histogram type should be truly exponential. return HistogramFactoryGetCountsLinear(name, min, max, bucket_count); } // Histogram with linearly spaced buckets. // Creates (or finds) histogram. // The returned histogram pointer is cached (and used for adding samples in // subsequent calls). Histogram* HistogramFactoryGetCountsLinear(absl::string_view name, int min, int max, int bucket_count) { RtcHistogramMap* map = GetMap(); if (!map) return nullptr; return map->GetCountsHistogram(name, min, max, bucket_count); } // Histogram with linearly spaced buckets. // Creates (or finds) histogram. // The returned histogram pointer is cached (and used for adding samples in // subsequent calls). Histogram* HistogramFactoryGetEnumeration(absl::string_view name, int boundary) { RtcHistogramMap* map = GetMap(); if (!map) return nullptr; return map->GetEnumerationHistogram(name, boundary); } // Our default implementation reuses the non-sparse histogram. Histogram* SparseHistogramFactoryGetEnumeration(absl::string_view name, int boundary) { return HistogramFactoryGetEnumeration(name, boundary); } // Fast path. Adds `sample` to cached `histogram_pointer`. void HistogramAdd(Histogram* histogram_pointer, int sample) { RtcHistogram* ptr = reinterpret_cast<RtcHistogram*>(histogram_pointer); ptr->Add(sample); } #endif // WEBRTC_EXCLUDE_METRICS_DEFAULT SampleInfo::SampleInfo(absl::string_view name, int min, int max, size_t bucket_count) : … { … } SampleInfo::~SampleInfo() { … } // Implementation of global functions in metrics.h. void Enable() { … } void GetAndReset( std::map<std::string, std::unique_ptr<SampleInfo>, rtc::AbslStringViewCmp>* histograms) { … } void Reset() { … } int NumEvents(absl::string_view name, int sample) { … } int NumSamples(absl::string_view name) { … } int MinSample(absl::string_view name) { … } std::map<int, int> Samples(absl::string_view name) { … } } // namespace metrics } // namespace webrtc