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