Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: chrome/installer/setup/installer_metrics.cc

Issue 1738063002: Refactor histogram_persistence to be a class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed review comments by Alexi and temporarily re-order methods for easier review Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/histogram_persistence.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::SetPersistentHistogramMemoryAllocator( 18 base::PersistentHistogramAllocator::SetGlobalAllocator(
19 new base::LocalPersistentMemoryAllocator( 19 make_scoped_ptr(new base::PersistentHistogramAllocator(
20 1 << 20, 0, // 1 MiB 20 make_scoped_ptr(new base::LocalPersistentMemoryAllocator(
21 installer::kSetupHistogramAllocatorName)); 21 1 << 20, 0, // 1 MiB
22 base::GetPersistentHistogramMemoryAllocator()->CreateTrackingHistograms( 22 installer::kSetupHistogramAllocatorName)))));
23 installer::kSetupHistogramAllocatorName); 23 base::PersistentHistogramAllocator::GetGlobalAllocator()
24 ->CreateTrackingHistograms(installer::kSetupHistogramAllocatorName);
24 } 25 }
25 26
26 void EndPersistentHistogramStorage(const base::FilePath& target_path) { 27 void EndPersistentHistogramStorage(const base::FilePath& target_path) {
28 base::PersistentHistogramAllocator::GetGlobalAllocator()
Alexei Svitkine (slow) 2016/03/07 16:40:43 Nit: Save a pointer to base::PersistentHistogramAl
bcwhite 2016/03/08 00:24:04 Done. Hmmm... Perhaps it's better to export the
Alexei Svitkine (slow) 2016/03/08 19:58:28 Exporting them looks good.
29 ->UpdateTrackingHistograms();
30 base::PersistentMemoryAllocator* allocator =
31 base::PersistentHistogramAllocator::GetGlobalAllocator()
32 ->memory_allocator();
33
27 // For atomicity, first write to a temporary file and then rename it. 34 // For atomicity, first write to a temporary file and then rename it.
28 // The ImportantFileWriter would be good for this except it supports only 35 // The ImportantFileWriter would be good for this except it supports only
29 // std::string for its data. 36 // std::string for its data.
30 base::PersistentMemoryAllocator* allocator =
31 base::GetPersistentHistogramMemoryAllocator();
32 allocator->UpdateTrackingHistograms();
33
34 base::FilePath file_path = target_path 37 base::FilePath file_path = target_path
35 .AppendASCII(allocator->Name()) 38 .AppendASCII(allocator->Name())
36 .AddExtension(L".pma"); 39 .AddExtension(L".pma");
37 base::FilePath tmp_file_path; 40 base::FilePath tmp_file_path;
38 base::DeleteFile(file_path, false); 41 base::DeleteFile(file_path, false);
39 42
40 if (base::CreateTemporaryFileInDir(file_path.DirName(), &tmp_file_path)) { 43 if (base::CreateTemporaryFileInDir(file_path.DirName(), &tmp_file_path)) {
41 // Allocator doesn't support more than 1GB so can never overflow. 44 // Allocator doesn't support more than 1GB so can never overflow.
42 int used = static_cast<int>(allocator->used()); 45 int used = static_cast<int>(allocator->used());
43 if (base::WriteFile(tmp_file_path, 46 if (base::WriteFile(tmp_file_path,
44 static_cast<const char*>(allocator->data()), 47 static_cast<const char*>(allocator->data()),
45 used) == used) { 48 used) == used) {
46 if (base::ReplaceFile(tmp_file_path, file_path, nullptr)) { 49 if (base::ReplaceFile(tmp_file_path, file_path, nullptr)) {
47 VLOG(1) << "Persistent histograms saved in file: " 50 VLOG(1) << "Persistent histograms saved in file: "
48 << file_path.value(); 51 << file_path.value();
49 } 52 }
50 } 53 }
51 base::DeleteFile(tmp_file_path, false); 54 base::DeleteFile(tmp_file_path, false);
52 } 55 }
53 } 56 }
54 57
55 } // namespace installer 58 } // namespace installer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698