blob: 1e5eaed6302cb6b98326c0f775492dc1166ab251 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2018 The Chromium Authors
Xi Han636c7ca52018-10-04 16:56:292// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/browser/startup_helper.h"
6
Alexei Svitkine8724ea502019-06-14 21:51:467#include <algorithm>
8#include <memory>
9#include <set>
10#include <string>
11
Xi Han636c7ca52018-10-04 16:56:2912#include "base/base_switches.h"
13#include "base/command_line.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0314#include "base/system/sys_info.h"
Gabriel Charette52fa3ae2019-04-15 21:44:3715#include "base/task/thread_pool/initialization_util.h"
Gabriel Charetteeadf58862019-08-29 05:20:2716#include "base/task/thread_pool/thread_pool_instance.h"
Xi Han8012e462018-10-05 19:52:3017#include "build/build_config.h"
Gabriel Charette52fa3ae2019-04-15 21:44:3718#include "content/common/thread_pool_util.h"
Lily Chend49e3752019-08-09 19:05:2419#include "content/public/common/content_switch_dependent_feature_overrides.h"
Xi Han8012e462018-10-05 19:52:3020#include "content/public/common/content_switches.h"
Xi Han636c7ca52018-10-04 16:56:2921
22namespace content {
23
24std::unique_ptr<base::FieldTrialList> SetUpFieldTrialsAndFeatureList() {
Alexei Svitkine8724ea502019-06-14 21:51:4625 std::unique_ptr<base::FieldTrialList> field_trial_list;
Josh Berenhaus0f3893f2023-11-08 00:57:3326 if (!base::FieldTrialList::GetInstance()) {
Steven Holtea84bc222022-09-29 07:11:4627 field_trial_list = std::make_unique<base::FieldTrialList>();
Josh Berenhaus0f3893f2023-11-08 00:57:3328 }
29
Xi Han636c7ca52018-10-04 16:56:2930 const base::CommandLine* command_line =
31 base::CommandLine::ForCurrentProcess();
32
33 // Ensure any field trials specified on the command line are initialized.
34 if (command_line->HasSwitch(::switches::kForceFieldTrials)) {
35 // Create field trials without activating them, so that this behaves in a
36 // consistent manner with field trials created from the server.
37 bool result = base::FieldTrialList::CreateTrialsFromString(
Ilya Sherman7633f362020-06-23 21:54:4138 command_line->GetSwitchValueASCII(::switches::kForceFieldTrials));
Xi Han636c7ca52018-10-04 16:56:2939 CHECK(result) << "Invalid --" << ::switches::kForceFieldTrials
40 << " list specified.";
41 }
42
Gabriel Gauthier-Shalom89627a67a2023-11-29 22:11:1943 base::FeatureList::InitInstance(
Xi Han636c7ca52018-10-04 16:56:2944 command_line->GetSwitchValueASCII(switches::kEnableFeatures),
Lily Chend49e3752019-08-09 19:05:2445 command_line->GetSwitchValueASCII(switches::kDisableFeatures),
46 GetSwitchDependentFeatureOverrides(*command_line));
Xi Han636c7ca52018-10-04 16:56:2947 return field_trial_list;
48}
49
Ben Kelly874806f2020-05-08 14:12:4750namespace {
Ben Kelly874806f2020-05-08 14:12:4751
Minoru Chikamune1b266e32024-09-04 09:43:2652#if BUILDFLAG(IS_ANDROID)
53// Mobile config, for iOS see ios/web/app/web_main_loop.cc.
54constexpr size_t kThreadPoolDefaultMin = 6;
55constexpr size_t kThreadPoolMax = 8;
56constexpr double kThreadPoolCoresMultiplier = 0.6;
57constexpr int kThreadPoolOffset = 0;
58#else
59// Desktop config.
60constexpr size_t kThreadPoolDefaultMin = 16;
61constexpr size_t kThreadPoolMax = 32;
62constexpr double kThreadPoolCoresMultiplier = 0.6;
63constexpr int kThreadPoolOffset = 0;
64#endif
65
Daniel Cheng0abd9f32022-09-22 04:20:1166BASE_FEATURE(kBrowserThreadPoolAdjustment,
67 "BrowserThreadPoolAdjustment",
Minoru Chikamune1b266e32024-09-04 09:43:2668 base::FEATURE_DISABLED_BY_DEFAULT);
Ben Kelly874806f2020-05-08 14:12:4769
Minoru Chikamune1b266e32024-09-04 09:43:2670const base::FeatureParam<int> kBrowserThreadPoolMin{
71 &kBrowserThreadPoolAdjustment, "min", kThreadPoolDefaultMin};
Minoru Chikamune1e341152023-09-04 10:20:4472
Ben Kelly874806f2020-05-08 14:12:4773} // namespace
74
Francois Doray7f777312019-05-16 12:26:3175// TODO(scheduler-dev): Standardize thread pool logic and remove the need for
76// specifying thread count manually.
Gabriel Charette52fa3ae2019-04-15 21:44:3777void StartBrowserThreadPool() {
Ben Kelly874806f2020-05-08 14:12:4778 // Ensure we always support at least one thread regardless of the field trial
79 // param setting.
Minoru Chikamune900c9ad2024-05-21 12:56:2080 size_t min =
Minoru Chikamune1b266e32024-09-04 09:43:2681 base::checked_cast<size_t>(std::max(kBrowserThreadPoolMin.Get(), 1));
Gabriel Charette43fd3702019-05-29 16:36:5182 base::ThreadPoolInstance::InitParams thread_pool_init_params = {
Ben Kelly874806f2020-05-08 14:12:4783 base::RecommendedMaxNumberOfThreadsInThreadGroup(
Minoru Chikamune1b266e32024-09-04 09:43:2684 min, kThreadPoolMax, kThreadPoolCoresMultiplier, kThreadPoolOffset)};
Francois Doray7f777312019-05-16 12:26:3185
Xiaohan Wang1ecfd002022-01-19 22:33:1086#if BUILDFLAG(IS_WIN)
Gabriel Charette43fd3702019-05-29 16:36:5187 thread_pool_init_params.common_thread_pool_environment = base::
88 ThreadPoolInstance::InitParams::CommonThreadPoolEnvironment::COM_MTA;
Francois Doray7f777312019-05-16 12:26:3189#endif
Xi Han8012e462018-10-05 19:52:3090
91 // If a renderer lives in the browser process, adjust the number of
92 // threads in the foreground pool.
93 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
94 switches::kSingleProcess)) {
Francois Doray7f777312019-05-16 12:26:3195 thread_pool_init_params.max_num_foreground_threads =
96 std::max(GetMinForegroundThreadsInRendererThreadPool(),
97 thread_pool_init_params.max_num_foreground_threads);
Xi Han8012e462018-10-05 19:52:3098 }
99
Gabriel Charette43fd3702019-05-29 16:36:51100 base::ThreadPoolInstance::Get()->Start(thread_pool_init_params);
Xi Han8012e462018-10-05 19:52:30101}
102
Xi Han636c7ca52018-10-04 16:56:29103} // namespace content