OLD | NEW |
---|---|
(Empty) | |
1 /* | |
pbos-webrtc
2016/05/02 00:29:10
Shouldn't this file be part of metrics_default? I
åsapersson
2016/05/06 13:31:57
Added implementation to metrics_default and have r
| |
2 * Copyright 2016 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 | |
11 #include <map> | |
12 | |
13 #include "webrtc/api/java/jni/classreferenceholder.h" | |
14 #include "webrtc/api/java/jni/jni_helpers.h" | |
15 #include "webrtc/api/java/jni/native_handle_impl.h" | |
16 #include "webrtc/base/criticalsection.h" | |
17 #include "webrtc/base/thread_annotations.h" | |
18 #include "webrtc/system_wrappers/include/metrics.h" | |
19 | |
20 namespace { | |
21 // Limit for the maximum number of sample values that can be stored. | |
22 const int kMaxMapSize = 10000; | |
23 | |
24 struct SampleInfo { | |
25 SampleInfo(const std::string& name, int min, int max, int bucket_count) | |
26 : name(name), min(min), max(max), bucket_count(bucket_count) {} | |
27 const std::string name; | |
28 const int min; | |
29 const int max; | |
30 const int bucket_count; | |
31 std::map<int, int> samples; // <value, # of events> | |
32 }; | |
33 | |
34 // Map holding samples of a histogram (mapped by the histogram name). | |
35 rtc::CriticalSection rtc_histograms_crit_; | |
36 bool rtc_histograms_enabled_ = false; | |
pbos-webrtc
2016/05/02 00:29:10
Can we expect this to be set before any webrtc cod
åsapersson
2016/05/06 13:31:57
Done.
| |
37 std::map<std::string, SampleInfo> rtc_histograms_ | |
38 GUARDED_BY(rtc_histograms_crit_); | |
39 } // namespace | |
40 | |
41 // Android implementation of histogram methods in | |
42 // webrtc/system_wrappers/interface/metrics.h. | |
43 | |
44 namespace webrtc { | |
45 namespace metrics { | |
46 class Histogram; | |
47 | |
48 // Histogram with exponentially spaced buckets. | |
49 Histogram* HistogramFactoryGetCounts(const std::string& name, | |
50 int min, | |
51 int max, | |
52 int bucket_count) { | |
53 rtc::CritScope cs(&rtc_histograms_crit_); | |
54 if (rtc_histograms_.find(name) == rtc_histograms_.end()) { | |
55 rtc_histograms_.insert( | |
56 std::make_pair(name, SampleInfo(name, min, max, bucket_count))); | |
57 } | |
58 auto it = rtc_histograms_.find(name); | |
59 return reinterpret_cast<Histogram*>(&it->second); | |
60 } | |
61 | |
62 // Histogram with linearly spaced buckets. | |
63 Histogram* HistogramFactoryGetEnumeration(const std::string& name, | |
64 int boundary) { | |
65 rtc::CritScope cs(&rtc_histograms_crit_); | |
66 if (rtc_histograms_.find(name) == rtc_histograms_.end()) { | |
67 rtc_histograms_.insert( | |
68 std::make_pair(name, SampleInfo(name, 1, boundary, boundary + 1))); | |
69 } | |
70 auto it = rtc_histograms_.find(name); | |
71 return reinterpret_cast<Histogram*>(&it->second); | |
72 } | |
73 | |
74 void HistogramAdd(Histogram* histogram_pointer, | |
75 const std::string& name, | |
76 int sample) { | |
77 rtc::CritScope cs(&rtc_histograms_crit_); | |
78 if (!rtc_histograms_enabled_) | |
79 return; | |
80 | |
81 SampleInfo* ptr = reinterpret_cast<SampleInfo*>(histogram_pointer); | |
pbos-webrtc
2016/05/02 00:29:10
This function shouldn't hold the rtc_histograms_cr
åsapersson
2016/05/06 13:31:57
Done.
| |
82 RTC_DCHECK(ptr->name == name) << "The name should not vary."; | |
83 | |
84 if (sample < 0) | |
85 sample = 0; | |
86 if (sample > ptr->max) | |
87 sample = ptr->max; | |
88 | |
89 if (ptr->samples.size() == kMaxMapSize && | |
90 ptr->samples.find(sample) == ptr->samples.end()) { | |
91 return; | |
92 } | |
93 ++ptr->samples[sample]; | |
94 } | |
95 | |
96 } // namespace metrics | |
97 } // namespace webrtc | |
98 | |
99 | |
100 // Enables collection of samples to |rtc_histograms_|. | |
101 namespace webrtc_jni { | |
102 JOW(void, Histograms_nativeEnable)(JNIEnv* jni, jclass) { | |
pbos-webrtc
2016/05/02 00:29:10
Can we have C++ versions of these functions as wel
åsapersson
2016/05/06 13:31:57
Done.
| |
103 rtc::CritScope cs(&rtc_histograms_crit_); | |
104 rtc_histograms_enabled_ = true; | |
105 } | |
106 | |
107 // Gets and resets samples in |rtc_histograms_|. | |
108 JOW(jobject, Histograms_nativeGetAndReset)(JNIEnv* jni, jclass) { | |
109 jclass j_histograms_class = FindClass(jni, "org/webrtc/Histograms"); | |
110 jmethodID j_add = | |
111 GetMethodID(jni, j_histograms_class, "add", | |
112 "(Ljava/lang/String;Lorg/webrtc/Histograms$HistogramInfo;)V"); | |
113 jclass j_info_class = FindClass(jni, "org/webrtc/Histograms$HistogramInfo"); | |
114 jmethodID j_add_sample = GetMethodID(jni, j_info_class, "addSample", "(II)V"); | |
115 | |
116 // Create |Histograms|. | |
117 jobject j_histograms = | |
118 jni->NewObject(j_histograms_class, | |
119 GetMethodID(jni, j_histograms_class, "<init>", "()V")); | |
120 | |
121 rtc::CritScope cs(&rtc_histograms_crit_); | |
122 for (auto& kv : rtc_histograms_) { | |
123 // Create and add samples to |HistogramInfo|. | |
124 jobject j_info = jni->NewObject( | |
125 j_info_class, GetMethodID(jni, j_info_class, "<init>", "(III)V"), | |
126 kv.second.min, kv.second.max, kv.second.bucket_count); | |
127 for (const auto& sample : kv.second.samples) { | |
128 jni->CallVoidMethod(j_info, j_add_sample, sample.first, sample.second); | |
129 } | |
130 kv.second.samples.clear(); | |
131 // Add |HistogramInfo| to |Histograms|. | |
132 jstring j_name = jni->NewStringUTF(kv.first.c_str()); | |
133 jni->CallVoidMethod(j_histograms, j_add, j_name, j_info); | |
134 jni->DeleteLocalRef(j_name); | |
135 jni->DeleteLocalRef(j_info); | |
136 } | |
137 CHECK_EXCEPTION(jni); | |
138 return j_histograms; | |
139 } | |
140 } // namespace webrtc_jni | |
OLD | NEW |