[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 | |
bcwhite | 9475145 | 2017-05-09 04:01:17 | [diff] [blame] | 10 | #include <atomic> |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 11 | #include <map> |
| 12 | #include <string> |
bcwhite | 34c6bbf | 2016-02-19 22:14:46 | [diff] [blame] | 13 | #include <vector> |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 14 | |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 15 | #include "base/gtest_prod_util.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 17 | #include "base/metrics/histogram_base.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. |
François Degros | ecd34d4 | 2017-12-19 01:58:49 | [diff] [blame] | 31 | class BASE_EXPORT HistogramSnapshotManager final { |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 32 | public: |
| 33 | explicit HistogramSnapshotManager(HistogramFlattener* histogram_flattener); |
François Degros | ecd34d4 | 2017-12-19 01:58:49 | [diff] [blame] | 34 | ~HistogramSnapshotManager(); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 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|. |
François Degros | ecd34d4 | 2017-12-19 01:58:49 | [diff] [blame] | 42 | void PrepareDeltas(const std::vector<HistogramBase*>& histograms, |
bcwhite | 8373bd3 | 2016-07-13 02:49:11 | [diff] [blame] | 43 | HistogramBase::Flags flags_to_set, |
François Degros | 8329d67 | 2018-01-30 23:53:16 | [diff] [blame] | 44 | HistogramBase::Flags required_flags); |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 45 | |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 46 | // When the collection is not so simple as can be done using a single |
| 47 | // iterator, the steps can be performed separately. Call PerpareDelta() |
bcwhite | 8373bd3 | 2016-07-13 02:49:11 | [diff] [blame] | 48 | // 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. |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 51 | void PrepareDelta(HistogramBase* histogram); |
bcwhite | 8373bd3 | 2016-07-13 02:49:11 | [diff] [blame] | 52 | void PrepareFinalDelta(const HistogramBase* histogram); |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 53 | |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 54 | private: |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 55 | FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge); |
| 56 | |
| 57 | // During a snapshot, samples are acquired and aggregated. This structure |
bcwhite | 8373bd3 | 2016-07-13 02:49:11 | [diff] [blame] | 58 | // contains all the information for a given histogram that persists between |
| 59 | // collections. |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 60 | struct SampleInfo { |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 61 | // The set of inconsistencies (flags) already seen for the histogram. |
| 62 | // See HistogramBase::Inconsistency for values. |
bcwhite | 34c6bbf | 2016-02-19 22:14:46 | [diff] [blame] | 63 | uint32_t inconsistencies = 0; |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 64 | }; |
| 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, |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 69 | std::unique_ptr<HistogramSamples> samples); |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 70 | |
bcwhite | 9475145 | 2017-05-09 04:01:17 | [diff] [blame] | 71 | // |histogram_flattener_| handles the logistics of recording the histogram |
| 72 | // deltas. |
| 73 | HistogramFlattener* const histogram_flattener_; // Weak. |
| 74 | |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 75 | // For histograms, track what has been previously seen, indexed |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 76 | // by the hash of the histogram name. |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 77 | std::map<uint64_t, SampleInfo> known_histograms_; |
| 78 | |
bcwhite | 9475145 | 2017-05-09 04:01:17 | [diff] [blame] | 79 | // 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_; |
bcwhite | 1c8b1fb1 | 2017-04-24 20:58:35 | [diff] [blame] | 84 | |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 85 | DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager); |
| 86 | }; |
| 87 | |
| 88 | } // namespace base |
| 89 | |
| 90 | #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ |