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 | |
| 11 | #include "webrtc/test/field_trial.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <cassert> |
| 15 | #include <cstdio> |
| 16 | #include <cstdlib> |
| 17 | #include <map> |
| 18 | #include <string> |
| 19 | |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/include/field_trial.h" |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | namespace { |
stefan | bc14164 | 2015-12-14 04:31:19 -0800 | [diff] [blame] | 24 | // Clients of this library have show a clear intent to setup field trials by |
| 25 | // linking with it. As so try to crash if they forget to call |
| 26 | // InitFieldTrialsFromString before webrtc tries to access a field trial. |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 27 | bool field_trials_initiated_ = false; |
stefan | bc14164 | 2015-12-14 04:31:19 -0800 | [diff] [blame] | 28 | std::map<std::string, std::string> field_trials_; |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 29 | } // namespace |
| 30 | |
stefan | bc14164 | 2015-12-14 04:31:19 -0800 | [diff] [blame] | 31 | namespace field_trial { |
| 32 | std::string FindFullName(const std::string& trial_name) { |
| 33 | assert(field_trials_initiated_); |
| 34 | std::map<std::string, std::string>::const_iterator it = |
| 35 | field_trials_.find(trial_name); |
| 36 | if (it == field_trials_.end()) |
| 37 | return std::string(); |
| 38 | return it->second; |
| 39 | } |
| 40 | } // namespace field_trial |
| 41 | |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 42 | namespace test { |
| 43 | // Note: this code is copied from src/base/metrics/field_trial.cc since the aim |
| 44 | // is to mimic chromium --force-fieldtrials. |
| 45 | void InitFieldTrialsFromString(const std::string& trials_string) { |
| 46 | static const char kPersistentStringSeparator = '/'; |
| 47 | |
pbos | 19492f1 | 2015-07-07 08:22:27 -0700 | [diff] [blame] | 48 | // Catch an error if this is called more than once. |
pbos | 4d9d097 | 2015-07-09 17:21:06 -0700 | [diff] [blame] | 49 | assert(!field_trials_initiated_); |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 50 | field_trials_initiated_ = true; |
| 51 | |
pbos | bb36fdf | 2015-07-09 07:48:14 -0700 | [diff] [blame] | 52 | if (trials_string.empty()) |
stefan | c62642c | 2015-07-07 04:20:34 -0700 | [diff] [blame] | 53 | return; |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 54 | |
| 55 | size_t next_item = 0; |
| 56 | while (next_item < trials_string.length()) { |
| 57 | size_t name_end = trials_string.find(kPersistentStringSeparator, next_item); |
| 58 | if (name_end == trials_string.npos || next_item == name_end) |
| 59 | break; |
| 60 | size_t group_name_end = trials_string.find(kPersistentStringSeparator, |
| 61 | name_end + 1); |
| 62 | if (group_name_end == trials_string.npos || name_end + 1 == group_name_end) |
| 63 | break; |
| 64 | std::string name(trials_string, next_item, name_end - next_item); |
| 65 | std::string group_name(trials_string, name_end + 1, |
| 66 | group_name_end - name_end - 1); |
| 67 | next_item = group_name_end + 1; |
| 68 | |
| 69 | // Fail if duplicate with different group name. |
stefan | bc14164 | 2015-12-14 04:31:19 -0800 | [diff] [blame] | 70 | if (field_trials_.find(name) != field_trials_.end() && |
| 71 | field_trials_.find(name)->second != group_name) |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 72 | break; |
| 73 | |
stefan | bc14164 | 2015-12-14 04:31:19 -0800 | [diff] [blame] | 74 | field_trials_[name] = group_name; |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 75 | |
| 76 | // Successfully parsed all field trials from the string. |
stefan | bc14164 | 2015-12-14 04:31:19 -0800 | [diff] [blame] | 77 | if (next_item == trials_string.length()) |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 78 | return; |
| 79 | } |
pbos | 4d9d097 | 2015-07-09 17:21:06 -0700 | [diff] [blame] | 80 | // Using fprintf as LOG does not print when this is called early in main. |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 81 | fprintf(stderr, "Invalid field trials string.\n"); |
| 82 | |
pbos | 4d9d097 | 2015-07-09 17:21:06 -0700 | [diff] [blame] | 83 | // Using abort so it crashes in both debug and release mode. |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 84 | abort(); |
| 85 | } |
pbos | 19492f1 | 2015-07-07 08:22:27 -0700 | [diff] [blame] | 86 | |
| 87 | ScopedFieldTrials::ScopedFieldTrials(const std::string& config) |
stefan | bc14164 | 2015-12-14 04:31:19 -0800 | [diff] [blame] | 88 | : previous_field_trials_(field_trials_) { |
pbos | 19492f1 | 2015-07-07 08:22:27 -0700 | [diff] [blame] | 89 | assert(field_trials_initiated_); |
| 90 | field_trials_initiated_ = false; |
stefan | bc14164 | 2015-12-14 04:31:19 -0800 | [diff] [blame] | 91 | field_trials_.clear(); |
| 92 | InitFieldTrialsFromString(config); |
pbos | 19492f1 | 2015-07-07 08:22:27 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | ScopedFieldTrials::~ScopedFieldTrials() { |
pbos | 4d9d097 | 2015-07-09 17:21:06 -0700 | [diff] [blame] | 96 | // Should still be initialized, since InitFieldTrials is called from ctor. |
| 97 | // That's why we don't restore the flag. |
| 98 | assert(field_trials_initiated_); |
stefan | bc14164 | 2015-12-14 04:31:19 -0800 | [diff] [blame] | 99 | field_trials_ = previous_field_trials_; |
pbos | 19492f1 | 2015-07-07 08:22:27 -0700 | [diff] [blame] | 100 | } |
| 101 | |
andresp@webrtc.org | 60015d2 | 2014-05-16 09:39:51 +0000 | [diff] [blame] | 102 | } // namespace test |
| 103 | } // namespace webrtc |