Index: components/task_scheduler_util/initialization/browser_util.cc |
diff --git a/components/task_scheduler_util/initialization/browser_util.cc b/components/task_scheduler_util/initialization/browser_util.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5ce7ddcd6fa60a714df2817bace0b58c517c6c23 |
--- /dev/null |
+++ b/components/task_scheduler_util/initialization/browser_util.cc |
@@ -0,0 +1,135 @@ |
+// 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/initialization/browser_util.h" |
+ |
+#include <map> |
+#include <string> |
+#include <vector> |
fdoray
2016/12/06 17:22:23
<vector> already included in .h file
robliao
2016/12/07 00:03:54
Done.
|
+ |
+#include "base/task_scheduler/initialization_util.h" |
+#include "base/task_scheduler/switches.h" |
+#include "base/task_scheduler/task_traits.h" |
+#include "base/threading/sequenced_worker_pool.h" |
+#include "build/build_config.h" |
+ |
+namespace task_scheduler_util { |
+namespace initialization { |
+ |
+namespace { |
+ |
+using StandbyThreadPolicy = |
+ base::SchedulerWorkerPoolParams::StandbyThreadPolicy; |
+using ThreadPriority = base::ThreadPriority; |
+ |
+struct SchedulerWorkerPoolPredefinedParams { |
+ const char* name; |
+ ThreadPriority priority_hint; |
+}; |
+ |
+} // namespace |
+ |
+std::vector<base::SchedulerWorkerPoolParams> |
+BrowserWorkerPoolConfigurationToSchedulerWorkerPoolParams( |
+ const BrowserWorkerPoolsConfiguration& config) { |
+ constexpr SchedulerWorkerPoolPredefinedParams kAllPredefinedParams[] = { |
fdoray
2016/12/06 17:22:23
You could add a SingleWorkerPoolConfiguration* mem
gab
2016/12/06 19:16:07
static per https://groups.google.com/a/chromium.or
robliao
2016/12/07 00:03:54
This can't be static with the array inclusion chan
robliao
2016/12/07 00:03:54
I can live with that. Done.
|
+ {"Background", ThreadPriority::BACKGROUND}, |
+ {"BackgroundFileIO", ThreadPriority::BACKGROUND}, |
+ {"Foreground", ThreadPriority::NORMAL}, |
+ {"ForegroundFileIO", ThreadPriority::NORMAL}, |
+ }; |
+ static_assert(arraysize(kAllPredefinedParams) == WORKER_POOL_COUNT, |
+ "Mismatched Worker Pool Types and Predefined Parameters"); |
+ constexpr size_t worker_pool_total_size = |
+ sizeof(BrowserWorkerPoolsConfiguration); |
+ constexpr size_t single_worker_pool_size = |
+ sizeof(SingleWorkerPoolConfiguration); |
fdoray
2016/12/06 17:22:23
Compute this the same way as in BrowserWorkerPools
robliao
2016/12/07 00:03:54
Done
|
+ static_assert(arraysize(kAllPredefinedParams) == |
+ worker_pool_total_size / single_worker_pool_size, |
+ "Mismatched Predefined Parameters and Browser Worker Pools"); |
+ const SingleWorkerPoolConfiguration* const all_pools[]{ |
+ &config.background, &config.background_file_io, &config.foreground, |
+ &config.foreground_file_io, |
+ }; |
+ static_assert(arraysize(kAllPredefinedParams) == arraysize(all_pools), |
fdoray
2016/12/06 17:22:23
Since most of the code in this file can be reused
robliao
2016/12/07 00:03:54
We can't do the rename because initialization/util
|
+ "Mismatched Prefined Parameters and All Pools Array"); |
+ std::vector<base::SchedulerWorkerPoolParams> params_vector; |
+ for (size_t i = 0; i < arraysize(kAllPredefinedParams); ++i) { |
+ const auto& predefined_param = kAllPredefinedParams[i]; |
+ const auto& pool_config = all_pools[i]; |
gab
2016/12/06 19:16:07
s/&/*/
robliao
2016/12/07 00:03:54
Done.
|
+ params_vector.emplace_back( |
+ predefined_param.name, predefined_param.priority_hint, |
+ pool_config->standby_thread_policy, pool_config->threads, |
+ pool_config->detach_period); |
+ } |
+ DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size()); |
+ return params_vector; |
+} |
+ |
+// Returns the worker pool index for |traits| defaulting to |
+// FOREGROUND or FOREGROUND_FILE_IO on unknown |
fdoray
2016/12/06 17:22:23
wrap
gab
2016/12/06 19:16:07
"unknown priorities"? "non-background" priorities"
robliao
2016/12/07 00:03:54
Went with "any other" priorities.
robliao
2016/12/07 00:03:54
Done.
|
+// priorities. |
+size_t BrowserWorkerPoolIndexForTraits(const base::TaskTraits& traits) { |
+ const bool is_background = |
+ traits.priority() == base::TaskPriority::BACKGROUND; |
+ if (traits.with_file_io()) { |
fdoray
2016/12/06 17:22:23
no braces
gab
2016/12/06 19:16:07
nit: no {}
robliao
2016/12/07 00:03:54
Done.
robliao
2016/12/07 00:03:54
Done.
|
+ return is_background ? BACKGROUND_FILE_IO : FOREGROUND_FILE_IO; |
+ } |
+ return is_background ? BACKGROUND : FOREGROUND; |
+} |
+ |
+std::vector<base::SchedulerWorkerPoolParams> |
+GetDefaultBrowserSchedulerWorkerPoolParams() { |
+ constexpr size_t kNumWorkerPoolsDefined = |
+ sizeof(BrowserWorkerPoolsConfiguration) / |
+ sizeof(SingleWorkerPoolConfiguration); |
+ static_assert(kNumWorkerPoolsDefined == 4, |
+ "Expected 4 worker pools in BrowserWorkerPoolsConfiguration"); |
+ BrowserWorkerPoolsConfiguration config; |
+#if defined(OS_ANDROID) || defined(OS_IOS) |
+ config.background.standby_thread_policy = StandbyThreadPolicy::ONE; |
+ config.background.threads = |
+ base::RecommendedMaxNumberOfThreadsInPool(2, 8, 0.1, 0); |
+ config.background.detach_period = base::TimeDelta::FromSeconds(30); |
+ |
+ config.background_file_io.standby_thread_policy = StandbyThreadPolicy::ONE; |
+ config.background_file_io.threads = |
+ base::RecommendedMaxNumberOfThreadsInPool(2, 8, 0.1, 0); |
+ config.background_file_io.detach_period = base::TimeDelta::FromSeconds(30); |
+ |
+ config.foreground.standby_thread_policy = StandbyThreadPolicy::ONE; |
+ config.foreground.threads = |
+ base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.3, 0); |
+ config.foreground.detach_period = base::TimeDelta::FromSeconds(30); |
+ |
+ config.foreground_file_io.standby_thread_policy = StandbyThreadPolicy::ONE; |
+ config.foreground_file_io.threads = |
+ base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.3, 0); |
+ config.foreground_file_io.detach_period = base::TimeDelta::FromSeconds(30); |
+#else |
+ config.background.standby_thread_policy = StandbyThreadPolicy::ONE; |
+ config.background.threads = |
+ base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.1, 0); |
+ config.background.detach_period = base::TimeDelta::FromSeconds(30); |
+ |
+ config.background_file_io.standby_thread_policy = StandbyThreadPolicy::ONE; |
+ config.background_file_io.threads = |
+ base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.1, 0); |
+ config.background_file_io.detach_period = base::TimeDelta::FromSeconds(30); |
+ |
+ config.foreground.standby_thread_policy = StandbyThreadPolicy::ONE; |
+ config.foreground.threads = |
+ base::RecommendedMaxNumberOfThreadsInPool(8, 32, 0.3, 0); |
+ config.foreground.detach_period = base::TimeDelta::FromSeconds(30); |
+ |
+ config.foreground_file_io.standby_thread_policy = StandbyThreadPolicy::ONE; |
+ config.foreground_file_io.threads = |
+ base::RecommendedMaxNumberOfThreadsInPool(8, 32, 0.3, 0); |
+ config.foreground_file_io.detach_period = base::TimeDelta::FromSeconds(30); |
+#endif |
+ return BrowserWorkerPoolConfigurationToSchedulerWorkerPoolParams(config); |
+} |
+ |
+} // namespace initialization |
+} // namespace task_scheduler_util |