Update V8 to version 4.7.21.
[chromium-blink-merge.git] / base / metrics / histogram_base.cc
blob6b3f69c2c047a63198d6cf69e94ec6e29336e8b7
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"
7 #include <climits>
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"
21 namespace base {
23 std::string HistogramTypeToString(HistogramType type) {
24 switch (type) {
25 case HISTOGRAM:
26 return "HISTOGRAM";
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";
35 default:
36 NOTREACHED();
38 return "UNKNOWN";
41 HistogramBase* DeserializeHistogramInfo(PickleIterator* iter) {
42 int type;
43 if (!iter->ReadInt(&type))
44 return NULL;
46 switch (type) {
47 case HISTOGRAM:
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);
57 default:
58 return NULL;
62 const HistogramBase::Sample HistogramBase::kSampleType_MAX = INT_MAX;
64 HistogramBase::HistogramBase(const std::string& name)
65 : histogram_name_(name),
66 flags_(kNoFlags) {}
68 HistogramBase::~HistogramBase() {}
70 void HistogramBase::CheckName(const StringPiece& name) const {
71 DCHECK_EQ(histogram_name(), name);
74 void HistogramBase::SetFlags(int32 flags) {
75 flags_ |= flags;
78 void HistogramBase::ClearFlags(int32 flags) {
79 flags_ &= ~flags;
82 void HistogramBase::AddTime(const TimeDelta& time) {
83 Add(static_cast<Sample>(time.InMilliseconds()));
86 void HistogramBase::AddBoolean(bool value) {
87 Add(value ? 1 : 0);
90 bool HistogramBase::SerializeInfo(Pickle* pickle) const {
91 if (!pickle->WriteInt(GetHistogramType()))
92 return false;
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 {
102 Count count;
103 int64 sum;
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)
123 return;
125 StatisticsRecorder::OnSampleCallback cb =
126 StatisticsRecorder::FindCallback(histogram_name());
127 if (!cb.is_null())
128 cb.Run(sample);
131 void HistogramBase::WriteAsciiBucketGraph(double current_size,
132 double max_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)
136 + 0.5);
137 int x_remainder = k_line_length - x_count;
139 while (0 < x_count--)
140 output->append("-");
141 output->append("O");
142 while (0 < x_remainder--)
143 output->append(" ");
146 const std::string HistogramBase::GetSimpleAsciiBucketRange(
147 Sample sample) const {
148 std::string result;
149 if (kHexRangePrintingFlag & flags())
150 StringAppendF(&result, "%#x", sample);
151 else
152 StringAppendF(&result, "%d", sample);
153 return result;
156 void HistogramBase::WriteAsciiBucketValue(Count current,
157 double scaled_sum,
158 std::string* output) const {
159 StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum);
162 } // namespace base