OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
Alexei Svitkine (slow)
2016/03/15 22:00:10
No (c)
bcwhite
2016/03/16 00:32:38
Done.
| |
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 PersistentMemoryAllocator* memory_allocator() { | |
48 return memory_allocator_.get(); | |
49 } | |
50 | |
51 // Implement the "metadata" API of a PersistentMemoryAllocator, forwarding | |
52 // those requests to the real one. | |
53 uint64_t Id() const { return memory_allocator_->Id(); } | |
54 const char* Name() const { return memory_allocator_->Name(); } | |
55 const void* data() const { return memory_allocator_->data(); } | |
56 size_t length() const { return memory_allocator_->length(); } | |
57 size_t used() const { return memory_allocator_->used(); } | |
58 | |
59 // 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 | |
61 // 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. | |
63 // This method will return null if any problem is detected with the data. | |
64 scoped_ptr<HistogramBase> GetHistogram(Reference ref); | |
65 | |
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. | |
Alexei Svitkine (slow)
2016/03/15 22:00:10
You were planning to refactor the iterators in an
bcwhite
2016/03/16 00:32:38
Already done:
https://codereview.chromium.org/1803
| |
72 void CreateIterator(Iterator* iter); | |
73 | |
74 // Allocate a new persistent histogram. The returned histogram will not | |
75 // be able to be located by other allocators until it is "finalized". | |
76 scoped_ptr<HistogramBase> AllocateHistogram( | |
77 HistogramType histogram_type, | |
78 const std::string& name, | |
79 int minimum, | |
80 int maximum, | |
81 const BucketRanges* bucket_ranges, | |
82 int32_t flags, | |
83 Reference* ref_ptr); | |
84 | |
85 // Finalize the creation of the histogram, making it available to other | |
86 // processes if |register| is True, forgetting it otherwise. | |
87 void FinalizeHistogram(Reference ref, bool register); | |
Alexei Svitkine (slow)
2016/03/15 22:00:10
Nit: "register" can be a keyword in C++ and gets s
bcwhite
2016/03/16 00:32:38
Oops. I'd already fixed it in the .cc file. Odd
| |
88 | |
89 // Create internal histograms for tracking memory use and allocation sizes | |
90 // for allocator of |name| (which can simply be the result of Name()). This | |
91 // is done seperately from construction for situations such as when the | |
92 // histograms will be backed by memory provided by this very allocator. | |
93 // | |
94 // IMPORTANT: Callers must update tools/metrics/histograms/histograms.xml | |
95 // with the following histograms: | |
96 // UMA.PersistentAllocator.name.Allocs | |
97 // UMA.PersistentAllocator.name.UsedPct | |
98 void CreateTrackingHistograms(StringPiece name); | |
99 void UpdateTrackingHistograms(); | |
100 | |
101 // Manage a PersistentHistogramAllocator for globally storing histograms in | |
102 // a space that can be persisted or shared between processes. There is only | |
103 // ever one allocator for all such histograms created by a single process. | |
104 // This takes ownership of the object and should be called as soon as | |
105 // possible during startup to capture as many histograms as possible and | |
106 // while operating single-threaded so there are no race-conditions. | |
107 static void SetGlobalAllocator( | |
108 scoped_ptr<PersistentHistogramAllocator> allocator); | |
109 static PersistentHistogramAllocator* GetGlobalAllocator(); | |
110 | |
111 // This access to the persistent allocator is only for testing; it extracts | |
112 // the current allocator completely. This allows easy creation of histograms | |
113 // within persistent memory segments which can then be extracted and used | |
114 // in other ways. | |
115 static scoped_ptr<PersistentHistogramAllocator> | |
116 ReleaseGlobalAllocatorForTesting(); | |
117 | |
118 // These helper methods perform SetGlobalAllocator() calls with allocators | |
119 // of the specified type and parameters. | |
120 static void CreateGlobalAllocatorOnPersistentMemory( | |
121 void* base, | |
122 size_t size, | |
123 size_t page_size, | |
124 uint64_t id, | |
125 StringPiece name); | |
126 static void CreateGlobalAllocatorOnLocalMemory( | |
127 size_t size, | |
128 uint64_t id, | |
129 StringPiece name); | |
130 static void CreateGlobalAllocatorOnSharedMemory( | |
131 size_t size, | |
132 const SharedMemoryHandle& handle); | |
133 | |
134 // Import new histograms from the global PersistentHistogramAllocator. It's | |
135 // possible for other processes to create histograms in the active memory | |
136 // segment; this adds those to the internal list of known histograms to | |
137 // avoid creating duplicates that would have to be merged during reporting. | |
138 // Every call to this method resumes from the last entry it saw; it costs | |
139 // nothing if nothing new has been added. | |
140 static void ImportGlobalHistograms(); | |
141 | |
142 // Histogram containing creation results. Visible for testing. | |
143 static HistogramBase* GetCreateHistogramResultHistogram(); | |
144 | |
145 private: | |
146 // Enumerate possible creation results for reporting. | |
147 enum CreateHistogramResultType { | |
148 // Everything was fine. | |
149 CREATE_HISTOGRAM_SUCCESS = 0, | |
150 | |
151 // Pointer to metadata was not valid. | |
152 CREATE_HISTOGRAM_INVALID_METADATA_POINTER, | |
153 | |
154 // Histogram metadata was not valid. | |
155 CREATE_HISTOGRAM_INVALID_METADATA, | |
156 | |
157 // Ranges information was not valid. | |
158 CREATE_HISTOGRAM_INVALID_RANGES_ARRAY, | |
159 | |
160 // Counts information was not valid. | |
161 CREATE_HISTOGRAM_INVALID_COUNTS_ARRAY, | |
162 | |
163 // Could not allocate histogram memory due to corruption. | |
164 CREATE_HISTOGRAM_ALLOCATOR_CORRUPT, | |
165 | |
166 // Could not allocate histogram memory due to lack of space. | |
167 CREATE_HISTOGRAM_ALLOCATOR_FULL, | |
168 | |
169 // Could not allocate histogram memory due to unknown error. | |
170 CREATE_HISTOGRAM_ALLOCATOR_ERROR, | |
171 | |
172 // Histogram was of unknown type. | |
173 CREATE_HISTOGRAM_UNKNOWN_TYPE, | |
174 | |
175 // Instance has detected a corrupt allocator (recorded only once). | |
176 CREATE_HISTOGRAM_ALLOCATOR_NEWLY_CORRUPT, | |
177 | |
178 // Always keep this at the end. | |
179 CREATE_HISTOGRAM_MAX | |
180 }; | |
181 | |
182 // The structure used to hold histogram data in persistent memory. It is | |
183 // defined and used entirely within the .cc file. | |
184 struct PersistentHistogramData; | |
185 | |
186 // Get the next histogram in persistent data based on iterator while | |
187 // ignoring a particular reference if it is found. | |
188 scoped_ptr<HistogramBase> GetNextHistogramWithIgnore( | |
189 Iterator* iter, | |
190 Reference ignore); | |
191 | |
192 // Create a histogram based on saved (persistent) information about it. | |
193 scoped_ptr<HistogramBase> CreateHistogram( | |
194 PersistentHistogramData* histogram_data_ptr); | |
195 | |
196 // Record the result of a histogram creation. | |
197 static void RecordCreateHistogramResult(CreateHistogramResultType result); | |
198 | |
199 // The memory allocator that provides the actual histogram storage. | |
200 scoped_ptr<PersistentMemoryAllocator> memory_allocator_; | |
201 | |
202 // A reference to the last-created histogram in the allocator, used to avoid | |
203 // trying to import what was just created. | |
204 subtle::AtomicWord last_created_ = 0; | |
205 | |
206 DISALLOW_COPY_AND_ASSIGN(PersistentHistogramAllocator); | |
207 }; | |
208 | |
209 } // namespace base | |
210 | |
211 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ | |
OLD | NEW |