OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/installer/setup/installer_metrics.h" |
| 6 |
| 7 #include "base/files/file.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/metrics/histogram_base.h" |
| 11 #include "base/metrics/histogram_persistence.h" |
| 12 #include "base/metrics/persistent_memory_allocator.h" |
| 13 |
| 14 namespace installer { |
| 15 |
| 16 void BeginPersistentHistogramStorage(const char* name) { |
| 17 base::SetPersistentHistogramMemoryAllocator( |
| 18 new base::LocalPersistentMemoryAllocator(1 << 20, 0, name)); // 1 MiB |
| 19 base::GetPersistentHistogramMemoryAllocator()->CreateTrackingHistograms(name); |
| 20 } |
| 21 |
| 22 void EndPersistentHistogramStorage(const base::FilePath& target_path) { |
| 23 // For atomicity, first write to a temporary file and then rename it. |
| 24 // The ImportantFileWriter would be good for this except it supports only |
| 25 // std::string for its data. |
| 26 base::PersistentMemoryAllocator* allocator = |
| 27 base::GetPersistentHistogramMemoryAllocator(); |
| 28 allocator->UpdateTrackingHistograms(); |
| 29 |
| 30 base::FilePath file_path = target_path |
| 31 .AppendASCII(allocator->Name()) |
| 32 .AddExtension(L".pma"); |
| 33 base::FilePath tmp_file_path; |
| 34 base::DeleteFile(file_path, false); |
| 35 |
| 36 if (base::CreateTemporaryFileInDir(file_path.DirName(), &tmp_file_path)) { |
| 37 base::File histograms(tmp_file_path, |
| 38 base::File::FLAG_OPEN | base::File::FLAG_WRITE); |
| 39 if (histograms.IsValid()) { |
| 40 size_t used = static_cast<int>(allocator->used()); |
| 41 if (histograms.Write(0, static_cast<const char*>(allocator->data()), |
| 42 used) == used && |
| 43 histograms.Flush()) { |
| 44 histograms.Close(); |
| 45 if (base::ReplaceFile(tmp_file_path, file_path, nullptr)) { |
| 46 VLOG(1) << "Persistent histograms saved in file: " |
| 47 << file_path.value(); |
| 48 } |
| 49 } |
| 50 histograms.Close(); |
| 51 } |
| 52 base::DeleteFile(tmp_file_path, false); |
| 53 } |
| 54 } |
| 55 |
| 56 } // namespace installer |
OLD | NEW |