123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806 |
- #ifndef BASE_METRICS_FIELD_TRIAL_H_
- #define BASE_METRICS_FIELD_TRIAL_H_
- #include <stddef.h>
- #include <stdint.h>
- #include <map>
- #include <memory>
- #include <string>
- #include <vector>
- #include "base/atomicops.h"
- #include "base/base_export.h"
- #include "base/command_line.h"
- #include "base/feature_list.h"
- #include "base/gtest_prod_util.h"
- #include "base/memory/read_only_shared_memory_region.h"
- #include "base/memory/ref_counted.h"
- #include "base/memory/shared_memory_mapping.h"
- #include "base/metrics/persistent_memory_allocator.h"
- #include "base/observer_list_threadsafe.h"
- #include "base/pickle.h"
- #include "base/process/launch.h"
- #include "base/strings/string_piece.h"
- #include "base/synchronization/lock.h"
- #include "build/build_config.h"
- #if defined(OS_MAC)
- #include "base/mac/mach_port_rendezvous.h"
- #endif
- namespace base {
- class FieldTrialList;
- class BASE_EXPORT FieldTrial : public RefCounted<FieldTrial> {
- public:
- typedef int Probability;
-
- enum RandomizationType {
-
-
-
- ONE_TIME_RANDOMIZED,
-
-
- SESSION_RANDOMIZED,
- };
-
-
- class BASE_EXPORT EntropyProvider {
- public:
- virtual ~EntropyProvider();
-
-
-
-
-
- virtual double GetEntropyForTrial(const std::string& trial_name,
- uint32_t randomization_seed) const = 0;
- };
-
- struct ActiveGroup {
- std::string trial_name;
- std::string group_name;
- };
-
-
-
-
- struct BASE_EXPORT State {
- const std::string* trial_name = nullptr;
- const std::string* group_name = nullptr;
- bool activated = false;
- State();
- State(const State& other);
- ~State();
- };
-
-
-
- struct BASE_EXPORT FieldTrialEntry {
-
- static constexpr uint32_t kPersistentTypeId = 0xABA17E13 + 2;
-
- static constexpr size_t kExpectedInstanceSize = 8;
-
-
-
-
-
- subtle::Atomic32 activated;
-
- uint32_t pickle_size;
-
-
-
- bool GetTrialAndGroupName(StringPiece* trial_name,
- StringPiece* group_name) const;
-
-
-
- bool GetParams(std::map<std::string, std::string>* params) const;
- private:
-
- PickleIterator GetPickleIterator() const;
-
-
- bool ReadStringPair(PickleIterator* iter,
- StringPiece* trial_name,
- StringPiece* group_name) const;
- };
- typedef std::vector<ActiveGroup> ActiveGroups;
-
-
- static const int kNotFinalized;
- FieldTrial(const FieldTrial&) = delete;
- FieldTrial& operator=(const FieldTrial&) = delete;
-
-
-
-
-
-
-
- void Disable();
-
-
-
-
- int AppendGroup(const std::string& name, Probability group_probability);
-
- const std::string& trial_name() const { return trial_name_; }
-
-
-
-
-
- int group();
-
-
- const std::string& group_name();
-
-
-
- const std::string& GetGroupNameWithoutActivation();
-
-
-
-
-
-
-
-
- void SetForced();
-
- static void EnableBenchmarking();
-
-
-
-
-
-
-
-
-
-
- static FieldTrial* CreateSimulatedFieldTrial(
- const std::string& trial_name,
- Probability total_probability,
- const std::string& default_group_name,
- double entropy_value);
- private:
-
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, Registration);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, AbsoluteProbabilities);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, RemainingProbability);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, FiftyFiftyProbability);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, MiddleProbabilities);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, OneWinner);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, DisableProbability);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, ActiveGroups);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, AllGroups);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, ActiveGroupsNotFinalized);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, Save);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, SaveAll);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, DuplicateRestore);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, SetForcedTurnFeatureOff);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, SetForcedTurnFeatureOn);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, SetForcedChangeDefault_Default);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, SetForcedChangeDefault_NonDefault);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, FloatBoundariesGiveEqualGroupSizes);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, DoesNotSurpassTotalProbability);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest,
- DoNotAddSimulatedFieldTrialsToAllocator);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, ClearParamsFromSharedMemory);
- friend class base::FieldTrialList;
- friend class RefCounted<FieldTrial>;
- using FieldTrialRef = PersistentMemoryAllocator::Reference;
-
-
-
- static const int kDefaultGroupNumber;
-
-
- FieldTrial(const std::string& trial_name,
- Probability total_probability,
- const std::string& default_group_name,
- double entropy_value);
- virtual ~FieldTrial();
-
- std::string default_group_name() const { return default_group_name_; }
-
-
- void SetTrialRegistered();
-
- void SetGroupChoice(const std::string& group_name, int number);
-
-
-
- void FinalizeGroupChoice();
-
-
- void FinalizeGroupChoiceImpl(bool is_locked);
-
-
-
-
-
-
- bool GetActiveGroup(ActiveGroup* active_group) const;
-
-
-
-
-
-
- bool GetStateWhileLocked(State* field_trial_state, bool include_disabled);
-
- std::string group_name_internal() const { return group_name_; }
-
- const std::string trial_name_;
-
-
- const Probability divisor_;
-
- const std::string default_group_name_;
-
-
-
- Probability random_;
-
- Probability accumulated_group_probability_;
-
- int next_group_number_;
-
-
- int group_;
-
-
- std::string group_name_;
-
-
- bool enable_field_trial_;
-
-
- bool forced_;
-
- bool group_reported_;
-
-
- bool trial_registered_;
-
- FieldTrialRef ref_;
-
-
- static bool enable_benchmarking_;
- };
- class BASE_EXPORT FieldTrialList {
- public:
- using FieldTrialAllocator = PersistentMemoryAllocator;
-
-
- typedef std::string (*EscapeDataFunc)(const std::string& input);
-
- class BASE_EXPORT Observer {
- public:
-
- virtual void OnFieldTrialGroupFinalized(const std::string& trial_name,
- const std::string& group_name) = 0;
- protected:
- virtual ~Observer();
- };
-
-
-
-
-
-
- explicit FieldTrialList(
- std::unique_ptr<const FieldTrial::EntropyProvider> entropy_provider);
- FieldTrialList(const FieldTrialList&) = delete;
- FieldTrialList& operator=(const FieldTrialList&) = delete;
-
- ~FieldTrialList();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static FieldTrial* FactoryGetFieldTrial(
- const std::string& trial_name,
- FieldTrial::Probability total_probability,
- const std::string& default_group_name,
- FieldTrial::RandomizationType randomization_type,
- int* default_group_number);
-
-
-
-
-
-
-
-
-
-
-
- static FieldTrial* FactoryGetFieldTrialWithRandomizationSeed(
- const std::string& trial_name,
- FieldTrial::Probability total_probability,
- const std::string& default_group_name,
- FieldTrial::RandomizationType randomization_type,
- uint32_t randomization_seed,
- int* default_group_number,
- const FieldTrial::EntropyProvider* override_entropy_provider);
-
-
- static FieldTrial* Find(const std::string& trial_name);
-
-
- static int FindValue(const std::string& trial_name);
-
-
-
-
-
-
- static std::string FindFullName(const std::string& trial_name);
-
- static bool TrialExists(const std::string& trial_name);
-
- static bool IsTrialActive(const std::string& trial_name);
-
-
-
-
-
-
-
-
- static void StatesToString(std::string* output);
-
-
-
-
-
-
-
-
- static void AllStatesToString(std::string* output, bool include_disabled);
-
-
-
-
-
-
-
- static std::string AllParamsToString(bool include_disabled,
- EscapeDataFunc encode_data_func);
-
-
-
-
- static void GetActiveFieldTrialGroups(
- FieldTrial::ActiveGroups* active_groups);
-
- static void GetActiveFieldTrialGroupsFromString(
- const std::string& trials_string,
- FieldTrial::ActiveGroups* active_groups);
-
-
-
-
- static void GetInitiallyActiveFieldTrials(
- const CommandLine& command_line,
- FieldTrial::ActiveGroups* active_groups);
-
-
-
-
-
-
-
-
- static bool CreateTrialsFromString(const std::string& trials_string);
-
-
-
-
-
-
-
-
- static void CreateTrialsFromCommandLine(const CommandLine& cmd_line,
- const char* field_trial_handle_switch,
- int fd_key);
-
-
- static void CreateFeaturesFromCommandLine(const CommandLine& command_line,
- const char* enable_features_switch,
- const char* disable_features_switch,
- FeatureList* feature_list);
- #if defined(OS_WIN)
-
-
-
- static void AppendFieldTrialHandleIfNeeded(HandlesToInheritVector* handles);
- #elif defined(OS_FUCHSIA)
-
- #elif defined(OS_MAC)
-
-
- static void InsertFieldTrialHandleIfNeeded(
- MachPortsForRendezvous* rendezvous_ports);
- #elif defined(OS_POSIX) && !defined(OS_NACL)
-
-
-
-
- static int GetFieldTrialDescriptor();
- #endif
- static ReadOnlySharedMemoryRegion DuplicateFieldTrialSharedMemoryForTesting();
-
-
-
-
-
- static void CopyFieldTrialStateToFlags(const char* field_trial_handle_switch,
- const char* enable_features_switch,
- const char* disable_features_switch,
- CommandLine* cmd_line);
-
-
-
-
-
-
- static FieldTrial* CreateFieldTrial(const std::string& name,
- const std::string& group_name);
-
-
-
-
- static bool AddObserver(Observer* observer);
-
- static void RemoveObserver(Observer* observer);
-
-
-
-
-
-
-
-
-
- static void SetSynchronousObserver(Observer* observer);
-
- static void RemoveSynchronousObserver(Observer* observer);
-
-
- static void OnGroupFinalized(bool is_locked, FieldTrial* field_trial);
-
- static void NotifyFieldTrialGroupSelection(FieldTrial* field_trial);
-
- static size_t GetFieldTrialCount();
-
-
-
- static bool GetParamsFromSharedMemory(
- FieldTrial* field_trial,
- std::map<std::string, std::string>* params);
-
- static void ClearParamsFromSharedMemoryForTesting();
-
-
- static void DumpAllFieldTrialsToPersistentAllocator(
- PersistentMemoryAllocator* allocator);
-
-
-
- static std::vector<const FieldTrial::FieldTrialEntry*>
- GetAllFieldTrialsFromPersistentAllocator(
- PersistentMemoryAllocator const& allocator);
-
-
-
- static FieldTrialList* GetInstance();
-
- static FieldTrialList* BackupInstanceForTesting();
-
- static void RestoreInstanceForTesting(FieldTrialList* instance);
- private:
-
- FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, InstantiateAllocator);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AddTrialsToAllocator);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest,
- DoNotAddSimulatedFieldTrialsToAllocator);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AssociateFieldTrialParams);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, ClearParamsFromSharedMemory);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest,
- SerializeSharedMemoryRegionMetadata);
- friend int SerializeSharedMemoryRegionMetadata(void);
- FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, CheckReadOnlySharedMemoryRegion);
-
-
-
-
- static std::string SerializeSharedMemoryRegionMetadata(
- const ReadOnlySharedMemoryRegion& shm);
- #if defined(OS_WIN) || defined(OS_FUCHSIA) || defined(OS_MAC)
- static ReadOnlySharedMemoryRegion DeserializeSharedMemoryRegionMetadata(
- const std::string& switch_value);
- #elif defined(OS_POSIX) && !defined(OS_NACL)
- static ReadOnlySharedMemoryRegion DeserializeSharedMemoryRegionMetadata(
- int fd,
- const std::string& switch_value);
- #endif
- #if defined(OS_WIN) || defined(OS_FUCHSIA) || defined(OS_MAC)
-
-
-
-
- static bool CreateTrialsFromSwitchValue(const std::string& switch_value);
- #elif defined(OS_POSIX) && !defined(OS_NACL)
-
-
-
-
- static bool CreateTrialsFromDescriptor(int fd_key,
- const std::string& switch_value);
- #endif
-
-
-
- static bool CreateTrialsFromSharedMemoryRegion(
- const ReadOnlySharedMemoryRegion& shm_region);
-
-
-
-
-
- static bool CreateTrialsFromSharedMemoryMapping(
- ReadOnlySharedMemoryMapping shm_mapping);
-
-
-
- static void InstantiateFieldTrialAllocatorIfNeeded();
-
-
- static void AddToAllocatorWhileLocked(PersistentMemoryAllocator* allocator,
- FieldTrial* field_trial);
-
- static void ActivateFieldTrialEntryWhileLocked(FieldTrial* field_trial);
-
- typedef std::map<std::string, FieldTrial*> RegistrationMap;
-
-
- static const FieldTrial::EntropyProvider*
- GetEntropyProviderForOneTimeRandomization();
-
- FieldTrial* PreLockedFind(const std::string& name);
-
-
-
- static void Register(FieldTrial* trial);
-
- static RegistrationMap GetRegisteredTrials();
- static FieldTrialList* global_;
-
-
-
-
- static bool used_without_global_;
-
- Lock lock_;
- RegistrationMap registered_;
- std::map<std::string, std::string> seen_states_;
-
-
- std::unique_ptr<const FieldTrial::EntropyProvider> entropy_provider_;
-
- scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_;
-
- Observer* synchronous_observer_ = nullptr;
-
-
-
-
- std::unique_ptr<FieldTrialAllocator> field_trial_allocator_ = nullptr;
-
-
-
- ReadOnlySharedMemoryRegion readonly_allocator_region_;
-
- bool create_trials_from_command_line_called_ = false;
- };
- }
- #endif
|