Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: base/metrics/histogram_base.cc

Issue 2973863002: Remove std::string histogram name from HistogramBase object.
Patch Set: never rely on outside string permanence Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/metrics/histogram_base.h ('k') | base/metrics/histogram_base_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <memory> 9 #include <memory>
10 #include <set>
10 #include <utility> 11 #include <utility>
11 12
12 #include "base/json/json_string_value_serializer.h" 13 #include "base/json/json_string_value_serializer.h"
14 #include "base/lazy_instance.h"
13 #include "base/logging.h" 15 #include "base/logging.h"
14 #include "base/metrics/histogram.h" 16 #include "base/metrics/histogram.h"
17 #include "base/metrics/histogram_macros.h"
15 #include "base/metrics/histogram_samples.h" 18 #include "base/metrics/histogram_samples.h"
16 #include "base/metrics/sparse_histogram.h" 19 #include "base/metrics/sparse_histogram.h"
17 #include "base/metrics/statistics_recorder.h" 20 #include "base/metrics/statistics_recorder.h"
18 #include "base/pickle.h" 21 #include "base/pickle.h"
19 #include "base/process/process_handle.h" 22 #include "base/process/process_handle.h"
20 #include "base/strings/stringprintf.h" 23 #include "base/strings/stringprintf.h"
24 #include "base/synchronization/lock.h"
21 #include "base/values.h" 25 #include "base/values.h"
22 26
23 namespace base { 27 namespace base {
24 28
25 std::string HistogramTypeToString(HistogramType type) { 29 std::string HistogramTypeToString(HistogramType type) {
26 switch (type) { 30 switch (type) {
27 case HISTOGRAM: 31 case HISTOGRAM:
28 return "HISTOGRAM"; 32 return "HISTOGRAM";
29 case LINEAR_HISTOGRAM: 33 case LINEAR_HISTOGRAM:
30 return "LINEAR_HISTOGRAM"; 34 return "LINEAR_HISTOGRAM";
(...skipping 25 matching lines...) Expand all
56 case SPARSE_HISTOGRAM: 60 case SPARSE_HISTOGRAM:
57 return SparseHistogram::DeserializeInfoImpl(iter); 61 return SparseHistogram::DeserializeInfoImpl(iter);
58 default: 62 default:
59 return NULL; 63 return NULL;
60 } 64 }
61 } 65 }
62 66
63 const HistogramBase::Sample HistogramBase::kSampleType_MAX = INT_MAX; 67 const HistogramBase::Sample HistogramBase::kSampleType_MAX = INT_MAX;
64 HistogramBase* HistogramBase::report_histogram_ = nullptr; 68 HistogramBase* HistogramBase::report_histogram_ = nullptr;
65 69
66 HistogramBase::HistogramBase(const std::string& name) 70 HistogramBase::HistogramBase(const char* name)
67 : histogram_name_(name), 71 : histogram_name_(name), flags_(kNoFlags) {}
68 flags_(kNoFlags) {}
69 72
70 HistogramBase::~HistogramBase() {} 73 HistogramBase::~HistogramBase() {}
71 74
72 void HistogramBase::CheckName(const StringPiece& name) const { 75 void HistogramBase::CheckName(const StringPiece& name) const {
73 DCHECK_EQ(histogram_name(), name); 76 DCHECK_EQ(StringPiece(histogram_name()), name);
74 } 77 }
75 78
76 void HistogramBase::SetFlags(int32_t flags) { 79 void HistogramBase::SetFlags(int32_t flags) {
77 HistogramBase::Count old_flags = subtle::NoBarrier_Load(&flags_); 80 HistogramBase::Count old_flags = subtle::NoBarrier_Load(&flags_);
78 subtle::NoBarrier_Store(&flags_, old_flags | flags); 81 subtle::NoBarrier_Store(&flags_, old_flags | flags);
79 } 82 }
80 83
81 void HistogramBase::ClearFlags(int32_t flags) { 84 void HistogramBase::ClearFlags(int32_t flags) {
82 HistogramBase::Count old_flags = subtle::NoBarrier_Load(&flags_); 85 HistogramBase::Count old_flags = subtle::NoBarrier_Load(&flags_);
83 subtle::NoBarrier_Store(&flags_, old_flags & ~flags); 86 subtle::NoBarrier_Store(&flags_, old_flags & ~flags);
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 return StringPrintf("%d", sample); 181 return StringPrintf("%d", sample);
179 } 182 }
180 183
181 void HistogramBase::WriteAsciiBucketValue(Count current, 184 void HistogramBase::WriteAsciiBucketValue(Count current,
182 double scaled_sum, 185 double scaled_sum,
183 std::string* output) const { 186 std::string* output) const {
184 StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum); 187 StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum);
185 } 188 }
186 189
187 // static 190 // static
191 char const* HistogramBase::GetPermanentName(const std::string& name) {
Alexei Svitkine (slow) 2017/07/17 21:34:43 Can we avoid having an extra temporary string cons
bcwhite 2017/07/18 01:07:16 We might also be able to make the name parameter b
192 // A set of histogram names that provides the "permanent" lifetime required
193 // by histogram objects for those strings that are not already code constants
194 // or held in persistent memory.
195 static LazyInstance<std::set<std::string>>::Leaky permanent_names;
196 static LazyInstance<Lock>::Leaky permanent_names_lock;
197
198 AutoLock lock(permanent_names_lock.Get());
199 auto result = permanent_names.Get().insert(name);
200 return result.first->c_str();
201 }
202
203 // static
188 void HistogramBase::ReportHistogramActivity(const HistogramBase& histogram, 204 void HistogramBase::ReportHistogramActivity(const HistogramBase& histogram,
189 ReportActivity activity) { 205 ReportActivity activity) {
190 if (!report_histogram_) 206 if (!report_histogram_)
191 return; 207 return;
192 208
193 const int32_t flags = histogram.flags_; 209 const int32_t flags = histogram.flags_;
194 HistogramReport report_type = HISTOGRAM_REPORT_MAX; 210 HistogramReport report_type = HISTOGRAM_REPORT_MAX;
195 switch (activity) { 211 switch (activity) {
196 case HISTOGRAM_CREATED: 212 case HISTOGRAM_CREATED:
197 report_histogram_->Add(HISTOGRAM_REPORT_HISTOGRAM_CREATED); 213 report_histogram_->Add(HISTOGRAM_REPORT_HISTOGRAM_CREATED);
(...skipping 23 matching lines...) Expand all
221 report_histogram_->Add(HISTOGRAM_REPORT_FLAG_UMA_TARGETED); 237 report_histogram_->Add(HISTOGRAM_REPORT_FLAG_UMA_TARGETED);
222 break; 238 break;
223 239
224 case HISTOGRAM_LOOKUP: 240 case HISTOGRAM_LOOKUP:
225 report_histogram_->Add(HISTOGRAM_REPORT_HISTOGRAM_LOOKUP); 241 report_histogram_->Add(HISTOGRAM_REPORT_HISTOGRAM_LOOKUP);
226 break; 242 break;
227 } 243 }
228 } 244 }
229 245
230 } // namespace base 246 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/histogram_base.h ('k') | base/metrics/histogram_base_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698