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