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; | |
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), | |
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); | |
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 | |
102 return group_; | |
103 } | |
104 | |
65 // static | 105 // static |
66 std::string FieldTrial::MakeName(const std::string& name_prefix, | 106 std::string FieldTrial::MakeName(const std::string& name_prefix, |
67 const std::string& trial_name) { | 107 const std::string& trial_name) { |
68 std::string big_string(name_prefix); | 108 std::string big_string(name_prefix); |
69 big_string.append(1, kHistogramFieldTrialSeparator); | 109 big_string.append(1, kHistogramFieldTrialSeparator); |
70 return big_string.append(FieldTrialList::FindFullName(trial_name)); | 110 return big_string.append(FieldTrialList::FindFullName(trial_name)); |
71 } | 111 } |
72 | 112 |
73 // static | 113 // static |
74 void FieldTrial::EnableBenchmarking() { | 114 void FieldTrial::EnableBenchmarking() { |
75 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); | 115 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); |
76 enable_benchmarking_ = true; | 116 enable_benchmarking_ = true; |
77 } | 117 } |
78 | 118 |
119 // static | |
120 Time FieldTrial::GetBuildTime() { | |
121 static Time integral_build_time; | |
jar (doing other things)
2011/01/15 20:52:47
Don't bother caching this. There may be a race on
| |
122 if (integral_build_time.is_null()) { | |
123 const char* kDateTime = __DATE__ " " __TIME__; | |
124 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), | |
125 &integral_build_time); | |
126 DCHECK(result); | |
127 } | |
128 return integral_build_time; | |
129 } | |
130 | |
79 FieldTrial::~FieldTrial() {} | 131 FieldTrial::~FieldTrial() {} |
80 | 132 |
81 //------------------------------------------------------------------------------ | 133 //------------------------------------------------------------------------------ |
82 // FieldTrialList methods and members. | 134 // FieldTrialList methods and members. |
83 | 135 |
84 // static | 136 // static |
85 FieldTrialList* FieldTrialList::global_ = NULL; | 137 FieldTrialList* FieldTrialList::global_ = NULL; |
86 | 138 |
87 // static | 139 // static |
88 bool FieldTrialList::register_without_global_ = false; | 140 bool FieldTrialList::register_without_global_ = false; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 return NULL; | 174 return NULL; |
123 AutoLock auto_lock(global_->lock_); | 175 AutoLock auto_lock(global_->lock_); |
124 return global_->PreLockedFind(name); | 176 return global_->PreLockedFind(name); |
125 } | 177 } |
126 | 178 |
127 // static | 179 // static |
128 int FieldTrialList::FindValue(const std::string& name) { | 180 int FieldTrialList::FindValue(const std::string& name) { |
129 FieldTrial* field_trial = Find(name); | 181 FieldTrial* field_trial = Find(name); |
130 if (field_trial) | 182 if (field_trial) |
131 return field_trial->group(); | 183 return field_trial->group(); |
132 return FieldTrial::kNotParticipating; | 184 return FieldTrial::kNotFinalized; |
133 } | 185 } |
134 | 186 |
135 // static | 187 // static |
136 std::string FieldTrialList::FindFullName(const std::string& name) { | 188 std::string FieldTrialList::FindFullName(const std::string& name) { |
137 FieldTrial* field_trial = Find(name); | 189 FieldTrial* field_trial = Find(name); |
138 if (field_trial) | 190 if (field_trial) |
139 return field_trial->group_name(); | 191 return field_trial->group_name(); |
140 return ""; | 192 return ""; |
141 } | 193 } |
142 | 194 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 next_item = group_name_end + 1; | 234 next_item = group_name_end + 1; |
183 | 235 |
184 FieldTrial *field_trial(FieldTrialList::Find(name)); | 236 FieldTrial *field_trial(FieldTrialList::Find(name)); |
185 if (field_trial) { | 237 if (field_trial) { |
186 // In single process mode, we may have already created the field trial. | 238 // In single process mode, we may have already created the field trial. |
187 if (field_trial->group_name() != group_name) | 239 if (field_trial->group_name() != group_name) |
188 return false; | 240 return false; |
189 continue; | 241 continue; |
190 } | 242 } |
191 const int kTotalProbability = 100; | 243 const int kTotalProbability = 100; |
192 field_trial = new FieldTrial(name, kTotalProbability); | 244 field_trial = |
193 field_trial->AppendGroup(group_name, kTotalProbability); | 245 new FieldTrial(name, kTotalProbability, group_name, 2099, 12, 31); |
jar (doing other things)
2011/01/15 20:52:47
This constant is a little dangerous on two account
| |
246 field_trial->group(); | |
194 } | 247 } |
195 return true; | 248 return true; |
196 } | 249 } |
197 | 250 |
198 // static | 251 // static |
199 size_t FieldTrialList::GetFieldTrialCount() { | 252 size_t FieldTrialList::GetFieldTrialCount() { |
200 if (!global_) | 253 if (!global_) |
201 return 0; | 254 return 0; |
202 AutoLock auto_lock(global_->lock_); | 255 AutoLock auto_lock(global_->lock_); |
203 return global_->registered_.size(); | 256 return global_->registered_.size(); |
204 } | 257 } |
205 | 258 |
206 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { | 259 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { |
207 RegistrationList::iterator it = registered_.find(name); | 260 RegistrationList::iterator it = registered_.find(name); |
208 if (registered_.end() == it) | 261 if (registered_.end() == it) |
209 return NULL; | 262 return NULL; |
210 return it->second; | 263 return it->second; |
211 } | 264 } |
212 | 265 |
213 } // namespace base | 266 } // namespace base |
OLD | NEW |