histogram_snapshot_manager.h 3.7 KB

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