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" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/shared_memory.h" | 12 #include "base/memory/shared_memory.h" |
13 #include "base/metrics/histogram_base.h" | 13 #include "base/metrics/histogram_base.h" |
14 #include "base/metrics/persistent_memory_allocator.h" | 14 #include "base/metrics/persistent_memory_allocator.h" |
15 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 // Feature definition for enabling histogram persistence. | 19 // Feature definition for enabling histogram persistence. |
20 BASE_EXPORT extern const Feature kPersistentHistogramsFeature; | 20 BASE_EXPORT extern const Feature kPersistentHistogramsFeature; |
21 | 21 |
22 // This class manages histograms created within a PersistentMemoryAllocator. | 22 // This class manages histograms created within a PersistentMemoryAllocator. |
23 class BASE_EXPORT PersistentHistogramAllocator { | 23 class BASE_EXPORT PersistentHistogramAllocator { |
24 public: | 24 public: |
25 // This iterator is used for fetching persistent histograms from an allocator. | 25 using Reference = PersistentMemoryAllocator::Reference; |
26 class Iterator { | 26 |
27 // Iterator used for fetching persistent histograms from an allocator. | |
28 // It is lock-free and thread-safe. | |
29 // See PersistentMemoryAllocator::Iterator for more information. | |
30 class BASE_EXPORT Iterator { | |
27 public: | 31 public: |
28 bool is_clear() { return memory_iter.is_clear(); } | 32 Iterator(PersistentHistogramAllocator* allocator); |
Ilya Sherman
2016/03/24 02:03:58
nit: explicit
Ilya Sherman
2016/03/24 02:03:58
nit: Please document lifetime expectations for the
bcwhite
2016/03/24 14:22:34
Done.
bcwhite
2016/03/24 14:22:34
Done.
| |
33 | |
34 // Gets the next histogram from persistent memory. Null will be returned | |
35 // if there are no more histograms to return. This may still be called | |
Ilya Sherman
2016/03/24 02:03:58
Optional nit: s/Null will be returned/Returns null
bcwhite
2016/03/24 14:22:34
Done.
| |
36 // again later to retrieve any new histograms added in the meantime. | |
37 scoped_ptr<HistogramBase> GetNext() { return GetNextWithIgnore(0); } | |
29 | 38 |
30 private: | 39 private: |
31 friend class PersistentHistogramAllocator; | 40 friend class PersistentHistogramAllocator; |
32 | 41 |
33 // The iterator used for stepping through persistent memory iterables. | 42 // Gets the next histogram from persistent memory, ignoring one particular |
34 PersistentMemoryAllocator::Iterator memory_iter; | 43 // reference in the process. Pass |ignore| of zero (0) to ignore nothing. |
44 scoped_ptr<HistogramBase> GetNextWithIgnore(Reference ignore); | |
45 | |
46 // Weak-pointer to histogram allocator being iterated over. | |
47 PersistentHistogramAllocator* allocator_; | |
48 | |
49 // The iterator used for stepping through objects in persistent memory. | |
50 // It is lock-free and thread-safe which is why this class is also so. | |
51 PersistentMemoryAllocator::Iterator memory_iter_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(Iterator); | |
35 }; | 54 }; |
36 | 55 |
37 using Reference = PersistentMemoryAllocator::Reference; | |
38 | |
39 // A PersistentHistogramAllocator is constructed from a PersistentMemory- | 56 // A PersistentHistogramAllocator is constructed from a PersistentMemory- |
40 // Allocator object of which it takes ownership. | 57 // Allocator object of which it takes ownership. |
41 PersistentHistogramAllocator(scoped_ptr<PersistentMemoryAllocator> memory); | 58 PersistentHistogramAllocator(scoped_ptr<PersistentMemoryAllocator> memory); |
42 ~PersistentHistogramAllocator(); | 59 ~PersistentHistogramAllocator(); |
43 | 60 |
44 // Direct access to underlying memory allocator. If the segment is shared | 61 // Direct access to underlying memory allocator. If the segment is shared |
45 // across threads or processes, reading data through these values does | 62 // across threads or processes, reading data through these values does |
46 // not guarantee consistency. Use with care. Do not write. | 63 // not guarantee consistency. Use with care. Do not write. |
47 PersistentMemoryAllocator* memory_allocator() { | 64 PersistentMemoryAllocator* memory_allocator() { |
48 return memory_allocator_.get(); | 65 return memory_allocator_.get(); |
49 } | 66 } |
50 | 67 |
51 // Implement the "metadata" API of a PersistentMemoryAllocator, forwarding | 68 // Implement the "metadata" API of a PersistentMemoryAllocator, forwarding |
52 // those requests to the real one. | 69 // those requests to the real one. |
53 uint64_t Id() const { return memory_allocator_->Id(); } | 70 uint64_t Id() const { return memory_allocator_->Id(); } |
54 const char* Name() const { return memory_allocator_->Name(); } | 71 const char* Name() const { return memory_allocator_->Name(); } |
55 const void* data() const { return memory_allocator_->data(); } | 72 const void* data() const { return memory_allocator_->data(); } |
56 size_t length() const { return memory_allocator_->length(); } | 73 size_t length() const { return memory_allocator_->length(); } |
57 size_t used() const { return memory_allocator_->used(); } | 74 size_t used() const { return memory_allocator_->used(); } |
58 | 75 |
59 // Recreate a Histogram from data held in persistent memory. Though this | 76 // 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 | 77 // 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| | 78 // 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. | 79 // 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. | 80 // This method will return null if any problem is detected with the data. |
64 scoped_ptr<HistogramBase> GetHistogram(Reference ref); | 81 scoped_ptr<HistogramBase> GetHistogram(Reference ref); |
65 | 82 |
66 // Get the next histogram in persistent data based on iterator. | |
67 scoped_ptr<HistogramBase> GetNextHistogram(Iterator* iter) { | |
68 return GetNextHistogramWithIgnore(iter, 0); | |
69 } | |
70 | |
71 // Create an iterator for going through all histograms in an allocator. | |
72 void CreateIterator(Iterator* iter); | |
73 | |
74 // Allocate a new persistent histogram. The returned histogram will not | 83 // Allocate a new persistent histogram. The returned histogram will not |
75 // be able to be located by other allocators until it is "finalized". | 84 // be able to be located by other allocators until it is "finalized". |
76 scoped_ptr<HistogramBase> AllocateHistogram( | 85 scoped_ptr<HistogramBase> AllocateHistogram( |
77 HistogramType histogram_type, | 86 HistogramType histogram_type, |
78 const std::string& name, | 87 const std::string& name, |
79 int minimum, | 88 int minimum, |
80 int maximum, | 89 int maximum, |
81 const BucketRanges* bucket_ranges, | 90 const BucketRanges* bucket_ranges, |
82 int32_t flags, | 91 int32_t flags, |
83 Reference* ref_ptr); | 92 Reference* ref_ptr); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 // A reference to the last-created histogram in the allocator, used to avoid | 212 // A reference to the last-created histogram in the allocator, used to avoid |
204 // trying to import what was just created. | 213 // trying to import what was just created. |
205 subtle::AtomicWord last_created_ = 0; | 214 subtle::AtomicWord last_created_ = 0; |
206 | 215 |
207 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocator); | 216 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocator); |
208 }; | 217 }; |
209 | 218 |
210 } // namespace base | 219 } // namespace base |
211 | 220 |
212 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ | 221 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ |
OLD | NEW |