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/logging.h" | 7 #include "base/logging.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/sha1.h" |
| 10 #include "base/string_util.h" |
9 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
10 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
11 | 13 |
12 namespace base { | 14 namespace base { |
13 | 15 |
14 // static | 16 // static |
15 const int FieldTrial::kNotFinalized = -1; | 17 const int FieldTrial::kNotFinalized = -1; |
16 | 18 |
17 // static | 19 // static |
18 const int FieldTrial::kDefaultGroupNumber = 0; | 20 const int FieldTrial::kDefaultGroupNumber = 0; |
(...skipping 11 matching lines...) Expand all Loading... |
30 | 32 |
31 FieldTrial::FieldTrial(const std::string& name, | 33 FieldTrial::FieldTrial(const std::string& name, |
32 const Probability total_probability, | 34 const Probability total_probability, |
33 const std::string& default_group_name, | 35 const std::string& default_group_name, |
34 const int year, | 36 const int year, |
35 const int month, | 37 const int month, |
36 const int day_of_month) | 38 const int day_of_month) |
37 : name_(name), | 39 : name_(name), |
38 divisor_(total_probability), | 40 divisor_(total_probability), |
39 default_group_name_(default_group_name), | 41 default_group_name_(default_group_name), |
40 random_(static_cast<Probability>(divisor_ * base::RandDouble())), | 42 random_(static_cast<Probability>(divisor_ * RandDouble())), |
41 accumulated_group_probability_(0), | 43 accumulated_group_probability_(0), |
42 next_group_number_(kDefaultGroupNumber+1), | 44 next_group_number_(kDefaultGroupNumber+1), |
43 group_(kNotFinalized) { | 45 group_(kNotFinalized), |
| 46 enable_field_trial_(true) { |
44 DCHECK_GT(total_probability, 0); | 47 DCHECK_GT(total_probability, 0); |
| 48 DCHECK(!name_.empty()); |
45 DCHECK(!default_group_name_.empty()); | 49 DCHECK(!default_group_name_.empty()); |
46 FieldTrialList::Register(this); | 50 FieldTrialList::Register(this); |
47 | 51 |
48 DCHECK_GT(year, 1970); | 52 DCHECK_GT(year, 1970); |
49 DCHECK_GT(month, 0); | 53 DCHECK_GT(month, 0); |
50 DCHECK_LT(month, 13); | 54 DCHECK_LT(month, 13); |
51 DCHECK_GT(day_of_month, 0); | 55 DCHECK_GT(day_of_month, 0); |
52 DCHECK_LT(day_of_month, 32); | 56 DCHECK_LT(day_of_month, 32); |
53 | 57 |
54 base::Time::Exploded exploded; | 58 Time::Exploded exploded; |
55 exploded.year = year; | 59 exploded.year = year; |
56 exploded.month = month; | 60 exploded.month = month; |
57 exploded.day_of_week = 0; // Should be unused. | 61 exploded.day_of_week = 0; // Should be unused. |
58 exploded.day_of_month = day_of_month; | 62 exploded.day_of_month = day_of_month; |
59 exploded.hour = 0; | 63 exploded.hour = 0; |
60 exploded.minute = 0; | 64 exploded.minute = 0; |
61 exploded.second = 0; | 65 exploded.second = 0; |
62 exploded.millisecond = 0; | 66 exploded.millisecond = 0; |
63 | 67 |
64 base::Time expiration_time = Time::FromLocalExploded(exploded); | 68 Time expiration_time = Time::FromLocalExploded(exploded); |
65 disable_field_trial_ = (GetBuildTime() > expiration_time) ? true : false; | 69 if (GetBuildTime() > expiration_time) |
| 70 Disable(); |
| 71 } |
| 72 |
| 73 void FieldTrial::UseOneTimeRandomization() { |
| 74 DCHECK_EQ(group_, kNotFinalized); |
| 75 if (!FieldTrialList::IsOneTimeRandomizationEnabled()) { |
| 76 NOTREACHED(); |
| 77 Disable(); |
| 78 return; |
| 79 } |
| 80 |
| 81 random_ = static_cast<Probability>( |
| 82 divisor_ * HashClientId(FieldTrialList::client_id(), name_)); |
| 83 } |
| 84 |
| 85 void FieldTrial::Disable() { |
| 86 enable_field_trial_ = false; |
| 87 |
| 88 // In case we are disabled after initialization, we need to switch |
| 89 // the trial to the default group. |
| 90 if (group_ != kNotFinalized) { |
| 91 group_ = kDefaultGroupNumber; |
| 92 group_name_ = default_group_name_; |
| 93 } |
66 } | 94 } |
67 | 95 |
68 int FieldTrial::AppendGroup(const std::string& name, | 96 int FieldTrial::AppendGroup(const std::string& name, |
69 Probability group_probability) { | 97 Probability group_probability) { |
70 DCHECK_LE(group_probability, divisor_); | 98 DCHECK_LE(group_probability, divisor_); |
71 DCHECK_GE(group_probability, 0); | 99 DCHECK_GE(group_probability, 0); |
72 | 100 |
73 if (enable_benchmarking_ || disable_field_trial_) | 101 if (enable_benchmarking_ || !enable_field_trial_) |
74 group_probability = 0; | 102 group_probability = 0; |
75 | 103 |
76 accumulated_group_probability_ += group_probability; | 104 accumulated_group_probability_ += group_probability; |
77 | 105 |
78 DCHECK_LE(accumulated_group_probability_, divisor_); | 106 DCHECK_LE(accumulated_group_probability_, divisor_); |
79 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) { | 107 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) { |
80 // This is the group that crossed the random line, so we do the assignment. | 108 // This is the group that crossed the random line, so we do the assignment. |
81 group_ = next_group_number_; | 109 group_ = next_group_number_; |
82 if (name.empty()) | 110 if (name.empty()) |
83 base::StringAppendF(&group_name_, "%d", group_); | 111 StringAppendF(&group_name_, "%d", group_); |
84 else | 112 else |
85 group_name_ = name; | 113 group_name_ = name; |
86 } | 114 } |
87 return next_group_number_++; | 115 return next_group_number_++; |
88 } | 116 } |
89 | 117 |
90 int FieldTrial::group() { | 118 int FieldTrial::group() { |
91 if (group_ == kNotFinalized) { | 119 if (group_ == kNotFinalized) { |
92 accumulated_group_probability_ = divisor_; | 120 accumulated_group_probability_ = divisor_; |
93 group_ = kDefaultGroupNumber; | 121 group_ = kDefaultGroupNumber; |
94 group_name_ = default_group_name_; | 122 group_name_ = default_group_name_; |
95 } | 123 } |
96 return group_; | 124 return group_; |
97 } | 125 } |
98 | 126 |
99 std::string FieldTrial::group_name() { | 127 std::string FieldTrial::group_name() { |
100 group(); // call group() to make group assignment was done. | 128 group(); // call group() to make group assignment was done. |
| 129 DCHECK(!group_name_.empty()); |
101 return group_name_; | 130 return group_name_; |
102 } | 131 } |
103 | 132 |
104 // static | 133 // static |
105 std::string FieldTrial::MakeName(const std::string& name_prefix, | 134 std::string FieldTrial::MakeName(const std::string& name_prefix, |
106 const std::string& trial_name) { | 135 const std::string& trial_name) { |
107 std::string big_string(name_prefix); | 136 std::string big_string(name_prefix); |
108 big_string.append(1, kHistogramFieldTrialSeparator); | 137 big_string.append(1, kHistogramFieldTrialSeparator); |
109 return big_string.append(FieldTrialList::FindFullName(trial_name)); | 138 return big_string.append(FieldTrialList::FindFullName(trial_name)); |
110 } | 139 } |
111 | 140 |
112 // static | 141 // static |
113 void FieldTrial::EnableBenchmarking() { | 142 void FieldTrial::EnableBenchmarking() { |
114 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); | 143 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); |
115 enable_benchmarking_ = true; | 144 enable_benchmarking_ = true; |
116 } | 145 } |
117 | 146 |
118 FieldTrial::~FieldTrial() {} | 147 FieldTrial::~FieldTrial() {} |
119 | 148 |
120 // static | 149 // static |
121 Time FieldTrial::GetBuildTime() { | 150 Time FieldTrial::GetBuildTime() { |
122 Time integral_build_time; | 151 Time integral_build_time; |
123 const char* kDateTime = __DATE__ " " __TIME__; | 152 const char* kDateTime = __DATE__ " " __TIME__; |
124 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), | 153 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), |
125 &integral_build_time); | 154 &integral_build_time); |
126 DCHECK(result); | 155 DCHECK(result); |
127 return integral_build_time; | 156 return integral_build_time; |
128 } | 157 } |
129 | 158 |
| 159 // static |
| 160 double FieldTrial::HashClientId(const std::string& client_id, |
| 161 const std::string& trial_name) { |
| 162 // SHA-1 is designed to produce a uniformly random spread in its output space, |
| 163 // even for nearly-identical inputs, so it helps massage whatever client_id |
| 164 // and trial_name we get into something with a uniform distribution, which |
| 165 // is desirable so that we don't skew any part of the 0-100% spectrum. |
| 166 std::string input(client_id + trial_name); |
| 167 unsigned char sha1_hash[SHA1_LENGTH]; |
| 168 SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), |
| 169 input.size(), |
| 170 sha1_hash); |
| 171 |
| 172 COMPILE_ASSERT(sizeof(uint64) < sizeof(sha1_hash), need_more_data); |
| 173 uint64* bits = reinterpret_cast<uint64*>(&sha1_hash[0]); |
| 174 |
| 175 return BitsToOpenEndedUnitInterval(*bits); |
| 176 } |
| 177 |
130 //------------------------------------------------------------------------------ | 178 //------------------------------------------------------------------------------ |
131 // FieldTrialList methods and members. | 179 // FieldTrialList methods and members. |
132 | 180 |
133 // static | 181 // static |
134 FieldTrialList* FieldTrialList::global_ = NULL; | 182 FieldTrialList* FieldTrialList::global_ = NULL; |
135 | 183 |
136 // static | 184 // static |
137 bool FieldTrialList::register_without_global_ = false; | 185 bool FieldTrialList::register_without_global_ = false; |
138 | 186 |
139 FieldTrialList::FieldTrialList() : application_start_time_(TimeTicks::Now()) { | 187 FieldTrialList::FieldTrialList() : application_start_time_(TimeTicks::Now()) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 231 |
184 // static | 232 // static |
185 std::string FieldTrialList::FindFullName(const std::string& name) { | 233 std::string FieldTrialList::FindFullName(const std::string& name) { |
186 FieldTrial* field_trial = Find(name); | 234 FieldTrial* field_trial = Find(name); |
187 if (field_trial) | 235 if (field_trial) |
188 return field_trial->group_name(); | 236 return field_trial->group_name(); |
189 return ""; | 237 return ""; |
190 } | 238 } |
191 | 239 |
192 // static | 240 // static |
| 241 bool FieldTrialList::TrialExists(const std::string& name) { |
| 242 return Find(name) != NULL; |
| 243 } |
| 244 |
| 245 // static |
193 void FieldTrialList::StatesToString(std::string* output) { | 246 void FieldTrialList::StatesToString(std::string* output) { |
194 if (!global_) | 247 if (!global_) |
195 return; | 248 return; |
196 DCHECK(output->empty()); | 249 DCHECK(output->empty()); |
197 AutoLock auto_lock(global_->lock_); | 250 AutoLock auto_lock(global_->lock_); |
198 for (RegistrationList::iterator it = global_->registered_.begin(); | 251 for (RegistrationList::iterator it = global_->registered_.begin(); |
199 it != global_->registered_.end(); ++it) { | 252 it != global_->registered_.end(); ++it) { |
200 const std::string name = it->first; | 253 const std::string name = it->first; |
201 std::string group_name = it->second->group_name_internal(); | 254 std::string group_name = it->second->group_name_internal(); |
202 if (group_name.empty()) | 255 if (group_name.empty()) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 } | 309 } |
257 | 310 |
258 // static | 311 // static |
259 size_t FieldTrialList::GetFieldTrialCount() { | 312 size_t FieldTrialList::GetFieldTrialCount() { |
260 if (!global_) | 313 if (!global_) |
261 return 0; | 314 return 0; |
262 AutoLock auto_lock(global_->lock_); | 315 AutoLock auto_lock(global_->lock_); |
263 return global_->registered_.size(); | 316 return global_->registered_.size(); |
264 } | 317 } |
265 | 318 |
| 319 // static |
| 320 void FieldTrialList::EnableOneTimeRandomization(const std::string& client_id) { |
| 321 DCHECK(global_); |
| 322 if (!global_) |
| 323 return; |
| 324 |
| 325 DCHECK(!global_->IsOneTimeRandomizationEnabled()); |
| 326 DCHECK(!client_id.empty()); |
| 327 |
| 328 global_->client_id_ = client_id; |
| 329 } |
| 330 |
| 331 // static |
| 332 bool FieldTrialList::IsOneTimeRandomizationEnabled() { |
| 333 DCHECK(global_); |
| 334 if (!global_) |
| 335 return false; |
| 336 |
| 337 return !global_->client_id_.empty(); |
| 338 } |
| 339 |
| 340 // static |
| 341 const std::string& FieldTrialList::client_id() { |
| 342 DCHECK(global_); |
| 343 if (!global_) |
| 344 return EmptyString(); |
| 345 |
| 346 return global_->client_id_; |
| 347 } |
| 348 |
266 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { | 349 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { |
267 RegistrationList::iterator it = registered_.find(name); | 350 RegistrationList::iterator it = registered_.find(name); |
268 if (registered_.end() == it) | 351 if (registered_.end() == it) |
269 return NULL; | 352 return NULL; |
270 return it->second; | 353 return it->second; |
271 } | 354 } |
272 | 355 |
273 } // namespace base | 356 } // namespace base |
OLD | NEW |