Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/chrome_browser_main.h" | 5 #include "chrome/browser/chrome_browser_main.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | |
| 11 #include <map> | |
| 10 #include <set> | 12 #include <set> |
| 11 #include <string> | 13 #include <string> |
| 12 #include <utility> | 14 #include <utility> |
| 13 #include <vector> | 15 #include <vector> |
| 14 | 16 |
| 15 #include "base/at_exit.h" | 17 #include "base/at_exit.h" |
| 16 #include "base/base_switches.h" | 18 #include "base/base_switches.h" |
| 17 #include "base/bind.h" | 19 #include "base/bind.h" |
| 18 #include "base/command_line.h" | 20 #include "base/command_line.h" |
| 19 #include "base/debug/crash_logging.h" | 21 #include "base/debug/crash_logging.h" |
| 20 #include "base/debug/debugger.h" | 22 #include "base/debug/debugger.h" |
| 21 #include "base/feature_list.h" | 23 #include "base/feature_list.h" |
| 22 #include "base/files/file_path.h" | 24 #include "base/files/file_path.h" |
| 23 #include "base/files/file_util.h" | 25 #include "base/files/file_util.h" |
| 26 #include "base/logging.h" | |
| 24 #include "base/memory/ptr_util.h" | 27 #include "base/memory/ptr_util.h" |
| 25 #include "base/metrics/field_trial.h" | 28 #include "base/metrics/field_trial.h" |
| 26 #include "base/metrics/histogram_macros.h" | 29 #include "base/metrics/histogram_macros.h" |
| 27 #include "base/path_service.h" | 30 #include "base/path_service.h" |
| 28 #include "base/profiler/scoped_tracker.h" | 31 #include "base/profiler/scoped_tracker.h" |
| 29 #include "base/run_loop.h" | 32 #include "base/run_loop.h" |
| 30 #include "base/strings/string_number_conversions.h" | 33 #include "base/strings/string_number_conversions.h" |
| 31 #include "base/strings/string_piece.h" | 34 #include "base/strings/string_piece.h" |
| 32 #include "base/strings/string_split.h" | 35 #include "base/strings/string_split.h" |
| 33 #include "base/strings/sys_string_conversions.h" | 36 #include "base/strings/sys_string_conversions.h" |
| 34 #include "base/strings/utf_string_conversions.h" | 37 #include "base/strings/utf_string_conversions.h" |
| 35 #include "base/sys_info.h" | 38 #include "base/sys_info.h" |
| 39 #include "base/task_scheduler/scheduler_worker_pool_params.h" | |
| 40 #include "base/task_scheduler/task_scheduler.h" | |
| 41 #include "base/task_scheduler/task_traits.h" | |
| 36 #include "base/threading/platform_thread.h" | 42 #include "base/threading/platform_thread.h" |
| 37 #include "base/time/default_tick_clock.h" | 43 #include "base/time/default_tick_clock.h" |
| 38 #include "base/time/time.h" | 44 #include "base/time/time.h" |
| 39 #include "base/trace_event/trace_event.h" | 45 #include "base/trace_event/trace_event.h" |
| 40 #include "base/values.h" | 46 #include "base/values.h" |
| 41 #include "build/build_config.h" | 47 #include "build/build_config.h" |
| 42 #include "cc/base/switches.h" | 48 #include "cc/base/switches.h" |
| 43 #include "chrome/browser/about_flags.h" | 49 #include "chrome/browser/about_flags.h" |
| 44 #include "chrome/browser/after_startup_task_utils.h" | 50 #include "chrome/browser/after_startup_task_utils.h" |
| 45 #include "chrome/browser/browser_process.h" | 51 #include "chrome/browser/browser_process.h" |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 void AddFirstRunNewTabs(StartupBrowserCreator* browser_creator, | 316 void AddFirstRunNewTabs(StartupBrowserCreator* browser_creator, |
| 311 const std::vector<GURL>& new_tabs) { | 317 const std::vector<GURL>& new_tabs) { |
| 312 for (std::vector<GURL>::const_iterator it = new_tabs.begin(); | 318 for (std::vector<GURL>::const_iterator it = new_tabs.begin(); |
| 313 it != new_tabs.end(); ++it) { | 319 it != new_tabs.end(); ++it) { |
| 314 if (it->is_valid()) | 320 if (it->is_valid()) |
| 315 browser_creator->AddFirstRunTab(*it); | 321 browser_creator->AddFirstRunTab(*it); |
| 316 } | 322 } |
| 317 } | 323 } |
| 318 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS) | 324 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 319 | 325 |
| 326 enum WorkerPoolType : size_t { | |
| 327 BACKGROUND_WORKER_POOL = 0, | |
| 328 BACKGROUND_FILE_IO_WORKER_POOL, | |
| 329 FOREGROUND_WORKER_POOL, | |
| 330 FOREGROUND_FILE_IO_WORKER_POOL, | |
| 331 WORKER_POOL_COUNT, | |
| 332 }; | |
| 333 | |
| 334 struct WorkerPoolVariationValues { | |
| 335 int threads = 0; | |
| 336 base::TimeDelta detach_period; | |
| 337 }; | |
| 338 | |
| 339 // Converts |pool_descriptor| to a WorkerPoolVariationValues. Returns a default | |
| 340 // WorkerPoolVariationValues on failure. | |
| 341 // | |
| 342 // |pool_descriptor| is a comma separated value string with the following items: | |
| 343 // 1. Minimum Thread Count (int) | |
| 344 // 2. Maximum Thread Count (int) | |
| 345 // 3. Thread Count Multiplier (double) | |
| 346 // 4. Thread Count Offset (int) | |
| 347 // 5. Detach Time in Milliseconds (milliseconds) | |
| 348 // Additional values may appear as necessary and will be ignored. | |
| 349 WorkerPoolVariationValues StringToWorkerPoolVariationValues( | |
| 350 const base::StringPiece& pool_descriptor) { | |
|
fdoray
2016/08/06 00:39:55
Prefer passing StringPiece by value https://cs.chr
robliao
2016/08/06 01:19:46
Done.
| |
| 351 const std::vector<std::string> tokens = | |
| 352 SplitString(pool_descriptor, ",", | |
| 353 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 354 int minimum; | |
| 355 int maximum; | |
| 356 double multiplier; | |
| 357 int offset; | |
| 358 int detach_milliseconds; | |
| 359 // Checking for a size greater than the expected amount allows us to be | |
| 360 // forward compatible if we add more variation values. | |
| 361 if (tokens.size() >= 5 && | |
| 362 base::StringToInt(tokens[0], &minimum) && | |
| 363 base::StringToInt(tokens[1], &maximum) && | |
| 364 base::StringToDouble(tokens[2], &multiplier) && | |
| 365 base::StringToInt(tokens[3], &offset) && | |
| 366 base::StringToInt(tokens[4], &detach_milliseconds)) { | |
| 367 const int num_of_cores = base::SysInfo::NumberOfProcessors(); | |
| 368 const int threads = std::round<int>(num_of_cores * multiplier) + offset; | |
| 369 WorkerPoolVariationValues values; | |
| 370 values.threads = std::min(maximum, std::max(minimum, threads)); | |
| 371 values.detach_period = | |
| 372 base::TimeDelta::FromMilliseconds(detach_milliseconds); | |
| 373 return values; | |
| 374 } | |
| 375 LOG(WARNING) << "Invalid Worker Pool Descriptor: " << pool_descriptor; | |
| 376 return WorkerPoolVariationValues(); | |
| 377 } | |
| 378 | |
| 379 // Returns the worker pool index for |traits| defaulting to | |
| 380 // FOREGROUND_WORKER_POOL or FOREGROUND_FILE_IO_WORKER_POOL on unknown | |
| 381 // priorities. | |
| 382 size_t WorkerPoolIndexForTraits(const base::TaskTraits& traits) { | |
| 383 if (traits.with_file_io()) { | |
| 384 return traits.priority() == base::TaskPriority::BACKGROUND | |
| 385 ? BACKGROUND_FILE_IO_WORKER_POOL | |
| 386 : FOREGROUND_FILE_IO_WORKER_POOL; | |
| 387 } | |
| 388 return traits.priority() == base::TaskPriority::BACKGROUND | |
| 389 ? BACKGROUND_WORKER_POOL | |
| 390 : FOREGROUND_WORKER_POOL; | |
| 391 } | |
| 392 | |
| 393 // Initializes the Default Browser Task Scheduler if there is a valid variation | |
| 394 // parameter for the field trial. | |
| 395 void MaybeInitializeTaskScheduler() { | |
| 396 static constexpr char kFieldTrialName[] = "BrowserScheduler"; | |
| 397 std::map<std::string, std::string> variation_params; | |
| 398 if (!variations::GetVariationParams(kFieldTrialName, &variation_params)) | |
| 399 return; | |
| 400 | |
| 401 using ThreadPriority = base::ThreadPriority; | |
| 402 using IORestriction = base::SchedulerWorkerPoolParams::IORestriction; | |
| 403 struct SchedulerWorkerPoolPredefinedParams { | |
| 404 const char* name; | |
| 405 ThreadPriority priority_hint; | |
| 406 IORestriction io_restriction; | |
| 407 }; | |
| 408 static const SchedulerWorkerPoolPredefinedParams kAllPredefinedParams[] = { | |
|
fdoray
2016/08/06 00:39:55
static const struct {
const char* name;
Thread
robliao
2016/08/06 01:19:46
Every time I've done this, the reviewer has sugges
fdoray
2016/08/06 01:47:33
Acknowledged.
| |
| 409 {"Background", ThreadPriority::BACKGROUND, IORestriction::DISALLOWED}, | |
| 410 {"BackgroundFileIO", ThreadPriority::BACKGROUND, IORestriction::ALLOWED}, | |
| 411 {"Foreground", ThreadPriority::NORMAL, IORestriction::DISALLOWED}, | |
| 412 {"ForegroundFileIO", ThreadPriority::NORMAL, IORestriction::ALLOWED}, | |
| 413 }; | |
| 414 static_assert(arraysize(kAllPredefinedParams) == WORKER_POOL_COUNT, | |
| 415 "Mismatched Worker Pool Types and Predefined Parameters"); | |
| 416 | |
| 417 std::vector<base::SchedulerWorkerPoolParams> params_vector; | |
| 418 for (const auto& predefined_params : kAllPredefinedParams) { | |
| 419 const auto pair = variation_params.find(predefined_params.name); | |
| 420 if (pair == variation_params.end()) { | |
| 421 LOG(WARNING) << "Missing Worker Pool Configuration: " << | |
| 422 predefined_params.name; | |
| 423 return; | |
| 424 } | |
| 425 | |
| 426 const WorkerPoolVariationValues variation_values = | |
| 427 StringToWorkerPoolVariationValues(pair->second); | |
| 428 | |
| 429 if (variation_values.threads == 0 || | |
| 430 variation_values.detach_period.is_zero()) { | |
| 431 LOG(WARNING) << "Invalid Worker Pool Configuration: " << | |
| 432 predefined_params.name << " [" << pair->second << "]"; | |
| 433 return; | |
| 434 } | |
| 435 | |
| 436 params_vector.emplace_back(predefined_params.name, | |
| 437 predefined_params.priority_hint, | |
| 438 predefined_params.io_restriction, | |
| 439 variation_values.threads, | |
| 440 variation_values.detach_period); | |
| 441 } | |
| 442 | |
| 443 DCHECK_EQ(params_vector.size(), WORKER_POOL_COUNT); | |
| 444 | |
| 445 base::TaskScheduler::CreateAndSetDefaultTaskScheduler( | |
| 446 params_vector, base::Bind(WorkerPoolIndexForTraits)); | |
| 447 } | |
| 448 | |
| 320 // Returns the new local state object, guaranteed non-NULL. | 449 // Returns the new local state object, guaranteed non-NULL. |
| 321 // |local_state_task_runner| must be a shutdown-blocking task runner. | 450 // |local_state_task_runner| must be a shutdown-blocking task runner. |
| 322 PrefService* InitializeLocalState( | 451 PrefService* InitializeLocalState( |
| 323 base::SequencedTaskRunner* local_state_task_runner, | 452 base::SequencedTaskRunner* local_state_task_runner, |
| 324 const base::CommandLine& parsed_command_line) { | 453 const base::CommandLine& parsed_command_line) { |
| 325 TRACE_EVENT0("startup", "ChromeBrowserMainParts::InitializeLocalState") | 454 TRACE_EVENT0("startup", "ChromeBrowserMainParts::InitializeLocalState") |
| 326 | 455 |
| 327 // Load local state. This includes the application locale so we know which | 456 // Load local state. This includes the application locale so we know which |
| 328 // locale dll to load. This also causes local state prefs to be registered. | 457 // locale dll to load. This also causes local state prefs to be registered. |
| 329 PrefService* local_state = g_browser_process->local_state(); | 458 PrefService* local_state = g_browser_process->local_state(); |
| (...skipping 877 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1207 chromeos::CrosSettings::Initialize(); | 1336 chromeos::CrosSettings::Initialize(); |
| 1208 #endif // defined(OS_CHROMEOS) | 1337 #endif // defined(OS_CHROMEOS) |
| 1209 | 1338 |
| 1210 SetupOriginTrialsCommandLine(); | 1339 SetupOriginTrialsCommandLine(); |
| 1211 | 1340 |
| 1212 // Now the command line has been mutated based on about:flags, we can setup | 1341 // Now the command line has been mutated based on about:flags, we can setup |
| 1213 // metrics and initialize field trials. The field trials are needed by | 1342 // metrics and initialize field trials. The field trials are needed by |
| 1214 // IOThread's initialization which happens in BrowserProcess:PreCreateThreads. | 1343 // IOThread's initialization which happens in BrowserProcess:PreCreateThreads. |
| 1215 SetupMetricsAndFieldTrials(); | 1344 SetupMetricsAndFieldTrials(); |
| 1216 | 1345 |
| 1346 MaybeInitializeTaskScheduler(); | |
| 1347 | |
| 1217 // ChromeOS needs ResourceBundle::InitSharedInstance to be called before this. | 1348 // ChromeOS needs ResourceBundle::InitSharedInstance to be called before this. |
| 1218 browser_process_->PreCreateThreads(); | 1349 browser_process_->PreCreateThreads(); |
| 1219 | 1350 |
| 1220 device::GeolocationProvider::SetGeolocationDelegate( | 1351 device::GeolocationProvider::SetGeolocationDelegate( |
| 1221 new ChromeGeolocationDelegate()); | 1352 new ChromeGeolocationDelegate()); |
| 1222 | 1353 |
| 1223 return content::RESULT_CODE_NORMAL_EXIT; | 1354 return content::RESULT_CODE_NORMAL_EXIT; |
| 1224 } | 1355 } |
| 1225 | 1356 |
| 1226 void ChromeBrowserMainParts::PreMainMessageLoopRun() { | 1357 void ChromeBrowserMainParts::PreMainMessageLoopRun() { |
| (...skipping 841 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2068 chromeos::CrosSettings::Shutdown(); | 2199 chromeos::CrosSettings::Shutdown(); |
| 2069 #endif // defined(OS_CHROMEOS) | 2200 #endif // defined(OS_CHROMEOS) |
| 2070 #endif // defined(OS_ANDROID) | 2201 #endif // defined(OS_ANDROID) |
| 2071 } | 2202 } |
| 2072 | 2203 |
| 2073 // Public members: | 2204 // Public members: |
| 2074 | 2205 |
| 2075 void ChromeBrowserMainParts::AddParts(ChromeBrowserMainExtraParts* parts) { | 2206 void ChromeBrowserMainParts::AddParts(ChromeBrowserMainExtraParts* parts) { |
| 2076 chrome_extra_parts_.push_back(parts); | 2207 chrome_extra_parts_.push_back(parts); |
| 2077 } | 2208 } |
| OLD | NEW |