OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/histogram_base.h" | 5 #include "base/metrics/histogram_base.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 case HISTOGRAM: | 27 case HISTOGRAM: |
28 return "HISTOGRAM"; | 28 return "HISTOGRAM"; |
29 case LINEAR_HISTOGRAM: | 29 case LINEAR_HISTOGRAM: |
30 return "LINEAR_HISTOGRAM"; | 30 return "LINEAR_HISTOGRAM"; |
31 case BOOLEAN_HISTOGRAM: | 31 case BOOLEAN_HISTOGRAM: |
32 return "BOOLEAN_HISTOGRAM"; | 32 return "BOOLEAN_HISTOGRAM"; |
33 case CUSTOM_HISTOGRAM: | 33 case CUSTOM_HISTOGRAM: |
34 return "CUSTOM_HISTOGRAM"; | 34 return "CUSTOM_HISTOGRAM"; |
35 case SPARSE_HISTOGRAM: | 35 case SPARSE_HISTOGRAM: |
36 return "SPARSE_HISTOGRAM"; | 36 return "SPARSE_HISTOGRAM"; |
37 default: | |
38 NOTREACHED(); | |
39 } | 37 } |
| 38 NOTREACHED(); |
40 return "UNKNOWN"; | 39 return "UNKNOWN"; |
41 } | 40 } |
42 | 41 |
43 HistogramBase* DeserializeHistogramInfo(PickleIterator* iter) { | 42 HistogramBase* DeserializeHistogramInfo(PickleIterator* iter) { |
44 int type; | 43 int type; |
45 if (!iter->ReadInt(&type)) | 44 if (!iter->ReadInt(&type)) |
46 return NULL; | 45 return NULL; |
47 | 46 |
48 switch (type) { | 47 switch (type) { |
49 case HISTOGRAM: | 48 case HISTOGRAM: |
50 return Histogram::DeserializeInfoImpl(iter); | 49 return Histogram::DeserializeInfoImpl(iter); |
51 case LINEAR_HISTOGRAM: | 50 case LINEAR_HISTOGRAM: |
52 return LinearHistogram::DeserializeInfoImpl(iter); | 51 return LinearHistogram::DeserializeInfoImpl(iter); |
53 case BOOLEAN_HISTOGRAM: | 52 case BOOLEAN_HISTOGRAM: |
54 return BooleanHistogram::DeserializeInfoImpl(iter); | 53 return BooleanHistogram::DeserializeInfoImpl(iter); |
55 case CUSTOM_HISTOGRAM: | 54 case CUSTOM_HISTOGRAM: |
56 return CustomHistogram::DeserializeInfoImpl(iter); | 55 return CustomHistogram::DeserializeInfoImpl(iter); |
57 case SPARSE_HISTOGRAM: | 56 case SPARSE_HISTOGRAM: |
58 return SparseHistogram::DeserializeInfoImpl(iter); | 57 return SparseHistogram::DeserializeInfoImpl(iter); |
59 default: | 58 default: |
60 return NULL; | 59 return NULL; |
61 } | 60 } |
62 } | 61 } |
63 | 62 |
64 const HistogramBase::Sample HistogramBase::kSampleType_MAX = INT_MAX; | 63 const HistogramBase::Sample HistogramBase::kSampleType_MAX = INT_MAX; |
| 64 HistogramBase* HistogramBase::report_histogram_ = nullptr; |
65 | 65 |
66 HistogramBase::HistogramBase(const std::string& name) | 66 HistogramBase::HistogramBase(const std::string& name) |
67 : histogram_name_(name), | 67 : histogram_name_(name), |
68 flags_(kNoFlags) {} | 68 flags_(kNoFlags) {} |
69 | 69 |
70 HistogramBase::~HistogramBase() {} | 70 HistogramBase::~HistogramBase() {} |
71 | 71 |
72 void HistogramBase::CheckName(const StringPiece& name) const { | 72 void HistogramBase::CheckName(const StringPiece& name) const { |
73 DCHECK_EQ(histogram_name(), name); | 73 DCHECK_EQ(histogram_name(), name); |
74 } | 74 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 root.SetString("name", histogram_name()); | 115 root.SetString("name", histogram_name()); |
116 root.SetInteger("count", count); | 116 root.SetInteger("count", count); |
117 root.SetDouble("sum", static_cast<double>(sum)); | 117 root.SetDouble("sum", static_cast<double>(sum)); |
118 root.SetInteger("flags", flags()); | 118 root.SetInteger("flags", flags()); |
119 root.Set("params", std::move(parameters)); | 119 root.Set("params", std::move(parameters)); |
120 root.Set("buckets", std::move(buckets)); | 120 root.Set("buckets", std::move(buckets)); |
121 root.SetInteger("pid", GetCurrentProcId()); | 121 root.SetInteger("pid", GetCurrentProcId()); |
122 serializer.Serialize(root); | 122 serializer.Serialize(root); |
123 } | 123 } |
124 | 124 |
| 125 // static |
| 126 void HistogramBase::EnableActivityReportHistogram( |
| 127 const std::string& process_type) { |
| 128 DCHECK(!report_histogram_); |
| 129 size_t existing = StatisticsRecorder::GetHistogramCount(); |
| 130 if (existing != 0) { |
| 131 DLOG(WARNING) << existing |
| 132 << " histograms were created before reporting was enabled."; |
| 133 } |
| 134 |
| 135 std::string name = |
| 136 "UMA.Histograms.Activity" + |
| 137 (process_type.empty() ? process_type : "." + process_type); |
| 138 |
| 139 // Calling FactoryGet() here rather than using a histogram-macro works |
| 140 // around some problems with tests that could end up seeing the results |
| 141 // histogram when not expected due to a bad interaction between |
| 142 // HistogramTester and StatisticsRecorder. |
| 143 report_histogram_ = LinearHistogram::FactoryGet( |
| 144 name, 1, HISTOGRAM_REPORT_MAX, HISTOGRAM_REPORT_MAX + 1, |
| 145 kUmaTargetedHistogramFlag); |
| 146 report_histogram_->Add(HISTOGRAM_REPORT_CREATED); |
| 147 } |
| 148 |
125 void HistogramBase::FindAndRunCallback(HistogramBase::Sample sample) const { | 149 void HistogramBase::FindAndRunCallback(HistogramBase::Sample sample) const { |
126 if ((flags() & kCallbackExists) == 0) | 150 if ((flags() & kCallbackExists) == 0) |
127 return; | 151 return; |
128 | 152 |
129 StatisticsRecorder::OnSampleCallback cb = | 153 StatisticsRecorder::OnSampleCallback cb = |
130 StatisticsRecorder::FindCallback(histogram_name()); | 154 StatisticsRecorder::FindCallback(histogram_name()); |
131 if (!cb.is_null()) | 155 if (!cb.is_null()) |
132 cb.Run(sample); | 156 cb.Run(sample); |
133 } | 157 } |
134 | 158 |
(...skipping 21 matching lines...) Expand all Loading... |
156 StringAppendF(&result, "%d", sample); | 180 StringAppendF(&result, "%d", sample); |
157 return result; | 181 return result; |
158 } | 182 } |
159 | 183 |
160 void HistogramBase::WriteAsciiBucketValue(Count current, | 184 void HistogramBase::WriteAsciiBucketValue(Count current, |
161 double scaled_sum, | 185 double scaled_sum, |
162 std::string* output) const { | 186 std::string* output) const { |
163 StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum); | 187 StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum); |
164 } | 188 } |
165 | 189 |
| 190 // static |
| 191 void HistogramBase::ReportHistogramActivity(const HistogramBase& histogram, |
| 192 ReportActivity activity) { |
| 193 if (!report_histogram_) |
| 194 return; |
| 195 |
| 196 const int32_t flags = histogram.flags_; |
| 197 HistogramReport report_type = HISTOGRAM_REPORT_MAX; |
| 198 switch (activity) { |
| 199 case HISTOGRAM_CREATED: |
| 200 report_histogram_->Add(HISTOGRAM_REPORT_HISTOGRAM_CREATED); |
| 201 switch (histogram.GetHistogramType()) { |
| 202 case HISTOGRAM: |
| 203 report_type = HISTOGRAM_REPORT_TYPE_LOGARITHMIC; |
| 204 break; |
| 205 case LINEAR_HISTOGRAM: |
| 206 report_type = HISTOGRAM_REPORT_TYPE_LINEAR; |
| 207 break; |
| 208 case BOOLEAN_HISTOGRAM: |
| 209 report_type = HISTOGRAM_REPORT_TYPE_BOOLEAN; |
| 210 break; |
| 211 case CUSTOM_HISTOGRAM: |
| 212 report_type = HISTOGRAM_REPORT_TYPE_CUSTOM; |
| 213 break; |
| 214 case SPARSE_HISTOGRAM: |
| 215 report_type = HISTOGRAM_REPORT_TYPE_SPARSE; |
| 216 break; |
| 217 } |
| 218 report_histogram_->Add(report_type); |
| 219 if (flags & kIsPersistent) |
| 220 report_histogram_->Add(HISTOGRAM_REPORT_FLAG_PERSISTENT); |
| 221 if ((flags & kUmaStabilityHistogramFlag) == kUmaStabilityHistogramFlag) |
| 222 report_histogram_->Add(HISTOGRAM_REPORT_FLAG_UMA_STABILITY); |
| 223 else if (flags & kUmaTargetedHistogramFlag) |
| 224 report_histogram_->Add(HISTOGRAM_REPORT_FLAG_UMA_TARGETED); |
| 225 break; |
| 226 |
| 227 case HISTOGRAM_LOOKUP: |
| 228 report_histogram_->Add(HISTOGRAM_REPORT_HISTOGRAM_LOOKUP); |
| 229 break; |
| 230 } |
| 231 } |
| 232 |
166 } // namespace base | 233 } // namespace base |
OLD | NEW |