blob: c4d8b3c33799e671039fe33e42326332176d2c01 [file] [log] [blame]
andresp@webrtc.org60015d22014-05-16 09:39:51 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "test/field_trial.h"
andresp@webrtc.org60015d22014-05-16 09:39:51 +000012
13#include <algorithm>
14#include <cassert>
15#include <cstdio>
16#include <cstdlib>
17#include <map>
18#include <string>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "system_wrappers/include/field_trial.h"
21#include "system_wrappers/include/field_trial_default.h"
andresp@webrtc.org60015d22014-05-16 09:39:51 +000022
23namespace webrtc {
24namespace {
andresp@webrtc.org60015d22014-05-16 09:39:51 +000025bool field_trials_initiated_ = false;
andresp@webrtc.org60015d22014-05-16 09:39:51 +000026} // namespace
27
andresp@webrtc.org60015d22014-05-16 09:39:51 +000028namespace test {
Bjorn Tereliusedab3012018-01-31 17:23:40 +010029
30void ValidateFieldTrialsStringOrDie(const std::string& trials_string) {
andresp@webrtc.org60015d22014-05-16 09:39:51 +000031 static const char kPersistentStringSeparator = '/';
32
pbos19492f12015-07-07 08:22:27 -070033 // Catch an error if this is called more than once.
pbos4d9d0972015-07-09 17:21:06 -070034 assert(!field_trials_initiated_);
andresp@webrtc.org60015d22014-05-16 09:39:51 +000035 field_trials_initiated_ = true;
36
pbosbb36fdf2015-07-09 07:48:14 -070037 if (trials_string.empty())
stefanc62642c2015-07-07 04:20:34 -070038 return;
andresp@webrtc.org60015d22014-05-16 09:39:51 +000039
40 size_t next_item = 0;
phoglund37ebcf02016-01-08 05:04:57 -080041 std::map<std::string, std::string> field_trials;
andresp@webrtc.org60015d22014-05-16 09:39:51 +000042 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;
Yves Gerey665174f2018-06-19 15:03:05 +020046 size_t group_name_end =
47 trials_string.find(kPersistentStringSeparator, name_end + 1);
andresp@webrtc.org60015d22014-05-16 09:39:51 +000048 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.
phoglund37ebcf02016-01-08 05:04:57 -080056 if (field_trials.find(name) != field_trials.end() &&
57 field_trials.find(name)->second != group_name) {
andresp@webrtc.org60015d22014-05-16 09:39:51 +000058 break;
phoglund37ebcf02016-01-08 05:04:57 -080059 }
andresp@webrtc.org60015d22014-05-16 09:39:51 +000060
phoglund37ebcf02016-01-08 05:04:57 -080061 field_trials[name] = group_name;
andresp@webrtc.org60015d22014-05-16 09:39:51 +000062
63 // Successfully parsed all field trials from the string.
phoglund37ebcf02016-01-08 05:04:57 -080064 if (next_item == trials_string.length()) {
Bjorn Tereliusedab3012018-01-31 17:23:40 +010065 // webrtc::field_trial::InitFieldTrialsFromString(trials_string.c_str());
andresp@webrtc.org60015d22014-05-16 09:39:51 +000066 return;
phoglund37ebcf02016-01-08 05:04:57 -080067 }
andresp@webrtc.org60015d22014-05-16 09:39:51 +000068 }
Mirko Bonadei675513b2017-11-09 11:09:25 +010069 // Using fprintf as RTC_LOG does not print when this is called early in main.
andresp@webrtc.org60015d22014-05-16 09:39:51 +000070 fprintf(stderr, "Invalid field trials string.\n");
71
pbos4d9d0972015-07-09 17:21:06 -070072 // Using abort so it crashes in both debug and release mode.
andresp@webrtc.org60015d22014-05-16 09:39:51 +000073 abort();
74}
pbos19492f12015-07-07 08:22:27 -070075
76ScopedFieldTrials::ScopedFieldTrials(const std::string& config)
Yves Gerey665174f2018-06-19 15:03:05 +020077 : previous_field_trials_(webrtc::field_trial::GetFieldTrialString()) {
pbos19492f12015-07-07 08:22:27 -070078 assert(field_trials_initiated_);
79 field_trials_initiated_ = false;
phoglund37ebcf02016-01-08 05:04:57 -080080 current_field_trials_ = config;
Bjorn Tereliusedab3012018-01-31 17:23:40 +010081 ValidateFieldTrialsStringOrDie(current_field_trials_);
82 webrtc::field_trial::InitFieldTrialsFromString(current_field_trials_.c_str());
pbos19492f12015-07-07 08:22:27 -070083}
84
85ScopedFieldTrials::~ScopedFieldTrials() {
pbos4d9d0972015-07-09 17:21:06 -070086 // 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_);
phoglund37ebcf02016-01-08 05:04:57 -080089 webrtc::field_trial::InitFieldTrialsFromString(previous_field_trials_);
pbos19492f12015-07-07 08:22:27 -070090}
91
andresp@webrtc.org60015d22014-05-16 09:39:51 +000092} // namespace test
93} // namespace webrtc