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/common/variations_util.h" | 5 #include "components/task_scheduler_util/common/variations_util.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ptr_util.h" | |
9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
12 #include "base/task_scheduler/initialization_util.h" | 13 #include "base/task_scheduler/initialization_util.h" |
13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
14 #include "components/variations/variations_associated_data.h" | 15 #include "components/variations/variations_associated_data.h" |
15 | 16 |
16 namespace task_scheduler_util { | 17 namespace task_scheduler_util { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 struct SchedulerCustomizableWorkerPoolParams { | |
21 base::SchedulerWorkerPoolParams::StandbyThreadPolicy standby_thread_policy; | |
22 int max_threads = 0; | |
23 base::TimeDelta detach_period; | |
24 }; | |
25 | |
26 #if !defined(OS_IOS) | 21 #if !defined(OS_IOS) |
27 constexpr char kTaskSchedulerVariationParamsSwitch[] = | 22 constexpr char kTaskSchedulerVariationParamsSwitch[] = |
28 "task-scheduler-variation-params"; | 23 "task-scheduler-variation-params"; |
29 | 24 |
30 constexpr char kSeparator[] = "|"; | 25 constexpr char kSeparator[] = "|"; |
31 | 26 |
32 bool ContainsSeparator(const std::string& str) { | 27 bool ContainsSeparator(const std::string& str) { |
33 return str.find(kSeparator) != std::string::npos; | 28 return str.find(kSeparator) != std::string::npos; |
34 } | 29 } |
35 #endif // !defined(OS_IOS) | 30 #endif // !defined(OS_IOS) |
36 | 31 |
37 // Converts |pool_descriptor| to a SchedulerWorkerPoolVariableParams. Returns a | 32 } // namespace |
38 // default SchedulerWorkerPoolVariableParams on failure. | 33 |
39 // | 34 std::unique_ptr<base::SchedulerWorkerPoolParams> StringToWorkerPoolParams( |
40 // |pool_descriptor| is a semi-colon separated value string with the following | 35 const base::StringPiece pool_descriptor, |
41 // items: | 36 base::SchedulerBackwardCompatibility backward_compatibility) { |
42 // 0. Minimum Thread Count (int) | |
43 // 1. Maximum Thread Count (int) | |
44 // 2. Thread Count Multiplier (double) | |
45 // 3. Thread Count Offset (int) | |
46 // 4. Detach Time in Milliseconds (int) | |
47 // 5. Standby Thread Policy (string) | |
48 // Additional values may appear as necessary and will be ignored. | |
49 SchedulerCustomizableWorkerPoolParams StringToVariableWorkerPoolParams( | |
50 const base::StringPiece pool_descriptor) { | |
51 using StandbyThreadPolicy = | 37 using StandbyThreadPolicy = |
52 base::SchedulerWorkerPoolParams::StandbyThreadPolicy; | 38 base::SchedulerWorkerPoolParams::StandbyThreadPolicy; |
53 const std::vector<base::StringPiece> tokens = SplitStringPiece( | 39 const std::vector<base::StringPiece> tokens = SplitStringPiece( |
54 pool_descriptor, ";", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 40 pool_descriptor, ";", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
55 // Normally, we wouldn't initialize the values below because we don't read | 41 // Normally, we wouldn't initialize the values below because we don't read |
56 // from them before we write to them. However, some compilers (like MSVC) | 42 // from them before we write to them. However, some compilers (like MSVC) |
57 // complain about uninitialized variables due to the as_string() call below. | 43 // complain about uninitialized variables due to the as_string() call below. |
58 int min = 0; | 44 int min = 0; |
59 int max = 0; | 45 int max = 0; |
60 double cores_multiplier = 0.0; | 46 double cores_multiplier = 0.0; |
61 int offset = 0; | 47 int offset = 0; |
62 int detach_milliseconds = 0; | 48 int detach_milliseconds = 0; |
63 // Checking for a size greater than the expected amount allows us to be | 49 // Checking for a size greater than the expected amount allows us to be |
64 // forward compatible if we add more variation values. | 50 // forward compatible if we add more variation values. |
65 if (tokens.size() >= 5 && base::StringToInt(tokens[0], &min) && | 51 if (tokens.size() < 5 || !base::StringToInt(tokens[0], &min) || |
66 base::StringToInt(tokens[1], &max) && | 52 !base::StringToInt(tokens[1], &max) || |
67 base::StringToDouble(tokens[2].as_string(), &cores_multiplier) && | 53 !base::StringToDouble(tokens[2].as_string(), &cores_multiplier) || |
68 base::StringToInt(tokens[3], &offset) && | 54 !base::StringToInt(tokens[3], &offset) || |
69 base::StringToInt(tokens[4], &detach_milliseconds)) { | 55 !base::StringToInt(tokens[4], &detach_milliseconds)) { |
70 SchedulerCustomizableWorkerPoolParams params; | 56 DLOG(ERROR) << "Invalid Worker Pool Descriptor Format: " << pool_descriptor; |
71 params.max_threads = base::RecommendedMaxNumberOfThreadsInPool( | 57 return nullptr; |
72 min, max, cores_multiplier, offset); | |
73 params.detach_period = | |
74 base::TimeDelta::FromMilliseconds(detach_milliseconds); | |
75 params.standby_thread_policy = (tokens.size() >= 6 && tokens[5] == "lazy") | |
76 ? StandbyThreadPolicy::LAZY | |
77 : StandbyThreadPolicy::ONE; | |
78 return params; | |
79 } | 58 } |
80 DLOG(ERROR) << "Invalid Worker Pool Descriptor: " << pool_descriptor; | |
81 return SchedulerCustomizableWorkerPoolParams(); | |
82 } | |
83 | 59 |
84 } // namespace | 60 auto params = base::MakeUnique<base::SchedulerWorkerPoolParams>( |
61 (tokens.size() >= 6 && tokens[5] == "lazy") ? StandbyThreadPolicy::LAZY | |
62 : StandbyThreadPolicy::ONE, | |
63 base::RecommendedMaxNumberOfThreadsInPool(min, max, cores_multiplier, | |
64 offset), | |
65 base::TimeDelta::FromMilliseconds(detach_milliseconds), | |
66 backward_compatibility); | |
85 | 67 |
86 SchedulerImmutableWorkerPoolParams::SchedulerImmutableWorkerPoolParams( | 68 if (params->max_threads() < 0 || |
robliao
2017/04/03 17:42:36
Break this into the individual failure cases:
if
fdoray
2017/04/03 19:01:44
Done.
| |
87 const char* name, | 69 params->suggested_reclaim_time() > base::TimeDelta()) { |
robliao
2017/04/03 17:42:35
Shouldn't this be
params->suggested_reclaim_time()
fdoray
2017/04/03 19:01:44
Done.
| |
88 base::ThreadPriority priority_hint, | 70 DLOG(ERROR) << "Invalid Values in Worker Pool Descriptor: " |
89 base::SchedulerBackwardCompatibility backward_compatibility) | 71 << pool_descriptor; |
90 : name_(name), | 72 return nullptr; |
91 priority_hint_(priority_hint), | 73 } |
92 backward_compatibility_(backward_compatibility) {} | |
93 | 74 |
94 std::vector<base::SchedulerWorkerPoolParams> GetWorkerPoolParams( | 75 return params; |
95 const std::vector<SchedulerImmutableWorkerPoolParams>& | |
96 constant_worker_pool_params_vector, | |
97 const std::map<std::string, std::string>& variation_params) { | |
98 std::vector<base::SchedulerWorkerPoolParams> worker_pool_params_vector; | |
99 for (const auto& constant_worker_pool_params : | |
100 constant_worker_pool_params_vector) { | |
101 const char* const worker_pool_name = constant_worker_pool_params.name(); | |
102 auto it = variation_params.find(worker_pool_name); | |
103 if (it == variation_params.end()) { | |
104 // Non-branded builds don't have access to external worker pool | |
105 // configurations. | |
106 return std::vector<base::SchedulerWorkerPoolParams>(); | |
107 } | |
108 const auto variable_worker_pool_params = | |
109 StringToVariableWorkerPoolParams(it->second); | |
110 if (variable_worker_pool_params.max_threads <= 0 || | |
111 variable_worker_pool_params.detach_period <= base::TimeDelta()) { | |
112 DLOG(ERROR) << "Invalid Worker Pool Configuration: " << worker_pool_name | |
113 << " [" << it->second << "]"; | |
114 return std::vector<base::SchedulerWorkerPoolParams>(); | |
115 } | |
116 worker_pool_params_vector.emplace_back( | |
117 worker_pool_name, constant_worker_pool_params.priority_hint(), | |
118 variable_worker_pool_params.standby_thread_policy, | |
119 variable_worker_pool_params.max_threads, | |
120 variable_worker_pool_params.detach_period, | |
121 constant_worker_pool_params.backward_compatibility()); | |
122 } | |
123 return worker_pool_params_vector; | |
124 } | 76 } |
125 | 77 |
126 #if !defined(OS_IOS) | 78 #if !defined(OS_IOS) |
127 void AddVariationParamsToCommandLine(base::StringPiece key_prefix, | 79 void AddVariationParamsToCommandLine(base::StringPiece key_prefix, |
128 base::CommandLine* command_line) { | 80 base::CommandLine* command_line) { |
129 DCHECK(command_line); | 81 DCHECK(command_line); |
130 | 82 |
131 std::map<std::string, std::string> variation_params; | 83 std::map<std::string, std::string> variation_params; |
132 if (!variations::GetVariationParams("BrowserScheduler", &variation_params)) | 84 if (!variations::GetVariationParams("BrowserScheduler", &variation_params)) |
133 return; | 85 return; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 return std::map<std::string, std::string>(); | 122 return std::map<std::string, std::string>(); |
171 } | 123 } |
172 base::StringPiece value = *it; | 124 base::StringPiece value = *it; |
173 variation_params[key.as_string()] = value.as_string(); | 125 variation_params[key.as_string()] = value.as_string(); |
174 } | 126 } |
175 return variation_params; | 127 return variation_params; |
176 } | 128 } |
177 #endif // !defined(OS_IOS) | 129 #endif // !defined(OS_IOS) |
178 | 130 |
179 } // namespace task_scheduler_util | 131 } // namespace task_scheduler_util |
OLD | NEW |