1 // Copyright (c) 2012 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 "base/metrics/histogram_base.h"
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/histogram.h"
13 #include "base/metrics/histogram_samples.h"
14 #include "base/metrics/sparse_histogram.h"
15 #include "base/metrics/statistics_recorder.h"
16 #include "base/pickle.h"
17 #include "base/process/process_handle.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/values.h"
23 std::string
HistogramTypeToString(HistogramType type
) {
27 case LINEAR_HISTOGRAM
:
28 return "LINEAR_HISTOGRAM";
29 case BOOLEAN_HISTOGRAM
:
30 return "BOOLEAN_HISTOGRAM";
31 case CUSTOM_HISTOGRAM
:
32 return "CUSTOM_HISTOGRAM";
33 case SPARSE_HISTOGRAM
:
34 return "SPARSE_HISTOGRAM";
41 HistogramBase
* DeserializeHistogramInfo(PickleIterator
* iter
) {
43 if (!iter
->ReadInt(&type
))
48 return Histogram::DeserializeInfoImpl(iter
);
49 case LINEAR_HISTOGRAM
:
50 return LinearHistogram::DeserializeInfoImpl(iter
);
51 case BOOLEAN_HISTOGRAM
:
52 return BooleanHistogram::DeserializeInfoImpl(iter
);
53 case CUSTOM_HISTOGRAM
:
54 return CustomHistogram::DeserializeInfoImpl(iter
);
55 case SPARSE_HISTOGRAM
:
56 return SparseHistogram::DeserializeInfoImpl(iter
);
62 const HistogramBase::Sample
HistogramBase::kSampleType_MAX
= INT_MAX
;
64 HistogramBase::HistogramBase(const std::string
& name
)
65 : histogram_name_(name
),
68 HistogramBase::~HistogramBase() {}
70 void HistogramBase::CheckName(const StringPiece
& name
) const {
71 DCHECK_EQ(histogram_name(), name
);
74 void HistogramBase::SetFlags(int32 flags
) {
78 void HistogramBase::ClearFlags(int32 flags
) {
82 void HistogramBase::AddTime(const TimeDelta
& time
) {
83 Add(static_cast<Sample
>(time
.InMilliseconds()));
86 void HistogramBase::AddBoolean(bool value
) {
90 bool HistogramBase::SerializeInfo(Pickle
* pickle
) const {
91 if (!pickle
->WriteInt(GetHistogramType()))
93 return SerializeInfoImpl(pickle
);
96 int HistogramBase::FindCorruption(const HistogramSamples
& samples
) const {
97 // Not supported by default.
98 return NO_INCONSISTENCIES
;
101 void HistogramBase::WriteJSON(std::string
* output
) const {
104 scoped_ptr
<ListValue
> buckets(new ListValue());
105 GetCountAndBucketData(&count
, &sum
, buckets
.get());
106 scoped_ptr
<DictionaryValue
> parameters(new DictionaryValue());
107 GetParameters(parameters
.get());
109 JSONStringValueSerializer
serializer(output
);
110 DictionaryValue root
;
111 root
.SetString("name", histogram_name());
112 root
.SetInteger("count", count
);
113 root
.SetDouble("sum", static_cast<double>(sum
));
114 root
.SetInteger("flags", flags());
115 root
.Set("params", parameters
.Pass());
116 root
.Set("buckets", buckets
.Pass());
117 root
.SetInteger("pid", GetCurrentProcId());
118 serializer
.Serialize(root
);
121 void HistogramBase::FindAndRunCallback(HistogramBase::Sample sample
) const {
122 if ((flags_
& kCallbackExists
) == 0)
125 StatisticsRecorder::OnSampleCallback cb
=
126 StatisticsRecorder::FindCallback(histogram_name());
131 void HistogramBase::WriteAsciiBucketGraph(double current_size
,
133 std::string
* output
) const {
134 const int k_line_length
= 72; // Maximal horizontal width of graph.
135 int x_count
= static_cast<int>(k_line_length
* (current_size
/ max_size
)
137 int x_remainder
= k_line_length
- x_count
;
139 while (0 < x_count
--)
142 while (0 < x_remainder
--)
146 const std::string
HistogramBase::GetSimpleAsciiBucketRange(
147 Sample sample
) const {
149 if (kHexRangePrintingFlag
& flags())
150 StringAppendF(&result
, "%#x", sample
);
152 StringAppendF(&result
, "%d", sample
);
156 void HistogramBase::WriteAsciiBucketValue(Count current
,
158 std::string
* output
) const {
159 StringAppendF(output
, " (%d = %3.1f%%)", current
, current
/scaled_sum
);