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_; |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(MakeActive); |
| 33 }; |
| 34 |
| 35 } // namespace |
| 36 |
17 HistogramSnapshotManager::HistogramSnapshotManager( | 37 HistogramSnapshotManager::HistogramSnapshotManager( |
18 HistogramFlattener* histogram_flattener) | 38 HistogramFlattener* histogram_flattener) |
19 : histogram_flattener_(histogram_flattener) { | 39 : histogram_flattener_(histogram_flattener) { |
20 DCHECK(histogram_flattener_); | 40 DCHECK(histogram_flattener_); |
| 41 is_active_.store(false, std::memory_order_relaxed); |
21 } | 42 } |
22 | 43 |
23 HistogramSnapshotManager::~HistogramSnapshotManager() { | 44 HistogramSnapshotManager::~HistogramSnapshotManager() { |
24 } | 45 } |
25 | 46 |
26 void HistogramSnapshotManager::PrepareDelta(HistogramBase* histogram) { | 47 void HistogramSnapshotManager::PrepareDelta(HistogramBase* histogram) { |
27 PrepareSamples(histogram, histogram->SnapshotDelta()); | 48 PrepareSamples(histogram, histogram->SnapshotDelta()); |
28 } | 49 } |
29 | 50 |
30 void HistogramSnapshotManager::PrepareFinalDelta( | 51 void HistogramSnapshotManager::PrepareFinalDelta( |
31 const HistogramBase* histogram) { | 52 const HistogramBase* histogram) { |
32 PrepareSamples(histogram, histogram->SnapshotFinalDelta()); | 53 PrepareSamples(histogram, histogram->SnapshotFinalDelta()); |
33 } | 54 } |
34 | 55 |
35 void HistogramSnapshotManager::PrepareSamples( | 56 void HistogramSnapshotManager::PrepareSamples( |
36 const HistogramBase* histogram, | 57 const HistogramBase* histogram, |
37 std::unique_ptr<HistogramSamples> samples) { | 58 std::unique_ptr<HistogramSamples> samples) { |
38 DCHECK(thread_checker_.CalledOnValidThread()); | |
39 DCHECK(histogram_flattener_); | 59 DCHECK(histogram_flattener_); |
40 | 60 |
| 61 // Ensure that there is no concurrent access going on while accessing the |
| 62 // set of known histograms. The flag will be reset when this object goes |
| 63 // out of scope. |
| 64 MakeActive make_active(&is_active_); |
| 65 |
41 // Get information known about this histogram. If it did not previously | 66 // Get information known about this histogram. If it did not previously |
42 // exist, one will be created and initialized. | 67 // exist, one will be created and initialized. |
43 SampleInfo* sample_info = &known_histograms_[histogram->name_hash()]; | 68 SampleInfo* sample_info = &known_histograms_[histogram->name_hash()]; |
44 | 69 |
45 // Crash if we detect that our histograms have been overwritten. This may be | 70 // 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 | 71 // 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. | 72 // crashes with other events, such as plugins, or usage patterns, etc. |
48 uint32_t corruption = histogram->FindCorruption(*samples); | 73 uint32_t corruption = histogram->FindCorruption(*samples); |
49 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { | 74 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { |
50 // Extract fields useful during debug. | 75 // Extract fields useful during debug. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 histogram_flattener_->UniqueInconsistencyDetected( | 113 histogram_flattener_->UniqueInconsistencyDetected( |
89 static_cast<HistogramBase::Inconsistency>(corruption)); | 114 static_cast<HistogramBase::Inconsistency>(corruption)); |
90 return; | 115 return; |
91 } | 116 } |
92 | 117 |
93 if (samples->TotalCount() > 0) | 118 if (samples->TotalCount() > 0) |
94 histogram_flattener_->RecordDelta(*histogram, *samples); | 119 histogram_flattener_->RecordDelta(*histogram, *samples); |
95 } | 120 } |
96 | 121 |
97 } // namespace base | 122 } // namespace base |
OLD | NEW |