OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ | |
6 #define BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ | |
7 | |
8 #include "base/atomicops.h" | |
9 #include "base/base_export.h" | |
10 #include "base/feature_list.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/memory/shared_memory.h" | |
13 #include "base/metrics/histogram_base.h" | |
14 #include "base/metrics/persistent_memory_allocator.h" | |
15 #include "base/strings/string_piece.h" | |
16 | |
17 namespace base { | |
18 | |
19 // Feature definition for enabling histogram persistence. | |
20 BASE_EXPORT extern const Feature kPersistentHistogramsFeature; | |
21 | |
22 // This class manages histograms created within a PersistentMemoryAllocator. | |
23 class BASE_EXPORT PersistentHistogramAllocator { | |
24 public: | |
25 // This iterator is used for fetching persistent histograms from an allocator. | |
26 class Iterator { | |
27 public: | |
28 bool is_clear() { return memory_iter.is_clear(); } | |
29 | |
30 private: | |
31 friend class PersistentHistogramAllocator; | |
32 | |
33 // The iterator used for stepping through persistent memory iterables. | |
34 PersistentMemoryAllocator::Iterator memory_iter; | |
35 }; | |
36 | |
37 using Reference = PersistentMemoryAllocator::Reference; | |
38 | |
39 // A PersistentHistogramAllocator is constructed from a PersistentMemory- | |
40 // Allocator object of which it takes ownership. | |
41 PersistentHistogramAllocator(scoped_ptr<PersistentMemoryAllocator> memory); | |
42 ~PersistentHistogramAllocator(); | |
43 | |
44 // Direct access to underlying memory allocator. If the segment is shared | |
45 // across threads or processes, reading data through these values does | |
46 // not guarantee consistency. Use with care. Do not write. | |
47 uint64_t Id() const { return memory_allocator_->Id(); } | |
48 const char* Name() const { return memory_allocator_->Name(); } | |
49 const void* data() const { return memory_allocator_->data(); } | |
50 size_t length() const { return memory_allocator_->length(); } | |
51 size_t used() const { return memory_allocator_->used(); } | |
Alexei Svitkine (slow)
2016/03/14 19:17:20
Nit: Move these below the memory_allocator() gette
bcwhite
2016/03/14 23:41:50
Done.
I implemented the others so that the "basic
| |
52 PersistentMemoryAllocator* memory_allocator() { | |
Alexei Svitkine (slow)
2016/03/14 19:17:20
Nit: Add a newline before this.
bcwhite
2016/03/14 23:41:50
Done.
| |
53 return memory_allocator_.get(); | |
54 } | |
55 | |
56 // Recreate a Histogram from data held in persistent memory. Though this | |
57 // object will be local to the current process, the sample data will be | |
58 // shared with all other threads referencing it. This method takes a |ref| | |
59 // to where the top-level histogram data may be found in this allocator. | |
60 // This method will return null if any problem is detected with the data. | |
61 scoped_ptr<HistogramBase> GetHistogram(Reference ref); | |
62 | |
63 // Get the next histogram in persistent data based on iterator. | |
64 scoped_ptr<HistogramBase> GetNextHistogram(Iterator* iter) { | |
65 return GetNextHistogramWithIgnore(iter, 0); | |
66 } | |
67 | |
68 // Create an iterator for going through all histograms in an allocator. | |
69 void CreateIterator(Iterator* iter); | |
Alexei Svitkine (slow)
2016/03/14 19:17:20
Why not return by value?
bcwhite
2016/03/14 23:41:50
This mimics the way iterators are done in the Pers
| |
70 | |
71 // Allocate a new persistent histogram. The returned histogram will not | |
72 // be able to be located by other allocators until it is "finalized". | |
73 scoped_ptr<HistogramBase> AllocateHistogram( | |
74 HistogramType histogram_type, | |
75 const std::string& name, | |
76 int minimum, | |
77 int maximum, | |
78 const BucketRanges* bucket_ranges, | |
79 int32_t flags, | |
80 Reference* ref_ptr); | |
81 | |
82 // Finalize the creation of the histogram, making it available to other | |
83 // processes if |register| is True, forgetting it otherwise. | |
84 void FinalizeHistogram(Reference ref, bool register); | |
85 | |
86 // Create and update any internal histograms. | |
Alexei Svitkine (slow)
2016/03/14 19:17:20
Document what "name" is and how it relates to hist
bcwhite
2016/03/14 23:41:50
Done.
| |
87 void CreateTrackingHistograms(StringPiece name); | |
88 void UpdateTrackingHistograms(); | |
89 | |
90 // Manage a PersistentHistogramAllocator for globally storing histograms in | |
91 // a space that can be persisted or shared between processes. There is only | |
92 // ever one allocator for all such histograms created by a single process. | |
93 // This takes ownership of the object and should be called as soon as | |
94 // possible during startup to capture as many histograms as possible and | |
95 // while operating single-threaded so there are no race-conditions. | |
96 static void SetGlobalAllocator( | |
97 scoped_ptr<PersistentHistogramAllocator> allocator); | |
98 static PersistentHistogramAllocator* GetGlobalAllocator(); | |
99 | |
100 // This access to the persistent allocator is only for testing; it extracts | |
101 // the current allocator completely. This allows easy creation of histograms | |
102 // within persistent memory segments which can then be extracted and used | |
103 // in other ways. | |
104 static scoped_ptr<PersistentHistogramAllocator> | |
105 ReleaseGlobalAllocatorForTesting(); | |
106 | |
107 // These helper methods perform SetGlobalAllocator() calls with allocators | |
108 // of the specified type and parameters. | |
109 static void CreateGlobalAllocatorOnPersistentMemory( | |
110 void* base, | |
111 size_t size, | |
112 size_t page_size, | |
113 uint64_t id, | |
114 StringPiece name); | |
115 static void CreateGlobalAllocatorOnLocalMemory( | |
116 size_t size, | |
117 uint64_t id, | |
118 StringPiece name); | |
119 static void CreateGlobalAllocatorOnSharedMemory( | |
120 size_t size, | |
121 const SharedMemoryHandle& handle); | |
122 | |
123 // Import new histograms from the global PersistentHistogramAllocator. It's | |
124 // possible for other processes to create histograms in the active memory | |
125 // segment; this adds those to the internal list of known histograms to | |
126 // avoid creating duplicates that would have to be merged during reporting. | |
127 // Every call to this method resumes from the last entry it saw; it costs | |
128 // nothing if nothing new has been added. | |
129 static void ImportGlobalHistograms(); | |
130 | |
131 // Histogram containing creation results. Visible for testing. | |
132 static HistogramBase* GetCreateHistogramResultHistogram(); | |
133 | |
134 private: | |
135 // Enumerate possible creation results for reporting. | |
136 enum CreateHistogramResultType { | |
137 // Everything was fine. | |
138 CREATE_HISTOGRAM_SUCCESS = 0, | |
139 | |
140 // Pointer to metadata was not valid. | |
141 CREATE_HISTOGRAM_INVALID_METADATA_POINTER, | |
142 | |
143 // Histogram metadata was not valid. | |
144 CREATE_HISTOGRAM_INVALID_METADATA, | |
145 | |
146 // Ranges information was not valid. | |
147 CREATE_HISTOGRAM_INVALID_RANGES_ARRAY, | |
148 | |
149 // Counts information was not valid. | |
150 CREATE_HISTOGRAM_INVALID_COUNTS_ARRAY, | |
151 | |
152 // Could not allocate histogram memory due to corruption. | |
153 CREATE_HISTOGRAM_ALLOCATOR_CORRUPT, | |
154 | |
155 // Could not allocate histogram memory due to lack of space. | |
156 CREATE_HISTOGRAM_ALLOCATOR_FULL, | |
157 | |
158 // Could not allocate histogram memory due to unknown error. | |
159 CREATE_HISTOGRAM_ALLOCATOR_ERROR, | |
160 | |
161 // Histogram was of unknown type. | |
162 CREATE_HISTOGRAM_UNKNOWN_TYPE, | |
163 | |
164 // Instance has detected a corrupt allocator (recorded only once). | |
165 CREATE_HISTOGRAM_ALLOCATOR_NEWLY_CORRUPT, | |
166 | |
167 // Always keep this at the end. | |
168 CREATE_HISTOGRAM_MAX | |
169 }; | |
170 | |
171 // The structure used to hold histogram data in persistent memory. It is | |
172 // defined and used entirely within the .cc file. | |
173 struct PersistentHistogramData; | |
174 | |
175 // Get the next histogram in persistent data based on iterator while | |
176 // ignoring a particular reference if it is found. | |
177 scoped_ptr<HistogramBase> GetNextHistogramWithIgnore( | |
178 Iterator* iter, | |
179 Reference ignore); | |
180 | |
181 // Create a histogram based on saved (persistent) information about it. | |
182 scoped_ptr<HistogramBase> CreateHistogram( | |
183 PersistentHistogramData* histogram_data_ptr); | |
184 | |
185 // Record the result of a histogram creation. | |
186 static void RecordCreateHistogramResult(CreateHistogramResultType result); | |
187 | |
188 // The memory allocator that provides the actual histogram storage. | |
189 scoped_ptr<PersistentMemoryAllocator> memory_allocator_; | |
190 | |
191 // A reference to the last-created histogram in the allocator, used to avoid | |
192 // trying to import what was just created. | |
193 subtle::AtomicWord last_created_ = 0; | |
194 | |
195 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocator); | |
196 }; | |
197 | |
198 } // namespace base | |
199 | |
200 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ | |
OLD | NEW |