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/logging.h" | 8 #include "base/logging.h" |
8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
10 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
12 #include "base/strings/string_util.h" | |
11 #include "base/task_scheduler/initialization_util.h" | 13 #include "base/task_scheduler/initialization_util.h" |
12 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "components/variations/variations_associated_data.h" | |
13 | 16 |
14 namespace task_scheduler_util { | 17 namespace task_scheduler_util { |
15 | 18 |
16 namespace { | 19 namespace { |
17 | 20 |
18 struct SchedulerCustomizableWorkerPoolParams { | 21 struct SchedulerCustomizableWorkerPoolParams { |
19 base::SchedulerWorkerPoolParams::StandbyThreadPolicy standby_thread_policy; | 22 base::SchedulerWorkerPoolParams::StandbyThreadPolicy standby_thread_policy; |
20 int max_threads = 0; | 23 int max_threads = 0; |
21 base::TimeDelta detach_period; | 24 base::TimeDelta detach_period; |
22 }; | 25 }; |
23 | 26 |
27 constexpr char kTaskSchedulerVariationParamsSwitch[] = | |
28 "task-scheduler-variation-params"; | |
29 | |
30 constexpr char kSeparator[] = "|"; | |
31 | |
32 bool ContainsSeparator(const std::string& str) { | |
33 return str.find(kSeparator) != std::string::npos; | |
34 } | |
35 | |
24 // Converts |pool_descriptor| to a SchedulerWorkerPoolVariableParams. Returns a | 36 // Converts |pool_descriptor| to a SchedulerWorkerPoolVariableParams. Returns a |
25 // default SchedulerWorkerPoolVariableParams on failure. | 37 // default SchedulerWorkerPoolVariableParams on failure. |
26 // | 38 // |
27 // |pool_descriptor| is a semi-colon separated value string with the following | 39 // |pool_descriptor| is a semi-colon separated value string with the following |
28 // items: | 40 // items: |
29 // 0. Minimum Thread Count (int) | 41 // 0. Minimum Thread Count (int) |
30 // 1. Maximum Thread Count (int) | 42 // 1. Maximum Thread Count (int) |
31 // 2. Thread Count Multiplier (double) | 43 // 2. Thread Count Multiplier (double) |
32 // 3. Thread Count Offset (int) | 44 // 3. Thread Count Offset (int) |
33 // 4. Detach Time in Milliseconds (int) | 45 // 4. Detach Time in Milliseconds (int) |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 } | 110 } |
99 worker_pool_params_vector.emplace_back( | 111 worker_pool_params_vector.emplace_back( |
100 worker_pool_name, constant_worker_pool_params.priority_hint(), | 112 worker_pool_name, constant_worker_pool_params.priority_hint(), |
101 variable_worker_pool_params.standby_thread_policy, | 113 variable_worker_pool_params.standby_thread_policy, |
102 variable_worker_pool_params.max_threads, | 114 variable_worker_pool_params.max_threads, |
103 variable_worker_pool_params.detach_period); | 115 variable_worker_pool_params.detach_period); |
104 } | 116 } |
105 return worker_pool_params_vector; | 117 return worker_pool_params_vector; |
106 } | 118 } |
107 | 119 |
120 void AddVariationParamsToCommandLine(base::StringPiece key_prefix, | |
121 base::CommandLine* command_line) { | |
122 DCHECK(command_line); | |
123 | |
124 std::map<std::string, std::string> variation_params; | |
125 if (!variations::GetVariationParams("BrowserScheduler", &variation_params)) | |
126 return; | |
127 | |
128 std::vector<std::string> parts; | |
129 for (const auto& key_value : variation_params) { | |
130 if (base::StartsWith(key_value.first, key_prefix, | |
131 base::CompareCase::SENSITIVE)) { | |
132 if (ContainsSeparator(key_value.first) || | |
133 ContainsSeparator(key_value.second)) { | |
134 DLOG(ERROR) | |
135 << "Unexpected Character in Task Scheduler Variation Params: " | |
136 << key_value.first << " [" << key_value.second << "]"; | |
137 return; | |
138 } | |
139 parts.push_back(key_value.first); | |
140 parts.push_back(key_value.second); | |
141 } | |
142 } | |
143 | |
144 if (!parts.empty()) { | |
145 command_line->AppendSwitchASCII(kTaskSchedulerVariationParamsSwitch, | |
146 base::JoinString(parts, kSeparator)); | |
147 } | |
148 } | |
149 | |
150 std::map<std::string, std::string> GetVariationParamsFromCommandLine( | |
151 const base::CommandLine& command_line) { | |
152 const auto serialized_variation_params = | |
153 command_line.GetSwitchValueASCII(kTaskSchedulerVariationParamsSwitch); | |
154 const auto parts = | |
155 base::SplitStringPiece(serialized_variation_params, kSeparator, | |
156 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
157 std::map<std::string, std::string> variation_params; | |
158 for (auto it = parts.begin(); it != parts.end(); ++it) { | |
159 base::StringPiece key = *it; | |
160 ++it; | |
161 DCHECK(it != parts.end()); | |
gab
2017/01/06 17:41:09
Rob had a good point that we still need to handle
fdoray
2017/01/06 18:10:39
Done.
| |
162 base::StringPiece value = *it; | |
163 variation_params[key.as_string()] = value.as_string(); | |
164 } | |
165 return variation_params; | |
166 } | |
167 | |
108 } // namespace task_scheduler_util | 168 } // namespace task_scheduler_util |
OLD | NEW |