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

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

Issue 2553953002: Split initialization_util into a Hermetic Library and a Variations Library (Closed)
Patch Set: Quick Wording Fix 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<std::string> tokens = SplitString(
49 pool_descriptor, ";", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
50 int min;
51 int max;
52 double cores_multiplier;
53 int offset;
54 int detach_milliseconds;
55 // Checking for a size greater than the expected amount allows us to be
56 // forward compatible if we add more variation values.
57 if (tokens.size() >= 5 && base::StringToInt(tokens[0], &min) &&
58 base::StringToInt(tokens[1], &max) &&
59 base::StringToDouble(tokens[2], &cores_multiplier) &&
60 base::StringToInt(tokens[3], &offset) &&
61 base::StringToInt(tokens[4], &detach_milliseconds)) {
62 initialization::SingleWorkerPoolConfiguration config;
63 constexpr size_t kSizeAssignedFields = sizeof(config.threads) +
64 sizeof(config.detach_period) +
65 sizeof(config.standby_thread_policy);
66 static_assert(kSizeAssignedFields == sizeof(config),
67 "Not all fields were assigned");
68 config.threads = base::RecommendedMaxNumberOfThreadsInPool(
69 min, max, cores_multiplier, offset);
70 config.detach_period =
71 base::TimeDelta::FromMilliseconds(detach_milliseconds);
72 config.standby_thread_policy = (tokens.size() >= 6 && tokens[5] == "lazy")
73 ? StandbyThreadPolicy::LAZY
74 : StandbyThreadPolicy::ONE;
75 return config;
76 }
77 DLOG(ERROR) << "Invalid Worker Pool Descriptor: " << pool_descriptor;
78 return initialization::SingleWorkerPoolConfiguration();
79 }
80
81 // Converts a browser-based |variation_params| to
82 // std::vector<base::SchedulerWorkerPoolParams>. Returns an empty vector on
83 // failure.
84 std::vector<base::SchedulerWorkerPoolParams>
85 VariationsParamsToSchedulerWorkerPoolParamsVector(
86 const std::map<std::string, std::string>& variation_params) {
87 static const char* const kWorkerPoolNames[] = {
88 "Background", "BackgroundFileIO", "Foreground", "ForegroundFileIO"};
89 static_assert(
90 arraysize(kWorkerPoolNames) == initialization::WORKER_POOL_COUNT,
91 "Mismatched Worker Pool Types and Worker Pool Names");
92 initialization::BrowserWorkerPoolsConfiguration config;
93 initialization::SingleWorkerPoolConfiguration* const all_pools[]{
94 &config.background, &config.background_file_io, &config.foreground,
95 &config.foreground_file_io,
96 };
97 static_assert(arraysize(kWorkerPoolNames) == arraysize(all_pools),
98 "Mismatched Worker Pool Names and All Pools Array");
99 for (size_t i = 0; i < arraysize(kWorkerPoolNames); ++i) {
100 const auto* const worker_pool_name = kWorkerPoolNames[i];
101 const auto pair = variation_params.find(worker_pool_name);
102 if (pair == variation_params.end()) {
103 DLOG(ERROR) << "Missing Worker Pool Configuration: " << worker_pool_name;
104 return std::vector<base::SchedulerWorkerPoolParams>();
105 }
106
107 auto* const pool_config = all_pools[i];
108 *pool_config = StringToSingleWorkerPoolConfiguration(pair->second);
109 if (pool_config->threads <= 0 || pool_config->detach_period.is_zero() ||
110 pool_config->detach_period < base::TimeDelta()) {
gab 2016/12/07 15:53:06 <= and drop is_zero() check
robliao 2016/12/07 18:39:19 Done.
111 DLOG(ERROR) << "Invalid Worker Pool Configuration: " << worker_pool_name
112 << " [" << pair->second << "]";
113 return std::vector<base::SchedulerWorkerPoolParams>();
114 }
115 }
116 return BrowserWorkerPoolConfigurationToSchedulerWorkerPoolParams(config);
117 }
118
119 } // namespace
120
121 std::vector<base::SchedulerWorkerPoolParams>
122 GetBrowserSchedulerWorkerPoolParamsFromVariations() {
123 std::map<std::string, std::string> variation_params;
124 if (!::variations::GetVariationParams(kFieldTrialName, &variation_params))
125 return std::vector<base::SchedulerWorkerPoolParams>();
126
127 return VariationsParamsToSchedulerWorkerPoolParamsVector(variation_params);
128 }
129
130 void MaybePerformBrowserTaskSchedulerRedirection() {
131 std::map<std::string, std::string> variation_params;
132 ::variations::GetVariationParams(kFieldTrialName, &variation_params);
133
134 // TODO(gab): Remove this when http://crbug.com/622400 concludes.
135 const auto sequenced_worker_pool_param =
136 variation_params.find("RedirectSequencedWorkerPools");
137 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
138 switches::kDisableBrowserTaskScheduler) &&
139 sequenced_worker_pool_param != variation_params.end() &&
140 sequenced_worker_pool_param->second == "true") {
141 base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess();
142 } else {
143 base::SequencedWorkerPool::EnableForProcess();
144 }
145 }
146
147 } // variations
148 } // namespace task_scheduler_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698