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