1 // Copyright 2012 The Chromium Authors 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 <memory> 13 #include <vector> 14 15 #include "base/base_export.h" 16 #include "base/gtest_prod_util.h" 17 #include "base/memory/raw_ptr.h" 18 #include "base/metrics/histogram_base.h" 19 20 namespace base { 21 22 class HistogramSamples; 23 class HistogramFlattener; 24 25 // HistogramSnapshotManager handles the logistics of gathering up available 26 // histograms for recording either to disk or for transmission (such as from 27 // renderer to browser, or from browser to UMA upload). Since histograms can sit 28 // in memory for an extended period of time, and are vulnerable to memory 29 // corruption, this class also validates as much redundancy as it can before 30 // calling for the marginal change (a.k.a., delta) in a histogram to be 31 // recorded. 32 class BASE_EXPORT HistogramSnapshotManager final { 33 public: 34 explicit HistogramSnapshotManager(HistogramFlattener* histogram_flattener); 35 36 HistogramSnapshotManager(const HistogramSnapshotManager&) = delete; 37 HistogramSnapshotManager& operator=(const HistogramSnapshotManager&) = delete; 38 39 ~HistogramSnapshotManager(); 40 41 // Snapshots all histograms and asks |histogram_flattener_| to record the 42 // delta. |flags_to_set| is used to set flags for each histogram. 43 // |required_flags| is used to select which histograms to record. Only 44 // histograms with all of the required flags are selected. If all histograms 45 // should be recorded, use |Histogram::kNoFlags| as the required flag. 46 void PrepareDeltas(const std::vector<HistogramBase*>& histograms, 47 HistogramBase::Flags flags_to_set, 48 HistogramBase::Flags required_flags); 49 50 // Same as PrepareDeltas() above, but the samples obtained from the histograms 51 // are not immediately marked as logged. Instead, they are stored internally 52 // in |histograms_and_snapshots_|, and a call to MarkUnloggedSamplesAsLogged() 53 // should be made subsequently in order to mark them as logged. 54 void SnapshotUnloggedSamples(const std::vector<HistogramBase*>& histograms, 55 HistogramBase::Flags required_flags); 56 57 // Marks the unlogged samples obtained from SnapshotUnloggedSamples() as 58 // logged. For each call to this function, there should be a corresponding 59 // call to SnapshotUnloggedSamples() before it. 60 void MarkUnloggedSamplesAsLogged(); 61 62 // When the collection is not so simple as can be done using a single 63 // iterator, the steps can be performed separately. Call PerpareDelta() 64 // as many times as necessary. PrepareFinalDelta() works like PrepareDelta() 65 // except that it does not update the previous logged values and can thus 66 // be used with read-only files. 67 void PrepareDelta(HistogramBase* histogram); 68 void PrepareFinalDelta(const HistogramBase* histogram); 69 70 private: 71 FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge); 72 73 using HistogramSnapshotPair = 74 std::pair<HistogramBase*, std::unique_ptr<HistogramSamples>>; 75 76 // Capture and hold samples from a histogram. This does all the heavy 77 // lifting for PrepareDelta() and PrepareFinalDelta(). 78 void PrepareSamples(const HistogramBase* histogram, 79 const HistogramSamples& samples); 80 81 // A list of histograms and snapshots of unlogged samples. Filled when calling 82 // SnapshotUnloggedSamples(). They are marked as logged when calling 83 // MarkUnloggedSamplesAsLogged(). 84 std::vector<HistogramSnapshotPair> histograms_and_snapshots_; 85 86 // Keeps track of whether SnapshotUnloggedSamples() has been called. This 87 // resets back to false after calling MarkUnloggedSamplesAsLogged(), so that 88 // the same HistogramSnapshotManager instance can be used to take multiple 89 // snapshots if needed. 90 bool unlogged_samples_snapshot_taken_ = false; 91 92 // |histogram_flattener_| handles the logistics of recording the histogram 93 // deltas. 94 const raw_ptr<HistogramFlattener> histogram_flattener_; // Weak. 95 }; 96 97 } // namespace base 98 99 #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ 100