1 /*
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 #include <memory>
13
14 #include "rtc_base/string_utils.h"
15 #include "sdk/android/generated_metrics_jni/Metrics_jni.h"
16 #include "sdk/android/native_api/jni/java_types.h"
17 #include "sdk/android/src/jni/jni_helpers.h"
18 #include "system_wrappers/include/metrics.h"
19
20 // Enables collection of native histograms and creating them.
21 namespace webrtc {
22 namespace jni {
23
JNI_Metrics_Enable(JNIEnv * jni)24 static void JNI_Metrics_Enable(JNIEnv* jni) {
25 metrics::Enable();
26 }
27
28 // Gets and clears native histograms.
JNI_Metrics_GetAndReset(JNIEnv * jni)29 static ScopedJavaLocalRef<jobject> JNI_Metrics_GetAndReset(JNIEnv* jni) {
30 ScopedJavaLocalRef<jobject> j_metrics = Java_Metrics_Constructor(jni);
31
32 std::map<std::string, std::unique_ptr<metrics::SampleInfo>,
33 rtc::AbslStringViewCmp>
34 histograms;
35 metrics::GetAndReset(&histograms);
36 for (const auto& kv : histograms) {
37 // Create and add samples to `HistogramInfo`.
38 ScopedJavaLocalRef<jobject> j_info = Java_HistogramInfo_Constructor(
39 jni, kv.second->min, kv.second->max,
40 static_cast<int>(kv.second->bucket_count));
41 for (const auto& sample : kv.second->samples) {
42 Java_HistogramInfo_addSample(jni, j_info, sample.first, sample.second);
43 }
44 // Add `HistogramInfo` to `Metrics`.
45 ScopedJavaLocalRef<jstring> j_name = NativeToJavaString(jni, kv.first);
46 Java_Metrics_add(jni, j_metrics, j_name, j_info);
47 }
48 CHECK_EXCEPTION(jni);
49 return j_metrics;
50 }
51
52 } // namespace jni
53 } // namespace webrtc
54