| 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 #include "base/metrics/persistent_histogram_allocator.h" | 5 #include "base/metrics/persistent_histogram_allocator.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/files/important_file_writer.h" | 9 #include "base/files/important_file_writer.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 kTypeIdCountsArray = 0x53215530 + 1, // SHA1(CountsArray) v1 | 40 kTypeIdCountsArray = 0x53215530 + 1, // SHA1(CountsArray) v1 |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 // The current globally-active persistent allocator for all new histograms. | 43 // The current globally-active persistent allocator for all new histograms. |
| 44 // The object held here will obviously not be destructed at process exit | 44 // The object held here will obviously not be destructed at process exit |
| 45 // but that's best since PersistentMemoryAllocator objects (that underlie | 45 // but that's best since PersistentMemoryAllocator objects (that underlie |
| 46 // GlobalHistogramAllocator objects) are explicitly forbidden from doing | 46 // GlobalHistogramAllocator objects) are explicitly forbidden from doing |
| 47 // anything essential at exit anyway due to the fact that they depend on data | 47 // anything essential at exit anyway due to the fact that they depend on data |
| 48 // managed elsewhere and which could be destructed first. | 48 // managed elsewhere and which could be destructed first. |
| 49 GlobalHistogramAllocator* g_allocator = nullptr; | 49 GlobalHistogramAllocator* g_allocator = nullptr; |
| 50 bool g_allocator_enabled = false; | |
| 51 | 50 |
| 52 // Take an array of range boundaries and create a proper BucketRanges object | 51 // Take an array of range boundaries and create a proper BucketRanges object |
| 53 // which is returned to the caller. A return of nullptr indicates that the | 52 // which is returned to the caller. A return of nullptr indicates that the |
| 54 // passed boundaries are invalid. | 53 // passed boundaries are invalid. |
| 55 std::unique_ptr<BucketRanges> CreateRangesFromData( | 54 std::unique_ptr<BucketRanges> CreateRangesFromData( |
| 56 HistogramBase::Sample* ranges_data, | 55 HistogramBase::Sample* ranges_data, |
| 57 uint32_t ranges_checksum, | 56 uint32_t ranges_checksum, |
| 58 size_t count) { | 57 size_t count) { |
| 59 // To avoid racy destruction at shutdown, the following may be leaked. | 58 // To avoid racy destruction at shutdown, the following may be leaked. |
| 60 std::unique_ptr<BucketRanges> ranges(new BucketRanges(count)); | 59 std::unique_ptr<BucketRanges> ranges(new BucketRanges(count)); |
| (...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 NOTREACHED(); | 649 NOTREACHED(); |
| 651 return; | 650 return; |
| 652 } | 651 } |
| 653 | 652 |
| 654 Set(WrapUnique(new GlobalHistogramAllocator( | 653 Set(WrapUnique(new GlobalHistogramAllocator( |
| 655 WrapUnique(new SharedPersistentMemoryAllocator( | 654 WrapUnique(new SharedPersistentMemoryAllocator( |
| 656 std::move(shm), 0, StringPiece(), /*readonly=*/false))))); | 655 std::move(shm), 0, StringPiece(), /*readonly=*/false))))); |
| 657 } | 656 } |
| 658 | 657 |
| 659 // static | 658 // static |
| 660 void GlobalHistogramAllocator::Enable() { | |
| 661 DCHECK(g_allocator); | |
| 662 g_allocator_enabled = true; | |
| 663 } | |
| 664 | |
| 665 // static | |
| 666 void GlobalHistogramAllocator::Disable() { | |
| 667 DCHECK(g_allocator); | |
| 668 g_allocator_enabled = false; | |
| 669 } | |
| 670 | |
| 671 // static | |
| 672 void GlobalHistogramAllocator::Set( | 659 void GlobalHistogramAllocator::Set( |
| 673 std::unique_ptr<GlobalHistogramAllocator> allocator) { | 660 std::unique_ptr<GlobalHistogramAllocator> allocator) { |
| 674 // Releasing or changing an allocator is extremely dangerous because it | 661 // Releasing or changing an allocator is extremely dangerous because it |
| 675 // likely has histograms stored within it. If the backing memory is also | 662 // likely has histograms stored within it. If the backing memory is also |
| 676 // also released, future accesses to those histograms will seg-fault. | 663 // also released, future accesses to those histograms will seg-fault. |
| 677 CHECK(!g_allocator); | 664 CHECK(!g_allocator); |
| 678 g_allocator = allocator.release(); | 665 g_allocator = allocator.release(); |
| 679 g_allocator_enabled = true; | |
| 680 size_t existing = StatisticsRecorder::GetHistogramCount(); | 666 size_t existing = StatisticsRecorder::GetHistogramCount(); |
| 681 | 667 |
| 682 DVLOG_IF(1, existing) | 668 DVLOG_IF(1, existing) |
| 683 << existing << " histograms were created before persistence was enabled."; | 669 << existing << " histograms were created before persistence was enabled."; |
| 684 } | 670 } |
| 685 | 671 |
| 686 // static | 672 // static |
| 687 GlobalHistogramAllocator* GlobalHistogramAllocator::Get() { | 673 GlobalHistogramAllocator* GlobalHistogramAllocator::Get() { |
| 688 return g_allocator_enabled ? g_allocator : nullptr; | |
| 689 } | |
| 690 | |
| 691 // static | |
| 692 GlobalHistogramAllocator* GlobalHistogramAllocator::GetEvenIfDisabled() { | |
| 693 return g_allocator; | 674 return g_allocator; |
| 694 } | 675 } |
| 695 | 676 |
| 696 // static | 677 // static |
| 697 std::unique_ptr<GlobalHistogramAllocator> | 678 std::unique_ptr<GlobalHistogramAllocator> |
| 698 GlobalHistogramAllocator::ReleaseForTesting() { | 679 GlobalHistogramAllocator::ReleaseForTesting() { |
| 699 GlobalHistogramAllocator* histogram_allocator = g_allocator; | 680 GlobalHistogramAllocator* histogram_allocator = g_allocator; |
| 700 if (!histogram_allocator) | 681 if (!histogram_allocator) |
| 701 return nullptr; | 682 return nullptr; |
| 702 PersistentMemoryAllocator* memory_allocator = | 683 PersistentMemoryAllocator* memory_allocator = |
| (...skipping 23 matching lines...) Expand all Loading... |
| 726 | 707 |
| 727 g_allocator = nullptr; | 708 g_allocator = nullptr; |
| 728 return WrapUnique(histogram_allocator); | 709 return WrapUnique(histogram_allocator); |
| 729 }; | 710 }; |
| 730 | 711 |
| 731 void GlobalHistogramAllocator::SetPersistentLocation(const FilePath& location) { | 712 void GlobalHistogramAllocator::SetPersistentLocation(const FilePath& location) { |
| 732 persistent_location_ = location; | 713 persistent_location_ = location; |
| 733 } | 714 } |
| 734 | 715 |
| 735 bool GlobalHistogramAllocator::WriteToPersistentLocation() { | 716 bool GlobalHistogramAllocator::WriteToPersistentLocation() { |
| 736 DCHECK(g_allocator_enabled); | |
| 737 | |
| 738 #if defined(OS_NACL) | 717 #if defined(OS_NACL) |
| 739 // NACL doesn't support file operations, including ImportantFileWriter. | 718 // NACL doesn't support file operations, including ImportantFileWriter. |
| 740 NOTREACHED(); | 719 NOTREACHED(); |
| 741 return false; | 720 return false; |
| 742 #else | 721 #else |
| 743 // Stop if no destination is set. | 722 // Stop if no destination is set. |
| 744 if (persistent_location_.empty()) { | 723 if (persistent_location_.empty()) { |
| 745 NOTREACHED() << "Could not write \"" << Name() << "\" persistent histograms" | 724 NOTREACHED() << "Could not write \"" << Name() << "\" persistent histograms" |
| 746 << " to file because no location was set."; | 725 << " to file because no location was set."; |
| 747 return false; | 726 return false; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 779 while (true) { | 758 while (true) { |
| 780 std::unique_ptr<HistogramBase> histogram = | 759 std::unique_ptr<HistogramBase> histogram = |
| 781 import_iterator_.GetNextWithIgnore(record_to_ignore); | 760 import_iterator_.GetNextWithIgnore(record_to_ignore); |
| 782 if (!histogram) | 761 if (!histogram) |
| 783 break; | 762 break; |
| 784 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); | 763 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); |
| 785 } | 764 } |
| 786 } | 765 } |
| 787 | 766 |
| 788 } // namespace base | 767 } // namespace base |
| OLD | NEW |