Debian Codegraph @

Xpjproject/third_party/webrtc_aec3/src/rtc_base/experiments/field_trial_parser.cc

raw symbols SCIP
1 /*
2 * Copyright 2019 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10 #include "rtc_base/experiments/field_trial_parser.h"
11
12 #include <inttypes.h>
13
14 #include <algorithm>
15 #include <map>
16 #include <type_traits>
17 #include <utility>
18
19 #include "rtc_base/checks.h"
20 #include "rtc_base/logging.h"
21 #include "rtc_base/numerics/safe_conversions.h"
22
23 namespace webrtc {
24 namespace {
25
26 int FindOrEnd(std::string str, size_t start, char delimiter) {
27 size_t pos = str.find(delimiter, start);
28 pos = (pos == std::string::npos) ? str.length() : pos;
29 return static_cast<int>(pos);
30 }
31 } // namespace
32
33 FieldTrialParameterInterface::FieldTrialParameterInterface(std::string key)
34 : key_(key) {}
35 FieldTrialParameterInterface::~FieldTrialParameterInterface() {
36 RTC_DCHECK(used_) << "Field trial parameter with key: '" << key_
37 << "' never used.";
38 }
39
40 void ParseFieldTrial(
41 std::initializer_list<FieldTrialParameterInterface*> fields,
42 std::string trial_string) {
43 std::map<std::string, FieldTrialParameterInterface*> field_map;
44 FieldTrialParameterInterface* keyless_field = nullptr;
45 for (FieldTrialParameterInterface* field : fields) {
46 field->MarkAsUsed();
47 if (!field->sub_parameters_.empty()) {
48 for (FieldTrialParameterInterface* sub_field : field->sub_parameters_) {
49 RTC_DCHECK(!sub_field->key_.empty());
50 sub_field->MarkAsUsed();
51 field_map[sub_field->key_] = sub_field;
52 }
53 continue;
54 }
55
56 if (field->key_.empty()) {
57 RTC_DCHECK(!keyless_field);
58 keyless_field = field;
59 } else {
60 field_map[field->key_] = field;
61 }
62 }
63
64 size_t i = 0;
65 while (i < trial_string.length()) {
66 int val_end = FindOrEnd(trial_string, i, ',');
67 int colon_pos = FindOrEnd(trial_string, i, ':');
68 int key_end = std::min(val_end, colon_pos);
69 int val_begin = key_end + 1;
70 std::string key = trial_string.substr(i, key_end - i);
71 absl::optional<std::string> opt_value;
72 if (val_end >= val_begin)
73 opt_value = trial_string.substr(val_begin, val_end - val_begin);
74 i = val_end + 1;
75 auto field = field_map.find(key);
76 if (field != field_map.end()) {
77 if (!field->second->Parse(std::move(opt_value))) {
78 RTC_LOG(LS_WARNING) << "Failed to read field with key: '" << key
79 << "' in trial: \"" << trial_string << "\"";
80 }
81 } else if (!opt_value && keyless_field && !key.empty()) {
82 if (!keyless_field->Parse(key)) {
83 RTC_LOG(LS_WARNING) << "Failed to read empty key field with value '"
84 << key << "' in trial: \"" << trial_string << "\"";
85 }
86 } else if (key.empty() || key[0] != '_') {
87 // "_" is be used to prefix keys that are part of the string for
88 // debugging purposes but not neccessarily used.
89 // e.g. WebRTC-Experiment/param: value, _DebuggingString
90 RTC_LOG(LS_INFO) << "No field with key: '" << key
91 << "' (found in trial: \"" << trial_string << "\")";
92 std::string valid_keys;
93 for (const auto& f : field_map) {
94 valid_keys += f.first;
95 valid_keys += ", ";
96 }
97 RTC_LOG(LS_INFO) << "Valid keys are: " << valid_keys;
98 }
99 }
100
101 for (FieldTrialParameterInterface* field : fields) {
102 field->ParseDone();
103 }
104 }
105
106 template <>
107 absl::optional<bool> ParseTypedParameter<bool>(std::string str) {
108 if (str == "true" || str == "1") {
109 return true;
110 } else if (str == "false" || str == "0") {
111 return false;
112 }
113 return absl::nullopt;
114 }
115
116 template <>
117 absl::optional<double> ParseTypedParameter<double>(std::string str) {
118 double value;
119 char unit[2]{0, 0};
120 if (sscanf(str.c_str(), "%lf%1s", &value, unit) >= 1) {
121 if (unit[0] == '%')
122 return value / 100;
123 return value;
124 } else {
125 return absl::nullopt;
126 }
127 }
128
129 template <>
130 absl::optional<int> ParseTypedParameter<int>(std::string str) {
131 int64_t value;
132 if (sscanf(str.c_str(), "%" SCNd64, &value) == 1) {
133 if (rtc::IsValueInRangeForNumericType<int, int64_t>(value)) {
134 return static_cast<int>(value);
135 }
136 }
137 return absl::nullopt;
138 }
139
140 template <>
141 absl::optional<unsigned> ParseTypedParameter<unsigned>(std::string str) {
142 int64_t value;
143 if (sscanf(str.c_str(), "%" SCNd64, &value) == 1) {
144 if (rtc::IsValueInRangeForNumericType<unsigned, int64_t>(value)) {
145 return static_cast<unsigned>(value);
146 }
147 }
148 return absl::nullopt;
149 }
150
151 template <>
152 absl::optional<std::string> ParseTypedParameter<std::string>(std::string str) {
153 return std::move(str);
154 }
155
156 template <>
157 absl::optional<absl::optional<bool>> ParseTypedParameter<absl::optional<bool>>(
158 std::string str) {
159 return ParseOptionalParameter<bool>(str);
160 }
161 template <>
162 absl::optional<absl::optional<int>> ParseTypedParameter<absl::optional<int>>(
163 std::string str) {
164 return ParseOptionalParameter<int>(str);
165 }
166 template <>
167 absl::optional<absl::optional<unsigned>>
168 ParseTypedParameter<absl::optional<unsigned>>(std::string str) {
169 return ParseOptionalParameter<unsigned>(str);
170 }
171 template <>
172 absl::optional<absl::optional<double>>
173 ParseTypedParameter<absl::optional<double>>(std::string str) {
174 return ParseOptionalParameter<double>(str);
175 }
176
177 FieldTrialFlag::FieldTrialFlag(std::string key) : FieldTrialFlag(key, false) {}
178
179 FieldTrialFlag::FieldTrialFlag(std::string key, bool default_value)
180 : FieldTrialParameterInterface(key), value_(default_value) {}
181
182 bool FieldTrialFlag::Get() const {
183 return value_;
184 }
185
186 webrtc::FieldTrialFlag::operator bool() const {
187 return value_;
188 }
189
190 bool FieldTrialFlag::Parse(absl::optional<std::string> str_value) {
191 // Only set the flag if there is no argument provided.
192 if (str_value) {
193 absl::optional<bool> opt_value = ParseTypedParameter<bool>(*str_value);
194 if (!opt_value)
195 return false;
196 value_ = *opt_value;
197 } else {
198 value_ = true;
199 }
200 return true;
201 }
202
203 AbstractFieldTrialEnum::AbstractFieldTrialEnum(
204 std::string key,
205 int default_value,
206 std::map<std::string, int> mapping)
207 : FieldTrialParameterInterface(key),
208 value_(default_value),
209 enum_mapping_(mapping) {
210 for (auto& key_val : enum_mapping_)
211 valid_values_.insert(key_val.second);
212 }
213 AbstractFieldTrialEnum::AbstractFieldTrialEnum(const AbstractFieldTrialEnum&) =
214 default;
215 AbstractFieldTrialEnum::~AbstractFieldTrialEnum() = default;
216
217 bool AbstractFieldTrialEnum::Parse(absl::optional<std::string> str_value) {
218 if (str_value) {
219 auto it = enum_mapping_.find(*str_value);
220 if (it != enum_mapping_.end()) {
221 value_ = it->second;
222 return true;
223 }
224 absl::optional<int> value = ParseTypedParameter<int>(*str_value);
225 if (value.has_value() &&
226 (valid_values_.find(*value) != valid_values_.end())) {
227 value_ = *value;
228 return true;
229 }
230 }
231 return false;
232 }
233
234 template class FieldTrialParameter<bool>;
235 template class FieldTrialParameter<double>;
236 template class FieldTrialParameter<int>;
237 template class FieldTrialParameter<unsigned>;
238 template class FieldTrialParameter<std::string>;
239
240 template class FieldTrialConstrained<double>;
241 template class FieldTrialConstrained<int>;
242 template class FieldTrialConstrained<unsigned>;
243
244 template class FieldTrialOptional<double>;
245 template class FieldTrialOptional<int>;
246 template class FieldTrialOptional<unsigned>;
247 template class FieldTrialOptional<bool>;
248 template class FieldTrialOptional<std::string>;
249
250 } // namespace webrtc