| 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 "chrome/installer/setup/installer_metrics.h" | 5 #include "chrome/installer/setup/installer_metrics.h" |
| 6 | 6 |
| 7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/metrics/histogram_base.h" | 10 #include "base/metrics/histogram_base.h" |
| 11 #include "base/metrics/persistent_histogram_allocator.h" | 11 #include "base/metrics/persistent_histogram_allocator.h" |
| 12 #include "base/metrics/persistent_memory_allocator.h" | 12 #include "base/metrics/persistent_memory_allocator.h" |
| 13 #include "chrome/installer/util/util_constants.h" | 13 #include "chrome/installer/util/util_constants.h" |
| 14 | 14 |
| 15 namespace installer { | 15 namespace installer { |
| 16 | 16 |
| 17 void BeginPersistentHistogramStorage() { | 17 void BeginPersistentHistogramStorage() { |
| 18 base::PersistentHistogramAllocator::CreateGlobalAllocatorOnLocalMemory( | 18 base::GlobalHistogramAllocator::CreateWithLocalMemory( |
| 19 1 << 20, // 1 MiB | 19 1 << 20, // 1 MiB |
| 20 0, // No identifier. | 20 0, // No identifier. |
| 21 installer::kSetupHistogramAllocatorName); | 21 installer::kSetupHistogramAllocatorName); |
| 22 base::PersistentHistogramAllocator::GetGlobalAllocator() | 22 base::GlobalHistogramAllocator::Get()->CreateTrackingHistograms( |
| 23 ->CreateTrackingHistograms(installer::kSetupHistogramAllocatorName); | 23 installer::kSetupHistogramAllocatorName); |
| 24 | 24 |
| 25 // This can't be enabled until after the allocator is configured because | 25 // This can't be enabled until after the allocator is configured because |
| 26 // there is no other reporting out of setup other than persistent memory. | 26 // there is no other reporting out of setup other than persistent memory. |
| 27 base::HistogramBase::EnableActivityReportHistogram("setup"); | 27 base::HistogramBase::EnableActivityReportHistogram("setup"); |
| 28 } | 28 } |
| 29 | 29 |
| 30 void EndPersistentHistogramStorage(const base::FilePath& target_path) { | 30 void EndPersistentHistogramStorage(const base::FilePath& target_path) { |
| 31 base::PersistentHistogramAllocator* allocator = | 31 base::PersistentHistogramAllocator* allocator = |
| 32 base::PersistentHistogramAllocator::GetGlobalAllocator(); | 32 base::GlobalHistogramAllocator::Get(); |
| 33 allocator->UpdateTrackingHistograms(); | 33 allocator->UpdateTrackingHistograms(); |
| 34 | 34 |
| 35 // For atomicity, first write to a temporary file and then rename it. | 35 // For atomicity, first write to a temporary file and then rename it. |
| 36 // The ImportantFileWriter would be good for this except it supports only | 36 // The ImportantFileWriter would be good for this except it supports only |
| 37 // std::string for its data. | 37 // std::string for its data. |
| 38 base::FilePath file_path = target_path | 38 base::FilePath file_path = target_path |
| 39 .AppendASCII(allocator->Name()) | 39 .AppendASCII(allocator->Name()) |
| 40 .AddExtension(L".pma"); | 40 .AddExtension(L".pma"); |
| 41 base::FilePath tmp_file_path; | 41 base::FilePath tmp_file_path; |
| 42 base::DeleteFile(file_path, false); | 42 base::DeleteFile(file_path, false); |
| 43 | 43 |
| 44 if (base::CreateTemporaryFileInDir(file_path.DirName(), &tmp_file_path)) { | 44 if (base::CreateTemporaryFileInDir(file_path.DirName(), &tmp_file_path)) { |
| 45 // Allocator doesn't support more than 1GB so can never overflow. | 45 // Allocator doesn't support more than 1GB so can never overflow. |
| 46 int used = static_cast<int>(allocator->used()); | 46 int used = static_cast<int>(allocator->used()); |
| 47 if (base::WriteFile(tmp_file_path, | 47 if (base::WriteFile(tmp_file_path, |
| 48 static_cast<const char*>(allocator->data()), | 48 static_cast<const char*>(allocator->data()), |
| 49 used) == used) { | 49 used) == used) { |
| 50 if (base::ReplaceFile(tmp_file_path, file_path, nullptr)) { | 50 if (base::ReplaceFile(tmp_file_path, file_path, nullptr)) { |
| 51 VLOG(1) << "Persistent histograms saved in file: " | 51 VLOG(1) << "Persistent histograms saved in file: " |
| 52 << file_path.value(); | 52 << file_path.value(); |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 base::DeleteFile(tmp_file_path, false); | 55 base::DeleteFile(tmp_file_path, false); |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 | 58 |
| 59 } // namespace installer | 59 } // namespace installer |
| OLD | NEW |