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

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

Issue 1780993002: Break global impact of PersistentHistogramAllocator into a separate class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor-hp
Patch Set: fixed bad formatting from upstream scoped_ptr change Created 4 years, 8 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ 5 #ifndef BASE_METRICS_HISTOGRAM_PERSISTENCE_H_
6 #define BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ 6 #define BASE_METRICS_HISTOGRAM_PERSISTENCE_H_
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/atomicops.h" 10 #include "base/atomicops.h"
(...skipping 23 matching lines...) Expand all
34 // The iterator used for stepping through persistent memory iterables. 34 // The iterator used for stepping through persistent memory iterables.
35 PersistentMemoryAllocator::Iterator memory_iter; 35 PersistentMemoryAllocator::Iterator memory_iter;
36 }; 36 };
37 37
38 using Reference = PersistentMemoryAllocator::Reference; 38 using Reference = PersistentMemoryAllocator::Reference;
39 39
40 // A PersistentHistogramAllocator is constructed from a PersistentMemory- 40 // A PersistentHistogramAllocator is constructed from a PersistentMemory-
41 // Allocator object of which it takes ownership. 41 // Allocator object of which it takes ownership.
42 PersistentHistogramAllocator( 42 PersistentHistogramAllocator(
43 std::unique_ptr<PersistentMemoryAllocator> memory); 43 std::unique_ptr<PersistentMemoryAllocator> memory);
44 ~PersistentHistogramAllocator(); 44 virtual ~PersistentHistogramAllocator();
45 45
46 // Direct access to underlying memory allocator. If the segment is shared 46 // Direct access to underlying memory allocator. If the segment is shared
47 // across threads or processes, reading data through these values does 47 // across threads or processes, reading data through these values does
48 // not guarantee consistency. Use with care. Do not write. 48 // not guarantee consistency. Use with care. Do not write.
49 PersistentMemoryAllocator* memory_allocator() { 49 PersistentMemoryAllocator* memory_allocator() {
50 return memory_allocator_.get(); 50 return memory_allocator_.get();
51 } 51 }
52 52
53 // Implement the "metadata" API of a PersistentMemoryAllocator, forwarding 53 // Implement the "metadata" API of a PersistentMemoryAllocator, forwarding
54 // those requests to the real one. 54 // those requests to the real one.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 // is done seperately from construction for situations such as when the 94 // is done seperately from construction for situations such as when the
95 // histograms will be backed by memory provided by this very allocator. 95 // histograms will be backed by memory provided by this very allocator.
96 // 96 //
97 // IMPORTANT: Callers must update tools/metrics/histograms/histograms.xml 97 // IMPORTANT: Callers must update tools/metrics/histograms/histograms.xml
98 // with the following histograms: 98 // with the following histograms:
99 // UMA.PersistentAllocator.name.Allocs 99 // UMA.PersistentAllocator.name.Allocs
100 // UMA.PersistentAllocator.name.UsedPct 100 // UMA.PersistentAllocator.name.UsedPct
101 void CreateTrackingHistograms(StringPiece name); 101 void CreateTrackingHistograms(StringPiece name);
102 void UpdateTrackingHistograms(); 102 void UpdateTrackingHistograms();
103 103
104 // Manage a PersistentHistogramAllocator for globally storing histograms in 104 // Histogram containing creation results. Visible for testing.
105 // a space that can be persisted or shared between processes. There is only 105 static HistogramBase* GetCreateHistogramResultHistogram();
106 // ever one allocator for all such histograms created by a single process.
107 // This takes ownership of the object and should be called as soon as
108 // possible during startup to capture as many histograms as possible and
109 // while operating single-threaded so there are no race-conditions.
110 static void SetGlobalAllocator(
111 std::unique_ptr<PersistentHistogramAllocator> allocator);
112 static PersistentHistogramAllocator* GetGlobalAllocator();
113 106
114 // This access to the persistent allocator is only for testing; it extracts 107 // This access to the persistent allocator is only for testing; it extracts
115 // the current allocator completely. This allows easy creation of histograms 108 // the current allocator completely. This allows easy creation of histograms
116 // within persistent memory segments which can then be extracted and used 109 // within persistent memory segments which can then be extracted and used
117 // in other ways. 110 // in other ways.
118 static std::unique_ptr<PersistentHistogramAllocator> 111 static std::unique_ptr<PersistentHistogramAllocator>
119 ReleaseGlobalAllocatorForTesting(); 112 ReleaseGlobalAllocatorForTesting();
120 113
121 // These helper methods perform SetGlobalAllocator() calls with allocators 114 protected:
122 // of the specified type and parameters. 115 // The structure used to hold histogram data in persistent memory. It is
123 static void CreateGlobalAllocatorOnPersistentMemory( 116 // defined and used entirely within the .cc file.
124 void* base, 117 struct PersistentHistogramData;
125 size_t size,
126 size_t page_size,
127 uint64_t id,
128 StringPiece name);
129 static void CreateGlobalAllocatorOnLocalMemory(
130 size_t size,
131 uint64_t id,
132 StringPiece name);
133 static void CreateGlobalAllocatorOnSharedMemory(
134 size_t size,
135 const SharedMemoryHandle& handle);
136 118
137 // Import new histograms from the global PersistentHistogramAllocator. It's 119 // Gets the reference of the last histogram created, used to avoid
138 // possible for other processes to create histograms in the active memory 120 // trying to import what was just created.
139 // segment; this adds those to the internal list of known histograms to 121 PersistentHistogramAllocator::Reference last_created() {
140 // avoid creating duplicates that would have to be merged during reporting. 122 return subtle::NoBarrier_Load(&last_created_);
141 // Every call to this method resumes from the last entry it saw; it costs 123 }
142 // nothing if nothing new has been added.
143 static void ImportGlobalHistograms();
144 124
145 // Histogram containing creation results. Visible for testing. 125 // Gets the next histogram in persistent data based on iterator while
146 static HistogramBase* GetCreateHistogramResultHistogram(); 126 // ignoring a particular reference if it is found.
127 std::unique_ptr<HistogramBase> GetNextHistogramWithIgnore(Iterator* iter,
128 Reference ignore);
147 129
148 private: 130 private:
149 // Enumerate possible creation results for reporting. 131 // Enumerate possible creation results for reporting.
150 enum CreateHistogramResultType { 132 enum CreateHistogramResultType {
151 // Everything was fine. 133 // Everything was fine.
152 CREATE_HISTOGRAM_SUCCESS = 0, 134 CREATE_HISTOGRAM_SUCCESS = 0,
153 135
154 // Pointer to metadata was not valid. 136 // Pointer to metadata was not valid.
155 CREATE_HISTOGRAM_INVALID_METADATA_POINTER, 137 CREATE_HISTOGRAM_INVALID_METADATA_POINTER,
156 138
(...skipping 18 matching lines...) Expand all
175 // Histogram was of unknown type. 157 // Histogram was of unknown type.
176 CREATE_HISTOGRAM_UNKNOWN_TYPE, 158 CREATE_HISTOGRAM_UNKNOWN_TYPE,
177 159
178 // Instance has detected a corrupt allocator (recorded only once). 160 // Instance has detected a corrupt allocator (recorded only once).
179 CREATE_HISTOGRAM_ALLOCATOR_NEWLY_CORRUPT, 161 CREATE_HISTOGRAM_ALLOCATOR_NEWLY_CORRUPT,
180 162
181 // Always keep this at the end. 163 // Always keep this at the end.
182 CREATE_HISTOGRAM_MAX 164 CREATE_HISTOGRAM_MAX
183 }; 165 };
184 166
185 // The structure used to hold histogram data in persistent memory. It is
186 // defined and used entirely within the .cc file.
187 struct PersistentHistogramData;
188
189 // Get the next histogram in persistent data based on iterator while
190 // ignoring a particular reference if it is found.
191 std::unique_ptr<HistogramBase> GetNextHistogramWithIgnore(Iterator* iter,
192 Reference ignore);
193
194 // Create a histogram based on saved (persistent) information about it. 167 // Create a histogram based on saved (persistent) information about it.
195 std::unique_ptr<HistogramBase> CreateHistogram( 168 std::unique_ptr<HistogramBase> CreateHistogram(
196 PersistentHistogramData* histogram_data_ptr); 169 PersistentHistogramData* histogram_data_ptr);
197 170
198 // Record the result of a histogram creation. 171 // Record the result of a histogram creation.
199 static void RecordCreateHistogramResult(CreateHistogramResultType result); 172 static void RecordCreateHistogramResult(CreateHistogramResultType result);
200 173
201 // The memory allocator that provides the actual histogram storage. 174 // The memory allocator that provides the actual histogram storage.
202 std::unique_ptr<PersistentMemoryAllocator> memory_allocator_; 175 std::unique_ptr<PersistentMemoryAllocator> memory_allocator_;
203 176
204 // A reference to the last-created histogram in the allocator, used to avoid 177 // A reference to the last-created histogram in the allocator, used to avoid
205 // trying to import what was just created. 178 // trying to import what was just created.
206 subtle::AtomicWord last_created_ = 0; 179 // TODO(bcwhite): Change this to std::atomic<PMA::Reference> when available.
180 subtle::Atomic32 last_created_ = 0;
207 181
208 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocator); 182 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocator);
209 }; 183 };
210 184
185
186 // A special case of the PersistentHistogramAllocator that operates on a
187 // global scale, collecting histograms created through standard macros and
188 // the FactoryGet() method.
189 class BASE_EXPORT GlobalHistogramAllocator
190 : public PersistentHistogramAllocator {
191 public:
192 ~GlobalHistogramAllocator() override;
193
194 // Create a global allocator using the passed-in memory |base|, |size|, and
195 // other parameters. Ownership of the memory segment remains with the caller.
196 static void CreateWithPersistentMemory(void* base,
197 size_t size,
198 size_t page_size,
199 uint64_t id,
200 StringPiece name);
201
202 // Create a global allocator using an internal block of memory of the
203 // specified |size| taken from the heap.
204 static void CreateWithLocalMemory(size_t size, uint64_t id, StringPiece name);
205
206 // Create a global allocator using a block of shared |memory| of the
207 // specified |size|. The allocator takes ownership of the shared memory
208 // and releases it upon destruction, though the memory will continue to
209 // live if other processes have access to it.
210 static void CreateWithSharedMemory(std::unique_ptr<SharedMemory> memory,
211 size_t size,
212 uint64_t id,
213 StringPiece name);
214
215 // Create a global allocator using a block of shared memory accessed
216 // through the given |handle| and |size|. The allocator takes ownership
217 // of the handle and closes it upon destruction, though the memory will
218 // continue to live if other processes have access to it.
219 static void CreateWithSharedMemoryHandle(const SharedMemoryHandle& handle,
220 size_t size);
221
222 // Sets a GlobalHistogramAllocator for globally storing histograms in
223 // a space that can be persisted or shared between processes. There is only
224 // ever one allocator for all such histograms created by a single process.
225 // This takes ownership of the object and should be called as soon as
226 // possible during startup to capture as many histograms as possible and
227 // while operating single-threaded so there are no race-conditions.
228 static void Set(std::unique_ptr<GlobalHistogramAllocator> allocator);
229
230 // Gets a pointer to the global histogram allocator.
231 static GlobalHistogramAllocator* Get();
232
233 // This access to the persistent allocator is only for testing; it extracts
234 // the current allocator completely. This allows easy creation of histograms
235 // within persistent memory segments which can then be extracted and used
236 // in other ways.
237 static std::unique_ptr<GlobalHistogramAllocator> ReleaseForTesting();
238
239 private:
240 friend class StatisticsRecorder;
241
242 explicit GlobalHistogramAllocator(
243 std::unique_ptr<PersistentMemoryAllocator> memory);
244
245 // Import new histograms from the global histogram allocator. It's possible
246 // for other processes to create histograms in the active memory segment;
247 // this adds those to the internal list of known histograms to avoid creating
248 // duplicates that would have to be merged during reporting. Every call to
249 // this method resumes from the last entry it saw; it costs nothing if
250 // nothing new has been added.
251 void ImportHistogramsToStatisticsRecorder();
252
253 // Import always continues from where it left off, making use of a single
254 // iterator to continue the work.
255 Iterator import_iterator_;
256
257 DISALLOW_COPY_AND_ASSIGN(GlobalHistogramAllocator);
258 };
259
211 } // namespace base 260 } // namespace base
212 261
213 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ 262 #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