- add sources.
[platform/framework/web/crosswalk.git] / src / base / metrics / sparse_histogram.h
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.
4
5 #ifndef BASE_METRICS_SPARSE_HISTOGRAM_H_
6 #define BASE_METRICS_SPARSE_HISTOGRAM_H_
7
8 #include <map>
9 #include <string>
10
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"
19
20 namespace base {
21
22 // The common code for different SparseHistogram macros.
23 #define HISTOGRAM_SPARSE_COMMON(name, sample, flag) \
24     do { \
25       base::HistogramBase* histogram( \
26           base::SparseHistogram::FactoryGet(name, flag)); \
27       DCHECK_EQ(histogram->histogram_name(), name); \
28       histogram->Add(sample); \
29     } while (0)
30
31 #define HISTOGRAM_SPARSE_SLOWLY(name, sample) \
32     HISTOGRAM_SPARSE_COMMON(name, sample, base::HistogramBase::kNoFlags)
33
34 #define UMA_HISTOGRAM_SPARSE_SLOWLY(name, sample) \
35     HISTOGRAM_SPARSE_COMMON(name, sample, \
36                             base::HistogramBase::kUmaTargetedHistogramFlag)
37
38 //------------------------------------------------------------------------------
39 // Define debug only version of macros.
40 #ifndef NDEBUG
41
42 #define DHISTOGRAM_SPARSE_SLOWLY(name, sample) \
43     HISTOGRAM_SPARSE_SLOWLY(name, sample)
44
45 #else  // NDEBUG
46
47 #define DHISTOGRAM_SPARSE_SLOWLY(name, sample) \
48     while (0) { \
49       static_cast<void>(name); \
50       static_cast<void>(sample); \
51     }
52
53 #endif  // NDEBUG
54
55 class HistogramSamples;
56
57 class BASE_EXPORT_PRIVATE SparseHistogram : public HistogramBase {
58  public:
59   // If there's one with same name, return the existing one. If not, create a
60   // new one.
61   static HistogramBase* FactoryGet(const std::string& name, int32 flags);
62
63   virtual ~SparseHistogram();
64
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;
77
78  protected:
79   // HistogramBase implementation:
80   virtual bool SerializeInfoImpl(Pickle* pickle) const OVERRIDE;
81
82  private:
83   // Clients should always use FactoryGet to create SparseHistogram.
84   explicit SparseHistogram(const std::string& name);
85
86   friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo(
87       PickleIterator* iter);
88   static HistogramBase* DeserializeInfoImpl(PickleIterator* iter);
89
90   virtual void GetParameters(DictionaryValue* params) const OVERRIDE;
91   virtual void GetCountAndBucketData(Count* count,
92                                      int64* sum,
93                                      ListValue* buckets) const OVERRIDE;
94
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;
99
100   // Write a common header message describing this histogram.
101   void WriteAsciiHeader(const Count total_count,
102                         std::string* output) const;
103
104   // For constuctor calling.
105   friend class SparseHistogramTest;
106
107   // Protects access to |samples_|.
108   mutable base::Lock lock_;
109
110   SampleMap samples_;
111
112   DISALLOW_COPY_AND_ASSIGN(SparseHistogram);
113 };
114
115 }  // namespace base
116
117 #endif  // BASE_METRICS_SPARSE_HISTOGRAM_H_