Index: chrome/installer/setup/installer_metrics.cc |
diff --git a/chrome/installer/setup/installer_metrics.cc b/chrome/installer/setup/installer_metrics.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f80e2cb749daa7749677db895c2dceb59806f711 |
--- /dev/null |
+++ b/chrome/installer/setup/installer_metrics.cc |
@@ -0,0 +1,56 @@ |
+// Copyright (c) 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 "chrome/installer/setup/installer_metrics.h" |
+ |
+#include "base/files/file_path.h" |
+#include "base/files/file_util.h" |
+#include "base/metrics/histogram_base.h" |
+#include "base/metrics/histogram_persistence.h" |
+#include "base/metrics/persistent_memory_allocator.h" |
+ |
+namespace installer { |
+ |
+void BeginPersistentHistogramStorage(const char* name) { |
grt (UTC plus 2)
2016/02/08 18:09:19
what's the advantage of taking |name| as an arg ra
bcwhite
2016/02/09 21:08:45
Each file needs a different preference name becaus
grt (UTC plus 2)
2016/02/10 16:01:53
Will setup_main.cc eventually call this multiple t
bcwhite
2016/02/10 16:11:24
Probably not. Just seemed a better abstraction.
grt (UTC plus 2)
2016/02/10 20:28:46
Sine this file is for exclusive use by the install
bcwhite
2016/02/10 21:28:43
Done.
|
+ base::SetPersistentHistogramMemoryAllocator( |
+ new base::LocalPersistentMemoryAllocator(1 << 20, 0, name)); // 1 MiB |
+ base::GetPersistentHistogramMemoryAllocator()->CreateTrackingHistograms(name); |
+} |
+ |
+ void EndPersistentHistogramStorage(const base::FilePath& target_path) { |
grt (UTC plus 2)
2016/02/08 18:09:18
nit: indentation
bcwhite
2016/02/09 21:08:46
Done.
|
+ // For atomicity, first write to a temporary file and then rename it. |
+ // The ImportantFileWriter would be good for this except it supports only |
+ // std::string for its data. |
+ base::PersistentMemoryAllocator* allocator = |
+ base::GetPersistentHistogramMemoryAllocator(); |
+ allocator->UpdateTrackingHistograms(); |
+ |
+ base::FilePath file_path = target_path |
+ .AppendASCII(allocator->Name()) |
+ .AddExtension(L".pma"); |
+ base::FilePath tmp_file_path; |
+ base::DeleteFile(file_path, false); |
+ |
+ if (base::CreateTemporaryFileInDir(file_path.DirName(), &tmp_file_path)) { |
+ base::File histograms(tmp_file_path, |
grt (UTC plus 2)
2016/02/08 18:09:18
#include "base/files/file.h"
bcwhite
2016/02/09 21:08:45
Done.
|
+ base::File::FLAG_OPEN | base::File::FLAG_WRITE); |
+ if (histograms.IsValid()) { |
+ base::PersistentMemoryAllocator* alloc = |
grt (UTC plus 2)
2016/02/08 18:09:18
is this the same as |allocator| on line 25?
bcwhite
2016/02/09 21:08:45
Heh. That's what happens when you add code at the
|
+ base::GetPersistentHistogramMemoryAllocator(); |
+ int used = static_cast<int>(alloc->used()); |
grt (UTC plus 2)
2016/02/08 18:09:19
base::checked_cast?
bcwhite
2016/02/09 21:08:45
Should be size_t.
bcwhite
2016/02/10 15:26:45
Scratch that. Write() returns int. Added comment
grt (UTC plus 2)
2016/02/10 16:01:53
File::Write takes its |size| arg as an int and ret
|
+ if (histograms.Write(0, static_cast<const char*>(alloc->data()), |
+ used) == used && |
+ histograms.Flush()) { |
+ histograms.Close(); |
+ if (base::ReplaceFile(tmp_file_path, file_path, nullptr)) { |
+ VLOG(1) << "Persistent histograms saved in file: " |
+ << file_path.AsUTF8Unsafe(); |
grt (UTC plus 2)
2016/02/08 18:09:18
.value() instead of AsUTF8Unsafe()
bcwhite
2016/02/09 21:08:45
Done.
|
+ } |
+ } |
+ } |
+ } |
+ base::DeleteFile(tmp_file_path, false); |
grt (UTC plus 2)
2016/02/08 18:09:19
move this up one line so that the file is only del
bcwhite
2016/02/09 21:08:45
Done.
|
+} |
+ |
+} // namespace installer |