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 // Always last. |
| 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) { |
| 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::ceil<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 DLOG(ERROR) << "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 const bool is_background = |
| 384 traits.priority() == base::TaskPriority::BACKGROUND; |
| 385 if (traits.with_file_io()) { |
| 386 return is_background ? BACKGROUND_FILE_IO_WORKER_POOL |
| 387 : FOREGROUND_FILE_IO_WORKER_POOL; |
| 388 } |
| 389 return is_background ? BACKGROUND_WORKER_POOL : FOREGROUND_WORKER_POOL; |
| 390 } |
| 391 |
| 392 // Initializes the Default Browser Task Scheduler if there is a valid variation |
| 393 // parameter for the field trial. |
| 394 void MaybeInitializeTaskScheduler() { |
| 395 static constexpr char kFieldTrialName[] = "BrowserScheduler"; |
| 396 std::map<std::string, std::string> variation_params; |
| 397 if (!variations::GetVariationParams(kFieldTrialName, &variation_params)) |
| 398 return; |
| 399 |
| 400 using ThreadPriority = base::ThreadPriority; |
| 401 using IORestriction = base::SchedulerWorkerPoolParams::IORestriction; |
| 402 struct SchedulerWorkerPoolPredefinedParams { |
| 403 const char* name; |
| 404 ThreadPriority priority_hint; |
| 405 IORestriction io_restriction; |
| 406 }; |
| 407 static const SchedulerWorkerPoolPredefinedParams kAllPredefinedParams[] = { |
| 408 {"Background", ThreadPriority::BACKGROUND, IORestriction::DISALLOWED}, |
| 409 {"BackgroundFileIO", ThreadPriority::BACKGROUND, IORestriction::ALLOWED}, |
| 410 {"Foreground", ThreadPriority::NORMAL, IORestriction::DISALLOWED}, |
| 411 {"ForegroundFileIO", ThreadPriority::NORMAL, IORestriction::ALLOWED}, |
| 412 }; |
| 413 static_assert(arraysize(kAllPredefinedParams) == WORKER_POOL_COUNT, |
| 414 "Mismatched Worker Pool Types and Predefined Parameters"); |
| 415 |
| 416 std::vector<base::SchedulerWorkerPoolParams> params_vector; |
| 417 for (const auto& predefined_params : kAllPredefinedParams) { |
| 418 const auto pair = variation_params.find(predefined_params.name); |
| 419 if (pair == variation_params.end()) { |
| 420 DLOG(ERROR) << "Missing Worker Pool Configuration: " |
| 421 << predefined_params.name; |
| 422 return; |
| 423 } |
| 424 |
| 425 const WorkerPoolVariationValues variation_values = |
| 426 StringToWorkerPoolVariationValues(pair->second); |
| 427 |
| 428 if (variation_values.threads <= 0 || |
| 429 variation_values.detach_period.is_zero()) { |
| 430 DLOG(ERROR) << "Invalid Worker Pool Configuration: " << |
| 431 predefined_params.name << " [" << pair->second << "]"; |
| 432 return; |
| 433 } |
| 434 |
| 435 params_vector.emplace_back(predefined_params.name, |
| 436 predefined_params.priority_hint, |
| 437 predefined_params.io_restriction, |
| 438 variation_values.threads, |
| 439 variation_values.detach_period); |
| 440 } |
| 441 |
| 442 DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size()); |
| 443 |
| 444 base::TaskScheduler::CreateAndSetDefaultTaskScheduler( |
| 445 params_vector, base::Bind(WorkerPoolIndexForTraits)); |
| 446 } |
| 447 |
320 // Returns the new local state object, guaranteed non-NULL. | 448 // Returns the new local state object, guaranteed non-NULL. |
321 // |local_state_task_runner| must be a shutdown-blocking task runner. | 449 // |local_state_task_runner| must be a shutdown-blocking task runner. |
322 PrefService* InitializeLocalState( | 450 PrefService* InitializeLocalState( |
323 base::SequencedTaskRunner* local_state_task_runner, | 451 base::SequencedTaskRunner* local_state_task_runner, |
324 const base::CommandLine& parsed_command_line) { | 452 const base::CommandLine& parsed_command_line) { |
325 TRACE_EVENT0("startup", "ChromeBrowserMainParts::InitializeLocalState") | 453 TRACE_EVENT0("startup", "ChromeBrowserMainParts::InitializeLocalState") |
326 | 454 |
327 // Load local state. This includes the application locale so we know which | 455 // 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. | 456 // locale dll to load. This also causes local state prefs to be registered. |
329 PrefService* local_state = g_browser_process->local_state(); | 457 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(); | 1335 chromeos::CrosSettings::Initialize(); |
1208 #endif // defined(OS_CHROMEOS) | 1336 #endif // defined(OS_CHROMEOS) |
1209 | 1337 |
1210 SetupOriginTrialsCommandLine(); | 1338 SetupOriginTrialsCommandLine(); |
1211 | 1339 |
1212 // Now the command line has been mutated based on about:flags, we can setup | 1340 // 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 | 1341 // metrics and initialize field trials. The field trials are needed by |
1214 // IOThread's initialization which happens in BrowserProcess:PreCreateThreads. | 1342 // IOThread's initialization which happens in BrowserProcess:PreCreateThreads. |
1215 SetupMetricsAndFieldTrials(); | 1343 SetupMetricsAndFieldTrials(); |
1216 | 1344 |
| 1345 MaybeInitializeTaskScheduler(); |
| 1346 |
1217 // ChromeOS needs ResourceBundle::InitSharedInstance to be called before this. | 1347 // ChromeOS needs ResourceBundle::InitSharedInstance to be called before this. |
1218 browser_process_->PreCreateThreads(); | 1348 browser_process_->PreCreateThreads(); |
1219 | 1349 |
1220 device::GeolocationProvider::SetGeolocationDelegate( | 1350 device::GeolocationProvider::SetGeolocationDelegate( |
1221 new ChromeGeolocationDelegate()); | 1351 new ChromeGeolocationDelegate()); |
1222 | 1352 |
1223 return content::RESULT_CODE_NORMAL_EXIT; | 1353 return content::RESULT_CODE_NORMAL_EXIT; |
1224 } | 1354 } |
1225 | 1355 |
1226 void ChromeBrowserMainParts::PreMainMessageLoopRun() { | 1356 void ChromeBrowserMainParts::PreMainMessageLoopRun() { |
(...skipping 841 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2068 chromeos::CrosSettings::Shutdown(); | 2198 chromeos::CrosSettings::Shutdown(); |
2069 #endif // defined(OS_CHROMEOS) | 2199 #endif // defined(OS_CHROMEOS) |
2070 #endif // defined(OS_ANDROID) | 2200 #endif // defined(OS_ANDROID) |
2071 } | 2201 } |
2072 | 2202 |
2073 // Public members: | 2203 // Public members: |
2074 | 2204 |
2075 void ChromeBrowserMainParts::AddParts(ChromeBrowserMainExtraParts* parts) { | 2205 void ChromeBrowserMainParts::AddParts(ChromeBrowserMainExtraParts* parts) { |
2076 chrome_extra_parts_.push_back(parts); | 2206 chrome_extra_parts_.push_back(parts); |
2077 } | 2207 } |
OLD | NEW |