• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 #include "base/macros.h"
14 #include "base/metrics/histogram_base.h"
15 
16 namespace base {
17 
18 class HistogramSamples;
19 class HistogramFlattener;
20 
21 // HistogramSnapshotManager handles the logistics of gathering up available
22 // histograms for recording either to disk or for transmission (such as from
23 // renderer to browser, or from browser to UMA upload). Since histograms can sit
24 // in memory for an extended period of time, and are vulnerable to memory
25 // corruption, this class also validates as much rendundancy as it can before
26 // calling for the marginal change (a.k.a., delta) in a histogram to be
27 // recorded.
28 class BASE_EXPORT HistogramSnapshotManager {
29  public:
30   explicit HistogramSnapshotManager(HistogramFlattener* histogram_flattener);
31   virtual ~HistogramSnapshotManager();
32 
33   // Snapshot all histograms, and ask |histogram_flattener_| to record the
34   // delta. |flags_to_set| is used to set flags for each histogram.
35   // |required_flags| is used to select histograms to be recorded.
36   // Only histograms that have all the flags specified by the argument will be
37   // chosen. If all histograms should be recorded, set it to
38   // |Histogram::kNoFlags|.
39   void PrepareDeltas(HistogramBase::Flags flags_to_set,
40                      HistogramBase::Flags required_flags);
41 
42  private:
43   // Snapshot this histogram, and record the delta.
44   void PrepareDelta(const HistogramBase& histogram);
45 
46   // Try to detect and fix count inconsistency of logged samples.
47   void InspectLoggedSamplesInconsistency(
48       const HistogramSamples& new_snapshot,
49       HistogramSamples* logged_samples);
50 
51   // For histograms, track what we've already recorded (as a sample for
52   // each histogram) so that we can record only the delta with the next log.
53   // The information is indexed by the hash of the histogram name.
54   std::map<uint64_t, HistogramSamples*> logged_samples_;
55 
56   // Set of histograms found to be corrupt and their problems, indexed
57   // by the hash of the histogram name.
58   std::map<uint64_t, int> inconsistencies_;
59 
60   // |histogram_flattener_| handles the logistics of recording the histogram
61   // deltas.
62   HistogramFlattener* histogram_flattener_;  // Weak.
63 
64   DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager);
65 };
66 
67 }  // namespace base
68 
69 #endif  // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_
70