| OLD | NEW |
| 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; |
| 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() { |
| 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; |
| 63 base::FilePath active_file; |
| 64 base::FilePath spare_file; |
| 47 base::GlobalHistogramAllocator::ConstructFilePaths( | 65 base::GlobalHistogramAllocator::ConstructFilePaths( |
| 48 metrics_dir, ChromeMetricsServiceClient::kBrowserMetricsName, | 66 metrics_dir, ChromeMetricsServiceClient::kBrowserMetricsName, |
| 49 &metrics_file, &active_file); | 67 &metrics_file, &active_file, &spare_file); |
| 50 | 68 |
| 51 // Move any existing "active" file to the final name from which it will be | 69 // 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 | 70 // read when reporting initial stability metrics. If there is no file to |
| 53 // move, remove any old, existing file from before the previous session. | 71 // move, remove any old, existing file from before the previous session. |
| 54 if (!base::ReplaceFile(active_file, metrics_file, nullptr)) | 72 if (!base::ReplaceFile(active_file, metrics_file, nullptr)) |
| 55 base::DeleteFile(metrics_file, /*recursive=*/false); | 73 base::DeleteFile(metrics_file, /*recursive=*/false); |
| 56 | 74 |
| 57 // This is used to report results to an UMA histogram. | 75 // This is used to report results to an UMA histogram. |
| 58 enum InitResult { | 76 enum InitResult { |
| 59 LOCAL_MEMORY_SUCCESS, | 77 LOCAL_MEMORY_SUCCESS, |
| 60 LOCAL_MEMORY_FAILED, | 78 LOCAL_MEMORY_FAILED, |
| 61 MAPPED_FILE_SUCCESS, | 79 MAPPED_FILE_SUCCESS, |
| 62 MAPPED_FILE_FAILED, | 80 MAPPED_FILE_FAILED, |
| 63 MAPPED_FILE_EXISTS, | 81 MAPPED_FILE_EXISTS, |
| 82 NO_SPARE_FILE, |
| 64 INIT_RESULT_MAX | 83 INIT_RESULT_MAX |
| 65 }; | 84 }; |
| 66 InitResult result; | 85 InitResult result; |
| 67 | 86 |
| 68 // Create persistent/shared memory and allow histograms to be stored in | 87 // 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 | 88 // 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 | 89 // system. BrowserMetrics usage, as reported in UMA, has the 99.9 percentile |
| 71 // around 4MiB as of 2017-02-16. | 90 // around 4MiB as of 2017-02-16. |
| 72 const size_t kAllocSize = 8 << 20; // 8 MiB | 91 const size_t kAllocSize = 8 << 20; // 8 MiB |
| 73 const uint32_t kAllocId = 0x935DDD43; // SHA1(BrowserMetrics) | 92 const uint32_t kAllocId = 0x935DDD43; // SHA1(BrowserMetrics) |
| 74 std::string storage = variations::GetVariationParamValueByFeature( | 93 std::string storage = variations::GetVariationParamValueByFeature( |
| 75 base::kPersistentHistogramsFeature, "storage"); | 94 base::kPersistentHistogramsFeature, "storage"); |
| 76 | 95 |
| 77 if (storage.empty() || storage == "MappedFile") { | 96 if (storage.empty() || storage == "MappedFile") { |
| 78 // If for some reason the existing "active" file could not be moved above | 97 // 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 | 98 // 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 | 99 // 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 | 100 // Windows where another process reading the file (backup, AV, etc.) can |
| 82 // prevent its alteration, it's necessary to handle this case by switching | 101 // prevent its alteration, it's necessary to handle this case by switching |
| 83 // to the equivalent of "LocalMemory" for this run. | 102 // to the equivalent of "LocalMemory" for this run. |
| 84 if (base::PathExists(active_file)) { | 103 if (base::PathExists(active_file)) { |
| 85 base::File file(active_file, base::File::FLAG_OPEN | | 104 base::File file(active_file, base::File::FLAG_OPEN | |
| 86 base::File::FLAG_READ | | 105 base::File::FLAG_READ | |
| 87 base::File::FLAG_DELETE_ON_CLOSE); | 106 base::File::FLAG_DELETE_ON_CLOSE); |
| 88 result = MAPPED_FILE_EXISTS; | 107 result = MAPPED_FILE_EXISTS; |
| 89 base::GlobalHistogramAllocator::CreateWithLocalMemory( | 108 base::GlobalHistogramAllocator::CreateWithLocalMemory( |
| 90 kAllocSize, kAllocId, | 109 kAllocSize, kAllocId, |
| 91 ChromeMetricsServiceClient::kBrowserMetricsName); | 110 ChromeMetricsServiceClient::kBrowserMetricsName); |
| 92 } else { | 111 } else { |
| 93 // Create global allocator with the "active" file. | 112 // Move any sparse file into the active position. |
| 94 if (base::GlobalHistogramAllocator::CreateWithFile( | 113 base::ReplaceFile(spare_file, active_file, nullptr); |
| 95 active_file, kAllocSize, kAllocId, | 114 // Create global allocator using the "active" file. |
| 96 ChromeMetricsServiceClient::kBrowserMetricsName)) { | 115 if (kSpareFileRequired && !base::PathExists(active_file)) { |
| 116 result = NO_SPARE_FILE; |
| 117 base::GlobalHistogramAllocator::CreateWithLocalMemory( |
| 118 kAllocSize, kAllocId, |
| 119 ChromeMetricsServiceClient::kBrowserMetricsName); |
| 120 } else if (base::GlobalHistogramAllocator::CreateWithFile( |
| 121 active_file, kAllocSize, kAllocId, |
| 122 ChromeMetricsServiceClient::kBrowserMetricsName)) { |
| 97 result = MAPPED_FILE_SUCCESS; | 123 result = MAPPED_FILE_SUCCESS; |
| 98 } else { | 124 } else { |
| 99 result = MAPPED_FILE_FAILED; | 125 result = MAPPED_FILE_FAILED; |
| 100 } | 126 } |
| 101 } | 127 } |
| 128 // Schedule the creation of a "spare" file for use on the next run. |
| 129 base::PostDelayedTaskWithTraits( |
| 130 FROM_HERE, |
| 131 {base::MayBlock(), base::TaskPriority::LOWEST, |
| 132 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}, |
| 133 base::BindOnce(base::IgnoreResult( |
| 134 &base::GlobalHistogramAllocator::CreateSpareFile), |
| 135 base::Passed(&spare_file), kAllocSize), |
| 136 base::TimeDelta::FromSeconds(kSpareFileCreateDelaySeconds)); |
| 102 } else if (storage == "LocalMemory") { | 137 } else if (storage == "LocalMemory") { |
| 103 // Use local memory for storage even though it will not persist across | 138 // Use local memory for storage even though it will not persist across |
| 104 // an unclean shutdown. | 139 // an unclean shutdown. |
| 105 base::GlobalHistogramAllocator::CreateWithLocalMemory( | 140 base::GlobalHistogramAllocator::CreateWithLocalMemory( |
| 106 kAllocSize, kAllocId, ChromeMetricsServiceClient::kBrowserMetricsName); | 141 kAllocSize, kAllocId, ChromeMetricsServiceClient::kBrowserMetricsName); |
| 107 result = LOCAL_MEMORY_SUCCESS; | 142 result = LOCAL_MEMORY_SUCCESS; |
| 108 } else { | 143 } else { |
| 109 // Persistent metric storage is disabled. | 144 // Persistent metric storage is disabled. |
| 110 return; | 145 return; |
| 111 } | 146 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 bool has_seed, | 200 bool has_seed, |
| 166 base::FeatureList* feature_list) { | 201 base::FeatureList* feature_list) { |
| 167 CreateFallbackSamplingTrialIfNeeded(has_seed, feature_list); | 202 CreateFallbackSamplingTrialIfNeeded(has_seed, feature_list); |
| 168 } | 203 } |
| 169 | 204 |
| 170 void ChromeBrowserFieldTrials::InstantiateDynamicTrials() { | 205 void ChromeBrowserFieldTrials::InstantiateDynamicTrials() { |
| 171 // Persistent histograms must be enabled as soon as possible. | 206 // Persistent histograms must be enabled as soon as possible. |
| 172 InstantiatePersistentHistograms(); | 207 InstantiatePersistentHistograms(); |
| 173 tracing::SetupBackgroundTracingFieldTrial(); | 208 tracing::SetupBackgroundTracingFieldTrial(); |
| 174 } | 209 } |
| OLD | NEW |