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