Roll src/third_party/WebKit c63b89c:29324ab (svn 202546:202547)
[chromium-blink-merge.git] / components / metrics / histogram_encoder.cc
blobefca70bb1c1d157eb9def769e18c7ae898f98872
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.
5 #include "components/metrics/histogram_encoder.h"
7 #include <string>
9 #include "base/basictypes.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/metrics/histogram.h"
12 #include "base/metrics/histogram_samples.h"
13 #include "components/metrics/metrics_hashes.h"
15 using base::SampleCountIterator;
17 namespace metrics {
19 void EncodeHistogramDelta(const std::string& histogram_name,
20 const base::HistogramSamples& snapshot,
21 ChromeUserMetricsExtension* uma_proto) {
22 DCHECK_NE(0, snapshot.TotalCount());
23 DCHECK(uma_proto);
25 // We will ignore the MAX_INT/infinite value in the last element of range[].
27 HistogramEventProto* histogram_proto = uma_proto->add_histogram_event();
28 histogram_proto->set_name_hash(HashMetricName(histogram_name));
29 histogram_proto->set_sum(snapshot.sum());
31 for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); !it->Done();
32 it->Next()) {
33 base::Histogram::Sample min;
34 base::Histogram::Sample max;
35 base::Histogram::Count count;
36 it->Get(&min, &max, &count);
37 HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket();
38 bucket->set_min(min);
39 bucket->set_max(max);
40 bucket->set_count(count);
43 // Omit fields to save space (see rules in histogram_event.proto comments).
44 for (int i = 0; i < histogram_proto->bucket_size(); ++i) {
45 HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i);
46 if (i + 1 < histogram_proto->bucket_size() &&
47 bucket->max() == histogram_proto->bucket(i + 1).min()) {
48 bucket->clear_max();
49 } else if (bucket->max() == bucket->min() + 1) {
50 bucket->clear_min();
55 } // namespace metrics