blob: 12e934fb9db895359d2d4e07c06ab438a1cce4a2 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]7c7a42752012-08-09 05:14:152// 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"
[email protected]7c7a42752012-08-09 05:14:1516#include "base/metrics/histogram_base.h"
nikunjbddaa36a2016-10-19 04:12:5517#include "base/metrics/histogram_samples.h"
[email protected]7c7a42752012-08-09 05:14:1518#include "base/synchronization/lock.h"
Jun Kokatsu505af9f2020-05-05 11:59:4719#include "base/values.h"
[email protected]7c7a42752012-08-09 05:14:1520
21namespace base {
22
[email protected]877ef562012-10-20 02:56:1823class HistogramSamples;
bcwhiteb0bb9192016-04-18 01:33:1024class PersistentHistogramAllocator;
nikunjbddaa36a2016-10-19 04:12:5525class Pickle;
26class PickleIterator;
[email protected]877ef562012-10-20 02:56:1827
xhwang3e9ca562015-11-06 18:50:3628class BASE_EXPORT SparseHistogram : public HistogramBase {
[email protected]7c7a42752012-08-09 05:14:1529 public:
30 // If there's one with same name, return the existing one. If not, create a
31 // new one.
Alexei Svitkine784ce692024-05-24 16:19:2432 static HistogramBase* FactoryGet(std::string_view name, int32_t flags);
[email protected]7c7a42752012-08-09 05:14:1533
bcwhite27f5f34e2016-04-19 21:49:1634 // Create a histogram using data in persistent storage. The allocator must
35 // live longer than the created sparse histogram.
dcheng093de9b2016-04-04 21:25:5136 static std::unique_ptr<HistogramBase> PersistentCreate(
bcwhiteb0bb9192016-04-18 01:33:1037 PersistentHistogramAllocator* allocator,
Roger McFarlane89528592025-02-20 20:50:2738 DurableStringView name,
Ramon Cano Aparicioeb4e3152025-05-09 14:26:5739 uint64_t name_hash,
bcwhite3dd85c4f2016-03-17 13:21:5640 HistogramSamples::Metadata* meta,
41 HistogramSamples::Metadata* logged_meta);
42
Peter Boström7319bbd2021-09-15 22:59:3843 SparseHistogram(const SparseHistogram&) = delete;
44 SparseHistogram& operator=(const SparseHistogram&) = delete;
45
dcheng56488182014-10-21 10:54:5146 ~SparseHistogram() override;
[email protected]7c7a42752012-08-09 05:14:1547
Alexei Svitkine784ce692024-05-24 16:19:2448 // HistogramBase:
bcwhiteb036e4322015-12-10 18:36:3449 uint64_t name_hash() const override;
dcheng56488182014-10-21 10:54:5150 HistogramType GetHistogramType() const override;
Ramon Cano Aparicio4a39d122025-01-20 13:00:1751 bool HasConstructionArguments(Sample32 expected_minimum,
52 Sample32 expected_maximum,
Peter Kastingfc94f5062022-06-08 16:41:4553 size_t expected_bucket_count) const override;
Ramon Cano Aparicio4a39d122025-01-20 13:00:1754 void Add(Sample32 value) override;
55 void AddCount(Sample32 value, int count) override;
Alexei Svitkine1e86b0c892024-10-02 21:02:0356 bool AddSamples(const HistogramSamples& samples) override;
brettw05cfd8ddb2015-06-02 07:02:4757 bool AddSamplesFromPickle(base::PickleIterator* iter) override;
dcheng093de9b2016-04-04 21:25:5158 std::unique_ptr<HistogramSamples> SnapshotSamples() const override;
Luc Nguyene01c6752022-12-01 18:40:1559 std::unique_ptr<HistogramSamples> SnapshotUnloggedSamples() const override;
60 void MarkSamplesAsLogged(const HistogramSamples& samples) override;
dcheng093de9b2016-04-04 21:25:5161 std::unique_ptr<HistogramSamples> SnapshotDelta() override;
bcwhite65e57d02016-05-13 14:39:4062 std::unique_ptr<HistogramSamples> SnapshotFinalDelta() const override;
Nathan Memmottc034fa4e2022-07-15 23:59:4363 base::Value::Dict ToGraphDict() const override;
[email protected]c50c21d2013-01-11 21:52:4464
65 protected:
Alexei Svitkine784ce692024-05-24 16:19:2466 // HistogramBase:
Daniel Cheng0d89f9222017-09-22 05:05:0767 void SerializeInfoImpl(base::Pickle* pickle) const override;
[email protected]b4af2ec2012-10-05 21:29:4468
69 private:
[email protected]7c7a42752012-08-09 05:14:1570 // Clients should always use FactoryGet to create SparseHistogram.
Roger McFarlane89528592025-02-20 20:50:2771 explicit SparseHistogram(DurableStringView name);
[email protected]7c7a42752012-08-09 05:14:1572
Ramon Cano Aparicioeb4e3152025-05-09 14:26:5773 // Same as above, but takes a pre-computed `name_hash`. This function is more
74 // efficient as it avoids recomputing the hash if it's already known. The
75 // `name_hash` must be the hash of `name`, this is enforced with a DCHECK.
76 SparseHistogram(DurableStringView name, uint64_t name_hash);
77
bcwhiteb0bb9192016-04-18 01:33:1078 SparseHistogram(PersistentHistogramAllocator* allocator,
Roger McFarlane89528592025-02-20 20:50:2779 DurableStringView name,
Ramon Cano Aparicioeb4e3152025-05-09 14:26:5780 uint64_t name_hash,
bcwhite3dd85c4f2016-03-17 13:21:5681 HistogramSamples::Metadata* meta,
82 HistogramSamples::Metadata* logged_meta);
83
xhwang3e9ca562015-11-06 18:50:3684 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo(
brettw05cfd8ddb2015-06-02 07:02:4785 base::PickleIterator* iter);
86 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter);
[email protected]c50c21d2013-01-11 21:52:4487
Weilun Shif07fe532020-06-11 03:21:5488 // Writes the type of the sparse histogram in the |params|.
Nathan Memmottc034fa4e2022-07-15 23:59:4389 Value::Dict GetParameters() const override;
[email protected]24a7ec52012-10-08 10:31:5090
Weilun Shif07fe532020-06-11 03:21:5491 // For constructor calling.
[email protected]c50c21d2013-01-11 21:52:4492 friend class SparseHistogramTest;
Luc Nguyen6458a5c2023-03-31 20:59:1793 friend class HistogramThreadsafeTest;
[email protected]7c7a42752012-08-09 05:14:1594
[email protected]c50c21d2013-01-11 21:52:4495 // Protects access to |samples_|.
[email protected]7c7a42752012-08-09 05:14:1596 mutable base::Lock lock_;
[email protected]c50c21d2013-01-11 21:52:4497
bcwhite65e57d02016-05-13 14:39:4098 // Flag to indicate if PrepareFinalDelta has been previously called.
99 mutable bool final_delta_created_ = false;
100
altimin498c8382017-05-12 17:49:18101 std::unique_ptr<HistogramSamples> unlogged_samples_;
dcheng093de9b2016-04-04 21:25:51102 std::unique_ptr<HistogramSamples> logged_samples_;
[email protected]7c7a42752012-08-09 05:14:15103};
104
105} // namespace base
106
107#endif // BASE_METRICS_SPARSE_HISTOGRAM_H_