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> | 8 #include <memory> |
9 | 9 |
10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
13 #include "base/memory/shared_memory.h" | 13 #include "base/memory/shared_memory.h" |
14 #include "base/metrics/histogram_base.h" | 14 #include "base/metrics/histogram_base.h" |
15 #include "base/metrics/persistent_memory_allocator.h" | 15 #include "base/metrics/persistent_memory_allocator.h" |
16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 | 19 |
20 // Feature definition for enabling histogram persistence. | 20 // Feature definition for enabling histogram persistence. |
21 BASE_EXPORT extern const Feature kPersistentHistogramsFeature; | 21 BASE_EXPORT extern const Feature kPersistentHistogramsFeature; |
22 | 22 |
23 // This class manages histograms created within a PersistentMemoryAllocator. | 23 // This class manages histograms created within a PersistentMemoryAllocator. |
24 class BASE_EXPORT PersistentHistogramAllocator { | 24 class BASE_EXPORT PersistentHistogramAllocator { |
25 public: | 25 public: |
26 // This iterator is used for fetching persistent histograms from an allocator. | 26 // A reference to a histogram. While this is implemented as PMA::Reference, |
27 class Iterator { | 27 // it is not conceptually the same thing. Outside callers should always use |
| 28 // a Reference matching the class it is for and not mix the two. |
| 29 using Reference = PersistentMemoryAllocator::Reference; |
| 30 |
| 31 // Iterator used for fetching persistent histograms from an allocator. |
| 32 // It is lock-free and thread-safe. |
| 33 // See PersistentMemoryAllocator::Iterator for more information. |
| 34 class BASE_EXPORT Iterator { |
28 public: | 35 public: |
29 bool is_clear() { return memory_iter.is_clear(); } | 36 // Constructs an iterator on a given |allocator|, starting at the beginning. |
| 37 // The allocator must live beyond the lifetime of the iterator. |
| 38 explicit Iterator(PersistentHistogramAllocator* allocator); |
| 39 |
| 40 // Gets the next histogram from persistent memory; returns null if there |
| 41 // are no more histograms to be found. This may still be called again |
| 42 // later to retrieve any new histograms added in the meantime. |
| 43 std::unique_ptr<HistogramBase> GetNext() { return GetNextWithIgnore(0); } |
| 44 |
| 45 // Gets the next histogram from persistent memory, ignoring one particular |
| 46 // reference in the process. Pass |ignore| of zero (0) to ignore nothing. |
| 47 std::unique_ptr<HistogramBase> GetNextWithIgnore(Reference ignore); |
30 | 48 |
31 private: | 49 private: |
32 friend class PersistentHistogramAllocator; | 50 // Weak-pointer to histogram allocator being iterated over. |
| 51 PersistentHistogramAllocator* allocator_; |
33 | 52 |
34 // The iterator used for stepping through persistent memory iterables. | 53 // The iterator used for stepping through objects in persistent memory. |
35 PersistentMemoryAllocator::Iterator memory_iter; | 54 // It is lock-free and thread-safe which is why this class is also such. |
| 55 PersistentMemoryAllocator::Iterator memory_iter_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(Iterator); |
36 }; | 58 }; |
37 | 59 |
38 using Reference = PersistentMemoryAllocator::Reference; | |
39 | |
40 // A PersistentHistogramAllocator is constructed from a PersistentMemory- | 60 // A PersistentHistogramAllocator is constructed from a PersistentMemory- |
41 // Allocator object of which it takes ownership. | 61 // Allocator object of which it takes ownership. |
42 PersistentHistogramAllocator( | 62 PersistentHistogramAllocator( |
43 std::unique_ptr<PersistentMemoryAllocator> memory); | 63 std::unique_ptr<PersistentMemoryAllocator> memory); |
44 virtual ~PersistentHistogramAllocator(); | 64 virtual ~PersistentHistogramAllocator(); |
45 | 65 |
46 // Direct access to underlying memory allocator. If the segment is shared | 66 // Direct access to underlying memory allocator. If the segment is shared |
47 // across threads or processes, reading data through these values does | 67 // across threads or processes, reading data through these values does |
48 // not guarantee consistency. Use with care. Do not write. | 68 // not guarantee consistency. Use with care. Do not write. |
49 PersistentMemoryAllocator* memory_allocator() { | 69 PersistentMemoryAllocator* memory_allocator() { |
50 return memory_allocator_.get(); | 70 return memory_allocator_.get(); |
51 } | 71 } |
52 | 72 |
53 // Implement the "metadata" API of a PersistentMemoryAllocator, forwarding | 73 // Implement the "metadata" API of a PersistentMemoryAllocator, forwarding |
54 // those requests to the real one. | 74 // those requests to the real one. |
55 uint64_t Id() const { return memory_allocator_->Id(); } | 75 uint64_t Id() const { return memory_allocator_->Id(); } |
56 const char* Name() const { return memory_allocator_->Name(); } | 76 const char* Name() const { return memory_allocator_->Name(); } |
57 const void* data() const { return memory_allocator_->data(); } | 77 const void* data() const { return memory_allocator_->data(); } |
58 size_t length() const { return memory_allocator_->length(); } | 78 size_t length() const { return memory_allocator_->length(); } |
59 size_t used() const { return memory_allocator_->used(); } | 79 size_t used() const { return memory_allocator_->used(); } |
60 | 80 |
61 // Recreate a Histogram from data held in persistent memory. Though this | 81 // Recreate a Histogram from data held in persistent memory. Though this |
62 // object will be local to the current process, the sample data will be | 82 // object will be local to the current process, the sample data will be |
63 // shared with all other threads referencing it. This method takes a |ref| | 83 // shared with all other threads referencing it. This method takes a |ref| |
64 // to where the top-level histogram data may be found in this allocator. | 84 // to where the top-level histogram data may be found in this allocator. |
65 // This method will return null if any problem is detected with the data. | 85 // This method will return null if any problem is detected with the data. |
66 std::unique_ptr<HistogramBase> GetHistogram(Reference ref); | 86 std::unique_ptr<HistogramBase> GetHistogram(Reference ref); |
67 | 87 |
68 // Get the next histogram in persistent data based on iterator. | |
69 std::unique_ptr<HistogramBase> GetNextHistogram(Iterator* iter) { | |
70 return GetNextHistogramWithIgnore(iter, 0); | |
71 } | |
72 | |
73 // Create an iterator for going through all histograms in an allocator. | |
74 void CreateIterator(Iterator* iter); | |
75 | |
76 // Allocate a new persistent histogram. The returned histogram will not | 88 // Allocate a new persistent histogram. The returned histogram will not |
77 // be able to be located by other allocators until it is "finalized". | 89 // be able to be located by other allocators until it is "finalized". |
78 std::unique_ptr<HistogramBase> AllocateHistogram( | 90 std::unique_ptr<HistogramBase> AllocateHistogram( |
79 HistogramType histogram_type, | 91 HistogramType histogram_type, |
80 const std::string& name, | 92 const std::string& name, |
81 int minimum, | 93 int minimum, |
82 int maximum, | 94 int maximum, |
83 const BucketRanges* bucket_ranges, | 95 const BucketRanges* bucket_ranges, |
84 int32_t flags, | 96 int32_t flags, |
85 Reference* ref_ptr); | 97 Reference* ref_ptr); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 // Import always continues from where it left off, making use of a single | 265 // Import always continues from where it left off, making use of a single |
254 // iterator to continue the work. | 266 // iterator to continue the work. |
255 Iterator import_iterator_; | 267 Iterator import_iterator_; |
256 | 268 |
257 DISALLOW_COPY_AND_ASSIGN(GlobalHistogramAllocator); | 269 DISALLOW_COPY_AND_ASSIGN(GlobalHistogramAllocator); |
258 }; | 270 }; |
259 | 271 |
260 } // namespace base | 272 } // namespace base |
261 | 273 |
262 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ | 274 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ |
OLD | NEW |