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