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 Initialize(year, month, day_of_month); |
| 48 DCHECK(!default_group_name_.empty()); |
| 49 } |
| 50 |
| 51 // The following is a private constructor used for testing. |
| 52 FieldTrial::FieldTrial(const std::string& name, |
| 53 const Probability total_probability, |
| 54 const std::string& default_group_name) |
| 55 : name_(name), |
| 56 divisor_(total_probability), |
| 57 default_group_name_(default_group_name), |
| 58 random_(static_cast<Probability>(divisor_ * base::RandDouble())), |
| 59 accumulated_group_probability_(0), |
| 60 next_group_number_(kDefaultGroupNumber+1), |
| 61 group_(kNotFinalized) { |
| 62 Initialize(2099, 12, 31); |
| 63 DCHECK(!default_group_name_.empty()); |
| 64 } |
| 65 |
| 66 void FieldTrial::Initialize(const int year, |
| 67 const int month, |
| 68 const int day_of_month) { |
38 FieldTrialList::Register(this); | 69 FieldTrialList::Register(this); |
| 70 |
| 71 DCHECK_GT(year, 1970); |
| 72 DCHECK_GT(month, 0); |
| 73 DCHECK_LT(month, 13); |
| 74 DCHECK_GT(day_of_month, 0); |
| 75 DCHECK_LT(day_of_month, 32); |
| 76 |
| 77 base::Time::Exploded exploded; |
| 78 exploded.year = year; |
| 79 exploded.month = month; |
| 80 exploded.day_of_week = 0; // Should be unusued. |
| 81 exploded.day_of_month = day_of_month; |
| 82 exploded.hour = 0; |
| 83 exploded.minute = 0; |
| 84 exploded.second = 0; |
| 85 exploded.millisecond = 0; |
| 86 |
| 87 expiration_time_ = Time::FromLocalExploded(exploded); |
| 88 disable_field_trial_ = (GetBuildTime() > expiration_time_) ? true : false; |
39 } | 89 } |
40 | 90 |
41 int FieldTrial::AppendGroup(const std::string& name, | 91 int FieldTrial::AppendGroup(const std::string& name, |
42 Probability group_probability) { | 92 Probability group_probability) { |
43 DCHECK(group_probability <= divisor_); | 93 DCHECK(group_probability <= divisor_); |
44 DCHECK(group_probability >=0 || | 94 DCHECK_GE(group_probability, 0); |
45 group_probability == kAllRemainingProbability); | 95 |
46 if (group_probability == kAllRemainingProbability) { | 96 if (enable_benchmarking_ || disable_field_trial_) |
47 accumulated_group_probability_ = divisor_; | 97 group_probability = 0; |
48 } else { | 98 |
49 if (enable_benchmarking_) | 99 accumulated_group_probability_ += group_probability; |
50 group_probability = 0; | 100 |
51 accumulated_group_probability_ += group_probability; | |
52 } | |
53 DCHECK(accumulated_group_probability_ <= divisor_); | 101 DCHECK(accumulated_group_probability_ <= divisor_); |
54 if (group_ == kNotParticipating && accumulated_group_probability_ > random_) { | 102 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) { |
55 // This is the group that crossed the random line, so we do the assignment. | 103 // This is the group that crossed the random line, so we do the assignment. |
56 group_ = next_group_number_; | 104 group_ = next_group_number_; |
57 if (name.empty()) | 105 if (name.empty()) |
58 base::StringAppendF(&group_name_, "%d", group_); | 106 base::StringAppendF(&group_name_, "%d", group_); |
59 else | 107 else |
60 group_name_ = name; | 108 group_name_ = name; |
61 } | 109 } |
62 return next_group_number_++; | 110 return next_group_number_++; |
63 } | 111 } |
64 | 112 |
| 113 int FieldTrial::group() { |
| 114 if (group_ == kNotFinalized) { |
| 115 accumulated_group_probability_ = divisor_; |
| 116 group_ = kDefaultGroupNumber; |
| 117 group_name_ = default_group_name_; |
| 118 } |
| 119 |
| 120 return group_; |
| 121 } |
| 122 |
65 // static | 123 // static |
66 std::string FieldTrial::MakeName(const std::string& name_prefix, | 124 std::string FieldTrial::MakeName(const std::string& name_prefix, |
67 const std::string& trial_name) { | 125 const std::string& trial_name) { |
68 std::string big_string(name_prefix); | 126 std::string big_string(name_prefix); |
69 big_string.append(1, kHistogramFieldTrialSeparator); | 127 big_string.append(1, kHistogramFieldTrialSeparator); |
70 return big_string.append(FieldTrialList::FindFullName(trial_name)); | 128 return big_string.append(FieldTrialList::FindFullName(trial_name)); |
71 } | 129 } |
72 | 130 |
73 // static | 131 // static |
74 void FieldTrial::EnableBenchmarking() { | 132 void FieldTrial::EnableBenchmarking() { |
75 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); | 133 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); |
76 enable_benchmarking_ = true; | 134 enable_benchmarking_ = true; |
77 } | 135 } |
78 | 136 |
| 137 // static |
| 138 Time FieldTrial::GetBuildTime() { |
| 139 static Time integral_build_time; |
| 140 if (integral_build_time.is_null()) { |
| 141 const char* kDateTime = __DATE__ " " __TIME__; |
| 142 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), |
| 143 &integral_build_time); |
| 144 DCHECK(result); |
| 145 } |
| 146 return integral_build_time; |
| 147 } |
| 148 |
79 FieldTrial::~FieldTrial() {} | 149 FieldTrial::~FieldTrial() {} |
80 | 150 |
81 //------------------------------------------------------------------------------ | 151 //------------------------------------------------------------------------------ |
82 // FieldTrialList methods and members. | 152 // FieldTrialList methods and members. |
83 | 153 |
84 // static | 154 // static |
85 FieldTrialList* FieldTrialList::global_ = NULL; | 155 FieldTrialList* FieldTrialList::global_ = NULL; |
86 | 156 |
87 // static | 157 // static |
88 bool FieldTrialList::register_without_global_ = false; | 158 bool FieldTrialList::register_without_global_ = false; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 return NULL; | 192 return NULL; |
123 AutoLock auto_lock(global_->lock_); | 193 AutoLock auto_lock(global_->lock_); |
124 return global_->PreLockedFind(name); | 194 return global_->PreLockedFind(name); |
125 } | 195 } |
126 | 196 |
127 // static | 197 // static |
128 int FieldTrialList::FindValue(const std::string& name) { | 198 int FieldTrialList::FindValue(const std::string& name) { |
129 FieldTrial* field_trial = Find(name); | 199 FieldTrial* field_trial = Find(name); |
130 if (field_trial) | 200 if (field_trial) |
131 return field_trial->group(); | 201 return field_trial->group(); |
132 return FieldTrial::kNotParticipating; | 202 return FieldTrial::kNotFinalized; |
133 } | 203 } |
134 | 204 |
135 // static | 205 // static |
136 std::string FieldTrialList::FindFullName(const std::string& name) { | 206 std::string FieldTrialList::FindFullName(const std::string& name) { |
137 FieldTrial* field_trial = Find(name); | 207 FieldTrial* field_trial = Find(name); |
138 if (field_trial) | 208 if (field_trial) |
139 return field_trial->group_name(); | 209 return field_trial->group_name(); |
140 return ""; | 210 return ""; |
141 } | 211 } |
142 | 212 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 next_item = group_name_end + 1; | 252 next_item = group_name_end + 1; |
183 | 253 |
184 FieldTrial *field_trial(FieldTrialList::Find(name)); | 254 FieldTrial *field_trial(FieldTrialList::Find(name)); |
185 if (field_trial) { | 255 if (field_trial) { |
186 // In single process mode, we may have already created the field trial. | 256 // In single process mode, we may have already created the field trial. |
187 if (field_trial->group_name() != group_name) | 257 if (field_trial->group_name() != group_name) |
188 return false; | 258 return false; |
189 continue; | 259 continue; |
190 } | 260 } |
191 const int kTotalProbability = 100; | 261 const int kTotalProbability = 100; |
192 field_trial = new FieldTrial(name, kTotalProbability); | 262 field_trial = new FieldTrial(name, kTotalProbability, group_name, 2099, 12, |
193 field_trial->AppendGroup(group_name, kTotalProbability); | 263 31); |
| 264 // The following call finalizes the FieldTrial. |
| 265 field_trial->group(); |
194 } | 266 } |
195 return true; | 267 return true; |
196 } | 268 } |
197 | 269 |
198 // static | 270 // static |
199 size_t FieldTrialList::GetFieldTrialCount() { | 271 size_t FieldTrialList::GetFieldTrialCount() { |
200 if (!global_) | 272 if (!global_) |
201 return 0; | 273 return 0; |
202 AutoLock auto_lock(global_->lock_); | 274 AutoLock auto_lock(global_->lock_); |
203 return global_->registered_.size(); | 275 return global_->registered_.size(); |
204 } | 276 } |
205 | 277 |
206 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { | 278 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { |
207 RegistrationList::iterator it = registered_.find(name); | 279 RegistrationList::iterator it = registered_.find(name); |
208 if (registered_.end() == it) | 280 if (registered_.end() == it) |
209 return NULL; | 281 return NULL; |
210 return it->second; | 282 return it->second; |
211 } | 283 } |
212 | 284 |
213 } // namespace base | 285 } // namespace base |
OLD | NEW |