Chromium Code Reviews| 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 ~PersistentHistogramAllocator(); |
|
Alexei Svitkine (slow)
2016/04/01 19:52:40
This should be virtual since there is a subclass.
bcwhite
2016/04/04 18:47:29
Done.
| |
| 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 // The memory allocator that provides the actual histogram storage. | |
| 111 scoped_ptr<PersistentMemoryAllocator> memory_allocator_; | |
|
Alexei Svitkine (slow)
2016/04/01 19:52:40
Members shouldn't be protected per style guide. ("
bcwhite
2016/04/04 18:47:29
Done.
| |
| 112 | |
| 113 // A reference to the last-created histogram in the allocator, used to avoid | |
| 114 // trying to import what was just created. | |
| 115 // TODO(bcwhite): Change this to std::atomic<PMA::Reference> when available. | |
| 116 subtle::Atomic32 last_created_ = 0; | |
| 117 | |
| 118 // Get the next histogram in persistent data based on iterator while | |
| 119 // ignoring a particular reference if it is found. | |
| 120 scoped_ptr<HistogramBase> GetNextHistogramWithIgnore(Iterator* iter, | |
| 121 Reference ignore); | |
| 122 | |
| 146 private: | 123 private: |
| 147 // Enumerate possible creation results for reporting. | 124 // Enumerate possible creation results for reporting. |
| 148 enum CreateHistogramResultType { | 125 enum CreateHistogramResultType { |
| 149 // Everything was fine. | 126 // Everything was fine. |
| 150 CREATE_HISTOGRAM_SUCCESS = 0, | 127 CREATE_HISTOGRAM_SUCCESS = 0, |
| 151 | 128 |
| 152 // Pointer to metadata was not valid. | 129 // Pointer to metadata was not valid. |
| 153 CREATE_HISTOGRAM_INVALID_METADATA_POINTER, | 130 CREATE_HISTOGRAM_INVALID_METADATA_POINTER, |
| 154 | 131 |
| 155 // Histogram metadata was not valid. | 132 // Histogram metadata was not valid. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 173 // Histogram was of unknown type. | 150 // Histogram was of unknown type. |
| 174 CREATE_HISTOGRAM_UNKNOWN_TYPE, | 151 CREATE_HISTOGRAM_UNKNOWN_TYPE, |
| 175 | 152 |
| 176 // Instance has detected a corrupt allocator (recorded only once). | 153 // Instance has detected a corrupt allocator (recorded only once). |
| 177 CREATE_HISTOGRAM_ALLOCATOR_NEWLY_CORRUPT, | 154 CREATE_HISTOGRAM_ALLOCATOR_NEWLY_CORRUPT, |
| 178 | 155 |
| 179 // Always keep this at the end. | 156 // Always keep this at the end. |
| 180 CREATE_HISTOGRAM_MAX | 157 CREATE_HISTOGRAM_MAX |
| 181 }; | 158 }; |
| 182 | 159 |
| 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. | 160 // Create a histogram based on saved (persistent) information about it. |
| 194 scoped_ptr<HistogramBase> CreateHistogram( | 161 scoped_ptr<HistogramBase> CreateHistogram( |
| 195 PersistentHistogramData* histogram_data_ptr); | 162 PersistentHistogramData* histogram_data_ptr); |
| 196 | 163 |
| 197 // Record the result of a histogram creation. | 164 // Record the result of a histogram creation. |
| 198 static void RecordCreateHistogramResult(CreateHistogramResultType result); | 165 static void RecordCreateHistogramResult(CreateHistogramResultType result); |
| 199 | 166 |
| 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); | 167 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocator); |
| 208 }; | 168 }; |
| 209 | 169 |
| 170 | |
| 171 class BASE_EXPORT GlobalHistogramAllocator | |
|
Alexei Svitkine (slow)
2016/04/01 19:52:40
Can this be a separate file?
Also, add a comment.
bcwhite
2016/04/01 20:24:16
I could, but I'd rather not. It's very closely ti
bcwhite
2016/04/04 18:47:29
Done.
| |
| 172 : public PersistentHistogramAllocator { | |
| 173 public: | |
| 174 ~GlobalHistogramAllocator(); | |
|
Alexei Svitkine (slow)
2016/04/01 19:52:40
Where's the dtor? Did you mean to make it private?
bcwhite
2016/04/01 20:24:16
Objects of this class are passed by scoped_ptr so
| |
| 175 | |
| 176 // Create a global allocator using the passed-in memory |base|, |size|, and | |
| 177 // other parameters. Ownership of the memory segment remains with the caller. | |
| 178 static void CreateWithPersistentMemory(void* base, | |
| 179 size_t size, | |
| 180 size_t page_size, | |
| 181 uint64_t id, | |
| 182 StringPiece name); | |
| 183 | |
| 184 // Create a global allocator using an internal block of memory of the | |
| 185 // specified |size| taken from the heap. | |
| 186 static void CreateWithLocalMemory(size_t size, uint64_t id, StringPiece name); | |
| 187 | |
| 188 // Create a global allocator using a block of shared |memory| of the | |
| 189 // specified |size|. The allocator takes ownership of the shared memory | |
| 190 // and releases it upon destruction, though the memory will continue to | |
| 191 // live if other processes have access to it. | |
| 192 static void CreateWithSharedMemory(scoped_ptr<SharedMemory> memory, | |
| 193 size_t size, | |
| 194 uint64_t id, | |
| 195 StringPiece name); | |
| 196 | |
| 197 // Create a global allocator using a block of shared memory accessed | |
| 198 // through the given |handle| and |size|. The allocator takes ownership | |
| 199 // of the handle and closes it upon destruction, though the memory will | |
| 200 // continue to live if other processes have access to it. | |
| 201 static void CreateWithSharedMemoryHandle(const SharedMemoryHandle& handle, | |
| 202 size_t size); | |
| 203 | |
| 204 // Manage a GlobalHistogramAllocator for globally storing histograms in | |
| 205 // a space that can be persisted or shared between processes. There is only | |
| 206 // ever one allocator for all such histograms created by a single process. | |
| 207 // This takes ownership of the object and should be called as soon as | |
| 208 // possible during startup to capture as many histograms as possible and | |
| 209 // while operating single-threaded so there are no race-conditions. | |
| 210 static void Set(scoped_ptr<GlobalHistogramAllocator> allocator); | |
| 211 static GlobalHistogramAllocator* Get(); | |
|
Alexei Svitkine (slow)
2016/04/01 19:52:40
Split these functions with a comment for Get() - o
bcwhite
2016/04/04 18:47:29
Done.
| |
| 212 | |
| 213 // This access to the persistent allocator is only for testing; it extracts | |
| 214 // the current allocator completely. This allows easy creation of histograms | |
| 215 // within persistent memory segments which can then be extracted and used | |
| 216 // in other ways. | |
| 217 static scoped_ptr<GlobalHistogramAllocator> ReleaseForTesting(); | |
| 218 | |
| 219 private: | |
| 220 friend class StatisticsRecorder; | |
| 221 | |
| 222 GlobalHistogramAllocator(scoped_ptr<PersistentMemoryAllocator> memory); | |
| 223 | |
| 224 // Import new histograms from the global histogram allocator. It's possible | |
| 225 // for other processes to create histograms in the active memory segment; | |
| 226 // this adds those to the internal list of known histograms to avoid creating | |
| 227 // duplicates that would have to be merged during reporting. Every call to | |
| 228 // this method resumes from the last entry it saw; it costs nothing if | |
| 229 // nothing new has been added. | |
| 230 void ImportHistogramsToStatisticsRecorder(); | |
| 231 | |
| 232 // Import always continues from where it left off, making use of a single | |
| 233 // iterator to continue the work. | |
| 234 Iterator import_iterator_; | |
| 235 | |
| 236 DISALLOW_COPY_AND_ASSIGN(GlobalHistogramAllocator); | |
| 237 }; | |
| 238 | |
| 210 } // namespace base | 239 } // namespace base |
| 211 | 240 |
| 212 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ | 241 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ |
| OLD | NEW |