Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: base/metrics/sample_vector.cc

Issue 116983004: Change some operations touching counts_[] to be atomic. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/metrics/sample_vector.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/sample_vector.h" 5 #include "base/metrics/sample_vector.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/metrics/bucket_ranges.h" 8 #include "base/metrics/bucket_ranges.h"
9 9
10 using std::vector; 10 using std::vector;
(...skipping 14 matching lines...) Expand all
25 void SampleVector::Accumulate(Sample value, Count count) { 25 void SampleVector::Accumulate(Sample value, Count count) {
26 size_t bucket_index = GetBucketIndex(value); 26 size_t bucket_index = GetBucketIndex(value);
27 subtle::NoBarrier_Store(&counts_[bucket_index], 27 subtle::NoBarrier_Store(&counts_[bucket_index],
28 subtle::NoBarrier_Load(&counts_[bucket_index]) + count); 28 subtle::NoBarrier_Load(&counts_[bucket_index]) + count);
29 IncreaseSum(count * value); 29 IncreaseSum(count * value);
30 IncreaseRedundantCount(count); 30 IncreaseRedundantCount(count);
31 } 31 }
32 32
33 Count SampleVector::GetCount(Sample value) const { 33 Count SampleVector::GetCount(Sample value) const {
34 size_t bucket_index = GetBucketIndex(value); 34 size_t bucket_index = GetBucketIndex(value);
35 return counts_[bucket_index]; 35 return subtle::NoBarrier_Load(&counts_[bucket_index]);
36 } 36 }
37 37
38 Count SampleVector::TotalCount() const { 38 Count SampleVector::TotalCount() const {
39 Count count = 0; 39 Count count = 0;
40 for (size_t i = 0; i < counts_.size(); i++) { 40 for (size_t i = 0; i < counts_.size(); i++) {
41 count += counts_[i]; 41 count += subtle::NoBarrier_Load(&counts_[i]);
42 } 42 }
43 return count; 43 return count;
44 } 44 }
45 45
46 Count SampleVector::GetCountAtIndex(size_t bucket_index) const { 46 Count SampleVector::GetCountAtIndex(size_t bucket_index) const {
47 DCHECK(bucket_index < counts_.size()); 47 DCHECK(bucket_index < counts_.size());
48 return counts_[bucket_index]; 48 return subtle::NoBarrier_Load(&counts_[bucket_index]);
49 } 49 }
50 50
51 scoped_ptr<SampleCountIterator> SampleVector::Iterator() const { 51 scoped_ptr<SampleCountIterator> SampleVector::Iterator() const {
52 return scoped_ptr<SampleCountIterator>( 52 return scoped_ptr<SampleCountIterator>(
53 new SampleVectorIterator(&counts_, bucket_ranges_)); 53 new SampleVectorIterator(&counts_, bucket_ranges_));
54 } 54 }
55 55
56 bool SampleVector::AddSubtractImpl(SampleCountIterator* iter, 56 bool SampleVector::AddSubtractImpl(SampleCountIterator* iter,
57 HistogramSamples::Operator op) { 57 HistogramSamples::Operator op) {
58 HistogramBase::Sample min; 58 HistogramBase::Sample min;
59 HistogramBase::Sample max; 59 HistogramBase::Sample max;
60 HistogramBase::Count count; 60 HistogramBase::Count count;
61 61
62 // Go through the iterator and add the counts into correct bucket. 62 // Go through the iterator and add the counts into correct bucket.
63 size_t index = 0; 63 size_t index = 0;
64 while (index < counts_.size() && !iter->Done()) { 64 while (index < counts_.size() && !iter->Done()) {
65 iter->Get(&min, &max, &count); 65 iter->Get(&min, &max, &count);
66 if (min == bucket_ranges_->range(index) && 66 if (min == bucket_ranges_->range(index) &&
67 max == bucket_ranges_->range(index + 1)) { 67 max == bucket_ranges_->range(index + 1)) {
68 // Sample matches this bucket! 68 // Sample matches this bucket!
69 counts_[index] += (op == HistogramSamples::ADD) ? count : -count; 69 HistogramBase::Count old_counts =
70 subtle::NoBarrier_Load(&counts_[index]);
71 subtle::NoBarrier_Store(&counts_[index],
72 old_counts + ((op == HistogramSamples::ADD) ? count : -count));
70 iter->Next(); 73 iter->Next();
71 } else if (min > bucket_ranges_->range(index)) { 74 } else if (min > bucket_ranges_->range(index)) {
72 // Sample is larger than current bucket range. Try next. 75 // Sample is larger than current bucket range. Try next.
73 index++; 76 index++;
74 } else { 77 } else {
75 // Sample is smaller than current bucket range. We scan buckets from 78 // Sample is smaller than current bucket range. We scan buckets from
76 // smallest to largest, so the sample value must be invalid. 79 // smallest to largest, so the sample value must be invalid.
77 return false; 80 return false;
78 } 81 }
79 } 82 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 134
132 void SampleVectorIterator::Get(HistogramBase::Sample* min, 135 void SampleVectorIterator::Get(HistogramBase::Sample* min,
133 HistogramBase::Sample* max, 136 HistogramBase::Sample* max,
134 HistogramBase::Count* count) const { 137 HistogramBase::Count* count) const {
135 DCHECK(!Done()); 138 DCHECK(!Done());
136 if (min != NULL) 139 if (min != NULL)
137 *min = bucket_ranges_->range(index_); 140 *min = bucket_ranges_->range(index_);
138 if (max != NULL) 141 if (max != NULL)
139 *max = bucket_ranges_->range(index_ + 1); 142 *max = bucket_ranges_->range(index_ + 1);
140 if (count != NULL) 143 if (count != NULL)
141 *count = (*counts_)[index_]; 144 *count = subtle::NoBarrier_Load(&(*counts_)[index_]);
142 } 145 }
143 146
144 bool SampleVectorIterator::GetBucketIndex(size_t* index) const { 147 bool SampleVectorIterator::GetBucketIndex(size_t* index) const {
145 DCHECK(!Done()); 148 DCHECK(!Done());
146 if (index != NULL) 149 if (index != NULL)
147 *index = index_; 150 *index = index_;
148 return true; 151 return true;
149 } 152 }
150 153
151 void SampleVectorIterator::SkipEmptyBuckets() { 154 void SampleVectorIterator::SkipEmptyBuckets() {
152 if (Done()) 155 if (Done())
153 return; 156 return;
154 157
155 while (index_ < counts_->size()) { 158 while (index_ < counts_->size()) {
156 if ((*counts_)[index_] != 0) 159 if (subtle::NoBarrier_Load(&(*counts_)[index_]) != 0)
157 return; 160 return;
158 index_++; 161 index_++;
159 } 162 }
160 } 163 }
161 164
162 } // namespace base 165 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/sample_vector.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698