blob: 2fb5da458ba9d6654cbe757f25ab185d7753a326 [file] [log] [blame]
andresp@webrtc.org60015d22014-05-16 09:39:511/*
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 04:47:3111#include "test/field_trial.h"
andresp@webrtc.org60015d22014-05-16 09:39:5112
13#include <algorithm>
14#include <cassert>
15#include <cstdio>
16#include <cstdlib>
17#include <map>
18#include <string>
19
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "system_wrappers/include/field_trial.h"
andresp@webrtc.org60015d22014-05-16 09:39:5121
22namespace webrtc {
23namespace {
andresp@webrtc.org60015d22014-05-16 09:39:5124bool field_trials_initiated_ = false;
andresp@webrtc.org60015d22014-05-16 09:39:5125} // namespace
26
andresp@webrtc.org60015d22014-05-16 09:39:5127namespace test {
Bjorn Tereliusedab3012018-01-31 16:23:4028
29void ValidateFieldTrialsStringOrDie(const std::string& trials_string) {
andresp@webrtc.org60015d22014-05-16 09:39:5130 static const char kPersistentStringSeparator = '/';
31
pbos19492f12015-07-07 15:22:2732 // Catch an error if this is called more than once.
pbos4d9d0972015-07-10 00:21:0633 assert(!field_trials_initiated_);
andresp@webrtc.org60015d22014-05-16 09:39:5134 field_trials_initiated_ = true;
35
pbosbb36fdf2015-07-09 14:48:1436 if (trials_string.empty())
stefanc62642c2015-07-07 11:20:3437 return;
andresp@webrtc.org60015d22014-05-16 09:39:5138
39 size_t next_item = 0;
phoglund37ebcf02016-01-08 13:04:5740 std::map<std::string, std::string> field_trials;
andresp@webrtc.org60015d22014-05-16 09:39:5141 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 Gerey665174f2018-06-19 13:03:0545 size_t group_name_end =
46 trials_string.find(kPersistentStringSeparator, name_end + 1);
andresp@webrtc.org60015d22014-05-16 09:39:5147 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.
phoglund37ebcf02016-01-08 13:04:5755 if (field_trials.find(name) != field_trials.end() &&
56 field_trials.find(name)->second != group_name) {
andresp@webrtc.org60015d22014-05-16 09:39:5157 break;
phoglund37ebcf02016-01-08 13:04:5758 }
andresp@webrtc.org60015d22014-05-16 09:39:5159
phoglund37ebcf02016-01-08 13:04:5760 field_trials[name] = group_name;
andresp@webrtc.org60015d22014-05-16 09:39:5161
62 // Successfully parsed all field trials from the string.
phoglund37ebcf02016-01-08 13:04:5763 if (next_item == trials_string.length()) {
Bjorn Tereliusedab3012018-01-31 16:23:4064 // webrtc::field_trial::InitFieldTrialsFromString(trials_string.c_str());
andresp@webrtc.org60015d22014-05-16 09:39:5165 return;
phoglund37ebcf02016-01-08 13:04:5766 }
andresp@webrtc.org60015d22014-05-16 09:39:5167 }
Mirko Bonadei675513b2017-11-09 10:09:2568 // Using fprintf as RTC_LOG does not print when this is called early in main.
andresp@webrtc.org60015d22014-05-16 09:39:5169 fprintf(stderr, "Invalid field trials string.\n");
70
pbos4d9d0972015-07-10 00:21:0671 // Using abort so it crashes in both debug and release mode.
andresp@webrtc.org60015d22014-05-16 09:39:5172 abort();
73}
pbos19492f12015-07-07 15:22:2774
75ScopedFieldTrials::ScopedFieldTrials(const std::string& config)
Yves Gerey665174f2018-06-19 13:03:0576 : previous_field_trials_(webrtc::field_trial::GetFieldTrialString()) {
pbos19492f12015-07-07 15:22:2777 assert(field_trials_initiated_);
78 field_trials_initiated_ = false;
phoglund37ebcf02016-01-08 13:04:5779 current_field_trials_ = config;
Bjorn Tereliusedab3012018-01-31 16:23:4080 ValidateFieldTrialsStringOrDie(current_field_trials_);
81 webrtc::field_trial::InitFieldTrialsFromString(current_field_trials_.c_str());
pbos19492f12015-07-07 15:22:2782}
83
84ScopedFieldTrials::~ScopedFieldTrials() {
pbos4d9d0972015-07-10 00:21:0685 // 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_);
phoglund37ebcf02016-01-08 13:04:5788 webrtc::field_trial::InitFieldTrialsFromString(previous_field_trials_);
pbos19492f12015-07-07 15:22:2789}
90
andresp@webrtc.org60015d22014-05-16 09:39:5191} // namespace test
92} // namespace webrtc