Chromium Code Reviews| Index: components/cronet/android/histogram_manager.cc |
| diff --git a/components/cronet/android/histogram_manager.cc b/components/cronet/android/histogram_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..98d62ea50c59c98fbf3ae2b160bd0c159fb89418 |
| --- /dev/null |
| +++ b/components/cronet/android/histogram_manager.cc |
| @@ -0,0 +1,100 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/cronet/android/histogram_manager.h" |
| + |
| +#include <string> |
| + |
| +#include "base/android/jni_array.h" |
| +#include "base/metrics/histogram_delta_serialization.h" |
| +#include "base/metrics/histogram_samples.h" |
| +#include "base/metrics/histogram_snapshot_manager.h" |
| +#include "base/metrics/sample_map.h" |
| +#include "base/metrics/statistics_recorder.h" |
| +#include "components/metrics/metrics_hashes.h" |
| +#include "components/metrics/metrics_log.h" |
| +#include "components/metrics/proto/histogram_event.pb.h" |
| + |
| +#include "jni/HistogramManager_jni.h" |
| + |
| +using base::SampleCountIterator; |
| + |
| +namespace cronet { |
| + |
| +static const char kCronetHistogramName[] = "cronet"; |
| + |
| +// Explicitly register static JNI functions. |
| +bool HistogramManagerRegisterJni(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +HistogramManager::HistogramManager() { |
| + histogram_flattener_.reset( |
| + new base::HistogramDeltaSerialization(kCronetHistogramName)); |
| + histogram_snapshot_manager_.reset( |
| + new base::HistogramSnapshotManager(histogram_flattener_.get())); |
|
Alexei Svitkine (slow)
2014/12/03 16:46:53
Instead of creating a flattener, HistogramManager
|
| +} |
| + |
| +HistogramManager::~HistogramManager() { |
| +} |
| + |
| +void HistogramManager::RecordHistogramDelta(const std::string& histogram_name, |
|
Alexei Svitkine (slow)
2014/12/03 16:46:53
I think we should avoid duplicating this code - so
|
| + const base::HistogramSamples& snapshot) { |
| + // TODO(mef): figure out where should snapshot come from so it isn't empty. |
| + DCHECK_NE(0, snapshot.TotalCount()); |
| + // We will ignore the MAX_INT/infinite value in the last element of range[]. |
| + |
| + metrics::HistogramEventProto* histogram_proto = |
| + uma_proto_.add_histogram_event(); |
| + histogram_proto->set_name_hash(metrics::HashMetricName(histogram_name)); |
| + histogram_proto->set_sum(snapshot.sum()); |
| + |
| + for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); !it->Done(); |
| + it->Next()) { |
| + base::Histogram::Sample min; |
| + base::Histogram::Sample max; |
| + base::Histogram::Count count; |
| + it->Get(&min, &max, &count); |
| + metrics::HistogramEventProto::Bucket* bucket = |
| + histogram_proto->add_bucket(); |
| + bucket->set_min(min); |
| + bucket->set_max(max); |
| + bucket->set_count(count); |
| + } |
| + |
| + // Omit fields to save space (see rules in histogram_event.proto comments). |
| + for (int i = 0; i < histogram_proto->bucket_size(); ++i) { |
| + metrics::HistogramEventProto::Bucket* bucket = |
| + histogram_proto->mutable_bucket(i); |
| + if (i + 1 < histogram_proto->bucket_size() && |
| + bucket->max() == histogram_proto->bucket(i + 1).min()) { |
| + bucket->clear_max(); |
| + } else if (bucket->max() == bucket->min() + 1) { |
| + bucket->clear_min(); |
| + } |
| + } |
| +} |
| + |
| +void HistogramManager::UpdateUmaProto() { |
| + histogram_snapshot_manager_->PrepareDeltas( |
| + base::Histogram::kNoFlags, base::Histogram::kUmaTargetedHistogramFlag); |
| + |
| + scoped_ptr<base::HistogramSamples> snapshot(new base::SampleMap()); |
| + RecordHistogramDelta(kCronetHistogramName, *snapshot.get()); |
|
Alexei Svitkine (slow)
2014/12/03 16:46:53
You shouldn't need to call this manually. Instead,
|
| +} |
| + |
| +static jbyteArray GetHistogramDeltas(JNIEnv* env, jobject jcaller) { |
| + // TODO(mef): make it singleton? |
| + scoped_ptr<HistogramManager> histogram_manager(new HistogramManager()); |
|
Alexei Svitkine (slow)
2014/12/03 16:46:53
This isn't right. We need to get the deltas only s
|
| + histogram_manager->UpdateUmaProto(); |
| + int data_size = histogram_manager->uma_proto().ByteSize(); |
| + std::vector<uint8> data(data_size); |
| + if (histogram_manager->uma_proto().SerializeToArray(&data[0], data_size)) { |
| + return base::android::ToJavaByteArray(env, &data[0], data_size).Release(); |
| + } |
| + |
| + return NULL; |
| +} |
| + |
| +} // namespace cronet |