Index: base/metrics/field_trial.cc |
=================================================================== |
--- base/metrics/field_trial.cc (revision 118842) |
+++ base/metrics/field_trial.cc (working copy) |
@@ -42,6 +42,7 @@ |
const int month, |
const int day_of_month) |
: name_(name), |
+ name_hash_(HashName(name)), |
divisor_(total_probability), |
default_group_name_(default_group_name), |
random_(static_cast<Probability>(divisor_ * RandDouble())), |
@@ -96,6 +97,7 @@ |
if (group_ != kNotFinalized) { |
group_ = kDefaultGroupNumber; |
group_name_ = default_group_name_; |
+ group_name_hash_ = HashName(group_name_); |
} |
} |
@@ -117,6 +119,7 @@ |
StringAppendF(&group_name_, "%d", group_); |
else |
group_name_ = name; |
+ group_name_hash_ = HashName(group_name_); |
FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_); |
} |
return next_group_number_++; |
@@ -127,6 +130,7 @@ |
accumulated_group_probability_ = divisor_; |
group_ = kDefaultGroupNumber; |
group_name_ = default_group_name_; |
+ group_name_hash_ = HashName(group_name_); |
FieldTrialList::NotifyFieldTrialGroupSelection(name_, group_name_); |
} |
return group_; |
@@ -138,6 +142,14 @@ |
return group_name_; |
} |
+bool FieldTrial::GetNameGroupId(NameGroupId* name_group_id) { |
+ 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.
|
+ return false; |
+ name_group_id->name = name_hash_; |
+ name_group_id->group = group_name_hash_; |
+ return true; |
+} |
+ |
// static |
std::string FieldTrial::MakeName(const std::string& name_prefix, |
const std::string& trial_name) { |
@@ -173,6 +185,21 @@ |
return BitsToOpenEndedUnitInterval(*bits); |
} |
+// static |
+uint32 FieldTrial::HashName(const std::string& name) { |
+ // SHA-1 is designed to produce a uniformly random spread in its output space, |
+ // even for nearly-identical name. |
+ unsigned char sha1_hash[kSHA1Length]; |
+ SHA1HashBytes(reinterpret_cast<const unsigned char*>(name.c_str()), |
+ name.size(), |
+ sha1_hash); |
+ |
+ COMPILE_ASSERT(sizeof(uint32) < sizeof(sha1_hash), need_more_data); |
+ uint32* bits = reinterpret_cast<uint32*>(&sha1_hash[0]); |
+ |
+ return *bits; |
+} |
+ |
//------------------------------------------------------------------------------ |
// FieldTrialList methods and members. |
@@ -272,6 +299,22 @@ |
} |
// static |
+void FieldTrialList::GetFieldTrialNameGroupIds( |
+ std::vector<FieldTrial::NameGroupId>* name_group_ids) { |
+ if (!global_) |
+ return; |
+ DCHECK(name_group_ids->empty()); |
+ AutoLock auto_lock(global_->lock_); |
+ |
+ for (RegistrationList::iterator it = global_->registered_.begin(); |
+ it != global_->registered_.end(); ++it) { |
+ FieldTrial::NameGroupId name_group_id; |
+ if (it->second->GetNameGroupId(&name_group_id)) |
+ name_group_ids->push_back(name_group_id); |
+ } |
+} |
+ |
+// static |
bool FieldTrialList::CreateTrialsInChildProcess( |
const std::string& parent_trials) { |
DCHECK(global_); |