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

Side by Side Diff: chrome/browser/chrome_browser_field_trials.cc

Issue 2888563005: Added support for 'spare' file that can be used at startup. (Closed)
Patch Set: rebased Created 3 years, 7 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/browser/chrome_browser_field_trials.h" 5 #include "chrome/browser/chrome_browser_field_trials.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
9 #include "base/command_line.h" 11 #include "base/command_line.h"
10 #include "base/feature_list.h" 12 #include "base/feature_list.h"
11 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
12 #include "base/metrics/field_trial.h" 14 #include "base/metrics/field_trial.h"
13 #include "base/metrics/histogram_base.h" 15 #include "base/metrics/histogram_base.h"
14 #include "base/metrics/histogram_macros.h" 16 #include "base/metrics/histogram_macros.h"
15 #include "base/metrics/persistent_histogram_allocator.h" 17 #include "base/metrics/persistent_histogram_allocator.h"
16 #include "base/path_service.h" 18 #include "base/path_service.h"
17 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
20 #include "base/task_scheduler/post_task.h"
18 #include "base/time/time.h" 21 #include "base/time/time.h"
19 #include "build/build_config.h" 22 #include "build/build_config.h"
20 #include "chrome/browser/metrics/chrome_metrics_service_client.h" 23 #include "chrome/browser/metrics/chrome_metrics_service_client.h"
21 #include "chrome/browser/metrics/chrome_metrics_services_manager_client.h" 24 #include "chrome/browser/metrics/chrome_metrics_services_manager_client.h"
22 #include "chrome/browser/tracing/background_tracing_field_trial.h" 25 #include "chrome/browser/tracing/background_tracing_field_trial.h"
23 #include "chrome/common/channel_info.h" 26 #include "chrome/common/channel_info.h"
24 #include "chrome/common/chrome_paths.h" 27 #include "chrome/common/chrome_paths.h"
25 #include "chrome/common/chrome_switches.h" 28 #include "chrome/common/chrome_switches.h"
26 #include "components/metrics/metrics_pref_names.h" 29 #include "components/metrics/metrics_pref_names.h"
27 #include "components/variations/variations_associated_data.h" 30 #include "components/variations/variations_associated_data.h"
28 31
29 #if defined(OS_ANDROID) 32 #if defined(OS_ANDROID)
30 #include "chrome/browser/chrome_browser_field_trials_mobile.h" 33 #include "chrome/browser/chrome_browser_field_trials_mobile.h"
31 #else 34 #else
32 #include "chrome/browser/chrome_browser_field_trials_desktop.h" 35 #include "chrome/browser/chrome_browser_field_trials_desktop.h"
33 #endif 36 #endif
34 37
35 namespace { 38 namespace {
36 39
40 // Creating a "spare" file for persistent metrics involves a lot of I/O and
41 // isn't important so delay the operation for a while after startup.
42 #if defined(OS_ANDROID)
43 // Android needs the spare file and also launches faster.
44 constexpr bool kSpareFileRequired = true;
45 constexpr int kSpareFileCreateDelaySeconds = 10;
46 #else
47 // Desktop may have to restore a lot of tabs so give it more time before doing
48 // non-essential work.
49 constexpr bool kSpareFileRequired = false;
50 constexpr int kSpareFileCreateDelaySeconds = 90;
Alexei Svitkine (slow) 2017/05/23 17:27:58 If spare file is not required, why should we even
bcwhite 2017/05/24 17:01:50 It's still a performance boost on all platforms, j
Alexei Svitkine (slow) 2017/05/24 17:09:53 OK, please add this to the comment in the #else to
bcwhite 2017/05/24 18:23:29 Done.
51 #endif
52
37 // Check for feature enabling the use of persistent histogram storage and 53 // Check for feature enabling the use of persistent histogram storage and
38 // enable the global allocator if so. 54 // enable the global allocator if so.
39 // TODO(bcwhite): Move this and CreateInstallerFileMetricsProvider into a new 55 // TODO(bcwhite): Move this and CreateInstallerFileMetricsProvider into a new
40 // file and make kBrowserMetricsName local to that file. 56 // file and make kBrowserMetricsName local to that file.
41 void InstantiatePersistentHistograms() { 57 void InstantiatePersistentHistograms() {
Alexei Svitkine (slow) 2017/05/23 17:27:58 This function is getting more and more complicated
bcwhite 2017/05/24 17:01:50 Sure. I tried to split it into two methods but th
42 base::FilePath metrics_dir; 58 base::FilePath metrics_dir;
43 if (!base::PathService::Get(chrome::DIR_USER_DATA, &metrics_dir)) 59 if (!base::PathService::Get(chrome::DIR_USER_DATA, &metrics_dir))
44 return; 60 return;
45 61
46 base::FilePath metrics_file, active_file; 62 base::FilePath metrics_file, active_file, spare_file;
Alexei Svitkine (slow) 2017/05/23 17:27:58 Nit: 1 per line.
bcwhite 2017/05/24 17:01:49 Done.
47 base::GlobalHistogramAllocator::ConstructFilePaths( 63 base::GlobalHistogramAllocator::ConstructFilePaths(
48 metrics_dir, ChromeMetricsServiceClient::kBrowserMetricsName, 64 metrics_dir, ChromeMetricsServiceClient::kBrowserMetricsName,
49 &metrics_file, &active_file); 65 &metrics_file, &active_file, &spare_file);
50 66
51 // Move any existing "active" file to the final name from which it will be 67 // Move any existing "active" file to the final name from which it will be
52 // read when reporting initial stability metrics. If there is no file to 68 // read when reporting initial stability metrics. If there is no file to
53 // move, remove any old, existing file from before the previous session. 69 // move, remove any old, existing file from before the previous session.
54 if (!base::ReplaceFile(active_file, metrics_file, nullptr)) 70 if (!base::ReplaceFile(active_file, metrics_file, nullptr))
55 base::DeleteFile(metrics_file, /*recursive=*/false); 71 base::DeleteFile(metrics_file, /*recursive=*/false);
56 72
57 // This is used to report results to an UMA histogram. 73 // This is used to report results to an UMA histogram.
58 enum InitResult { 74 enum InitResult {
59 LOCAL_MEMORY_SUCCESS, 75 LOCAL_MEMORY_SUCCESS,
60 LOCAL_MEMORY_FAILED, 76 LOCAL_MEMORY_FAILED,
61 MAPPED_FILE_SUCCESS, 77 MAPPED_FILE_SUCCESS,
62 MAPPED_FILE_FAILED, 78 MAPPED_FILE_FAILED,
63 MAPPED_FILE_EXISTS, 79 MAPPED_FILE_EXISTS,
80 NO_SPARE_FILE,
64 INIT_RESULT_MAX 81 INIT_RESULT_MAX
65 }; 82 };
66 InitResult result; 83 InitResult result;
67 84
68 // Create persistent/shared memory and allow histograms to be stored in 85 // Create persistent/shared memory and allow histograms to be stored in
69 // it. Memory that is not actualy used won't be physically mapped by the 86 // it. Memory that is not actualy used won't be physically mapped by the
70 // system. BrowserMetrics usage, as reported in UMA, has the 99.9 percentile 87 // system. BrowserMetrics usage, as reported in UMA, has the 99.9 percentile
71 // around 4MiB as of 2017-02-16. 88 // around 4MiB as of 2017-02-16.
72 const size_t kAllocSize = 8 << 20; // 8 MiB 89 const size_t kAllocSize = 8 << 20; // 8 MiB
73 const uint32_t kAllocId = 0x935DDD43; // SHA1(BrowserMetrics) 90 const uint32_t kAllocId = 0x935DDD43; // SHA1(BrowserMetrics)
74 std::string storage = variations::GetVariationParamValueByFeature( 91 std::string storage = variations::GetVariationParamValueByFeature(
75 base::kPersistentHistogramsFeature, "storage"); 92 base::kPersistentHistogramsFeature, "storage");
76 93
77 if (storage.empty() || storage == "MappedFile") { 94 if (storage.empty() || storage == "MappedFile") {
78 // If for some reason the existing "active" file could not be moved above 95 // If for some reason the existing "active" file could not be moved above
79 // then it is essential it be scheduled for deletion when possible and the 96 // then it is essential it be scheduled for deletion when possible and the
80 // contents ignored. Because this shouldn't happen but can on an OS like 97 // contents ignored. Because this shouldn't happen but can on an OS like
81 // Windows where another process reading the file (backup, AV, etc.) can 98 // Windows where another process reading the file (backup, AV, etc.) can
82 // prevent its alteration, it's necessary to handle this case by switching 99 // prevent its alteration, it's necessary to handle this case by switching
83 // to the equivalent of "LocalMemory" for this run. 100 // to the equivalent of "LocalMemory" for this run.
84 if (base::PathExists(active_file)) { 101 if (base::PathExists(active_file)) {
85 base::File file(active_file, base::File::FLAG_OPEN | 102 base::File file(active_file, base::File::FLAG_OPEN |
86 base::File::FLAG_READ | 103 base::File::FLAG_READ |
87 base::File::FLAG_DELETE_ON_CLOSE); 104 base::File::FLAG_DELETE_ON_CLOSE);
88 result = MAPPED_FILE_EXISTS; 105 result = MAPPED_FILE_EXISTS;
89 base::GlobalHistogramAllocator::CreateWithLocalMemory( 106 base::GlobalHistogramAllocator::CreateWithLocalMemory(
90 kAllocSize, kAllocId, 107 kAllocSize, kAllocId,
91 ChromeMetricsServiceClient::kBrowserMetricsName); 108 ChromeMetricsServiceClient::kBrowserMetricsName);
92 } else { 109 } else {
93 // Create global allocator with the "active" file. 110 // Move any sparse file into the active position.
94 if (base::GlobalHistogramAllocator::CreateWithFile( 111 base::ReplaceFile(spare_file, active_file, nullptr);
95 active_file, kAllocSize, kAllocId, 112 // Create global allocator using the "active" file.
96 ChromeMetricsServiceClient::kBrowserMetricsName)) { 113 if (kSpareFileRequired && !base::PathExists(active_file)) {
114 result = NO_SPARE_FILE;
115 base::GlobalHistogramAllocator::CreateWithLocalMemory(
116 kAllocSize, kAllocId,
117 ChromeMetricsServiceClient::kBrowserMetricsName);
118 } else if (base::GlobalHistogramAllocator::CreateWithFile(
119 active_file, kAllocSize, kAllocId,
120 ChromeMetricsServiceClient::kBrowserMetricsName)) {
97 result = MAPPED_FILE_SUCCESS; 121 result = MAPPED_FILE_SUCCESS;
98 } else { 122 } else {
99 result = MAPPED_FILE_FAILED; 123 result = MAPPED_FILE_FAILED;
100 } 124 }
101 } 125 } // clang-format on
Alexei Svitkine (slow) 2017/05/23 17:27:58 What's this comment for?
bcwhite 2017/05/24 17:01:49 Leftover from previous organization where clang-fo
126 // Schedule the creation of a "spare" file for use on the next run.
127 base::PostDelayedTaskWithTraits(
128 FROM_HERE,
129 {base::MayBlock(), base::TaskPriority::LOWEST,
130 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
131 base::BindOnce(base::IgnoreResult(
132 &base::GlobalHistogramAllocator::CreateSpareFile),
133 base::Passed(&spare_file), kAllocSize),
134 base::TimeDelta::FromSeconds(kSpareFileCreateDelaySeconds));
102 } else if (storage == "LocalMemory") { 135 } else if (storage == "LocalMemory") {
103 // Use local memory for storage even though it will not persist across 136 // Use local memory for storage even though it will not persist across
104 // an unclean shutdown. 137 // an unclean shutdown.
105 base::GlobalHistogramAllocator::CreateWithLocalMemory( 138 base::GlobalHistogramAllocator::CreateWithLocalMemory(
106 kAllocSize, kAllocId, ChromeMetricsServiceClient::kBrowserMetricsName); 139 kAllocSize, kAllocId, ChromeMetricsServiceClient::kBrowserMetricsName);
107 result = LOCAL_MEMORY_SUCCESS; 140 result = LOCAL_MEMORY_SUCCESS;
108 } else { 141 } else {
109 // Persistent metric storage is disabled. 142 // Persistent metric storage is disabled.
110 return; 143 return;
111 } 144 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 bool has_seed, 198 bool has_seed,
166 base::FeatureList* feature_list) { 199 base::FeatureList* feature_list) {
167 CreateFallbackSamplingTrialIfNeeded(has_seed, feature_list); 200 CreateFallbackSamplingTrialIfNeeded(has_seed, feature_list);
168 } 201 }
169 202
170 void ChromeBrowserFieldTrials::InstantiateDynamicTrials() { 203 void ChromeBrowserFieldTrials::InstantiateDynamicTrials() {
171 // Persistent histograms must be enabled as soon as possible. 204 // Persistent histograms must be enabled as soon as possible.
172 InstantiatePersistentHistograms(); 205 InstantiatePersistentHistograms();
173 tracing::SetupBackgroundTracingFieldTrial(); 206 tracing::SetupBackgroundTracingFieldTrial();
174 } 207 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698