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/sparse_histogram.h" | 5 #include "base/metrics/sparse_histogram.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/metrics/metrics_hashes.h" | |
9 #include "base/metrics/sample_map.h" | 10 #include "base/metrics/sample_map.h" |
10 #include "base/metrics/statistics_recorder.h" | 11 #include "base/metrics/statistics_recorder.h" |
11 #include "base/pickle.h" | 12 #include "base/pickle.h" |
12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 | 17 |
17 typedef HistogramBase::Count Count; | 18 typedef HistogramBase::Count Count; |
18 typedef HistogramBase::Sample Sample; | 19 typedef HistogramBase::Sample Sample; |
19 | 20 |
20 // static | 21 // static |
21 HistogramBase* SparseHistogram::FactoryGet(const std::string& name, | 22 HistogramBase* SparseHistogram::FactoryGet(const std::string& name, |
22 int32 flags) { | 23 int32 flags) { |
23 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | 24 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
24 | 25 |
25 if (!histogram) { | 26 if (!histogram) { |
26 // To avoid racy destruction at shutdown, the following will be leaked. | 27 // To avoid racy destruction at shutdown, the following will be leaked. |
27 HistogramBase* tentative_histogram = new SparseHistogram(name); | 28 HistogramBase* tentative_histogram = new SparseHistogram(name); |
28 tentative_histogram->SetFlags(flags); | 29 tentative_histogram->SetFlags(flags); |
29 histogram = | 30 histogram = |
30 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); | 31 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
31 } | 32 } |
32 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType()); | 33 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType()); |
33 return histogram; | 34 return histogram; |
34 } | 35 } |
35 | 36 |
36 SparseHistogram::~SparseHistogram() {} | 37 SparseHistogram::~SparseHistogram() {} |
37 | 38 |
39 uint64_t SparseHistogram::name_hash() const { | |
40 return samples_.id(); | |
41 } | |
42 | |
38 HistogramType SparseHistogram::GetHistogramType() const { | 43 HistogramType SparseHistogram::GetHistogramType() const { |
39 return SPARSE_HISTOGRAM; | 44 return SPARSE_HISTOGRAM; |
40 } | 45 } |
41 | 46 |
42 bool SparseHistogram::HasConstructionArguments( | 47 bool SparseHistogram::HasConstructionArguments( |
43 Sample expected_minimum, | 48 Sample expected_minimum, |
44 Sample expected_maximum, | 49 Sample expected_maximum, |
45 size_t expected_bucket_count) const { | 50 size_t expected_bucket_count) const { |
46 // SparseHistogram never has min/max/bucket_count limit. | 51 // SparseHistogram never has min/max/bucket_count limit. |
47 return false; | 52 return false; |
(...skipping 10 matching lines...) Expand all Loading... | |
58 } | 63 } |
59 { | 64 { |
60 base::AutoLock auto_lock(lock_); | 65 base::AutoLock auto_lock(lock_); |
61 samples_.Accumulate(value, count); | 66 samples_.Accumulate(value, count); |
62 } | 67 } |
63 | 68 |
64 FindAndRunCallback(value); | 69 FindAndRunCallback(value); |
65 } | 70 } |
66 | 71 |
67 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { | 72 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { |
68 scoped_ptr<SampleMap> snapshot(new SampleMap()); | 73 scoped_ptr<SampleMap> snapshot( |
74 new SampleMap(HashMetricName(histogram_name()))); | |
Alexei Svitkine (slow)
2015/12/09 23:03:40
Pass in name_hash() instead.
bcwhite
2015/12/10 14:51:33
Done.
| |
69 | 75 |
70 base::AutoLock auto_lock(lock_); | 76 base::AutoLock auto_lock(lock_); |
71 snapshot->Add(samples_); | 77 snapshot->Add(samples_); |
72 return std::move(snapshot); | 78 return std::move(snapshot); |
73 } | 79 } |
74 | 80 |
75 void SparseHistogram::AddSamples(const HistogramSamples& samples) { | 81 void SparseHistogram::AddSamples(const HistogramSamples& samples) { |
76 base::AutoLock auto_lock(lock_); | 82 base::AutoLock auto_lock(lock_); |
77 samples_.Add(samples); | 83 samples_.Add(samples); |
78 } | 84 } |
(...skipping 11 matching lines...) Expand all Loading... | |
90 | 96 |
91 void SparseHistogram::WriteAscii(std::string* output) const { | 97 void SparseHistogram::WriteAscii(std::string* output) const { |
92 WriteAsciiImpl(true, "\n", output); | 98 WriteAsciiImpl(true, "\n", output); |
93 } | 99 } |
94 | 100 |
95 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { | 101 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { |
96 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); | 102 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); |
97 } | 103 } |
98 | 104 |
99 SparseHistogram::SparseHistogram(const std::string& name) | 105 SparseHistogram::SparseHistogram(const std::string& name) |
100 : HistogramBase(name) {} | 106 : HistogramBase(name), |
107 samples_(HashMetricName(name)) {} | |
101 | 108 |
102 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { | 109 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
103 std::string histogram_name; | 110 std::string histogram_name; |
104 int flags; | 111 int flags; |
105 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) { | 112 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) { |
106 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; | 113 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; |
107 return NULL; | 114 return NULL; |
108 } | 115 } |
109 | 116 |
110 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag); | 117 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 std::string* output) const { | 187 std::string* output) const { |
181 StringAppendF(output, | 188 StringAppendF(output, |
182 "Histogram: %s recorded %d samples", | 189 "Histogram: %s recorded %d samples", |
183 histogram_name().c_str(), | 190 histogram_name().c_str(), |
184 total_count); | 191 total_count); |
185 if (flags() & ~kHexRangePrintingFlag) | 192 if (flags() & ~kHexRangePrintingFlag) |
186 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); | 193 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); |
187 } | 194 } |
188 | 195 |
189 } // namespace base | 196 } // namespace base |
OLD | NEW |