| 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" |
| 11 |
| 12 using base::Time; |
| 13 using base::TimeDelta; |
| 10 | 14 |
| 11 namespace base { | 15 namespace base { |
| 12 | 16 |
| 13 // static | 17 // static |
| 14 const int FieldTrial::kNotParticipating = -1; | 18 const int FieldTrial::kNotParticipating = -1; |
| 15 | 19 |
| 16 // static | 20 // static |
| 17 const int FieldTrial::kAllRemainingProbability = -2; | 21 const int FieldTrial::kAllRemainingProbability = -2; |
| 18 | 22 |
| 19 // static | 23 // static |
| 20 bool FieldTrial::enable_benchmarking_ = false; | 24 bool FieldTrial::enable_benchmarking_ = false; |
| 21 | 25 |
| 22 // static | 26 // static |
| 23 const char FieldTrialList::kPersistentStringSeparator('/'); | 27 const char FieldTrialList::kPersistentStringSeparator('/'); |
| 24 | 28 |
| 25 static const char kHistogramFieldTrialSeparator('_'); | 29 static const char kHistogramFieldTrialSeparator('_'); |
| 26 | 30 |
| 27 //------------------------------------------------------------------------------ | 31 //------------------------------------------------------------------------------ |
| 28 // FieldTrial methods and members. | 32 // FieldTrial methods and members. |
| 29 | 33 |
| 30 FieldTrial::FieldTrial(const std::string& name, | 34 FieldTrial::FieldTrial(const std::string& name, |
| 31 const Probability total_probability) | 35 const Probability total_probability) |
| 32 : name_(name), | 36 : name_(name), |
| 33 divisor_(total_probability), | 37 divisor_(total_probability), |
| 34 random_(static_cast<Probability>(divisor_ * base::RandDouble())), | 38 random_(static_cast<Probability>(divisor_ * base::RandDouble())), |
| 35 accumulated_group_probability_(0), | 39 accumulated_group_probability_(0), |
| 36 next_group_number_(0), | 40 next_group_number_(0), |
| 37 group_(kNotParticipating) { | 41 group_(kNotParticipating), |
| 42 all_remaining_probability_set_(false), |
| 43 usage_timeout_(base::TimeDelta::FromDays(30)) { |
| 38 FieldTrialList::Register(this); | 44 FieldTrialList::Register(this); |
| 45 disable_field_trial_ = ShouldFieldTrialBeDisabled(usage_timeout_); |
| 39 } | 46 } |
| 40 | 47 |
| 41 int FieldTrial::AppendGroup(const std::string& name, | 48 int FieldTrial::AppendGroup(const std::string& name, |
| 42 Probability group_probability) { | 49 Probability group_probability) { |
| 43 DCHECK(group_probability <= divisor_); | 50 DCHECK(group_probability <= divisor_); |
| 44 DCHECK(group_probability >=0 || | 51 DCHECK(group_probability >=0 || |
| 45 group_probability == kAllRemainingProbability); | 52 group_probability == kAllRemainingProbability); |
| 46 if (group_probability == kAllRemainingProbability) { | 53 if (group_probability == kAllRemainingProbability) { |
| 47 accumulated_group_probability_ = divisor_; | 54 accumulated_group_probability_ = divisor_; |
| 55 all_remaining_probability_set_ = true; |
| 48 } else { | 56 } else { |
| 49 if (enable_benchmarking_) | 57 if (enable_benchmarking_ || disable_field_trial_) |
| 50 group_probability = 0; | 58 group_probability = 0; |
| 59 |
| 51 accumulated_group_probability_ += group_probability; | 60 accumulated_group_probability_ += group_probability; |
| 52 } | 61 } |
| 53 DCHECK(accumulated_group_probability_ <= divisor_); | 62 DCHECK(accumulated_group_probability_ <= divisor_); |
| 54 if (group_ == kNotParticipating && accumulated_group_probability_ > random_) { | 63 if (group_ == kNotParticipating && accumulated_group_probability_ > random_) { |
| 55 // This is the group that crossed the random line, so we do the assignment. | 64 // This is the group that crossed the random line, so we do the assignment. |
| 56 group_ = next_group_number_; | 65 group_ = next_group_number_; |
| 57 if (name.empty()) | 66 if (name.empty()) |
| 58 base::StringAppendF(&group_name_, "%d", group_); | 67 base::StringAppendF(&group_name_, "%d", group_); |
| 59 else | 68 else |
| 60 group_name_ = name; | 69 group_name_ = name; |
| 61 } | 70 } |
| 62 return next_group_number_++; | 71 return next_group_number_++; |
| 63 } | 72 } |
| 64 | 73 |
| 74 void FieldTrial::SetUsageTimeout(const base::TimeDelta& usage_timeout) { |
| 75 usage_timeout_ = usage_timeout; |
| 76 disable_field_trial_ = ShouldFieldTrialBeDisabled(usage_timeout); |
| 77 } |
| 78 |
| 65 // static | 79 // static |
| 66 std::string FieldTrial::MakeName(const std::string& name_prefix, | 80 std::string FieldTrial::MakeName(const std::string& name_prefix, |
| 67 const std::string& trial_name) { | 81 const std::string& trial_name) { |
| 68 std::string big_string(name_prefix); | 82 std::string big_string(name_prefix); |
| 69 big_string.append(1, kHistogramFieldTrialSeparator); | 83 big_string.append(1, kHistogramFieldTrialSeparator); |
| 70 return big_string.append(FieldTrialList::FindFullName(trial_name)); | 84 return big_string.append(FieldTrialList::FindFullName(trial_name)); |
| 71 } | 85 } |
| 72 | 86 |
| 73 // static | 87 // static |
| 74 void FieldTrial::EnableBenchmarking() { | 88 void FieldTrial::EnableBenchmarking() { |
| 75 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); | 89 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); |
| 76 enable_benchmarking_ = true; | 90 enable_benchmarking_ = true; |
| 77 } | 91 } |
| 78 | 92 |
| 93 // static |
| 94 bool FieldTrial::ShouldFieldTrialBeDisabled( |
| 95 const base::TimeDelta& usage_timeout) { |
| 96 if (base::Time::NowFromSystemTime() - GetBuildTime() >= usage_timeout) |
| 97 return true; |
| 98 return false; |
| 99 } |
| 100 |
| 101 // static |
| 102 Time FieldTrial::GetBuildTime() { |
| 103 static Time integral_build_time; |
| 104 if (integral_build_time.is_null()) { |
| 105 const char* kDateTime = __DATE__ " " __TIME__; |
| 106 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), |
| 107 &integral_build_time); |
| 108 DCHECK(result); |
| 109 } |
| 110 return integral_build_time; |
| 111 } |
| 112 |
| 79 FieldTrial::~FieldTrial() {} | 113 FieldTrial::~FieldTrial() {} |
| 80 | 114 |
| 81 //------------------------------------------------------------------------------ | 115 //------------------------------------------------------------------------------ |
| 82 // FieldTrialList methods and members. | 116 // FieldTrialList methods and members. |
| 83 | 117 |
| 84 // static | 118 // static |
| 85 FieldTrialList* FieldTrialList::global_ = NULL; | 119 FieldTrialList* FieldTrialList::global_ = NULL; |
| 86 | 120 |
| 87 // static | 121 // static |
| 88 bool FieldTrialList::register_without_global_ = false; | 122 bool FieldTrialList::register_without_global_ = false; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 int FieldTrialList::FindValue(const std::string& name) { | 154 int FieldTrialList::FindValue(const std::string& name) { |
| 121 FieldTrial* field_trial = Find(name); | 155 FieldTrial* field_trial = Find(name); |
| 122 if (field_trial) | 156 if (field_trial) |
| 123 return field_trial->group(); | 157 return field_trial->group(); |
| 124 return FieldTrial::kNotParticipating; | 158 return FieldTrial::kNotParticipating; |
| 125 } | 159 } |
| 126 | 160 |
| 127 // static | 161 // static |
| 128 std::string FieldTrialList::FindFullName(const std::string& name) { | 162 std::string FieldTrialList::FindFullName(const std::string& name) { |
| 129 FieldTrial* field_trial = Find(name); | 163 FieldTrial* field_trial = Find(name); |
| 130 if (field_trial) | 164 if (field_trial) { |
| 165 DCHECK(field_trial->all_remaining_probability_set()); |
| 131 return field_trial->group_name(); | 166 return field_trial->group_name(); |
| 167 } |
| 132 return ""; | 168 return ""; |
| 133 } | 169 } |
| 134 | 170 |
| 135 // static | 171 // static |
| 136 FieldTrial* FieldTrialList::Find(const std::string& name) { | 172 FieldTrial* FieldTrialList::Find(const std::string& name) { |
| 137 if (!global_) | 173 if (!global_) |
| 138 return NULL; | 174 return NULL; |
| 139 AutoLock auto_lock(global_->lock_); | 175 AutoLock auto_lock(global_->lock_); |
| 140 return global_->PreLockedFind(name); | 176 return global_->PreLockedFind(name); |
| 141 } | 177 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 | 226 |
| 191 FieldTrial *field_trial(FieldTrialList::Find(name)); | 227 FieldTrial *field_trial(FieldTrialList::Find(name)); |
| 192 if (field_trial) { | 228 if (field_trial) { |
| 193 // In single process mode, we may have already created the field trial. | 229 // In single process mode, we may have already created the field trial. |
| 194 if (field_trial->group_name() != group_name) | 230 if (field_trial->group_name() != group_name) |
| 195 return false; | 231 return false; |
| 196 continue; | 232 continue; |
| 197 } | 233 } |
| 198 const int kTotalProbability = 100; | 234 const int kTotalProbability = 100; |
| 199 field_trial = new FieldTrial(name, kTotalProbability); | 235 field_trial = new FieldTrial(name, kTotalProbability); |
| 200 field_trial->AppendGroup(group_name, kTotalProbability); | 236 field_trial->AppendGroup(group_name, FieldTrial::kAllRemainingProbability); |
| 201 } | 237 } |
| 202 return true; | 238 return true; |
| 203 } | 239 } |
| 204 | 240 |
| 205 // static | 241 // static |
| 206 size_t FieldTrialList::GetFieldTrialCount() { | 242 size_t FieldTrialList::GetFieldTrialCount() { |
| 207 if (!global_) | 243 if (!global_) |
| 208 return 0; | 244 return 0; |
| 209 AutoLock auto_lock(global_->lock_); | 245 AutoLock auto_lock(global_->lock_); |
| 210 return global_->registered_.size(); | 246 return global_->registered_.size(); |
| 211 } | 247 } |
| 212 | 248 |
| 213 } // namespace base | 249 } // namespace base |
| OLD | NEW |