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

Side by Side Diff: components/task_scheduler_util/initialization_util.cc

Issue 2553953002: Split initialization_util into a Hermetic Library and a Variations Library (Closed)
Patch Set: Quick Adjustments Created 4 years 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 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"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/string_split.h"
16 #include "base/task_scheduler/initialization_util.h"
17 #include "base/task_scheduler/scheduler_worker_pool_params.h" 10 #include "base/task_scheduler/scheduler_worker_pool_params.h"
18 #include "base/task_scheduler/switches.h"
19 #include "base/task_scheduler/task_scheduler.h" 11 #include "base/task_scheduler/task_scheduler.h"
20 #include "base/task_scheduler/task_traits.h" 12 #include "components/task_scheduler_util/initialization/browser_util.h"
21 #include "base/threading/sequenced_worker_pool.h" 13 #include "components/task_scheduler_util/variations/browser_variations_util.h"
22 #include "base/time/time.h"
23 #include "build/build_config.h"
24 #include "components/variations/variations_associated_data.h"
25 14
26 namespace task_scheduler_util { 15 namespace task_scheduler_util {
27 16
28 namespace { 17 void InitializeDefaultBrowserTaskScheduler() {
18 std::vector<base::SchedulerWorkerPoolParams> params_vector =
19 variations::GetBrowserSchedulerWorkerPoolParamsFromVariations();
20 if (params_vector.empty()) {
21 params_vector =
22 initialization::GetDefaultBrowserSchedulerWorkerPoolParams();
23 }
29 24
30 using StandbyThreadPolicy =
31 base::SchedulerWorkerPoolParams::StandbyThreadPolicy;
32
33 enum WorkerPoolType : size_t {
34 BACKGROUND_WORKER_POOL = 0,
35 BACKGROUND_FILE_IO_WORKER_POOL,
36 FOREGROUND_WORKER_POOL,
37 FOREGROUND_FILE_IO_WORKER_POOL,
38 WORKER_POOL_COUNT // Always last.
39 };
40
41 struct WorkerPoolVariationValues {
42 StandbyThreadPolicy standby_thread_policy;
43 int threads = 0;
44 base::TimeDelta detach_period;
45 };
46
47 // Converts |pool_descriptor| to a WorkerPoolVariationValues. Returns a default
48 // WorkerPoolVariationValues on failure.
49 //
50 // |pool_descriptor| is a semi-colon separated value string with the following
51 // items:
52 // 0. Minimum Thread Count (int)
53 // 1. Maximum Thread Count (int)
54 // 2. Thread Count Multiplier (double)
55 // 3. Thread Count Offset (int)
56 // 4. Detach Time in Milliseconds (milliseconds)
57 // 5. Standby Thread Policy (string)
58 // Additional values may appear as necessary and will be ignored.
59 WorkerPoolVariationValues StringToWorkerPoolVariationValues(
60 const base::StringPiece pool_descriptor) {
61 const std::vector<std::string> tokens =
62 SplitString(pool_descriptor, ";",
63 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
64 int min;
65 int max;
66 double cores_multiplier;
67 int offset;
68 int detach_milliseconds;
69 // Checking for a size greater than the expected amount allows us to be
70 // forward compatible if we add more variation values.
71 if (tokens.size() >= 5 && base::StringToInt(tokens[0], &min) &&
72 base::StringToInt(tokens[1], &max) &&
73 base::StringToDouble(tokens[2], &cores_multiplier) &&
74 base::StringToInt(tokens[3], &offset) &&
75 base::StringToInt(tokens[4], &detach_milliseconds)) {
76 WorkerPoolVariationValues values;
77 values.threads = base::RecommendedMaxNumberOfThreadsInPool(
78 min, max, cores_multiplier, offset);
79 values.detach_period =
80 base::TimeDelta::FromMilliseconds(detach_milliseconds);
81 values.standby_thread_policy =
82 (tokens.size() >= 6 && tokens[5] == "lazy")
83 ? StandbyThreadPolicy::LAZY
84 : StandbyThreadPolicy::ONE;
85 return values;
86 }
87 DLOG(ERROR) << "Invalid Worker Pool Descriptor: " << pool_descriptor;
88 return WorkerPoolVariationValues();
89 }
90
91 // Returns the worker pool index for |traits| defaulting to
92 // FOREGROUND_WORKER_POOL or FOREGROUND_FILE_IO_WORKER_POOL on unknown
93 // priorities.
94 size_t WorkerPoolIndexForTraits(const base::TaskTraits& traits) {
95 const bool is_background =
96 traits.priority() == base::TaskPriority::BACKGROUND;
97 if (traits.with_file_io()) {
98 return is_background ? BACKGROUND_FILE_IO_WORKER_POOL
99 : FOREGROUND_FILE_IO_WORKER_POOL;
100 }
101 return is_background ? BACKGROUND_WORKER_POOL : FOREGROUND_WORKER_POOL;
102 }
103
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 }
119
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(
125 const std::map<std::string, std::string>& variation_params) {
126 using ThreadPriority = base::ThreadPriority;
127 struct SchedulerWorkerPoolPredefinedParams {
128 const char* name;
129 ThreadPriority priority_hint;
130 };
131 static const SchedulerWorkerPoolPredefinedParams kAllPredefinedParams[] = {
132 {"Background", ThreadPriority::BACKGROUND},
133 {"BackgroundFileIO", ThreadPriority::BACKGROUND},
134 {"Foreground", ThreadPriority::NORMAL},
135 {"ForegroundFileIO", ThreadPriority::NORMAL},
136 };
137 static_assert(arraysize(kAllPredefinedParams) == WORKER_POOL_COUNT,
138 "Mismatched Worker Pool Types and Predefined Parameters");
139 std::vector<base::SchedulerWorkerPoolParams> params_vector;
140 for (const auto& predefined_params : kAllPredefinedParams) {
141 const auto pair = variation_params.find(predefined_params.name);
142 if (pair == variation_params.end()) {
143 DLOG(ERROR) << "Missing Worker Pool Configuration: "
144 << predefined_params.name;
145 return std::vector<base::SchedulerWorkerPoolParams>();
146 }
147
148 const WorkerPoolVariationValues variation_values =
149 StringToWorkerPoolVariationValues(pair->second);
150
151 if (variation_values.threads <= 0 ||
152 variation_values.detach_period.is_zero()) {
153 DLOG(ERROR) << "Invalid Worker Pool Configuration: " <<
154 predefined_params.name << " [" << pair->second << "]";
155 return std::vector<base::SchedulerWorkerPoolParams>();
156 }
157
158 params_vector.emplace_back(predefined_params.name,
159 predefined_params.priority_hint,
160 variation_values.standby_thread_policy,
161 variation_values.threads,
162 variation_values.detach_period);
163 }
164 DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size());
165 return params_vector;
166 }
167
168 } // namespace
169
170 void InitializeDefaultBrowserTaskScheduler() {
171 static constexpr char kFieldTrialName[] = "BrowserScheduler";
172 std::map<std::string, std::string> variation_params;
173 if (!variations::GetVariationParams(kFieldTrialName, &variation_params))
174 variation_params = GetDefaultBrowserVariationParams();
175
176 auto params_vector =
177 VariationsParamsToBrowserSchedulerWorkerPoolParams(variation_params);
178 if (params_vector.empty()) {
179 variation_params = GetDefaultBrowserVariationParams();
180 params_vector =
181 VariationsParamsToBrowserSchedulerWorkerPoolParams(variation_params);
182 DCHECK(!params_vector.empty());
183 }
184 base::TaskScheduler::CreateAndSetDefaultTaskScheduler( 25 base::TaskScheduler::CreateAndSetDefaultTaskScheduler(
185 params_vector, base::Bind(WorkerPoolIndexForTraits)); 26 params_vector,
186 27 base::Bind(&initialization::BrowserWorkerPoolIndexForTraits));
187 // TODO(gab): Remove this when http://crbug.com/622400 concludes. 28 task_scheduler_util::variations::
188 const auto sequenced_worker_pool_param = 29 MaybePerformBrowserTaskSchedulerRedirection();
189 variation_params.find("RedirectSequencedWorkerPools");
190 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
191 switches::kDisableBrowserTaskScheduler) &&
192 sequenced_worker_pool_param != variation_params.end() &&
193 sequenced_worker_pool_param->second == "true") {
194 base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess();
195 } else {
196 base::SequencedWorkerPool::EnableForProcess();
197 }
198 } 30 }
199 31
200 } // namespace task_scheduler_util 32 } // namespace task_scheduler_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698