Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 // The below call would normally be inline, but some compilers (like MSVC) | |
| 51 // will warn that the number declarations below are uninitialized. Putting the | |
| 52 // as_string() call immediately above the declarations works around that. | |
|
gab
2016/12/07 20:17:22
Hmm that seems incorrect to me, i.e. say we fix St
robliao
2016/12/07 21:55:37
Initialized to 0 + added a comment. Changing Strin
| |
| 53 const std::string tokens_2_str = tokens[2].as_string(); | |
| 54 int min; | |
| 55 int max; | |
| 56 double cores_multiplier; | |
| 57 int offset; | |
| 58 int detach_milliseconds; | |
| 59 // Checking for a size greater than the expected amount allows us to be | |
| 60 // forward compatible if we add more variation values. | |
| 61 if (tokens.size() >= 5 && base::StringToInt(tokens[0], &min) && | |
| 62 base::StringToInt(tokens[1], &max) && | |
| 63 base::StringToDouble(tokens_2_str, &cores_multiplier) && | |
| 64 base::StringToInt(tokens[3], &offset) && | |
| 65 base::StringToInt(tokens[4], &detach_milliseconds)) { | |
| 66 initialization::SingleWorkerPoolConfiguration config; | |
| 67 config.threads = base::RecommendedMaxNumberOfThreadsInPool( | |
| 68 min, max, cores_multiplier, offset); | |
| 69 config.detach_period = | |
| 70 base::TimeDelta::FromMilliseconds(detach_milliseconds); | |
| 71 config.standby_thread_policy = (tokens.size() >= 6 && tokens[5] == "lazy") | |
| 72 ? StandbyThreadPolicy::LAZY | |
| 73 : StandbyThreadPolicy::ONE; | |
| 74 return config; | |
| 75 } | |
| 76 DLOG(ERROR) << "Invalid Worker Pool Descriptor: " << pool_descriptor; | |
| 77 return initialization::SingleWorkerPoolConfiguration(); | |
| 78 } | |
| 79 | |
| 80 // Converts a browser-based |variation_params| to | |
| 81 // std::vector<base::SchedulerWorkerPoolParams>. Returns an empty vector on | |
| 82 // failure. | |
| 83 std::vector<base::SchedulerWorkerPoolParams> | |
| 84 VariationsParamsToSchedulerWorkerPoolParamsVector( | |
| 85 const std::map<std::string, std::string>& variation_params) { | |
| 86 static const char* const kWorkerPoolNames[] = { | |
| 87 "Background", "BackgroundFileIO", "Foreground", "ForegroundFileIO"}; | |
| 88 static_assert( | |
| 89 arraysize(kWorkerPoolNames) == initialization::WORKER_POOL_COUNT, | |
| 90 "Mismatched Worker Pool Types and Worker Pool Names"); | |
| 91 initialization::BrowserWorkerPoolsConfiguration config; | |
| 92 initialization::SingleWorkerPoolConfiguration* const all_pools[]{ | |
| 93 &config.background, &config.background_file_io, &config.foreground, | |
| 94 &config.foreground_file_io, | |
| 95 }; | |
| 96 static_assert(arraysize(kWorkerPoolNames) == arraysize(all_pools), | |
| 97 "Mismatched Worker Pool Names and All Pools Array"); | |
| 98 for (size_t i = 0; i < arraysize(kWorkerPoolNames); ++i) { | |
| 99 const auto* const worker_pool_name = kWorkerPoolNames[i]; | |
| 100 const auto pair = variation_params.find(worker_pool_name); | |
| 101 if (pair == variation_params.end()) { | |
| 102 DLOG(ERROR) << "Missing Worker Pool Configuration: " << worker_pool_name; | |
| 103 return std::vector<base::SchedulerWorkerPoolParams>(); | |
| 104 } | |
| 105 | |
| 106 auto* const pool_config = all_pools[i]; | |
| 107 *pool_config = StringToSingleWorkerPoolConfiguration(pair->second); | |
| 108 if (pool_config->threads <= 0 || | |
| 109 pool_config->detach_period <= base::TimeDelta()) { | |
| 110 DLOG(ERROR) << "Invalid Worker Pool Configuration: " << worker_pool_name | |
| 111 << " [" << pair->second << "]"; | |
| 112 return std::vector<base::SchedulerWorkerPoolParams>(); | |
| 113 } | |
| 114 } | |
| 115 return BrowserWorkerPoolConfigurationToSchedulerWorkerPoolParams(config); | |
| 116 } | |
| 117 | |
| 118 } // namespace | |
| 119 | |
| 120 std::vector<base::SchedulerWorkerPoolParams> | |
| 121 GetBrowserSchedulerWorkerPoolParamsFromVariations() { | |
| 122 std::map<std::string, std::string> variation_params; | |
| 123 if (!::variations::GetVariationParams(kFieldTrialName, &variation_params)) | |
| 124 return std::vector<base::SchedulerWorkerPoolParams>(); | |
| 125 | |
| 126 return VariationsParamsToSchedulerWorkerPoolParamsVector(variation_params); | |
| 127 } | |
| 128 | |
| 129 void MaybePerformBrowserTaskSchedulerRedirection() { | |
| 130 std::map<std::string, std::string> variation_params; | |
| 131 ::variations::GetVariationParams(kFieldTrialName, &variation_params); | |
| 132 | |
| 133 // TODO(gab): Remove this when http://crbug.com/622400 concludes. | |
| 134 const auto sequenced_worker_pool_param = | |
| 135 variation_params.find("RedirectSequencedWorkerPools"); | |
| 136 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 137 switches::kDisableBrowserTaskScheduler) && | |
| 138 sequenced_worker_pool_param != variation_params.end() && | |
| 139 sequenced_worker_pool_param->second == "true") { | |
| 140 base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess(); | |
| 141 } else { | |
| 142 base::SequencedWorkerPool::EnableForProcess(); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 } // variations | |
| 147 } // namespace task_scheduler_util | |
| OLD | NEW |