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_t id) |
63 : meta_(&local_meta_) { | |
64 SetMetadataId(id); | |
65 } | |
66 | |
67 HistogramSamples::HistogramSamples(uint64_t 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_t id) { | |
75 CHECK(meta_->id == 0 || meta_->id == id); | |
Alexei Svitkine (slow)
2015/12/09 23:03:40
Make this a DCHECK(). CHECKs() are generally reser
bcwhite
2015/12/10 14:51:33
Done.
| |
76 *const_cast<uint64_t*>(&meta_->id) = id; | |
Alexei Svitkine (slow)
2015/12/09 23:03:39
Nit: Remove the const qualifier on the field and m
bcwhite
2015/12/10 14:51:33
Given that the assignment constraints have been sh
| |
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 meta_->sum += other.sum(); |
91 | |
68 HistogramBase::Count old_redundant_count = | 92 HistogramBase::Count old_redundant_count = |
69 subtle::NoBarrier_Load(&redundant_count_); | 93 subtle::NoBarrier_Load(&meta_->redundant_count); |
70 subtle::NoBarrier_Store(&redundant_count_, | 94 subtle::NoBarrier_Store(&meta_->redundant_count, |
71 old_redundant_count + other.redundant_count()); | 95 old_redundant_count + other.redundant_count()); |
72 bool success = AddSubtractImpl(other.Iterator().get(), ADD); | 96 bool success = AddSubtractImpl(other.Iterator().get(), ADD); |
73 DCHECK(success); | 97 DCHECK(success); |
74 } | 98 } |
75 | 99 |
76 bool HistogramSamples::AddFromPickle(PickleIterator* iter) { | 100 bool HistogramSamples::AddFromPickle(PickleIterator* iter) { |
77 int64 sum; | 101 int64_t sum; |
78 HistogramBase::Count redundant_count; | 102 HistogramBase::Count redundant_count; |
79 | 103 |
80 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count)) | 104 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count)) |
81 return false; | 105 return false; |
82 sum_ += sum; | 106 |
107 meta_->sum += sum; | |
108 | |
83 HistogramBase::Count old_redundant_count = | 109 HistogramBase::Count old_redundant_count = |
84 subtle::NoBarrier_Load(&redundant_count_); | 110 subtle::NoBarrier_Load(&meta_->redundant_count); |
85 subtle::NoBarrier_Store(&redundant_count_, | 111 subtle::NoBarrier_Store(&meta_->redundant_count, |
86 old_redundant_count + redundant_count); | 112 old_redundant_count + redundant_count); |
87 | 113 |
88 SampleCountPickleIterator pickle_iter(iter); | 114 SampleCountPickleIterator pickle_iter(iter); |
89 return AddSubtractImpl(&pickle_iter, ADD); | 115 return AddSubtractImpl(&pickle_iter, ADD); |
90 } | 116 } |
91 | 117 |
92 void HistogramSamples::Subtract(const HistogramSamples& other) { | 118 void HistogramSamples::Subtract(const HistogramSamples& other) { |
93 sum_ -= other.sum(); | 119 meta_->sum -= other.sum(); |
120 | |
94 HistogramBase::Count old_redundant_count = | 121 HistogramBase::Count old_redundant_count = |
95 subtle::NoBarrier_Load(&redundant_count_); | 122 subtle::NoBarrier_Load(&meta_->redundant_count); |
96 subtle::NoBarrier_Store(&redundant_count_, | 123 subtle::NoBarrier_Store(&meta_->redundant_count, |
97 old_redundant_count - other.redundant_count()); | 124 old_redundant_count - other.redundant_count()); |
98 bool success = AddSubtractImpl(other.Iterator().get(), SUBTRACT); | 125 bool success = AddSubtractImpl(other.Iterator().get(), SUBTRACT); |
99 DCHECK(success); | 126 DCHECK(success); |
100 } | 127 } |
101 | 128 |
102 bool HistogramSamples::Serialize(Pickle* pickle) const { | 129 bool HistogramSamples::Serialize(Pickle* pickle) const { |
103 if (!pickle->WriteInt64(sum_) || | 130 if (!pickle->WriteInt64(meta_->sum)) |
104 !pickle->WriteInt(subtle::NoBarrier_Load(&redundant_count_))) | 131 return false; |
132 if (!pickle->WriteInt(subtle::NoBarrier_Load(&meta_->redundant_count))) | |
105 return false; | 133 return false; |
106 | 134 |
107 HistogramBase::Sample min; | 135 HistogramBase::Sample min; |
108 HistogramBase::Sample max; | 136 HistogramBase::Sample max; |
109 HistogramBase::Count count; | 137 HistogramBase::Count count; |
110 for (scoped_ptr<SampleCountIterator> it = Iterator(); | 138 for (scoped_ptr<SampleCountIterator> it = Iterator(); |
111 !it->Done(); | 139 !it->Done(); |
112 it->Next()) { | 140 it->Next()) { |
113 it->Get(&min, &max, &count); | 141 it->Get(&min, &max, &count); |
114 if (!pickle->WriteInt(min) || | 142 if (!pickle->WriteInt(min) || |
115 !pickle->WriteInt(max) || | 143 !pickle->WriteInt(max) || |
116 !pickle->WriteInt(count)) | 144 !pickle->WriteInt(count)) |
117 return false; | 145 return false; |
118 } | 146 } |
119 return true; | 147 return true; |
120 } | 148 } |
121 | 149 |
122 void HistogramSamples::IncreaseSum(int64 diff) { | 150 void HistogramSamples::IncreaseSum(int64_t diff) { |
123 sum_ += diff; | 151 meta_->sum += diff; |
124 } | 152 } |
125 | 153 |
126 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) { | 154 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) { |
127 subtle::NoBarrier_Store(&redundant_count_, | 155 subtle::NoBarrier_Store(&meta_->redundant_count, |
128 subtle::NoBarrier_Load(&redundant_count_) + diff); | 156 subtle::NoBarrier_Load(&meta_->redundant_count) + diff); |
129 } | 157 } |
130 | 158 |
131 SampleCountIterator::~SampleCountIterator() {} | 159 SampleCountIterator::~SampleCountIterator() {} |
132 | 160 |
133 bool SampleCountIterator::GetBucketIndex(size_t* index) const { | 161 bool SampleCountIterator::GetBucketIndex(size_t* index) const { |
134 DCHECK(!Done()); | 162 DCHECK(!Done()); |
135 return false; | 163 return false; |
136 } | 164 } |
137 | 165 |
138 } // namespace base | 166 } // namespace base |
OLD | NEW |