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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 size_t size); | 386 size_t size); |
387 | 387 |
388 // Sets a GlobalHistogramAllocator for globally storing histograms in | 388 // Sets a GlobalHistogramAllocator for globally storing histograms in |
389 // a space that can be persisted or shared between processes. There is only | 389 // 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. | 390 // 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 | 391 // 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 | 392 // possible during startup to capture as many histograms as possible and |
393 // while operating single-threaded so there are no race-conditions. | 393 // while operating single-threaded so there are no race-conditions. |
394 static void Set(std::unique_ptr<GlobalHistogramAllocator> allocator); | 394 static void Set(std::unique_ptr<GlobalHistogramAllocator> allocator); |
395 | 395 |
396 // Gets a pointer to the global histogram allocator. | 396 // Enables the global persistent allocator. Existing histograms will not be |
| 397 // affected; only newly created ones will go into the allocator. |
| 398 static void Enable(); |
| 399 |
| 400 // Disables the global persistent allocator. Existing histograms will not be |
| 401 // affected; newly created histograms will be allocated from the heap. |
| 402 static void Disable(); |
| 403 |
| 404 // Gets a pointer to an enabled global histogram allocator. If a global |
| 405 // allocator exists but is disabled, this will return null. |
397 static GlobalHistogramAllocator* Get(); | 406 static GlobalHistogramAllocator* Get(); |
398 | 407 |
| 408 // Gets a pointer to a global histogram allocator if one exists, even if |
| 409 // that allocator is disabled. |
| 410 static GlobalHistogramAllocator* GetEvenIfDisabled(); |
| 411 |
399 // This access to the persistent allocator is only for testing; it extracts | 412 // This access to the persistent allocator is only for testing; it extracts |
400 // the current allocator completely. This allows easy creation of histograms | 413 // the current allocator completely regardless whether it is enabled or not. |
401 // within persistent memory segments which can then be extracted and used | 414 // This allows easy creation of histograms within persistent memory segments |
402 // in other ways. | 415 // which can then be extracted and used in other ways. |
403 static std::unique_ptr<GlobalHistogramAllocator> ReleaseForTesting(); | 416 static std::unique_ptr<GlobalHistogramAllocator> ReleaseForTesting(); |
404 | 417 |
| 418 // Stores a pathname to which the contents of this allocator should be saved |
| 419 // in order to persist the data for a later use. |
| 420 void SetPersistentLocation(const FilePath& location); |
| 421 |
| 422 // Writes the internal data to a previously set location. This is generally |
| 423 // called when a process is exiting from a section of code that may not know |
| 424 // the filesystem. The data is written in an atomic manner. The return value |
| 425 // indicates success. |
| 426 bool WriteToPersistentLocation(); |
| 427 |
405 private: | 428 private: |
406 friend class StatisticsRecorder; | 429 friend class StatisticsRecorder; |
407 | 430 |
| 431 // Creates a new global histogram allocator. It will be enabled by default. |
408 explicit GlobalHistogramAllocator( | 432 explicit GlobalHistogramAllocator( |
409 std::unique_ptr<PersistentMemoryAllocator> memory); | 433 std::unique_ptr<PersistentMemoryAllocator> memory); |
410 | 434 |
411 // Import new histograms from the global histogram allocator. It's possible | 435 // Import new histograms from the global histogram allocator. It's possible |
412 // for other processes to create histograms in the active memory segment; | 436 // 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 | 437 // 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 | 438 // 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 | 439 // this method resumes from the last entry it saw; it costs nothing if |
416 // nothing new has been added. | 440 // nothing new has been added. |
417 void ImportHistogramsToStatisticsRecorder(); | 441 void ImportHistogramsToStatisticsRecorder(); |
418 | 442 |
419 // Import always continues from where it left off, making use of a single | 443 // Import always continues from where it left off, making use of a single |
420 // iterator to continue the work. | 444 // iterator to continue the work. |
421 Iterator import_iterator_; | 445 Iterator import_iterator_; |
422 | 446 |
| 447 // The location to which the data should be persisted. |
| 448 FilePath persistent_location_; |
| 449 |
423 DISALLOW_COPY_AND_ASSIGN(GlobalHistogramAllocator); | 450 DISALLOW_COPY_AND_ASSIGN(GlobalHistogramAllocator); |
424 }; | 451 }; |
425 | 452 |
426 } // namespace base | 453 } // namespace base |
427 | 454 |
428 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ | 455 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ |
OLD | NEW |