1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <stdint.h> 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #include <atomic> 11*635a8641SAndroid Build Coastguard Worker #include <map> 12*635a8641SAndroid Build Coastguard Worker #include <string> 13*635a8641SAndroid Build Coastguard Worker #include <vector> 14*635a8641SAndroid Build Coastguard Worker 15*635a8641SAndroid Build Coastguard Worker #include "base/gtest_prod_util.h" 16*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 17*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_base.h" 18*635a8641SAndroid Build Coastguard Worker 19*635a8641SAndroid Build Coastguard Worker namespace base { 20*635a8641SAndroid Build Coastguard Worker 21*635a8641SAndroid Build Coastguard Worker class HistogramSamples; 22*635a8641SAndroid Build Coastguard Worker class HistogramFlattener; 23*635a8641SAndroid Build Coastguard Worker 24*635a8641SAndroid Build Coastguard Worker // HistogramSnapshotManager handles the logistics of gathering up available 25*635a8641SAndroid Build Coastguard Worker // histograms for recording either to disk or for transmission (such as from 26*635a8641SAndroid Build Coastguard Worker // renderer to browser, or from browser to UMA upload). Since histograms can sit 27*635a8641SAndroid Build Coastguard Worker // in memory for an extended period of time, and are vulnerable to memory 28*635a8641SAndroid Build Coastguard Worker // corruption, this class also validates as much redundancy as it can before 29*635a8641SAndroid Build Coastguard Worker // calling for the marginal change (a.k.a., delta) in a histogram to be 30*635a8641SAndroid Build Coastguard Worker // recorded. 31*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT HistogramSnapshotManager final { 32*635a8641SAndroid Build Coastguard Worker public: 33*635a8641SAndroid Build Coastguard Worker explicit HistogramSnapshotManager(HistogramFlattener* histogram_flattener); 34*635a8641SAndroid Build Coastguard Worker ~HistogramSnapshotManager(); 35*635a8641SAndroid Build Coastguard Worker 36*635a8641SAndroid Build Coastguard Worker // Snapshot all histograms, and ask |histogram_flattener_| to record the 37*635a8641SAndroid Build Coastguard Worker // delta. |flags_to_set| is used to set flags for each histogram. 38*635a8641SAndroid Build Coastguard Worker // |required_flags| is used to select histograms to be recorded. 39*635a8641SAndroid Build Coastguard Worker // Only histograms that have all the flags specified by the argument will be 40*635a8641SAndroid Build Coastguard Worker // chosen. If all histograms should be recorded, set it to 41*635a8641SAndroid Build Coastguard Worker // |Histogram::kNoFlags|. 42*635a8641SAndroid Build Coastguard Worker void PrepareDeltas(const std::vector<HistogramBase*>& histograms, 43*635a8641SAndroid Build Coastguard Worker HistogramBase::Flags flags_to_set, 44*635a8641SAndroid Build Coastguard Worker HistogramBase::Flags required_flags); 45*635a8641SAndroid Build Coastguard Worker 46*635a8641SAndroid Build Coastguard Worker // When the collection is not so simple as can be done using a single 47*635a8641SAndroid Build Coastguard Worker // iterator, the steps can be performed separately. Call PerpareDelta() 48*635a8641SAndroid Build Coastguard Worker // as many times as necessary. PrepareFinalDelta() works like PrepareDelta() 49*635a8641SAndroid Build Coastguard Worker // except that it does not update the previous logged values and can thus 50*635a8641SAndroid Build Coastguard Worker // be used with read-only files. 51*635a8641SAndroid Build Coastguard Worker void PrepareDelta(HistogramBase* histogram); 52*635a8641SAndroid Build Coastguard Worker void PrepareFinalDelta(const HistogramBase* histogram); 53*635a8641SAndroid Build Coastguard Worker 54*635a8641SAndroid Build Coastguard Worker private: 55*635a8641SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge); 56*635a8641SAndroid Build Coastguard Worker 57*635a8641SAndroid Build Coastguard Worker // During a snapshot, samples are acquired and aggregated. This structure 58*635a8641SAndroid Build Coastguard Worker // contains all the information for a given histogram that persists between 59*635a8641SAndroid Build Coastguard Worker // collections. 60*635a8641SAndroid Build Coastguard Worker struct SampleInfo { 61*635a8641SAndroid Build Coastguard Worker // The set of inconsistencies (flags) already seen for the histogram. 62*635a8641SAndroid Build Coastguard Worker // See HistogramBase::Inconsistency for values. 63*635a8641SAndroid Build Coastguard Worker uint32_t inconsistencies = 0; 64*635a8641SAndroid Build Coastguard Worker }; 65*635a8641SAndroid Build Coastguard Worker 66*635a8641SAndroid Build Coastguard Worker // Capture and hold samples from a histogram. This does all the heavy 67*635a8641SAndroid Build Coastguard Worker // lifting for PrepareDelta() and PrepareAbsolute(). 68*635a8641SAndroid Build Coastguard Worker void PrepareSamples(const HistogramBase* histogram, 69*635a8641SAndroid Build Coastguard Worker std::unique_ptr<HistogramSamples> samples); 70*635a8641SAndroid Build Coastguard Worker 71*635a8641SAndroid Build Coastguard Worker // |histogram_flattener_| handles the logistics of recording the histogram 72*635a8641SAndroid Build Coastguard Worker // deltas. 73*635a8641SAndroid Build Coastguard Worker HistogramFlattener* const histogram_flattener_; // Weak. 74*635a8641SAndroid Build Coastguard Worker 75*635a8641SAndroid Build Coastguard Worker // For histograms, track what has been previously seen, indexed 76*635a8641SAndroid Build Coastguard Worker // by the hash of the histogram name. 77*635a8641SAndroid Build Coastguard Worker std::map<uint64_t, SampleInfo> known_histograms_; 78*635a8641SAndroid Build Coastguard Worker 79*635a8641SAndroid Build Coastguard Worker // A flag indicating if a thread is currently doing an operation. This is 80*635a8641SAndroid Build Coastguard Worker // used to check against concurrent access which is not supported. A Thread- 81*635a8641SAndroid Build Coastguard Worker // Checker is not sufficient because it may be guarded by at outside lock 82*635a8641SAndroid Build Coastguard Worker // (as is the case with cronet). 83*635a8641SAndroid Build Coastguard Worker std::atomic<bool> is_active_; 84*635a8641SAndroid Build Coastguard Worker 85*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager); 86*635a8641SAndroid Build Coastguard Worker }; 87*635a8641SAndroid Build Coastguard Worker 88*635a8641SAndroid Build Coastguard Worker } // namespace base 89*635a8641SAndroid Build Coastguard Worker 90*635a8641SAndroid Build Coastguard Worker #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ 91