Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: base/metrics/field_trial.cc

Issue 9117037: Added a Unique ID for a Field Trial containing it's hashed name and the selected group ID. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/metrics/field_trial.h ('k') | base/metrics/field_trial_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 23 const uint32 FieldTrial::kFallbackHashValue = 1;
24 bool FieldTrial::enable_benchmarking_ = false; 24 bool FieldTrial::enable_benchmarking_ = false;
25 25
26 // static
27 const char FieldTrialList::kPersistentStringSeparator('/'); 26 const char FieldTrialList::kPersistentStringSeparator('/');
28
29 static const char kHistogramFieldTrialSeparator('_');
30
31 // static
32 int FieldTrialList::kExpirationYearInFuture = 0; 27 int FieldTrialList::kExpirationYearInFuture = 0;
33 28
34
35 //------------------------------------------------------------------------------ 29 //------------------------------------------------------------------------------
36 // FieldTrial methods and members. 30 // FieldTrial methods and members.
37 31
38 FieldTrial::FieldTrial(const std::string& name, 32 FieldTrial::FieldTrial(const std::string& name,
39 const Probability total_probability, 33 const Probability total_probability,
40 const std::string& default_group_name, 34 const std::string& default_group_name,
41 const int year, 35 const int year,
42 const int month, 36 const int month,
43 const int day_of_month) 37 const int day_of_month)
44 : name_(name), 38 : name_(name),
39 name_hash_(HashName(name)),
45 divisor_(total_probability), 40 divisor_(total_probability),
46 default_group_name_(default_group_name), 41 default_group_name_(default_group_name),
47 random_(static_cast<Probability>(divisor_ * RandDouble())), 42 random_(static_cast<Probability>(divisor_ * RandDouble())),
48 accumulated_group_probability_(0), 43 accumulated_group_probability_(0),
49 next_group_number_(kDefaultGroupNumber + 1), 44 next_group_number_(kDefaultGroupNumber + 1),
50 group_(kNotFinalized), 45 group_(kNotFinalized),
46 group_name_hash_(kReservedHashValue),
51 enable_field_trial_(true) { 47 enable_field_trial_(true) {
52 DCHECK_GT(total_probability, 0); 48 DCHECK_GT(total_probability, 0);
53 DCHECK(!name_.empty()); 49 DCHECK(!name_.empty());
54 DCHECK(!default_group_name_.empty()); 50 DCHECK(!default_group_name_.empty());
55 FieldTrialList::Register(this); 51 FieldTrialList::Register(this);
56 52
57 DCHECK_GT(year, 1970); 53 DCHECK_GT(year, 1970);
58 DCHECK_GT(month, 0); 54 DCHECK_GT(month, 0);
59 DCHECK_LT(month, 13); 55 DCHECK_LT(month, 13);
60 DCHECK_GT(day_of_month, 0); 56 DCHECK_GT(day_of_month, 0);
(...skipping 28 matching lines...) Expand all
89 } 85 }
90 86
91 void FieldTrial::Disable() { 87 void FieldTrial::Disable() {
92 enable_field_trial_ = false; 88 enable_field_trial_ = false;
93 89
94 // In case we are disabled after initialization, we need to switch 90 // In case we are disabled after initialization, we need to switch
95 // the trial to the default group. 91 // the trial to the default group.
96 if (group_ != kNotFinalized) { 92 if (group_ != kNotFinalized) {
97 group_ = kDefaultGroupNumber; 93 group_ = kDefaultGroupNumber;
98 group_name_ = default_group_name_; 94 group_name_ = default_group_name_;
95 group_name_hash_ = HashName(group_name_);
99 } 96 }
100 } 97 }
101 98
102 int FieldTrial::AppendGroup(const std::string& name, 99 int FieldTrial::AppendGroup(const std::string& name,
103 Probability group_probability) { 100 Probability group_probability) {
104 DCHECK_LE(group_probability, divisor_); 101 DCHECK_LE(group_probability, divisor_);
105 DCHECK_GE(group_probability, 0); 102 DCHECK_GE(group_probability, 0);
106 103
107 if (enable_benchmarking_ || !enable_field_trial_) 104 if (enable_benchmarking_ || !enable_field_trial_)
108 group_probability = 0; 105 group_probability = 0;
109 106
110 accumulated_group_probability_ += group_probability; 107 accumulated_group_probability_ += group_probability;
111 108
112 DCHECK_LE(accumulated_group_probability_, divisor_); 109 DCHECK_LE(accumulated_group_probability_, divisor_);
113 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) { 110 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) {
114 // This is the group that crossed the random line, so we do the assignment. 111 // This is the group that crossed the random line, so we do the assignment.
115 group_ = next_group_number_; 112 group_ = next_group_number_;
116 if (name.empty()) 113 if (name.empty())
117 StringAppendF(&group_name_, "%d", group_); 114 StringAppendF(&group_name_, "%d", group_);
118 else 115 else
119 group_name_ = name; 116 group_name_ = name;
117 group_name_hash_ = HashName(group_name_);
120 FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_); 118 FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_);
121 } 119 }
122 return next_group_number_++; 120 return next_group_number_++;
123 } 121 }
124 122
125 int FieldTrial::group() { 123 int FieldTrial::group() {
126 if (group_ == kNotFinalized) { 124 if (group_ == kNotFinalized) {
127 accumulated_group_probability_ = divisor_; 125 accumulated_group_probability_ = divisor_;
128 group_ = kDefaultGroupNumber; 126 group_ = kDefaultGroupNumber;
129 group_name_ = default_group_name_; 127 group_name_ = default_group_name_;
128 group_name_hash_ = HashName(group_name_);
130 FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_); 129 FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_);
131 } 130 }
132 return group_; 131 return group_;
133 } 132 }
134 133
135 std::string FieldTrial::group_name() { 134 std::string FieldTrial::group_name() {
136 group(); // call group() to make sure group assignment was done. 135 group(); // call group() to make sure group assignment was done.
137 DCHECK(!group_name_.empty()); 136 DCHECK(!group_name_.empty());
138 return group_name_; 137 return group_name_;
139 } 138 }
140 139
140 bool FieldTrial::GetNameGroupId(NameGroupId* name_group_id) {
141 if (group_name_hash_ == kReservedHashValue)
142 return false;
143 name_group_id->name = name_hash_;
144 name_group_id->group = group_name_hash_;
145 return true;
146 }
147
141 // static 148 // static
142 std::string FieldTrial::MakeName(const std::string& name_prefix, 149 std::string FieldTrial::MakeName(const std::string& name_prefix,
143 const std::string& trial_name) { 150 const std::string& trial_name) {
144 std::string big_string(name_prefix); 151 std::string big_string(name_prefix);
145 big_string.append(1, kHistogramFieldTrialSeparator); 152 big_string.append(1, kHistogramFieldTrialSeparator);
146 return big_string.append(FieldTrialList::FindFullName(trial_name)); 153 return big_string.append(FieldTrialList::FindFullName(trial_name));
147 } 154 }
148 155
149 // static 156 // static
150 void FieldTrial::EnableBenchmarking() { 157 void FieldTrial::EnableBenchmarking() {
(...skipping 15 matching lines...) Expand all
166 SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), 173 SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()),
167 input.size(), 174 input.size(),
168 sha1_hash); 175 sha1_hash);
169 176
170 COMPILE_ASSERT(sizeof(uint64) < sizeof(sha1_hash), need_more_data); 177 COMPILE_ASSERT(sizeof(uint64) < sizeof(sha1_hash), need_more_data);
171 uint64* bits = reinterpret_cast<uint64*>(&sha1_hash[0]); 178 uint64* bits = reinterpret_cast<uint64*>(&sha1_hash[0]);
172 179
173 return BitsToOpenEndedUnitInterval(*bits); 180 return BitsToOpenEndedUnitInterval(*bits);
174 } 181 }
175 182
183 // static
184 uint32 FieldTrial::HashName(const std::string& name) {
185 // SHA-1 is designed to produce a uniformly random spread in its output space,
186 // even for nearly-identical inputs.
187 unsigned char sha1_hash[kSHA1Length];
188 SHA1HashBytes(reinterpret_cast<const unsigned char*>(name.c_str()),
189 name.size(),
190 sha1_hash);
191
192 COMPILE_ASSERT(sizeof(uint32) < sizeof(sha1_hash), need_more_data);
193 // We need to make sure we avoid kReservedHashValue.
194 // So we try different offsets within sha1_hash.
195 size_t offset = 0;
196 static const size_t kStep = sizeof(uint32) / sizeof(char);
Alexei Svitkine (slow) 2012/01/25 18:59:39 You can make this logic a little simpler - if you
jar (doing other things) 2012/01/25 19:52:14 +1 for some variant of Alexei's suggestion. nit:
ian fette 2012/01/25 20:03:42 A related note is that you're currently stepping t
MAD 2012/01/26 03:14:43 Went back to previous version...
197 uint32* bits;
198 do {
199 bits = reinterpret_cast<uint32*>(&sha1_hash[offset]);
jar (doing other things) 2012/01/25 19:52:14 I suspect this code is not safe for big-endian vs
MAD 2012/01/26 03:14:43 Added tests... Good idea...
200 offset += kStep;
201 } while (*bits == kReservedHashValue && offset <= kSHA1Length - kStep);
202
203 // Or fallback to a constant hard coded value.
204 if (*bits == kReservedHashValue)
205 *bits = kFallbackHashValue;
Alexei Svitkine (slow) 2012/01/25 18:59:39 While this case seems extremely unlikely, its also
jar (doing other things) 2012/01/25 19:52:14 At best, a DCHECK is called for IMO... but the poi
206 return *bits;
207 }
208
176 //------------------------------------------------------------------------------ 209 //------------------------------------------------------------------------------
177 // FieldTrialList methods and members. 210 // FieldTrialList methods and members.
178 211
179 // static 212 // static
180 FieldTrialList* FieldTrialList::global_ = NULL; 213 FieldTrialList* FieldTrialList::global_ = NULL;
181 214
182 // static 215 // static
183 bool FieldTrialList::used_without_global_ = false; 216 bool FieldTrialList::used_without_global_ = false;
184 217
185 FieldTrialList::FieldTrialList(const std::string& client_id) 218 FieldTrialList::FieldTrialList(const std::string& client_id)
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 return ""; 277 return "";
245 } 278 }
246 279
247 // static 280 // static
248 bool FieldTrialList::TrialExists(const std::string& name) { 281 bool FieldTrialList::TrialExists(const std::string& name) {
249 return Find(name) != NULL; 282 return Find(name) != NULL;
250 } 283 }
251 284
252 // static 285 // static
253 void FieldTrialList::StatesToString(std::string* output) { 286 void FieldTrialList::StatesToString(std::string* output) {
287 DCHECK(output->empty());
254 if (!global_) 288 if (!global_)
255 return; 289 return;
256 DCHECK(output->empty());
257 AutoLock auto_lock(global_->lock_); 290 AutoLock auto_lock(global_->lock_);
258 291
259 for (RegistrationList::iterator it = global_->registered_.begin(); 292 for (RegistrationList::iterator it = global_->registered_.begin();
260 it != global_->registered_.end(); ++it) { 293 it != global_->registered_.end(); ++it) {
261 const std::string name = it->first; 294 const std::string name = it->first;
262 std::string group_name = it->second->group_name_internal(); 295 std::string group_name = it->second->group_name_internal();
263 if (group_name.empty()) 296 if (group_name.empty())
264 continue; // Should not include uninitialized trials. 297 continue; // Should not include uninitialized trials.
265 DCHECK_EQ(name.find(kPersistentStringSeparator), std::string::npos); 298 DCHECK_EQ(name.find(kPersistentStringSeparator), std::string::npos);
266 DCHECK_EQ(group_name.find(kPersistentStringSeparator), std::string::npos); 299 DCHECK_EQ(group_name.find(kPersistentStringSeparator), std::string::npos);
267 output->append(name); 300 output->append(name);
268 output->append(1, kPersistentStringSeparator); 301 output->append(1, kPersistentStringSeparator);
269 output->append(group_name); 302 output->append(group_name);
270 output->append(1, kPersistentStringSeparator); 303 output->append(1, kPersistentStringSeparator);
271 } 304 }
272 } 305 }
273 306
274 // static 307 // static
308 void FieldTrialList::GetFieldTrialNameGroupIds(
309 std::vector<FieldTrial::NameGroupId>* name_group_ids) {
310 DCHECK(name_group_ids->empty());
311 if (!global_)
312 return;
313 AutoLock auto_lock(global_->lock_);
314
315 for (RegistrationList::iterator it = global_->registered_.begin();
316 it != global_->registered_.end(); ++it) {
317 FieldTrial::NameGroupId name_group_id;
318 if (it->second->GetNameGroupId(&name_group_id))
319 name_group_ids->push_back(name_group_id);
320 }
321 }
322
323 // static
275 bool FieldTrialList::CreateTrialsInChildProcess( 324 bool FieldTrialList::CreateTrialsInChildProcess(
276 const std::string& parent_trials) { 325 const std::string& parent_trials) {
277 DCHECK(global_); 326 DCHECK(global_);
278 if (parent_trials.empty() || !global_) 327 if (parent_trials.empty() || !global_)
279 return true; 328 return true;
280 329
281 size_t next_item = 0; 330 size_t next_item = 0;
282 while (next_item < parent_trials.length()) { 331 while (next_item < parent_trials.length()) {
283 size_t name_end = parent_trials.find(kPersistentStringSeparator, next_item); 332 size_t name_end = parent_trials.find(kPersistentStringSeparator, next_item);
284 if (name_end == parent_trials.npos || next_item == name_end) 333 if (name_end == parent_trials.npos || next_item == name_end)
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 } 427 }
379 428
380 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { 429 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) {
381 RegistrationList::iterator it = registered_.find(name); 430 RegistrationList::iterator it = registered_.find(name);
382 if (registered_.end() == it) 431 if (registered_.end() == it)
383 return NULL; 432 return NULL;
384 return it->second; 433 return it->second;
385 } 434 }
386 435
387 } // namespace base 436 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/field_trial.h ('k') | base/metrics/field_trial_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698