Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: base/metrics/persistent_histogram_allocator.h

Issue 1738063002: Refactor histogram_persistence to be a class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: refactored (again) into PersistentHistogramAllocator class Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016 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_PERSISTENCE_H_
6 #define BASE_METRICS_HISTOGRAM_PERSISTENCE_H_
7
8 #include "base/atomicops.h"
9 #include "base/base_export.h"
10 #include "base/feature_list.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/histogram_base.h"
13 #include "base/metrics/persistent_memory_allocator.h"
14 #include "base/strings/string_piece.h"
15
16 namespace base {
17
18 // Feature definition for enabling histogram persistence.
19 BASE_EXPORT extern const Feature kPersistentHistogramsFeature;
20
21 // This class manages histograms created within a PersistentMemoryAllocator.
22 class BASE_EXPORT PersistentHistogramAllocator {
23 public:
24 // This iterator is used for fetching persistent histograms from an allocator.
25 class Iterator {
26 public:
27 bool is_clear() { return memory_iter.is_clear(); }
28
29 private:
30 friend class PersistentHistogramAllocator;
31
32 // The iterator used for stepping through persistent memory iterables.
33 PersistentMemoryAllocator::Iterator memory_iter;
34 };
35
36 using Reference = PersistentMemoryAllocator::Reference;
37
38 // A PersistentHistogramAllocator is constructed from a PersistentMemory-
39 // Allocator object of which it takes ownership.
40 PersistentHistogramAllocator(scoped_ptr<PersistentMemoryAllocator> memory);
41 ~PersistentHistogramAllocator();
42
43 // Access underlying memory allocator.
44 PersistentMemoryAllocator* memory_allocator() {
45 return memory_allocator_.get();
46 }
47
48 // Recreate a Histogram from data held in persistent memory. Though this
49 // object will be local to the current process, the sample data will be
50 // shared with all other threads referencing it. This method takes a |ref|
51 // to where the top-level histogram data may be found in this allocator.
52 // This method will return NULL if any problem is detected with the data.
53 scoped_ptr<HistogramBase> GetHistogram(Reference ref);
54
55 // Get the next histogram in persistent data based on iterator.
56 scoped_ptr<HistogramBase> GetNextHistogram(Iterator* iter) {
57 return GetNextHistogramWithIgnore(iter, 0);
58 }
59
60 // Create an iterator for going through all histograms in an allocator.
61 void CreateIterator(Iterator* iter);
62
63 // Allocate a new persistent histogram. The returned histogram will not
64 // be able to be located by other allocators until it is "finalized".
65 scoped_ptr<HistogramBase> AllocateHistogram(
66 HistogramType histogram_type,
67 const std::string& name,
68 int minimum,
69 int maximum,
70 const BucketRanges* bucket_ranges,
71 int32_t flags,
72 Reference* ref_ptr);
73
74 // Finalize the creation of the histogram, making it available to other
75 // processes if |register| is True, forgetting it otherwise.
76 void FinalizeHistogram(Reference ref, bool register);
77
78 // Create and update any internal histograms.
79 void CreateTrackingHistograms(StringPiece name);
80 void UpdateTrackingHistograms();
81
82 // Manage a PersistentHistogramAllocator for globally storing histograms in
83 // a space that can be persisted or shared between processes. There is only
84 // ever one allocator for all such histograms created by a single process.
85 // This takes ownership of the object and should be called as soon as
86 // possible during startup to capture as many histograms as possible and
87 // while operating single-threaded so there are no race-conditions.
88 static void SetGlobalAllocator(
89 scoped_ptr<PersistentHistogramAllocator> allocator);
90 static PersistentHistogramAllocator* GetGlobalAllocator();
91
92 // This access to the persistent allocator is only for testing; it extracts
93 // the current allocator completely. This allows easy creation of histograms
94 // within persistent memory segments which can then be extracted and used
95 // in other ways.
96 static scoped_ptr<PersistentHistogramAllocator>
97 ReleaseGlobalAllocatorForTesting();
98
99 // Import new histograms from the global PersistentHistogramAllocator. It's
100 // possible for other processes to create histograms in the active memory
101 // segment; this adds those to the internal list of known histograms to
102 // avoid creating duplicates that would have to be merged during reporting.
103 // Every call to this method resumes from the last entry it saw so it costs
104 // nothing if nothing new has been added.
105 static void ImportGlobalHistograms();
106
107 // Histogram containing creation results. Visible for testing.
108 static HistogramBase* GetCreateHistogramResultHistogram();
109
110 private:
111 // Enumerate possible creation results for reporting.
112 enum CreateHistogramResultType {
113 // Everything was fine.
114 CREATE_HISTOGRAM_SUCCESS = 0,
115
116 // Pointer to metadata was not valid.
117 CREATE_HISTOGRAM_INVALID_METADATA_POINTER,
118
119 // Histogram metadata was not valid.
120 CREATE_HISTOGRAM_INVALID_METADATA,
121
122 // Ranges information was not valid.
123 CREATE_HISTOGRAM_INVALID_RANGES_ARRAY,
124
125 // Counts information was not valid.
126 CREATE_HISTOGRAM_INVALID_COUNTS_ARRAY,
127
128 // Could not allocate histogram memory due to corruption.
129 CREATE_HISTOGRAM_ALLOCATOR_CORRUPT,
130
131 // Could not allocate histogram memory due to lack of space.
132 CREATE_HISTOGRAM_ALLOCATOR_FULL,
133
134 // Could not allocate histogram memory due to unknown error.
135 CREATE_HISTOGRAM_ALLOCATOR_ERROR,
136
137 // Histogram was of unknown type.
138 CREATE_HISTOGRAM_UNKNOWN_TYPE,
139
140 // Instance has detected a corrupt allocator (recorded only once).
141 CREATE_HISTOGRAM_ALLOCATOR_NEWLY_CORRUPT,
142
143 // Always keep this at the end.
144 CREATE_HISTOGRAM_MAX
145 };
146
147 // The structure used to hold histogram data in persistent memory. It is
148 // defined and used entirely within the .cc file.
149 struct PersistentHistogramData;
150
151 // Get the next histogram in persistent data based on iterator while
152 // ignoring a particular reference if it is found.
153 scoped_ptr<HistogramBase> GetNextHistogramWithIgnore(
154 Iterator* iter,
155 Reference ignore);
156
157 // Create a histogram based on saved (persistent) information about it.
158 scoped_ptr<HistogramBase> CreateHistogram(
159 PersistentHistogramData* histogram_data_ptr);
160
161 // Record the result of a histogram creation.
162 static void RecordCreateHistogramResult(CreateHistogramResultType result);
163
164 // The memory allocator that provides the actual histogram storage.
165 scoped_ptr<PersistentMemoryAllocator> memory_allocator_;
166
167 // A reference to the last-created histogram in the allocator, used to avoid
168 // trying to import what was just created.
169 subtle::AtomicWord last_created_ = 0;
170
171 // The current globally-active persistent allocator for new histograms.
172 static PersistentHistogramAllocator* g_allocator_;
173
174 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocator);
175 };
176
177 } // namespace base
178
179 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698