Chromium Code Reviews| Index: components/metrics/histogram_encoder.cc |
| diff --git a/components/metrics/histogram_encoder.cc b/components/metrics/histogram_encoder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3d28cd5526b037ffff3dd5064518c07e1d66064b |
| --- /dev/null |
| +++ b/components/metrics/histogram_encoder.cc |
| @@ -0,0 +1,54 @@ |
| +// 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/metrics/histogram_encoder.h" |
| + |
| +#include <string> |
|
Alexei Svitkine (slow)
2014/12/10 15:19:31
Nit: Add a line after this.
ramant (doing other things)
2014/12/10 22:27:51
Done.
|
| +#include "base/basictypes.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/metrics/histogram.h" |
| +#include "base/metrics/histogram_samples.h" |
| +#include "components/metrics/metrics_log.h" |
|
Alexei Svitkine (slow)
2014/12/10 15:19:31
This include shouldn't be needed - I think you jus
ramant (doing other things)
2014/12/10 22:27:51
This code was calling MetricsLog::Hash. Added a TO
|
| + |
| +using base::SampleCountIterator; |
| + |
| +namespace metrics { |
| + |
| +// extern |
|
Alexei Svitkine (slow)
2014/12/10 15:19:31
Remove comment.
ramant (doing other things)
2014/12/10 22:27:51
Done.
|
| +void RecordHistogramDelta(ChromeUserMetricsExtension& uma_proto, |
|
Alexei Svitkine (slow)
2014/12/10 15:19:31
Nit: Pass by pointer, also modifiable params shoul
ramant (doing other things)
2014/12/10 22:27:51
Done.
|
| + const std::string& histogram_name, |
| + const base::HistogramSamples& snapshot) { |
| + DCHECK_NE(0, snapshot.TotalCount()); |
| + |
| + // We will ignore the MAX_INT/infinite value in the last element of range[]. |
| + |
| + HistogramEventProto* histogram_proto = uma_proto.add_histogram_event(); |
| + histogram_proto->set_name_hash(MetricsLog::Hash(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); |
| + 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) { |
| + 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(); |
| + } |
| + } |
| +} |
| + |
| +} // namespace metrics |