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 <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 PersistentMemoryAllocator* memory_allocator() { | 215 PersistentMemoryAllocator* memory_allocator() { |
216 return memory_allocator_.get(); | 216 return memory_allocator_.get(); |
217 } | 217 } |
218 | 218 |
219 // Implement the "metadata" API of a PersistentMemoryAllocator, forwarding | 219 // Implement the "metadata" API of a PersistentMemoryAllocator, forwarding |
220 // those requests to the real one. | 220 // those requests to the real one. |
221 uint64_t Id() const { return memory_allocator_->Id(); } | 221 uint64_t Id() const { return memory_allocator_->Id(); } |
222 const char* Name() const { return memory_allocator_->Name(); } | 222 const char* Name() const { return memory_allocator_->Name(); } |
223 const void* data() const { return memory_allocator_->data(); } | 223 const void* data() const { return memory_allocator_->data(); } |
224 size_t length() const { return memory_allocator_->length(); } | 224 size_t length() const { return memory_allocator_->length(); } |
| 225 size_t size() const { return memory_allocator_->size(); } |
225 size_t used() const { return memory_allocator_->used(); } | 226 size_t used() const { return memory_allocator_->used(); } |
226 | 227 |
227 // Recreate a Histogram from data held in persistent memory. Though this | 228 // Recreate a Histogram from data held in persistent memory. Though this |
228 // object will be local to the current process, the sample data will be | 229 // object will be local to the current process, the sample data will be |
229 // shared with all other threads referencing it. This method takes a |ref| | 230 // shared with all other threads referencing it. This method takes a |ref| |
230 // to where the top-level histogram data may be found in this allocator. | 231 // to where the top-level histogram data may be found in this allocator. |
231 // This method will return null if any problem is detected with the data. | 232 // This method will return null if any problem is detected with the data. |
232 std::unique_ptr<HistogramBase> GetHistogram(Reference ref); | 233 std::unique_ptr<HistogramBase> GetHistogram(Reference ref); |
233 | 234 |
234 // Allocate a new persistent histogram. The returned histogram will not | 235 // Allocate a new persistent histogram. The returned histogram will not |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 size_t size); | 387 size_t size); |
387 | 388 |
388 // Sets a GlobalHistogramAllocator for globally storing histograms in | 389 // Sets a GlobalHistogramAllocator for globally storing histograms in |
389 // a space that can be persisted or shared between processes. There is only | 390 // a space that can be persisted or shared between processes. There is only |
390 // ever one allocator for all such histograms created by a single process. | 391 // ever one allocator for all such histograms created by a single process. |
391 // This takes ownership of the object and should be called as soon as | 392 // This takes ownership of the object and should be called as soon as |
392 // possible during startup to capture as many histograms as possible and | 393 // possible during startup to capture as many histograms as possible and |
393 // while operating single-threaded so there are no race-conditions. | 394 // while operating single-threaded so there are no race-conditions. |
394 static void Set(std::unique_ptr<GlobalHistogramAllocator> allocator); | 395 static void Set(std::unique_ptr<GlobalHistogramAllocator> allocator); |
395 | 396 |
396 // Gets a pointer to the global histogram allocator. | 397 // Enables the global persistent allocator. Existing histograms will not be |
| 398 // affected; only newly created ones will go into the allocator. |
| 399 static void Enable(); |
| 400 |
| 401 // Disables the global persistent allocator. Existing histograms will not be |
| 402 // affected; newly created histograms will be allocated from the heap. |
| 403 static void Disable(); |
| 404 |
| 405 // Gets a pointer to an enabled global histogram allocator. If a global |
| 406 // allocator exists but is disabled, this will return null. |
397 static GlobalHistogramAllocator* Get(); | 407 static GlobalHistogramAllocator* Get(); |
398 | 408 |
| 409 // Gets a pointer to a global histogram allocator if one exists, even if |
| 410 // that allocator is disabled. |
| 411 static GlobalHistogramAllocator* GetEvenIfDisabled(); |
| 412 |
399 // This access to the persistent allocator is only for testing; it extracts | 413 // This access to the persistent allocator is only for testing; it extracts |
400 // the current allocator completely. This allows easy creation of histograms | 414 // the current allocator completely regardless whether it is enabled or not. |
401 // within persistent memory segments which can then be extracted and used | 415 // This allows easy creation of histograms within persistent memory segments |
402 // in other ways. | 416 // which can then be extracted and used in other ways. |
403 static std::unique_ptr<GlobalHistogramAllocator> ReleaseForTesting(); | 417 static std::unique_ptr<GlobalHistogramAllocator> ReleaseForTesting(); |
404 | 418 |
| 419 // Stores a pathname to which the contents of this allocator should be saved |
| 420 // in order to persist the data for a later use. |
| 421 void SetPersistentLocation(const FilePath& location); |
| 422 |
| 423 // Writes the internal data to a previously set location. This is generally |
| 424 // called when a process is exiting from a section of code that may not know |
| 425 // the filesystem. The data is written in an atomic manner. The return value |
| 426 // indicates success. |
| 427 bool WriteToPersistentLocation(); |
| 428 |
405 private: | 429 private: |
406 friend class StatisticsRecorder; | 430 friend class StatisticsRecorder; |
407 | 431 |
| 432 // Creates a new global histogram allocator. It will be enabled by default. |
408 explicit GlobalHistogramAllocator( | 433 explicit GlobalHistogramAllocator( |
409 std::unique_ptr<PersistentMemoryAllocator> memory); | 434 std::unique_ptr<PersistentMemoryAllocator> memory); |
410 | 435 |
411 // Import new histograms from the global histogram allocator. It's possible | 436 // Import new histograms from the global histogram allocator. It's possible |
412 // for other processes to create histograms in the active memory segment; | 437 // for other processes to create histograms in the active memory segment; |
413 // this adds those to the internal list of known histograms to avoid creating | 438 // this adds those to the internal list of known histograms to avoid creating |
414 // duplicates that would have to be merged during reporting. Every call to | 439 // duplicates that would have to be merged during reporting. Every call to |
415 // this method resumes from the last entry it saw; it costs nothing if | 440 // this method resumes from the last entry it saw; it costs nothing if |
416 // nothing new has been added. | 441 // nothing new has been added. |
417 void ImportHistogramsToStatisticsRecorder(); | 442 void ImportHistogramsToStatisticsRecorder(); |
418 | 443 |
419 // Import always continues from where it left off, making use of a single | 444 // Import always continues from where it left off, making use of a single |
420 // iterator to continue the work. | 445 // iterator to continue the work. |
421 Iterator import_iterator_; | 446 Iterator import_iterator_; |
422 | 447 |
| 448 // The location to which the data should be persisted. |
| 449 FilePath persistent_location_; |
| 450 |
423 DISALLOW_COPY_AND_ASSIGN(GlobalHistogramAllocator); | 451 DISALLOW_COPY_AND_ASSIGN(GlobalHistogramAllocator); |
424 }; | 452 }; |
425 | 453 |
426 } // namespace base | 454 } // namespace base |
427 | 455 |
428 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ | 456 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ |
OLD | NEW |