OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/task_scheduler_util/common/variations_util.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_piece.h" |
| 10 #include "base/strings/string_split.h" |
| 11 #include "base/task_scheduler/initialization_util.h" |
| 12 #include "base/time/time.h" |
| 13 |
| 14 namespace task_scheduler_util { |
| 15 |
| 16 namespace { |
| 17 |
| 18 struct SchedulerCustomizableWorkerPoolParams { |
| 19 base::SchedulerWorkerPoolParams::StandbyThreadPolicy standby_thread_policy; |
| 20 int max_threads = 0; |
| 21 base::TimeDelta detach_period; |
| 22 }; |
| 23 |
| 24 // Converts |pool_descriptor| to a SchedulerWorkerPoolVariableParams. Returns a |
| 25 // default SchedulerWorkerPoolVariableParams on failure. |
| 26 // |
| 27 // |pool_descriptor| is a semi-colon separated value string with the following |
| 28 // items: |
| 29 // 0. Minimum Thread Count (int) |
| 30 // 1. Maximum Thread Count (int) |
| 31 // 2. Thread Count Multiplier (double) |
| 32 // 3. Thread Count Offset (int) |
| 33 // 4. Detach Time in Milliseconds (int) |
| 34 // 5. Standby Thread Policy (string) |
| 35 // Additional values may appear as necessary and will be ignored. |
| 36 SchedulerCustomizableWorkerPoolParams StringToVariableWorkerPoolParams( |
| 37 const base::StringPiece pool_descriptor) { |
| 38 using StandbyThreadPolicy = |
| 39 base::SchedulerWorkerPoolParams::StandbyThreadPolicy; |
| 40 const std::vector<base::StringPiece> tokens = SplitStringPiece( |
| 41 pool_descriptor, ";", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 42 // Normally, we wouldn't initialize the values below because we don't read |
| 43 // from them before we write to them. However, some compilers (like MSVC) |
| 44 // complain about uninitialized variables due to the as_string() call below. |
| 45 int min = 0; |
| 46 int max = 0; |
| 47 double cores_multiplier = 0.0; |
| 48 int offset = 0; |
| 49 int detach_milliseconds = 0; |
| 50 // Checking for a size greater than the expected amount allows us to be |
| 51 // forward compatible if we add more variation values. |
| 52 if (tokens.size() >= 5 && base::StringToInt(tokens[0], &min) && |
| 53 base::StringToInt(tokens[1], &max) && |
| 54 base::StringToDouble(tokens[2].as_string(), &cores_multiplier) && |
| 55 base::StringToInt(tokens[3], &offset) && |
| 56 base::StringToInt(tokens[4], &detach_milliseconds)) { |
| 57 SchedulerCustomizableWorkerPoolParams params; |
| 58 params.max_threads = base::RecommendedMaxNumberOfThreadsInPool( |
| 59 min, max, cores_multiplier, offset); |
| 60 params.detach_period = |
| 61 base::TimeDelta::FromMilliseconds(detach_milliseconds); |
| 62 params.standby_thread_policy = (tokens.size() >= 6 && tokens[5] == "lazy") |
| 63 ? StandbyThreadPolicy::LAZY |
| 64 : StandbyThreadPolicy::ONE; |
| 65 return params; |
| 66 } |
| 67 DLOG(ERROR) << "Invalid Worker Pool Descriptor: " << pool_descriptor; |
| 68 return SchedulerCustomizableWorkerPoolParams(); |
| 69 } |
| 70 |
| 71 } // namespace |
| 72 |
| 73 SchedulerImmutableWorkerPoolParams::SchedulerImmutableWorkerPoolParams( |
| 74 const char* name_in, |
| 75 base::ThreadPriority priority_hint_in) |
| 76 : name(name_in), priority_hint(priority_hint_in) {} |
| 77 |
| 78 std::vector<base::SchedulerWorkerPoolParams> GetWorkerPoolParams( |
| 79 const std::vector<SchedulerImmutableWorkerPoolParams> |
| 80 constant_worker_pool_params_vector, |
| 81 const std::map<std::string, std::string>& variation_params) { |
| 82 std::vector<base::SchedulerWorkerPoolParams> worker_pool_params_vector; |
| 83 for (const auto& constant_worker_pool_params : |
| 84 constant_worker_pool_params_vector) { |
| 85 const char* const worker_pool_name = constant_worker_pool_params.name; |
| 86 auto it = variation_params.find(worker_pool_name); |
| 87 if (it == variation_params.end()) { |
| 88 DLOG(ERROR) << "Missing Worker Pool Configuration: " << worker_pool_name; |
| 89 return std::vector<base::SchedulerWorkerPoolParams>(); |
| 90 } |
| 91 const auto variable_worker_pool_params = |
| 92 StringToVariableWorkerPoolParams(it->second); |
| 93 if (variable_worker_pool_params.max_threads <= 0 || |
| 94 variable_worker_pool_params.detach_period <= base::TimeDelta()) { |
| 95 DLOG(ERROR) << "Invalid Worker Pool Configuration: " << worker_pool_name |
| 96 << " [" << it->second << "]"; |
| 97 return std::vector<base::SchedulerWorkerPoolParams>(); |
| 98 } |
| 99 worker_pool_params_vector.emplace_back( |
| 100 worker_pool_name, constant_worker_pool_params.priority_hint, |
| 101 variable_worker_pool_params.standby_thread_policy, |
| 102 variable_worker_pool_params.max_threads, |
| 103 variable_worker_pool_params.detach_period); |
| 104 } |
| 105 return worker_pool_params_vector; |
| 106 } |
| 107 |
| 108 } // namespace task_scheduler_util |
OLD | NEW |