OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "base/android/record_histogram.h" |
| 6 |
| 7 #include <map> |
| 8 |
5 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
6 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
7 #include "base/android/record_histogram.h" | 11 #include "base/lazy_instance.h" |
8 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
9 #include "base/metrics/statistics_recorder.h" | 13 #include "base/metrics/statistics_recorder.h" |
| 14 #include "base/synchronization/lock.h" |
10 #include "jni/RecordHistogram_jni.h" | 15 #include "jni/RecordHistogram_jni.h" |
11 | 16 |
12 namespace base { | 17 namespace base { |
13 namespace android { | 18 namespace android { |
| 19 namespace { |
| 20 |
| 21 // Simple thread-safe wrapper for caching histograms. This avoids |
| 22 // relatively expensive JNI string translation for each recording. |
| 23 class HistogramCache { |
| 24 public: |
| 25 HistogramCache() {} |
| 26 |
| 27 HistogramBase* BooleanHistogram(JNIEnv* env, |
| 28 jstring j_histogram_name, |
| 29 jint j_histogram_key) { |
| 30 DCHECK(j_histogram_name); |
| 31 DCHECK(j_histogram_key); |
| 32 HistogramBase* histogram = FindLocked(j_histogram_key); |
| 33 if (histogram) |
| 34 return histogram; |
| 35 |
| 36 std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name); |
| 37 histogram = BooleanHistogram::FactoryGet( |
| 38 histogram_name, HistogramBase::kUmaTargetedHistogramFlag); |
| 39 return InsertLocked(j_histogram_key, histogram); |
| 40 } |
| 41 |
| 42 HistogramBase* EnumeratedHistogram(JNIEnv* env, |
| 43 jstring j_histogram_name, |
| 44 jint j_histogram_key, |
| 45 jint j_boundary) { |
| 46 DCHECK(j_histogram_name); |
| 47 DCHECK(j_histogram_key); |
| 48 HistogramBase* histogram = FindLocked(j_histogram_key); |
| 49 if (histogram) |
| 50 return histogram; |
| 51 |
| 52 std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name); |
| 53 // Note: This caching bypasses the boundary validation that occurs between |
| 54 // repeated lookups by the same name. It is up to the caller to ensure that |
| 55 // the provided boundary remains consistent. |
| 56 int boundary = static_cast<int>(j_boundary); |
| 57 histogram = |
| 58 LinearHistogram::FactoryGet(histogram_name, 1, boundary, boundary + 1, |
| 59 HistogramBase::kUmaTargetedHistogramFlag); |
| 60 return InsertLocked(j_histogram_key, histogram); |
| 61 } |
| 62 |
| 63 private: |
| 64 HistogramBase* FindLocked(jint j_histogram_key) { |
| 65 base::AutoLock locked(lock_); |
| 66 auto histogram_it = histograms_.find(j_histogram_key); |
| 67 return histogram_it != histograms_.end() ? histogram_it->second : nullptr; |
| 68 } |
| 69 |
| 70 HistogramBase* InsertLocked(jint j_histogram_key, HistogramBase* histogram) { |
| 71 base::AutoLock locked(lock_); |
| 72 histograms_.insert(std::make_pair(j_histogram_key, histogram)); |
| 73 return histogram; |
| 74 } |
| 75 |
| 76 base::Lock lock_; |
| 77 std::map<jint, HistogramBase*> histograms_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(HistogramCache); |
| 80 }; |
| 81 |
| 82 base::LazyInstance<HistogramCache>::Leaky g_histograms; |
| 83 |
| 84 } // namespace |
14 | 85 |
15 void RecordBooleanHistogram(JNIEnv* env, | 86 void RecordBooleanHistogram(JNIEnv* env, |
16 jclass clazz, | 87 jclass clazz, |
17 jstring j_histogram_name, | 88 jstring j_histogram_name, |
| 89 jint j_histogram_key, |
18 jboolean j_sample) { | 90 jboolean j_sample) { |
19 std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name); | |
20 bool sample = static_cast<bool>(j_sample); | 91 bool sample = static_cast<bool>(j_sample); |
21 | 92 g_histograms.Get() |
22 BooleanHistogram::FactoryGet(histogram_name, | 93 .BooleanHistogram(env, j_histogram_name, j_histogram_key) |
23 HistogramBase::kUmaTargetedHistogramFlag) | |
24 ->AddBoolean(sample); | 94 ->AddBoolean(sample); |
25 } | 95 } |
26 | 96 |
27 void RecordEnumeratedHistogram(JNIEnv* env, | 97 void RecordEnumeratedHistogram(JNIEnv* env, |
28 jclass clazz, | 98 jclass clazz, |
29 jstring j_histogram_name, | 99 jstring j_histogram_name, |
| 100 jint j_histogram_key, |
30 jint j_sample, | 101 jint j_sample, |
31 jint j_boundary) { | 102 jint j_boundary) { |
32 std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name); | |
33 int sample = static_cast<int>(j_sample); | 103 int sample = static_cast<int>(j_sample); |
34 int boundary = static_cast<int>(j_boundary); | |
35 | 104 |
36 LinearHistogram::FactoryGet(histogram_name, 1, boundary, boundary + 1, | 105 g_histograms.Get() |
37 HistogramBase::kUmaTargetedHistogramFlag) | 106 .EnumeratedHistogram(env, j_histogram_name, j_histogram_key, j_boundary) |
38 ->Add(sample); | 107 ->Add(sample); |
39 } | 108 } |
40 | 109 |
41 void Initialize(JNIEnv* env, jclass) { | 110 void Initialize(JNIEnv* env, jclass) { |
42 StatisticsRecorder::Initialize(); | 111 StatisticsRecorder::Initialize(); |
43 } | 112 } |
44 | 113 |
45 // This backs a Java test util for testing histograms - | 114 // This backs a Java test util for testing histograms - |
46 // MetricsUtils.HistogramDelta. It should live in a test-specific file, but we | 115 // MetricsUtils.HistogramDelta. It should live in a test-specific file, but we |
47 // currently can't have test-specific native code packaged in test-specific Java | 116 // currently can't have test-specific native code packaged in test-specific Java |
(...skipping 12 matching lines...) Expand all Loading... |
60 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); | 129 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
61 return samples->GetCount(static_cast<int>(sample)); | 130 return samples->GetCount(static_cast<int>(sample)); |
62 } | 131 } |
63 | 132 |
64 bool RegisterRecordHistogram(JNIEnv* env) { | 133 bool RegisterRecordHistogram(JNIEnv* env) { |
65 return RegisterNativesImpl(env); | 134 return RegisterNativesImpl(env); |
66 } | 135 } |
67 | 136 |
68 } // namespace android | 137 } // namespace android |
69 } // namespace base | 138 } // namespace base |
OLD | NEW |