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_samples.h" | 5 #include "base/metrics/histogram_samples.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/pickle.h" | 8 #include "base/pickle.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 HistogramBase::Sample* max, | 52 HistogramBase::Sample* max, |
53 HistogramBase::Count* count) const { | 53 HistogramBase::Count* count) const { |
54 DCHECK(!Done()); | 54 DCHECK(!Done()); |
55 *min = min_; | 55 *min = min_; |
56 *max = max_; | 56 *max = max_; |
57 *count = count_; | 57 *count = count_; |
58 } | 58 } |
59 | 59 |
60 } // namespace | 60 } // namespace |
61 | 61 |
62 HistogramSamples::HistogramSamples() : sum_(0), redundant_count_(0) {} | 62 HistogramSamples::HistogramSamples(uint64 id) |
63 : meta_(&local_meta_) { | |
64 SetMetadataId(id); | |
65 } | |
66 | |
67 HistogramSamples::HistogramSamples(uint64 id, Metadata* meta) | |
68 : meta_(meta) { | |
69 SetMetadataId(id); | |
70 } | |
63 | 71 |
64 HistogramSamples::~HistogramSamples() {} | 72 HistogramSamples::~HistogramSamples() {} |
65 | 73 |
74 void HistogramSamples::SetMetadataId(uint64 id) { | |
75 CHECK(meta_->id == 0 || meta_->id == id); | |
76 *const_cast<uint64*>(&meta_->id) = id; | |
77 } | |
78 | |
79 // Despite using atomic operations, the increment/add actions below are *not* | |
80 // atomic! Race conditions may cause loss of samples or even completely corrupt | |
81 // the 64-bit sum on 32-bit machines. This is done intentionally to reduce the | |
82 // cost of these operations that could be executed in performance-significant | |
83 // points of the code. | |
84 // | |
85 // TODO(bcwhite): Gather quantitative information as to the cost of using | |
86 // proper atomic increments and improve either globally or for those histograms | |
87 // that really need it. | |
88 | |
66 void HistogramSamples::Add(const HistogramSamples& other) { | 89 void HistogramSamples::Add(const HistogramSamples& other) { |
67 sum_ += other.sum(); | 90 #ifdef ARCH_CPU_64_BITS |
91 subtle::NoBarrier_Store( | |
92 &meta_->sum, | |
93 subtle::NoBarrier_Load(&meta_->sum) + other.sum()); | |
94 #else // No Atomic64 on 32-bit platforms. | |
95 meta_->sum += other.sum(); | |
96 #endif | |
97 | |
68 HistogramBase::Count old_redundant_count = | 98 HistogramBase::Count old_redundant_count = |
69 subtle::NoBarrier_Load(&redundant_count_); | 99 subtle::NoBarrier_Load(&meta_->redundant_count); |
70 subtle::NoBarrier_Store(&redundant_count_, | 100 subtle::NoBarrier_Store(&meta_->redundant_count, |
71 old_redundant_count + other.redundant_count()); | 101 old_redundant_count + other.redundant_count()); |
72 bool success = AddSubtractImpl(other.Iterator().get(), ADD); | 102 bool success = AddSubtractImpl(other.Iterator().get(), ADD); |
73 DCHECK(success); | 103 DCHECK(success); |
74 } | 104 } |
75 | 105 |
76 bool HistogramSamples::AddFromPickle(PickleIterator* iter) { | 106 bool HistogramSamples::AddFromPickle(PickleIterator* iter) { |
77 int64 sum; | 107 int64 sum; |
78 HistogramBase::Count redundant_count; | 108 HistogramBase::Count redundant_count; |
79 | 109 |
80 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count)) | 110 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count)) |
81 return false; | 111 return false; |
82 sum_ += sum; | 112 |
113 #ifdef ARCH_CPU_64_BITS | |
114 subtle::NoBarrier_Store( | |
115 &meta_->sum, | |
116 subtle::NoBarrier_Load(&meta_->sum) + sum); | |
117 #else // No Atomic64 on 32-bit platforms. | |
118 meta_->sum += sum; | |
119 #endif | |
120 | |
83 HistogramBase::Count old_redundant_count = | 121 HistogramBase::Count old_redundant_count = |
84 subtle::NoBarrier_Load(&redundant_count_); | 122 subtle::NoBarrier_Load(&meta_->redundant_count); |
85 subtle::NoBarrier_Store(&redundant_count_, | 123 subtle::NoBarrier_Store(&meta_->redundant_count, |
86 old_redundant_count + redundant_count); | 124 old_redundant_count + redundant_count); |
87 | 125 |
88 SampleCountPickleIterator pickle_iter(iter); | 126 SampleCountPickleIterator pickle_iter(iter); |
89 return AddSubtractImpl(&pickle_iter, ADD); | 127 return AddSubtractImpl(&pickle_iter, ADD); |
90 } | 128 } |
91 | 129 |
92 void HistogramSamples::Subtract(const HistogramSamples& other) { | 130 void HistogramSamples::Subtract(const HistogramSamples& other) { |
93 sum_ -= other.sum(); | 131 #ifdef ARCH_CPU_64_BITS |
132 subtle::NoBarrier_Store( | |
133 &meta_->sum, | |
134 subtle::NoBarrier_Load(&meta_->sum) - other.sum()); | |
135 #else // No Atomic64 on 32-bit platforms. | |
136 meta_->sum -= other.sum(); | |
137 #endif | |
138 | |
94 HistogramBase::Count old_redundant_count = | 139 HistogramBase::Count old_redundant_count = |
95 subtle::NoBarrier_Load(&redundant_count_); | 140 subtle::NoBarrier_Load(&meta_->redundant_count); |
96 subtle::NoBarrier_Store(&redundant_count_, | 141 subtle::NoBarrier_Store(&meta_->redundant_count, |
97 old_redundant_count - other.redundant_count()); | 142 old_redundant_count - other.redundant_count()); |
98 bool success = AddSubtractImpl(other.Iterator().get(), SUBTRACT); | 143 bool success = AddSubtractImpl(other.Iterator().get(), SUBTRACT); |
99 DCHECK(success); | 144 DCHECK(success); |
100 } | 145 } |
101 | 146 |
102 bool HistogramSamples::Serialize(Pickle* pickle) const { | 147 bool HistogramSamples::Serialize(Pickle* pickle) const { |
103 if (!pickle->WriteInt64(sum_) || | 148 #ifdef ARCH_CPU_64_BITS |
104 !pickle->WriteInt(subtle::NoBarrier_Load(&redundant_count_))) | 149 if (!pickle->WriteInt64(subtle::NoBarrier_Load(&meta_->sum))) |
150 return false; | |
151 #else // No Atomic64 on 32-bit platforms. | |
152 if (!pickle->WriteInt64(meta_->sum)) | |
153 return false; | |
154 #endif | |
155 if (!pickle->WriteInt(subtle::NoBarrier_Load(&meta_->redundant_count))) | |
105 return false; | 156 return false; |
106 | 157 |
107 HistogramBase::Sample min; | 158 HistogramBase::Sample min; |
108 HistogramBase::Sample max; | 159 HistogramBase::Sample max; |
109 HistogramBase::Count count; | 160 HistogramBase::Count count; |
110 for (scoped_ptr<SampleCountIterator> it = Iterator(); | 161 for (scoped_ptr<SampleCountIterator> it = Iterator(); |
111 !it->Done(); | 162 !it->Done(); |
112 it->Next()) { | 163 it->Next()) { |
113 it->Get(&min, &max, &count); | 164 it->Get(&min, &max, &count); |
114 if (!pickle->WriteInt(min) || | 165 if (!pickle->WriteInt(min) || |
115 !pickle->WriteInt(max) || | 166 !pickle->WriteInt(max) || |
116 !pickle->WriteInt(count)) | 167 !pickle->WriteInt(count)) |
117 return false; | 168 return false; |
118 } | 169 } |
119 return true; | 170 return true; |
120 } | 171 } |
121 | 172 |
122 void HistogramSamples::IncreaseSum(int64 diff) { | 173 void HistogramSamples::IncreaseSum(int64 diff) { |
123 sum_ += diff; | 174 #ifdef ARCH_CPU_64_BITS |
Alexei Svitkine (slow)
2015/11/25 18:05:27
Can we hide these details in the implementation of
bcwhite
2015/11/25 21:54:17
I'll look at moving the metadata to std::atomic wh
| |
175 subtle::NoBarrier_Store( | |
176 &meta_->sum, | |
177 subtle::NoBarrier_Load(&meta_->sum) + diff); | |
178 #else // No Atomic64 on 32-bit platforms. | |
179 meta_->sum += diff; | |
180 #endif | |
124 } | 181 } |
125 | 182 |
126 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) { | 183 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) { |
127 subtle::NoBarrier_Store(&redundant_count_, | 184 subtle::NoBarrier_Store(&meta_->redundant_count, |
128 subtle::NoBarrier_Load(&redundant_count_) + diff); | 185 subtle::NoBarrier_Load(&meta_->redundant_count) + diff); |
129 } | 186 } |
130 | 187 |
131 SampleCountIterator::~SampleCountIterator() {} | 188 SampleCountIterator::~SampleCountIterator() {} |
132 | 189 |
133 bool SampleCountIterator::GetBucketIndex(size_t* index) const { | 190 bool SampleCountIterator::GetBucketIndex(size_t* index) const { |
134 DCHECK(!Done()); | 191 DCHECK(!Done()); |
135 return false; | 192 return false; |
136 } | 193 } |
137 | 194 |
138 } // namespace base | 195 } // namespace base |
OLD | NEW |