| 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 <memory> |
| 9 |
| 8 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
| 9 #include "base/base_export.h" | 11 #include "base/base_export.h" |
| 10 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/memory/shared_memory.h" | 13 #include "base/memory/shared_memory.h" |
| 13 #include "base/metrics/histogram_base.h" | 14 #include "base/metrics/histogram_base.h" |
| 14 #include "base/metrics/persistent_memory_allocator.h" | 15 #include "base/metrics/persistent_memory_allocator.h" |
| 15 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 | 19 |
| 19 // Feature definition for enabling histogram persistence. | 20 // Feature definition for enabling histogram persistence. |
| 20 BASE_EXPORT extern const Feature kPersistentHistogramsFeature; | 21 BASE_EXPORT extern const Feature kPersistentHistogramsFeature; |
| 21 | 22 |
| 22 // This class manages histograms created within a PersistentMemoryAllocator. | 23 // This class manages histograms created within a PersistentMemoryAllocator. |
| 23 class BASE_EXPORT PersistentHistogramAllocator { | 24 class BASE_EXPORT PersistentHistogramAllocator { |
| 24 public: | 25 public: |
| 25 // This iterator is used for fetching persistent histograms from an allocator. | 26 // This iterator is used for fetching persistent histograms from an allocator. |
| 26 class Iterator { | 27 class Iterator { |
| 27 public: | 28 public: |
| 28 bool is_clear() { return memory_iter.is_clear(); } | 29 bool is_clear() { return memory_iter.is_clear(); } |
| 29 | 30 |
| 30 private: | 31 private: |
| 31 friend class PersistentHistogramAllocator; | 32 friend class PersistentHistogramAllocator; |
| 32 | 33 |
| 33 // The iterator used for stepping through persistent memory iterables. | 34 // The iterator used for stepping through persistent memory iterables. |
| 34 PersistentMemoryAllocator::Iterator memory_iter; | 35 PersistentMemoryAllocator::Iterator memory_iter; |
| 35 }; | 36 }; |
| 36 | 37 |
| 37 using Reference = PersistentMemoryAllocator::Reference; | 38 using Reference = PersistentMemoryAllocator::Reference; |
| 38 | 39 |
| 39 // A PersistentHistogramAllocator is constructed from a PersistentMemory- | 40 // A PersistentHistogramAllocator is constructed from a PersistentMemory- |
| 40 // Allocator object of which it takes ownership. | 41 // Allocator object of which it takes ownership. |
| 41 PersistentHistogramAllocator(scoped_ptr<PersistentMemoryAllocator> memory); | 42 PersistentHistogramAllocator( |
| 43 std::unique_ptr<PersistentMemoryAllocator> memory); |
| 42 ~PersistentHistogramAllocator(); | 44 ~PersistentHistogramAllocator(); |
| 43 | 45 |
| 44 // Direct access to underlying memory allocator. If the segment is shared | 46 // Direct access to underlying memory allocator. If the segment is shared |
| 45 // across threads or processes, reading data through these values does | 47 // across threads or processes, reading data through these values does |
| 46 // not guarantee consistency. Use with care. Do not write. | 48 // not guarantee consistency. Use with care. Do not write. |
| 47 PersistentMemoryAllocator* memory_allocator() { | 49 PersistentMemoryAllocator* memory_allocator() { |
| 48 return memory_allocator_.get(); | 50 return memory_allocator_.get(); |
| 49 } | 51 } |
| 50 | 52 |
| 51 // Implement the "metadata" API of a PersistentMemoryAllocator, forwarding | 53 // Implement the "metadata" API of a PersistentMemoryAllocator, forwarding |
| 52 // those requests to the real one. | 54 // those requests to the real one. |
| 53 uint64_t Id() const { return memory_allocator_->Id(); } | 55 uint64_t Id() const { return memory_allocator_->Id(); } |
| 54 const char* Name() const { return memory_allocator_->Name(); } | 56 const char* Name() const { return memory_allocator_->Name(); } |
| 55 const void* data() const { return memory_allocator_->data(); } | 57 const void* data() const { return memory_allocator_->data(); } |
| 56 size_t length() const { return memory_allocator_->length(); } | 58 size_t length() const { return memory_allocator_->length(); } |
| 57 size_t used() const { return memory_allocator_->used(); } | 59 size_t used() const { return memory_allocator_->used(); } |
| 58 | 60 |
| 59 // Recreate a Histogram from data held in persistent memory. Though this | 61 // 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 | 62 // 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| | 63 // 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. | 64 // 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. | 65 // This method will return null if any problem is detected with the data. |
| 64 scoped_ptr<HistogramBase> GetHistogram(Reference ref); | 66 std::unique_ptr<HistogramBase> GetHistogram(Reference ref); |
| 65 | 67 |
| 66 // Get the next histogram in persistent data based on iterator. | 68 // Get the next histogram in persistent data based on iterator. |
| 67 scoped_ptr<HistogramBase> GetNextHistogram(Iterator* iter) { | 69 std::unique_ptr<HistogramBase> GetNextHistogram(Iterator* iter) { |
| 68 return GetNextHistogramWithIgnore(iter, 0); | 70 return GetNextHistogramWithIgnore(iter, 0); |
| 69 } | 71 } |
| 70 | 72 |
| 71 // Create an iterator for going through all histograms in an allocator. | 73 // Create an iterator for going through all histograms in an allocator. |
| 72 void CreateIterator(Iterator* iter); | 74 void CreateIterator(Iterator* iter); |
| 73 | 75 |
| 74 // Allocate a new persistent histogram. The returned histogram will not | 76 // Allocate a new persistent histogram. The returned histogram will not |
| 75 // be able to be located by other allocators until it is "finalized". | 77 // be able to be located by other allocators until it is "finalized". |
| 76 scoped_ptr<HistogramBase> AllocateHistogram( | 78 std::unique_ptr<HistogramBase> AllocateHistogram( |
| 77 HistogramType histogram_type, | 79 HistogramType histogram_type, |
| 78 const std::string& name, | 80 const std::string& name, |
| 79 int minimum, | 81 int minimum, |
| 80 int maximum, | 82 int maximum, |
| 81 const BucketRanges* bucket_ranges, | 83 const BucketRanges* bucket_ranges, |
| 82 int32_t flags, | 84 int32_t flags, |
| 83 Reference* ref_ptr); | 85 Reference* ref_ptr); |
| 84 | 86 |
| 85 // Finalize the creation of the histogram, making it available to other | 87 // Finalize the creation of the histogram, making it available to other |
| 86 // processes if |registered| (as in: added to the StatisticsRecorder) is | 88 // processes if |registered| (as in: added to the StatisticsRecorder) is |
| (...skipping 12 matching lines...) Expand all Loading... |
| 99 void CreateTrackingHistograms(StringPiece name); | 101 void CreateTrackingHistograms(StringPiece name); |
| 100 void UpdateTrackingHistograms(); | 102 void UpdateTrackingHistograms(); |
| 101 | 103 |
| 102 // Manage a PersistentHistogramAllocator for globally storing histograms in | 104 // Manage a PersistentHistogramAllocator for globally storing histograms in |
| 103 // a space that can be persisted or shared between processes. There is only | 105 // 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. | 106 // 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 | 107 // 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 | 108 // possible during startup to capture as many histograms as possible and |
| 107 // while operating single-threaded so there are no race-conditions. | 109 // while operating single-threaded so there are no race-conditions. |
| 108 static void SetGlobalAllocator( | 110 static void SetGlobalAllocator( |
| 109 scoped_ptr<PersistentHistogramAllocator> allocator); | 111 std::unique_ptr<PersistentHistogramAllocator> allocator); |
| 110 static PersistentHistogramAllocator* GetGlobalAllocator(); | 112 static PersistentHistogramAllocator* GetGlobalAllocator(); |
| 111 | 113 |
| 112 // This access to the persistent allocator is only for testing; it extracts | 114 // This access to the persistent allocator is only for testing; it extracts |
| 113 // the current allocator completely. This allows easy creation of histograms | 115 // the current allocator completely. This allows easy creation of histograms |
| 114 // within persistent memory segments which can then be extracted and used | 116 // within persistent memory segments which can then be extracted and used |
| 115 // in other ways. | 117 // in other ways. |
| 116 static scoped_ptr<PersistentHistogramAllocator> | 118 static std::unique_ptr<PersistentHistogramAllocator> |
| 117 ReleaseGlobalAllocatorForTesting(); | 119 ReleaseGlobalAllocatorForTesting(); |
| 118 | 120 |
| 119 // These helper methods perform SetGlobalAllocator() calls with allocators | 121 // These helper methods perform SetGlobalAllocator() calls with allocators |
| 120 // of the specified type and parameters. | 122 // of the specified type and parameters. |
| 121 static void CreateGlobalAllocatorOnPersistentMemory( | 123 static void CreateGlobalAllocatorOnPersistentMemory( |
| 122 void* base, | 124 void* base, |
| 123 size_t size, | 125 size_t size, |
| 124 size_t page_size, | 126 size_t page_size, |
| 125 uint64_t id, | 127 uint64_t id, |
| 126 StringPiece name); | 128 StringPiece name); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // Always keep this at the end. | 181 // Always keep this at the end. |
| 180 CREATE_HISTOGRAM_MAX | 182 CREATE_HISTOGRAM_MAX |
| 181 }; | 183 }; |
| 182 | 184 |
| 183 // The structure used to hold histogram data in persistent memory. It is | 185 // The structure used to hold histogram data in persistent memory. It is |
| 184 // defined and used entirely within the .cc file. | 186 // defined and used entirely within the .cc file. |
| 185 struct PersistentHistogramData; | 187 struct PersistentHistogramData; |
| 186 | 188 |
| 187 // Get the next histogram in persistent data based on iterator while | 189 // Get the next histogram in persistent data based on iterator while |
| 188 // ignoring a particular reference if it is found. | 190 // ignoring a particular reference if it is found. |
| 189 scoped_ptr<HistogramBase> GetNextHistogramWithIgnore( | 191 std::unique_ptr<HistogramBase> GetNextHistogramWithIgnore(Iterator* iter, |
| 190 Iterator* iter, | 192 Reference ignore); |
| 191 Reference ignore); | |
| 192 | 193 |
| 193 // Create a histogram based on saved (persistent) information about it. | 194 // Create a histogram based on saved (persistent) information about it. |
| 194 scoped_ptr<HistogramBase> CreateHistogram( | 195 std::unique_ptr<HistogramBase> CreateHistogram( |
| 195 PersistentHistogramData* histogram_data_ptr); | 196 PersistentHistogramData* histogram_data_ptr); |
| 196 | 197 |
| 197 // Record the result of a histogram creation. | 198 // Record the result of a histogram creation. |
| 198 static void RecordCreateHistogramResult(CreateHistogramResultType result); | 199 static void RecordCreateHistogramResult(CreateHistogramResultType result); |
| 199 | 200 |
| 200 // The memory allocator that provides the actual histogram storage. | 201 // The memory allocator that provides the actual histogram storage. |
| 201 scoped_ptr<PersistentMemoryAllocator> memory_allocator_; | 202 std::unique_ptr<PersistentMemoryAllocator> memory_allocator_; |
| 202 | 203 |
| 203 // A reference to the last-created histogram in the allocator, used to avoid | 204 // A reference to the last-created histogram in the allocator, used to avoid |
| 204 // trying to import what was just created. | 205 // trying to import what was just created. |
| 205 subtle::AtomicWord last_created_ = 0; | 206 subtle::AtomicWord last_created_ = 0; |
| 206 | 207 |
| 207 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocator); | 208 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocator); |
| 208 }; | 209 }; |
| 209 | 210 |
| 210 } // namespace base | 211 } // namespace base |
| 211 | 212 |
| 212 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ | 213 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ |
| OLD | NEW |