blob: 48d0bd3858798e0c87f1f76e6db7f5578cabac2e [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"
andresp@webrtc.org60015d22014-05-16 09:39:51 +000021
22namespace webrtc {
andresp@webrtc.org60015d22014-05-16 09:39:51 +000023namespace test {
Sebastian Jansson87a92d02019-04-29 13:55:07 +020024namespace {
Bjorn Tereliusedab3012018-01-31 17:23:40 +010025
Sebastian Jansson87a92d02019-04-29 13:55:07 +020026void InnerValidateFieldTrialsStringOrDie(const std::string& trials_string) {
andresp@webrtc.org60015d22014-05-16 09:39:51 +000027 static const char kPersistentStringSeparator = '/';
28
pbosbb36fdf2015-07-09 07:48:14 -070029 if (trials_string.empty())
stefanc62642c2015-07-07 04:20:34 -070030 return;
andresp@webrtc.org60015d22014-05-16 09:39:51 +000031
32 size_t next_item = 0;
phoglund37ebcf02016-01-08 05:04:57 -080033 std::map<std::string, std::string> field_trials;
andresp@webrtc.org60015d22014-05-16 09:39:51 +000034 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 Gerey665174f2018-06-19 15:03:05 +020038 size_t group_name_end =
39 trials_string.find(kPersistentStringSeparator, name_end + 1);
andresp@webrtc.org60015d22014-05-16 09:39:51 +000040 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.
phoglund37ebcf02016-01-08 05:04:57 -080048 if (field_trials.find(name) != field_trials.end() &&
49 field_trials.find(name)->second != group_name) {
andresp@webrtc.org60015d22014-05-16 09:39:51 +000050 break;
phoglund37ebcf02016-01-08 05:04:57 -080051 }
andresp@webrtc.org60015d22014-05-16 09:39:51 +000052
phoglund37ebcf02016-01-08 05:04:57 -080053 field_trials[name] = group_name;
andresp@webrtc.org60015d22014-05-16 09:39:51 +000054
55 // Successfully parsed all field trials from the string.
phoglund37ebcf02016-01-08 05:04:57 -080056 if (next_item == trials_string.length()) {
andresp@webrtc.org60015d22014-05-16 09:39:51 +000057 return;
phoglund37ebcf02016-01-08 05:04:57 -080058 }
andresp@webrtc.org60015d22014-05-16 09:39:51 +000059 }
Mirko Bonadei675513b2017-11-09 11:09:25 +010060 // Using fprintf as RTC_LOG does not print when this is called early in main.
andresp@webrtc.org60015d22014-05-16 09:39:51 +000061 fprintf(stderr, "Invalid field trials string.\n");
62
pbos4d9d0972015-07-09 17:21:06 -070063 // Using abort so it crashes in both debug and release mode.
andresp@webrtc.org60015d22014-05-16 09:39:51 +000064 abort();
65}
Sebastian Jansson87a92d02019-04-29 13:55:07 +020066} // namespace
67
68void 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}
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()) {
phoglund37ebcf02016-01-08 05:04:57 -080078 current_field_trials_ = config;
Sebastian Jansson87a92d02019-04-29 13:55:07 +020079 InnerValidateFieldTrialsStringOrDie(current_field_trials_);
Bjorn Tereliusedab3012018-01-31 17:23:40 +010080 webrtc::field_trial::InitFieldTrialsFromString(current_field_trials_.c_str());
pbos19492f12015-07-07 08:22:27 -070081}
82
83ScopedFieldTrials::~ScopedFieldTrials() {
phoglund37ebcf02016-01-08 05:04:57 -080084 webrtc::field_trial::InitFieldTrialsFromString(previous_field_trials_);
pbos19492f12015-07-07 08:22:27 -070085}
86
andresp@webrtc.org60015d22014-05-16 09:39:51 +000087} // namespace test
88} // namespace webrtc