[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 1 | // 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 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 10 | #include <map> |
| 11 | #include <string> |
bcwhite | 34c6bbf | 2016-02-19 22:14:46 | [diff] [blame] | 12 | #include <vector> |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 13 | |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 14 | #include "base/gtest_prod_util.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 16 | #include "base/metrics/histogram_base.h" |
bcwhite | 1c8b1fb1 | 2017-04-24 20:58:35 | [diff] [blame^] | 17 | #include "base/threading/thread_checker.h" |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 18 | |
| 19 | namespace base { |
| 20 | |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 21 | class HistogramSamples; |
| 22 | class HistogramFlattener; |
| 23 | |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 24 | // 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 |
rkaplow | a1ee205 | 2016-09-27 17:12:08 | [diff] [blame] | 28 | // corruption, this class also validates as much redundancy as it can before |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 29 | // calling for the marginal change (a.k.a., delta) in a histogram to be |
| 30 | // recorded. |
| 31 | class 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] | c778687a | 2014-02-11 14:46:45 | [diff] [blame] | 37 | // 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 |
bcwhite | 8373bd3 | 2016-07-13 02:49:11 | [diff] [blame] | 41 | // |Histogram::kNoFlags|. |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 42 | template <class ForwardHistogramIterator> |
bcwhite | 8373bd3 | 2016-07-13 02:49:11 | [diff] [blame] | 43 | void PrepareDeltas(ForwardHistogramIterator begin, |
| 44 | ForwardHistogramIterator end, |
| 45 | HistogramBase::Flags flags_to_set, |
| 46 | HistogramBase::Flags required_flags) { |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 47 | 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 | |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 54 | // When the collection is not so simple as can be done using a single |
| 55 | // iterator, the steps can be performed separately. Call PerpareDelta() |
bcwhite | 8373bd3 | 2016-07-13 02:49:11 | [diff] [blame] | 56 | // 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. |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 59 | void PrepareDelta(HistogramBase* histogram); |
bcwhite | 8373bd3 | 2016-07-13 02:49:11 | [diff] [blame] | 60 | void PrepareFinalDelta(const HistogramBase* histogram); |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 61 | |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 62 | private: |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 63 | FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge); |
| 64 | |
| 65 | // During a snapshot, samples are acquired and aggregated. This structure |
bcwhite | 8373bd3 | 2016-07-13 02:49:11 | [diff] [blame] | 66 | // contains all the information for a given histogram that persists between |
| 67 | // collections. |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 68 | struct SampleInfo { |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 69 | // The set of inconsistencies (flags) already seen for the histogram. |
| 70 | // See HistogramBase::Inconsistency for values. |
bcwhite | 34c6bbf | 2016-02-19 22:14:46 | [diff] [blame] | 71 | uint32_t inconsistencies = 0; |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 72 | }; |
| 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, |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 77 | std::unique_ptr<HistogramSamples> samples); |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 78 | |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 79 | // For histograms, track what has been previously seen, indexed |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 80 | // by the hash of the histogram name. |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 81 | std::map<uint64_t, SampleInfo> known_histograms_; |
| 82 | |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 83 | // |histogram_flattener_| handles the logistics of recording the histogram |
| 84 | // deltas. |
| 85 | HistogramFlattener* histogram_flattener_; // Weak. |
| 86 | |
bcwhite | 1c8b1fb1 | 2017-04-24 20:58:35 | [diff] [blame^] | 87 | ThreadChecker thread_checker_; |
| 88 | |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 89 | DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager); |
| 90 | }; |
| 91 | |
| 92 | } // namespace base |
| 93 | |
| 94 | #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ |