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/histogram_snapshot_manager.h" | 5 #include "base/metrics/histogram_snapshot_manager.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/debug/alias.h" | 9 #include "base/debug/alias.h" |
| 10 #include "base/metrics/histogram_flattener.h" | 10 #include "base/metrics/histogram_flattener.h" |
| 11 #include "base/metrics/histogram_samples.h" | 11 #include "base/metrics/histogram_samples.h" |
| 12 #include "base/metrics/statistics_recorder.h" | 12 #include "base/metrics/statistics_recorder.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 | 16 |
| 17 namespace { | |
| 18 | |
| 19 // A simple object to set an "active" flag and clear it upon destruction. It is | |
| 20 // an error if the flag is already set. | |
| 21 class MakeActive { | |
| 22 public: | |
| 23 MakeActive(std::atomic<bool>* is_active) : is_active_(is_active) { | |
| 24 bool was_active = is_active_->exchange(true, std::memory_order_relaxed); | |
| 25 CHECK(!was_active); | |
| 26 } | |
| 27 ~MakeActive() { is_active_->store(false, std::memory_order_relaxed); } | |
| 28 | |
| 29 private: | |
| 30 std::atomic<bool>* is_active_; | |
|
Alexei Svitkine (slow)
2017/05/08 18:51:09
Nit: DISALLOW_COPY_AND_ASSIGN()
bcwhite
2017/05/08 20:06:49
Done.
| |
| 31 }; | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 17 HistogramSnapshotManager::HistogramSnapshotManager( | 35 HistogramSnapshotManager::HistogramSnapshotManager( |
| 18 HistogramFlattener* histogram_flattener) | 36 HistogramFlattener* histogram_flattener) |
| 19 : histogram_flattener_(histogram_flattener) { | 37 : histogram_flattener_(histogram_flattener) { |
| 20 DCHECK(histogram_flattener_); | 38 DCHECK(histogram_flattener_); |
| 39 is_active_.store(false, std::memory_order_relaxed); | |
| 21 } | 40 } |
| 22 | 41 |
| 23 HistogramSnapshotManager::~HistogramSnapshotManager() { | 42 HistogramSnapshotManager::~HistogramSnapshotManager() { |
| 24 } | 43 } |
| 25 | 44 |
| 26 void HistogramSnapshotManager::PrepareDelta(HistogramBase* histogram) { | 45 void HistogramSnapshotManager::PrepareDelta(HistogramBase* histogram) { |
| 27 PrepareSamples(histogram, histogram->SnapshotDelta()); | 46 PrepareSamples(histogram, histogram->SnapshotDelta()); |
| 28 } | 47 } |
| 29 | 48 |
| 30 void HistogramSnapshotManager::PrepareFinalDelta( | 49 void HistogramSnapshotManager::PrepareFinalDelta( |
| 31 const HistogramBase* histogram) { | 50 const HistogramBase* histogram) { |
| 32 PrepareSamples(histogram, histogram->SnapshotFinalDelta()); | 51 PrepareSamples(histogram, histogram->SnapshotFinalDelta()); |
| 33 } | 52 } |
| 34 | 53 |
| 35 void HistogramSnapshotManager::PrepareSamples( | 54 void HistogramSnapshotManager::PrepareSamples( |
| 36 const HistogramBase* histogram, | 55 const HistogramBase* histogram, |
| 37 std::unique_ptr<HistogramSamples> samples) { | 56 std::unique_ptr<HistogramSamples> samples) { |
| 38 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 39 DCHECK(histogram_flattener_); | 57 DCHECK(histogram_flattener_); |
| 40 | 58 |
| 59 // Ensure that there is no concurrent access going on while accessing the | |
| 60 // set of known histograms. The flag will be reset when this object goes | |
| 61 // out of scope. | |
| 62 MakeActive make_active(&is_active_); | |
| 63 | |
| 41 // Get information known about this histogram. If it did not previously | 64 // Get information known about this histogram. If it did not previously |
| 42 // exist, one will be created and initialized. | 65 // exist, one will be created and initialized. |
| 43 SampleInfo* sample_info = &known_histograms_[histogram->name_hash()]; | 66 SampleInfo* sample_info = &known_histograms_[histogram->name_hash()]; |
| 44 | 67 |
| 45 // Crash if we detect that our histograms have been overwritten. This may be | 68 // Crash if we detect that our histograms have been overwritten. This may be |
| 46 // a fair distance from the memory smasher, but we hope to correlate these | 69 // a fair distance from the memory smasher, but we hope to correlate these |
| 47 // crashes with other events, such as plugins, or usage patterns, etc. | 70 // crashes with other events, such as plugins, or usage patterns, etc. |
| 48 uint32_t corruption = histogram->FindCorruption(*samples); | 71 uint32_t corruption = histogram->FindCorruption(*samples); |
| 49 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { | 72 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { |
| 50 // Extract fields useful during debug. | 73 // Extract fields useful during debug. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 histogram_flattener_->UniqueInconsistencyDetected( | 111 histogram_flattener_->UniqueInconsistencyDetected( |
| 89 static_cast<HistogramBase::Inconsistency>(corruption)); | 112 static_cast<HistogramBase::Inconsistency>(corruption)); |
| 90 return; | 113 return; |
| 91 } | 114 } |
| 92 | 115 |
| 93 if (samples->TotalCount() > 0) | 116 if (samples->TotalCount() > 0) |
| 94 histogram_flattener_->RecordDelta(*histogram, *samples); | 117 histogram_flattener_->RecordDelta(*histogram, *samples); |
| 95 } | 118 } |
| 96 | 119 |
| 97 } // namespace base | 120 } // namespace base |
| OLD | NEW |