andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 +0000 | [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 12:26:07 +0100 | [diff] [blame] | 10 | #include "system_wrappers/include/field_trial.h" |
andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 +0000 | [diff] [blame] | 11 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 12 | #include <stddef.h> |
Jonas Olsson | 5b2eda4 | 2019-06-11 14:29:40 +0200 | [diff] [blame] | 13 | |
| 14 | #include <map> |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 +0000 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Jonas Olsson | 5b2eda4 | 2019-06-11 14:29:40 +0200 | [diff] [blame] | 17 | #include "absl/strings/string_view.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/logging.h" |
| 20 | |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 +0000 | [diff] [blame] | 21 | // Simple field trial implementation, which allows client to |
| 22 | // specify desired flags in InitFieldTrialsFromString. |
andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 +0000 | [diff] [blame] | 23 | namespace webrtc { |
| 24 | namespace field_trial { |
| 25 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 26 | static const char* trials_init_string = NULL; |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 +0000 | [diff] [blame] | 27 | |
Mirko Bonadei | 92e0038 | 2018-09-15 10:37:11 +0200 | [diff] [blame] | 28 | #ifndef WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT |
Jonas Olsson | 5b2eda4 | 2019-06-11 14:29:40 +0200 | [diff] [blame] | 29 | namespace { |
| 30 | constexpr char kPersistentStringSeparator = '/'; |
| 31 | // Validates the given field trial string. |
| 32 | // E.g.: |
| 33 | // "WebRTC-experimentFoo/Enabled/WebRTC-experimentBar/Enabled100kbps/" |
| 34 | // Assigns the process to group "Enabled" on WebRTCExperimentFoo trial |
| 35 | // and to group "Enabled100kbps" on WebRTCExperimentBar. |
| 36 | // |
| 37 | // E.g. invalid config: |
| 38 | // "WebRTC-experiment1/Enabled" (note missing / separator at the end). |
| 39 | bool FieldTrialsStringIsValid(const absl::string_view trials) { |
| 40 | if (trials.empty()) |
| 41 | return true; |
| 42 | |
| 43 | size_t next_item = 0; |
| 44 | std::map<absl::string_view, absl::string_view> field_trials; |
| 45 | while (next_item < trials.length()) { |
| 46 | size_t name_end = trials.find(kPersistentStringSeparator, next_item); |
| 47 | if (name_end == trials.npos || next_item == name_end) |
| 48 | return false; |
| 49 | size_t group_name_end = |
| 50 | trials.find(kPersistentStringSeparator, name_end + 1); |
| 51 | if (group_name_end == trials.npos || name_end + 1 == group_name_end) |
| 52 | return false; |
| 53 | absl::string_view name = trials.substr(next_item, name_end - next_item); |
| 54 | absl::string_view group_name = |
| 55 | trials.substr(name_end + 1, group_name_end - name_end - 1); |
| 56 | |
| 57 | next_item = group_name_end + 1; |
| 58 | |
| 59 | // Fail if duplicate with different group name. |
| 60 | if (field_trials.find(name) != field_trials.end() && |
| 61 | field_trials.find(name)->second != group_name) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | field_trials[name] = group_name; |
| 66 | } |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | } // namespace |
| 71 | |
andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 +0000 | [diff] [blame] | 72 | std::string FindFullName(const std::string& name) { |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 +0000 | [diff] [blame] | 73 | if (trials_init_string == NULL) |
| 74 | return std::string(); |
| 75 | |
| 76 | std::string trials_string(trials_init_string); |
| 77 | if (trials_string.empty()) |
| 78 | return std::string(); |
| 79 | |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 +0000 | [diff] [blame] | 80 | size_t next_item = 0; |
| 81 | while (next_item < trials_string.length()) { |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 +0000 | [diff] [blame] | 82 | // Find next name/value pair in field trial configuration string. |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 83 | size_t field_name_end = |
| 84 | trials_string.find(kPersistentStringSeparator, next_item); |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 +0000 | [diff] [blame] | 85 | if (field_name_end == trials_string.npos || field_name_end == next_item) |
| 86 | break; |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 87 | size_t field_value_end = |
| 88 | trials_string.find(kPersistentStringSeparator, field_name_end + 1); |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 +0000 | [diff] [blame] | 89 | if (field_value_end == trials_string.npos || |
| 90 | field_value_end == field_name_end + 1) |
| 91 | break; |
| 92 | std::string field_name(trials_string, next_item, |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 93 | field_name_end - next_item); |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 +0000 | [diff] [blame] | 94 | std::string field_value(trials_string, field_name_end + 1, |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 95 | field_value_end - field_name_end - 1); |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 +0000 | [diff] [blame] | 96 | next_item = field_value_end + 1; |
| 97 | |
| 98 | if (name == field_name) |
| 99 | return field_value; |
| 100 | } |
andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 +0000 | [diff] [blame] | 101 | return std::string(); |
| 102 | } |
Mirko Bonadei | 92e0038 | 2018-09-15 10:37:11 +0200 | [diff] [blame] | 103 | #endif // WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT |
andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 +0000 | [diff] [blame] | 104 | |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 +0000 | [diff] [blame] | 105 | // Optionally initialize field trial from a string. |
| 106 | void InitFieldTrialsFromString(const char* trials_string) { |
Jonas Olsson | 5b2eda4 | 2019-06-11 14:29:40 +0200 | [diff] [blame] | 107 | RTC_LOG(LS_INFO) << "Setting field trial string:" << trials_string; |
| 108 | #ifndef WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT |
| 109 | if (trials_string) { |
| 110 | RTC_DCHECK(FieldTrialsStringIsValid(trials_string)) |
| 111 | << "Invalid field trials string:" << trials_string; |
| 112 | }; |
| 113 | #endif // WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT |
glaznev@webrtc.org | 669bc7e | 2015-02-09 18:17:46 +0000 | [diff] [blame] | 114 | trials_init_string = trials_string; |
| 115 | } |
| 116 | |
phoglund | 37ebcf0 | 2016-01-08 05:04:57 -0800 | [diff] [blame] | 117 | const char* GetFieldTrialString() { |
| 118 | return trials_init_string; |
| 119 | } |
| 120 | |
andresp@webrtc.org | a36ad69 | 2014-05-14 12:24:04 +0000 | [diff] [blame] | 121 | } // namespace field_trial |
| 122 | } // namespace webrtc |