Chromium Code Reviews| 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/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 13 #include "base/metrics/histogram_base.h" | 14 #include "base/metrics/histogram_base.h" |
| 14 #include "base/metrics/histogram_samples.h" | 15 #include "base/metrics/histogram_samples.h" |
| 15 #include "base/metrics/persistent_sample_map.h" | 16 #include "base/metrics/persistent_sample_map.h" |
| 16 #include "base/metrics/sparse_histogram.h" | 17 #include "base/metrics/sparse_histogram.h" |
| 17 #include "base/metrics/statistics_recorder.h" | 18 #include "base/metrics/statistics_recorder.h" |
| 18 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 39 kTypeIdCountsArray = 0x53215530 + 1, // SHA1(CountsArray) v1 | 40 kTypeIdCountsArray = 0x53215530 + 1, // SHA1(CountsArray) v1 |
| 40 }; | 41 }; |
| 41 | 42 |
| 42 // The current globally-active persistent allocator for all new histograms. | 43 // The current globally-active persistent allocator for all new histograms. |
| 43 // 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 |
| 44 // but that's best since PersistentMemoryAllocator objects (that underlie | 45 // but that's best since PersistentMemoryAllocator objects (that underlie |
| 45 // GlobalHistogramAllocator objects) are explicitly forbidden from doing | 46 // GlobalHistogramAllocator objects) are explicitly forbidden from doing |
| 46 // 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 |
| 47 // managed elsewhere and which could be destructed first. | 48 // managed elsewhere and which could be destructed first. |
| 48 GlobalHistogramAllocator* g_allocator; | 49 GlobalHistogramAllocator* g_allocator; |
| 50 bool g_allocator_enabled; | |
|
Ilya Sherman
2016/04/27 20:31:33
Sorry, I'm going to go back to my previous questio
bcwhite
2016/05/02 14:46:47
It would have to be tested by CreateHistogram() wh
Ilya Sherman
2016/05/05 22:22:52
g_allocator_enabled is only accessed internally to
bcwhite
2016/05/06 16:59:27
That would work just fine. The difference would b
| |
| 49 | 51 |
| 50 // Take an array of range boundaries and create a proper BucketRanges object | 52 // Take an array of range boundaries and create a proper BucketRanges object |
| 51 // which is returned to the caller. A return of nullptr indicates that the | 53 // which is returned to the caller. A return of nullptr indicates that the |
| 52 // passed boundaries are invalid. | 54 // passed boundaries are invalid. |
| 53 std::unique_ptr<BucketRanges> CreateRangesFromData( | 55 std::unique_ptr<BucketRanges> CreateRangesFromData( |
| 54 HistogramBase::Sample* ranges_data, | 56 HistogramBase::Sample* ranges_data, |
| 55 uint32_t ranges_checksum, | 57 uint32_t ranges_checksum, |
| 56 size_t count) { | 58 size_t count) { |
| 57 // To avoid racy destruction at shutdown, the following may be leaked. | 59 // To avoid racy destruction at shutdown, the following may be leaked. |
| 58 std::unique_ptr<BucketRanges> ranges(new BucketRanges(count)); | 60 std::unique_ptr<BucketRanges> ranges(new BucketRanges(count)); |
| (...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 644 NOTREACHED(); | 646 NOTREACHED(); |
| 645 return; | 647 return; |
| 646 } | 648 } |
| 647 | 649 |
| 648 Set(WrapUnique(new GlobalHistogramAllocator( | 650 Set(WrapUnique(new GlobalHistogramAllocator( |
| 649 WrapUnique(new SharedPersistentMemoryAllocator( | 651 WrapUnique(new SharedPersistentMemoryAllocator( |
| 650 std::move(shm), 0, StringPiece(), /*readonly=*/false))))); | 652 std::move(shm), 0, StringPiece(), /*readonly=*/false))))); |
| 651 } | 653 } |
| 652 | 654 |
| 653 // static | 655 // static |
| 656 void GlobalHistogramAllocator::Enable() { | |
| 657 DCHECK(g_allocator); | |
| 658 g_allocator_enabled = true; | |
| 659 } | |
| 660 | |
| 661 // static | |
| 662 void GlobalHistogramAllocator::Disable() { | |
| 663 DCHECK(g_allocator); | |
| 664 g_allocator_enabled = false; | |
| 665 } | |
| 666 | |
| 667 // static | |
| 654 void GlobalHistogramAllocator::Set( | 668 void GlobalHistogramAllocator::Set( |
| 655 std::unique_ptr<GlobalHistogramAllocator> allocator) { | 669 std::unique_ptr<GlobalHistogramAllocator> allocator) { |
| 656 // Releasing or changing an allocator is extremely dangerous because it | 670 // Releasing or changing an allocator is extremely dangerous because it |
| 657 // likely has histograms stored within it. If the backing memory is also | 671 // likely has histograms stored within it. If the backing memory is also |
| 658 // also released, future accesses to those histograms will seg-fault. | 672 // also released, future accesses to those histograms will seg-fault. |
| 659 CHECK(!g_allocator); | 673 CHECK(!g_allocator); |
| 660 g_allocator = allocator.release(); | 674 g_allocator = allocator.release(); |
| 675 g_allocator_enabled = true; | |
| 661 size_t existing = StatisticsRecorder::GetHistogramCount(); | 676 size_t existing = StatisticsRecorder::GetHistogramCount(); |
| 662 | 677 |
| 663 DLOG_IF(WARNING, existing) | 678 DLOG_IF(WARNING, existing) |
| 664 << existing << " histograms were created before persistence was enabled."; | 679 << existing << " histograms were created before persistence was enabled."; |
| 665 } | 680 } |
| 666 | 681 |
| 667 // static | 682 // static |
| 668 GlobalHistogramAllocator* GlobalHistogramAllocator::Get() { | 683 GlobalHistogramAllocator* GlobalHistogramAllocator::Get() { |
| 684 return g_allocator_enabled ? g_allocator : nullptr; | |
| 685 } | |
| 686 | |
| 687 // static | |
| 688 GlobalHistogramAllocator* GlobalHistogramAllocator::GetEvenIfDisabled() { | |
| 669 return g_allocator; | 689 return g_allocator; |
| 670 } | 690 } |
| 671 | 691 |
| 672 // static | 692 // static |
| 673 std::unique_ptr<GlobalHistogramAllocator> | 693 std::unique_ptr<GlobalHistogramAllocator> |
| 674 GlobalHistogramAllocator::ReleaseForTesting() { | 694 GlobalHistogramAllocator::ReleaseForTesting() { |
| 675 GlobalHistogramAllocator* histogram_allocator = g_allocator; | 695 GlobalHistogramAllocator* histogram_allocator = g_allocator; |
| 676 if (!histogram_allocator) | 696 if (!histogram_allocator) |
| 677 return nullptr; | 697 return nullptr; |
| 678 PersistentMemoryAllocator* memory_allocator = | 698 PersistentMemoryAllocator* memory_allocator = |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 697 // the method GetCreateHistogramResultHistogram() *before* setting | 717 // the method GetCreateHistogramResultHistogram() *before* setting |
| 698 // the (temporary) memory allocator via SetGlobalAllocator() so that | 718 // the (temporary) memory allocator via SetGlobalAllocator() so that |
| 699 // histogram is instead allocated from the process heap. | 719 // histogram is instead allocated from the process heap. |
| 700 DCHECK_NE(kResultHistogram, histogram_data->name); | 720 DCHECK_NE(kResultHistogram, histogram_data->name); |
| 701 } | 721 } |
| 702 | 722 |
| 703 g_allocator = nullptr; | 723 g_allocator = nullptr; |
| 704 return WrapUnique(histogram_allocator); | 724 return WrapUnique(histogram_allocator); |
| 705 }; | 725 }; |
| 706 | 726 |
| 727 void GlobalHistogramAllocator::SetPersistentLocation(const FilePath& location) { | |
| 728 persistent_location_ = location; | |
| 729 } | |
| 730 | |
| 731 bool GlobalHistogramAllocator::WriteToPersistentLocation() { | |
| 732 #if defined(OS_NACL) | |
| 733 // NACL doesn't support file operations, including ImportantFileWriter. | |
| 734 NOTREACHED(); | |
| 735 return false; | |
| 736 #else | |
|
Ilya Sherman
2016/04/27 20:31:33
Please DCHECK(g_allocator_enabled) in this code.
bcwhite
2016/05/02 14:46:47
Done.
| |
| 737 // Stop if no destination is set, perhaps because it has not been enabled. | |
| 738 if (persistent_location_.empty()) { | |
| 739 NOTREACHED() << "Could not write \"" << Name() << "\" persistent histograms" | |
| 740 << " to file because no location was set."; | |
| 741 return false; | |
| 742 } | |
| 743 | |
| 744 StringPiece contents(static_cast<const char*>(data()), used()); | |
| 745 if (!ImportantFileWriter::WriteFileAtomically(persistent_location_, | |
| 746 contents)) { | |
| 747 LOG(ERROR) << "Could not write \"" << Name() << "\" persistent histograms" | |
| 748 << " to file: " << persistent_location_.value(); | |
| 749 return false; | |
| 750 } | |
| 751 | |
| 752 return true; | |
| 753 #endif | |
| 754 } | |
| 755 | |
| 707 GlobalHistogramAllocator::GlobalHistogramAllocator( | 756 GlobalHistogramAllocator::GlobalHistogramAllocator( |
| 708 std::unique_ptr<PersistentMemoryAllocator> memory) | 757 std::unique_ptr<PersistentMemoryAllocator> memory) |
| 709 : PersistentHistogramAllocator(std::move(memory)), | 758 : PersistentHistogramAllocator(std::move(memory)), |
| 710 import_iterator_(this) {} | 759 import_iterator_(this) {} |
| 711 | 760 |
| 712 void GlobalHistogramAllocator::ImportHistogramsToStatisticsRecorder() { | 761 void GlobalHistogramAllocator::ImportHistogramsToStatisticsRecorder() { |
| 713 // Skip the import if it's the histogram that was last created. Should a | 762 // Skip the import if it's the histogram that was last created. Should a |
| 714 // race condition cause the "last created" to be overwritten before it | 763 // race condition cause the "last created" to be overwritten before it |
| 715 // is recognized here then the histogram will be created and be ignored | 764 // is recognized here then the histogram will be created and be ignored |
| 716 // when it is detected as a duplicate by the statistics-recorder. This | 765 // when it is detected as a duplicate by the statistics-recorder. This |
| 717 // simple check reduces the time of creating persistent histograms by | 766 // simple check reduces the time of creating persistent histograms by |
| 718 // about 40%. | 767 // about 40%. |
| 719 Reference record_to_ignore = last_created(); | 768 Reference record_to_ignore = last_created(); |
| 720 | 769 |
| 721 // There is no lock on this because the iterator is lock-free while still | 770 // There is no lock on this because the iterator is lock-free while still |
| 722 // guaranteed to only return each entry only once. The StatisticsRecorder | 771 // guaranteed to only return each entry only once. The StatisticsRecorder |
| 723 // has its own lock so the Register operation is safe. | 772 // has its own lock so the Register operation is safe. |
| 724 while (true) { | 773 while (true) { |
| 725 std::unique_ptr<HistogramBase> histogram = | 774 std::unique_ptr<HistogramBase> histogram = |
| 726 import_iterator_.GetNextWithIgnore(record_to_ignore); | 775 import_iterator_.GetNextWithIgnore(record_to_ignore); |
| 727 if (!histogram) | 776 if (!histogram) |
| 728 break; | 777 break; |
| 729 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); | 778 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); |
| 730 } | 779 } |
| 731 } | 780 } |
| 732 | 781 |
| 733 } // namespace base | 782 } // namespace base |
| OLD | NEW |