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

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: addressed final review comments by Alexei 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
« no previous file with comments | « base/metrics/histogram_unittest.cc ('k') | base/metrics/persistent_histogram_allocator.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 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/memory/shared_memory.h"
13 #include "base/metrics/histogram_base.h"
14 #include "base/metrics/persistent_memory_allocator.h"
15 #include "base/strings/string_piece.h"
16
17 namespace base {
18
19 // Feature definition for enabling histogram persistence.
20 BASE_EXPORT extern const Feature kPersistentHistogramsFeature;
21
22 // This class manages histograms created within a PersistentMemoryAllocator.
23 class BASE_EXPORT PersistentHistogramAllocator {
24 public:
25 // This iterator is used for fetching persistent histograms from an allocator.
26 class Iterator {
27 public:
28 bool is_clear() { return memory_iter.is_clear(); }
29
30 private:
31 friend class PersistentHistogramAllocator;
32
33 // The iterator used for stepping through persistent memory iterables.
34 PersistentMemoryAllocator::Iterator memory_iter;
35 };
36
37 using Reference = PersistentMemoryAllocator::Reference;
38
39 // A PersistentHistogramAllocator is constructed from a PersistentMemory-
40 // Allocator object of which it takes ownership.
41 PersistentHistogramAllocator(scoped_ptr<PersistentMemoryAllocator> memory);
42 ~PersistentHistogramAllocator();
43
44 // Direct access to underlying memory allocator. If the segment is shared
45 // across threads or processes, reading data through these values does
46 // not guarantee consistency. Use with care. Do not write.
47 PersistentMemoryAllocator* memory_allocator() {
48 return memory_allocator_.get();
49 }
50
51 // Implement the "metadata" API of a PersistentMemoryAllocator, forwarding
52 // those requests to the real one.
53 uint64_t Id() const { return memory_allocator_->Id(); }
54 const char* Name() const { return memory_allocator_->Name(); }
55 const void* data() const { return memory_allocator_->data(); }
56 size_t length() const { return memory_allocator_->length(); }
57 size_t used() const { return memory_allocator_->used(); }
58
59 // Recreate a Histogram from data held in persistent memory. Though this
60 // object will be local to the current process, the sample data will be
61 // shared with all other threads referencing it. This method takes a |ref|
62 // to where the top-level histogram data may be found in this allocator.
63 // This method will return null if any problem is detected with the data.
64 scoped_ptr<HistogramBase> GetHistogram(Reference ref);
65
66 // Get the next histogram in persistent data based on iterator.
67 scoped_ptr<HistogramBase> GetNextHistogram(Iterator* iter) {
68 return GetNextHistogramWithIgnore(iter, 0);
69 }
70
71 // Create an iterator for going through all histograms in an allocator.
72 void CreateIterator(Iterator* iter);
73
74 // Allocate a new persistent histogram. The returned histogram will not
75 // be able to be located by other allocators until it is "finalized".
76 scoped_ptr<HistogramBase> AllocateHistogram(
77 HistogramType histogram_type,
78 const std::string& name,
79 int minimum,
80 int maximum,
81 const BucketRanges* bucket_ranges,
82 int32_t flags,
83 Reference* ref_ptr);
84
85 // Finalize the creation of the histogram, making it available to other
86 // processes if |registered| (as in: added to the StatisticsRecorder) is
87 // True, forgetting it otherwise.
88 void FinalizeHistogram(Reference ref, bool registered);
89
90 // Create internal histograms for tracking memory use and allocation sizes
91 // for allocator of |name| (which can simply be the result of Name()). This
92 // is done seperately from construction for situations such as when the
93 // histograms will be backed by memory provided by this very allocator.
94 //
95 // IMPORTANT: Callers must update tools/metrics/histograms/histograms.xml
96 // with the following histograms:
97 // UMA.PersistentAllocator.name.Allocs
98 // UMA.PersistentAllocator.name.UsedPct
99 void CreateTrackingHistograms(StringPiece name);
100 void UpdateTrackingHistograms();
101
102 // Manage a PersistentHistogramAllocator for globally storing histograms in
103 // a space that can be persisted or shared between processes. There is only
104 // ever one allocator for all such histograms created by a single process.
105 // This takes ownership of the object and should be called as soon as
106 // possible during startup to capture as many histograms as possible and
107 // while operating single-threaded so there are no race-conditions.
108 static void SetGlobalAllocator(
109 scoped_ptr<PersistentHistogramAllocator> allocator);
110 static PersistentHistogramAllocator* GetGlobalAllocator();
111
112 // This access to the persistent allocator is only for testing; it extracts
113 // the current allocator completely. This allows easy creation of histograms
114 // within persistent memory segments which can then be extracted and used
115 // in other ways.
116 static scoped_ptr<PersistentHistogramAllocator>
117 ReleaseGlobalAllocatorForTesting();
118
119 // These helper methods perform SetGlobalAllocator() calls with allocators
120 // of the specified type and parameters.
121 static void CreateGlobalAllocatorOnPersistentMemory(
122 void* base,
123 size_t size,
124 size_t page_size,
125 uint64_t id,
126 StringPiece name);
127 static void CreateGlobalAllocatorOnLocalMemory(
128 size_t size,
129 uint64_t id,
130 StringPiece name);
131 static void CreateGlobalAllocatorOnSharedMemory(
132 size_t size,
133 const SharedMemoryHandle& handle);
134
135 // Import new histograms from the global PersistentHistogramAllocator. It's
136 // possible for other processes to create histograms in the active memory
137 // segment; this adds those to the internal list of known histograms to
138 // avoid creating duplicates that would have to be merged during reporting.
139 // Every call to this method resumes from the last entry it saw; it costs
140 // nothing if nothing new has been added.
141 static void ImportGlobalHistograms();
142
143 // Histogram containing creation results. Visible for testing.
144 static HistogramBase* GetCreateHistogramResultHistogram();
145
146 private:
147 // Enumerate possible creation results for reporting.
148 enum CreateHistogramResultType {
149 // Everything was fine.
150 CREATE_HISTOGRAM_SUCCESS = 0,
151
152 // Pointer to metadata was not valid.
153 CREATE_HISTOGRAM_INVALID_METADATA_POINTER,
154
155 // Histogram metadata was not valid.
156 CREATE_HISTOGRAM_INVALID_METADATA,
157
158 // Ranges information was not valid.
159 CREATE_HISTOGRAM_INVALID_RANGES_ARRAY,
160
161 // Counts information was not valid.
162 CREATE_HISTOGRAM_INVALID_COUNTS_ARRAY,
163
164 // Could not allocate histogram memory due to corruption.
165 CREATE_HISTOGRAM_ALLOCATOR_CORRUPT,
166
167 // Could not allocate histogram memory due to lack of space.
168 CREATE_HISTOGRAM_ALLOCATOR_FULL,
169
170 // Could not allocate histogram memory due to unknown error.
171 CREATE_HISTOGRAM_ALLOCATOR_ERROR,
172
173 // Histogram was of unknown type.
174 CREATE_HISTOGRAM_UNKNOWN_TYPE,
175
176 // Instance has detected a corrupt allocator (recorded only once).
177 CREATE_HISTOGRAM_ALLOCATOR_NEWLY_CORRUPT,
178
179 // Always keep this at the end.
180 CREATE_HISTOGRAM_MAX
181 };
182
183 // The structure used to hold histogram data in persistent memory. It is
184 // defined and used entirely within the .cc file.
185 struct PersistentHistogramData;
186
187 // Get the next histogram in persistent data based on iterator while
188 // ignoring a particular reference if it is found.
189 scoped_ptr<HistogramBase> GetNextHistogramWithIgnore(
190 Iterator* iter,
191 Reference ignore);
192
193 // Create a histogram based on saved (persistent) information about it.
194 scoped_ptr<HistogramBase> CreateHistogram(
195 PersistentHistogramData* histogram_data_ptr);
196
197 // Record the result of a histogram creation.
198 static void RecordCreateHistogramResult(CreateHistogramResultType result);
199
200 // The memory allocator that provides the actual histogram storage.
201 scoped_ptr<PersistentMemoryAllocator> memory_allocator_;
202
203 // A reference to the last-created histogram in the allocator, used to avoid
204 // trying to import what was just created.
205 subtle::AtomicWord last_created_ = 0;
206
207 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocator);
208 };
209
210 } // namespace base
211
212 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_
OLDNEW
« no previous file with comments | « base/metrics/histogram_unittest.cc ('k') | base/metrics/persistent_histogram_allocator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698