xref: /aosp_15_r20/external/cronet/base/allocator/miracle_parameter.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #ifndef BASE_ALLOCATOR_MIRACLE_PARAMETER_H_
6 #define BASE_ALLOCATOR_MIRACLE_PARAMETER_H_
7 
8 #include "base/base_export.h"
9 #include "base/containers/span.h"
10 #include "base/feature_list.h"
11 #include "base/metrics/field_trial_params.h"
12 
13 // This is a mirror copy of the //components/miracle_parameter/ to resolve the
14 // dependency cycle of (base->miracle_parameter->base).
15 // Eventually the miracle_parameter component will have a public interface in
16 // //base/ and this could be removed.
17 // TODO(crbug.com/1475915): remove miracle_parameter from
18 // //base/allocator/.
19 
20 namespace base {
21 
22 namespace miracle_parameter {
23 
24 namespace {
25 
26 template <typename Enum>
GetFieldTrialParamByFeatureAsEnum(const base::Feature & feature,const std::string & param_name,const Enum default_value,const base::span<const typename base::FeatureParam<Enum>::Option> & options)27 Enum GetFieldTrialParamByFeatureAsEnum(
28     const base::Feature& feature,
29     const std::string& param_name,
30     const Enum default_value,
31     const base::span<const typename base::FeatureParam<Enum>::Option>&
32         options) {
33   std::string string_value =
34       base::GetFieldTrialParamValueByFeature(feature, param_name);
35   if (string_value.empty()) {
36     return default_value;
37   }
38 
39   for (const auto& option : options) {
40     if (string_value == option.name) {
41       return option.value;
42     }
43   }
44 
45   base::LogInvalidEnumValue(feature, param_name, string_value,
46                             static_cast<int>(default_value));
47   return default_value;
48 }
49 
50 }  // namespace
51 
52 constexpr int kMiracleParameterMemory512MB = 512;
53 constexpr int kMiracleParameterMemory1GB = 1024;
54 constexpr int kMiracleParameterMemory2GB = 2 * 1024;
55 constexpr int kMiracleParameterMemory4GB = 4 * 1024;
56 constexpr int kMiracleParameterMemory8GB = 8 * 1024;
57 constexpr int kMiracleParameterMemory16GB = 16 * 1024;
58 
59 // GetParamNameWithSuffix put a parameter name suffix based on
60 // the amount of physical memory.
61 //
62 // - "ForLessThan512MB" for less than 512MB memory devices.
63 // - "For512MBTo1GB" for 512MB to 1GB memory devices.
64 // - "For1GBTo2GB" for 1GB to 2GB memory devices.
65 // - "For2GBTo4GB" for 2GB to 4GB memory devices.
66 // - "For4GBTo8GB" for 4GB to 8GB memory devices.
67 // - "For8GBTo16GB" for 8GB to 16GB memory devices.
68 // - "For16GBAndAbove" for 16GB memory and above devices.
69 BASE_EXPORT
70 std::string GetParamNameWithSuffix(const std::string& param_name);
71 
72 // Provides a similar behavior with FeatureParam<std::string> except the return
73 // value is determined by the amount of physical memory.
74 BASE_EXPORT
75 std::string GetMiracleParameterAsString(const base::Feature& feature,
76                                         const std::string& param_name,
77                                         const std::string& default_value);
78 
79 // Provides a similar behavior with FeatureParam<double> except the return value
80 // is determined by the amount of physical memory.
81 BASE_EXPORT
82 double GetMiracleParameterAsDouble(const base::Feature& feature,
83                                    const std::string& param_name,
84                                    double default_value);
85 
86 // Provides a similar behavior with FeatureParam<int> except the return value is
87 // determined by the amount of physical memory.
88 BASE_EXPORT
89 int GetMiracleParameterAsInt(const base::Feature& feature,
90                              const std::string& param_name,
91                              int default_value);
92 
93 // Provides a similar behavior with FeatureParam<bool> except the return value
94 // is determined by the amount of physical memory.
95 BASE_EXPORT
96 bool GetMiracleParameterAsBool(const base::Feature& feature,
97                                const std::string& param_name,
98                                bool default_value);
99 
100 // Provides a similar behavior with FeatureParam<base::TimeDelta> except the
101 // return value is determined by the amount of physical memory.
102 BASE_EXPORT
103 base::TimeDelta GetMiracleParameterAsTimeDelta(const base::Feature& feature,
104                                                const std::string& param_name,
105                                                base::TimeDelta default_value);
106 
107 // Provides a similar behavior with FeatureParam<Enum> except the return value
108 // is determined by the amount of physical memory.
109 template <typename Enum>
GetMiracleParameterAsEnum(const base::Feature & feature,const std::string & param_name,const Enum default_value,const base::span<const typename base::FeatureParam<Enum>::Option> options)110 Enum GetMiracleParameterAsEnum(
111     const base::Feature& feature,
112     const std::string& param_name,
113     const Enum default_value,
114     const base::span<const typename base::FeatureParam<Enum>::Option> options) {
115   return GetFieldTrialParamByFeatureAsEnum(
116       feature, GetParamNameWithSuffix(param_name),
117       GetFieldTrialParamByFeatureAsEnum(feature, param_name, default_value,
118                                         options),
119       options);
120 }
121 
122 #define MIRACLE_PARAMETER_FOR_STRING(function_name, feature, param_name,    \
123                                      default_value)                         \
124   std::string function_name() {                                             \
125     static const std::string value =                                        \
126         miracle_parameter::GetMiracleParameterAsString(feature, param_name, \
127                                                        default_value);      \
128     return value;                                                           \
129   }
130 
131 #define MIRACLE_PARAMETER_FOR_DOUBLE(function_name, feature, param_name,    \
132                                      default_value)                         \
133   double function_name() {                                                  \
134     static const double value =                                             \
135         miracle_parameter::GetMiracleParameterAsDouble(feature, param_name, \
136                                                        default_value);      \
137     return value;                                                           \
138   }
139 
140 #define MIRACLE_PARAMETER_FOR_INT(function_name, feature, param_name,     \
141                                   default_value)                          \
142   int function_name() {                                                   \
143     static const int value = miracle_parameter::GetMiracleParameterAsInt( \
144         feature, param_name, default_value);                              \
145     return value;                                                         \
146   }
147 
148 #define MIRACLE_PARAMETER_FOR_BOOL(function_name, feature, param_name,      \
149                                    default_value)                           \
150   bool function_name() {                                                    \
151     static const bool value = miracle_parameter::GetMiracleParameterAsBool( \
152         feature, param_name, default_value);                                \
153     return value;                                                           \
154   }
155 
156 #define MIRACLE_PARAMETER_FOR_TIME_DELTA(function_name, feature, param_name,   \
157                                          default_value)                        \
158   base::TimeDelta function_name() {                                            \
159     static const base::TimeDelta value =                                       \
160         miracle_parameter::GetMiracleParameterAsTimeDelta(feature, param_name, \
161                                                           default_value);      \
162     return value;                                                              \
163   }
164 
165 #define MIRACLE_PARAMETER_FOR_ENUM(function_name, feature, param_name,      \
166                                    default_value, type, options)            \
167   type function_name() {                                                    \
168     static const type value = miracle_parameter::GetMiracleParameterAsEnum( \
169         feature, param_name, default_value, base::make_span(options));      \
170     return value;                                                           \
171   }
172 
173 }  // namespace miracle_parameter
174 
175 }  // namespace base
176 
177 #endif  // BASE_ALLOCATOR_MIRACLE_PARAMETER_H_
178