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