Home | History | Annotate | Download | only in metrics
      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 <map>
     11 #include <string>
     12 #include <vector>
     13 
     14 #include "base/gtest_prod_util.h"
     15 #include "base/macros.h"
     16 #include "base/metrics/histogram_base.h"
     17 
     18 namespace base {
     19 
     20 class HistogramSamples;
     21 class HistogramFlattener;
     22 
     23 // HistogramSnapshotManager handles the logistics of gathering up available
     24 // histograms for recording either to disk or for transmission (such as from
     25 // renderer to browser, or from browser to UMA upload). Since histograms can sit
     26 // in memory for an extended period of time, and are vulnerable to memory
     27 // corruption, this class also validates as much redundancy as it can before
     28 // calling for the marginal change (a.k.a., delta) in a histogram to be
     29 // recorded.
     30 class BASE_EXPORT HistogramSnapshotManager {
     31  public:
     32   explicit HistogramSnapshotManager(HistogramFlattener* histogram_flattener);
     33   virtual ~HistogramSnapshotManager();
     34 
     35   // Snapshot all histograms, and ask |histogram_flattener_| to record the
     36   // delta. |flags_to_set| is used to set flags for each histogram.
     37   // |required_flags| is used to select histograms to be recorded.
     38   // Only histograms that have all the flags specified by the argument will be
     39   // chosen. If all histograms should be recorded, set it to
     40   // |Histogram::kNoFlags|.
     41   template <class ForwardHistogramIterator>
     42   void PrepareDeltas(ForwardHistogramIterator begin,
     43                      ForwardHistogramIterator end,
     44                      HistogramBase::Flags flags_to_set,
     45                      HistogramBase::Flags required_flags) {
     46     for (ForwardHistogramIterator it = begin; it != end; ++it) {
     47       (*it)->SetFlags(flags_to_set);
     48       if (((*it)->flags() & required_flags) == required_flags)
     49         PrepareDelta(*it);
     50     }
     51   }
     52 
     53   // When the collection is not so simple as can be done using a single
     54   // iterator, the steps can be performed separately. Call PerpareDelta()
     55   // as many times as necessary. PrepareFinalDelta() works like PrepareDelta()
     56   // except that it does not update the previous logged values and can thus
     57   // be used with read-only files.
     58   void PrepareDelta(HistogramBase* histogram);
     59   void PrepareFinalDelta(const HistogramBase* histogram);
     60 
     61  private:
     62   FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge);
     63 
     64   // During a snapshot, samples are acquired and aggregated. This structure
     65   // contains all the information for a given histogram that persists between
     66   // collections.
     67   struct SampleInfo {
     68     // The set of inconsistencies (flags) already seen for the histogram.
     69     // See HistogramBase::Inconsistency for values.
     70     uint32_t inconsistencies = 0;
     71   };
     72 
     73   // Capture and hold samples from a histogram. This does all the heavy
     74   // lifting for PrepareDelta() and PrepareAbsolute().
     75   void PrepareSamples(const HistogramBase* histogram,
     76                       std::unique_ptr<HistogramSamples> samples);
     77 
     78   // Try to detect and fix count inconsistency of logged samples.
     79   void InspectLoggedSamplesInconsistency(
     80       const HistogramSamples& new_snapshot,
     81       HistogramSamples* logged_samples);
     82 
     83   // For histograms, track what has been previously seen, indexed
     84   // by the hash of the histogram name.
     85   std::map<uint64_t, SampleInfo> known_histograms_;
     86 
     87   // |histogram_flattener_| handles the logistics of recording the histogram
     88   // deltas.
     89   HistogramFlattener* histogram_flattener_;  // Weak.
     90 
     91   DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager);
     92 };
     93 
     94 }  // namespace base
     95 
     96 #endif  // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_
     97