blob: ab1d915706388a9d6e817b93e9ca591d0c527f9e [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>
dcheng093de9b2016-04-04 21:25:5112#include <memory>
[email protected]7c7a42752012-08-09 05:14:1513#include <string>
14
15#include "base/base_export.h"
avi9b6f42932015-12-26 22:15:1416#include "base/macros.h"
[email protected]7c7a42752012-08-09 05:14:1517#include "base/metrics/histogram_base.h"
nikunjbddaa36a2016-10-19 04:12:5518#include "base/metrics/histogram_samples.h"
[email protected]7c7a42752012-08-09 05:14:1519#include "base/synchronization/lock.h"
Jun Kokatsu505af9f2020-05-05 11:59:4720#include "base/values.h"
[email protected]7c7a42752012-08-09 05:14:1521
22namespace base {
23
[email protected]877ef562012-10-20 02:56:1824class HistogramSamples;
bcwhiteb0bb9192016-04-18 01:33:1025class PersistentHistogramAllocator;
nikunjbddaa36a2016-10-19 04:12:5526class Pickle;
27class PickleIterator;
[email protected]877ef562012-10-20 02:56:1828
xhwang3e9ca562015-11-06 18:50:3629class BASE_EXPORT SparseHistogram : public HistogramBase {
[email protected]7c7a42752012-08-09 05:14:1530 public:
31 // If there's one with same name, return the existing one. If not, create a
32 // new one.
avi9b6f42932015-12-26 22:15:1433 static HistogramBase* FactoryGet(const std::string& name, int32_t flags);
[email protected]7c7a42752012-08-09 05:14:1534
bcwhite27f5f34e2016-04-19 21:49:1635 // Create a histogram using data in persistent storage. The allocator must
36 // live longer than the created sparse histogram.
dcheng093de9b2016-04-04 21:25:5137 static std::unique_ptr<HistogramBase> PersistentCreate(
bcwhiteb0bb9192016-04-18 01:33:1038 PersistentHistogramAllocator* allocator,
Brian Whited1c91082017-11-03 14:46:4239 const char* name,
bcwhite3dd85c4f2016-03-17 13:21:5640 HistogramSamples::Metadata* meta,
41 HistogramSamples::Metadata* logged_meta);
42
dcheng56488182014-10-21 10:54:5143 ~SparseHistogram() override;
[email protected]7c7a42752012-08-09 05:14:1544
[email protected]b4af2ec2012-10-05 21:29:4445 // HistogramBase implementation:
bcwhiteb036e4322015-12-10 18:36:3446 uint64_t name_hash() const override;
dcheng56488182014-10-21 10:54:5147 HistogramType GetHistogramType() const override;
48 bool HasConstructionArguments(Sample expected_minimum,
49 Sample expected_maximum,
jam1eacd7e2016-02-08 22:48:1650 uint32_t expected_bucket_count) const override;
dcheng56488182014-10-21 10:54:5151 void Add(Sample value) override;
amohammadkhan6779b5c32015-08-05 20:31:1152 void AddCount(Sample value, int count) override;
dcheng56488182014-10-21 10:54:5153 void AddSamples(const HistogramSamples& samples) override;
brettw05cfd8ddb2015-06-02 07:02:4754 bool AddSamplesFromPickle(base::PickleIterator* iter) override;
dcheng093de9b2016-04-04 21:25:5155 std::unique_ptr<HistogramSamples> SnapshotSamples() const override;
56 std::unique_ptr<HistogramSamples> SnapshotDelta() override;
bcwhite65e57d02016-05-13 14:39:4057 std::unique_ptr<HistogramSamples> SnapshotFinalDelta() const override;
dcheng56488182014-10-21 10:54:5158 void WriteAscii(std::string* output) const override;
Jun Kokatsu505af9f2020-05-05 11:59:4759 base::DictionaryValue ToGraphDict() const override;
[email protected]c50c21d2013-01-11 21:52:4460
61 protected:
62 // HistogramBase implementation:
Daniel Cheng0d89f9222017-09-22 05:05:0763 void SerializeInfoImpl(base::Pickle* pickle) const override;
[email protected]b4af2ec2012-10-05 21:29:4464
65 private:
[email protected]7c7a42752012-08-09 05:14:1566 // Clients should always use FactoryGet to create SparseHistogram.
Brian Whited1c91082017-11-03 14:46:4267 explicit SparseHistogram(const char* name);
[email protected]7c7a42752012-08-09 05:14:1568
bcwhiteb0bb9192016-04-18 01:33:1069 SparseHistogram(PersistentHistogramAllocator* allocator,
Brian Whited1c91082017-11-03 14:46:4270 const char* name,
bcwhite3dd85c4f2016-03-17 13:21:5671 HistogramSamples::Metadata* meta,
72 HistogramSamples::Metadata* logged_meta);
73
xhwang3e9ca562015-11-06 18:50:3674 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo(
brettw05cfd8ddb2015-06-02 07:02:4775 base::PickleIterator* iter);
76 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter);
[email protected]c50c21d2013-01-11 21:52:4477
dcheng56488182014-10-21 10:54:5178 void GetParameters(DictionaryValue* params) const override;
79 void GetCountAndBucketData(Count* count,
avi9b6f42932015-12-26 22:15:1480 int64_t* sum,
dcheng56488182014-10-21 10:54:5181 ListValue* buckets) const override;
[email protected]24a7ec52012-10-08 10:31:5082
[email protected]f2bb3202013-04-05 21:21:5483 // Helpers for emitting Ascii graphic. Each method appends data to output.
Andrew Solovey4b8ee3d2020-01-27 14:22:4284 void WriteAsciiBody(const HistogramSamples& snapshot,
85 bool graph_it,
[email protected]f2bb3202013-04-05 21:21:5486 const std::string& newline,
87 std::string* output) const;
88
89 // Write a common header message describing this histogram.
Andrew Solovey4b8ee3d2020-01-27 14:22:4290 void WriteAsciiHeader(const HistogramSamples& snapshot,
[email protected]f2bb3202013-04-05 21:21:5491 std::string* output) const;
92
[email protected]c50c21d2013-01-11 21:52:4493 // For constuctor calling.
94 friend class SparseHistogramTest;
[email protected]7c7a42752012-08-09 05:14:1595
[email protected]c50c21d2013-01-11 21:52:4496 // Protects access to |samples_|.
[email protected]7c7a42752012-08-09 05:14:1597 mutable base::Lock lock_;
[email protected]c50c21d2013-01-11 21:52:4498
bcwhite65e57d02016-05-13 14:39:4099 // Flag to indicate if PrepareFinalDelta has been previously called.
100 mutable bool final_delta_created_ = false;
101
altimin498c8382017-05-12 17:49:18102 std::unique_ptr<HistogramSamples> unlogged_samples_;
dcheng093de9b2016-04-04 21:25:51103 std::unique_ptr<HistogramSamples> logged_samples_;
[email protected]7c7a42752012-08-09 05:14:15104
105 DISALLOW_COPY_AND_ASSIGN(SparseHistogram);
106};
107
108} // namespace base
109
110#endif // BASE_METRICS_SPARSE_HISTOGRAM_H_