| 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/statistics_recorder.h" | 5 #include "base/metrics/statistics_recorder.h" |
| 6 | 6 |
| 7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
| 8 #include "base/debug/leak_annotations.h" | 8 #include "base/debug/leak_annotations.h" |
| 9 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 // static | 76 // static |
| 77 bool StatisticsRecorder::IsActive() { | 77 bool StatisticsRecorder::IsActive() { |
| 78 if (lock_ == NULL) | 78 if (lock_ == NULL) |
| 79 return false; | 79 return false; |
| 80 base::AutoLock auto_lock(*lock_); | 80 base::AutoLock auto_lock(*lock_); |
| 81 return NULL != histograms_; | 81 return NULL != histograms_; |
| 82 } | 82 } |
| 83 | 83 |
| 84 // static | 84 // static |
| 85 HistogramBase* StatisticsRecorder::RegisterOrDeleteDuplicate( | 85 HistogramBase* StatisticsRecorder::RegisterOrDeleteDuplicate( |
| 86 HistogramBase* histogram) { | 86 scoped_ptr<HistogramBase> histogram_ptr) { |
| 87 // Ownership is managed outside of scoped_ptr, the use of which is only to |
| 88 // explicitly document the transfer of ownership during the call. |
| 89 HistogramBase* histogram = histogram_ptr.release(); |
| 90 |
| 87 // As per crbug.com/79322 the histograms are intentionally leaked, so we need | 91 // As per crbug.com/79322 the histograms are intentionally leaked, so we need |
| 88 // to annotate them. Because ANNOTATE_LEAKING_OBJECT_PTR may be used only once | 92 // to annotate them. Because ANNOTATE_LEAKING_OBJECT_PTR may be used only once |
| 89 // for an object, the duplicates should not be annotated. | 93 // for an object, the duplicates should not be annotated. |
| 90 // Callers are responsible for not calling RegisterOrDeleteDuplicate(ptr) | 94 // Callers are responsible for not calling RegisterOrDeleteDuplicate(ptr) |
| 91 // twice if (lock_ == NULL) || (!histograms_). | 95 // twice if (lock_ == NULL) || (!histograms_). |
| 92 if (lock_ == NULL) { | 96 if (lock_ == NULL) { |
| 93 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 | 97 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 |
| 94 return histogram; | 98 return histogram; |
| 95 } | 99 } |
| 96 | 100 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 112 // flag. | 116 // flag. |
| 113 auto callback_iterator = callbacks_->find(name); | 117 auto callback_iterator = callbacks_->find(name); |
| 114 if (callback_iterator != callbacks_->end()) { | 118 if (callback_iterator != callbacks_->end()) { |
| 115 if (!callback_iterator->second.is_null()) | 119 if (!callback_iterator->second.is_null()) |
| 116 histogram->SetFlags(HistogramBase::kCallbackExists); | 120 histogram->SetFlags(HistogramBase::kCallbackExists); |
| 117 else | 121 else |
| 118 histogram->ClearFlags(HistogramBase::kCallbackExists); | 122 histogram->ClearFlags(HistogramBase::kCallbackExists); |
| 119 } | 123 } |
| 120 histogram_to_return = histogram; | 124 histogram_to_return = histogram; |
| 121 } else if (histogram == it->second) { | 125 } else if (histogram == it->second) { |
| 122 // The histogram was registered before. | 126 // The histogram was registered before. This should never happen since |
| 127 // the method signature ensures moving of the histogram pointer. |
| 128 NOTREACHED(); |
| 123 histogram_to_return = histogram; | 129 histogram_to_return = histogram; |
| 124 } else { | 130 } else { |
| 125 // We already have one histogram with this name. | 131 // We already have one histogram with this name. |
| 126 DCHECK_EQ(histogram->histogram_name(), | 132 DCHECK_EQ(histogram->histogram_name(), |
| 127 it->second->histogram_name()) << "hash collision"; | 133 it->second->histogram_name()) << "hash collision"; |
| 128 histogram_to_return = it->second; | 134 histogram_to_return = it->second; |
| 129 histogram_to_delete = histogram; | 135 histogram_to_delete = histogram; |
| 130 } | 136 } |
| 131 } | 137 } |
| 132 } | 138 } |
| 133 delete histogram_to_delete; | 139 delete histogram_to_delete; |
| 134 return histogram_to_return; | 140 return histogram_to_return; |
| 135 } | 141 } |
| 136 | 142 |
| 137 // static | 143 // static |
| 138 const BucketRanges* StatisticsRecorder::RegisterOrDeleteDuplicateRanges( | 144 const BucketRanges* StatisticsRecorder::RegisterOrDeleteDuplicateRanges( |
| 139 const BucketRanges* ranges) { | 145 scoped_ptr<const BucketRanges> ranges_ptr) { |
| 146 // Ownership is managed outside of scoped_ptr, the use of which is only to |
| 147 // explicitly document the transfer of ownership during the call. |
| 148 const BucketRanges* ranges = ranges_ptr.release(); |
| 149 |
| 140 DCHECK(ranges->HasValidChecksum()); | 150 DCHECK(ranges->HasValidChecksum()); |
| 141 scoped_ptr<const BucketRanges> ranges_deleter; | 151 scoped_ptr<const BucketRanges> ranges_deleter; |
| 142 | 152 |
| 143 if (lock_ == NULL) { | 153 if (lock_ == NULL) { |
| 144 ANNOTATE_LEAKING_OBJECT_PTR(ranges); | 154 ANNOTATE_LEAKING_OBJECT_PTR(ranges); |
| 145 return ranges; | 155 return ranges; |
| 146 } | 156 } |
| 147 | 157 |
| 148 base::AutoLock auto_lock(*lock_); | 158 base::AutoLock auto_lock(*lock_); |
| 149 if (ranges_ == NULL) { | 159 if (ranges_ == NULL) { |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 // static | 454 // static |
| 445 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; | 455 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; |
| 446 // static | 456 // static |
| 447 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; | 457 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; |
| 448 // static | 458 // static |
| 449 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; | 459 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; |
| 450 // static | 460 // static |
| 451 base::Lock* StatisticsRecorder::lock_ = NULL; | 461 base::Lock* StatisticsRecorder::lock_ = NULL; |
| 452 | 462 |
| 453 } // namespace base | 463 } // namespace base |
| OLD | NEW |