| 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 PersistentSampleMapRecords* | 110 PersistentSampleMapRecords* |
| 111 PersistentSparseHistogramDataManager::GetSampleMapRecordsWhileLocked( | 111 PersistentSparseHistogramDataManager::GetSampleMapRecordsWhileLocked( |
| 112 uint64_t id) { | 112 uint64_t id) { |
| 113 lock_.AssertAcquired(); | 113 lock_.AssertAcquired(); |
| 114 | 114 |
| 115 auto found = sample_records_.find(id); | 115 auto found = sample_records_.find(id); |
| 116 if (found != sample_records_.end()) | 116 if (found != sample_records_.end()) |
| 117 return found->second.get(); | 117 return found->second.get(); |
| 118 | 118 |
| 119 std::unique_ptr<PersistentSampleMapRecords>& samples = sample_records_[id]; | 119 std::unique_ptr<PersistentSampleMapRecords>& samples = sample_records_[id]; |
| 120 samples = WrapUnique(new PersistentSampleMapRecords(this, id)); | 120 samples = MakeUnique<PersistentSampleMapRecords>(this, id); |
| 121 return samples.get(); | 121 return samples.get(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 bool PersistentSparseHistogramDataManager::LoadRecords( | 124 bool PersistentSparseHistogramDataManager::LoadRecords( |
| 125 PersistentSampleMapRecords* sample_map_records) { | 125 PersistentSampleMapRecords* sample_map_records) { |
| 126 // DataManager must be locked in order to access the found_ field of any | 126 // DataManager must be locked in order to access the found_ field of any |
| 127 // PersistentSampleMapRecords object. | 127 // PersistentSampleMapRecords object. |
| 128 base::AutoLock auto_lock(lock_); | 128 base::AutoLock auto_lock(lock_); |
| 129 bool found = false; | 129 bool found = false; |
| 130 | 130 |
| (...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 | 663 |
| 664 GlobalHistogramAllocator::~GlobalHistogramAllocator() {} | 664 GlobalHistogramAllocator::~GlobalHistogramAllocator() {} |
| 665 | 665 |
| 666 // static | 666 // static |
| 667 void GlobalHistogramAllocator::CreateWithPersistentMemory( | 667 void GlobalHistogramAllocator::CreateWithPersistentMemory( |
| 668 void* base, | 668 void* base, |
| 669 size_t size, | 669 size_t size, |
| 670 size_t page_size, | 670 size_t page_size, |
| 671 uint64_t id, | 671 uint64_t id, |
| 672 StringPiece name) { | 672 StringPiece name) { |
| 673 Set(WrapUnique(new GlobalHistogramAllocator( | 673 Set(WrapUnique( |
| 674 WrapUnique(new PersistentMemoryAllocator( | 674 new GlobalHistogramAllocator(MakeUnique<PersistentMemoryAllocator>( |
| 675 base, size, page_size, id, name, false))))); | 675 base, size, page_size, id, name, false)))); |
| 676 } | 676 } |
| 677 | 677 |
| 678 // static | 678 // static |
| 679 void GlobalHistogramAllocator::CreateWithLocalMemory( | 679 void GlobalHistogramAllocator::CreateWithLocalMemory( |
| 680 size_t size, | 680 size_t size, |
| 681 uint64_t id, | 681 uint64_t id, |
| 682 StringPiece name) { | 682 StringPiece name) { |
| 683 Set(WrapUnique(new GlobalHistogramAllocator( | 683 Set(WrapUnique(new GlobalHistogramAllocator( |
| 684 WrapUnique(new LocalPersistentMemoryAllocator(size, id, name))))); | 684 MakeUnique<LocalPersistentMemoryAllocator>(size, id, name)))); |
| 685 } | 685 } |
| 686 | 686 |
| 687 #if !defined(OS_NACL) | 687 #if !defined(OS_NACL) |
| 688 // static | 688 // static |
| 689 void GlobalHistogramAllocator::CreateWithFile( | 689 void GlobalHistogramAllocator::CreateWithFile( |
| 690 const FilePath& file_path, | 690 const FilePath& file_path, |
| 691 size_t size, | 691 size_t size, |
| 692 uint64_t id, | 692 uint64_t id, |
| 693 StringPiece name) { | 693 StringPiece name) { |
| 694 bool exists = PathExists(file_path); | 694 bool exists = PathExists(file_path); |
| 695 File file( | 695 File file( |
| 696 file_path, File::FLAG_OPEN_ALWAYS | File::FLAG_SHARE_DELETE | | 696 file_path, File::FLAG_OPEN_ALWAYS | File::FLAG_SHARE_DELETE | |
| 697 File::FLAG_READ | File::FLAG_WRITE); | 697 File::FLAG_READ | File::FLAG_WRITE); |
| 698 | 698 |
| 699 std::unique_ptr<MemoryMappedFile> mmfile(new MemoryMappedFile()); | 699 std::unique_ptr<MemoryMappedFile> mmfile(new MemoryMappedFile()); |
| 700 if (exists) { | 700 if (exists) { |
| 701 mmfile->Initialize(std::move(file), MemoryMappedFile::READ_WRITE); | 701 mmfile->Initialize(std::move(file), MemoryMappedFile::READ_WRITE); |
| 702 } else { | 702 } else { |
| 703 mmfile->Initialize(std::move(file), {0, static_cast<int64_t>(size)}, | 703 mmfile->Initialize(std::move(file), {0, static_cast<int64_t>(size)}, |
| 704 MemoryMappedFile::READ_WRITE_EXTEND); | 704 MemoryMappedFile::READ_WRITE_EXTEND); |
| 705 } | 705 } |
| 706 if (!mmfile->IsValid() || | 706 if (!mmfile->IsValid() || |
| 707 !FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile, true)) { | 707 !FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile, true)) { |
| 708 NOTREACHED(); | 708 NOTREACHED(); |
| 709 return; | 709 return; |
| 710 } | 710 } |
| 711 | 711 |
| 712 Set(WrapUnique(new GlobalHistogramAllocator( | 712 Set(WrapUnique( |
| 713 WrapUnique(new FilePersistentMemoryAllocator( | 713 new GlobalHistogramAllocator(MakeUnique<FilePersistentMemoryAllocator>( |
| 714 std::move(mmfile), size, id, name, false))))); | 714 std::move(mmfile), size, id, name, false)))); |
| 715 } | 715 } |
| 716 #endif | 716 #endif |
| 717 | 717 |
| 718 // static | 718 // static |
| 719 void GlobalHistogramAllocator::CreateWithSharedMemory( | 719 void GlobalHistogramAllocator::CreateWithSharedMemory( |
| 720 std::unique_ptr<SharedMemory> memory, | 720 std::unique_ptr<SharedMemory> memory, |
| 721 size_t size, | 721 size_t size, |
| 722 uint64_t id, | 722 uint64_t id, |
| 723 StringPiece name) { | 723 StringPiece name) { |
| 724 if ((!memory->memory() && !memory->Map(size)) || | 724 if ((!memory->memory() && !memory->Map(size)) || |
| 725 !SharedPersistentMemoryAllocator::IsSharedMemoryAcceptable(*memory)) { | 725 !SharedPersistentMemoryAllocator::IsSharedMemoryAcceptable(*memory)) { |
| 726 NOTREACHED(); | 726 NOTREACHED(); |
| 727 return; | 727 return; |
| 728 } | 728 } |
| 729 | 729 |
| 730 DCHECK_LE(memory->mapped_size(), size); | 730 DCHECK_LE(memory->mapped_size(), size); |
| 731 Set(WrapUnique(new GlobalHistogramAllocator( | 731 Set(WrapUnique( |
| 732 WrapUnique(new SharedPersistentMemoryAllocator( | 732 new GlobalHistogramAllocator(MakeUnique<SharedPersistentMemoryAllocator>( |
| 733 std::move(memory), 0, StringPiece(), /*readonly=*/false))))); | 733 std::move(memory), 0, StringPiece(), /*readonly=*/false)))); |
| 734 } | 734 } |
| 735 | 735 |
| 736 // static | 736 // static |
| 737 void GlobalHistogramAllocator::CreateWithSharedMemoryHandle( | 737 void GlobalHistogramAllocator::CreateWithSharedMemoryHandle( |
| 738 const SharedMemoryHandle& handle, | 738 const SharedMemoryHandle& handle, |
| 739 size_t size) { | 739 size_t size) { |
| 740 std::unique_ptr<SharedMemory> shm( | 740 std::unique_ptr<SharedMemory> shm( |
| 741 new SharedMemory(handle, /*readonly=*/false)); | 741 new SharedMemory(handle, /*readonly=*/false)); |
| 742 if (!shm->Map(size) || | 742 if (!shm->Map(size) || |
| 743 !SharedPersistentMemoryAllocator::IsSharedMemoryAcceptable(*shm)) { | 743 !SharedPersistentMemoryAllocator::IsSharedMemoryAcceptable(*shm)) { |
| 744 NOTREACHED(); | 744 NOTREACHED(); |
| 745 return; | 745 return; |
| 746 } | 746 } |
| 747 | 747 |
| 748 Set(WrapUnique(new GlobalHistogramAllocator( | 748 Set(WrapUnique( |
| 749 WrapUnique(new SharedPersistentMemoryAllocator( | 749 new GlobalHistogramAllocator(MakeUnique<SharedPersistentMemoryAllocator>( |
| 750 std::move(shm), 0, StringPiece(), /*readonly=*/false))))); | 750 std::move(shm), 0, StringPiece(), /*readonly=*/false)))); |
| 751 } | 751 } |
| 752 | 752 |
| 753 // static | 753 // static |
| 754 void GlobalHistogramAllocator::Set( | 754 void GlobalHistogramAllocator::Set( |
| 755 std::unique_ptr<GlobalHistogramAllocator> allocator) { | 755 std::unique_ptr<GlobalHistogramAllocator> allocator) { |
| 756 // Releasing or changing an allocator is extremely dangerous because it | 756 // Releasing or changing an allocator is extremely dangerous because it |
| 757 // likely has histograms stored within it. If the backing memory is also | 757 // likely has histograms stored within it. If the backing memory is also |
| 758 // also released, future accesses to those histograms will seg-fault. | 758 // also released, future accesses to those histograms will seg-fault. |
| 759 CHECK(!g_allocator); | 759 CHECK(!g_allocator); |
| 760 g_allocator = allocator.release(); | 760 g_allocator = allocator.release(); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 857 while (true) { | 857 while (true) { |
| 858 std::unique_ptr<HistogramBase> histogram = | 858 std::unique_ptr<HistogramBase> histogram = |
| 859 import_iterator_.GetNextWithIgnore(record_to_ignore); | 859 import_iterator_.GetNextWithIgnore(record_to_ignore); |
| 860 if (!histogram) | 860 if (!histogram) |
| 861 break; | 861 break; |
| 862 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); | 862 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); |
| 863 } | 863 } |
| 864 } | 864 } |
| 865 | 865 |
| 866 } // namespace base | 866 } // namespace base |
| OLD | NEW |