andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 | [diff] [blame] | 1 | // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 2 | // |
| 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the LICENSE file in the root of the source |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
| 8 | // |
| 9 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 10 | #include "system_wrappers/include/field_trial.h" |
andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 | [diff] [blame] | 11 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 12 | #include <stddef.h> |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 13 | |
| 14 | #include <map> |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 15 | #include <string> |
Emil Lundmark | 6bf20cc | 2022-09-21 13:20:22 | [diff] [blame] | 16 | #include <utility> |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 17 | |
Emil Lundmark | 6bf20cc | 2022-09-21 13:20:22 | [diff] [blame] | 18 | #include "absl/algorithm/container.h" |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 19 | #include "absl/strings/string_view.h" |
Emil Lundmark | 6bf20cc | 2022-09-21 13:20:22 | [diff] [blame] | 20 | #include "experiments/registered_field_trials.h" |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Emil Lundmark | 6bf20cc | 2022-09-21 13:20:22 | [diff] [blame] | 22 | #include "rtc_base/containers/flat_set.h" |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 23 | #include "rtc_base/logging.h" |
Konrad Hofbauer | ee1e015 | 2019-12-05 15:25:40 | [diff] [blame] | 24 | #include "rtc_base/string_encode.h" |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 25 | |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 26 | // Simple field trial implementation, which allows client to |
| 27 | // specify desired flags in InitFieldTrialsFromString. |
andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 | [diff] [blame] | 28 | namespace webrtc { |
| 29 | namespace field_trial { |
| 30 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 31 | static const char* trials_init_string = NULL; |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 32 | |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 33 | namespace { |
Emil Lundmark | 6bf20cc | 2022-09-21 13:20:22 | [diff] [blame] | 34 | |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 35 | constexpr char kPersistentStringSeparator = '/'; |
Emil Lundmark | 6bf20cc | 2022-09-21 13:20:22 | [diff] [blame] | 36 | |
| 37 | flat_set<std::string>& TestKeys() { |
| 38 | static auto* test_keys = new flat_set<std::string>(); |
| 39 | return *test_keys; |
| 40 | } |
| 41 | |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 42 | // Validates the given field trial string. |
| 43 | // E.g.: |
| 44 | // "WebRTC-experimentFoo/Enabled/WebRTC-experimentBar/Enabled100kbps/" |
| 45 | // Assigns the process to group "Enabled" on WebRTCExperimentFoo trial |
| 46 | // and to group "Enabled100kbps" on WebRTCExperimentBar. |
| 47 | // |
| 48 | // E.g. invalid config: |
| 49 | // "WebRTC-experiment1/Enabled" (note missing / separator at the end). |
Konrad Hofbauer | ee1e015 | 2019-12-05 15:25:40 | [diff] [blame] | 50 | bool FieldTrialsStringIsValidInternal(const absl::string_view trials) { |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 51 | if (trials.empty()) |
| 52 | return true; |
| 53 | |
| 54 | size_t next_item = 0; |
| 55 | std::map<absl::string_view, absl::string_view> field_trials; |
| 56 | while (next_item < trials.length()) { |
| 57 | size_t name_end = trials.find(kPersistentStringSeparator, next_item); |
| 58 | if (name_end == trials.npos || next_item == name_end) |
| 59 | return false; |
| 60 | size_t group_name_end = |
| 61 | trials.find(kPersistentStringSeparator, name_end + 1); |
| 62 | if (group_name_end == trials.npos || name_end + 1 == group_name_end) |
| 63 | return false; |
| 64 | absl::string_view name = trials.substr(next_item, name_end - next_item); |
| 65 | absl::string_view group_name = |
| 66 | trials.substr(name_end + 1, group_name_end - name_end - 1); |
| 67 | |
| 68 | next_item = group_name_end + 1; |
| 69 | |
| 70 | // Fail if duplicate with different group name. |
| 71 | if (field_trials.find(name) != field_trials.end() && |
| 72 | field_trials.find(name)->second != group_name) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | field_trials[name] = group_name; |
| 77 | } |
| 78 | |
| 79 | return true; |
| 80 | } |
Emil Lundmark | 6bf20cc | 2022-09-21 13:20:22 | [diff] [blame] | 81 | |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 82 | } // namespace |
| 83 | |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 84 | bool FieldTrialsStringIsValid(absl::string_view trials_string) { |
Konrad Hofbauer | ee1e015 | 2019-12-05 15:25:40 | [diff] [blame] | 85 | return FieldTrialsStringIsValidInternal(trials_string); |
| 86 | } |
| 87 | |
| 88 | void InsertOrReplaceFieldTrialStringsInMap( |
| 89 | std::map<std::string, std::string>* fieldtrial_map, |
| 90 | const absl::string_view trials_string) { |
| 91 | if (FieldTrialsStringIsValidInternal(trials_string)) { |
Niels Möller | f1d822b | 2022-06-07 11:58:27 | [diff] [blame] | 92 | std::vector<absl::string_view> tokens = rtc::split(trials_string, '/'); |
Konrad Hofbauer | ee1e015 | 2019-12-05 15:25:40 | [diff] [blame] | 93 | // Skip last token which is empty due to trailing '/'. |
| 94 | for (size_t idx = 0; idx < tokens.size() - 1; idx += 2) { |
Niels Möller | f1d822b | 2022-06-07 11:58:27 | [diff] [blame] | 95 | (*fieldtrial_map)[std::string(tokens[idx])] = |
| 96 | std::string(tokens[idx + 1]); |
Konrad Hofbauer | ee1e015 | 2019-12-05 15:25:40 | [diff] [blame] | 97 | } |
| 98 | } else { |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 99 | RTC_DCHECK_NOTREACHED() << "Invalid field trials string:" << trials_string; |
Konrad Hofbauer | ee1e015 | 2019-12-05 15:25:40 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Ali Tofigh | 969c1356 | 2022-05-13 08:26:58 | [diff] [blame] | 103 | std::string MergeFieldTrialsStrings(absl::string_view first, |
| 104 | absl::string_view second) { |
Konrad Hofbauer | ee1e015 | 2019-12-05 15:25:40 | [diff] [blame] | 105 | std::map<std::string, std::string> fieldtrial_map; |
| 106 | InsertOrReplaceFieldTrialStringsInMap(&fieldtrial_map, first); |
| 107 | InsertOrReplaceFieldTrialStringsInMap(&fieldtrial_map, second); |
| 108 | |
| 109 | // Merge into fieldtrial string. |
| 110 | std::string merged = ""; |
| 111 | for (auto const& fieldtrial : fieldtrial_map) { |
| 112 | merged += fieldtrial.first + '/' + fieldtrial.second + '/'; |
| 113 | } |
| 114 | return merged; |
| 115 | } |
| 116 | |
Mirko Bonadei | 83240ac | 2022-03-14 13:48:17 | [diff] [blame] | 117 | #ifndef WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT |
Ali Tofigh | 04c8816 | 2022-03-21 13:47:35 | [diff] [blame] | 118 | std::string FindFullName(absl::string_view name) { |
Emil Lundmark | 9109e85 | 2023-02-28 12:57:01 | [diff] [blame] | 119 | #if WEBRTC_STRICT_FIELD_TRIALS == 1 |
Emil Lundmark | 6bf20cc | 2022-09-21 13:20:22 | [diff] [blame] | 120 | RTC_DCHECK(absl::c_linear_search(kRegisteredFieldTrials, name) || |
| 121 | TestKeys().contains(name)) |
Emil Lundmark | 9109e85 | 2023-02-28 12:57:01 | [diff] [blame] | 122 | << name << " is not registered, see g3doc/field-trials.md."; |
| 123 | #elif WEBRTC_STRICT_FIELD_TRIALS == 2 |
| 124 | RTC_LOG_IF(LS_WARNING, |
| 125 | !(absl::c_linear_search(kRegisteredFieldTrials, name) || |
| 126 | TestKeys().contains(name))) |
| 127 | << name << " is not registered, see g3doc/field-trials.md."; |
Emil Lundmark | 6bf20cc | 2022-09-21 13:20:22 | [diff] [blame] | 128 | #endif |
| 129 | |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 130 | if (trials_init_string == NULL) |
| 131 | return std::string(); |
| 132 | |
Ali Tofigh | 04c8816 | 2022-03-21 13:47:35 | [diff] [blame] | 133 | absl::string_view trials_string(trials_init_string); |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 134 | if (trials_string.empty()) |
| 135 | return std::string(); |
| 136 | |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 137 | size_t next_item = 0; |
| 138 | while (next_item < trials_string.length()) { |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 139 | // Find next name/value pair in field trial configuration string. |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 140 | size_t field_name_end = |
| 141 | trials_string.find(kPersistentStringSeparator, next_item); |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 142 | if (field_name_end == trials_string.npos || field_name_end == next_item) |
| 143 | break; |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 144 | size_t field_value_end = |
| 145 | trials_string.find(kPersistentStringSeparator, field_name_end + 1); |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 146 | if (field_value_end == trials_string.npos || |
| 147 | field_value_end == field_name_end + 1) |
| 148 | break; |
Ali Tofigh | 04c8816 | 2022-03-21 13:47:35 | [diff] [blame] | 149 | absl::string_view field_name = |
| 150 | trials_string.substr(next_item, field_name_end - next_item); |
| 151 | absl::string_view field_value = trials_string.substr( |
| 152 | field_name_end + 1, field_value_end - field_name_end - 1); |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 153 | next_item = field_value_end + 1; |
| 154 | |
| 155 | if (name == field_name) |
Ali Tofigh | 04c8816 | 2022-03-21 13:47:35 | [diff] [blame] | 156 | return std::string(field_value); |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 157 | } |
andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 | [diff] [blame] | 158 | return std::string(); |
| 159 | } |
Mirko Bonadei | 92e0038 | 2018-09-15 08:37:11 | [diff] [blame] | 160 | #endif // WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT |
andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 | [diff] [blame] | 161 | |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 162 | // Optionally initialize field trial from a string. |
| 163 | void InitFieldTrialsFromString(const char* trials_string) { |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 164 | RTC_LOG(LS_INFO) << "Setting field trial string:" << trials_string; |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 165 | if (trials_string) { |
Konrad Hofbauer | ee1e015 | 2019-12-05 15:25:40 | [diff] [blame] | 166 | RTC_DCHECK(FieldTrialsStringIsValidInternal(trials_string)) |
Jonas Olsson | 5b2eda4 | 2019-06-11 12:29:40 | [diff] [blame] | 167 | << "Invalid field trials string:" << trials_string; |
| 168 | }; |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 | [diff] [blame] | 169 | trials_init_string = trials_string; |
| 170 | } |
| 171 | |
phoglund | 37ebcf0 | 2016-01-08 13:04:57 | [diff] [blame] | 172 | const char* GetFieldTrialString() { |
| 173 | return trials_init_string; |
| 174 | } |
| 175 | |
Emil Lundmark | 2d7a3e7 | 2022-11-25 09:19:12 | [diff] [blame] | 176 | FieldTrialsAllowedInScopeForTesting::FieldTrialsAllowedInScopeForTesting( |
Emil Lundmark | 6bf20cc | 2022-09-21 13:20:22 | [diff] [blame] | 177 | flat_set<std::string> keys) { |
| 178 | TestKeys() = std::move(keys); |
| 179 | } |
| 180 | |
Emil Lundmark | 2d7a3e7 | 2022-11-25 09:19:12 | [diff] [blame] | 181 | FieldTrialsAllowedInScopeForTesting::~FieldTrialsAllowedInScopeForTesting() { |
Emil Lundmark | 6bf20cc | 2022-09-21 13:20:22 | [diff] [blame] | 182 | TestKeys().clear(); |
| 183 | } |
| 184 | |
andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 | [diff] [blame] | 185 | } // namespace field_trial |
| 186 | } // namespace webrtc |