| 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/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 879 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 890 // be open elsewhere. Open handles will continue to operate normally but | 890 // be open elsewhere. Open handles will continue to operate normally but |
| 891 // new opens will not be possible. | 891 // new opens will not be possible. |
| 892 File file(persistent_location_, | 892 File file(persistent_location_, |
| 893 File::FLAG_OPEN | File::FLAG_READ | File::FLAG_DELETE_ON_CLOSE); | 893 File::FLAG_OPEN | File::FLAG_READ | File::FLAG_DELETE_ON_CLOSE); |
| 894 #endif | 894 #endif |
| 895 } | 895 } |
| 896 | 896 |
| 897 GlobalHistogramAllocator::GlobalHistogramAllocator( | 897 GlobalHistogramAllocator::GlobalHistogramAllocator( |
| 898 std::unique_ptr<PersistentMemoryAllocator> memory) | 898 std::unique_ptr<PersistentMemoryAllocator> memory) |
| 899 : PersistentHistogramAllocator(std::move(memory)), | 899 : PersistentHistogramAllocator(std::move(memory)), |
| 900 import_iterator_(this) {} | 900 import_iterator_(this) { |
| 901 // Make sure the StatisticsRecorder is initialized to prevent duplicate |
| 902 // histograms from being created. It's safe to call this multiple times. |
| 903 StatisticsRecorder::Initialize(); |
| 904 } |
| 901 | 905 |
| 902 void GlobalHistogramAllocator::ImportHistogramsToStatisticsRecorder() { | 906 void GlobalHistogramAllocator::ImportHistogramsToStatisticsRecorder() { |
| 903 // Skip the import if it's the histogram that was last created. Should a | 907 // Skip the import if it's the histogram that was last created. Should a |
| 904 // race condition cause the "last created" to be overwritten before it | 908 // race condition cause the "last created" to be overwritten before it |
| 905 // is recognized here then the histogram will be created and be ignored | 909 // is recognized here then the histogram will be created and be ignored |
| 906 // when it is detected as a duplicate by the statistics-recorder. This | 910 // when it is detected as a duplicate by the statistics-recorder. This |
| 907 // simple check reduces the time of creating persistent histograms by | 911 // simple check reduces the time of creating persistent histograms by |
| 908 // about 40%. | 912 // about 40%. |
| 909 Reference record_to_ignore = last_created(); | 913 Reference record_to_ignore = last_created(); |
| 910 | 914 |
| 911 // There is no lock on this because the iterator is lock-free while still | 915 // There is no lock on this because the iterator is lock-free while still |
| 912 // guaranteed to only return each entry only once. The StatisticsRecorder | 916 // guaranteed to only return each entry only once. The StatisticsRecorder |
| 913 // has its own lock so the Register operation is safe. | 917 // has its own lock so the Register operation is safe. |
| 914 while (true) { | 918 while (true) { |
| 915 std::unique_ptr<HistogramBase> histogram = | 919 std::unique_ptr<HistogramBase> histogram = |
| 916 import_iterator_.GetNextWithIgnore(record_to_ignore); | 920 import_iterator_.GetNextWithIgnore(record_to_ignore); |
| 917 if (!histogram) | 921 if (!histogram) |
| 918 break; | 922 break; |
| 919 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); | 923 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); |
| 920 } | 924 } |
| 921 } | 925 } |
| 922 | 926 |
| 923 } // namespace base | 927 } // namespace base |
| OLD | NEW |