Chromium Code Reviews| 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/histogram_persistence.h" | |
| 9 #include "base/metrics/metrics_hashes.h" | 10 #include "base/metrics/metrics_hashes.h" |
| 11 #include "base/metrics/persistent_sample_map.h" | |
| 10 #include "base/metrics/sample_map.h" | 12 #include "base/metrics/sample_map.h" |
| 11 #include "base/metrics/statistics_recorder.h" | 13 #include "base/metrics/statistics_recorder.h" |
| 12 #include "base/pickle.h" | 14 #include "base/pickle.h" |
| 13 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 14 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 15 | 17 |
| 16 namespace base { | 18 namespace base { |
| 17 | 19 |
| 18 typedef HistogramBase::Count Count; | 20 typedef HistogramBase::Count Count; |
| 19 typedef HistogramBase::Sample Sample; | 21 typedef HistogramBase::Sample Sample; |
| 20 | 22 |
| 21 // static | 23 // static |
| 22 HistogramBase* SparseHistogram::FactoryGet(const std::string& name, | 24 HistogramBase* SparseHistogram::FactoryGet(const std::string& name, |
| 23 int32_t flags) { | 25 int32_t flags) { |
| 26 // Import histograms from known persistent storage. Histograms could have | |
| 27 // been added by other processes and they must be fetched and recognized | |
| 28 // locally in order to be found by FindHistograms() below. If the persistent | |
| 29 // memory segment is not shared between processes, this call does nothing. | |
| 30 ImportPersistentHistograms(); | |
| 31 | |
| 24 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | 32 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| 33 if (!histogram) { | |
| 34 // Try to create the histogram using a "persistent" allocator. As of | |
| 35 // 2016-02-25, the availability of such is controlled by a base::Feature | |
| 36 // that is off by default. If the allocator doesn't exist or if | |
| 37 // allocating from it fails, code below will allocate the histogram from | |
| 38 // the process heap. | |
| 39 PersistentMemoryAllocator::Reference histogram_ref = 0; | |
| 40 HistogramBase* tentative_histogram = nullptr; | |
| 41 PersistentMemoryAllocator* allocator = | |
| 42 GetPersistentHistogramMemoryAllocator(); | |
| 43 if (allocator) { | |
| 44 flags |= HistogramBase::kIsPersistent; | |
|
Alexei Svitkine (slow)
2016/03/09 20:35:19
Can we move this to inside AllocatePersistentHisto
bcwhite
2016/03/09 22:57:33
It would get overwritten below on line 65... but
| |
| 45 tentative_histogram = AllocatePersistentHistogram( | |
| 46 allocator, | |
| 47 SPARSE_HISTOGRAM, | |
| 48 name, | |
| 49 0, | |
| 50 0, | |
| 51 nullptr, | |
| 52 flags, | |
| 53 &histogram_ref); | |
| 54 } | |
| 25 | 55 |
| 26 if (!histogram) { | 56 // Handle the case where no persistent allocator is present or the |
| 27 // To avoid racy destruction at shutdown, the following will be leaked. | 57 // persistent allocation fails (perhaps because it is full). |
| 28 HistogramBase* tentative_histogram = new SparseHistogram(name); | 58 if (!tentative_histogram) { |
| 59 DCHECK(!histogram_ref); // Should never have been set. | |
| 60 DCHECK(!allocator); // Shouldn't have failed. | |
| 61 flags &= ~HistogramBase::kIsPersistent; | |
| 62 tentative_histogram = new SparseHistogram(name); | |
| 63 } | |
| 64 | |
| 29 tentative_histogram->SetFlags(flags); | 65 tentative_histogram->SetFlags(flags); |
| 30 histogram = | 66 histogram = |
| 31 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); | 67 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
| 68 | |
| 69 // Persistent histograms need some follow-up processing. | |
| 70 if (histogram_ref) { | |
| 71 FinalizePersistentHistogram(histogram_ref, | |
| 72 histogram == tentative_histogram); | |
| 73 } | |
| 32 } | 74 } |
| 75 | |
| 33 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType()); | 76 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType()); |
| 34 return histogram; | 77 return histogram; |
| 35 } | 78 } |
| 36 | 79 |
| 80 // static | |
| 81 HistogramBase* SparseHistogram::PersistentGet( | |
| 82 PersistentMemoryAllocator* allocator, | |
| 83 const std::string& name, | |
| 84 HistogramSamples::Metadata* meta, | |
| 85 HistogramSamples::Metadata* logged_meta) { | |
| 86 return new SparseHistogram(allocator, name, meta, logged_meta); | |
| 87 } | |
| 88 | |
| 37 SparseHistogram::~SparseHistogram() {} | 89 SparseHistogram::~SparseHistogram() {} |
| 38 | 90 |
| 39 uint64_t SparseHistogram::name_hash() const { | 91 uint64_t SparseHistogram::name_hash() const { |
| 40 return samples_.id(); | 92 return samples_->id(); |
| 41 } | 93 } |
| 42 | 94 |
| 43 HistogramType SparseHistogram::GetHistogramType() const { | 95 HistogramType SparseHistogram::GetHistogramType() const { |
| 44 return SPARSE_HISTOGRAM; | 96 return SPARSE_HISTOGRAM; |
| 45 } | 97 } |
| 46 | 98 |
| 47 bool SparseHistogram::HasConstructionArguments( | 99 bool SparseHistogram::HasConstructionArguments( |
| 48 Sample expected_minimum, | 100 Sample expected_minimum, |
| 49 Sample expected_maximum, | 101 Sample expected_maximum, |
| 50 uint32_t expected_bucket_count) const { | 102 uint32_t expected_bucket_count) const { |
| 51 // SparseHistogram never has min/max/bucket_count limit. | 103 // SparseHistogram never has min/max/bucket_count limit. |
| 52 return false; | 104 return false; |
| 53 } | 105 } |
| 54 | 106 |
| 55 void SparseHistogram::Add(Sample value) { | 107 void SparseHistogram::Add(Sample value) { |
| 56 AddCount(value, 1); | 108 AddCount(value, 1); |
| 57 } | 109 } |
| 58 | 110 |
| 59 void SparseHistogram::AddCount(Sample value, int count) { | 111 void SparseHistogram::AddCount(Sample value, int count) { |
| 60 if (count <= 0) { | 112 if (count <= 0) { |
| 61 NOTREACHED(); | 113 NOTREACHED(); |
| 62 return; | 114 return; |
| 63 } | 115 } |
| 64 { | 116 { |
| 65 base::AutoLock auto_lock(lock_); | 117 base::AutoLock auto_lock(lock_); |
| 66 samples_.Accumulate(value, count); | 118 samples_->Accumulate(value, count); |
| 67 } | 119 } |
| 68 | 120 |
| 69 FindAndRunCallback(value); | 121 FindAndRunCallback(value); |
| 70 } | 122 } |
| 71 | 123 |
| 72 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { | 124 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { |
| 73 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash())); | 125 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash())); |
| 74 | 126 |
| 75 base::AutoLock auto_lock(lock_); | 127 base::AutoLock auto_lock(lock_); |
| 76 snapshot->Add(samples_); | 128 snapshot->Add(*samples_); |
| 77 return std::move(snapshot); | 129 return std::move(snapshot); |
| 78 } | 130 } |
| 79 | 131 |
| 80 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotDelta() { | 132 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotDelta() { |
| 81 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash())); | 133 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash())); |
| 82 base::AutoLock auto_lock(lock_); | 134 base::AutoLock auto_lock(lock_); |
| 83 snapshot->Add(samples_); | 135 snapshot->Add(*samples_); |
| 84 | 136 |
| 85 // Subtract what was previously logged and update that information. | 137 // Subtract what was previously logged and update that information. |
| 86 snapshot->Subtract(logged_samples_); | 138 snapshot->Subtract(*logged_samples_); |
| 87 logged_samples_.Add(*snapshot); | 139 logged_samples_->Add(*snapshot); |
| 88 return std::move(snapshot); | 140 return std::move(snapshot); |
| 89 } | 141 } |
| 90 | 142 |
| 91 void SparseHistogram::AddSamples(const HistogramSamples& samples) { | 143 void SparseHistogram::AddSamples(const HistogramSamples& samples) { |
| 92 base::AutoLock auto_lock(lock_); | 144 base::AutoLock auto_lock(lock_); |
| 93 samples_.Add(samples); | 145 samples_->Add(samples); |
| 94 } | 146 } |
| 95 | 147 |
| 96 bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { | 148 bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { |
| 97 base::AutoLock auto_lock(lock_); | 149 base::AutoLock auto_lock(lock_); |
| 98 return samples_.AddFromPickle(iter); | 150 return samples_->AddFromPickle(iter); |
| 99 } | 151 } |
| 100 | 152 |
| 101 void SparseHistogram::WriteHTMLGraph(std::string* output) const { | 153 void SparseHistogram::WriteHTMLGraph(std::string* output) const { |
| 102 output->append("<PRE>"); | 154 output->append("<PRE>"); |
| 103 WriteAsciiImpl(true, "<br>", output); | 155 WriteAsciiImpl(true, "<br>", output); |
| 104 output->append("</PRE>"); | 156 output->append("</PRE>"); |
| 105 } | 157 } |
| 106 | 158 |
| 107 void SparseHistogram::WriteAscii(std::string* output) const { | 159 void SparseHistogram::WriteAscii(std::string* output) const { |
| 108 WriteAsciiImpl(true, "\n", output); | 160 WriteAsciiImpl(true, "\n", output); |
| 109 } | 161 } |
| 110 | 162 |
| 111 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { | 163 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { |
| 112 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); | 164 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); |
| 113 } | 165 } |
| 114 | 166 |
| 115 SparseHistogram::SparseHistogram(const std::string& name) | 167 SparseHistogram::SparseHistogram(const std::string& name) |
| 116 : HistogramBase(name), | 168 : HistogramBase(name), |
| 117 samples_(HashMetricName(name)) {} | 169 samples_(new SampleMap(HashMetricName(name))), |
| 170 logged_samples_(new SampleMap(samples_->id())) {} | |
| 171 | |
| 172 SparseHistogram::SparseHistogram(PersistentMemoryAllocator* allocator, | |
| 173 const std::string& name, | |
| 174 HistogramSamples::Metadata* meta, | |
| 175 HistogramSamples::Metadata* logged_meta) | |
| 176 : HistogramBase(name), | |
| 177 samples_(new PersistentSampleMap(HashMetricName(name), allocator, meta)), | |
| 178 // The ID must be different so as to not confuse sample records. | |
|
Alexei Svitkine (slow)
2016/03/09 20:35:19
Why is this the case here but not for other histog
bcwhite
2016/03/09 22:57:33
Done.
| |
| 179 logged_samples_( | |
| 180 new PersistentSampleMap(samples_->id() + 1, allocator, logged_meta)) { | |
| 181 } | |
| 118 | 182 |
| 119 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { | 183 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
| 120 std::string histogram_name; | 184 std::string histogram_name; |
| 121 int flags; | 185 int flags; |
| 122 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) { | 186 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) { |
| 123 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; | 187 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; |
| 124 return NULL; | 188 return NULL; |
| 125 } | 189 } |
| 126 | 190 |
| 127 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag); | 191 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 std::string* output) const { | 261 std::string* output) const { |
| 198 StringAppendF(output, | 262 StringAppendF(output, |
| 199 "Histogram: %s recorded %d samples", | 263 "Histogram: %s recorded %d samples", |
| 200 histogram_name().c_str(), | 264 histogram_name().c_str(), |
| 201 total_count); | 265 total_count); |
| 202 if (flags() & ~kHexRangePrintingFlag) | 266 if (flags() & ~kHexRangePrintingFlag) |
| 203 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); | 267 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); |
| 204 } | 268 } |
| 205 | 269 |
| 206 } // namespace base | 270 } // namespace base |
| OLD | NEW |