Fix race in WebRTC logging.
[chromium-blink-merge.git] / base / metrics / histogram_samples.h
blobc4c0a9842b7da28ad9ae7caf05b7dedb4d415047
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
3 // found in the LICENSE file.
5 #ifndef BASE_METRICS_HISTOGRAM_SAMPLES_H_
6 #define BASE_METRICS_HISTOGRAM_SAMPLES_H_
8 #include "base/basictypes.h"
9 #include "base/metrics/histogram_base.h"
10 #include "base/memory/scoped_ptr.h"
12 class Pickle;
13 class PickleIterator;
15 namespace base {
17 class SampleCountIterator;
19 // HistogramSamples is a container storing all samples of a histogram.
20 class BASE_EXPORT HistogramSamples {
21 public:
22 HistogramSamples();
23 virtual ~HistogramSamples();
25 virtual void Accumulate(HistogramBase::Sample value,
26 HistogramBase::Count count) = 0;
27 virtual HistogramBase::Count GetCount(HistogramBase::Sample value) const = 0;
28 virtual HistogramBase::Count TotalCount() const = 0;
30 virtual void Add(const HistogramSamples& other);
32 // Add from serialized samples.
33 virtual bool AddFromPickle(PickleIterator* iter);
35 virtual void Subtract(const HistogramSamples& other);
37 virtual scoped_ptr<SampleCountIterator> Iterator() const = 0;
38 virtual bool Serialize(Pickle* pickle) const;
40 // Accessor fuctions.
41 int64 sum() const { return sum_; }
42 HistogramBase::Count redundant_count() const {
43 return subtle::NoBarrier_Load(&redundant_count_);
46 protected:
47 // Based on |op| type, add or subtract sample counts data from the iterator.
48 enum Operator { ADD, SUBTRACT };
49 virtual bool AddSubtractImpl(SampleCountIterator* iter, Operator op) = 0;
51 void IncreaseSum(int64 diff);
52 void IncreaseRedundantCount(HistogramBase::Count diff);
54 private:
55 int64 sum_;
57 // |redundant_count_| helps identify memory corruption. It redundantly stores
58 // the total number of samples accumulated in the histogram. We can compare
59 // this count to the sum of the counts (TotalCount() function), and detect
60 // problems. Note, depending on the implementation of different histogram
61 // types, there might be races during histogram accumulation and snapshotting
62 // that we choose to accept. In this case, the tallies might mismatch even
63 // when no memory corruption has happened.
64 HistogramBase::AtomicCount redundant_count_;
67 class BASE_EXPORT SampleCountIterator {
68 public:
69 virtual ~SampleCountIterator();
71 virtual bool Done() const = 0;
72 virtual void Next() = 0;
74 // Get the sample and count at current position.
75 // |min| |max| and |count| can be NULL if the value is not of interest.
76 // Requires: !Done();
77 virtual void Get(HistogramBase::Sample* min,
78 HistogramBase::Sample* max,
79 HistogramBase::Count* count) const = 0;
81 // Get the index of current histogram bucket.
82 // For histograms that don't use predefined buckets, it returns false.
83 // Requires: !Done();
84 virtual bool GetBucketIndex(size_t* index) const;
87 } // namespace base
89 #endif // BASE_METRICS_HISTOGRAM_SAMPLES_H_