This source file includes following definitions.
- CreateTimeFromParams
- GetGroupBoundaryValue
- Disable
- AppendGroup
- group
- group_name
- SetForced
- EnableBenchmarking
- CreateSimulatedFieldTrial
- trial_registered_
- SetTrialRegistered
- SetGroupChoice
- FinalizeGroupChoice
- GetActiveGroup
- observer_list_
- FactoryGetFieldTrial
- FactoryGetFieldTrialWithRandomizationSeed
- Find
- FindValue
- FindFullName
- TrialExists
- StatesToString
- GetActiveFieldTrialGroups
- CreateTrialsFromString
- CreateFieldTrial
- AddObserver
- RemoveObserver
- NotifyFieldTrialGroupSelection
- GetFieldTrialCount
- GetEntropyProviderForOneTimeRandomization
- PreLockedFind
- Register
#include "base/metrics/field_trial.h"
#include <algorithm>
#include "base/build_time.h"
#include "base/logging.h"
#include "base/rand_util.h"
#include "base/sha1.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/sys_byteorder.h"
namespace base {
namespace {
Time CreateTimeFromParams(int year, int month, int day_of_month) {
DCHECK_GT(year, 1970);
DCHECK_GT(month, 0);
DCHECK_LT(month, 13);
DCHECK_GT(day_of_month, 0);
DCHECK_LT(day_of_month, 32);
Time::Exploded exploded;
exploded.year = year;
exploded.month = month;
exploded.day_of_week = 0;
exploded.day_of_month = day_of_month;
exploded.hour = 0;
exploded.minute = 0;
exploded.second = 0;
exploded.millisecond = 0;
return Time::FromLocalExploded(exploded);
}
FieldTrial::Probability GetGroupBoundaryValue(
FieldTrial::Probability divisor,
double entropy_value) {
const double kEpsilon = 1e-8;
const FieldTrial::Probability result =
static_cast<FieldTrial::Probability>(divisor * entropy_value + kEpsilon);
return std::min(result, divisor - 1);
}
}
const int FieldTrial::kNotFinalized = -1;
const int FieldTrial::kDefaultGroupNumber = 0;
bool FieldTrial::enable_benchmarking_ = false;
const char FieldTrialList::kPersistentStringSeparator('/');
int FieldTrialList::kNoExpirationYear = 0;
FieldTrial::EntropyProvider::~EntropyProvider() {
}
void FieldTrial::Disable() {
DCHECK(!group_reported_);
enable_field_trial_ = false;
if (group_ != kNotFinalized) {
if (group_name_ != default_group_name_)
SetGroupChoice(default_group_name_, kDefaultGroupNumber);
}
}
int FieldTrial::AppendGroup(const std::string& name,
Probability group_probability) {
if (forced_) {
DCHECK(!group_name_.empty());
if (name == group_name_) {
return group_;
}
DCHECK_NE(next_group_number_, group_);
return next_group_number_++;
}
DCHECK_LE(group_probability, divisor_);
DCHECK_GE(group_probability, 0);
if (enable_benchmarking_ || !enable_field_trial_)
group_probability = 0;
accumulated_group_probability_ += group_probability;
DCHECK_LE(accumulated_group_probability_, divisor_);
if (group_ == kNotFinalized && accumulated_group_probability_ > random_) {
SetGroupChoice(name, next_group_number_);
}
return next_group_number_++;
}
int FieldTrial::group() {
FinalizeGroupChoice();
if (trial_registered_)
FieldTrialList::NotifyFieldTrialGroupSelection(this);
return group_;
}
const std::string& FieldTrial::group_name() {
group();
DCHECK(!group_name_.empty());
return group_name_;
}
void FieldTrial::SetForced() {
if (forced_)
return;
FinalizeGroupChoice();
forced_ = true;
}
void FieldTrial::EnableBenchmarking() {
DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount());
enable_benchmarking_ = true;
}
FieldTrial* FieldTrial::CreateSimulatedFieldTrial(
const std::string& trial_name,
Probability total_probability,
const std::string& default_group_name,
double entropy_value) {
return new FieldTrial(trial_name, total_probability, default_group_name,
entropy_value);
}
FieldTrial::FieldTrial(const std::string& trial_name,
const Probability total_probability,
const std::string& default_group_name,
double entropy_value)
: trial_name_(trial_name),
divisor_(total_probability),
default_group_name_(default_group_name),
random_(GetGroupBoundaryValue(total_probability, entropy_value)),
accumulated_group_probability_(0),
next_group_number_(kDefaultGroupNumber + 1),
group_(kNotFinalized),
enable_field_trial_(true),
forced_(false),
group_reported_(false),
trial_registered_(false) {
DCHECK_GT(total_probability, 0);
DCHECK(!trial_name_.empty());
DCHECK(!default_group_name_.empty());
}
FieldTrial::~FieldTrial() {}
void FieldTrial::SetTrialRegistered() {
DCHECK_EQ(kNotFinalized, group_);
DCHECK(!trial_registered_);
trial_registered_ = true;
}
void FieldTrial::SetGroupChoice(const std::string& group_name, int number) {
group_ = number;
if (group_name.empty())
StringAppendF(&group_name_, "%d", group_);
else
group_name_ = group_name;
DVLOG(1) << "Field trial: " << trial_name_ << " Group choice:" << group_name_;
}
void FieldTrial::FinalizeGroupChoice() {
if (group_ != kNotFinalized)
return;
accumulated_group_probability_ = divisor_;
DCHECK(!forced_);
SetGroupChoice(default_group_name_, kDefaultGroupNumber);
}
bool FieldTrial::GetActiveGroup(ActiveGroup* active_group) const {
if (!group_reported_ || !enable_field_trial_)
return false;
DCHECK_NE(group_, kNotFinalized);
active_group->trial_name = trial_name_;
active_group->group_name = group_name_;
return true;
}
FieldTrialList* FieldTrialList::global_ = NULL;
bool FieldTrialList::used_without_global_ = false;
FieldTrialList::Observer::~Observer() {
}
FieldTrialList::FieldTrialList(
const FieldTrial::EntropyProvider* entropy_provider)
: entropy_provider_(entropy_provider),
observer_list_(new ObserverListThreadSafe<FieldTrialList::Observer>(
ObserverListBase<FieldTrialList::Observer>::NOTIFY_EXISTING_ONLY)) {
DCHECK(!global_);
DCHECK(!used_without_global_);
global_ = this;
Time two_years_from_build_time = GetBuildTime() + TimeDelta::FromDays(730);
Time::Exploded exploded;
two_years_from_build_time.LocalExplode(&exploded);
kNoExpirationYear = exploded.year;
}
FieldTrialList::~FieldTrialList() {
AutoLock auto_lock(lock_);
while (!registered_.empty()) {
RegistrationMap::iterator it = registered_.begin();
it->second->Release();
registered_.erase(it->first);
}
DCHECK_EQ(this, global_);
global_ = NULL;
}
FieldTrial* FieldTrialList::FactoryGetFieldTrial(
const std::string& trial_name,
FieldTrial::Probability total_probability,
const std::string& default_group_name,
const int year,
const int month,
const int day_of_month,
FieldTrial::RandomizationType randomization_type,
int* default_group_number) {
return FactoryGetFieldTrialWithRandomizationSeed(
trial_name, total_probability, default_group_name,
year, month, day_of_month, randomization_type, 0, default_group_number);
}
FieldTrial* FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed(
const std::string& trial_name,
FieldTrial::Probability total_probability,
const std::string& default_group_name,
const int year,
const int month,
const int day_of_month,
FieldTrial::RandomizationType randomization_type,
uint32 randomization_seed,
int* default_group_number) {
if (default_group_number)
*default_group_number = FieldTrial::kDefaultGroupNumber;
FieldTrial* existing_trial = Find(trial_name);
if (existing_trial) {
CHECK(existing_trial->forced_);
if (default_group_number &&
default_group_name != existing_trial->default_group_name()) {
if (default_group_name == existing_trial->group_name_internal()) {
*default_group_number = existing_trial->group_;
} else {
const int kNonConflictingGroupNumber = -2;
COMPILE_ASSERT(
kNonConflictingGroupNumber != FieldTrial::kDefaultGroupNumber,
conflicting_default_group_number);
COMPILE_ASSERT(
kNonConflictingGroupNumber != FieldTrial::kNotFinalized,
conflicting_default_group_number);
*default_group_number = kNonConflictingGroupNumber;
}
}
return existing_trial;
}
double entropy_value;
if (randomization_type == FieldTrial::ONE_TIME_RANDOMIZED) {
entropy_value = GetEntropyProviderForOneTimeRandomization()->
GetEntropyForTrial(trial_name, randomization_seed);
} else {
DCHECK_EQ(FieldTrial::SESSION_RANDOMIZED, randomization_type);
DCHECK_EQ(0U, randomization_seed);
entropy_value = RandDouble();
}
FieldTrial* field_trial = new FieldTrial(trial_name, total_probability,
default_group_name, entropy_value);
if (GetBuildTime() > CreateTimeFromParams(year, month, day_of_month))
field_trial->Disable();
FieldTrialList::Register(field_trial);
return field_trial;
}
FieldTrial* FieldTrialList::Find(const std::string& name) {
if (!global_)
return NULL;
AutoLock auto_lock(global_->lock_);
return global_->PreLockedFind(name);
}
int FieldTrialList::FindValue(const std::string& name) {
FieldTrial* field_trial = Find(name);
if (field_trial)
return field_trial->group();
return FieldTrial::kNotFinalized;
}
std::string FieldTrialList::FindFullName(const std::string& name) {
FieldTrial* field_trial = Find(name);
if (field_trial)
return field_trial->group_name();
return std::string();
}
bool FieldTrialList::TrialExists(const std::string& name) {
return Find(name) != NULL;
}
void FieldTrialList::StatesToString(std::string* output) {
FieldTrial::ActiveGroups active_groups;
GetActiveFieldTrialGroups(&active_groups);
for (FieldTrial::ActiveGroups::const_iterator it = active_groups.begin();
it != active_groups.end(); ++it) {
DCHECK_EQ(std::string::npos,
it->trial_name.find(kPersistentStringSeparator));
DCHECK_EQ(std::string::npos,
it->group_name.find(kPersistentStringSeparator));
output->append(it->trial_name);
output->append(1, kPersistentStringSeparator);
output->append(it->group_name);
output->append(1, kPersistentStringSeparator);
}
}
void FieldTrialList::GetActiveFieldTrialGroups(
FieldTrial::ActiveGroups* active_groups) {
DCHECK(active_groups->empty());
if (!global_)
return;
AutoLock auto_lock(global_->lock_);
for (RegistrationMap::iterator it = global_->registered_.begin();
it != global_->registered_.end(); ++it) {
FieldTrial::ActiveGroup active_group;
if (it->second->GetActiveGroup(&active_group))
active_groups->push_back(active_group);
}
}
bool FieldTrialList::CreateTrialsFromString(
const std::string& trials_string,
FieldTrialActivationMode mode,
const std::set<std::string>& ignored_trial_names) {
DCHECK(global_);
if (trials_string.empty() || !global_)
return true;
size_t next_item = 0;
while (next_item < trials_string.length()) {
size_t name_end = trials_string.find(kPersistentStringSeparator, next_item);
if (name_end == trials_string.npos || next_item == name_end)
return false;
size_t group_name_end = trials_string.find(kPersistentStringSeparator,
name_end + 1);
if (group_name_end == trials_string.npos || name_end + 1 == group_name_end)
return false;
std::string name(trials_string, next_item, name_end - next_item);
std::string group_name(trials_string, name_end + 1,
group_name_end - name_end - 1);
next_item = group_name_end + 1;
if (ignored_trial_names.find(name) != ignored_trial_names.end())
continue;
FieldTrial* trial = CreateFieldTrial(name, group_name);
if (!trial)
return false;
if (mode == ACTIVATE_TRIALS) {
trial->group();
}
}
return true;
}
FieldTrial* FieldTrialList::CreateFieldTrial(
const std::string& name,
const std::string& group_name) {
DCHECK(global_);
DCHECK_GE(name.size(), 0u);
DCHECK_GE(group_name.size(), 0u);
if (name.empty() || group_name.empty() || !global_)
return NULL;
FieldTrial* field_trial = FieldTrialList::Find(name);
if (field_trial) {
if (field_trial->group_name_internal() != group_name)
return NULL;
return field_trial;
}
const int kTotalProbability = 100;
field_trial = new FieldTrial(name, kTotalProbability, group_name, 0);
FieldTrialList::Register(field_trial);
field_trial->SetForced();
return field_trial;
}
void FieldTrialList::AddObserver(Observer* observer) {
if (!global_)
return;
global_->observer_list_->AddObserver(observer);
}
void FieldTrialList::RemoveObserver(Observer* observer) {
if (!global_)
return;
global_->observer_list_->RemoveObserver(observer);
}
void FieldTrialList::NotifyFieldTrialGroupSelection(FieldTrial* field_trial) {
if (!global_)
return;
{
AutoLock auto_lock(global_->lock_);
if (field_trial->group_reported_)
return;
field_trial->group_reported_ = true;
}
if (!field_trial->enable_field_trial_)
return;
global_->observer_list_->Notify(
&FieldTrialList::Observer::OnFieldTrialGroupFinalized,
field_trial->trial_name(),
field_trial->group_name_internal());
}
size_t FieldTrialList::GetFieldTrialCount() {
if (!global_)
return 0;
AutoLock auto_lock(global_->lock_);
return global_->registered_.size();
}
const FieldTrial::EntropyProvider*
FieldTrialList::GetEntropyProviderForOneTimeRandomization() {
if (!global_) {
used_without_global_ = true;
return NULL;
}
return global_->entropy_provider_.get();
}
FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) {
RegistrationMap::iterator it = registered_.find(name);
if (registered_.end() == it)
return NULL;
return it->second;
}
void FieldTrialList::Register(FieldTrial* trial) {
if (!global_) {
used_without_global_ = true;
return;
}
AutoLock auto_lock(global_->lock_);
DCHECK(!global_->PreLockedFind(trial->trial_name()));
trial->AddRef();
trial->SetTrialRegistered();
global_->registered_[trial->trial_name()] = trial;
}
}