sparse_histogram.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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
20namespace 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
55class HistogramSamples;
56
57class 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(Sample minimum,
68                                        Sample maximum,
69                                        size_t bucket_count) const OVERRIDE;
70  virtual void Add(Sample value) OVERRIDE;
71  virtual void AddSamples(const HistogramSamples& samples) OVERRIDE;
72  virtual bool AddSamplesFromPickle(PickleIterator* iter) OVERRIDE;
73  virtual scoped_ptr<HistogramSamples> SnapshotSamples() const OVERRIDE;
74  virtual void WriteHTMLGraph(std::string* output) const OVERRIDE;
75  virtual void WriteAscii(std::string* output) const OVERRIDE;
76
77 protected:
78  // HistogramBase implementation:
79  virtual bool SerializeInfoImpl(Pickle* pickle) const OVERRIDE;
80
81 private:
82  // Clients should always use FactoryGet to create SparseHistogram.
83  explicit SparseHistogram(const std::string& name);
84
85  friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo(
86      PickleIterator* iter);
87  static HistogramBase* DeserializeInfoImpl(PickleIterator* iter);
88
89  virtual void GetParameters(DictionaryValue* params) const OVERRIDE;
90  virtual void GetCountAndBucketData(Count* count,
91                                     ListValue* buckets) const OVERRIDE;
92
93  // Helpers for emitting Ascii graphic.  Each method appends data to output.
94  void WriteAsciiImpl(bool graph_it,
95                      const std::string& newline,
96                      std::string* output) const;
97
98  // Write a common header message describing this histogram.
99  void WriteAsciiHeader(const Count total_count,
100                        std::string* output) const;
101
102  // For constuctor calling.
103  friend class SparseHistogramTest;
104
105  // Protects access to |samples_|.
106  mutable base::Lock lock_;
107
108  SampleMap samples_;
109
110  DISALLOW_COPY_AND_ASSIGN(SparseHistogram);
111};
112
113}  // namespace base
114
115#endif  // BASE_METRICS_SPARSE_HISTOGRAM_H_
116