histogram_snapshot_manager.h 3.5 KB

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