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 678 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 697 // the method GetCreateHistogramResultHistogram() *before* setting | 698 // the method GetCreateHistogramResultHistogram() *before* setting |
| 698 // the (temporary) memory allocator via SetGlobalAllocator() so that | 699 // the (temporary) memory allocator via SetGlobalAllocator() so that |
| 699 // histogram is instead allocated from the process heap. | 700 // histogram is instead allocated from the process heap. |
| 700 DCHECK_NE(kResultHistogram, histogram_data->name); | 701 DCHECK_NE(kResultHistogram, histogram_data->name); |
| 701 } | 702 } |
| 702 | 703 |
| 703 g_allocator = nullptr; | 704 g_allocator = nullptr; |
| 704 return WrapUnique(histogram_allocator); | 705 return WrapUnique(histogram_allocator); |
| 705 }; | 706 }; |
| 706 | 707 |
| 708 void GlobalHistogramAllocator::SetPersistentLocation(const FilePath& location) { | |
| 709 persistent_location_ = location; | |
| 710 } | |
| 711 | |
| 712 bool GlobalHistogramAllocator::WriteToPersistentLocation() { | |
| 713 #if defined(OS_NACL) | |
| 714 // NACL doesn't support file operations, including ImportantFileWriter. | |
| 715 NOTREACHED(); | |
| 716 return false; | |
| 717 #else | |
| 718 // Stop if no destination is set, perhaps because it has not been enabled. | |
| 719 if (persistent_location_.empty()) | |
|
Ilya Sherman
2016/04/20 17:10:33
I'd still prefer the location to be guaranteed to
bcwhite
2016/04/22 15:18:04
Enable/Disable: Done.
| |
| 720 return false; | |
| 721 | |
| 722 StringPiece contents(static_cast<const char*>(data()), used()); | |
| 723 if (!ImportantFileWriter::WriteFileAtomically(persistent_location_, | |
| 724 contents)) { | |
| 725 LOG(ERROR) << "Could not write persistent histograms to file: " | |
| 726 << persistent_location_.value(); | |
| 727 return false; | |
| 728 } | |
| 729 | |
| 730 return true; | |
| 731 #endif | |
| 732 } | |
| 733 | |
| 707 GlobalHistogramAllocator::GlobalHistogramAllocator( | 734 GlobalHistogramAllocator::GlobalHistogramAllocator( |
| 708 std::unique_ptr<PersistentMemoryAllocator> memory) | 735 std::unique_ptr<PersistentMemoryAllocator> memory) |
| 709 : PersistentHistogramAllocator(std::move(memory)), | 736 : PersistentHistogramAllocator(std::move(memory)), |
| 710 import_iterator_(this) {} | 737 import_iterator_(this) {} |
| 711 | 738 |
| 712 void GlobalHistogramAllocator::ImportHistogramsToStatisticsRecorder() { | 739 void GlobalHistogramAllocator::ImportHistogramsToStatisticsRecorder() { |
| 713 // Skip the import if it's the histogram that was last created. Should a | 740 // 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 | 741 // race condition cause the "last created" to be overwritten before it |
| 715 // is recognized here then the histogram will be created and be ignored | 742 // 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 | 743 // when it is detected as a duplicate by the statistics-recorder. This |
| 717 // simple check reduces the time of creating persistent histograms by | 744 // simple check reduces the time of creating persistent histograms by |
| 718 // about 40%. | 745 // about 40%. |
| 719 Reference record_to_ignore = last_created(); | 746 Reference record_to_ignore = last_created(); |
| 720 | 747 |
| 721 // There is no lock on this because the iterator is lock-free while still | 748 // 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 | 749 // guaranteed to only return each entry only once. The StatisticsRecorder |
| 723 // has its own lock so the Register operation is safe. | 750 // has its own lock so the Register operation is safe. |
| 724 while (true) { | 751 while (true) { |
| 725 std::unique_ptr<HistogramBase> histogram = | 752 std::unique_ptr<HistogramBase> histogram = |
| 726 import_iterator_.GetNextWithIgnore(record_to_ignore); | 753 import_iterator_.GetNextWithIgnore(record_to_ignore); |
| 727 if (!histogram) | 754 if (!histogram) |
| 728 break; | 755 break; |
| 729 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); | 756 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); |
| 730 } | 757 } |
| 731 } | 758 } |
| 732 | 759 |
| 733 } // namespace base | 760 } // namespace base |
| OLD | NEW |