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 8 #include <stdint.h> 9 10 #include <atomic> 11 #include <map> 12 #include <string> 13 #include <vector> 14 15 #include "base/gtest_prod_util.h" 16 #include "base/macros.h" 17 #include "base/metrics/histogram_base.h" 18 19 namespace base { 20 21 class HistogramSamples; 22 class HistogramFlattener; 23 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 28 // corruption, this class also validates as much redundancy as it can before 29 // calling for the marginal change (a.k.a., delta) in a histogram to be 30 // recorded. 31 class BASE_EXPORT HistogramSnapshotManager final { 32 public: 33 explicit HistogramSnapshotManager(HistogramFlattener* histogram_flattener); 34 ~HistogramSnapshotManager(); 35 36 // Snapshot all histograms, and ask |histogram_flattener_| to record the 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 41 // |Histogram::kNoFlags|. 42 void PrepareDeltas(const std::vector<HistogramBase*>& histograms, 43 HistogramBase::Flags flags_to_set, 44 HistogramBase::Flags required_flags); 45 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() 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. 51 void PrepareDelta(HistogramBase* histogram); 52 void PrepareFinalDelta(const HistogramBase* histogram); 53 54 private: 55 FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge); 56 57 // During a snapshot, samples are acquired and aggregated. This structure 58 // contains all the information for a given histogram that persists between 59 // collections. 60 struct SampleInfo { 61 // The set of inconsistencies (flags) already seen for the histogram. 62 // See HistogramBase::Inconsistency for values. 63 uint32_t inconsistencies = 0; 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, 69 std::unique_ptr<HistogramSamples> samples); 70 71 // |histogram_flattener_| handles the logistics of recording the histogram 72 // deltas. 73 HistogramFlattener* const histogram_flattener_; // Weak. 74 75 // For histograms, track what has been previously seen, indexed 76 // by the hash of the histogram name. 77 std::map<uint64_t, SampleInfo> known_histograms_; 78 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_; 84 85 DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager); 86 }; 87 88 } // namespace base 89 90 #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ 91