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/atomicops.h" | 9 #include "base/atomicops.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 765 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 Set(WrapUnique( | 776 Set(WrapUnique( |
777 new GlobalHistogramAllocator(MakeUnique<FilePersistentMemoryAllocator>( | 777 new GlobalHistogramAllocator(MakeUnique<FilePersistentMemoryAllocator>( |
778 std::move(mmfile), size, id, name, false)))); | 778 std::move(mmfile), size, id, name, false)))); |
779 Get()->SetPersistentLocation(file_path); | 779 Get()->SetPersistentLocation(file_path); |
780 return true; | 780 return true; |
781 } | 781 } |
782 | 782 |
783 // static | 783 // static |
784 bool GlobalHistogramAllocator::CreateWithActiveFile(const FilePath& base_path, | 784 bool GlobalHistogramAllocator::CreateWithActiveFile(const FilePath& base_path, |
785 const FilePath& active_path, | 785 const FilePath& active_path, |
| 786 const FilePath& spare_path, |
786 size_t size, | 787 size_t size, |
787 uint64_t id, | 788 uint64_t id, |
788 StringPiece name) { | 789 StringPiece name) { |
| 790 // Old "active" becomes "base". |
789 if (!base::ReplaceFile(active_path, base_path, nullptr)) | 791 if (!base::ReplaceFile(active_path, base_path, nullptr)) |
790 base::DeleteFile(base_path, /*recursive=*/false); | 792 base::DeleteFile(base_path, /*recursive=*/false); |
| 793 DCHECK(!base::PathExists(active_path)); |
| 794 |
| 795 // Move any "spare" into "active". Okay to continue if file doesn't exist. |
| 796 if (!spare_path.empty()) { |
| 797 base::ReplaceFile(spare_path, active_path, nullptr); |
| 798 DCHECK(!base::PathExists(spare_path)); |
| 799 } |
791 | 800 |
792 return base::GlobalHistogramAllocator::CreateWithFile(active_path, size, id, | 801 return base::GlobalHistogramAllocator::CreateWithFile(active_path, size, id, |
793 name); | 802 name); |
794 } | 803 } |
795 | 804 |
796 // static | 805 // static |
797 bool GlobalHistogramAllocator::CreateWithActiveFileInDir(const FilePath& dir, | 806 bool GlobalHistogramAllocator::CreateWithActiveFileInDir(const FilePath& dir, |
798 size_t size, | 807 size_t size, |
799 uint64_t id, | 808 uint64_t id, |
800 StringPiece name) { | 809 StringPiece name) { |
801 FilePath base_path, active_path; | 810 FilePath base_path, active_path, spare_path; |
802 ConstructFilePaths(dir, name, &base_path, &active_path); | 811 ConstructFilePaths(dir, name, &base_path, &active_path, &spare_path); |
803 return CreateWithActiveFile(base_path, active_path, size, id, name); | 812 return CreateWithActiveFile(base_path, active_path, spare_path, size, id, |
| 813 name); |
804 } | 814 } |
805 | 815 |
806 // static | 816 // static |
807 void GlobalHistogramAllocator::ConstructFilePaths(const FilePath& dir, | 817 void GlobalHistogramAllocator::ConstructFilePaths(const FilePath& dir, |
808 StringPiece name, | 818 StringPiece name, |
809 FilePath* out_base_path, | 819 FilePath* out_base_path, |
810 FilePath* out_active_path) { | 820 FilePath* out_active_path, |
| 821 FilePath* out_spare_path) { |
811 if (out_base_path) { | 822 if (out_base_path) { |
812 *out_base_path = dir.AppendASCII(name).AddExtension( | 823 *out_base_path = dir.AppendASCII(name).AddExtension( |
813 PersistentMemoryAllocator::kFileExtension); | 824 PersistentMemoryAllocator::kFileExtension); |
814 } | 825 } |
815 if (out_active_path) { | 826 if (out_active_path) { |
816 *out_active_path = | 827 *out_active_path = |
817 dir.AppendASCII(name.as_string() + std::string("-active")) | 828 dir.AppendASCII(name.as_string() + std::string("-active")) |
818 .AddExtension(PersistentMemoryAllocator::kFileExtension); | 829 .AddExtension(PersistentMemoryAllocator::kFileExtension); |
819 } | 830 } |
| 831 if (out_spare_path) { |
| 832 *out_spare_path = |
| 833 dir.AppendASCII(name.as_string() + std::string("-spare")) |
| 834 .AddExtension(PersistentMemoryAllocator::kFileExtension); |
| 835 } |
| 836 } |
| 837 |
| 838 // static |
| 839 bool GlobalHistogramAllocator::CreateSpareFile(const FilePath& spare_path, |
| 840 size_t size) { |
| 841 FilePath temp_spare_path = spare_path.AddExtension(FILE_PATH_LITERAL(".tmp")); |
| 842 bool success = true; |
| 843 { |
| 844 File spare_file(temp_spare_path, File::FLAG_CREATE_ALWAYS | |
| 845 File::FLAG_READ | File::FLAG_WRITE); |
| 846 if (!spare_file.IsValid()) |
| 847 return false; |
| 848 |
| 849 MemoryMappedFile mmfile; |
| 850 mmfile.Initialize(std::move(spare_file), {0, static_cast<int64_t>(size)}, |
| 851 MemoryMappedFile::READ_WRITE_EXTEND); |
| 852 success = mmfile.IsValid(); |
| 853 } |
| 854 |
| 855 if (success) |
| 856 success = ReplaceFile(temp_spare_path, spare_path, nullptr); |
| 857 |
| 858 if (!success) |
| 859 DeleteFile(temp_spare_path, /*recursive=*/false); |
| 860 |
| 861 return success; |
| 862 } |
| 863 |
| 864 // static |
| 865 bool GlobalHistogramAllocator::CreateSpareFileInDir(const FilePath& dir, |
| 866 size_t size, |
| 867 StringPiece name) { |
| 868 FilePath spare_path; |
| 869 ConstructFilePaths(dir, name, nullptr, nullptr, &spare_path); |
| 870 return CreateSpareFile(spare_path, size); |
820 } | 871 } |
821 #endif // !defined(OS_NACL) | 872 #endif // !defined(OS_NACL) |
822 | 873 |
823 // static | 874 // static |
824 void GlobalHistogramAllocator::CreateWithSharedMemoryHandle( | 875 void GlobalHistogramAllocator::CreateWithSharedMemoryHandle( |
825 const SharedMemoryHandle& handle, | 876 const SharedMemoryHandle& handle, |
826 size_t size) { | 877 size_t size) { |
827 std::unique_ptr<SharedMemory> shm( | 878 std::unique_ptr<SharedMemory> shm( |
828 new SharedMemory(handle, /*readonly=*/false)); | 879 new SharedMemory(handle, /*readonly=*/false)); |
829 if (!shm->Map(size) || | 880 if (!shm->Map(size) || |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
964 while (true) { | 1015 while (true) { |
965 std::unique_ptr<HistogramBase> histogram = | 1016 std::unique_ptr<HistogramBase> histogram = |
966 import_iterator_.GetNextWithIgnore(record_to_ignore); | 1017 import_iterator_.GetNextWithIgnore(record_to_ignore); |
967 if (!histogram) | 1018 if (!histogram) |
968 break; | 1019 break; |
969 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); | 1020 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); |
970 } | 1021 } |
971 } | 1022 } |
972 | 1023 |
973 } // namespace base | 1024 } // namespace base |
OLD | NEW |