andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "test/field_trial.h" |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <cassert> |
| 15 | #include <cstdio> |
| 16 | #include <cstdlib> |
| 17 | #include <map> |
| 18 | #include <string> |
| 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "system_wrappers/include/field_trial.h" |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 23 | namespace test { |
Sebastian Jansson | 87a92d0 | 2019-04-29 13:55:07 +0200 | [diff] [blame] | 24 | namespace { |
Bjorn Terelius | edab301 | 2018-01-31 17:23:40 +0100 | [diff] [blame] | 25 | |
Sebastian Jansson | 87a92d0 | 2019-04-29 13:55:07 +0200 | [diff] [blame] | 26 | void InnerValidateFieldTrialsStringOrDie(const std::string& trials_string) { |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 27 | static const char kPersistentStringSeparator = '/'; |
| 28 | |
pbos | bb36fdf | 2015-07-09 07:48:14 -0700 | [diff] [blame] | 29 | if (trials_string.empty()) |
stefan | c62642c | 2015-07-07 04:20:34 -0700 | [diff] [blame] | 30 | return; |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 31 | |
| 32 | size_t next_item = 0; |
phoglund | 37ebcf0 | 2016-01-08 05:04:57 -0800 | [diff] [blame] | 33 | std::map<std::string, std::string> field_trials; |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 34 | while (next_item < trials_string.length()) { |
| 35 | size_t name_end = trials_string.find(kPersistentStringSeparator, next_item); |
| 36 | if (name_end == trials_string.npos || next_item == name_end) |
| 37 | break; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 38 | size_t group_name_end = |
| 39 | trials_string.find(kPersistentStringSeparator, name_end + 1); |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 40 | if (group_name_end == trials_string.npos || name_end + 1 == group_name_end) |
| 41 | break; |
| 42 | std::string name(trials_string, next_item, name_end - next_item); |
| 43 | std::string group_name(trials_string, name_end + 1, |
| 44 | group_name_end - name_end - 1); |
| 45 | next_item = group_name_end + 1; |
| 46 | |
| 47 | // Fail if duplicate with different group name. |
phoglund | 37ebcf0 | 2016-01-08 05:04:57 -0800 | [diff] [blame] | 48 | if (field_trials.find(name) != field_trials.end() && |
| 49 | field_trials.find(name)->second != group_name) { |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 50 | break; |
phoglund | 37ebcf0 | 2016-01-08 05:04:57 -0800 | [diff] [blame] | 51 | } |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 52 | |
phoglund | 37ebcf0 | 2016-01-08 05:04:57 -0800 | [diff] [blame] | 53 | field_trials[name] = group_name; |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 54 | |
| 55 | // Successfully parsed all field trials from the string. |
phoglund | 37ebcf0 | 2016-01-08 05:04:57 -0800 | [diff] [blame] | 56 | if (next_item == trials_string.length()) { |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 57 | return; |
phoglund | 37ebcf0 | 2016-01-08 05:04:57 -0800 | [diff] [blame] | 58 | } |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 59 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 60 | // Using fprintf as RTC_LOG does not print when this is called early in main. |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 61 | fprintf(stderr, "Invalid field trials string.\n"); |
| 62 | |
pbos | 4d9d097 | 2015-07-09 17:21:06 -0700 | [diff] [blame] | 63 | // Using abort so it crashes in both debug and release mode. |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 64 | abort(); |
| 65 | } |
Sebastian Jansson | 87a92d0 | 2019-04-29 13:55:07 +0200 | [diff] [blame] | 66 | } // namespace |
| 67 | |
| 68 | void ValidateFieldTrialsStringOrDie(const std::string& trials_string) { |
| 69 | static bool field_trials_initiated_ = false; |
| 70 | // Catch an error if this is called more than once. |
| 71 | assert(!field_trials_initiated_); |
| 72 | field_trials_initiated_ = true; |
| 73 | InnerValidateFieldTrialsStringOrDie(trials_string); |
| 74 | } |
pbos | 19492f1 | 2015-07-07 08:22:27 -0700 | [diff] [blame] | 75 | |
| 76 | ScopedFieldTrials::ScopedFieldTrials(const std::string& config) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 77 | : previous_field_trials_(webrtc::field_trial::GetFieldTrialString()) { |
phoglund | 37ebcf0 | 2016-01-08 05:04:57 -0800 | [diff] [blame] | 78 | current_field_trials_ = config; |
Sebastian Jansson | 87a92d0 | 2019-04-29 13:55:07 +0200 | [diff] [blame] | 79 | InnerValidateFieldTrialsStringOrDie(current_field_trials_); |
Bjorn Terelius | edab301 | 2018-01-31 17:23:40 +0100 | [diff] [blame] | 80 | webrtc::field_trial::InitFieldTrialsFromString(current_field_trials_.c_str()); |
pbos | 19492f1 | 2015-07-07 08:22:27 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | ScopedFieldTrials::~ScopedFieldTrials() { |
phoglund | 37ebcf0 | 2016-01-08 05:04:57 -0800 | [diff] [blame] | 84 | webrtc::field_trial::InitFieldTrialsFromString(previous_field_trials_); |
pbos | 19492f1 | 2015-07-07 08:22:27 -0700 | [diff] [blame] | 85 | } |
| 86 | |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 87 | } // namespace test |
| 88 | } // namespace webrtc |