xref: /aosp_15_r20/external/libchrome/base/metrics/persistent_sample_map.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2016 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/metrics/persistent_sample_map.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
8*635a8641SAndroid Build Coastguard Worker #include "base/memory/ptr_util.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_macros.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/metrics/persistent_histogram_allocator.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/numerics/safe_conversions.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/stl_util.h"
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker namespace base {
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker typedef HistogramBase::Count Count;
17*635a8641SAndroid Build Coastguard Worker typedef HistogramBase::Sample Sample;
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker namespace {
20*635a8641SAndroid Build Coastguard Worker 
21*635a8641SAndroid Build Coastguard Worker // An iterator for going through a PersistentSampleMap. The logic here is
22*635a8641SAndroid Build Coastguard Worker // identical to that of SampleMapIterator but with different data structures.
23*635a8641SAndroid Build Coastguard Worker // Changes here likely need to be duplicated there.
24*635a8641SAndroid Build Coastguard Worker class PersistentSampleMapIterator : public SampleCountIterator {
25*635a8641SAndroid Build Coastguard Worker  public:
26*635a8641SAndroid Build Coastguard Worker   typedef std::map<HistogramBase::Sample, HistogramBase::Count*>
27*635a8641SAndroid Build Coastguard Worker       SampleToCountMap;
28*635a8641SAndroid Build Coastguard Worker 
29*635a8641SAndroid Build Coastguard Worker   explicit PersistentSampleMapIterator(const SampleToCountMap& sample_counts);
30*635a8641SAndroid Build Coastguard Worker   ~PersistentSampleMapIterator() override;
31*635a8641SAndroid Build Coastguard Worker 
32*635a8641SAndroid Build Coastguard Worker   // SampleCountIterator:
33*635a8641SAndroid Build Coastguard Worker   bool Done() const override;
34*635a8641SAndroid Build Coastguard Worker   void Next() override;
35*635a8641SAndroid Build Coastguard Worker   void Get(HistogramBase::Sample* min,
36*635a8641SAndroid Build Coastguard Worker            int64_t* max,
37*635a8641SAndroid Build Coastguard Worker            HistogramBase::Count* count) const override;
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker  private:
40*635a8641SAndroid Build Coastguard Worker   void SkipEmptyBuckets();
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker   SampleToCountMap::const_iterator iter_;
43*635a8641SAndroid Build Coastguard Worker   const SampleToCountMap::const_iterator end_;
44*635a8641SAndroid Build Coastguard Worker };
45*635a8641SAndroid Build Coastguard Worker 
PersistentSampleMapIterator(const SampleToCountMap & sample_counts)46*635a8641SAndroid Build Coastguard Worker PersistentSampleMapIterator::PersistentSampleMapIterator(
47*635a8641SAndroid Build Coastguard Worker     const SampleToCountMap& sample_counts)
48*635a8641SAndroid Build Coastguard Worker     : iter_(sample_counts.begin()),
49*635a8641SAndroid Build Coastguard Worker       end_(sample_counts.end()) {
50*635a8641SAndroid Build Coastguard Worker   SkipEmptyBuckets();
51*635a8641SAndroid Build Coastguard Worker }
52*635a8641SAndroid Build Coastguard Worker 
53*635a8641SAndroid Build Coastguard Worker PersistentSampleMapIterator::~PersistentSampleMapIterator() = default;
54*635a8641SAndroid Build Coastguard Worker 
Done() const55*635a8641SAndroid Build Coastguard Worker bool PersistentSampleMapIterator::Done() const {
56*635a8641SAndroid Build Coastguard Worker   return iter_ == end_;
57*635a8641SAndroid Build Coastguard Worker }
58*635a8641SAndroid Build Coastguard Worker 
Next()59*635a8641SAndroid Build Coastguard Worker void PersistentSampleMapIterator::Next() {
60*635a8641SAndroid Build Coastguard Worker   DCHECK(!Done());
61*635a8641SAndroid Build Coastguard Worker   ++iter_;
62*635a8641SAndroid Build Coastguard Worker   SkipEmptyBuckets();
63*635a8641SAndroid Build Coastguard Worker }
64*635a8641SAndroid Build Coastguard Worker 
Get(Sample * min,int64_t * max,Count * count) const65*635a8641SAndroid Build Coastguard Worker void PersistentSampleMapIterator::Get(Sample* min,
66*635a8641SAndroid Build Coastguard Worker                                       int64_t* max,
67*635a8641SAndroid Build Coastguard Worker                                       Count* count) const {
68*635a8641SAndroid Build Coastguard Worker   DCHECK(!Done());
69*635a8641SAndroid Build Coastguard Worker   if (min)
70*635a8641SAndroid Build Coastguard Worker     *min = iter_->first;
71*635a8641SAndroid Build Coastguard Worker   if (max)
72*635a8641SAndroid Build Coastguard Worker     *max = strict_cast<int64_t>(iter_->first) + 1;
73*635a8641SAndroid Build Coastguard Worker   if (count)
74*635a8641SAndroid Build Coastguard Worker     *count = *iter_->second;
75*635a8641SAndroid Build Coastguard Worker }
76*635a8641SAndroid Build Coastguard Worker 
SkipEmptyBuckets()77*635a8641SAndroid Build Coastguard Worker void PersistentSampleMapIterator::SkipEmptyBuckets() {
78*635a8641SAndroid Build Coastguard Worker   while (!Done() && *iter_->second == 0) {
79*635a8641SAndroid Build Coastguard Worker     ++iter_;
80*635a8641SAndroid Build Coastguard Worker   }
81*635a8641SAndroid Build Coastguard Worker }
82*635a8641SAndroid Build Coastguard Worker 
83*635a8641SAndroid Build Coastguard Worker // This structure holds an entry for a PersistentSampleMap within a persistent
84*635a8641SAndroid Build Coastguard Worker // memory allocator. The "id" must be unique across all maps held by an
85*635a8641SAndroid Build Coastguard Worker // allocator or they will get attached to the wrong sample map.
86*635a8641SAndroid Build Coastguard Worker struct SampleRecord {
87*635a8641SAndroid Build Coastguard Worker   // SHA1(SampleRecord): Increment this if structure changes!
88*635a8641SAndroid Build Coastguard Worker   static constexpr uint32_t kPersistentTypeId = 0x8FE6A69F + 1;
89*635a8641SAndroid Build Coastguard Worker 
90*635a8641SAndroid Build Coastguard Worker   // Expected size for 32/64-bit check.
91*635a8641SAndroid Build Coastguard Worker   static constexpr size_t kExpectedInstanceSize = 16;
92*635a8641SAndroid Build Coastguard Worker 
93*635a8641SAndroid Build Coastguard Worker   uint64_t id;   // Unique identifier of owner.
94*635a8641SAndroid Build Coastguard Worker   Sample value;  // The value for which this record holds a count.
95*635a8641SAndroid Build Coastguard Worker   Count count;   // The count associated with the above value.
96*635a8641SAndroid Build Coastguard Worker };
97*635a8641SAndroid Build Coastguard Worker 
98*635a8641SAndroid Build Coastguard Worker }  // namespace
99*635a8641SAndroid Build Coastguard Worker 
PersistentSampleMap(uint64_t id,PersistentHistogramAllocator * allocator,Metadata * meta)100*635a8641SAndroid Build Coastguard Worker PersistentSampleMap::PersistentSampleMap(
101*635a8641SAndroid Build Coastguard Worker     uint64_t id,
102*635a8641SAndroid Build Coastguard Worker     PersistentHistogramAllocator* allocator,
103*635a8641SAndroid Build Coastguard Worker     Metadata* meta)
104*635a8641SAndroid Build Coastguard Worker     : HistogramSamples(id, meta), allocator_(allocator) {}
105*635a8641SAndroid Build Coastguard Worker 
~PersistentSampleMap()106*635a8641SAndroid Build Coastguard Worker PersistentSampleMap::~PersistentSampleMap() {
107*635a8641SAndroid Build Coastguard Worker   if (records_)
108*635a8641SAndroid Build Coastguard Worker     records_->Release(this);
109*635a8641SAndroid Build Coastguard Worker }
110*635a8641SAndroid Build Coastguard Worker 
Accumulate(Sample value,Count count)111*635a8641SAndroid Build Coastguard Worker void PersistentSampleMap::Accumulate(Sample value, Count count) {
112*635a8641SAndroid Build Coastguard Worker #if 0  // TODO(bcwhite) Re-enable efficient version after crbug.com/682680.
113*635a8641SAndroid Build Coastguard Worker   *GetOrCreateSampleCountStorage(value) += count;
114*635a8641SAndroid Build Coastguard Worker #else
115*635a8641SAndroid Build Coastguard Worker   Count* local_count_ptr = GetOrCreateSampleCountStorage(value);
116*635a8641SAndroid Build Coastguard Worker   if (count < 0) {
117*635a8641SAndroid Build Coastguard Worker     if (*local_count_ptr < -count)
118*635a8641SAndroid Build Coastguard Worker       RecordNegativeSample(SAMPLES_ACCUMULATE_WENT_NEGATIVE, -count);
119*635a8641SAndroid Build Coastguard Worker     else
120*635a8641SAndroid Build Coastguard Worker       RecordNegativeSample(SAMPLES_ACCUMULATE_NEGATIVE_COUNT, -count);
121*635a8641SAndroid Build Coastguard Worker     *local_count_ptr += count;
122*635a8641SAndroid Build Coastguard Worker   } else {
123*635a8641SAndroid Build Coastguard Worker     Sample old_value = *local_count_ptr;
124*635a8641SAndroid Build Coastguard Worker     Sample new_value = old_value + count;
125*635a8641SAndroid Build Coastguard Worker     *local_count_ptr = new_value;
126*635a8641SAndroid Build Coastguard Worker     if ((new_value >= 0) != (old_value >= 0))
127*635a8641SAndroid Build Coastguard Worker       RecordNegativeSample(SAMPLES_ACCUMULATE_OVERFLOW, count);
128*635a8641SAndroid Build Coastguard Worker   }
129*635a8641SAndroid Build Coastguard Worker #endif
130*635a8641SAndroid Build Coastguard Worker   IncreaseSumAndCount(strict_cast<int64_t>(count) * value, count);
131*635a8641SAndroid Build Coastguard Worker }
132*635a8641SAndroid Build Coastguard Worker 
GetCount(Sample value) const133*635a8641SAndroid Build Coastguard Worker Count PersistentSampleMap::GetCount(Sample value) const {
134*635a8641SAndroid Build Coastguard Worker   // Have to override "const" to make sure all samples have been loaded before
135*635a8641SAndroid Build Coastguard Worker   // being able to know what value to return.
136*635a8641SAndroid Build Coastguard Worker   Count* count_pointer =
137*635a8641SAndroid Build Coastguard Worker       const_cast<PersistentSampleMap*>(this)->GetSampleCountStorage(value);
138*635a8641SAndroid Build Coastguard Worker   return count_pointer ? *count_pointer : 0;
139*635a8641SAndroid Build Coastguard Worker }
140*635a8641SAndroid Build Coastguard Worker 
TotalCount() const141*635a8641SAndroid Build Coastguard Worker Count PersistentSampleMap::TotalCount() const {
142*635a8641SAndroid Build Coastguard Worker   // Have to override "const" in order to make sure all samples have been
143*635a8641SAndroid Build Coastguard Worker   // loaded before trying to iterate over the map.
144*635a8641SAndroid Build Coastguard Worker   const_cast<PersistentSampleMap*>(this)->ImportSamples(-1, true);
145*635a8641SAndroid Build Coastguard Worker 
146*635a8641SAndroid Build Coastguard Worker   Count count = 0;
147*635a8641SAndroid Build Coastguard Worker   for (const auto& entry : sample_counts_) {
148*635a8641SAndroid Build Coastguard Worker     count += *entry.second;
149*635a8641SAndroid Build Coastguard Worker   }
150*635a8641SAndroid Build Coastguard Worker   return count;
151*635a8641SAndroid Build Coastguard Worker }
152*635a8641SAndroid Build Coastguard Worker 
Iterator() const153*635a8641SAndroid Build Coastguard Worker std::unique_ptr<SampleCountIterator> PersistentSampleMap::Iterator() const {
154*635a8641SAndroid Build Coastguard Worker   // Have to override "const" in order to make sure all samples have been
155*635a8641SAndroid Build Coastguard Worker   // loaded before trying to iterate over the map.
156*635a8641SAndroid Build Coastguard Worker   const_cast<PersistentSampleMap*>(this)->ImportSamples(-1, true);
157*635a8641SAndroid Build Coastguard Worker   return WrapUnique(new PersistentSampleMapIterator(sample_counts_));
158*635a8641SAndroid Build Coastguard Worker }
159*635a8641SAndroid Build Coastguard Worker 
160*635a8641SAndroid Build Coastguard Worker // static
161*635a8641SAndroid Build Coastguard Worker PersistentMemoryAllocator::Reference
GetNextPersistentRecord(PersistentMemoryAllocator::Iterator & iterator,uint64_t * sample_map_id)162*635a8641SAndroid Build Coastguard Worker PersistentSampleMap::GetNextPersistentRecord(
163*635a8641SAndroid Build Coastguard Worker     PersistentMemoryAllocator::Iterator& iterator,
164*635a8641SAndroid Build Coastguard Worker     uint64_t* sample_map_id) {
165*635a8641SAndroid Build Coastguard Worker   const SampleRecord* record = iterator.GetNextOfObject<SampleRecord>();
166*635a8641SAndroid Build Coastguard Worker   if (!record)
167*635a8641SAndroid Build Coastguard Worker     return 0;
168*635a8641SAndroid Build Coastguard Worker 
169*635a8641SAndroid Build Coastguard Worker   *sample_map_id = record->id;
170*635a8641SAndroid Build Coastguard Worker   return iterator.GetAsReference(record);
171*635a8641SAndroid Build Coastguard Worker }
172*635a8641SAndroid Build Coastguard Worker 
173*635a8641SAndroid Build Coastguard Worker // static
174*635a8641SAndroid Build Coastguard Worker PersistentMemoryAllocator::Reference
CreatePersistentRecord(PersistentMemoryAllocator * allocator,uint64_t sample_map_id,Sample value)175*635a8641SAndroid Build Coastguard Worker PersistentSampleMap::CreatePersistentRecord(
176*635a8641SAndroid Build Coastguard Worker     PersistentMemoryAllocator* allocator,
177*635a8641SAndroid Build Coastguard Worker     uint64_t sample_map_id,
178*635a8641SAndroid Build Coastguard Worker     Sample value) {
179*635a8641SAndroid Build Coastguard Worker   SampleRecord* record = allocator->New<SampleRecord>();
180*635a8641SAndroid Build Coastguard Worker   if (!record) {
181*635a8641SAndroid Build Coastguard Worker     NOTREACHED() << "full=" << allocator->IsFull()
182*635a8641SAndroid Build Coastguard Worker                  << ", corrupt=" << allocator->IsCorrupt();
183*635a8641SAndroid Build Coastguard Worker     return 0;
184*635a8641SAndroid Build Coastguard Worker   }
185*635a8641SAndroid Build Coastguard Worker 
186*635a8641SAndroid Build Coastguard Worker   record->id = sample_map_id;
187*635a8641SAndroid Build Coastguard Worker   record->value = value;
188*635a8641SAndroid Build Coastguard Worker   record->count = 0;
189*635a8641SAndroid Build Coastguard Worker 
190*635a8641SAndroid Build Coastguard Worker   PersistentMemoryAllocator::Reference ref = allocator->GetAsReference(record);
191*635a8641SAndroid Build Coastguard Worker   allocator->MakeIterable(ref);
192*635a8641SAndroid Build Coastguard Worker   return ref;
193*635a8641SAndroid Build Coastguard Worker }
194*635a8641SAndroid Build Coastguard Worker 
AddSubtractImpl(SampleCountIterator * iter,Operator op)195*635a8641SAndroid Build Coastguard Worker bool PersistentSampleMap::AddSubtractImpl(SampleCountIterator* iter,
196*635a8641SAndroid Build Coastguard Worker                                           Operator op) {
197*635a8641SAndroid Build Coastguard Worker   Sample min;
198*635a8641SAndroid Build Coastguard Worker   int64_t max;
199*635a8641SAndroid Build Coastguard Worker   Count count;
200*635a8641SAndroid Build Coastguard Worker   for (; !iter->Done(); iter->Next()) {
201*635a8641SAndroid Build Coastguard Worker     iter->Get(&min, &max, &count);
202*635a8641SAndroid Build Coastguard Worker     if (count == 0)
203*635a8641SAndroid Build Coastguard Worker       continue;
204*635a8641SAndroid Build Coastguard Worker     if (strict_cast<int64_t>(min) + 1 != max)
205*635a8641SAndroid Build Coastguard Worker       return false;  // SparseHistogram only supports bucket with size 1.
206*635a8641SAndroid Build Coastguard Worker     *GetOrCreateSampleCountStorage(min) +=
207*635a8641SAndroid Build Coastguard Worker         (op == HistogramSamples::ADD) ? count : -count;
208*635a8641SAndroid Build Coastguard Worker   }
209*635a8641SAndroid Build Coastguard Worker   return true;
210*635a8641SAndroid Build Coastguard Worker }
211*635a8641SAndroid Build Coastguard Worker 
GetSampleCountStorage(Sample value)212*635a8641SAndroid Build Coastguard Worker Count* PersistentSampleMap::GetSampleCountStorage(Sample value) {
213*635a8641SAndroid Build Coastguard Worker   // If |value| is already in the map, just return that.
214*635a8641SAndroid Build Coastguard Worker   auto it = sample_counts_.find(value);
215*635a8641SAndroid Build Coastguard Worker   if (it != sample_counts_.end())
216*635a8641SAndroid Build Coastguard Worker     return it->second;
217*635a8641SAndroid Build Coastguard Worker 
218*635a8641SAndroid Build Coastguard Worker   // Import any new samples from persistent memory looking for the value.
219*635a8641SAndroid Build Coastguard Worker   return ImportSamples(value, false);
220*635a8641SAndroid Build Coastguard Worker }
221*635a8641SAndroid Build Coastguard Worker 
GetOrCreateSampleCountStorage(Sample value)222*635a8641SAndroid Build Coastguard Worker Count* PersistentSampleMap::GetOrCreateSampleCountStorage(Sample value) {
223*635a8641SAndroid Build Coastguard Worker   // Get any existing count storage.
224*635a8641SAndroid Build Coastguard Worker   Count* count_pointer = GetSampleCountStorage(value);
225*635a8641SAndroid Build Coastguard Worker   if (count_pointer)
226*635a8641SAndroid Build Coastguard Worker     return count_pointer;
227*635a8641SAndroid Build Coastguard Worker 
228*635a8641SAndroid Build Coastguard Worker   // Create a new record in persistent memory for the value. |records_| will
229*635a8641SAndroid Build Coastguard Worker   // have been initialized by the GetSampleCountStorage() call above.
230*635a8641SAndroid Build Coastguard Worker   DCHECK(records_);
231*635a8641SAndroid Build Coastguard Worker   PersistentMemoryAllocator::Reference ref = records_->CreateNew(value);
232*635a8641SAndroid Build Coastguard Worker   if (!ref) {
233*635a8641SAndroid Build Coastguard Worker     // If a new record could not be created then the underlying allocator is
234*635a8641SAndroid Build Coastguard Worker     // full or corrupt. Instead, allocate the counter from the heap. This
235*635a8641SAndroid Build Coastguard Worker     // sample will not be persistent, will not be shared, and will leak...
236*635a8641SAndroid Build Coastguard Worker     // but it's better than crashing.
237*635a8641SAndroid Build Coastguard Worker     count_pointer = new Count(0);
238*635a8641SAndroid Build Coastguard Worker     sample_counts_[value] = count_pointer;
239*635a8641SAndroid Build Coastguard Worker     return count_pointer;
240*635a8641SAndroid Build Coastguard Worker   }
241*635a8641SAndroid Build Coastguard Worker 
242*635a8641SAndroid Build Coastguard Worker   // A race condition between two independent processes (i.e. two independent
243*635a8641SAndroid Build Coastguard Worker   // histogram objects sharing the same sample data) could cause two of the
244*635a8641SAndroid Build Coastguard Worker   // above records to be created. The allocator, however, forces a strict
245*635a8641SAndroid Build Coastguard Worker   // ordering on iterable objects so use the import method to actually add the
246*635a8641SAndroid Build Coastguard Worker   // just-created record. This ensures that all PersistentSampleMap objects
247*635a8641SAndroid Build Coastguard Worker   // will always use the same record, whichever was first made iterable.
248*635a8641SAndroid Build Coastguard Worker   // Thread-safety within a process where multiple threads use the same
249*635a8641SAndroid Build Coastguard Worker   // histogram object is delegated to the controlling histogram object which,
250*635a8641SAndroid Build Coastguard Worker   // for sparse histograms, is a lock object.
251*635a8641SAndroid Build Coastguard Worker   count_pointer = ImportSamples(value, false);
252*635a8641SAndroid Build Coastguard Worker   DCHECK(count_pointer);
253*635a8641SAndroid Build Coastguard Worker   return count_pointer;
254*635a8641SAndroid Build Coastguard Worker }
255*635a8641SAndroid Build Coastguard Worker 
GetRecords()256*635a8641SAndroid Build Coastguard Worker PersistentSampleMapRecords* PersistentSampleMap::GetRecords() {
257*635a8641SAndroid Build Coastguard Worker   // The |records_| pointer is lazily fetched from the |allocator_| only on
258*635a8641SAndroid Build Coastguard Worker   // first use. Sometimes duplicate histograms are created by race conditions
259*635a8641SAndroid Build Coastguard Worker   // and if both were to grab the records object, there would be a conflict.
260*635a8641SAndroid Build Coastguard Worker   // Use of a histogram, and thus a call to this method, won't occur until
261*635a8641SAndroid Build Coastguard Worker   // after the histogram has been de-dup'd.
262*635a8641SAndroid Build Coastguard Worker   if (!records_)
263*635a8641SAndroid Build Coastguard Worker     records_ = allocator_->UseSampleMapRecords(id(), this);
264*635a8641SAndroid Build Coastguard Worker   return records_;
265*635a8641SAndroid Build Coastguard Worker }
266*635a8641SAndroid Build Coastguard Worker 
ImportSamples(Sample until_value,bool import_everything)267*635a8641SAndroid Build Coastguard Worker Count* PersistentSampleMap::ImportSamples(Sample until_value,
268*635a8641SAndroid Build Coastguard Worker                                           bool import_everything) {
269*635a8641SAndroid Build Coastguard Worker   Count* found_count = nullptr;
270*635a8641SAndroid Build Coastguard Worker   PersistentMemoryAllocator::Reference ref;
271*635a8641SAndroid Build Coastguard Worker   PersistentSampleMapRecords* records = GetRecords();
272*635a8641SAndroid Build Coastguard Worker   while ((ref = records->GetNext()) != 0) {
273*635a8641SAndroid Build Coastguard Worker     SampleRecord* record = records->GetAsObject<SampleRecord>(ref);
274*635a8641SAndroid Build Coastguard Worker     if (!record)
275*635a8641SAndroid Build Coastguard Worker       continue;
276*635a8641SAndroid Build Coastguard Worker 
277*635a8641SAndroid Build Coastguard Worker     DCHECK_EQ(id(), record->id);
278*635a8641SAndroid Build Coastguard Worker 
279*635a8641SAndroid Build Coastguard Worker     // Check if the record's value is already known.
280*635a8641SAndroid Build Coastguard Worker     if (!ContainsKey(sample_counts_, record->value)) {
281*635a8641SAndroid Build Coastguard Worker       // No: Add it to map of known values.
282*635a8641SAndroid Build Coastguard Worker       sample_counts_[record->value] = &record->count;
283*635a8641SAndroid Build Coastguard Worker     } else {
284*635a8641SAndroid Build Coastguard Worker       // Yes: Ignore it; it's a duplicate caused by a race condition -- see
285*635a8641SAndroid Build Coastguard Worker       // code & comment in GetOrCreateSampleCountStorage() for details.
286*635a8641SAndroid Build Coastguard Worker       // Check that nothing ever operated on the duplicate record.
287*635a8641SAndroid Build Coastguard Worker       DCHECK_EQ(0, record->count);
288*635a8641SAndroid Build Coastguard Worker     }
289*635a8641SAndroid Build Coastguard Worker 
290*635a8641SAndroid Build Coastguard Worker     // Check if it's the value being searched for and, if so, keep a pointer
291*635a8641SAndroid Build Coastguard Worker     // to return later. Stop here unless everything is being imported.
292*635a8641SAndroid Build Coastguard Worker     // Because race conditions can cause multiple records for a single value,
293*635a8641SAndroid Build Coastguard Worker     // be sure to return the first one found.
294*635a8641SAndroid Build Coastguard Worker     if (record->value == until_value) {
295*635a8641SAndroid Build Coastguard Worker       if (!found_count)
296*635a8641SAndroid Build Coastguard Worker         found_count = &record->count;
297*635a8641SAndroid Build Coastguard Worker       if (!import_everything)
298*635a8641SAndroid Build Coastguard Worker         break;
299*635a8641SAndroid Build Coastguard Worker     }
300*635a8641SAndroid Build Coastguard Worker   }
301*635a8641SAndroid Build Coastguard Worker 
302*635a8641SAndroid Build Coastguard Worker   return found_count;
303*635a8641SAndroid Build Coastguard Worker }
304*635a8641SAndroid Build Coastguard Worker 
305*635a8641SAndroid Build Coastguard Worker }  // namespace base
306