| 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 // TODO(fdoray): Remove this file once TaskScheduler initialization in the |
| 6 // browser process uses the components/task_scheduler_util/browser/ API on all |
| 7 // platforms. |
| 8 |
| 5 #include "components/task_scheduler_util/initialization/browser_util.h" | 9 #include "components/task_scheduler_util/initialization/browser_util.h" |
| 6 | 10 |
| 7 #include <map> | 11 #include "components/task_scheduler_util/browser/initialization.h" |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/task_scheduler/initialization_util.h" | |
| 11 #include "base/task_scheduler/switches.h" | |
| 12 #include "base/task_scheduler/task_traits.h" | |
| 13 #include "base/threading/sequenced_worker_pool.h" | |
| 14 | 12 |
| 15 namespace task_scheduler_util { | 13 namespace task_scheduler_util { |
| 16 namespace initialization { | 14 namespace initialization { |
| 17 | 15 |
| 18 namespace { | |
| 19 | |
| 20 using StandbyThreadPolicy = | |
| 21 base::SchedulerWorkerPoolParams::StandbyThreadPolicy; | |
| 22 using ThreadPriority = base::ThreadPriority; | |
| 23 | |
| 24 struct SchedulerWorkerPoolCustomizableConfiguration { | |
| 25 SchedulerWorkerPoolCustomizableConfiguration( | |
| 26 const char* name_in, | |
| 27 ThreadPriority priority_hint_in, | |
| 28 const SingleWorkerPoolConfiguration& single_worker_pool_config_in) | |
| 29 : name(name_in), | |
| 30 priority_hint(priority_hint_in), | |
| 31 single_worker_pool_config(single_worker_pool_config_in) {} | |
| 32 | |
| 33 const char* name; | |
| 34 ThreadPriority priority_hint; | |
| 35 const SingleWorkerPoolConfiguration& single_worker_pool_config; | |
| 36 }; | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 std::vector<base::SchedulerWorkerPoolParams> | |
| 41 BrowserWorkerPoolConfigurationToSchedulerWorkerPoolParams( | |
| 42 const BrowserWorkerPoolsConfiguration& config) { | |
| 43 const SchedulerWorkerPoolCustomizableConfiguration worker_pool_config[] = { | |
| 44 SchedulerWorkerPoolCustomizableConfiguration("Background", | |
| 45 ThreadPriority::BACKGROUND, | |
| 46 config.background), | |
| 47 SchedulerWorkerPoolCustomizableConfiguration("BackgroundFileIO", | |
| 48 ThreadPriority::BACKGROUND, | |
| 49 config.background_file_io), | |
| 50 SchedulerWorkerPoolCustomizableConfiguration("Foreground", | |
| 51 ThreadPriority::NORMAL, | |
| 52 config.foreground), | |
| 53 SchedulerWorkerPoolCustomizableConfiguration("ForegroundFileIO", | |
| 54 ThreadPriority::NORMAL, | |
| 55 config.foreground_file_io), | |
| 56 | |
| 57 }; | |
| 58 static_assert(arraysize(worker_pool_config) == WORKER_POOL_COUNT, | |
| 59 "Mismatched Worker Pool Types and Predefined Parameters"); | |
| 60 constexpr size_t kNumWorkerPoolsDefined = sizeof(config) / | |
| 61 sizeof(config.background); | |
| 62 static_assert(arraysize(worker_pool_config) == kNumWorkerPoolsDefined, | |
| 63 "Mismatch in predefined parameters and worker pools."); | |
| 64 std::vector<base::SchedulerWorkerPoolParams> params_vector; | |
| 65 for (const auto& config : worker_pool_config) { | |
| 66 params_vector.emplace_back( | |
| 67 config.name, config.priority_hint, | |
| 68 config.single_worker_pool_config.standby_thread_policy, | |
| 69 config.single_worker_pool_config.threads, | |
| 70 config.single_worker_pool_config.detach_period); | |
| 71 } | |
| 72 DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size()); | |
| 73 return params_vector; | |
| 74 } | |
| 75 | |
| 76 // Returns the worker pool index for |traits| defaulting to FOREGROUND or | 16 // Returns the worker pool index for |traits| defaulting to FOREGROUND or |
| 77 // FOREGROUND_FILE_IO on any priorities other than background. | 17 // FOREGROUND_FILE_IO on any priorities other than background. |
| 78 size_t BrowserWorkerPoolIndexForTraits(const base::TaskTraits& traits) { | 18 size_t BrowserWorkerPoolIndexForTraits(const base::TaskTraits& traits) { |
| 79 const bool is_background = | 19 return ::task_scheduler_util::BrowserWorkerPoolIndexForTraits(traits); |
| 80 traits.priority() == base::TaskPriority::BACKGROUND; | |
| 81 if (traits.with_file_io()) | |
| 82 return is_background ? BACKGROUND_FILE_IO : FOREGROUND_FILE_IO; | |
| 83 | |
| 84 return is_background ? BACKGROUND : FOREGROUND; | |
| 85 } | 20 } |
| 86 | 21 |
| 87 #if defined(OS_IOS) | |
| 88 std::vector<base::SchedulerWorkerPoolParams> | |
| 89 GetDefaultBrowserSchedulerWorkerPoolParams() { | |
| 90 constexpr size_t kNumWorkerPoolsDefined = | |
| 91 sizeof(BrowserWorkerPoolsConfiguration) / | |
| 92 sizeof(SingleWorkerPoolConfiguration); | |
| 93 static_assert(kNumWorkerPoolsDefined == 4, | |
| 94 "Expected 4 worker pools in BrowserWorkerPoolsConfiguration"); | |
| 95 BrowserWorkerPoolsConfiguration config; | |
| 96 constexpr size_t kSizeAssignedFields = | |
| 97 sizeof(config.background.threads) + | |
| 98 sizeof(config.background.detach_period) + | |
| 99 sizeof(config.background.standby_thread_policy); | |
| 100 static_assert(kSizeAssignedFields == sizeof(config.background), | |
| 101 "Not all fields were assigned"); | |
| 102 config.background.standby_thread_policy = StandbyThreadPolicy::ONE; | |
| 103 config.background.threads = | |
| 104 base::RecommendedMaxNumberOfThreadsInPool(2, 8, 0.1, 0); | |
| 105 config.background.detach_period = base::TimeDelta::FromSeconds(30); | |
| 106 | |
| 107 config.background_file_io.standby_thread_policy = StandbyThreadPolicy::ONE; | |
| 108 config.background_file_io.threads = | |
| 109 base::RecommendedMaxNumberOfThreadsInPool(2, 8, 0.1, 0); | |
| 110 config.background_file_io.detach_period = base::TimeDelta::FromSeconds(30); | |
| 111 | |
| 112 config.foreground.standby_thread_policy = StandbyThreadPolicy::ONE; | |
| 113 config.foreground.threads = | |
| 114 base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.3, 0); | |
| 115 config.foreground.detach_period = base::TimeDelta::FromSeconds(30); | |
| 116 | |
| 117 config.foreground_file_io.standby_thread_policy = StandbyThreadPolicy::ONE; | |
| 118 config.foreground_file_io.threads = | |
| 119 base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.3, 0); | |
| 120 config.foreground_file_io.detach_period = base::TimeDelta::FromSeconds(30); | |
| 121 return BrowserWorkerPoolConfigurationToSchedulerWorkerPoolParams(config); | |
| 122 } | |
| 123 #endif // defined(OS_IOS) | |
| 124 | |
| 125 } // namespace initialization | 22 } // namespace initialization |
| 126 } // namespace task_scheduler_util | 23 } // namespace task_scheduler_util |
| OLD | NEW |