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

Side by Side Diff: components/task_scheduler_util/variations/browser_variations_util.cc

Issue 2565013002: Split browser-specific and generic code in components/task_scheduler_util/. (Closed)
Patch Set: self-review Created 4 years 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/task_scheduler_util/variations/browser_variations_util.h"
6
7 #include <map>
8 #include <string>
9 #include <vector>
10
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_piece.h"
14 #include "base/strings/string_split.h"
15 #include "base/task_scheduler/initialization_util.h"
16 #include "base/task_scheduler/scheduler_worker_pool_params.h"
17 #include "base/task_scheduler/switches.h"
18 #include "base/task_scheduler/task_traits.h"
19 #include "base/threading/sequenced_worker_pool.h"
20 #include "base/time/time.h"
21 #include "build/build_config.h"
22 #include "components/task_scheduler_util/initialization/browser_util.h"
23 #include "components/variations/variations_associated_data.h"
24
25 namespace task_scheduler_util {
26 namespace variations {
27
28 namespace {
29
30 constexpr char kFieldTrialName[] = "BrowserScheduler";
31
32 // Converts |pool_descriptor| to a SingleWorkerPoolConfiguration. Returns a
33 // default SingleWorkerPoolConfiguration on failure.
34 //
35 // |pool_descriptor| is a semi-colon separated value string with the following
36 // items:
37 // 0. Minimum Thread Count (int)
38 // 1. Maximum Thread Count (int)
39 // 2. Thread Count Multiplier (double)
40 // 3. Thread Count Offset (int)
41 // 4. Detach Time in Milliseconds (milliseconds)
42 // 5. Standby Thread Policy (string)
43 // Additional values may appear as necessary and will be ignored.
44 initialization::SingleWorkerPoolConfiguration
45 StringToSingleWorkerPoolConfiguration(const base::StringPiece pool_descriptor) {
46 using StandbyThreadPolicy =
47 base::SchedulerWorkerPoolParams::StandbyThreadPolicy;
48 const std::vector<base::StringPiece> tokens = SplitStringPiece(
49 pool_descriptor, ";", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
50 // Normally, we wouldn't initialize the values below because we don't read
51 // from them before we write to them. However, some compilers (like MSVC)
52 // complain about uninitialized varaibles due to the as_string() call below.
53 int min = 0;
54 int max = 0;
55 double cores_multiplier = 0.0;
56 int offset = 0;
57 int detach_milliseconds = 0;
58 // Checking for a size greater than the expected amount allows us to be
59 // forward compatible if we add more variation values.
60 if (tokens.size() >= 5 && base::StringToInt(tokens[0], &min) &&
61 base::StringToInt(tokens[1], &max) &&
62 base::StringToDouble(tokens[2].as_string(), &cores_multiplier) &&
63 base::StringToInt(tokens[3], &offset) &&
64 base::StringToInt(tokens[4], &detach_milliseconds)) {
65 initialization::SingleWorkerPoolConfiguration config;
66 config.threads = base::RecommendedMaxNumberOfThreadsInPool(
67 min, max, cores_multiplier, offset);
68 config.detach_period =
69 base::TimeDelta::FromMilliseconds(detach_milliseconds);
70 config.standby_thread_policy = (tokens.size() >= 6 && tokens[5] == "lazy")
71 ? StandbyThreadPolicy::LAZY
72 : StandbyThreadPolicy::ONE;
73 return config;
74 }
75 DLOG(ERROR) << "Invalid Worker Pool Descriptor: " << pool_descriptor;
76 return initialization::SingleWorkerPoolConfiguration();
77 }
78
79 // Converts a browser-based |variation_params| to
80 // std::vector<base::SchedulerWorkerPoolParams>. Returns an empty vector on
81 // failure.
82 std::vector<base::SchedulerWorkerPoolParams>
83 VariationsParamsToSchedulerWorkerPoolParamsVector(
84 const std::map<std::string, std::string>& variation_params) {
85 static const char* const kWorkerPoolNames[] = {
86 "Background", "BackgroundFileIO", "Foreground", "ForegroundFileIO"};
87 static_assert(
88 arraysize(kWorkerPoolNames) == initialization::WORKER_POOL_COUNT,
89 "Mismatched Worker Pool Types and Worker Pool Names");
90 initialization::BrowserWorkerPoolsConfiguration config;
91 initialization::SingleWorkerPoolConfiguration* const all_pools[]{
92 &config.background, &config.background_file_io, &config.foreground,
93 &config.foreground_file_io,
94 };
95 static_assert(arraysize(kWorkerPoolNames) == arraysize(all_pools),
96 "Mismatched Worker Pool Names and All Pools Array");
97 for (size_t i = 0; i < arraysize(kWorkerPoolNames); ++i) {
98 const auto* const worker_pool_name = kWorkerPoolNames[i];
99 const auto pair = variation_params.find(worker_pool_name);
100 if (pair == variation_params.end()) {
101 DLOG(ERROR) << "Missing Worker Pool Configuration: " << worker_pool_name;
102 return std::vector<base::SchedulerWorkerPoolParams>();
103 }
104
105 auto* const pool_config = all_pools[i];
106 *pool_config = StringToSingleWorkerPoolConfiguration(pair->second);
107 if (pool_config->threads <= 0 ||
108 pool_config->detach_period <= base::TimeDelta()) {
109 DLOG(ERROR) << "Invalid Worker Pool Configuration: " << worker_pool_name
110 << " [" << pair->second << "]";
111 return std::vector<base::SchedulerWorkerPoolParams>();
112 }
113 }
114 return BrowserWorkerPoolConfigurationToSchedulerWorkerPoolParams(config);
115 }
116
117 } // namespace
118
119 std::vector<base::SchedulerWorkerPoolParams>
120 GetBrowserSchedulerWorkerPoolParamsFromVariations() {
121 std::map<std::string, std::string> variation_params;
122 if (!::variations::GetVariationParams(kFieldTrialName, &variation_params))
123 return std::vector<base::SchedulerWorkerPoolParams>();
124
125 return VariationsParamsToSchedulerWorkerPoolParamsVector(variation_params);
126 }
127
128 void MaybePerformBrowserTaskSchedulerRedirection() {
129 std::map<std::string, std::string> variation_params;
130 ::variations::GetVariationParams(kFieldTrialName, &variation_params);
131
132 // TODO(gab): Remove this when http://crbug.com/622400 concludes.
133 const auto sequenced_worker_pool_param =
134 variation_params.find("RedirectSequencedWorkerPools");
135 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
136 switches::kDisableBrowserTaskScheduler) &&
137 sequenced_worker_pool_param != variation_params.end() &&
138 sequenced_worker_pool_param->second == "true") {
139 base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess();
140 } else {
141 base::SequencedWorkerPool::EnableForProcess();
142 }
143 }
144
145 } // variations
146 } // namespace task_scheduler_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698