Index: components/task_scheduler_util/common/variations_util.cc |
diff --git a/components/task_scheduler_util/common/variations_util.cc b/components/task_scheduler_util/common/variations_util.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bc072a208ce40112fa6b23f4fb761f5edaa56ecd |
--- /dev/null |
+++ b/components/task_scheduler_util/common/variations_util.cc |
@@ -0,0 +1,108 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/task_scheduler_util/common/variations_util.h" |
+ |
+#include "base/logging.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_piece.h" |
+#include "base/strings/string_split.h" |
+#include "base/task_scheduler/initialization_util.h" |
+#include "base/time/time.h" |
+ |
+namespace task_scheduler_util { |
+ |
+namespace { |
+ |
+struct SchedulerVariableWorkerPoolParams { |
gab
2016/12/12 17:07:21
s/Variable/Customizable/ ?
fdoray
2016/12/12 18:30:06
Done.
|
+ base::SchedulerWorkerPoolParams::StandbyThreadPolicy standby_thread_policy; |
+ int max_threads = 0; |
+ base::TimeDelta detach_period; |
+}; |
+ |
+// Converts |pool_descriptor| to a SchedulerWorkerPoolVariableParams. Returns a |
+// default SchedulerWorkerPoolVariableParams on failure. |
+// |
+// |pool_descriptor| is a semi-colon separated value string with the following |
+// items: |
+// 0. Minimum Thread Count (int) |
+// 1. Maximum Thread Count (int) |
+// 2. Thread Count Multiplier (double) |
+// 3. Thread Count Offset (int) |
+// 4. Detach Time in Milliseconds (int) |
+// 5. Standby Thread Policy (string) |
+// Additional values may appear as necessary and will be ignored. |
+SchedulerVariableWorkerPoolParams StringToVariableWorkerPoolParams( |
+ const base::StringPiece pool_descriptor) { |
+ using StandbyThreadPolicy = |
+ base::SchedulerWorkerPoolParams::StandbyThreadPolicy; |
+ const std::vector<base::StringPiece> tokens = SplitStringPiece( |
+ pool_descriptor, ";", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
+ // Normally, we wouldn't initialize the values below because we don't read |
+ // from them before we write to them. However, some compilers (like MSVC) |
+ // complain about uninitialized variables due to the as_string() call below. |
+ int min = 0; |
+ int max = 0; |
+ double cores_multiplier = 0.0; |
+ int offset = 0; |
+ int detach_milliseconds = 0; |
+ // Checking for a size greater than the expected amount allows us to be |
+ // forward compatible if we add more variation values. |
+ if (tokens.size() >= 5 && base::StringToInt(tokens[0], &min) && |
+ base::StringToInt(tokens[1], &max) && |
+ base::StringToDouble(tokens[2].as_string(), &cores_multiplier) && |
+ base::StringToInt(tokens[3], &offset) && |
+ base::StringToInt(tokens[4], &detach_milliseconds)) { |
+ SchedulerVariableWorkerPoolParams params; |
+ params.max_threads = base::RecommendedMaxNumberOfThreadsInPool( |
+ min, max, cores_multiplier, offset); |
+ params.detach_period = |
+ base::TimeDelta::FromMilliseconds(detach_milliseconds); |
+ params.standby_thread_policy = (tokens.size() >= 6 && tokens[5] == "lazy") |
+ ? StandbyThreadPolicy::LAZY |
+ : StandbyThreadPolicy::ONE; |
+ return params; |
+ } |
+ DLOG(ERROR) << "Invalid Worker Pool Descriptor: " << pool_descriptor; |
+ return SchedulerVariableWorkerPoolParams(); |
+} |
+ |
+} // namespace |
+ |
+SchedulerConstantWorkerPoolParams::SchedulerConstantWorkerPoolParams( |
+ const char* name, |
+ base::ThreadPriority priority_hint) |
gab
2016/12/12 17:07:21
name_in, priority_hint_in
(I prefer that for stru
fdoray
2016/12/12 18:30:05
Done.
|
+ : name(name), priority_hint(priority_hint) {} |
+ |
+std::vector<base::SchedulerWorkerPoolParams> GetWorkerPoolParams( |
+ const std::vector<SchedulerConstantWorkerPoolParams> |
+ constant_worker_pool_params_vector, |
+ const std::map<std::string, std::string>& variation_params) { |
+ std::vector<base::SchedulerWorkerPoolParams> worker_pool_params_vector; |
+ for (const auto& constant_worker_pool_params : |
+ constant_worker_pool_params_vector) { |
+ const auto* const worker_pool_name = constant_worker_pool_params.name; |
gab
2016/12/12 17:07:21
s/auto/char/
same length so auto merely hinders r
fdoray
2016/12/12 18:30:05
Done.
|
+ auto it = variation_params.find(worker_pool_name); |
+ if (it == variation_params.end()) { |
+ DLOG(ERROR) << "Missing Worker Pool Configuration: " << worker_pool_name; |
+ return std::vector<base::SchedulerWorkerPoolParams>(); |
+ } |
+ const auto variable_worker_pool_params = |
+ StringToVariableWorkerPoolParams(it->second); |
+ if (variable_worker_pool_params.max_threads <= 0 || |
+ variable_worker_pool_params.detach_period <= base::TimeDelta()) { |
+ DLOG(ERROR) << "Invalid Worker Pool Configuration: " << worker_pool_name |
+ << " [" << it->second << "]"; |
+ return std::vector<base::SchedulerWorkerPoolParams>(); |
+ } |
+ worker_pool_params_vector.emplace_back( |
+ worker_pool_name, constant_worker_pool_params.priority_hint, |
+ variable_worker_pool_params.standby_thread_policy, |
+ variable_worker_pool_params.max_threads, |
+ variable_worker_pool_params.detach_period); |
+ } |
+ return worker_pool_params_vector; |
+} |
+ |
+} // namespace task_scheduler_util |