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" |
11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/files/important_file_writer.h" | 12 #include "base/files/important_file_writer.h" |
13 #include "base/files/memory_mapped_file.h" | 13 #include "base/files/memory_mapped_file.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
18 #include "base/metrics/histogram_base.h" | 18 #include "base/metrics/histogram_base.h" |
19 #include "base/metrics/histogram_samples.h" | 19 #include "base/metrics/histogram_samples.h" |
20 #include "base/metrics/persistent_sample_map.h" | 20 #include "base/metrics/persistent_sample_map.h" |
21 #include "base/metrics/sparse_histogram.h" | 21 #include "base/metrics/sparse_histogram.h" |
22 #include "base/metrics/statistics_recorder.h" | 22 #include "base/metrics/statistics_recorder.h" |
23 #include "base/pickle.h" | 23 #include "base/pickle.h" |
| 24 #include "base/strings/stringprintf.h" |
24 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
25 | 26 |
26 namespace base { | 27 namespace base { |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 // Name of histogram for storing results of local operations. | 31 // Name of histogram for storing results of local operations. |
31 const char kResultHistogram[] = "UMA.CreatePersistentHistogram.Result"; | 32 const char kResultHistogram[] = "UMA.CreatePersistentHistogram.Result"; |
32 | 33 |
33 // Type identifiers used when storing in persistent memory so they can be | 34 // Type identifiers used when storing in persistent memory so they can be |
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
812 return CreateWithActiveFile(base_path, active_path, spare_path, size, id, | 813 return CreateWithActiveFile(base_path, active_path, spare_path, size, id, |
813 name); | 814 name); |
814 } | 815 } |
815 | 816 |
816 // static | 817 // static |
817 void GlobalHistogramAllocator::ConstructFilePaths(const FilePath& dir, | 818 void GlobalHistogramAllocator::ConstructFilePaths(const FilePath& dir, |
818 StringPiece name, | 819 StringPiece name, |
819 FilePath* out_base_path, | 820 FilePath* out_base_path, |
820 FilePath* out_active_path, | 821 FilePath* out_active_path, |
821 FilePath* out_spare_path) { | 822 FilePath* out_spare_path) { |
822 if (out_base_path) { | 823 if (out_base_path) |
823 *out_base_path = dir.AppendASCII(name).AddExtension( | 824 *out_base_path = MakeMetricsFilePath(dir, name); |
824 PersistentMemoryAllocator::kFileExtension); | 825 |
825 } | |
826 if (out_active_path) { | 826 if (out_active_path) { |
827 *out_active_path = | 827 *out_active_path = |
828 dir.AppendASCII(name.as_string() + std::string("-active")) | 828 MakeMetricsFilePath(dir, name.as_string().append("-active")); |
829 .AddExtension(PersistentMemoryAllocator::kFileExtension); | |
830 } | 829 } |
| 830 |
831 if (out_spare_path) { | 831 if (out_spare_path) { |
832 *out_spare_path = | 832 *out_spare_path = |
833 dir.AppendASCII(name.as_string() + std::string("-spare")) | 833 MakeMetricsFilePath(dir, name.as_string().append("-spare")); |
834 .AddExtension(PersistentMemoryAllocator::kFileExtension); | |
835 } | 834 } |
836 } | 835 } |
837 | 836 |
| 837 // static |
| 838 void GlobalHistogramAllocator::ConstructFilePathsForUploadDir( |
| 839 const FilePath& active_dir, |
| 840 const FilePath& upload_dir, |
| 841 const std::string& name, |
| 842 FilePath* out_upload_path, |
| 843 FilePath* out_active_path, |
| 844 FilePath* out_spare_path) { |
| 845 if (out_upload_path) { |
| 846 std::string name_stamp = |
| 847 StringPrintf("%s-%X", name.c_str(), |
| 848 static_cast<unsigned int>(Time::Now().ToTimeT())); |
| 849 *out_upload_path = MakeMetricsFilePath(upload_dir, name_stamp); |
| 850 } |
| 851 |
| 852 if (out_active_path) { |
| 853 *out_active_path = |
| 854 MakeMetricsFilePath(active_dir, name + std::string("-active")); |
| 855 } |
| 856 |
| 857 if (out_spare_path) { |
| 858 *out_spare_path = |
| 859 MakeMetricsFilePath(active_dir, name + std::string("-spare")); |
| 860 } |
| 861 } |
| 862 |
838 // static | 863 // static |
839 bool GlobalHistogramAllocator::CreateSpareFile(const FilePath& spare_path, | 864 bool GlobalHistogramAllocator::CreateSpareFile(const FilePath& spare_path, |
840 size_t size) { | 865 size_t size) { |
841 FilePath temp_spare_path = spare_path.AddExtension(FILE_PATH_LITERAL(".tmp")); | 866 FilePath temp_spare_path = spare_path.AddExtension(FILE_PATH_LITERAL(".tmp")); |
842 bool success = true; | 867 bool success = true; |
843 { | 868 { |
844 File spare_file(temp_spare_path, File::FLAG_CREATE_ALWAYS | | 869 File spare_file(temp_spare_path, File::FLAG_CREATE_ALWAYS | |
845 File::FLAG_READ | File::FLAG_WRITE); | 870 File::FLAG_READ | File::FLAG_WRITE); |
846 if (!spare_file.IsValid()) | 871 if (!spare_file.IsValid()) |
847 return false; | 872 return false; |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1014 // has its own lock so the Register operation is safe. | 1039 // has its own lock so the Register operation is safe. |
1015 while (true) { | 1040 while (true) { |
1016 std::unique_ptr<HistogramBase> histogram = | 1041 std::unique_ptr<HistogramBase> histogram = |
1017 import_iterator_.GetNextWithIgnore(record_to_ignore); | 1042 import_iterator_.GetNextWithIgnore(record_to_ignore); |
1018 if (!histogram) | 1043 if (!histogram) |
1019 break; | 1044 break; |
1020 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); | 1045 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); |
1021 } | 1046 } |
1022 } | 1047 } |
1023 | 1048 |
| 1049 // static |
| 1050 FilePath GlobalHistogramAllocator::MakeMetricsFilePath(const FilePath& dir, |
| 1051 StringPiece name) { |
| 1052 return dir.AppendASCII(name).AddExtension( |
| 1053 PersistentMemoryAllocator::kFileExtension); |
| 1054 } |
| 1055 |
1024 } // namespace base | 1056 } // namespace base |
OLD | NEW |