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