| OLD | NEW |
| 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 "components/task_scheduler_util/initialization_util.h" | 5 #include "components/task_scheduler_util/initialization_util.h" |
| 6 | 6 |
| 7 #include <map> | |
| 8 #include <string> | |
| 9 #include <vector> | 7 #include <vector> |
| 10 | 8 |
| 11 #include "base/bind.h" | 9 #include "base/bind.h" |
| 12 #include "base/logging.h" | 10 #include "base/logging.h" |
| 13 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 15 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 16 #include "base/task_scheduler/initialization_util.h" | 14 #include "base/task_scheduler/initialization_util.h" |
| 17 #include "base/task_scheduler/scheduler_worker_pool_params.h" | 15 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
| 18 #include "base/task_scheduler/switches.h" | |
| 19 #include "base/task_scheduler/task_scheduler.h" | 16 #include "base/task_scheduler/task_scheduler.h" |
| 20 #include "base/task_scheduler/task_traits.h" | 17 #include "base/task_scheduler/task_traits.h" |
| 21 #include "base/threading/sequenced_worker_pool.h" | |
| 22 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 23 #include "build/build_config.h" | |
| 24 #include "components/variations/variations_associated_data.h" | |
| 25 | 19 |
| 26 namespace task_scheduler_util { | 20 namespace task_scheduler_util { |
| 27 | 21 |
| 28 namespace { | 22 namespace { |
| 29 | 23 |
| 30 using StandbyThreadPolicy = | 24 using StandbyThreadPolicy = |
| 31 base::SchedulerWorkerPoolParams::StandbyThreadPolicy; | 25 base::SchedulerWorkerPoolParams::StandbyThreadPolicy; |
| 32 | 26 |
| 33 enum WorkerPoolType : size_t { | 27 enum WorkerPoolType : size_t { |
| 34 BACKGROUND_WORKER_POOL = 0, | 28 BACKGROUND_WORKER_POOL = 0, |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 size_t WorkerPoolIndexForTraits(const base::TaskTraits& traits) { | 88 size_t WorkerPoolIndexForTraits(const base::TaskTraits& traits) { |
| 95 const bool is_background = | 89 const bool is_background = |
| 96 traits.priority() == base::TaskPriority::BACKGROUND; | 90 traits.priority() == base::TaskPriority::BACKGROUND; |
| 97 if (traits.with_file_io()) { | 91 if (traits.with_file_io()) { |
| 98 return is_background ? BACKGROUND_FILE_IO_WORKER_POOL | 92 return is_background ? BACKGROUND_FILE_IO_WORKER_POOL |
| 99 : FOREGROUND_FILE_IO_WORKER_POOL; | 93 : FOREGROUND_FILE_IO_WORKER_POOL; |
| 100 } | 94 } |
| 101 return is_background ? BACKGROUND_WORKER_POOL : FOREGROUND_WORKER_POOL; | 95 return is_background ? BACKGROUND_WORKER_POOL : FOREGROUND_WORKER_POOL; |
| 102 } | 96 } |
| 103 | 97 |
| 104 std::map<std::string, std::string> GetDefaultBrowserVariationParams() { | 98 } // namespace |
| 105 std::map<std::string, std::string> variation_params; | |
| 106 #if defined(OS_ANDROID) || defined(OS_IOS) | |
| 107 variation_params["Background"] = "2;8;0.1;0;30000"; | |
| 108 variation_params["BackgroundFileIO"] = "2;8;0.1;0;30000"; | |
| 109 variation_params["Foreground"] = "3;8;0.3;0;30000"; | |
| 110 variation_params["ForegroundFileIO"] = "3;8;0.3;0;30000"; | |
| 111 #else | |
| 112 variation_params["Background"] = "3;8;0.1;0;30000"; | |
| 113 variation_params["BackgroundFileIO"] = "3;8;0.1;0;30000"; | |
| 114 variation_params["Foreground"] = "8;32;0.3;0;30000"; | |
| 115 variation_params["ForegroundFileIO"] = "8;32;0.3;0;30000"; | |
| 116 #endif // defined(OS_ANDROID) || defined(OS_IOS) | |
| 117 return variation_params; | |
| 118 } | |
| 119 | 99 |
| 120 // Converts a browser-based |variation_params| to | 100 bool InitializeDefaultTaskScheduler( |
| 121 // std::vector<base::SchedulerWorkerPoolParams>. Returns an empty vector on | |
| 122 // failure. | |
| 123 std::vector<base::SchedulerWorkerPoolParams> | |
| 124 VariationsParamsToBrowserSchedulerWorkerPoolParams( | |
| 125 const std::map<std::string, std::string>& variation_params) { | 101 const std::map<std::string, std::string>& variation_params) { |
| 126 using ThreadPriority = base::ThreadPriority; | 102 using ThreadPriority = base::ThreadPriority; |
| 127 using IORestriction = base::SchedulerWorkerPoolParams::IORestriction; | 103 using IORestriction = base::SchedulerWorkerPoolParams::IORestriction; |
| 128 struct SchedulerWorkerPoolPredefinedParams { | 104 struct SchedulerWorkerPoolPredefinedParams { |
| 129 const char* name; | 105 const char* name; |
| 130 ThreadPriority priority_hint; | 106 ThreadPriority priority_hint; |
| 131 IORestriction io_restriction; | 107 IORestriction io_restriction; |
| 132 }; | 108 }; |
| 133 static const SchedulerWorkerPoolPredefinedParams kAllPredefinedParams[] = { | 109 static const SchedulerWorkerPoolPredefinedParams kAllPredefinedParams[] = { |
| 134 {"Background", ThreadPriority::BACKGROUND, IORestriction::DISALLOWED}, | 110 {"Background", ThreadPriority::BACKGROUND, IORestriction::DISALLOWED}, |
| 135 {"BackgroundFileIO", ThreadPriority::BACKGROUND, IORestriction::ALLOWED}, | 111 {"BackgroundFileIO", ThreadPriority::BACKGROUND, IORestriction::ALLOWED}, |
| 136 {"Foreground", ThreadPriority::NORMAL, IORestriction::DISALLOWED}, | 112 {"Foreground", ThreadPriority::NORMAL, IORestriction::DISALLOWED}, |
| 137 {"ForegroundFileIO", ThreadPriority::NORMAL, IORestriction::ALLOWED}, | 113 {"ForegroundFileIO", ThreadPriority::NORMAL, IORestriction::ALLOWED}, |
| 138 }; | 114 }; |
| 139 static_assert(arraysize(kAllPredefinedParams) == WORKER_POOL_COUNT, | 115 static_assert(arraysize(kAllPredefinedParams) == WORKER_POOL_COUNT, |
| 140 "Mismatched Worker Pool Types and Predefined Parameters"); | 116 "Mismatched Worker Pool Types and Predefined Parameters"); |
| 117 |
| 141 std::vector<base::SchedulerWorkerPoolParams> params_vector; | 118 std::vector<base::SchedulerWorkerPoolParams> params_vector; |
| 142 for (const auto& predefined_params : kAllPredefinedParams) { | 119 for (const auto& predefined_params : kAllPredefinedParams) { |
| 143 const auto pair = variation_params.find(predefined_params.name); | 120 const auto pair = variation_params.find(predefined_params.name); |
| 144 if (pair == variation_params.end()) { | 121 if (pair == variation_params.end()) { |
| 145 DLOG(ERROR) << "Missing Worker Pool Configuration: " | 122 DLOG(ERROR) << "Missing Worker Pool Configuration: " |
| 146 << predefined_params.name; | 123 << predefined_params.name; |
| 147 return std::vector<base::SchedulerWorkerPoolParams>(); | 124 return false; |
| 148 } | 125 } |
| 149 | 126 |
| 150 const WorkerPoolVariationValues variation_values = | 127 const WorkerPoolVariationValues variation_values = |
| 151 StringToWorkerPoolVariationValues(pair->second); | 128 StringToWorkerPoolVariationValues(pair->second); |
| 152 | 129 |
| 153 if (variation_values.threads <= 0 || | 130 if (variation_values.threads <= 0 || |
| 154 variation_values.detach_period.is_zero()) { | 131 variation_values.detach_period.is_zero()) { |
| 155 DLOG(ERROR) << "Invalid Worker Pool Configuration: " << | 132 DLOG(ERROR) << "Invalid Worker Pool Configuration: " << |
| 156 predefined_params.name << " [" << pair->second << "]"; | 133 predefined_params.name << " [" << pair->second << "]"; |
| 157 return std::vector<base::SchedulerWorkerPoolParams>(); | 134 return false; |
| 158 } | 135 } |
| 159 | 136 |
| 160 params_vector.emplace_back(predefined_params.name, | 137 params_vector.emplace_back(predefined_params.name, |
| 161 predefined_params.priority_hint, | 138 predefined_params.priority_hint, |
| 162 predefined_params.io_restriction, | 139 predefined_params.io_restriction, |
| 163 variation_values.standby_thread_policy, | 140 variation_values.standby_thread_policy, |
| 164 variation_values.threads, | 141 variation_values.threads, |
| 165 variation_values.detach_period); | 142 variation_values.detach_period); |
| 166 } | 143 } |
| 144 |
| 167 DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size()); | 145 DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size()); |
| 168 return params_vector; | |
| 169 } | |
| 170 | 146 |
| 171 } // namespace | |
| 172 | |
| 173 void InitializeDefaultBrowserTaskScheduler() { | |
| 174 static constexpr char kFieldTrialName[] = "BrowserScheduler"; | |
| 175 std::map<std::string, std::string> variation_params; | |
| 176 if (!variations::GetVariationParams(kFieldTrialName, &variation_params)) | |
| 177 variation_params = GetDefaultBrowserVariationParams(); | |
| 178 | |
| 179 auto params_vector = | |
| 180 VariationsParamsToBrowserSchedulerWorkerPoolParams(variation_params); | |
| 181 if (params_vector.empty()) { | |
| 182 variation_params = GetDefaultBrowserVariationParams(); | |
| 183 params_vector = | |
| 184 VariationsParamsToBrowserSchedulerWorkerPoolParams(variation_params); | |
| 185 DCHECK(!params_vector.empty()); | |
| 186 } | |
| 187 base::TaskScheduler::CreateAndSetDefaultTaskScheduler( | 147 base::TaskScheduler::CreateAndSetDefaultTaskScheduler( |
| 188 params_vector, base::Bind(WorkerPoolIndexForTraits)); | 148 params_vector, base::Bind(WorkerPoolIndexForTraits)); |
| 189 | 149 |
| 190 // TODO(gab): Remove this when http://crbug.com/622400 concludes. | 150 return true; |
| 191 const auto sequenced_worker_pool_param = | |
| 192 variation_params.find("RedirectSequencedWorkerPools"); | |
| 193 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 194 switches::kDisableBrowserTaskScheduler) && | |
| 195 sequenced_worker_pool_param != variation_params.end() && | |
| 196 sequenced_worker_pool_param->second == "true") { | |
| 197 base::SequencedWorkerPool::RedirectToTaskSchedulerForProcess(); | |
| 198 } | |
| 199 } | 151 } |
| 200 | 152 |
| 201 } // namespace task_scheduler_util | 153 } // namespace task_scheduler_util |
| OLD | NEW |