Mingyu Lei | 0be39d3 | 2023-10-18 11:13:38 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
| 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 "base/allocator/miracle_parameter.h" |
| 6 | |
| 7 | #include "base/command_line.h" |
| 8 | #include "base/strings/strcat.h" |
| 9 | #include "base/system/sys_info.h" |
| 10 | |
Takashi Toyoshima | 267198895 | 2024-08-26 05:58:34 | [diff] [blame] | 11 | namespace base::miracle_parameter { |
Mingyu Lei | 0be39d3 | 2023-10-18 11:13:38 | [diff] [blame] | 12 | |
| 13 | std::string GetParamNameWithSuffix(const std::string& param_name) { |
| 14 | // `base::SysInfo::AmountOfPhysicalMemoryMB()` refers to CommandLine |
| 15 | // internally. If the CommandLine is not initialized, we return early to avoid |
| 16 | // a crash. |
| 17 | if (!base::CommandLine::InitializedForCurrentProcess()) { |
| 18 | return param_name; |
| 19 | } |
| 20 | int physical_memory_mb = base::SysInfo::AmountOfPhysicalMemoryMB(); |
| 21 | const char* suffix = |
| 22 | physical_memory_mb < kMiracleParameterMemory512MB ? "ForLessThan512MB" |
| 23 | : physical_memory_mb < kMiracleParameterMemory1GB ? "For512MBTo1GB" |
| 24 | : physical_memory_mb < kMiracleParameterMemory2GB ? "For1GBTo2GB" |
| 25 | : physical_memory_mb < kMiracleParameterMemory4GB ? "For2GBTo4GB" |
| 26 | : physical_memory_mb < kMiracleParameterMemory8GB ? "For4GBTo8GB" |
| 27 | : physical_memory_mb < kMiracleParameterMemory16GB ? "For8GBTo16GB" |
| 28 | : "For16GBAndAbove"; |
| 29 | return base::StrCat({param_name, suffix}); |
| 30 | } |
| 31 | |
| 32 | std::string GetMiracleParameterAsString(const base::Feature& feature, |
| 33 | const std::string& param_name, |
| 34 | const std::string& default_value) { |
| 35 | return GetFieldTrialParamByFeatureAsString( |
| 36 | feature, GetParamNameWithSuffix(param_name), |
| 37 | GetFieldTrialParamByFeatureAsString(feature, param_name, default_value)); |
| 38 | } |
| 39 | |
| 40 | double GetMiracleParameterAsDouble(const base::Feature& feature, |
| 41 | const std::string& param_name, |
| 42 | double default_value) { |
| 43 | return base::GetFieldTrialParamByFeatureAsDouble( |
| 44 | feature, GetParamNameWithSuffix(param_name), |
| 45 | base::GetFieldTrialParamByFeatureAsDouble(feature, param_name, |
| 46 | default_value)); |
| 47 | } |
| 48 | |
| 49 | int GetMiracleParameterAsInt(const base::Feature& feature, |
| 50 | const std::string& param_name, |
| 51 | int default_value) { |
| 52 | return base::GetFieldTrialParamByFeatureAsInt( |
| 53 | feature, GetParamNameWithSuffix(param_name), |
| 54 | base::GetFieldTrialParamByFeatureAsInt(feature, param_name, |
| 55 | default_value)); |
| 56 | } |
| 57 | |
| 58 | bool GetMiracleParameterAsBool(const base::Feature& feature, |
| 59 | const std::string& param_name, |
| 60 | bool default_value) { |
| 61 | return base::GetFieldTrialParamByFeatureAsBool( |
| 62 | feature, GetParamNameWithSuffix(param_name), |
| 63 | base::GetFieldTrialParamByFeatureAsBool(feature, param_name, |
| 64 | default_value)); |
| 65 | } |
| 66 | |
| 67 | base::TimeDelta GetMiracleParameterAsTimeDelta(const base::Feature& feature, |
| 68 | const std::string& param_name, |
| 69 | base::TimeDelta default_value) { |
| 70 | return base::GetFieldTrialParamByFeatureAsTimeDelta( |
| 71 | feature, GetParamNameWithSuffix(param_name), |
| 72 | base::GetFieldTrialParamByFeatureAsTimeDelta(feature, param_name, |
| 73 | default_value)); |
| 74 | } |
| 75 | |
Takashi Toyoshima | 267198895 | 2024-08-26 05:58:34 | [diff] [blame] | 76 | } // namespace base::miracle_parameter |