blob: fe26faad1138c6dbaa57f40b9d23c56eeb3ecb94 [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
[email protected]83ab4a282012-07-12 18:19:4510#include <map>
11#include <string>
bcwhite34c6bbf2016-02-19 22:14:4612#include <vector>
[email protected]83ab4a282012-07-12 18:19:4513
bcwhitec85a1f822016-02-18 21:22:1414#include "base/gtest_prod_util.h"
avi9b6f42932015-12-26 22:15:1415#include "base/macros.h"
[email protected]cc7dec212013-03-01 03:53:2516#include "base/metrics/histogram_base.h"
bcwhite1c8b1fb12017-04-24 20:58:3517#include "base/threading/thread_checker.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.
31class BASE_EXPORT HistogramSnapshotManager {
32 public:
33 explicit HistogramSnapshotManager(HistogramFlattener* histogram_flattener);
34 virtual ~HistogramSnapshotManager();
35
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|.
bcwhite65e57d02016-05-13 14:39:4042 template <class ForwardHistogramIterator>
bcwhite8373bd32016-07-13 02:49:1143 void PrepareDeltas(ForwardHistogramIterator begin,
44 ForwardHistogramIterator end,
45 HistogramBase::Flags flags_to_set,
46 HistogramBase::Flags required_flags) {
bcwhite65e57d02016-05-13 14:39:4047 for (ForwardHistogramIterator it = begin; it != end; ++it) {
48 (*it)->SetFlags(flags_to_set);
49 if (((*it)->flags() & required_flags) == required_flags)
50 PrepareDelta(*it);
51 }
52 }
53
bcwhitec85a1f822016-02-18 21:22:1454 // When the collection is not so simple as can be done using a single
55 // iterator, the steps can be performed separately. Call PerpareDelta()
bcwhite8373bd32016-07-13 02:49:1156 // as many times as necessary. PrepareFinalDelta() works like PrepareDelta()
57 // except that it does not update the previous logged values and can thus
58 // be used with read-only files.
bcwhitec85a1f822016-02-18 21:22:1459 void PrepareDelta(HistogramBase* histogram);
bcwhite8373bd32016-07-13 02:49:1160 void PrepareFinalDelta(const HistogramBase* histogram);
bcwhitec85a1f822016-02-18 21:22:1461
[email protected]83ab4a282012-07-12 18:19:4562 private:
bcwhitec85a1f822016-02-18 21:22:1463 FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge);
64
65 // During a snapshot, samples are acquired and aggregated. This structure
bcwhite8373bd32016-07-13 02:49:1166 // contains all the information for a given histogram that persists between
67 // collections.
bcwhitec85a1f822016-02-18 21:22:1468 struct SampleInfo {
bcwhitec85a1f822016-02-18 21:22:1469 // The set of inconsistencies (flags) already seen for the histogram.
70 // See HistogramBase::Inconsistency for values.
bcwhite34c6bbf2016-02-19 22:14:4671 uint32_t inconsistencies = 0;
bcwhitec85a1f822016-02-18 21:22:1472 };
73
74 // Capture and hold samples from a histogram. This does all the heavy
75 // lifting for PrepareDelta() and PrepareAbsolute().
76 void PrepareSamples(const HistogramBase* histogram,
dcheng093de9b2016-04-04 21:25:5177 std::unique_ptr<HistogramSamples> samples);
[email protected]2f7d9cd2012-09-22 03:42:1278
bcwhitec85a1f822016-02-18 21:22:1479 // For histograms, track what has been previously seen, indexed
bcwhiteb036e4322015-12-10 18:36:3480 // by the hash of the histogram name.
bcwhitec85a1f822016-02-18 21:22:1481 std::map<uint64_t, SampleInfo> known_histograms_;
82
[email protected]83ab4a282012-07-12 18:19:4583 // |histogram_flattener_| handles the logistics of recording the histogram
84 // deltas.
85 HistogramFlattener* histogram_flattener_; // Weak.
86
bcwhite1c8b1fb12017-04-24 20:58:3587 ThreadChecker thread_checker_;
88
[email protected]83ab4a282012-07-12 18:19:4589 DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager);
90};
91
92} // namespace base
93
94#endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_