| 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/initialization_util.h" | 5 #include "components/task_scheduler_util/initialization_util.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <vector> | 7 #include <vector> |
| 9 | 8 |
| 10 #include "base/bind.h" | 9 #include "base/bind.h" |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 14 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 15 #include "base/sys_info.h" | 14 #include "base/task_scheduler/initialization_util.h" |
| 16 #include "base/task_scheduler/scheduler_worker_pool_params.h" | 15 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
| 17 #include "base/task_scheduler/task_scheduler.h" | 16 #include "base/task_scheduler/task_scheduler.h" |
| 18 #include "base/task_scheduler/task_traits.h" | 17 #include "base/task_scheduler/task_traits.h" |
| 19 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 20 | 19 |
| 21 namespace task_scheduler_util { | 20 namespace task_scheduler_util { |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 enum WorkerPoolType : size_t { | 24 enum WorkerPoolType : size_t { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 44 // 2. Maximum Thread Count (int) | 43 // 2. Maximum Thread Count (int) |
| 45 // 3. Thread Count Multiplier (double) | 44 // 3. Thread Count Multiplier (double) |
| 46 // 4. Thread Count Offset (int) | 45 // 4. Thread Count Offset (int) |
| 47 // 5. Detach Time in Milliseconds (milliseconds) | 46 // 5. Detach Time in Milliseconds (milliseconds) |
| 48 // Additional values may appear as necessary and will be ignored. | 47 // Additional values may appear as necessary and will be ignored. |
| 49 WorkerPoolVariationValues StringToWorkerPoolVariationValues( | 48 WorkerPoolVariationValues StringToWorkerPoolVariationValues( |
| 50 const base::StringPiece pool_descriptor) { | 49 const base::StringPiece pool_descriptor) { |
| 51 const std::vector<std::string> tokens = | 50 const std::vector<std::string> tokens = |
| 52 SplitString(pool_descriptor, ";", | 51 SplitString(pool_descriptor, ";", |
| 53 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 52 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 54 int minimum; | 53 int min; |
| 55 int maximum; | 54 int max; |
| 56 double multiplier; | 55 double cores_multiplier; |
| 57 int offset; | 56 int offset; |
| 58 int detach_milliseconds; | 57 int detach_milliseconds; |
| 59 // Checking for a size greater than the expected amount allows us to be | 58 // Checking for a size greater than the expected amount allows us to be |
| 60 // forward compatible if we add more variation values. | 59 // forward compatible if we add more variation values. |
| 61 if (tokens.size() >= 5 && | 60 if (tokens.size() >= 5 && base::StringToInt(tokens[0], &min) && |
| 62 base::StringToInt(tokens[0], &minimum) && | 61 base::StringToInt(tokens[1], &max) && |
| 63 base::StringToInt(tokens[1], &maximum) && | 62 base::StringToDouble(tokens[2], &cores_multiplier) && |
| 64 base::StringToDouble(tokens[2], &multiplier) && | |
| 65 base::StringToInt(tokens[3], &offset) && | 63 base::StringToInt(tokens[3], &offset) && |
| 66 base::StringToInt(tokens[4], &detach_milliseconds)) { | 64 base::StringToInt(tokens[4], &detach_milliseconds)) { |
| 67 const int num_of_cores = base::SysInfo::NumberOfProcessors(); | |
| 68 const int threads = std::ceil<int>(num_of_cores * multiplier) + offset; | |
| 69 WorkerPoolVariationValues values; | 65 WorkerPoolVariationValues values; |
| 70 values.threads = std::min(maximum, std::max(minimum, threads)); | 66 values.threads = base::RecommendedMaxNumberOfThreadsInPool( |
| 67 min, max, cores_multiplier, offset); |
| 71 values.detach_period = | 68 values.detach_period = |
| 72 base::TimeDelta::FromMilliseconds(detach_milliseconds); | 69 base::TimeDelta::FromMilliseconds(detach_milliseconds); |
| 73 return values; | 70 return values; |
| 74 } | 71 } |
| 75 DLOG(ERROR) << "Invalid Worker Pool Descriptor: " << pool_descriptor; | 72 DLOG(ERROR) << "Invalid Worker Pool Descriptor: " << pool_descriptor; |
| 76 return WorkerPoolVariationValues(); | 73 return WorkerPoolVariationValues(); |
| 77 } | 74 } |
| 78 | 75 |
| 79 // Returns the worker pool index for |traits| defaulting to | 76 // Returns the worker pool index for |traits| defaulting to |
| 80 // FOREGROUND_WORKER_POOL or FOREGROUND_FILE_IO_WORKER_POOL on unknown | 77 // FOREGROUND_WORKER_POOL or FOREGROUND_FILE_IO_WORKER_POOL on unknown |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 134 |
| 138 DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size()); | 135 DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size()); |
| 139 | 136 |
| 140 base::TaskScheduler::CreateAndSetDefaultTaskScheduler( | 137 base::TaskScheduler::CreateAndSetDefaultTaskScheduler( |
| 141 params_vector, base::Bind(WorkerPoolIndexForTraits)); | 138 params_vector, base::Bind(WorkerPoolIndexForTraits)); |
| 142 | 139 |
| 143 return true; | 140 return true; |
| 144 } | 141 } |
| 145 | 142 |
| 146 } // namespace task_scheduler_util | 143 } // namespace task_scheduler_util |
| OLD | NEW |