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 |
18 const int FieldTrial::kNotFinalized = -1; | 18 const int FieldTrial::kNotFinalized = -1; |
19 | 19 |
20 // static | 20 // static |
21 const int FieldTrial::kDefaultGroupNumber = 0; | 21 const int FieldTrial::kDefaultGroupNumber = 0; |
22 | 22 |
23 // static | 23 // static |
24 const uint32 FieldTrial::kReservedHashValue = 0; | |
25 | |
26 // static | |
24 bool FieldTrial::enable_benchmarking_ = false; | 27 bool FieldTrial::enable_benchmarking_ = false; |
25 | 28 |
26 // static | 29 // static |
27 const char FieldTrialList::kPersistentStringSeparator('/'); | 30 const char FieldTrialList::kPersistentStringSeparator('/'); |
28 | 31 |
29 static const char kHistogramFieldTrialSeparator('_'); | 32 static const char kHistogramFieldTrialSeparator('_'); |
30 | 33 |
31 // static | 34 // static |
32 int FieldTrialList::kExpirationYearInFuture = 0; | 35 int FieldTrialList::kExpirationYearInFuture = 0; |
33 | 36 |
34 | 37 |
35 //------------------------------------------------------------------------------ | 38 //------------------------------------------------------------------------------ |
36 // FieldTrial methods and members. | 39 // FieldTrial methods and members. |
37 | 40 |
38 FieldTrial::FieldTrial(const std::string& name, | 41 FieldTrial::FieldTrial(const std::string& name, |
39 const Probability total_probability, | 42 const Probability total_probability, |
40 const std::string& default_group_name, | 43 const std::string& default_group_name, |
41 const int year, | 44 const int year, |
42 const int month, | 45 const int month, |
43 const int day_of_month) | 46 const int day_of_month) |
44 : name_(name), | 47 : name_(name), |
48 name_hash_(HashName(name)), | |
45 divisor_(total_probability), | 49 divisor_(total_probability), |
46 default_group_name_(default_group_name), | 50 default_group_name_(default_group_name), |
47 random_(static_cast<Probability>(divisor_ * RandDouble())), | 51 random_(static_cast<Probability>(divisor_ * RandDouble())), |
48 accumulated_group_probability_(0), | 52 accumulated_group_probability_(0), |
49 next_group_number_(kDefaultGroupNumber + 1), | 53 next_group_number_(kDefaultGroupNumber + 1), |
50 group_(kNotFinalized), | 54 group_(kNotFinalized), |
55 group_name_hash_(kReservedHashValue), | |
51 enable_field_trial_(true) { | 56 enable_field_trial_(true) { |
52 DCHECK_GT(total_probability, 0); | 57 DCHECK_GT(total_probability, 0); |
53 DCHECK(!name_.empty()); | 58 DCHECK(!name_.empty()); |
54 DCHECK(!default_group_name_.empty()); | 59 DCHECK(!default_group_name_.empty()); |
55 FieldTrialList::Register(this); | 60 FieldTrialList::Register(this); |
56 | 61 |
57 DCHECK_GT(year, 1970); | 62 DCHECK_GT(year, 1970); |
58 DCHECK_GT(month, 0); | 63 DCHECK_GT(month, 0); |
59 DCHECK_LT(month, 13); | 64 DCHECK_LT(month, 13); |
60 DCHECK_GT(day_of_month, 0); | 65 DCHECK_GT(day_of_month, 0); |
(...skipping 28 matching lines...) Expand all Loading... | |
89 } | 94 } |
90 | 95 |
91 void FieldTrial::Disable() { | 96 void FieldTrial::Disable() { |
92 enable_field_trial_ = false; | 97 enable_field_trial_ = false; |
93 | 98 |
94 // In case we are disabled after initialization, we need to switch | 99 // In case we are disabled after initialization, we need to switch |
95 // the trial to the default group. | 100 // the trial to the default group. |
96 if (group_ != kNotFinalized) { | 101 if (group_ != kNotFinalized) { |
97 group_ = kDefaultGroupNumber; | 102 group_ = kDefaultGroupNumber; |
98 group_name_ = default_group_name_; | 103 group_name_ = default_group_name_; |
104 group_name_hash_ = HashName(group_name_); | |
99 } | 105 } |
100 } | 106 } |
101 | 107 |
102 int FieldTrial::AppendGroup(const std::string& name, | 108 int FieldTrial::AppendGroup(const std::string& name, |
103 Probability group_probability) { | 109 Probability group_probability) { |
104 DCHECK_LE(group_probability, divisor_); | 110 DCHECK_LE(group_probability, divisor_); |
105 DCHECK_GE(group_probability, 0); | 111 DCHECK_GE(group_probability, 0); |
106 | 112 |
107 if (enable_benchmarking_ || !enable_field_trial_) | 113 if (enable_benchmarking_ || !enable_field_trial_) |
108 group_probability = 0; | 114 group_probability = 0; |
109 | 115 |
110 accumulated_group_probability_ += group_probability; | 116 accumulated_group_probability_ += group_probability; |
111 | 117 |
112 DCHECK_LE(accumulated_group_probability_, divisor_); | 118 DCHECK_LE(accumulated_group_probability_, divisor_); |
113 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) { | 119 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) { |
114 // This is the group that crossed the random line, so we do the assignment. | 120 // This is the group that crossed the random line, so we do the assignment. |
115 group_ = next_group_number_; | 121 group_ = next_group_number_; |
116 if (name.empty()) | 122 if (name.empty()) |
117 StringAppendF(&group_name_, "%d", group_); | 123 StringAppendF(&group_name_, "%d", group_); |
118 else | 124 else |
119 group_name_ = name; | 125 group_name_ = name; |
126 group_name_hash_ = HashName(group_name_); | |
120 FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_); | 127 FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_); |
121 } | 128 } |
122 return next_group_number_++; | 129 return next_group_number_++; |
123 } | 130 } |
124 | 131 |
125 int FieldTrial::group() { | 132 int FieldTrial::group() { |
126 if (group_ == kNotFinalized) { | 133 if (group_ == kNotFinalized) { |
127 accumulated_group_probability_ = divisor_; | 134 accumulated_group_probability_ = divisor_; |
128 group_ = kDefaultGroupNumber; | 135 group_ = kDefaultGroupNumber; |
129 group_name_ = default_group_name_; | 136 group_name_ = default_group_name_; |
137 group_name_hash_ = HashName(group_name_); | |
130 FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_); | 138 FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_); |
131 } | 139 } |
132 return group_; | 140 return group_; |
133 } | 141 } |
134 | 142 |
135 std::string FieldTrial::group_name() { | 143 std::string FieldTrial::group_name() { |
136 group(); // call group() to make sure group assignment was done. | 144 group(); // call group() to make sure group assignment was done. |
137 DCHECK(!group_name_.empty()); | 145 DCHECK(!group_name_.empty()); |
138 return group_name_; | 146 return group_name_; |
139 } | 147 } |
140 | 148 |
149 bool FieldTrial::GetNameGroupId(NameGroupId* name_group_id) { | |
150 if (group_name_hash_ == kReservedHashValue) | |
151 return false; | |
152 name_group_id->name = name_hash_; | |
153 name_group_id->group = group_name_hash_; | |
154 return true; | |
155 } | |
156 | |
141 // static | 157 // static |
142 std::string FieldTrial::MakeName(const std::string& name_prefix, | 158 std::string FieldTrial::MakeName(const std::string& name_prefix, |
143 const std::string& trial_name) { | 159 const std::string& trial_name) { |
144 std::string big_string(name_prefix); | 160 std::string big_string(name_prefix); |
145 big_string.append(1, kHistogramFieldTrialSeparator); | 161 big_string.append(1, kHistogramFieldTrialSeparator); |
146 return big_string.append(FieldTrialList::FindFullName(trial_name)); | 162 return big_string.append(FieldTrialList::FindFullName(trial_name)); |
147 } | 163 } |
148 | 164 |
149 // static | 165 // static |
150 void FieldTrial::EnableBenchmarking() { | 166 void FieldTrial::EnableBenchmarking() { |
(...skipping 15 matching lines...) Expand all Loading... | |
166 SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), | 182 SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), |
167 input.size(), | 183 input.size(), |
168 sha1_hash); | 184 sha1_hash); |
169 | 185 |
170 COMPILE_ASSERT(sizeof(uint64) < sizeof(sha1_hash), need_more_data); | 186 COMPILE_ASSERT(sizeof(uint64) < sizeof(sha1_hash), need_more_data); |
171 uint64* bits = reinterpret_cast<uint64*>(&sha1_hash[0]); | 187 uint64* bits = reinterpret_cast<uint64*>(&sha1_hash[0]); |
172 | 188 |
173 return BitsToOpenEndedUnitInterval(*bits); | 189 return BitsToOpenEndedUnitInterval(*bits); |
174 } | 190 } |
175 | 191 |
192 // static | |
193 uint32 FieldTrial::HashName(const std::string& name) { | |
194 // SHA-1 is designed to produce a uniformly random spread in its output space, | |
195 // even for nearly-identical name. | |
Alexei Svitkine (slow)
2012/01/25 17:40:16
"nearly-identical name" -> "nearly-identical input
MAD
2012/01/25 18:48:53
Done.
| |
196 unsigned char sha1_hash[kSHA1Length]; | |
197 SHA1HashBytes(reinterpret_cast<const unsigned char*>(name.c_str()), | |
Alexei Svitkine (slow)
2012/01/25 15:25:19
I think you can use |SHA1HashString()| instead of
MAD
2012/01/25 17:02:33
I copied the behavior from HashClientId... Should
Alexei Svitkine (slow)
2012/01/25 17:40:16
Looking closer at |SHA1HashString()|, it returns i
MAD
2012/01/25 18:48:53
OK, Thanks for checking... ;-)
| |
198 name.size(), | |
199 sha1_hash); | |
200 | |
201 COMPILE_ASSERT(sizeof(uint32) < sizeof(sha1_hash), need_more_data); | |
202 uint32* bits = reinterpret_cast<uint32*>(&sha1_hash[0]); | |
203 DCHECK(*bits != kReservedHashValue) << "Bad luck, that name can't be used!"; | |
204 return *bits; | |
205 } | |
206 | |
176 //------------------------------------------------------------------------------ | 207 //------------------------------------------------------------------------------ |
177 // FieldTrialList methods and members. | 208 // FieldTrialList methods and members. |
178 | 209 |
179 // static | 210 // static |
180 FieldTrialList* FieldTrialList::global_ = NULL; | 211 FieldTrialList* FieldTrialList::global_ = NULL; |
181 | 212 |
182 // static | 213 // static |
183 bool FieldTrialList::used_without_global_ = false; | 214 bool FieldTrialList::used_without_global_ = false; |
184 | 215 |
185 FieldTrialList::FieldTrialList(const std::string& client_id) | 216 FieldTrialList::FieldTrialList(const std::string& client_id) |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 DCHECK_EQ(name.find(kPersistentStringSeparator), std::string::npos); | 296 DCHECK_EQ(name.find(kPersistentStringSeparator), std::string::npos); |
266 DCHECK_EQ(group_name.find(kPersistentStringSeparator), std::string::npos); | 297 DCHECK_EQ(group_name.find(kPersistentStringSeparator), std::string::npos); |
267 output->append(name); | 298 output->append(name); |
268 output->append(1, kPersistentStringSeparator); | 299 output->append(1, kPersistentStringSeparator); |
269 output->append(group_name); | 300 output->append(group_name); |
270 output->append(1, kPersistentStringSeparator); | 301 output->append(1, kPersistentStringSeparator); |
271 } | 302 } |
272 } | 303 } |
273 | 304 |
274 // static | 305 // static |
306 void FieldTrialList::GetFieldTrialNameGroupIds( | |
307 std::vector<FieldTrial::NameGroupId>* name_group_ids) { | |
308 if (!global_) | |
Alexei Svitkine (slow)
2012/01/25 15:25:19
Should you clear |name_group_ids| in this case (an
MAD
2012/01/25 17:02:33
Again, I copied the behavior from StatesToString w
Alexei Svitkine (slow)
2012/01/25 17:40:16
I'm okay with the DCHECK(), though then it should
MAD
2012/01/25 18:48:53
Done.
| |
309 return; | |
310 DCHECK(name_group_ids->empty()); | |
311 AutoLock auto_lock(global_->lock_); | |
312 | |
313 for (RegistrationList::iterator it = global_->registered_.begin(); | |
314 it != global_->registered_.end(); ++it) { | |
315 FieldTrial::NameGroupId name_group_id; | |
316 if (it->second->GetNameGroupId(&name_group_id)) | |
317 name_group_ids->push_back(name_group_id); | |
318 } | |
319 } | |
320 | |
321 // static | |
275 bool FieldTrialList::CreateTrialsInChildProcess( | 322 bool FieldTrialList::CreateTrialsInChildProcess( |
276 const std::string& parent_trials) { | 323 const std::string& parent_trials) { |
277 DCHECK(global_); | 324 DCHECK(global_); |
278 if (parent_trials.empty() || !global_) | 325 if (parent_trials.empty() || !global_) |
279 return true; | 326 return true; |
280 | 327 |
281 size_t next_item = 0; | 328 size_t next_item = 0; |
282 while (next_item < parent_trials.length()) { | 329 while (next_item < parent_trials.length()) { |
283 size_t name_end = parent_trials.find(kPersistentStringSeparator, next_item); | 330 size_t name_end = parent_trials.find(kPersistentStringSeparator, next_item); |
284 if (name_end == parent_trials.npos || next_item == name_end) | 331 if (name_end == parent_trials.npos || next_item == name_end) |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
378 } | 425 } |
379 | 426 |
380 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { | 427 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { |
381 RegistrationList::iterator it = registered_.find(name); | 428 RegistrationList::iterator it = registered_.find(name); |
382 if (registered_.end() == it) | 429 if (registered_.end() == it) |
383 return NULL; | 430 return NULL; |
384 return it->second; | 431 return it->second; |
385 } | 432 } |
386 | 433 |
387 } // namespace base | 434 } // namespace base |
OLD | NEW |