Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "components/cronet/android/histogram_manager.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/android/jni_array.h" | |
| 10 #include "base/metrics/histogram_delta_serialization.h" | |
| 11 #include "base/metrics/histogram_samples.h" | |
| 12 #include "base/metrics/histogram_snapshot_manager.h" | |
| 13 #include "base/metrics/sample_map.h" | |
| 14 #include "base/metrics/statistics_recorder.h" | |
| 15 #include "components/metrics/metrics_hashes.h" | |
| 16 #include "components/metrics/metrics_log.h" | |
| 17 #include "components/metrics/proto/histogram_event.pb.h" | |
| 18 | |
| 19 #include "jni/HistogramManager_jni.h" | |
| 20 | |
| 21 using base::SampleCountIterator; | |
| 22 | |
| 23 namespace cronet { | |
| 24 | |
| 25 static const char kCronetHistogramName[] = "cronet"; | |
| 26 | |
| 27 // Explicitly register static JNI functions. | |
| 28 bool HistogramManagerRegisterJni(JNIEnv* env) { | |
| 29 return RegisterNativesImpl(env); | |
| 30 } | |
| 31 | |
| 32 HistogramManager::HistogramManager() { | |
| 33 histogram_flattener_.reset( | |
| 34 new base::HistogramDeltaSerialization(kCronetHistogramName)); | |
| 35 histogram_snapshot_manager_.reset( | |
| 36 new base::HistogramSnapshotManager(histogram_flattener_.get())); | |
|
Alexei Svitkine (slow)
2014/12/03 16:46:53
Instead of creating a flattener, HistogramManager
| |
| 37 } | |
| 38 | |
| 39 HistogramManager::~HistogramManager() { | |
| 40 } | |
| 41 | |
| 42 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
| |
| 43 const base::HistogramSamples& snapshot) { | |
| 44 // TODO(mef): figure out where should snapshot come from so it isn't empty. | |
| 45 DCHECK_NE(0, snapshot.TotalCount()); | |
| 46 // We will ignore the MAX_INT/infinite value in the last element of range[]. | |
| 47 | |
| 48 metrics::HistogramEventProto* histogram_proto = | |
| 49 uma_proto_.add_histogram_event(); | |
| 50 histogram_proto->set_name_hash(metrics::HashMetricName(histogram_name)); | |
| 51 histogram_proto->set_sum(snapshot.sum()); | |
| 52 | |
| 53 for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); !it->Done(); | |
| 54 it->Next()) { | |
| 55 base::Histogram::Sample min; | |
| 56 base::Histogram::Sample max; | |
| 57 base::Histogram::Count count; | |
| 58 it->Get(&min, &max, &count); | |
| 59 metrics::HistogramEventProto::Bucket* bucket = | |
| 60 histogram_proto->add_bucket(); | |
| 61 bucket->set_min(min); | |
| 62 bucket->set_max(max); | |
| 63 bucket->set_count(count); | |
| 64 } | |
| 65 | |
| 66 // Omit fields to save space (see rules in histogram_event.proto comments). | |
| 67 for (int i = 0; i < histogram_proto->bucket_size(); ++i) { | |
| 68 metrics::HistogramEventProto::Bucket* bucket = | |
| 69 histogram_proto->mutable_bucket(i); | |
| 70 if (i + 1 < histogram_proto->bucket_size() && | |
| 71 bucket->max() == histogram_proto->bucket(i + 1).min()) { | |
| 72 bucket->clear_max(); | |
| 73 } else if (bucket->max() == bucket->min() + 1) { | |
| 74 bucket->clear_min(); | |
| 75 } | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 void HistogramManager::UpdateUmaProto() { | |
| 80 histogram_snapshot_manager_->PrepareDeltas( | |
| 81 base::Histogram::kNoFlags, base::Histogram::kUmaTargetedHistogramFlag); | |
| 82 | |
| 83 scoped_ptr<base::HistogramSamples> snapshot(new base::SampleMap()); | |
| 84 RecordHistogramDelta(kCronetHistogramName, *snapshot.get()); | |
|
Alexei Svitkine (slow)
2014/12/03 16:46:53
You shouldn't need to call this manually. Instead,
| |
| 85 } | |
| 86 | |
| 87 static jbyteArray GetHistogramDeltas(JNIEnv* env, jobject jcaller) { | |
| 88 // TODO(mef): make it singleton? | |
| 89 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
| |
| 90 histogram_manager->UpdateUmaProto(); | |
| 91 int data_size = histogram_manager->uma_proto().ByteSize(); | |
| 92 std::vector<uint8> data(data_size); | |
| 93 if (histogram_manager->uma_proto().SerializeToArray(&data[0], data_size)) { | |
| 94 return base::android::ToJavaByteArray(env, &data[0], data_size).Release(); | |
| 95 } | |
| 96 | |
| 97 return NULL; | |
| 98 } | |
| 99 | |
| 100 } // namespace cronet | |
| OLD | NEW |