| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/metrics/field_trial.h" | 5 #include "base/metrics/field_trial.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/utf_string_conversions.h" |
| 10 | 11 |
| 11 namespace base { | 12 namespace base { |
| 12 | 13 |
| 13 // static | 14 // static |
| 14 const int FieldTrial::kNotParticipating = -1; | 15 const int FieldTrial::kNotFinalized = -1; |
| 15 | 16 |
| 16 // static | 17 // static |
| 17 const int FieldTrial::kAllRemainingProbability = -2; | 18 const int FieldTrial::kDefaultGroupNumber = 0; |
| 18 | 19 |
| 19 // static | 20 // static |
| 20 bool FieldTrial::enable_benchmarking_ = false; | 21 bool FieldTrial::enable_benchmarking_ = false; |
| 21 | 22 |
| 22 // static | 23 // static |
| 23 const char FieldTrialList::kPersistentStringSeparator('/'); | 24 const char FieldTrialList::kPersistentStringSeparator('/'); |
| 24 | 25 |
| 25 static const char kHistogramFieldTrialSeparator('_'); | 26 static const char kHistogramFieldTrialSeparator('_'); |
| 26 | 27 |
| 27 //------------------------------------------------------------------------------ | 28 //------------------------------------------------------------------------------ |
| 28 // FieldTrial methods and members. | 29 // FieldTrial methods and members. |
| 29 | 30 |
| 30 FieldTrial::FieldTrial(const std::string& name, | 31 FieldTrial::FieldTrial(const std::string& name, |
| 31 const Probability total_probability) | 32 const Probability total_probability, |
| 33 const std::string& default_group_name, |
| 34 const int year, |
| 35 const int month, |
| 36 const int day_of_month) |
| 32 : name_(name), | 37 : name_(name), |
| 33 divisor_(total_probability), | 38 divisor_(total_probability), |
| 39 default_group_name_(default_group_name), |
| 34 random_(static_cast<Probability>(divisor_ * base::RandDouble())), | 40 random_(static_cast<Probability>(divisor_ * base::RandDouble())), |
| 35 accumulated_group_probability_(0), | 41 accumulated_group_probability_(0), |
| 36 next_group_number_(0), | 42 next_group_number_(kDefaultGroupNumber+1), |
| 37 group_(kNotParticipating) { | 43 group_(kNotFinalized) { |
| 44 DCHECK(!default_group_name_.empty()); |
| 38 FieldTrialList::Register(this); | 45 FieldTrialList::Register(this); |
| 46 |
| 47 DCHECK_GT(year, 1970); |
| 48 DCHECK_GT(month, 0); |
| 49 DCHECK_LT(month, 13); |
| 50 DCHECK_GT(day_of_month, 0); |
| 51 DCHECK_LT(day_of_month, 32); |
| 52 |
| 53 base::Time::Exploded exploded; |
| 54 exploded.year = year; |
| 55 exploded.month = month; |
| 56 exploded.day_of_week = 0; // Should be unusued. |
| 57 exploded.day_of_month = day_of_month; |
| 58 exploded.hour = 0; |
| 59 exploded.minute = 0; |
| 60 exploded.second = 0; |
| 61 exploded.millisecond = 0; |
| 62 |
| 63 base::Time expiration_time = Time::FromLocalExploded(exploded); |
| 64 disable_field_trial_ = (GetBuildTime() > expiration_time) ? true : false; |
| 39 } | 65 } |
| 40 | 66 |
| 41 int FieldTrial::AppendGroup(const std::string& name, | 67 int FieldTrial::AppendGroup(const std::string& name, |
| 42 Probability group_probability) { | 68 Probability group_probability) { |
| 43 DCHECK(group_probability <= divisor_); | 69 DCHECK(group_probability <= divisor_); |
| 44 DCHECK(group_probability >=0 || | 70 DCHECK_GE(group_probability, 0); |
| 45 group_probability == kAllRemainingProbability); | 71 |
| 46 if (group_probability == kAllRemainingProbability) { | 72 if (enable_benchmarking_ || disable_field_trial_) |
| 47 accumulated_group_probability_ = divisor_; | 73 group_probability = 0; |
| 48 } else { | 74 |
| 49 if (enable_benchmarking_) | 75 accumulated_group_probability_ += group_probability; |
| 50 group_probability = 0; | 76 |
| 51 accumulated_group_probability_ += group_probability; | |
| 52 } | |
| 53 DCHECK(accumulated_group_probability_ <= divisor_); | 77 DCHECK(accumulated_group_probability_ <= divisor_); |
| 54 if (group_ == kNotParticipating && accumulated_group_probability_ > random_) { | 78 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) { |
| 55 // This is the group that crossed the random line, so we do the assignment. | 79 // This is the group that crossed the random line, so we do the assignment. |
| 56 group_ = next_group_number_; | 80 group_ = next_group_number_; |
| 57 if (name.empty()) | 81 if (name.empty()) |
| 58 base::StringAppendF(&group_name_, "%d", group_); | 82 base::StringAppendF(&group_name_, "%d", group_); |
| 59 else | 83 else |
| 60 group_name_ = name; | 84 group_name_ = name; |
| 61 } | 85 } |
| 62 return next_group_number_++; | 86 return next_group_number_++; |
| 63 } | 87 } |
| 64 | 88 |
| 89 int FieldTrial::group() { |
| 90 if (group_ == kNotFinalized) { |
| 91 accumulated_group_probability_ = divisor_; |
| 92 group_ = kDefaultGroupNumber; |
| 93 group_name_ = default_group_name_; |
| 94 } |
| 95 return group_; |
| 96 } |
| 97 |
| 98 std::string FieldTrial::group_name() { |
| 99 group(); // call group() to make group assignment was done. |
| 100 return group_name_; |
| 101 } |
| 102 |
| 65 // static | 103 // static |
| 66 std::string FieldTrial::MakeName(const std::string& name_prefix, | 104 std::string FieldTrial::MakeName(const std::string& name_prefix, |
| 67 const std::string& trial_name) { | 105 const std::string& trial_name) { |
| 68 std::string big_string(name_prefix); | 106 std::string big_string(name_prefix); |
| 69 big_string.append(1, kHistogramFieldTrialSeparator); | 107 big_string.append(1, kHistogramFieldTrialSeparator); |
| 70 return big_string.append(FieldTrialList::FindFullName(trial_name)); | 108 return big_string.append(FieldTrialList::FindFullName(trial_name)); |
| 71 } | 109 } |
| 72 | 110 |
| 73 // static | 111 // static |
| 74 void FieldTrial::EnableBenchmarking() { | 112 void FieldTrial::EnableBenchmarking() { |
| 75 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); | 113 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); |
| 76 enable_benchmarking_ = true; | 114 enable_benchmarking_ = true; |
| 77 } | 115 } |
| 78 | 116 |
| 117 // static |
| 118 Time FieldTrial::GetBuildTime() { |
| 119 Time integral_build_time; |
| 120 const char* kDateTime = __DATE__ " " __TIME__; |
| 121 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), |
| 122 &integral_build_time); |
| 123 DCHECK(result); |
| 124 return integral_build_time; |
| 125 } |
| 126 |
| 79 FieldTrial::~FieldTrial() {} | 127 FieldTrial::~FieldTrial() {} |
| 80 | 128 |
| 81 //------------------------------------------------------------------------------ | 129 //------------------------------------------------------------------------------ |
| 82 // FieldTrialList methods and members. | 130 // FieldTrialList methods and members. |
| 83 | 131 |
| 84 // static | 132 // static |
| 85 FieldTrialList* FieldTrialList::global_ = NULL; | 133 FieldTrialList* FieldTrialList::global_ = NULL; |
| 86 | 134 |
| 87 // static | 135 // static |
| 88 bool FieldTrialList::register_without_global_ = false; | 136 bool FieldTrialList::register_without_global_ = false; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 return NULL; | 170 return NULL; |
| 123 AutoLock auto_lock(global_->lock_); | 171 AutoLock auto_lock(global_->lock_); |
| 124 return global_->PreLockedFind(name); | 172 return global_->PreLockedFind(name); |
| 125 } | 173 } |
| 126 | 174 |
| 127 // static | 175 // static |
| 128 int FieldTrialList::FindValue(const std::string& name) { | 176 int FieldTrialList::FindValue(const std::string& name) { |
| 129 FieldTrial* field_trial = Find(name); | 177 FieldTrial* field_trial = Find(name); |
| 130 if (field_trial) | 178 if (field_trial) |
| 131 return field_trial->group(); | 179 return field_trial->group(); |
| 132 return FieldTrial::kNotParticipating; | 180 return FieldTrial::kNotFinalized; |
| 133 } | 181 } |
| 134 | 182 |
| 135 // static | 183 // static |
| 136 std::string FieldTrialList::FindFullName(const std::string& name) { | 184 std::string FieldTrialList::FindFullName(const std::string& name) { |
| 137 FieldTrial* field_trial = Find(name); | 185 FieldTrial* field_trial = Find(name); |
| 138 if (field_trial) | 186 if (field_trial) |
| 139 return field_trial->group_name(); | 187 return field_trial->group_name(); |
| 140 return ""; | 188 return ""; |
| 141 } | 189 } |
| 142 | 190 |
| 143 // static | 191 // static |
| 144 void FieldTrialList::StatesToString(std::string* output) { | 192 void FieldTrialList::StatesToString(std::string* output) { |
| 145 if (!global_) | 193 if (!global_) |
| 146 return; | 194 return; |
| 147 DCHECK(output->empty()); | 195 DCHECK(output->empty()); |
| 148 AutoLock auto_lock(global_->lock_); | 196 AutoLock auto_lock(global_->lock_); |
| 149 for (RegistrationList::iterator it = global_->registered_.begin(); | 197 for (RegistrationList::iterator it = global_->registered_.begin(); |
| 150 it != global_->registered_.end(); ++it) { | 198 it != global_->registered_.end(); ++it) { |
| 151 const std::string name = it->first; | 199 const std::string name = it->first; |
| 152 const std::string group_name = it->second->group_name(); | 200 std::string group_name = it->second->group_name_internal(); |
| 153 if (group_name.empty()) | 201 if (group_name.empty()) |
| 154 continue; // No definitive winner in this trial. | 202 // No definitive winner in this trial, use default_group_name as the |
| 203 // group_name. |
| 204 group_name = it->second->default_group_name(); |
| 155 DCHECK_EQ(name.find(kPersistentStringSeparator), std::string::npos); | 205 DCHECK_EQ(name.find(kPersistentStringSeparator), std::string::npos); |
| 156 DCHECK_EQ(group_name.find(kPersistentStringSeparator), std::string::npos); | 206 DCHECK_EQ(group_name.find(kPersistentStringSeparator), std::string::npos); |
| 157 output->append(name); | 207 output->append(name); |
| 158 output->append(1, kPersistentStringSeparator); | 208 output->append(1, kPersistentStringSeparator); |
| 159 output->append(group_name); | 209 output->append(group_name); |
| 160 output->append(1, kPersistentStringSeparator); | 210 output->append(1, kPersistentStringSeparator); |
| 161 } | 211 } |
| 162 } | 212 } |
| 163 | 213 |
| 164 // static | 214 // static |
| 165 bool FieldTrialList::StringAugmentsState(const std::string& prior_state) { | 215 bool FieldTrialList::CreateTrialsInChildProcess( |
| 216 const std::string& parent_trials) { |
| 166 DCHECK(global_); | 217 DCHECK(global_); |
| 167 if (prior_state.empty() || !global_) | 218 if (parent_trials.empty() || !global_) |
| 168 return true; | 219 return true; |
| 169 | 220 |
| 221 Time::Exploded exploded; |
| 222 Time two_years_from_now = |
| 223 Time::NowFromSystemTime() + TimeDelta::FromDays(730); |
| 224 two_years_from_now.LocalExplode(&exploded); |
| 225 const int kTwoYearsFromNow = exploded.year; |
| 226 |
| 170 size_t next_item = 0; | 227 size_t next_item = 0; |
| 171 while (next_item < prior_state.length()) { | 228 while (next_item < parent_trials.length()) { |
| 172 size_t name_end = prior_state.find(kPersistentStringSeparator, next_item); | 229 size_t name_end = parent_trials.find(kPersistentStringSeparator, next_item); |
| 173 if (name_end == prior_state.npos || next_item == name_end) | 230 if (name_end == parent_trials.npos || next_item == name_end) |
| 174 return false; | 231 return false; |
| 175 size_t group_name_end = prior_state.find(kPersistentStringSeparator, | 232 size_t group_name_end = parent_trials.find(kPersistentStringSeparator, |
| 176 name_end + 1); | 233 name_end + 1); |
| 177 if (group_name_end == prior_state.npos || name_end + 1 == group_name_end) | 234 if (group_name_end == parent_trials.npos || name_end + 1 == group_name_end) |
| 178 return false; | 235 return false; |
| 179 std::string name(prior_state, next_item, name_end - next_item); | 236 std::string name(parent_trials, next_item, name_end - next_item); |
| 180 std::string group_name(prior_state, name_end + 1, | 237 std::string group_name(parent_trials, name_end + 1, |
| 181 group_name_end - name_end - 1); | 238 group_name_end - name_end - 1); |
| 182 next_item = group_name_end + 1; | 239 next_item = group_name_end + 1; |
| 183 | 240 |
| 184 FieldTrial *field_trial(FieldTrialList::Find(name)); | 241 FieldTrial *field_trial(FieldTrialList::Find(name)); |
| 185 if (field_trial) { | 242 if (field_trial) { |
| 186 // In single process mode, we may have already created the field trial. | 243 // In single process mode, we may have already created the field trial. |
| 187 if (field_trial->group_name() != group_name) | 244 if ((field_trial->group_name_internal() != group_name) && |
| 245 (field_trial->default_group_name() != group_name)) |
| 188 return false; | 246 return false; |
| 189 continue; | 247 continue; |
| 190 } | 248 } |
| 191 const int kTotalProbability = 100; | 249 const int kTotalProbability = 100; |
| 192 field_trial = new FieldTrial(name, kTotalProbability); | 250 field_trial = new FieldTrial(name, kTotalProbability, group_name, |
| 251 kTwoYearsFromNow, 1, 1); |
| 193 field_trial->AppendGroup(group_name, kTotalProbability); | 252 field_trial->AppendGroup(group_name, kTotalProbability); |
| 194 } | 253 } |
| 195 return true; | 254 return true; |
| 196 } | 255 } |
| 197 | 256 |
| 198 // static | 257 // static |
| 199 size_t FieldTrialList::GetFieldTrialCount() { | 258 size_t FieldTrialList::GetFieldTrialCount() { |
| 200 if (!global_) | 259 if (!global_) |
| 201 return 0; | 260 return 0; |
| 202 AutoLock auto_lock(global_->lock_); | 261 AutoLock auto_lock(global_->lock_); |
| 203 return global_->registered_.size(); | 262 return global_->registered_.size(); |
| 204 } | 263 } |
| 205 | 264 |
| 206 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { | 265 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { |
| 207 RegistrationList::iterator it = registered_.find(name); | 266 RegistrationList::iterator it = registered_.find(name); |
| 208 if (registered_.end() == it) | 267 if (registered_.end() == it) |
| 209 return NULL; | 268 return NULL; |
| 210 return it->second; | 269 return it->second; |
| 211 } | 270 } |
| 212 | 271 |
| 213 } // namespace base | 272 } // namespace base |
| OLD | NEW |