| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/build_time.h" | 7 #include "base/build_time.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/sha1.h" | 10 #include "base/sha1.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 | 16 |
| 17 // static | 17 static const char kHistogramFieldTrialSeparator('_'); |
| 18 |
| 19 // statics |
| 18 const int FieldTrial::kNotFinalized = -1; | 20 const int FieldTrial::kNotFinalized = -1; |
| 19 | |
| 20 // static | |
| 21 const int FieldTrial::kDefaultGroupNumber = 0; | 21 const int FieldTrial::kDefaultGroupNumber = 0; |
| 22 | 22 const uint32 FieldTrial::kReservedHashValue = 0; |
| 23 // static | |
| 24 bool FieldTrial::enable_benchmarking_ = false; | 23 bool FieldTrial::enable_benchmarking_ = false; |
| 25 | 24 |
| 26 // static | |
| 27 const char FieldTrialList::kPersistentStringSeparator('/'); | 25 const char FieldTrialList::kPersistentStringSeparator('/'); |
| 28 | |
| 29 static const char kHistogramFieldTrialSeparator('_'); | |
| 30 | |
| 31 // static | |
| 32 int FieldTrialList::kExpirationYearInFuture = 0; | 26 int FieldTrialList::kExpirationYearInFuture = 0; |
| 33 | 27 |
| 34 | |
| 35 //------------------------------------------------------------------------------ | 28 //------------------------------------------------------------------------------ |
| 36 // FieldTrial methods and members. | 29 // FieldTrial methods and members. |
| 37 | 30 |
| 38 FieldTrial::FieldTrial(const std::string& name, | 31 FieldTrial::FieldTrial(const std::string& name, |
| 39 const Probability total_probability, | 32 const Probability total_probability, |
| 40 const std::string& default_group_name, | 33 const std::string& default_group_name, |
| 41 const int year, | 34 const int year, |
| 42 const int month, | 35 const int month, |
| 43 const int day_of_month) | 36 const int day_of_month) |
| 44 : name_(name), | 37 : name_(name), |
| 38 name_hash_(HashName(name)), |
| 45 divisor_(total_probability), | 39 divisor_(total_probability), |
| 46 default_group_name_(default_group_name), | 40 default_group_name_(default_group_name), |
| 47 random_(static_cast<Probability>(divisor_ * RandDouble())), | 41 random_(static_cast<Probability>(divisor_ * RandDouble())), |
| 48 accumulated_group_probability_(0), | 42 accumulated_group_probability_(0), |
| 49 next_group_number_(kDefaultGroupNumber + 1), | 43 next_group_number_(kDefaultGroupNumber + 1), |
| 50 group_(kNotFinalized), | 44 group_(kNotFinalized), |
| 45 group_name_hash_(kReservedHashValue), |
| 51 enable_field_trial_(true) { | 46 enable_field_trial_(true) { |
| 52 DCHECK_GT(total_probability, 0); | 47 DCHECK_GT(total_probability, 0); |
| 53 DCHECK(!name_.empty()); | 48 DCHECK(!name_.empty()); |
| 54 DCHECK(!default_group_name_.empty()); | 49 DCHECK(!default_group_name_.empty()); |
| 55 FieldTrialList::Register(this); | 50 FieldTrialList::Register(this); |
| 56 | 51 |
| 57 DCHECK_GT(year, 1970); | 52 DCHECK_GT(year, 1970); |
| 58 DCHECK_GT(month, 0); | 53 DCHECK_GT(month, 0); |
| 59 DCHECK_LT(month, 13); | 54 DCHECK_LT(month, 13); |
| 60 DCHECK_GT(day_of_month, 0); | 55 DCHECK_GT(day_of_month, 0); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 89 } | 84 } |
| 90 | 85 |
| 91 void FieldTrial::Disable() { | 86 void FieldTrial::Disable() { |
| 92 enable_field_trial_ = false; | 87 enable_field_trial_ = false; |
| 93 | 88 |
| 94 // In case we are disabled after initialization, we need to switch | 89 // In case we are disabled after initialization, we need to switch |
| 95 // the trial to the default group. | 90 // the trial to the default group. |
| 96 if (group_ != kNotFinalized) { | 91 if (group_ != kNotFinalized) { |
| 97 group_ = kDefaultGroupNumber; | 92 group_ = kDefaultGroupNumber; |
| 98 group_name_ = default_group_name_; | 93 group_name_ = default_group_name_; |
| 94 group_name_hash_ = HashName(group_name_); |
| 99 } | 95 } |
| 100 } | 96 } |
| 101 | 97 |
| 102 int FieldTrial::AppendGroup(const std::string& name, | 98 int FieldTrial::AppendGroup(const std::string& name, |
| 103 Probability group_probability) { | 99 Probability group_probability) { |
| 104 DCHECK_LE(group_probability, divisor_); | 100 DCHECK_LE(group_probability, divisor_); |
| 105 DCHECK_GE(group_probability, 0); | 101 DCHECK_GE(group_probability, 0); |
| 106 | 102 |
| 107 if (enable_benchmarking_ || !enable_field_trial_) | 103 if (enable_benchmarking_ || !enable_field_trial_) |
| 108 group_probability = 0; | 104 group_probability = 0; |
| 109 | 105 |
| 110 accumulated_group_probability_ += group_probability; | 106 accumulated_group_probability_ += group_probability; |
| 111 | 107 |
| 112 DCHECK_LE(accumulated_group_probability_, divisor_); | 108 DCHECK_LE(accumulated_group_probability_, divisor_); |
| 113 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) { | 109 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) { |
| 114 // This is the group that crossed the random line, so we do the assignment. | 110 // This is the group that crossed the random line, so we do the assignment. |
| 115 group_ = next_group_number_; | 111 group_ = next_group_number_; |
| 116 if (name.empty()) | 112 if (name.empty()) |
| 117 StringAppendF(&group_name_, "%d", group_); | 113 StringAppendF(&group_name_, "%d", group_); |
| 118 else | 114 else |
| 119 group_name_ = name; | 115 group_name_ = name; |
| 116 group_name_hash_ = HashName(group_name_); |
| 120 FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_); | 117 FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_); |
| 121 } | 118 } |
| 122 return next_group_number_++; | 119 return next_group_number_++; |
| 123 } | 120 } |
| 124 | 121 |
| 125 int FieldTrial::group() { | 122 int FieldTrial::group() { |
| 126 if (group_ == kNotFinalized) { | 123 if (group_ == kNotFinalized) { |
| 127 accumulated_group_probability_ = divisor_; | 124 accumulated_group_probability_ = divisor_; |
| 128 group_ = kDefaultGroupNumber; | 125 group_ = kDefaultGroupNumber; |
| 129 group_name_ = default_group_name_; | 126 group_name_ = default_group_name_; |
| 127 group_name_hash_ = HashName(group_name_); |
| 130 FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_); | 128 FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_); |
| 131 } | 129 } |
| 132 return group_; | 130 return group_; |
| 133 } | 131 } |
| 134 | 132 |
| 135 std::string FieldTrial::group_name() { | 133 std::string FieldTrial::group_name() { |
| 136 group(); // call group() to make sure group assignment was done. | 134 group(); // call group() to make sure group assignment was done. |
| 137 DCHECK(!group_name_.empty()); | 135 DCHECK(!group_name_.empty()); |
| 138 return group_name_; | 136 return group_name_; |
| 139 } | 137 } |
| 140 | 138 |
| 139 bool FieldTrial::GetNameGroupId(NameGroupId* name_group_id) { |
| 140 if (group_name_hash_ == kReservedHashValue) |
| 141 return false; |
| 142 name_group_id->name = name_hash_; |
| 143 name_group_id->group = group_name_hash_; |
| 144 return true; |
| 145 } |
| 146 |
| 141 // static | 147 // static |
| 142 std::string FieldTrial::MakeName(const std::string& name_prefix, | 148 std::string FieldTrial::MakeName(const std::string& name_prefix, |
| 143 const std::string& trial_name) { | 149 const std::string& trial_name) { |
| 144 std::string big_string(name_prefix); | 150 std::string big_string(name_prefix); |
| 145 big_string.append(1, kHistogramFieldTrialSeparator); | 151 big_string.append(1, kHistogramFieldTrialSeparator); |
| 146 return big_string.append(FieldTrialList::FindFullName(trial_name)); | 152 return big_string.append(FieldTrialList::FindFullName(trial_name)); |
| 147 } | 153 } |
| 148 | 154 |
| 149 // static | 155 // static |
| 150 void FieldTrial::EnableBenchmarking() { | 156 void FieldTrial::EnableBenchmarking() { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 166 SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), | 172 SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), |
| 167 input.size(), | 173 input.size(), |
| 168 sha1_hash); | 174 sha1_hash); |
| 169 | 175 |
| 170 COMPILE_ASSERT(sizeof(uint64) < sizeof(sha1_hash), need_more_data); | 176 COMPILE_ASSERT(sizeof(uint64) < sizeof(sha1_hash), need_more_data); |
| 171 uint64* bits = reinterpret_cast<uint64*>(&sha1_hash[0]); | 177 uint64* bits = reinterpret_cast<uint64*>(&sha1_hash[0]); |
| 172 | 178 |
| 173 return BitsToOpenEndedUnitInterval(*bits); | 179 return BitsToOpenEndedUnitInterval(*bits); |
| 174 } | 180 } |
| 175 | 181 |
| 182 // static |
| 183 uint32 FieldTrial::HashName(const std::string& name) { |
| 184 // SHA-1 is designed to produce a uniformly random spread in its output space, |
| 185 // even for nearly-identical inputs. |
| 186 unsigned char sha1_hash[kSHA1Length]; |
| 187 SHA1HashBytes(reinterpret_cast<const unsigned char*>(name.c_str()), |
| 188 name.size(), |
| 189 sha1_hash); |
| 190 |
| 191 COMPILE_ASSERT(sizeof(uint32) < sizeof(sha1_hash), need_more_data); |
| 192 uint32* bits = reinterpret_cast<uint32*>(&sha1_hash[0]); |
| 193 |
| 194 // We only DCHECK, since this should not happen because the registration |
| 195 // of the experiment on the server should have already warn the developer. |
| 196 // If this ever happen, we'll ignore this experiment/group when reporting. |
| 197 DCHECK(*bits != kReservedHashValue); |
| 198 return *bits; |
| 199 } |
| 200 |
| 176 //------------------------------------------------------------------------------ | 201 //------------------------------------------------------------------------------ |
| 177 // FieldTrialList methods and members. | 202 // FieldTrialList methods and members. |
| 178 | 203 |
| 179 // static | 204 // static |
| 180 FieldTrialList* FieldTrialList::global_ = NULL; | 205 FieldTrialList* FieldTrialList::global_ = NULL; |
| 181 | 206 |
| 182 // static | 207 // static |
| 183 bool FieldTrialList::used_without_global_ = false; | 208 bool FieldTrialList::used_without_global_ = false; |
| 184 | 209 |
| 185 FieldTrialList::FieldTrialList(const std::string& client_id) | 210 FieldTrialList::FieldTrialList(const std::string& client_id) |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 return ""; | 269 return ""; |
| 245 } | 270 } |
| 246 | 271 |
| 247 // static | 272 // static |
| 248 bool FieldTrialList::TrialExists(const std::string& name) { | 273 bool FieldTrialList::TrialExists(const std::string& name) { |
| 249 return Find(name) != NULL; | 274 return Find(name) != NULL; |
| 250 } | 275 } |
| 251 | 276 |
| 252 // static | 277 // static |
| 253 void FieldTrialList::StatesToString(std::string* output) { | 278 void FieldTrialList::StatesToString(std::string* output) { |
| 279 DCHECK(output->empty()); |
| 254 if (!global_) | 280 if (!global_) |
| 255 return; | 281 return; |
| 256 DCHECK(output->empty()); | |
| 257 AutoLock auto_lock(global_->lock_); | 282 AutoLock auto_lock(global_->lock_); |
| 258 | 283 |
| 259 for (RegistrationList::iterator it = global_->registered_.begin(); | 284 for (RegistrationList::iterator it = global_->registered_.begin(); |
| 260 it != global_->registered_.end(); ++it) { | 285 it != global_->registered_.end(); ++it) { |
| 261 const std::string name = it->first; | 286 const std::string name = it->first; |
| 262 std::string group_name = it->second->group_name_internal(); | 287 std::string group_name = it->second->group_name_internal(); |
| 263 if (group_name.empty()) | 288 if (group_name.empty()) |
| 264 continue; // Should not include uninitialized trials. | 289 continue; // Should not include uninitialized trials. |
| 265 DCHECK_EQ(name.find(kPersistentStringSeparator), std::string::npos); | 290 DCHECK_EQ(name.find(kPersistentStringSeparator), std::string::npos); |
| 266 DCHECK_EQ(group_name.find(kPersistentStringSeparator), std::string::npos); | 291 DCHECK_EQ(group_name.find(kPersistentStringSeparator), std::string::npos); |
| 267 output->append(name); | 292 output->append(name); |
| 268 output->append(1, kPersistentStringSeparator); | 293 output->append(1, kPersistentStringSeparator); |
| 269 output->append(group_name); | 294 output->append(group_name); |
| 270 output->append(1, kPersistentStringSeparator); | 295 output->append(1, kPersistentStringSeparator); |
| 271 } | 296 } |
| 272 } | 297 } |
| 273 | 298 |
| 274 // static | 299 // static |
| 300 void FieldTrialList::GetFieldTrialNameGroupIds( |
| 301 std::vector<FieldTrial::NameGroupId>* name_group_ids) { |
| 302 DCHECK(name_group_ids->empty()); |
| 303 if (!global_) |
| 304 return; |
| 305 AutoLock auto_lock(global_->lock_); |
| 306 |
| 307 for (RegistrationList::iterator it = global_->registered_.begin(); |
| 308 it != global_->registered_.end(); ++it) { |
| 309 FieldTrial::NameGroupId name_group_id; |
| 310 if (it->second->GetNameGroupId(&name_group_id)) |
| 311 name_group_ids->push_back(name_group_id); |
| 312 } |
| 313 } |
| 314 |
| 315 // static |
| 275 bool FieldTrialList::CreateTrialsInChildProcess( | 316 bool FieldTrialList::CreateTrialsInChildProcess( |
| 276 const std::string& parent_trials) { | 317 const std::string& parent_trials) { |
| 277 DCHECK(global_); | 318 DCHECK(global_); |
| 278 if (parent_trials.empty() || !global_) | 319 if (parent_trials.empty() || !global_) |
| 279 return true; | 320 return true; |
| 280 | 321 |
| 281 size_t next_item = 0; | 322 size_t next_item = 0; |
| 282 while (next_item < parent_trials.length()) { | 323 while (next_item < parent_trials.length()) { |
| 283 size_t name_end = parent_trials.find(kPersistentStringSeparator, next_item); | 324 size_t name_end = parent_trials.find(kPersistentStringSeparator, next_item); |
| 284 if (name_end == parent_trials.npos || next_item == name_end) | 325 if (name_end == parent_trials.npos || next_item == name_end) |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 } | 419 } |
| 379 | 420 |
| 380 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { | 421 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { |
| 381 RegistrationList::iterator it = registered_.find(name); | 422 RegistrationList::iterator it = registered_.find(name); |
| 382 if (registered_.end() == it) | 423 if (registered_.end() == it) |
| 383 return NULL; | 424 return NULL; |
| 384 return it->second; | 425 return it->second; |
| 385 } | 426 } |
| 386 | 427 |
| 387 } // namespace base | 428 } // namespace base |
| OLD | NEW |