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

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

Powered by Google App Engine
This is Rietveld 408576698