Index: base/metrics/persistent_metrics_file_util.cc |
diff --git a/base/metrics/persistent_metrics_file_util.cc b/base/metrics/persistent_metrics_file_util.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..beaf0a5f06e3090a58c0d996919a4f52b032ef29 |
--- /dev/null |
+++ b/base/metrics/persistent_metrics_file_util.cc |
@@ -0,0 +1,66 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/metrics/persistent_metrics_file_util.h" |
+ |
+#include "base/files/file_path.h" |
+#include "base/files/file_util.h" |
+#include "base/metrics/persistent_histogram_allocator.h" |
+#include "base/metrics/persistent_memory_allocator.h" |
+ |
+namespace base { |
+ |
+void InitializeGlobalPersistentMetricsStorage(const base::FilePath& dir, |
+ const char* metrics_name, |
+ size_t size) { |
+ // Metrics maps one file that we're going to use during this run (the |
+ // "-active" one), and provides the previous one to the metrics system for for |
+ // association with the previous run. On startup, we move the current -active |
+ // to the base name, and then start this session on the -active one. Normally, |
+ // when the browser starts it will read and delete the base name one, but in |
+ // the case that something goes wrong, we will overwrite/delete any old one. |
+ base::FilePath metrics_file = |
+ dir.AppendASCII(metrics_name) |
+ .AddExtension(PersistentMemoryAllocator::kFileExtension); |
+ base::FilePath active_file = |
+ dir.AppendASCII(metrics_name + std::string("-active")) |
+ .AddExtension(PersistentMemoryAllocator::kFileExtension); |
+ |
+ if (!base::ReplaceFile(active_file, metrics_file, nullptr)) |
+ base::DeleteFile(metrics_file, /*recursive=*/false); |
+ |
+ base::GlobalHistogramAllocator::CreateWithFile(active_file, size, 0, |
+ metrics_name); |
+ |
+ // Get the allocator that was just created and report result. Exit if the |
+ // allocator could not be created. |
+ base::GlobalHistogramAllocator* allocator = |
+ base::GlobalHistogramAllocator::Get(); |
+ if (!allocator) |
+ return; |
+ |
+ // Create tracking histograms for the allocator and record storage file. |
+ allocator->CreateTrackingHistograms(metrics_name); |
+ allocator->SetPersistentLocation(active_file); |
+} |
+ |
+void CleanUpGlobalPersistentHistogramStorage() { |
+ base::GlobalHistogramAllocator* allocator = |
+ base::GlobalHistogramAllocator::Get(); |
+ if (!allocator) |
+ return; |
+ |
+ const base::FilePath& path = allocator->GetPersistentLocation(); |
+ if (path.empty()) |
+ return; |
+ |
+ // Open (with delete) and then immediately close the file by going out of |
+ // scope. This is the only cross-platform safe way to delete a file that may |
+ // be open elsewhere. Open handles will continue to operate normally but |
+ // new opens will not be possible. |
+ base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ | |
Mark Mentovai
2016/09/13 21:34:06
Ha, there’s a river here at column 24!
scottmg
2016/09/13 21:40:50
Yeah, I was surprised by that too, but I guess cla
|
+ base::File::FLAG_DELETE_ON_CLOSE); |
+} |
+ |
+} // namespace base |