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