| 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/memory/ptr_util.h" |
| 9 #include "base/metrics/metrics_hashes.h" | 10 #include "base/metrics/metrics_hashes.h" |
| 10 #include "base/metrics/persistent_histogram_allocator.h" | 11 #include "base/metrics/persistent_histogram_allocator.h" |
| 11 #include "base/metrics/persistent_sample_map.h" | 12 #include "base/metrics/persistent_sample_map.h" |
| 12 #include "base/metrics/sample_map.h" | 13 #include "base/metrics/sample_map.h" |
| 13 #include "base/metrics/statistics_recorder.h" | 14 #include "base/metrics/statistics_recorder.h" |
| 14 #include "base/pickle.h" | 15 #include "base/pickle.h" |
| 15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 30 PersistentHistogramAllocator::ImportGlobalHistograms(); | 31 PersistentHistogramAllocator::ImportGlobalHistograms(); |
| 31 | 32 |
| 32 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | 33 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| 33 if (!histogram) { | 34 if (!histogram) { |
| 34 // Try to create the histogram using a "persistent" allocator. As of | 35 // 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 // 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 // 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 // allocating from it fails, code below will allocate the histogram from |
| 38 // the process heap. | 39 // the process heap. |
| 39 PersistentMemoryAllocator::Reference histogram_ref = 0; | 40 PersistentMemoryAllocator::Reference histogram_ref = 0; |
| 40 scoped_ptr<HistogramBase> tentative_histogram; | 41 std::unique_ptr<HistogramBase> tentative_histogram; |
| 41 PersistentHistogramAllocator* allocator = | 42 PersistentHistogramAllocator* allocator = |
| 42 PersistentHistogramAllocator::GetGlobalAllocator(); | 43 PersistentHistogramAllocator::GetGlobalAllocator(); |
| 43 if (allocator) { | 44 if (allocator) { |
| 44 tentative_histogram = allocator->AllocateHistogram( | 45 tentative_histogram = allocator->AllocateHistogram( |
| 45 SPARSE_HISTOGRAM, name, 0, 0, nullptr, flags, &histogram_ref); | 46 SPARSE_HISTOGRAM, name, 0, 0, nullptr, flags, &histogram_ref); |
| 46 } | 47 } |
| 47 | 48 |
| 48 // Handle the case where no persistent allocator is present or the | 49 // Handle the case where no persistent allocator is present or the |
| 49 // persistent allocation fails (perhaps because it is full). | 50 // persistent allocation fails (perhaps because it is full). |
| 50 if (!tentative_histogram) { | 51 if (!tentative_histogram) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 72 ReportHistogramActivity(*histogram, HISTOGRAM_CREATED); | 73 ReportHistogramActivity(*histogram, HISTOGRAM_CREATED); |
| 73 } else { | 74 } else { |
| 74 ReportHistogramActivity(*histogram, HISTOGRAM_LOOKUP); | 75 ReportHistogramActivity(*histogram, HISTOGRAM_LOOKUP); |
| 75 } | 76 } |
| 76 | 77 |
| 77 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType()); | 78 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType()); |
| 78 return histogram; | 79 return histogram; |
| 79 } | 80 } |
| 80 | 81 |
| 81 // static | 82 // static |
| 82 scoped_ptr<HistogramBase> SparseHistogram::PersistentCreate( | 83 std::unique_ptr<HistogramBase> SparseHistogram::PersistentCreate( |
| 83 PersistentMemoryAllocator* allocator, | 84 PersistentMemoryAllocator* allocator, |
| 84 const std::string& name, | 85 const std::string& name, |
| 85 HistogramSamples::Metadata* meta, | 86 HistogramSamples::Metadata* meta, |
| 86 HistogramSamples::Metadata* logged_meta) { | 87 HistogramSamples::Metadata* logged_meta) { |
| 87 return make_scoped_ptr( | 88 return base::WrapUnique( |
| 88 new SparseHistogram(allocator, name, meta, logged_meta)); | 89 new SparseHistogram(allocator, name, meta, logged_meta)); |
| 89 } | 90 } |
| 90 | 91 |
| 91 SparseHistogram::~SparseHistogram() {} | 92 SparseHistogram::~SparseHistogram() {} |
| 92 | 93 |
| 93 uint64_t SparseHistogram::name_hash() const { | 94 uint64_t SparseHistogram::name_hash() const { |
| 94 return samples_->id(); | 95 return samples_->id(); |
| 95 } | 96 } |
| 96 | 97 |
| 97 HistogramType SparseHistogram::GetHistogramType() const { | 98 HistogramType SparseHistogram::GetHistogramType() const { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 116 return; | 117 return; |
| 117 } | 118 } |
| 118 { | 119 { |
| 119 base::AutoLock auto_lock(lock_); | 120 base::AutoLock auto_lock(lock_); |
| 120 samples_->Accumulate(value, count); | 121 samples_->Accumulate(value, count); |
| 121 } | 122 } |
| 122 | 123 |
| 123 FindAndRunCallback(value); | 124 FindAndRunCallback(value); |
| 124 } | 125 } |
| 125 | 126 |
| 126 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { | 127 std::unique_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { |
| 127 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash())); | 128 std::unique_ptr<SampleMap> snapshot(new SampleMap(name_hash())); |
| 128 | 129 |
| 129 base::AutoLock auto_lock(lock_); | 130 base::AutoLock auto_lock(lock_); |
| 130 snapshot->Add(*samples_); | 131 snapshot->Add(*samples_); |
| 131 return std::move(snapshot); | 132 return std::move(snapshot); |
| 132 } | 133 } |
| 133 | 134 |
| 134 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotDelta() { | 135 std::unique_ptr<HistogramSamples> SparseHistogram::SnapshotDelta() { |
| 135 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash())); | 136 std::unique_ptr<SampleMap> snapshot(new SampleMap(name_hash())); |
| 136 base::AutoLock auto_lock(lock_); | 137 base::AutoLock auto_lock(lock_); |
| 137 snapshot->Add(*samples_); | 138 snapshot->Add(*samples_); |
| 138 | 139 |
| 139 // Subtract what was previously logged and update that information. | 140 // Subtract what was previously logged and update that information. |
| 140 snapshot->Subtract(*logged_samples_); | 141 snapshot->Subtract(*logged_samples_); |
| 141 logged_samples_->Add(*snapshot); | 142 logged_samples_->Add(*snapshot); |
| 142 return std::move(snapshot); | 143 return std::move(snapshot); |
| 143 } | 144 } |
| 144 | 145 |
| 145 void SparseHistogram::AddSamples(const HistogramSamples& samples) { | 146 void SparseHistogram::AddSamples(const HistogramSamples& samples) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 void SparseHistogram::GetCountAndBucketData(Count* count, | 213 void SparseHistogram::GetCountAndBucketData(Count* count, |
| 213 int64_t* sum, | 214 int64_t* sum, |
| 214 ListValue* buckets) const { | 215 ListValue* buckets) const { |
| 215 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) | 216 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
| 216 } | 217 } |
| 217 | 218 |
| 218 void SparseHistogram::WriteAsciiImpl(bool graph_it, | 219 void SparseHistogram::WriteAsciiImpl(bool graph_it, |
| 219 const std::string& newline, | 220 const std::string& newline, |
| 220 std::string* output) const { | 221 std::string* output) const { |
| 221 // Get a local copy of the data so we are consistent. | 222 // Get a local copy of the data so we are consistent. |
| 222 scoped_ptr<HistogramSamples> snapshot = SnapshotSamples(); | 223 std::unique_ptr<HistogramSamples> snapshot = SnapshotSamples(); |
| 223 Count total_count = snapshot->TotalCount(); | 224 Count total_count = snapshot->TotalCount(); |
| 224 double scaled_total_count = total_count / 100.0; | 225 double scaled_total_count = total_count / 100.0; |
| 225 | 226 |
| 226 WriteAsciiHeader(total_count, output); | 227 WriteAsciiHeader(total_count, output); |
| 227 output->append(newline); | 228 output->append(newline); |
| 228 | 229 |
| 229 // Determine how wide the largest bucket range is (how many digits to print), | 230 // Determine how wide the largest bucket range is (how many digits to print), |
| 230 // so that we'll be able to right-align starts for the graphical bars. | 231 // so that we'll be able to right-align starts for the graphical bars. |
| 231 // Determine which bucket has the largest sample count so that we can | 232 // Determine which bucket has the largest sample count so that we can |
| 232 // normalize the graphical bar-width relative to that sample count. | 233 // normalize the graphical bar-width relative to that sample count. |
| 233 Count largest_count = 0; | 234 Count largest_count = 0; |
| 234 Sample largest_sample = 0; | 235 Sample largest_sample = 0; |
| 235 scoped_ptr<SampleCountIterator> it = snapshot->Iterator(); | 236 std::unique_ptr<SampleCountIterator> it = snapshot->Iterator(); |
| 236 while (!it->Done()) { | 237 while (!it->Done()) { |
| 237 Sample min; | 238 Sample min; |
| 238 Sample max; | 239 Sample max; |
| 239 Count count; | 240 Count count; |
| 240 it->Get(&min, &max, &count); | 241 it->Get(&min, &max, &count); |
| 241 if (min > largest_sample) | 242 if (min > largest_sample) |
| 242 largest_sample = min; | 243 largest_sample = min; |
| 243 if (count > largest_count) | 244 if (count > largest_count) |
| 244 largest_count = count; | 245 largest_count = count; |
| 245 it->Next(); | 246 it->Next(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 272 std::string* output) const { | 273 std::string* output) const { |
| 273 StringAppendF(output, | 274 StringAppendF(output, |
| 274 "Histogram: %s recorded %d samples", | 275 "Histogram: %s recorded %d samples", |
| 275 histogram_name().c_str(), | 276 histogram_name().c_str(), |
| 276 total_count); | 277 total_count); |
| 277 if (flags() & ~kHexRangePrintingFlag) | 278 if (flags() & ~kHexRangePrintingFlag) |
| 278 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); | 279 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); |
| 279 } | 280 } |
| 280 | 281 |
| 281 } // namespace base | 282 } // namespace base |
| OLD | NEW |