blob: a77c020d4cb87533d68c986eba1bd71a8902a90f [file] [log] [blame]
[email protected]7c7a42752012-08-09 05:14:151// 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
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
9#include <stdint.h>
10
[email protected]7c7a42752012-08-09 05:14:1511#include <map>
12#include <string>
13
14#include "base/base_export.h"
[email protected]7c7a42752012-08-09 05:14:1515#include "base/compiler_specific.h"
[email protected]5418aeb62013-03-16 09:15:5416#include "base/logging.h"
avi9b6f42932015-12-26 22:15:1417#include "base/macros.h"
[email protected]b4af2ec2012-10-05 21:29:4418#include "base/memory/scoped_ptr.h"
[email protected]7c7a42752012-08-09 05:14:1519#include "base/metrics/histogram_base.h"
[email protected]c50c21d2013-01-11 21:52:4420#include "base/metrics/sample_map.h"
[email protected]7c7a42752012-08-09 05:14:1521#include "base/synchronization/lock.h"
22
23namespace base {
24
asvitkinec0fb8022014-08-26 04:39:3525#define UMA_HISTOGRAM_SPARSE_SLOWLY(name, sample) \
[email protected]5418aeb62013-03-16 09:15:5426 do { \
asvitkinec0fb8022014-08-26 04:39:3527 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( \
28 name, base::HistogramBase::kUmaTargetedHistogramFlag); \
[email protected]5418aeb62013-03-16 09:15:5429 histogram->Add(sample); \
30 } while (0)
31
[email protected]877ef562012-10-20 02:56:1832class HistogramSamples;
33
xhwang3e9ca562015-11-06 18:50:3634class BASE_EXPORT SparseHistogram : public HistogramBase {
[email protected]7c7a42752012-08-09 05:14:1535 public:
36 // If there's one with same name, return the existing one. If not, create a
37 // new one.
avi9b6f42932015-12-26 22:15:1438 static HistogramBase* FactoryGet(const std::string& name, int32_t flags);
[email protected]7c7a42752012-08-09 05:14:1539
dcheng56488182014-10-21 10:54:5140 ~SparseHistogram() override;
[email protected]7c7a42752012-08-09 05:14:1541
[email protected]b4af2ec2012-10-05 21:29:4442 // HistogramBase implementation:
bcwhiteb036e4322015-12-10 18:36:3443 uint64_t name_hash() const override;
dcheng56488182014-10-21 10:54:5144 HistogramType GetHistogramType() const override;
45 bool HasConstructionArguments(Sample expected_minimum,
46 Sample expected_maximum,
47 size_t expected_bucket_count) const override;
48 void Add(Sample value) override;
amohammadkhan6779b5c32015-08-05 20:31:1149 void AddCount(Sample value, int count) override;
dcheng56488182014-10-21 10:54:5150 void AddSamples(const HistogramSamples& samples) override;
brettw05cfd8ddb2015-06-02 07:02:4751 bool AddSamplesFromPickle(base::PickleIterator* iter) override;
dcheng56488182014-10-21 10:54:5152 scoped_ptr<HistogramSamples> SnapshotSamples() const override;
53 void WriteHTMLGraph(std::string* output) const override;
54 void WriteAscii(std::string* output) const override;
[email protected]c50c21d2013-01-11 21:52:4455
56 protected:
57 // HistogramBase implementation:
brettw05cfd8ddb2015-06-02 07:02:4758 bool SerializeInfoImpl(base::Pickle* pickle) const override;
[email protected]b4af2ec2012-10-05 21:29:4459
60 private:
[email protected]7c7a42752012-08-09 05:14:1561 // Clients should always use FactoryGet to create SparseHistogram.
[email protected]f3c697c52013-01-15 10:52:1162 explicit SparseHistogram(const std::string& name);
[email protected]7c7a42752012-08-09 05:14:1563
xhwang3e9ca562015-11-06 18:50:3664 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo(
brettw05cfd8ddb2015-06-02 07:02:4765 base::PickleIterator* iter);
66 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter);
[email protected]c50c21d2013-01-11 21:52:4467
dcheng56488182014-10-21 10:54:5168 void GetParameters(DictionaryValue* params) const override;
69 void GetCountAndBucketData(Count* count,
avi9b6f42932015-12-26 22:15:1470 int64_t* sum,
dcheng56488182014-10-21 10:54:5171 ListValue* buckets) const override;
[email protected]24a7ec52012-10-08 10:31:5072
[email protected]f2bb3202013-04-05 21:21:5473 // Helpers for emitting Ascii graphic. Each method appends data to output.
74 void WriteAsciiImpl(bool graph_it,
75 const std::string& newline,
76 std::string* output) const;
77
78 // Write a common header message describing this histogram.
79 void WriteAsciiHeader(const Count total_count,
80 std::string* output) const;
81
[email protected]c50c21d2013-01-11 21:52:4482 // For constuctor calling.
83 friend class SparseHistogramTest;
[email protected]7c7a42752012-08-09 05:14:1584
[email protected]c50c21d2013-01-11 21:52:4485 // Protects access to |samples_|.
[email protected]7c7a42752012-08-09 05:14:1586 mutable base::Lock lock_;
[email protected]c50c21d2013-01-11 21:52:4487
88 SampleMap samples_;
[email protected]7c7a42752012-08-09 05:14:1589
90 DISALLOW_COPY_AND_ASSIGN(SparseHistogram);
91};
92
93} // namespace base
94
95#endif // BASE_METRICS_SPARSE_HISTOGRAM_H_