Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: components/task_scheduler_util/initialization_util.cc

Issue 2501763002: Add Thread Standby Policy SchedulerWorkerPoolImpl (Closed)
Patch Set: CR Feedback Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_piece.h" 13 #include "base/strings/string_piece.h"
14 #include "base/strings/string_split.h" 14 #include "base/strings/string_split.h"
15 #include "base/sys_info.h" 15 #include "base/sys_info.h"
16 #include "base/task_scheduler/scheduler_worker_pool_params.h" 16 #include "base/task_scheduler/scheduler_worker_pool_params.h"
17 #include "base/task_scheduler/task_scheduler.h" 17 #include "base/task_scheduler/task_scheduler.h"
18 #include "base/task_scheduler/task_traits.h" 18 #include "base/task_scheduler/task_traits.h"
19 #include "base/time/time.h" 19 #include "base/time/time.h"
20 20
21 namespace task_scheduler_util { 21 namespace task_scheduler_util {
22 22
23 namespace { 23 namespace {
24 24
25 using StandbyThreadPolicy =
26 base::SchedulerWorkerPoolParams::StandbyThreadPolicy;
27
25 enum WorkerPoolType : size_t { 28 enum WorkerPoolType : size_t {
26 BACKGROUND_WORKER_POOL = 0, 29 BACKGROUND_WORKER_POOL = 0,
27 BACKGROUND_FILE_IO_WORKER_POOL, 30 BACKGROUND_FILE_IO_WORKER_POOL,
28 FOREGROUND_WORKER_POOL, 31 FOREGROUND_WORKER_POOL,
29 FOREGROUND_FILE_IO_WORKER_POOL, 32 FOREGROUND_FILE_IO_WORKER_POOL,
30 WORKER_POOL_COUNT // Always last. 33 WORKER_POOL_COUNT // Always last.
31 }; 34 };
32 35
33 struct WorkerPoolVariationValues { 36 struct WorkerPoolVariationValues {
37 StandbyThreadPolicy standby_thread_policy;
34 int threads = 0; 38 int threads = 0;
35 base::TimeDelta detach_period; 39 base::TimeDelta detach_period;
36 }; 40 };
37 41
38 // Converts |pool_descriptor| to a WorkerPoolVariationValues. Returns a default 42 // Converts |pool_descriptor| to a WorkerPoolVariationValues. Returns a default
39 // WorkerPoolVariationValues on failure. 43 // WorkerPoolVariationValues on failure.
40 // 44 //
41 // |pool_descriptor| is a semi-colon separated value string with the following 45 // |pool_descriptor| is a semi-colon separated value string with the following
42 // items: 46 // items:
43 // 1. Minimum Thread Count (int) 47 // 1. Minimum Thread Count (int)
44 // 2. Maximum Thread Count (int) 48 // 2. Maximum Thread Count (int)
45 // 3. Thread Count Multiplier (double) 49 // 3. Thread Count Multiplier (double)
46 // 4. Thread Count Offset (int) 50 // 4. Thread Count Offset (int)
47 // 5. Detach Time in Milliseconds (milliseconds) 51 // 5. Detach Time in Milliseconds (milliseconds)
gab 2016/11/15 20:33:01 Document 6th token here (also the above list shoul
robliao 2016/11/16 21:09:31 Done.
48 // Additional values may appear as necessary and will be ignored. 52 // Additional values may appear as necessary and will be ignored.
49 WorkerPoolVariationValues StringToWorkerPoolVariationValues( 53 WorkerPoolVariationValues StringToWorkerPoolVariationValues(
50 const base::StringPiece pool_descriptor) { 54 const base::StringPiece pool_descriptor) {
51 const std::vector<std::string> tokens = 55 const std::vector<std::string> tokens =
52 SplitString(pool_descriptor, ";", 56 SplitString(pool_descriptor, ";",
53 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 57 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
54 int minimum; 58 int minimum;
55 int maximum; 59 int maximum;
56 double multiplier; 60 double multiplier;
57 int offset; 61 int offset;
58 int detach_milliseconds; 62 int detach_milliseconds;
59 // Checking for a size greater than the expected amount allows us to be 63 // Checking for a size greater than the expected amount allows us to be
60 // forward compatible if we add more variation values. 64 // forward compatible if we add more variation values.
61 if (tokens.size() >= 5 && 65 if (tokens.size() >= 5 &&
62 base::StringToInt(tokens[0], &minimum) && 66 base::StringToInt(tokens[0], &minimum) &&
63 base::StringToInt(tokens[1], &maximum) && 67 base::StringToInt(tokens[1], &maximum) &&
64 base::StringToDouble(tokens[2], &multiplier) && 68 base::StringToDouble(tokens[2], &multiplier) &&
65 base::StringToInt(tokens[3], &offset) && 69 base::StringToInt(tokens[3], &offset) &&
66 base::StringToInt(tokens[4], &detach_milliseconds)) { 70 base::StringToInt(tokens[4], &detach_milliseconds)) {
67 const int num_of_cores = base::SysInfo::NumberOfProcessors(); 71 const int num_of_cores = base::SysInfo::NumberOfProcessors();
68 const int threads = std::ceil<int>(num_of_cores * multiplier) + offset; 72 const int threads = std::ceil<int>(num_of_cores * multiplier) + offset;
69 WorkerPoolVariationValues values; 73 WorkerPoolVariationValues values;
70 values.threads = std::min(maximum, std::max(minimum, threads)); 74 values.threads = std::min(maximum, std::max(minimum, threads));
71 values.detach_period = 75 values.detach_period =
72 base::TimeDelta::FromMilliseconds(detach_milliseconds); 76 base::TimeDelta::FromMilliseconds(detach_milliseconds);
77 values.standby_thread_policy =
78 (tokens.size() >= 6 && tokens[5] == "lazy")
79 ? StandbyThreadPolicy::LAZY
80 : StandbyThreadPolicy::ONE;
73 return values; 81 return values;
74 } 82 }
75 DLOG(ERROR) << "Invalid Worker Pool Descriptor: " << pool_descriptor; 83 DLOG(ERROR) << "Invalid Worker Pool Descriptor: " << pool_descriptor;
76 return WorkerPoolVariationValues(); 84 return WorkerPoolVariationValues();
77 } 85 }
78 86
79 // Returns the worker pool index for |traits| defaulting to 87 // Returns the worker pool index for |traits| defaulting to
80 // FOREGROUND_WORKER_POOL or FOREGROUND_FILE_IO_WORKER_POOL on unknown 88 // FOREGROUND_WORKER_POOL or FOREGROUND_FILE_IO_WORKER_POOL on unknown
81 // priorities. 89 // priorities.
82 size_t WorkerPoolIndexForTraits(const base::TaskTraits& traits) { 90 size_t WorkerPoolIndexForTraits(const base::TaskTraits& traits) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 if (variation_values.threads <= 0 || 132 if (variation_values.threads <= 0 ||
125 variation_values.detach_period.is_zero()) { 133 variation_values.detach_period.is_zero()) {
126 DLOG(ERROR) << "Invalid Worker Pool Configuration: " << 134 DLOG(ERROR) << "Invalid Worker Pool Configuration: " <<
127 predefined_params.name << " [" << pair->second << "]"; 135 predefined_params.name << " [" << pair->second << "]";
128 return false; 136 return false;
129 } 137 }
130 138
131 params_vector.emplace_back(predefined_params.name, 139 params_vector.emplace_back(predefined_params.name,
132 predefined_params.priority_hint, 140 predefined_params.priority_hint,
133 predefined_params.io_restriction, 141 predefined_params.io_restriction,
142 variation_values.standby_thread_policy,
134 variation_values.threads, 143 variation_values.threads,
135 variation_values.detach_period); 144 variation_values.detach_period);
136 } 145 }
137 146
138 DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size()); 147 DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size());
139 148
140 base::TaskScheduler::CreateAndSetDefaultTaskScheduler( 149 base::TaskScheduler::CreateAndSetDefaultTaskScheduler(
141 params_vector, base::Bind(WorkerPoolIndexForTraits)); 150 params_vector, base::Bind(WorkerPoolIndexForTraits));
142 151
143 return true; 152 return true;
144 } 153 }
145 154
146 } // namespace task_scheduler_util 155 } // namespace task_scheduler_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698