| 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 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 StringPiece name); | 387 StringPiece name); |
| 388 | 388 |
| 389 // Create a global allocator using an internal block of memory of the | 389 // Create a global allocator using an internal block of memory of the |
| 390 // specified |size| taken from the heap. | 390 // specified |size| taken from the heap. |
| 391 static void CreateWithLocalMemory(size_t size, uint64_t id, StringPiece name); | 391 static void CreateWithLocalMemory(size_t size, uint64_t id, StringPiece name); |
| 392 | 392 |
| 393 #if !defined(OS_NACL) | 393 #if !defined(OS_NACL) |
| 394 // Create a global allocator by memory-mapping a |file|. If the file does | 394 // Create a global allocator by memory-mapping a |file|. If the file does |
| 395 // not exist, it will be created with the specified |size|. If the file does | 395 // not exist, it will be created with the specified |size|. If the file does |
| 396 // exist, the allocator will use and add to its contents, ignoring the passed | 396 // exist, the allocator will use and add to its contents, ignoring the passed |
| 397 // size in favor of the existing size. | 397 // size in favor of the existing size. Returns whether the global allocator |
| 398 static void CreateWithFile(const FilePath& file_path, | 398 // was set. |
| 399 static bool CreateWithFile(const FilePath& file_path, |
| 399 size_t size, | 400 size_t size, |
| 400 uint64_t id, | 401 uint64_t id, |
| 401 StringPiece name); | 402 StringPiece name); |
| 403 |
| 404 // Creates a new file at |active_path|. If it already exists, it will first be |
| 405 // moved to |base_path|. In all cases, any old file at |base_path| will be |
| 406 // removed. The file will be created using the given size, id, and name. |
| 407 // Returns whether the global allocator was set. |
| 408 static bool CreateWithActiveFile(const FilePath& base_path, |
| 409 const FilePath& active_path, |
| 410 size_t size, |
| 411 uint64_t id, |
| 412 StringPiece name); |
| 413 |
| 414 // Uses ConstructBaseActivePairFilePaths() to build a pair of file names which |
| 415 // are then used for CreateWithActiveFile(). |name| is used for both the |
| 416 // internal name for the allocator and also for the name of the file inside |
| 417 // |dir|. |
| 418 static bool CreateWithActiveFileInDir(const FilePath& dir, |
| 419 size_t size, |
| 420 uint64_t id, |
| 421 StringPiece name); |
| 422 |
| 423 // Constructs a pair of names in |dir| based on name that can be used for a |
| 424 // base + active persistent memory mapped location for CreateWithActiveFile(). |
| 425 // |name| will be used as the basename of the file inside |dir|. |
| 426 // |out_active_path| may be null if it is not needed. |
| 427 static void ConstructFilePaths(const FilePath& dir, |
| 428 StringPiece name, |
| 429 FilePath* out_base_path, |
| 430 FilePath* out_active_path); |
| 402 #endif | 431 #endif |
| 403 | 432 |
| 404 // Create a global allocator using a block of shared |memory| of the | 433 // Create a global allocator using a block of shared |memory| of the |
| 405 // specified |size|. The allocator takes ownership of the shared memory | 434 // specified |size|. The allocator takes ownership of the shared memory |
| 406 // and releases it upon destruction, though the memory will continue to | 435 // and releases it upon destruction, though the memory will continue to |
| 407 // live if other processes have access to it. | 436 // live if other processes have access to it. |
| 408 static void CreateWithSharedMemory(std::unique_ptr<SharedMemory> memory, | 437 static void CreateWithSharedMemory(std::unique_ptr<SharedMemory> memory, |
| 409 size_t size, | 438 size_t size, |
| 410 uint64_t id, | 439 uint64_t id, |
| 411 StringPiece name); | 440 StringPiece name); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 442 // Retrieves a previously set pathname to which the contents of this allocator | 471 // Retrieves a previously set pathname to which the contents of this allocator |
| 443 // are to be saved. | 472 // are to be saved. |
| 444 const FilePath& GetPersistentLocation() const; | 473 const FilePath& GetPersistentLocation() const; |
| 445 | 474 |
| 446 // Writes the internal data to a previously set location. This is generally | 475 // Writes the internal data to a previously set location. This is generally |
| 447 // called when a process is exiting from a section of code that may not know | 476 // called when a process is exiting from a section of code that may not know |
| 448 // the filesystem. The data is written in an atomic manner. The return value | 477 // the filesystem. The data is written in an atomic manner. The return value |
| 449 // indicates success. | 478 // indicates success. |
| 450 bool WriteToPersistentLocation(); | 479 bool WriteToPersistentLocation(); |
| 451 | 480 |
| 481 // If there is a global metrics file being updated on disk, mark it to be |
| 482 // deleted when the process exits. |
| 483 void DeletePersistentLocation(); |
| 484 |
| 452 private: | 485 private: |
| 453 friend class StatisticsRecorder; | 486 friend class StatisticsRecorder; |
| 454 | 487 |
| 455 // Creates a new global histogram allocator. | 488 // Creates a new global histogram allocator. |
| 456 explicit GlobalHistogramAllocator( | 489 explicit GlobalHistogramAllocator( |
| 457 std::unique_ptr<PersistentMemoryAllocator> memory); | 490 std::unique_ptr<PersistentMemoryAllocator> memory); |
| 458 | 491 |
| 459 // Import new histograms from the global histogram allocator. It's possible | 492 // Import new histograms from the global histogram allocator. It's possible |
| 460 // for other processes to create histograms in the active memory segment; | 493 // for other processes to create histograms in the active memory segment; |
| 461 // this adds those to the internal list of known histograms to avoid creating | 494 // this adds those to the internal list of known histograms to avoid creating |
| 462 // duplicates that would have to be merged during reporting. Every call to | 495 // duplicates that would have to be merged during reporting. Every call to |
| 463 // this method resumes from the last entry it saw; it costs nothing if | 496 // this method resumes from the last entry it saw; it costs nothing if |
| 464 // nothing new has been added. | 497 // nothing new has been added. |
| 465 void ImportHistogramsToStatisticsRecorder(); | 498 void ImportHistogramsToStatisticsRecorder(); |
| 466 | 499 |
| 467 // Import always continues from where it left off, making use of a single | 500 // Import always continues from where it left off, making use of a single |
| 468 // iterator to continue the work. | 501 // iterator to continue the work. |
| 469 Iterator import_iterator_; | 502 Iterator import_iterator_; |
| 470 | 503 |
| 471 // The location to which the data should be persisted. | 504 // The location to which the data should be persisted. |
| 472 FilePath persistent_location_; | 505 FilePath persistent_location_; |
| 473 | 506 |
| 474 DISALLOW_COPY_AND_ASSIGN(GlobalHistogramAllocator); | 507 DISALLOW_COPY_AND_ASSIGN(GlobalHistogramAllocator); |
| 475 }; | 508 }; |
| 476 | 509 |
| 477 } // namespace base | 510 } // namespace base |
| 478 | 511 |
| 479 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ | 512 #endif // BASE_METRICS_HISTOGRAM_PERSISTENCE_H_ |
| OLD | NEW |