ppi | dde405ee6 | 2015-01-22 01:12:51 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | b30f240 | 2015-12-24 03:43:28 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 7 | #include <map> |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 8 | #include <string> |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 9 | |
ppi | dde405ee6 | 2015-01-22 01:12:51 | [diff] [blame] | 10 | #include "base/android/jni_android.h" |
| 11 | #include "base/android/jni_string.h" |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 12 | #include "base/lazy_instance.h" |
avi | b30f240 | 2015-12-24 03:43:28 | [diff] [blame] | 13 | #include "base/macros.h" |
ppi | dde405ee6 | 2015-01-22 01:12:51 | [diff] [blame] | 14 | #include "base/metrics/histogram.h" |
pkotwicz | 5aab443 | 2015-04-03 08:01:02 | [diff] [blame] | 15 | #include "base/metrics/sparse_histogram.h" |
ppi | dde405ee6 | 2015-01-22 01:12:51 | [diff] [blame] | 16 | #include "base/metrics/statistics_recorder.h" |
asvitkine | bb7cf599 | 2016-02-12 00:21:03 | [diff] [blame] | 17 | #include "base/strings/stringprintf.h" |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 18 | #include "base/synchronization/lock.h" |
cjhopman | dcb2b41 | 2015-02-12 01:43:32 | [diff] [blame] | 19 | #include "base/time/time.h" |
ppi | dde405ee6 | 2015-01-22 01:12:51 | [diff] [blame] | 20 | #include "jni/RecordHistogram_jni.h" |
| 21 | |
| 22 | namespace base { |
| 23 | namespace android { |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | // Simple thread-safe wrapper for caching histograms. This avoids |
| 27 | // relatively expensive JNI string translation for each recording. |
| 28 | class HistogramCache { |
| 29 | public: |
| 30 | HistogramCache() {} |
| 31 | |
asvitkine | bb7cf599 | 2016-02-12 00:21:03 | [diff] [blame] | 32 | std::string HistogramConstructionParamsToString(HistogramBase* histogram) { |
| 33 | std::string params_str = histogram->histogram_name(); |
| 34 | switch (histogram->GetHistogramType()) { |
| 35 | case HISTOGRAM: |
| 36 | case LINEAR_HISTOGRAM: |
| 37 | case BOOLEAN_HISTOGRAM: |
| 38 | case CUSTOM_HISTOGRAM: { |
| 39 | Histogram* hist = static_cast<Histogram*>(histogram); |
| 40 | params_str += StringPrintf("/%d/%d/%d", hist->declared_min(), |
| 41 | hist->declared_max(), hist->bucket_count()); |
| 42 | break; |
| 43 | } |
| 44 | case SPARSE_HISTOGRAM: |
Gayane Petrosyan | 5745ac6 | 2018-03-23 01:45:24 | [diff] [blame] | 45 | case DUMMY_HISTOGRAM: |
asvitkine | bb7cf599 | 2016-02-12 00:21:03 | [diff] [blame] | 46 | break; |
| 47 | } |
| 48 | return params_str; |
| 49 | } |
| 50 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 51 | void JNI_RecordHistogram_CheckHistogramArgs(JNIEnv* env, |
| 52 | jstring j_histogram_name, |
| 53 | int32_t expected_min, |
| 54 | int32_t expected_max, |
| 55 | uint32_t expected_bucket_count, |
| 56 | HistogramBase* histogram) { |
romax | 0e11086 | 2017-02-07 19:54:00 | [diff] [blame] | 57 | std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name); |
| 58 | bool valid_arguments = Histogram::InspectConstructionArguments( |
| 59 | histogram_name, &expected_min, &expected_max, &expected_bucket_count); |
| 60 | DCHECK(valid_arguments); |
asvitkine | bb7cf599 | 2016-02-12 00:21:03 | [diff] [blame] | 61 | DCHECK(histogram->HasConstructionArguments(expected_min, expected_max, |
| 62 | expected_bucket_count)) |
romax | 0e11086 | 2017-02-07 19:54:00 | [diff] [blame] | 63 | << histogram_name << "/" << expected_min << "/" << expected_max << "/" |
| 64 | << expected_bucket_count << " vs. " |
asvitkine | bb7cf599 | 2016-02-12 00:21:03 | [diff] [blame] | 65 | << HistogramConstructionParamsToString(histogram); |
| 66 | } |
| 67 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 68 | HistogramBase* JNI_RecordHistogram_BooleanHistogram(JNIEnv* env, |
| 69 | jstring j_histogram_name, |
| 70 | jlong j_histogram_key) { |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 71 | DCHECK(j_histogram_name); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 72 | HistogramBase* histogram = HistogramFromKey(j_histogram_key); |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 73 | if (histogram) |
| 74 | return histogram; |
| 75 | |
| 76 | std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name); |
| 77 | histogram = BooleanHistogram::FactoryGet( |
| 78 | histogram_name, HistogramBase::kUmaTargetedHistogramFlag); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 79 | return histogram; |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 80 | } |
| 81 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 82 | HistogramBase* JNI_RecordHistogram_EnumeratedHistogram( |
| 83 | JNIEnv* env, |
| 84 | jstring j_histogram_name, |
| 85 | jlong j_histogram_key, |
| 86 | jint j_boundary) { |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 87 | DCHECK(j_histogram_name); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 88 | HistogramBase* histogram = HistogramFromKey(j_histogram_key); |
asvitkine | bb7cf599 | 2016-02-12 00:21:03 | [diff] [blame] | 89 | int32_t boundary = static_cast<int32_t>(j_boundary); |
cjhopman | dcb2b41 | 2015-02-12 01:43:32 | [diff] [blame] | 90 | if (histogram) { |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 91 | JNI_RecordHistogram_CheckHistogramArgs(env, j_histogram_name, 1, boundary, |
| 92 | boundary + 1, histogram); |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 93 | return histogram; |
cjhopman | dcb2b41 | 2015-02-12 01:43:32 | [diff] [blame] | 94 | } |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 95 | |
| 96 | std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name); |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 97 | histogram = |
| 98 | LinearHistogram::FactoryGet(histogram_name, 1, boundary, boundary + 1, |
| 99 | HistogramBase::kUmaTargetedHistogramFlag); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 100 | return histogram; |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 101 | } |
| 102 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 103 | HistogramBase* JNI_RecordHistogram_CustomCountHistogram( |
| 104 | JNIEnv* env, |
| 105 | jstring j_histogram_name, |
| 106 | jlong j_histogram_key, |
| 107 | jint j_min, |
| 108 | jint j_max, |
| 109 | jint j_num_buckets) { |
hartmanng | 39be88c7 | 2015-03-12 22:13:50 | [diff] [blame] | 110 | DCHECK(j_histogram_name); |
asvitkine | bb7cf599 | 2016-02-12 00:21:03 | [diff] [blame] | 111 | int32_t min = static_cast<int32_t>(j_min); |
| 112 | int32_t max = static_cast<int32_t>(j_max); |
| 113 | int32_t num_buckets = static_cast<int32_t>(j_num_buckets); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 114 | HistogramBase* histogram = HistogramFromKey(j_histogram_key); |
hartmanng | 39be88c7 | 2015-03-12 22:13:50 | [diff] [blame] | 115 | if (histogram) { |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 116 | JNI_RecordHistogram_CheckHistogramArgs(env, j_histogram_name, min, max, |
| 117 | num_buckets, histogram); |
hartmanng | 39be88c7 | 2015-03-12 22:13:50 | [diff] [blame] | 118 | return histogram; |
| 119 | } |
| 120 | |
sebsg | d96ddb1 | 2016-10-28 21:10:56 | [diff] [blame] | 121 | DCHECK_GE(min, 1) << "The min expected sample must be >= 1"; |
| 122 | |
hartmanng | 39be88c7 | 2015-03-12 22:13:50 | [diff] [blame] | 123 | std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name); |
pkotwicz | 5aab443 | 2015-04-03 08:01:02 | [diff] [blame] | 124 | histogram = |
| 125 | Histogram::FactoryGet(histogram_name, min, max, num_buckets, |
| 126 | HistogramBase::kUmaTargetedHistogramFlag); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 127 | return histogram; |
pkotwicz | 5aab443 | 2015-04-03 08:01:02 | [diff] [blame] | 128 | } |
| 129 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 130 | HistogramBase* JNI_RecordHistogram_LinearCountHistogram( |
| 131 | JNIEnv* env, |
| 132 | jstring j_histogram_name, |
| 133 | jlong j_histogram_key, |
| 134 | jint j_min, |
| 135 | jint j_max, |
| 136 | jint j_num_buckets) { |
khushalsagar | 0e93d6cb | 2015-08-31 17:33:42 | [diff] [blame] | 137 | DCHECK(j_histogram_name); |
asvitkine | bb7cf599 | 2016-02-12 00:21:03 | [diff] [blame] | 138 | int32_t min = static_cast<int32_t>(j_min); |
| 139 | int32_t max = static_cast<int32_t>(j_max); |
| 140 | int32_t num_buckets = static_cast<int32_t>(j_num_buckets); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 141 | HistogramBase* histogram = HistogramFromKey(j_histogram_key); |
khushalsagar | 0e93d6cb | 2015-08-31 17:33:42 | [diff] [blame] | 142 | if (histogram) { |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 143 | JNI_RecordHistogram_CheckHistogramArgs(env, j_histogram_name, min, max, |
| 144 | num_buckets, histogram); |
khushalsagar | 0e93d6cb | 2015-08-31 17:33:42 | [diff] [blame] | 145 | return histogram; |
| 146 | } |
| 147 | |
| 148 | std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name); |
| 149 | histogram = |
| 150 | LinearHistogram::FactoryGet(histogram_name, min, max, num_buckets, |
| 151 | HistogramBase::kUmaTargetedHistogramFlag); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 152 | return histogram; |
khushalsagar | 0e93d6cb | 2015-08-31 17:33:42 | [diff] [blame] | 153 | } |
| 154 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 155 | HistogramBase* JNI_RecordHistogram_SparseHistogram(JNIEnv* env, |
| 156 | jstring j_histogram_name, |
| 157 | jlong j_histogram_key) { |
pkotwicz | 5aab443 | 2015-04-03 08:01:02 | [diff] [blame] | 158 | DCHECK(j_histogram_name); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 159 | HistogramBase* histogram = HistogramFromKey(j_histogram_key); |
pkotwicz | 5aab443 | 2015-04-03 08:01:02 | [diff] [blame] | 160 | if (histogram) |
| 161 | return histogram; |
| 162 | |
| 163 | std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name); |
| 164 | histogram = SparseHistogram::FactoryGet( |
| 165 | histogram_name, HistogramBase::kUmaTargetedHistogramFlag); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 166 | return histogram; |
hartmanng | 39be88c7 | 2015-03-12 22:13:50 | [diff] [blame] | 167 | } |
| 168 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 169 | HistogramBase* JNI_RecordHistogram_CustomTimesHistogram( |
| 170 | JNIEnv* env, |
| 171 | jstring j_histogram_name, |
| 172 | jlong j_histogram_key, |
| 173 | jint j_min, |
| 174 | jint j_max, |
| 175 | jint j_bucket_count) { |
cjhopman | dcb2b41 | 2015-02-12 01:43:32 | [diff] [blame] | 176 | DCHECK(j_histogram_name); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 177 | HistogramBase* histogram = HistogramFromKey(j_histogram_key); |
asvitkine | bb7cf599 | 2016-02-12 00:21:03 | [diff] [blame] | 178 | int32_t min = static_cast<int32_t>(j_min); |
| 179 | int32_t max = static_cast<int32_t>(j_max); |
| 180 | int32_t bucket_count = static_cast<int32_t>(j_bucket_count); |
cjhopman | dcb2b41 | 2015-02-12 01:43:32 | [diff] [blame] | 181 | if (histogram) { |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 182 | JNI_RecordHistogram_CheckHistogramArgs(env, j_histogram_name, min, max, |
| 183 | bucket_count, histogram); |
cjhopman | dcb2b41 | 2015-02-12 01:43:32 | [diff] [blame] | 184 | return histogram; |
| 185 | } |
| 186 | |
| 187 | std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name); |
| 188 | // This intentionally uses FactoryGet and not FactoryTimeGet. FactoryTimeGet |
| 189 | // is just a convenience for constructing the underlying Histogram with |
| 190 | // TimeDelta arguments. |
| 191 | histogram = Histogram::FactoryGet(histogram_name, min, max, bucket_count, |
| 192 | HistogramBase::kUmaTargetedHistogramFlag); |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 193 | return histogram; |
| 194 | } |
| 195 | |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 196 | private: |
| 197 | // Convert a jlong |histogram_key| from Java to a HistogramBase* via a cast. |
| 198 | // The Java side caches these in a map (see RecordHistogram.java), which is |
| 199 | // safe to do since C++ Histogram objects are never freed. |
| 200 | static HistogramBase* HistogramFromKey(jlong j_histogram_key) { |
| 201 | return reinterpret_cast<HistogramBase*>(j_histogram_key); |
| 202 | } |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 203 | |
| 204 | DISALLOW_COPY_AND_ASSIGN(HistogramCache); |
| 205 | }; |
| 206 | |
asvitkine | bb7cf599 | 2016-02-12 00:21:03 | [diff] [blame] | 207 | LazyInstance<HistogramCache>::Leaky g_histograms; |
jdduke | 825e324 | 2015-01-27 00:13:54 | [diff] [blame] | 208 | |
| 209 | } // namespace |
ppi | dde405ee6 | 2015-01-22 01:12:51 | [diff] [blame] | 210 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 211 | jlong JNI_RecordHistogram_RecordBooleanHistogram( |
| 212 | JNIEnv* env, |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 213 | const JavaParamRef<jstring>& j_histogram_name, |
| 214 | jlong j_histogram_key, |
| 215 | jboolean j_sample) { |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 216 | bool sample = static_cast<bool>(j_sample); |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 217 | HistogramBase* histogram = |
| 218 | g_histograms.Get().JNI_RecordHistogram_BooleanHistogram( |
| 219 | env, j_histogram_name, j_histogram_key); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 220 | histogram->AddBoolean(sample); |
| 221 | return reinterpret_cast<jlong>(histogram); |
| 222 | } |
| 223 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 224 | jlong JNI_RecordHistogram_RecordEnumeratedHistogram( |
| 225 | JNIEnv* env, |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 226 | const JavaParamRef<jstring>& j_histogram_name, |
| 227 | jlong j_histogram_key, |
| 228 | jint j_sample, |
| 229 | jint j_boundary) { |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 230 | int sample = static_cast<int>(j_sample); |
| 231 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 232 | HistogramBase* histogram = |
| 233 | g_histograms.Get().JNI_RecordHistogram_EnumeratedHistogram( |
| 234 | env, j_histogram_name, j_histogram_key, j_boundary); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 235 | histogram->Add(sample); |
| 236 | return reinterpret_cast<jlong>(histogram); |
| 237 | } |
| 238 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 239 | jlong JNI_RecordHistogram_RecordCustomCountHistogram( |
| 240 | JNIEnv* env, |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 241 | const JavaParamRef<jstring>& j_histogram_name, |
| 242 | jlong j_histogram_key, |
| 243 | jint j_sample, |
| 244 | jint j_min, |
| 245 | jint j_max, |
| 246 | jint j_num_buckets) { |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 247 | int sample = static_cast<int>(j_sample); |
| 248 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 249 | HistogramBase* histogram = |
| 250 | g_histograms.Get().JNI_RecordHistogram_CustomCountHistogram( |
| 251 | env, j_histogram_name, j_histogram_key, j_min, j_max, j_num_buckets); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 252 | histogram->Add(sample); |
| 253 | return reinterpret_cast<jlong>(histogram); |
| 254 | } |
| 255 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 256 | jlong JNI_RecordHistogram_RecordLinearCountHistogram( |
| 257 | JNIEnv* env, |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 258 | const JavaParamRef<jstring>& j_histogram_name, |
| 259 | jlong j_histogram_key, |
| 260 | jint j_sample, |
| 261 | jint j_min, |
| 262 | jint j_max, |
| 263 | jint j_num_buckets) { |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 264 | int sample = static_cast<int>(j_sample); |
| 265 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 266 | HistogramBase* histogram = |
| 267 | g_histograms.Get().JNI_RecordHistogram_LinearCountHistogram( |
| 268 | env, j_histogram_name, j_histogram_key, j_min, j_max, j_num_buckets); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 269 | histogram->Add(sample); |
| 270 | return reinterpret_cast<jlong>(histogram); |
| 271 | } |
| 272 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 273 | jlong JNI_RecordHistogram_RecordSparseHistogram( |
| 274 | JNIEnv* env, |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 275 | const JavaParamRef<jstring>& j_histogram_name, |
| 276 | jlong j_histogram_key, |
| 277 | jint j_sample) { |
ppi | dde405ee6 | 2015-01-22 01:12:51 | [diff] [blame] | 278 | int sample = static_cast<int>(j_sample); |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 279 | HistogramBase* histogram = |
| 280 | g_histograms.Get().JNI_RecordHistogram_SparseHistogram( |
| 281 | env, j_histogram_name, j_histogram_key); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 282 | histogram->Add(sample); |
| 283 | return reinterpret_cast<jlong>(histogram); |
ppi | dde405ee6 | 2015-01-22 01:12:51 | [diff] [blame] | 284 | } |
| 285 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 286 | jlong JNI_RecordHistogram_RecordCustomTimesHistogramMilliseconds( |
torne | 89cc5d9 | 2015-09-04 11:16:35 | [diff] [blame] | 287 | JNIEnv* env, |
torne | 89cc5d9 | 2015-09-04 11:16:35 | [diff] [blame] | 288 | const JavaParamRef<jstring>& j_histogram_name, |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 289 | jlong j_histogram_key, |
asvitkine | bb7cf599 | 2016-02-12 00:21:03 | [diff] [blame] | 290 | jint j_duration, |
| 291 | jint j_min, |
| 292 | jint j_max, |
torne | 89cc5d9 | 2015-09-04 11:16:35 | [diff] [blame] | 293 | jint j_num_buckets) { |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 294 | HistogramBase* histogram = |
| 295 | g_histograms.Get().JNI_RecordHistogram_CustomTimesHistogram( |
| 296 | env, j_histogram_name, j_histogram_key, j_min, j_max, j_num_buckets); |
asvitkine | 0f5bc5e | 2016-04-05 22:43:30 | [diff] [blame] | 297 | histogram->AddTime( |
| 298 | TimeDelta::FromMilliseconds(static_cast<int64_t>(j_duration))); |
| 299 | return reinterpret_cast<jlong>(histogram); |
cjhopman | dcb2b41 | 2015-02-12 01:43:32 | [diff] [blame] | 300 | } |
| 301 | |
ppi | dde405ee6 | 2015-01-22 01:12:51 | [diff] [blame] | 302 | // This backs a Java test util for testing histograms - |
| 303 | // MetricsUtils.HistogramDelta. It should live in a test-specific file, but we |
| 304 | // currently can't have test-specific native code packaged in test-specific Java |
| 305 | // targets - see http://crbug.com/415945. |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 306 | jint JNI_RecordHistogram_GetHistogramValueCountForTesting( |
torne | 89cc5d9 | 2015-09-04 11:16:35 | [diff] [blame] | 307 | JNIEnv* env, |
torne | 89cc5d9 | 2015-09-04 11:16:35 | [diff] [blame] | 308 | const JavaParamRef<jstring>& histogram_name, |
| 309 | jint sample) { |
ppi | dde405ee6 | 2015-01-22 01:12:51 | [diff] [blame] | 310 | HistogramBase* histogram = StatisticsRecorder::FindHistogram( |
| 311 | android::ConvertJavaStringToUTF8(env, histogram_name)); |
| 312 | if (histogram == nullptr) { |
| 313 | // No samples have been recorded for this histogram (yet?). |
| 314 | return 0; |
| 315 | } |
| 316 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 317 | std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
ppi | dde405ee6 | 2015-01-22 01:12:51 | [diff] [blame] | 318 | return samples->GetCount(static_cast<int>(sample)); |
| 319 | } |
| 320 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 321 | jint JNI_RecordHistogram_GetHistogramTotalCountForTesting( |
pkotwicz | 5c9311ed | 2017-06-29 21:08:55 | [diff] [blame] | 322 | JNIEnv* env, |
pkotwicz | 5c9311ed | 2017-06-29 21:08:55 | [diff] [blame] | 323 | const JavaParamRef<jstring>& histogram_name) { |
| 324 | HistogramBase* histogram = StatisticsRecorder::FindHistogram( |
| 325 | android::ConvertJavaStringToUTF8(env, histogram_name)); |
| 326 | if (histogram == nullptr) { |
| 327 | // No samples have been recorded for this histogram. |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | return histogram->SnapshotSamples()->TotalCount(); |
| 332 | } |
| 333 | |
ppi | dde405ee6 | 2015-01-22 01:12:51 | [diff] [blame] | 334 | } // namespace android |
| 335 | } // namespace base |