OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/task_scheduler_util/variations/browser_variations_util.h" | |
6 | |
7 #include <map> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/strings/string_piece.h" | |
14 #include "base/strings/string_split.h" | |
15 #include "base/task_scheduler/initialization_util.h" | |
16 #include "base/task_scheduler/scheduler_worker_pool_params.h" | |
17 #include "base/task_scheduler/switches.h" | |
18 #include "base/task_scheduler/task_traits.h" | |
19 #include "base/threading/sequenced_worker_pool.h" | |
20 #include "base/time/time.h" | |
21 #include "build/build_config.h" | |
22 #include "components/task_scheduler_util/initialization/browser_util.h" | |
23 #include "components/variations/variations_associated_data.h" | |
24 | |
25 namespace task_scheduler_util { | |
26 namespace variations { | |
27 | |
28 namespace { | |
29 | |
30 constexpr char kFieldTrialName[] = "BrowserScheduler"; | |
31 | |
32 // Converts |pool_descriptor| to a SingleWorkerPoolConfiguration. Returns a | |
33 // default SingleWorkerPoolConfiguration on failure. | |
34 // | |
35 // |pool_descriptor| is a semi-colon separated value string with the following | |
36 // items: | |
37 // 0. Minimum Thread Count (int) | |
38 // 1. Maximum Thread Count (int) | |
39 // 2. Thread Count Multiplier (double) | |
40 // 3. Thread Count Offset (int) | |
41 // 4. Detach Time in Milliseconds (milliseconds) | |
42 // 5. Standby Thread Policy (string) | |
43 // Additional values may appear as necessary and will be ignored. | |
44 initialization::SingleWorkerPoolConfiguration | |
45 StringToSingleWorkerPoolConfiguration(const base::StringPiece pool_descriptor) { | |
46 using StandbyThreadPolicy = | |
47 base::SchedulerWorkerPoolParams::StandbyThreadPolicy; | |
48 const std::vector<std::string> tokens = SplitString( | |
gab
2016/12/06 19:16:07
Use SplitStringPiece to get a StringPiece vector :
robliao
2016/12/07 00:03:54
Nice, but there's no StringPiece equivalent of bas
gab
2016/12/07 15:53:06
Lame, but you can still do this and use StringPiec
robliao
2016/12/07 18:39:19
Ah, missed as_string(). I haven't done a deep read
| |
49 pool_descriptor, ";", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
50 int min; | |
51 int max; | |
52 double cores_multiplier; | |
53 int offset; | |
54 int detach_milliseconds; | |
55 // Checking for a size greater than the expected amount allows us to be | |
56 // forward compatible if we add more variation values. | |
57 if (tokens.size() >= 5 && base::StringToInt(tokens[0], &min) && | |
58 base::StringToInt(tokens[1], &max) && | |
59 base::StringToDouble(tokens[2], &cores_multiplier) && | |
60 base::StringToInt(tokens[3], &offset) && | |
61 base::StringToInt(tokens[4], &detach_milliseconds)) { | |
62 initialization::SingleWorkerPoolConfiguration config; | |
63 config.threads = base::RecommendedMaxNumberOfThreadsInPool( | |
64 min, max, cores_multiplier, offset); | |
65 config.detach_period = | |
66 base::TimeDelta::FromMilliseconds(detach_milliseconds); | |
67 config.standby_thread_policy = (tokens.size() >= 6 && tokens[5] == "lazy") | |
68 ? StandbyThreadPolicy::LAZY | |
69 : StandbyThreadPolicy::ONE; | |
70 return config; | |
gab
2016/12/06 19:16:08
Is there a way to static_assert here that there is
robliao
2016/12/07 00:03:54
I figured one out last night as I drove home and w
| |
71 } | |
72 DLOG(ERROR) << "Invalid Worker Pool Descriptor: " << pool_descriptor; | |
73 return initialization::SingleWorkerPoolConfiguration(); | |
74 } | |
75 | |
76 // Converts a browser-based |variation_params| to | |
77 // std::vector<base::SchedulerWorkerPoolParams>. Returns an empty vector on | |
78 // failure. | |
79 std::vector<base::SchedulerWorkerPoolParams> | |
80 VariationsParamsToSchedulerWorkerPoolParamsVector( | |
81 const std::map<std::string, std::string>& variation_params) { | |
82 static const char* const kWorkerPoolNames[] = { | |
83 "Background", "BackgroundFileIO", "Foreground", "ForegroundFileIO"}; | |
84 static_assert( | |
85 arraysize(kWorkerPoolNames) == initialization::WORKER_POOL_COUNT, | |
86 "Mismatched Worker Pool Types and Worker Pool Names"); | |
87 initialization::BrowserWorkerPoolsConfiguration config; | |
88 initialization::SingleWorkerPoolConfiguration* const all_pools[]{ | |
89 &config.background, &config.background_file_io, &config.foreground, | |
90 &config.foreground_file_io, | |
91 }; | |
92 static_assert(arraysize(kWorkerPoolNames) == arraysize(all_pools), | |
93 "Mismatched Worker Pool Names and All Pools Array"); | |
94 for (size_t i = 0; i < arraysize(kWorkerPoolNames); ++i) { | |
95 const auto& worker_pool_name = kWorkerPoolNames[i]; | |
gab
2016/12/06 19:16:07
s/&/*/
(always prefer auto* when referring to a p
robliao
2016/12/07 00:03:54
Done.
| |
96 const auto pair = variation_params.find(worker_pool_name); | |
97 if (pair == variation_params.end()) { | |
98 DLOG(ERROR) << "Missing Worker Pool Configuration: " << worker_pool_name; | |
99 return std::vector<base::SchedulerWorkerPoolParams>(); | |
100 } | |
101 | |
102 auto& pool_config = *all_pools[i]; | |
gab
2016/12/06 19:16:07
auto* pool_config = all_pools[i]
is less surprisi
robliao
2016/12/07 00:03:55
Done.
| |
103 pool_config = StringToSingleWorkerPoolConfiguration(pair->second); | |
104 if (pool_config.threads <= 0 || pool_config.detach_period.is_zero()) { | |
gab
2016/12/06 19:16:08
Negative detach_period would also be invalid, righ
robliao
2016/12/07 00:03:55
Done.
| |
105 DLOG(ERROR) << "Invalid Worker Pool Configuration: " << worker_pool_name | |
106 << " [" << pair->second << "]"; | |
107 return std::vector<base::SchedulerWorkerPoolParams>(); | |
108 } | |
109 } | |
110 return BrowserWorkerPoolConfigurationToSchedulerWorkerPoolParams(config); | |
111 } | |
112 | |
113 } // namespace | |
114 | |
115 std::vector<base::SchedulerWorkerPoolParams> | |
116 GetBrowserSchedulerWorkerPoolParamsFromVariations() { | |
117 std::map<std::string, std::string> variation_params; | |
118 if (!::variations::GetVariationParams(kFieldTrialName, &variation_params)) | |
119 return std::vector<base::SchedulerWorkerPoolParams>(); | |
120 | |
121 return VariationsParamsToSchedulerWorkerPoolParamsVector(variation_params); | |
122 } | |
123 | |
124 void MaybePerformBrowserTaskSchedulerRedirection() { | |
125 std::map<std::string, std::string> variation_params; | |
126 ::variations::GetVariationParams(kFieldTrialName, &variation_params); | |
127 | |
128 // TODO(gab): Remove this when http://crbug.com/622400 concludes. | |
129 const auto sequenced_worker_pool_param = | |
130 variation_params.find("RedirectSequencedWorkerPools"); | |
131 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
132 switches::kDisableBrowserTaskScheduler) && | |
133 sequenced_worker_pool_param != variation_params.end() && | |
134 sequenced_worker_pool_param->second == "true") { | |
135 base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess(); | |
136 } else { | |
137 base::SequencedWorkerPool::EnableForProcess(); | |
gab
2016/12/06 19:16:07
Hadn't previously noticed but it's pretty weird to
robliao
2016/12/07 00:03:54
Yep, but that's also similar to task scheduler ini
| |
138 } | |
139 } | |
140 | |
141 } // variations | |
142 } // namespace task_scheduler_util | |
OLD | NEW |