| 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/browser/initialization.h" | 5 #include "components/task_scheduler_util/browser/initialization.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/logging.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/task_scheduler/scheduler_worker_params.h" | |
| 13 #include "base/task_scheduler/switches.h" | 12 #include "base/task_scheduler/switches.h" |
| 14 #include "base/task_scheduler/task_traits.h" | |
| 15 #include "base/threading/platform_thread.h" | |
| 16 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/threading/sequenced_worker_pool.h" |
| 17 #include "components/task_scheduler_util/common/variations_util.h" | 14 #include "components/task_scheduler_util/common/variations_util.h" |
| 18 #include "components/variations/variations_associated_data.h" | 15 #include "components/variations/variations_associated_data.h" |
| 19 | 16 |
| 20 namespace task_scheduler_util { | 17 namespace task_scheduler_util { |
| 21 | 18 |
| 22 namespace { | 19 namespace { |
| 23 | 20 |
| 24 constexpr char kFieldTrialName[] = "BrowserScheduler"; | 21 constexpr char kFieldTrialName[] = "BrowserScheduler"; |
| 25 | 22 |
| 26 enum WorkerPoolType : size_t { | |
| 27 BACKGROUND = 0, | |
| 28 BACKGROUND_BLOCKING, | |
| 29 FOREGROUND, | |
| 30 FOREGROUND_BLOCKING, | |
| 31 WORKER_POOL_COUNT // Always last. | |
| 32 }; | |
| 33 | |
| 34 } // namespace | 23 } // namespace |
| 35 | 24 |
| 36 std::vector<base::SchedulerWorkerPoolParams> | 25 std::unique_ptr<base::TaskSchedulerInitParams> |
| 37 GetBrowserWorkerPoolParamsFromVariations() { | 26 GetBrowserTaskSchedulerInitParamsFromVariations() { |
| 38 using ThreadPriority = base::ThreadPriority; | |
| 39 | |
| 40 std::map<std::string, std::string> variation_params; | 27 std::map<std::string, std::string> variation_params; |
| 41 if (!::variations::GetVariationParams(kFieldTrialName, &variation_params)) | 28 if (!::variations::GetVariationParams(kFieldTrialName, &variation_params)) |
| 42 return std::vector<base::SchedulerWorkerPoolParams>(); | 29 return nullptr; |
| 43 | 30 |
| 44 std::vector<SchedulerImmutableWorkerPoolParams> immutable_worker_pool_params; | 31 auto init_params = base::MakeUnique<base::TaskSchedulerInitParams>( |
| 45 DCHECK_EQ(BACKGROUND, immutable_worker_pool_params.size()); | 32 StringToVariableWorkerPoolParams(variation_params["Background"]), |
| 46 immutable_worker_pool_params.emplace_back("Background", | 33 StringToVariableWorkerPoolParams(variation_params["BackgroundBlocking"]), |
| 47 ThreadPriority::BACKGROUND); | 34 StringToVariableWorkerPoolParams(variation_params["Foreground"]), |
| 48 DCHECK_EQ(BACKGROUND_BLOCKING, immutable_worker_pool_params.size()); | 35 StringToVariableWorkerPoolParams(variation_params["ForegroundBlocking"])); |
| 49 immutable_worker_pool_params.emplace_back("BackgroundBlocking", | |
| 50 ThreadPriority::BACKGROUND); | |
| 51 DCHECK_EQ(FOREGROUND, immutable_worker_pool_params.size()); | |
| 52 immutable_worker_pool_params.emplace_back("Foreground", | |
| 53 ThreadPriority::NORMAL); | |
| 54 // Tasks posted to SequencedWorkerPool or BrowserThreadImpl may be redirected | |
| 55 // to this pool. Since COM STA is initialized in these environments, it must | |
| 56 // also be initialized in this pool. | |
| 57 DCHECK_EQ(FOREGROUND_BLOCKING, immutable_worker_pool_params.size()); | |
| 58 immutable_worker_pool_params.emplace_back( | |
| 59 "ForegroundBlocking", ThreadPriority::NORMAL, | |
| 60 base::SchedulerBackwardCompatibility::INIT_COM_STA); | |
| 61 | 36 |
| 62 return GetWorkerPoolParams(immutable_worker_pool_params, variation_params); | 37 if (init_params->IsValid()) |
| 63 } | 38 return init_params; |
| 64 | 39 |
| 65 size_t BrowserWorkerPoolIndexForTraits(const base::TaskTraits& traits) { | 40 return nullptr; |
| 66 const bool is_background = | |
| 67 traits.priority() == base::TaskPriority::BACKGROUND; | |
| 68 if (traits.may_block() || traits.with_base_sync_primitives()) | |
| 69 return is_background ? BACKGROUND_BLOCKING : FOREGROUND_BLOCKING; | |
| 70 return is_background ? BACKGROUND : FOREGROUND; | |
| 71 } | 41 } |
| 72 | 42 |
| 73 void MaybePerformBrowserTaskSchedulerRedirection() { | 43 void MaybePerformBrowserTaskSchedulerRedirection() { |
| 74 // TODO(gab): Remove this when http://crbug.com/622400 concludes. | 44 // TODO(gab): Remove this when http://crbug.com/622400 concludes. |
| 75 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 45 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 76 switches::kDisableBrowserTaskScheduler) && | 46 switches::kDisableBrowserTaskScheduler) && |
| 77 variations::GetVariationParamValue( | 47 variations::GetVariationParamValue( |
| 78 kFieldTrialName, "RedirectSequencedWorkerPools") == "true") { | 48 kFieldTrialName, "RedirectSequencedWorkerPools") == "true") { |
| 79 const base::TaskPriority max_task_priority = | 49 const base::TaskPriority max_task_priority = |
| 80 variations::GetVariationParamValue( | 50 variations::GetVariationParamValue( |
| 81 kFieldTrialName, "CapSequencedWorkerPoolsAtUserVisible") == "true" | 51 kFieldTrialName, "CapSequencedWorkerPoolsAtUserVisible") == "true" |
| 82 ? base::TaskPriority::USER_VISIBLE | 52 ? base::TaskPriority::USER_VISIBLE |
| 83 : base::TaskPriority::HIGHEST; | 53 : base::TaskPriority::HIGHEST; |
| 84 base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess( | 54 base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess( |
| 85 max_task_priority); | 55 max_task_priority); |
| 86 } | 56 } |
| 87 } | 57 } |
| 88 | 58 |
| 89 } // namespace task_scheduler_util | 59 } // namespace task_scheduler_util |
| OLD | NEW |