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; | |
jar (doing other things)
2011/01/18 19:39:44
Given that all the code is in a "namespace base" g
rtenneti
2011/01/19 01:45:28
Done.
| |
10 | 14 |
11 namespace base { | 15 namespace base { |
12 | 16 |
13 // static | 17 // static |
14 const int FieldTrial::kNotParticipating = -1; | 18 const int FieldTrial::kNotFinalized = -1; |
15 | 19 |
16 // static | 20 // static |
17 const int FieldTrial::kAllRemainingProbability = -2; | 21 const int FieldTrial::kDefaultGroupNumber = 0; |
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, |
36 const std::string& default_group_name, | |
37 const int year, | |
38 const int month, | |
39 const int day_of_month) | |
32 : name_(name), | 40 : name_(name), |
33 divisor_(total_probability), | 41 divisor_(total_probability), |
42 default_group_name_(default_group_name), | |
34 random_(static_cast<Probability>(divisor_ * base::RandDouble())), | 43 random_(static_cast<Probability>(divisor_ * base::RandDouble())), |
35 accumulated_group_probability_(0), | 44 accumulated_group_probability_(0), |
36 next_group_number_(0), | 45 next_group_number_(kDefaultGroupNumber+1), |
37 group_(kNotParticipating) { | 46 group_(kNotFinalized), |
47 expiration_year_(year), | |
jar (doing other things)
2011/01/18 19:39:44
If these are only used in the constructor, and the
rtenneti
2011/01/19 01:45:28
Done.
| |
48 expiration_month_(month), | |
49 expiration_day_of_month_(day_of_month) { | |
50 DCHECK(!default_group_name_.empty()); | |
38 FieldTrialList::Register(this); | 51 FieldTrialList::Register(this); |
52 | |
53 DCHECK_GT(expiration_year_, 1970); | |
54 DCHECK_GT(expiration_month_, 0); | |
55 DCHECK_LT(expiration_month_, 13); | |
56 DCHECK_GT(expiration_day_of_month_, 0); | |
57 DCHECK_LT(expiration_day_of_month_, 32); | |
58 | |
59 base::Time::Exploded exploded; | |
60 exploded.year = expiration_year_; | |
61 exploded.month = expiration_month_; | |
62 exploded.day_of_week = 0; // Should be unusued. | |
63 exploded.day_of_month = expiration_day_of_month_; | |
64 exploded.hour = 0; | |
65 exploded.minute = 0; | |
66 exploded.second = 0; | |
67 exploded.millisecond = 0; | |
68 | |
69 base::Time expiration_time = Time::FromLocalExploded(exploded); | |
jar (doing other things)
2011/01/18 19:39:44
If we were to save anything (rather than expiratio
rtenneti
2011/01/19 01:45:28
Done.
| |
70 disable_field_trial_ = (GetBuildTime() > expiration_time) ? true : false; | |
39 } | 71 } |
40 | 72 |
41 int FieldTrial::AppendGroup(const std::string& name, | 73 int FieldTrial::AppendGroup(const std::string& name, |
42 Probability group_probability) { | 74 Probability group_probability) { |
43 DCHECK(group_probability <= divisor_); | 75 DCHECK(group_probability <= divisor_); |
44 DCHECK(group_probability >=0 || | 76 DCHECK_GE(group_probability, 0); |
45 group_probability == kAllRemainingProbability); | 77 |
46 if (group_probability == kAllRemainingProbability) { | 78 if (enable_benchmarking_ || disable_field_trial_) |
47 accumulated_group_probability_ = divisor_; | 79 group_probability = 0; |
48 } else { | 80 |
49 if (enable_benchmarking_) | 81 accumulated_group_probability_ += group_probability; |
50 group_probability = 0; | 82 |
51 accumulated_group_probability_ += group_probability; | |
52 } | |
53 DCHECK(accumulated_group_probability_ <= divisor_); | 83 DCHECK(accumulated_group_probability_ <= divisor_); |
54 if (group_ == kNotParticipating && accumulated_group_probability_ > random_) { | 84 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) { |
55 // This is the group that crossed the random line, so we do the assignment. | 85 // This is the group that crossed the random line, so we do the assignment. |
56 group_ = next_group_number_; | 86 group_ = next_group_number_; |
57 if (name.empty()) | 87 if (name.empty()) |
58 base::StringAppendF(&group_name_, "%d", group_); | 88 base::StringAppendF(&group_name_, "%d", group_); |
59 else | 89 else |
60 group_name_ = name; | 90 group_name_ = name; |
61 } | 91 } |
62 return next_group_number_++; | 92 return next_group_number_++; |
63 } | 93 } |
64 | 94 |
95 int FieldTrial::group() { | |
96 if (group_ == kNotFinalized) { | |
97 accumulated_group_probability_ = divisor_; | |
98 group_ = kDefaultGroupNumber; | |
99 group_name_ = default_group_name_; | |
100 } | |
101 return group_; | |
102 } | |
103 | |
104 std::string FieldTrial::group_name() { | |
105 group(); // call group() to make group assignment was done. | |
106 return group_name_; | |
107 } | |
108 | |
65 // static | 109 // static |
66 std::string FieldTrial::MakeName(const std::string& name_prefix, | 110 std::string FieldTrial::MakeName(const std::string& name_prefix, |
67 const std::string& trial_name) { | 111 const std::string& trial_name) { |
68 std::string big_string(name_prefix); | 112 std::string big_string(name_prefix); |
69 big_string.append(1, kHistogramFieldTrialSeparator); | 113 big_string.append(1, kHistogramFieldTrialSeparator); |
70 return big_string.append(FieldTrialList::FindFullName(trial_name)); | 114 return big_string.append(FieldTrialList::FindFullName(trial_name)); |
71 } | 115 } |
72 | 116 |
73 // static | 117 // static |
74 void FieldTrial::EnableBenchmarking() { | 118 void FieldTrial::EnableBenchmarking() { |
75 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); | 119 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); |
76 enable_benchmarking_ = true; | 120 enable_benchmarking_ = true; |
77 } | 121 } |
78 | 122 |
123 // static | |
124 Time FieldTrial::GetBuildTime() { | |
125 Time integral_build_time; | |
126 const char* kDateTime = __DATE__ " " __TIME__; | |
127 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), | |
128 &integral_build_time); | |
129 DCHECK(result); | |
130 return integral_build_time; | |
131 } | |
132 | |
79 FieldTrial::~FieldTrial() {} | 133 FieldTrial::~FieldTrial() {} |
80 | 134 |
81 //------------------------------------------------------------------------------ | 135 //------------------------------------------------------------------------------ |
82 // FieldTrialList methods and members. | 136 // FieldTrialList methods and members. |
83 | 137 |
84 // static | 138 // static |
85 FieldTrialList* FieldTrialList::global_ = NULL; | 139 FieldTrialList* FieldTrialList::global_ = NULL; |
86 | 140 |
87 // static | 141 // static |
88 bool FieldTrialList::register_without_global_ = false; | 142 bool FieldTrialList::register_without_global_ = false; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 return NULL; | 176 return NULL; |
123 AutoLock auto_lock(global_->lock_); | 177 AutoLock auto_lock(global_->lock_); |
124 return global_->PreLockedFind(name); | 178 return global_->PreLockedFind(name); |
125 } | 179 } |
126 | 180 |
127 // static | 181 // static |
128 int FieldTrialList::FindValue(const std::string& name) { | 182 int FieldTrialList::FindValue(const std::string& name) { |
129 FieldTrial* field_trial = Find(name); | 183 FieldTrial* field_trial = Find(name); |
130 if (field_trial) | 184 if (field_trial) |
131 return field_trial->group(); | 185 return field_trial->group(); |
132 return FieldTrial::kNotParticipating; | 186 return FieldTrial::kNotFinalized; |
133 } | 187 } |
134 | 188 |
135 // static | 189 // static |
136 std::string FieldTrialList::FindFullName(const std::string& name) { | 190 std::string FieldTrialList::FindFullName(const std::string& name) { |
137 FieldTrial* field_trial = Find(name); | 191 FieldTrial* field_trial = Find(name); |
138 if (field_trial) | 192 if (field_trial) |
139 return field_trial->group_name(); | 193 return field_trial->group_name(); |
140 return ""; | 194 return ""; |
141 } | 195 } |
142 | 196 |
143 // static | 197 // static |
144 void FieldTrialList::StatesToString(std::string* output) { | 198 void FieldTrialList::StatesToString(std::string* output) { |
145 if (!global_) | 199 if (!global_) |
146 return; | 200 return; |
147 DCHECK(output->empty()); | 201 DCHECK(output->empty()); |
148 AutoLock auto_lock(global_->lock_); | 202 AutoLock auto_lock(global_->lock_); |
149 for (RegistrationList::iterator it = global_->registered_.begin(); | 203 for (RegistrationList::iterator it = global_->registered_.begin(); |
150 it != global_->registered_.end(); ++it) { | 204 it != global_->registered_.end(); ++it) { |
151 const std::string name = it->first; | 205 const std::string name = it->first; |
152 const std::string group_name = it->second->group_name(); | 206 const std::string group_name = it->second->group_name_internal(); |
153 if (group_name.empty()) | 207 if (group_name.empty()) |
154 continue; // No definitive winner in this trial. | 208 continue; // No definitive winner in this trial. |
jar (doing other things)
2011/01/18 19:39:44
This method is used to serialize fieldTrial settin
rtenneti
2011/01/19 01:45:28
Done.
| |
155 DCHECK_EQ(name.find(kPersistentStringSeparator), std::string::npos); | 209 DCHECK_EQ(name.find(kPersistentStringSeparator), std::string::npos); |
156 DCHECK_EQ(group_name.find(kPersistentStringSeparator), std::string::npos); | 210 DCHECK_EQ(group_name.find(kPersistentStringSeparator), std::string::npos); |
157 output->append(name); | 211 output->append(name); |
158 output->append(1, kPersistentStringSeparator); | 212 output->append(1, kPersistentStringSeparator); |
159 output->append(group_name); | 213 output->append(group_name); |
160 output->append(1, kPersistentStringSeparator); | 214 output->append(1, kPersistentStringSeparator); |
161 } | 215 } |
162 } | 216 } |
163 | 217 |
164 // static | 218 // static |
165 bool FieldTrialList::StringAugmentsState(const std::string& prior_state) { | 219 bool FieldTrialList::StringAugmentsState(const std::string& prior_state) { |
jar (doing other things)
2011/01/18 19:39:44
nit: Again, my fault. this name is poor IMO. I h
rtenneti
2011/01/19 01:45:28
Done.
| |
166 DCHECK(global_); | 220 DCHECK(global_); |
167 if (prior_state.empty() || !global_) | 221 if (prior_state.empty() || !global_) |
168 return true; | 222 return true; |
169 | 223 |
170 size_t next_item = 0; | 224 size_t next_item = 0; |
171 while (next_item < prior_state.length()) { | 225 while (next_item < prior_state.length()) { |
172 size_t name_end = prior_state.find(kPersistentStringSeparator, next_item); | 226 size_t name_end = prior_state.find(kPersistentStringSeparator, next_item); |
173 if (name_end == prior_state.npos || next_item == name_end) | 227 if (name_end == prior_state.npos || next_item == name_end) |
174 return false; | 228 return false; |
175 size_t group_name_end = prior_state.find(kPersistentStringSeparator, | 229 size_t group_name_end = prior_state.find(kPersistentStringSeparator, |
176 name_end + 1); | 230 name_end + 1); |
177 if (group_name_end == prior_state.npos || name_end + 1 == group_name_end) | 231 if (group_name_end == prior_state.npos || name_end + 1 == group_name_end) |
178 return false; | 232 return false; |
179 std::string name(prior_state, next_item, name_end - next_item); | 233 std::string name(prior_state, next_item, name_end - next_item); |
180 std::string group_name(prior_state, name_end + 1, | 234 std::string group_name(prior_state, name_end + 1, |
181 group_name_end - name_end - 1); | 235 group_name_end - name_end - 1); |
182 next_item = group_name_end + 1; | 236 next_item = group_name_end + 1; |
183 | 237 |
184 FieldTrial *field_trial(FieldTrialList::Find(name)); | 238 FieldTrial *field_trial(FieldTrialList::Find(name)); |
185 if (field_trial) { | 239 if (field_trial) { |
186 // In single process mode, we may have already created the field trial. | 240 // In single process mode, we may have already created the field trial. |
187 if (field_trial->group_name() != group_name) | 241 if (field_trial->group_name_internal() != group_name) |
188 return false; | 242 return false; |
189 continue; | 243 continue; |
190 } | 244 } |
191 const int kTotalProbability = 100; | 245 const int kTotalProbability = 100; |
192 field_trial = new FieldTrial(name, kTotalProbability); | 246 field_trial = |
247 new FieldTrial(name, kTotalProbability, group_name, 2011, 6, 30); | |
jar (doing other things)
2011/01/18 19:39:44
That code won't define field trials in the rendere
rtenneti
2011/01/19 01:45:28
Done.
| |
193 field_trial->AppendGroup(group_name, kTotalProbability); | 248 field_trial->AppendGroup(group_name, kTotalProbability); |
194 } | 249 } |
195 return true; | 250 return true; |
196 } | 251 } |
197 | 252 |
198 // static | 253 // static |
199 size_t FieldTrialList::GetFieldTrialCount() { | 254 size_t FieldTrialList::GetFieldTrialCount() { |
200 if (!global_) | 255 if (!global_) |
201 return 0; | 256 return 0; |
202 AutoLock auto_lock(global_->lock_); | 257 AutoLock auto_lock(global_->lock_); |
203 return global_->registered_.size(); | 258 return global_->registered_.size(); |
204 } | 259 } |
205 | 260 |
206 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { | 261 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { |
207 RegistrationList::iterator it = registered_.find(name); | 262 RegistrationList::iterator it = registered_.find(name); |
208 if (registered_.end() == it) | 263 if (registered_.end() == it) |
209 return NULL; | 264 return NULL; |
210 return it->second; | 265 return it->second; |
211 } | 266 } |
212 | 267 |
213 } // namespace base | 268 } // namespace base |
OLD | NEW |