blob: cf7c149cc125be915be8c782767cc0fe47a613be [file] [log] [blame]
[email protected]83ab4a282012-07-12 18:19:451// 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_HISTOGRAM_SNAPSHOT_MANAGER_H_
6#define BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_
7
avi9b6f42932015-12-26 22:15:148#include <stdint.h>
9
bcwhite94751452017-05-09 04:01:1710#include <atomic>
[email protected]83ab4a282012-07-12 18:19:4511#include <map>
12#include <string>
bcwhite34c6bbf2016-02-19 22:14:4613#include <vector>
[email protected]83ab4a282012-07-12 18:19:4514
bcwhitec85a1f822016-02-18 21:22:1415#include "base/gtest_prod_util.h"
avi9b6f42932015-12-26 22:15:1416#include "base/macros.h"
[email protected]cc7dec212013-03-01 03:53:2517#include "base/metrics/histogram_base.h"
[email protected]83ab4a282012-07-12 18:19:4518
19namespace base {
20
[email protected]2f7d9cd2012-09-22 03:42:1221class HistogramSamples;
22class HistogramFlattener;
23
[email protected]83ab4a282012-07-12 18:19:4524// HistogramSnapshotManager handles the logistics of gathering up available
25// histograms for recording either to disk or for transmission (such as from
26// renderer to browser, or from browser to UMA upload). Since histograms can sit
27// in memory for an extended period of time, and are vulnerable to memory
rkaplowa1ee2052016-09-27 17:12:0828// corruption, this class also validates as much redundancy as it can before
[email protected]83ab4a282012-07-12 18:19:4529// calling for the marginal change (a.k.a., delta) in a histogram to be
30// recorded.
François Degrosecd34d42017-12-19 01:58:4931class BASE_EXPORT HistogramSnapshotManager final {
[email protected]83ab4a282012-07-12 18:19:4532 public:
33 explicit HistogramSnapshotManager(HistogramFlattener* histogram_flattener);
François Degrosecd34d42017-12-19 01:58:4934 ~HistogramSnapshotManager();
[email protected]83ab4a282012-07-12 18:19:4535
36 // Snapshot all histograms, and ask |histogram_flattener_| to record the
[email protected]c778687a2014-02-11 14:46:4537 // delta. |flags_to_set| is used to set flags for each histogram.
38 // |required_flags| is used to select histograms to be recorded.
39 // Only histograms that have all the flags specified by the argument will be
40 // chosen. If all histograms should be recorded, set it to
bcwhite8373bd32016-07-13 02:49:1141 // |Histogram::kNoFlags|.
François Degrosecd34d42017-12-19 01:58:4942 void PrepareDeltas(const std::vector<HistogramBase*>& histograms,
bcwhite8373bd32016-07-13 02:49:1143 HistogramBase::Flags flags_to_set,
François Degros8329d672018-01-30 23:53:1644 HistogramBase::Flags required_flags);
bcwhite65e57d02016-05-13 14:39:4045
bcwhitec85a1f822016-02-18 21:22:1446 // When the collection is not so simple as can be done using a single
47 // iterator, the steps can be performed separately. Call PerpareDelta()
bcwhite8373bd32016-07-13 02:49:1148 // as many times as necessary. PrepareFinalDelta() works like PrepareDelta()
49 // except that it does not update the previous logged values and can thus
50 // be used with read-only files.
bcwhitec85a1f822016-02-18 21:22:1451 void PrepareDelta(HistogramBase* histogram);
bcwhite8373bd32016-07-13 02:49:1152 void PrepareFinalDelta(const HistogramBase* histogram);
bcwhitec85a1f822016-02-18 21:22:1453
[email protected]83ab4a282012-07-12 18:19:4554 private:
bcwhitec85a1f822016-02-18 21:22:1455 FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge);
56
57 // During a snapshot, samples are acquired and aggregated. This structure
bcwhite8373bd32016-07-13 02:49:1158 // contains all the information for a given histogram that persists between
59 // collections.
bcwhitec85a1f822016-02-18 21:22:1460 struct SampleInfo {
bcwhitec85a1f822016-02-18 21:22:1461 // The set of inconsistencies (flags) already seen for the histogram.
62 // See HistogramBase::Inconsistency for values.
bcwhite34c6bbf2016-02-19 22:14:4663 uint32_t inconsistencies = 0;
bcwhitec85a1f822016-02-18 21:22:1464 };
65
66 // Capture and hold samples from a histogram. This does all the heavy
67 // lifting for PrepareDelta() and PrepareAbsolute().
68 void PrepareSamples(const HistogramBase* histogram,
dcheng093de9b2016-04-04 21:25:5169 std::unique_ptr<HistogramSamples> samples);
[email protected]2f7d9cd2012-09-22 03:42:1270
bcwhite94751452017-05-09 04:01:1771 // |histogram_flattener_| handles the logistics of recording the histogram
72 // deltas.
73 HistogramFlattener* const histogram_flattener_; // Weak.
74
bcwhitec85a1f822016-02-18 21:22:1475 // For histograms, track what has been previously seen, indexed
bcwhiteb036e4322015-12-10 18:36:3476 // by the hash of the histogram name.
bcwhitec85a1f822016-02-18 21:22:1477 std::map<uint64_t, SampleInfo> known_histograms_;
78
bcwhite94751452017-05-09 04:01:1779 // A flag indicating if a thread is currently doing an operation. This is
80 // used to check against concurrent access which is not supported. A Thread-
81 // Checker is not sufficient because it may be guarded by at outside lock
82 // (as is the case with cronet).
83 std::atomic<bool> is_active_;
bcwhite1c8b1fb12017-04-24 20:58:3584
[email protected]83ab4a282012-07-12 18:19:4585 DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager);
86};
87
88} // namespace base
89
90#endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_