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_SPARSE_HISTOGRAM_H_
6 #define BASE_METRICS_SPARSE_HISTOGRAM_H_
11 #include "base/base_export.h"
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/metrics/histogram_base.h"
17 #include "base/metrics/sample_map.h"
18 #include "base/synchronization/lock.h"
22 // The common code for different SparseHistogram macros.
23 #define HISTOGRAM_SPARSE_COMMON(name, sample, flag) \
25 base::HistogramBase* histogram( \
26 base::SparseHistogram::FactoryGet(name, flag)); \
27 DCHECK_EQ(histogram->histogram_name(), name); \
28 histogram->Add(sample); \
31 #define HISTOGRAM_SPARSE_SLOWLY(name, sample) \
32 HISTOGRAM_SPARSE_COMMON(name, sample, base::HistogramBase::kNoFlags)
34 #define UMA_HISTOGRAM_SPARSE_SLOWLY(name, sample) \
35 HISTOGRAM_SPARSE_COMMON(name, sample, \
36 base::HistogramBase::kUmaTargetedHistogramFlag)
38 //------------------------------------------------------------------------------
39 // Define debug only version of macros.
42 #define DHISTOGRAM_SPARSE_SLOWLY(name, sample) \
43 HISTOGRAM_SPARSE_SLOWLY(name, sample)
47 #define DHISTOGRAM_SPARSE_SLOWLY(name, sample) \
49 static_cast<void>(name); \
50 static_cast<void>(sample); \
55 class HistogramSamples
;
57 class BASE_EXPORT_PRIVATE SparseHistogram
: public HistogramBase
{
59 // If there's one with same name, return the existing one. If not, create a
61 static HistogramBase
* FactoryGet(const std::string
& name
, int32 flags
);
63 virtual ~SparseHistogram();
65 // HistogramBase implementation:
66 virtual HistogramType
GetHistogramType() const OVERRIDE
;
67 virtual bool HasConstructionArguments(
68 Sample expected_minimum
,
69 Sample expected_maximum
,
70 size_t expected_bucket_count
) const OVERRIDE
;
71 virtual void Add(Sample value
) OVERRIDE
;
72 virtual void AddSamples(const HistogramSamples
& samples
) OVERRIDE
;
73 virtual bool AddSamplesFromPickle(PickleIterator
* iter
) OVERRIDE
;
74 virtual scoped_ptr
<HistogramSamples
> SnapshotSamples() const OVERRIDE
;
75 virtual void WriteHTMLGraph(std::string
* output
) const OVERRIDE
;
76 virtual void WriteAscii(std::string
* output
) const OVERRIDE
;
79 // HistogramBase implementation:
80 virtual bool SerializeInfoImpl(Pickle
* pickle
) const OVERRIDE
;
83 // Clients should always use FactoryGet to create SparseHistogram.
84 explicit SparseHistogram(const std::string
& name
);
86 friend BASE_EXPORT_PRIVATE HistogramBase
* DeserializeHistogramInfo(
87 PickleIterator
* iter
);
88 static HistogramBase
* DeserializeInfoImpl(PickleIterator
* iter
);
90 virtual void GetParameters(DictionaryValue
* params
) const OVERRIDE
;
91 virtual void GetCountAndBucketData(Count
* count
,
93 ListValue
* buckets
) const OVERRIDE
;
95 // Helpers for emitting Ascii graphic. Each method appends data to output.
96 void WriteAsciiImpl(bool graph_it
,
97 const std::string
& newline
,
98 std::string
* output
) const;
100 // Write a common header message describing this histogram.
101 void WriteAsciiHeader(const Count total_count
,
102 std::string
* output
) const;
104 // For constuctor calling.
105 friend class SparseHistogramTest
;
107 // Protects access to |samples_|.
108 mutable base::Lock lock_
;
112 DISALLOW_COPY_AND_ASSIGN(SparseHistogram
);
117 #endif // BASE_METRICS_SPARSE_HISTOGRAM_H_