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