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/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
16 #include "base/metrics/statistics_recorder.h" | 17 #include "base/metrics/statistics_recorder.h" |
17 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
18 | 19 |
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
556 // the method GetCreateHistogramResultHistogram() *before* setting | 557 // the method GetCreateHistogramResultHistogram() *before* setting |
557 // the (temporary) memory allocator via SetGlobalAllocator() so that | 558 // the (temporary) memory allocator via SetGlobalAllocator() so that |
558 // histogram is instead allocated from the process heap. | 559 // histogram is instead allocated from the process heap. |
559 DCHECK_NE(kResultHistogram, histogram_data->name); | 560 DCHECK_NE(kResultHistogram, histogram_data->name); |
560 } | 561 } |
561 | 562 |
562 g_allocator = nullptr; | 563 g_allocator = nullptr; |
563 return WrapUnique(histogram_allocator); | 564 return WrapUnique(histogram_allocator); |
564 }; | 565 }; |
565 | 566 |
567 void GlobalHistogramAllocator::SetPersistentLocation(FilePath location) { | |
568 persistent_location_ = std::move(location); | |
569 } | |
570 | |
571 bool GlobalHistogramAllocator::WritePersistentLocation() { | |
572 #if defined(OS_NACL) | |
573 // NACL doesn't support file operations, including ImportantFileWriter. | |
574 NOTREACHED(); | |
575 return false; | |
576 #else | |
577 // Stop if no destination is set, perhaps because it has not been enabled. | |
578 if (persistent_location_.empty()) | |
579 return false; | |
Ilya Sherman
2016/04/19 00:57:46
Hmm. Could this be a DCHECK instead? That is, ar
bcwhite
2016/04/19 16:33:37
Right now, it's mostly unset because the experimen
| |
580 | |
581 StringPiece contents(static_cast<const char*>(data()), used()); | |
582 if (!ImportantFileWriter::WriteFileAtomically(persistent_location_, | |
583 contents)) { | |
584 LOG(ERROR) << "Could not write persistent histograms to file: " | |
585 << persistent_location_.value(); | |
586 return false; | |
587 } | |
588 | |
589 return true; | |
590 #endif | |
591 } | |
592 | |
566 GlobalHistogramAllocator::GlobalHistogramAllocator( | 593 GlobalHistogramAllocator::GlobalHistogramAllocator( |
567 std::unique_ptr<PersistentMemoryAllocator> memory) | 594 std::unique_ptr<PersistentMemoryAllocator> memory) |
568 : PersistentHistogramAllocator(std::move(memory)), | 595 : PersistentHistogramAllocator(std::move(memory)), |
569 import_iterator_(this) {} | 596 import_iterator_(this) {} |
570 | 597 |
571 void GlobalHistogramAllocator::ImportHistogramsToStatisticsRecorder() { | 598 void GlobalHistogramAllocator::ImportHistogramsToStatisticsRecorder() { |
572 // Skip the import if it's the histogram that was last created. Should a | 599 // Skip the import if it's the histogram that was last created. Should a |
573 // race condition cause the "last created" to be overwritten before it | 600 // race condition cause the "last created" to be overwritten before it |
574 // is recognized here then the histogram will be created and be ignored | 601 // is recognized here then the histogram will be created and be ignored |
575 // when it is detected as a duplicate by the statistics-recorder. This | 602 // when it is detected as a duplicate by the statistics-recorder. This |
576 // simple check reduces the time of creating persistent histograms by | 603 // simple check reduces the time of creating persistent histograms by |
577 // about 40%. | 604 // about 40%. |
578 Reference record_to_ignore = last_created(); | 605 Reference record_to_ignore = last_created(); |
579 | 606 |
580 // There is no lock on this because the iterator is lock-free while still | 607 // There is no lock on this because the iterator is lock-free while still |
581 // guaranteed to only return each entry only once. The StatisticsRecorder | 608 // guaranteed to only return each entry only once. The StatisticsRecorder |
582 // has its own lock so the Register operation is safe. | 609 // has its own lock so the Register operation is safe. |
583 while (true) { | 610 while (true) { |
584 std::unique_ptr<HistogramBase> histogram = | 611 std::unique_ptr<HistogramBase> histogram = |
585 import_iterator_.GetNextWithIgnore(record_to_ignore); | 612 import_iterator_.GetNextWithIgnore(record_to_ignore); |
586 if (!histogram) | 613 if (!histogram) |
587 break; | 614 break; |
588 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); | 615 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); |
589 } | 616 } |
590 } | 617 } |
591 | 618 |
592 } // namespace base | 619 } // namespace base |
OLD | NEW |