blob: 80f46011502240e6e3aeb0410bfdbfbb87f7bc50 [file] [log] [blame]
[email protected]2d4729582012-04-12 07:08:071// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ac262c9f2008-10-19 17:45:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// FieldTrial is a class for handling details of statistical experiments
6// performed by actual users in the field (i.e., in a shipped or beta product).
7// All code is called exclusively on the UI thread currently.
8//
[email protected]9660b972009-03-02 19:02:569// The simplest example is an experiment to see whether one of two options
10// produces "better" results across our user population. In that scenario, UMA
11// data is uploaded to aggregate the test results, and this FieldTrial class
12// manages the state of each such experiment (state == which option was
13// pseudo-randomly selected).
14//
[email protected]ac262c9f2008-10-19 17:45:2115// States are typically generated randomly, either based on a one time
[email protected]edafd4c2011-05-10 17:18:5316// randomization (which will yield the same results, in terms of selecting
17// the client for a field trial or not, for every run of the program on a
18// given machine), or by a startup randomization (generated each time the
19// application starts up, but held constant during the duration of the
20// process), or by continuous randomization across a run (where the state
21// can be recalculated again and again, many times during a process).
22// Continuous randomization is not yet implemented.
[email protected]9660b972009-03-02 19:02:5623
24//------------------------------------------------------------------------------
25// Example: Suppose we have an experiment involving memory, such as determining
[email protected]57a336a2009-09-30 20:42:2726// the impact of some pruning algorithm.
[email protected]9660b972009-03-02 19:02:5627// We assume that we already have a histogram of memory usage, such as:
28
29// HISTOGRAM_COUNTS("Memory.RendererTotal", count);
30
31// Somewhere in main thread initialization code, we'd probably define an
32// instance of a FieldTrial, with code such as:
33
[email protected]f1d16d4a2011-03-21 14:04:0134// // FieldTrials are reference counted, and persist automagically until
[email protected]9660b972009-03-02 19:02:5635// // process teardown, courtesy of their automatic registration in
36// // FieldTrialList.
[email protected]f1d16d4a2011-03-21 14:04:0137// // Note: This field trial will run in Chrome instances compiled through
38// // 8 July, 2015, and after that all instances will be in "StandardMem".
[email protected]9f8c0a22012-06-13 02:01:2439// scoped_refptr<base::FieldTrial> trial(
40// base::FieldTrialList::FactoryGetFieldTrial("MemoryExperiment", 1000,
[email protected]70d7ca92012-08-17 22:43:1041// "StandardMem", 2015, 7, 8,
42// NULL));
43// const int high_mem_group =
[email protected]f1d16d4a2011-03-21 14:04:0144// trial->AppendGroup("HighMem", 20); // 2% in HighMem group.
[email protected]70d7ca92012-08-17 22:43:1045// const int low_mem_group =
[email protected]f1d16d4a2011-03-21 14:04:0146// trial->AppendGroup("LowMem", 20); // 2% in LowMem group.
[email protected]9660b972009-03-02 19:02:5647// // Take action depending of which group we randomly land in.
[email protected]70d7ca92012-08-17 22:43:1048// if (trial->group() == high_mem_group)
[email protected]57a336a2009-09-30 20:42:2749// SetPruningAlgorithm(kType1); // Sample setting of browser state.
[email protected]70d7ca92012-08-17 22:43:1050// else if (trial->group() == low_mem_group)
[email protected]57a336a2009-09-30 20:42:2751// SetPruningAlgorithm(kType2); // Sample alternate setting.
[email protected]9660b972009-03-02 19:02:5652
[email protected]f1d16d4a2011-03-21 14:04:0153// We then, in addition to our original histogram, output histograms which have
54// slightly different names depending on what group the trial instance happened
55// to randomly be assigned:
[email protected]9660b972009-03-02 19:02:5656
[email protected]f1d16d4a2011-03-21 14:04:0157// HISTOGRAM_COUNTS("Memory.RendererTotal", count); // The original histogram.
[email protected]edafd4c2011-05-10 17:18:5358// static const bool memory_renderer_total_trial_exists =
[email protected]a140efe2011-09-15 23:15:0259// FieldTrialList::TrialExists("MemoryExperiment");
[email protected]edafd4c2011-05-10 17:18:5360// if (memory_renderer_total_trial_exists) {
[email protected]f1d16d4a2011-03-21 14:04:0161// HISTOGRAM_COUNTS(FieldTrial::MakeName("Memory.RendererTotal",
62// "MemoryExperiment"), count);
63// }
[email protected]9660b972009-03-02 19:02:5664
[email protected]f1d16d4a2011-03-21 14:04:0165// The above code will create four distinct histograms, with each run of the
[email protected]b37fdaa2009-07-01 01:14:5666// application being assigned to of of the three groups, and for each group, the
[email protected]9660b972009-03-02 19:02:5667// correspondingly named histogram will be populated:
68
[email protected]f1d16d4a2011-03-21 14:04:0169// Memory.RendererTotal // 100% of users still fill this histogram.
70// Memory.RendererTotal_HighMem // 2% of users will fill this histogram.
71// Memory.RendererTotal_LowMem // 2% of users will fill this histogram.
72// Memory.RendererTotal_StandardMem // 96% of users will fill this histogram.
[email protected]9660b972009-03-02 19:02:5673
74//------------------------------------------------------------------------------
[email protected]ac262c9f2008-10-19 17:45:2175
[email protected]835d7c82010-10-14 04:38:3876#ifndef BASE_METRICS_FIELD_TRIAL_H_
77#define BASE_METRICS_FIELD_TRIAL_H_
[email protected]ac262c9f2008-10-19 17:45:2178
79#include <map>
80#include <string>
[email protected]ad2461c2012-04-27 21:11:0381#include <vector>
[email protected]ac262c9f2008-10-19 17:45:2182
[email protected]0bea7252011-08-05 15:34:0083#include "base/base_export.h"
[email protected]933729bc2011-01-19 18:52:3284#include "base/gtest_prod_util.h"
[email protected]3b63f8f42011-03-28 01:54:1585#include "base/memory/ref_counted.h"
[email protected]8cffde0e2012-05-04 01:14:1486#include "base/observer_list_threadsafe.h"
[email protected]20305ec2011-01-21 04:55:5287#include "base/synchronization/lock.h"
[email protected]ac262c9f2008-10-19 17:45:2188#include "base/time.h"
89
[email protected]835d7c82010-10-14 04:38:3890namespace base {
[email protected]9660b972009-03-02 19:02:5691
[email protected]933729bc2011-01-19 18:52:3292class FieldTrialList;
93
[email protected]0bea7252011-08-05 15:34:0094class BASE_EXPORT FieldTrial : public RefCounted<FieldTrial> {
[email protected]ac262c9f2008-10-19 17:45:2195 public:
[email protected]e695fbd62009-06-30 16:31:5496 typedef int Probability; // Probability type for being selected in a trial.
[email protected]ad2461c2012-04-27 21:11:0397
[email protected]20f999b52012-08-24 22:32:5998 // EntropyProvider is an interface for providing entropy for one-time
99 // randomized (persistent) field trials.
100 class BASE_EXPORT EntropyProvider {
101 public:
102 virtual ~EntropyProvider();
103
104 // Returns a double in the range of [0, 1) based on |trial_name| that will
105 // be used for the dice roll for the specified field trial. A given instance
106 // should always return the same value given the same input |trial_name|.
107 virtual double GetEntropyForTrial(const std::string& trial_name) const = 0;
108 };
109
[email protected]ad2461c2012-04-27 21:11:03110 // A pair representing a Field Trial and its selected group.
[email protected]0c8b7ad2012-11-06 07:08:14111 struct ActiveGroup {
[email protected]bb65ef72012-11-19 20:07:59112 std::string trial_name;
113 std::string group_name;
[email protected]25655dd2012-01-27 13:50:26114 };
[email protected]e695fbd62009-06-30 16:31:54115
[email protected]0c8b7ad2012-11-06 07:08:14116 typedef std::vector<ActiveGroup> ActiveGroups;
[email protected]ad2461c2012-04-27 21:11:03117
[email protected]e695fbd62009-06-30 16:31:54118 // A return value to indicate that a given instance has not yet had a group
119 // assignment (and hence is not yet participating in the trial).
[email protected]933729bc2011-01-19 18:52:32120 static const int kNotFinalized;
[email protected]9660b972009-03-02 19:02:56121
[email protected]edafd4c2011-05-10 17:18:53122 // Changes the field trial to use one-time randomization, i.e. produce the
123 // same result for the current trial on every run of this client. Must be
124 // called right after construction.
[email protected]edafd4c2011-05-10 17:18:53125 void UseOneTimeRandomization();
126
127 // Disables this trial, meaning it always determines the default group
128 // has been selected. May be called immediately after construction, or
129 // at any time after initialization (should not be interleaved with
130 // AppendGroup calls). Once disabled, there is no way to re-enable a
131 // trial.
[email protected]2d4729582012-04-12 07:08:07132 // TODO(mad): http://code.google.com/p/chromium/issues/detail?id=121446
133 // This doesn't properly reset to Default when a group was forced.
[email protected]edafd4c2011-05-10 17:18:53134 void Disable();
135
[email protected]9660b972009-03-02 19:02:56136 // Establish the name and probability of the next group in this trial.
[email protected]933729bc2011-01-19 18:52:32137 // Sometimes, based on construction randomization, this call may cause the
[email protected]9660b972009-03-02 19:02:56138 // provided group to be *THE* group selected for use in this instance.
[email protected]9238ab92011-02-25 17:22:46139 // The return value is the group number of the new group.
[email protected]9660b972009-03-02 19:02:56140 int AppendGroup(const std::string& name, Probability group_probability);
141
142 // Return the name of the FieldTrial (excluding the group name).
[email protected]bb65ef72012-11-19 20:07:59143 const std::string& trial_name() const { return trial_name_; }
[email protected]9660b972009-03-02 19:02:56144
[email protected]7aea1172012-11-01 18:59:47145 // Return the randomly selected group number that was assigned, and notify
146 // any/all observers that this finalized group number has presumably been used
147 // (queried), and will never change. Note that this will force an instance to
148 // participate, and make it illegal to attempt to probabilistically add any
149 // other groups to the trial.
[email protected]933729bc2011-01-19 18:52:32150 int group();
[email protected]9660b972009-03-02 19:02:56151
[email protected]2d4729582012-04-12 07:08:07152 // If the group's name is empty, a string version containing the group number
153 // is used as the group name. This causes a winner to be chosen if none was.
[email protected]bb65ef72012-11-19 20:07:59154 const std::string& group_name();
[email protected]933729bc2011-01-19 18:52:32155
[email protected]edafd4c2011-05-10 17:18:53156 // Helper function for the most common use: as an argument to specify the
[email protected]9660b972009-03-02 19:02:56157 // name of a HISTOGRAM. Use the original histogram name as the name_prefix.
158 static std::string MakeName(const std::string& name_prefix,
159 const std::string& trial_name);
[email protected]ac262c9f2008-10-19 17:45:21160
[email protected]e8d82c612010-12-07 22:54:27161 // Enable benchmarking sets field trials to a common setting.
162 static void EnableBenchmarking();
163
[email protected]0284bf7c2012-05-07 22:48:19164 // Set the field trial as forced, meaning that it was setup earlier than
165 // the hard coded registration of the field trial to override it.
166 // This allows the code that was hard coded to register the field trial to
167 // still succeed even though the field trial has already been registered.
168 // This must be called after appending all the groups, since we will make
169 // the group choice here. Note that this is a NOOP for already forced trials.
170 // And, as the rest of the FieldTrial code, this is not thread safe and must
171 // be done from the UI thread.
172 void SetForced();
173
[email protected]ac262c9f2008-10-19 17:45:21174 private:
[email protected]933729bc2011-01-19 18:52:32175 // Allow tests to access our innards for testing purposes.
[email protected]225020ce2011-11-29 14:45:53176 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, Registration);
177 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, AbsoluteProbabilities);
178 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, RemainingProbability);
179 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, FiftyFiftyProbability);
180 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, MiddleProbabilities);
181 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, OneWinner);
182 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, DisableProbability);
[email protected]0c8b7ad2012-11-06 07:08:14183 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, ActiveGroups);
184 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, ActiveGroupsNotFinalized);
[email protected]225020ce2011-11-29 14:45:53185 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, Save);
186 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, DuplicateRestore);
187 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, MakeName);
188 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientId);
189 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientIdIsUniform);
[email protected]25655dd2012-01-27 13:50:26190 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, NameGroupIds);
[email protected]225020ce2011-11-29 14:45:53191 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, UseOneTimeRandomization);
[email protected]933729bc2011-01-19 18:52:32192
193 friend class base::FieldTrialList;
194
[email protected]835d7c82010-10-14 04:38:38195 friend class RefCounted<FieldTrial>;
[email protected]877d55d2009-11-05 21:53:08196
[email protected]2d4729582012-04-12 07:08:07197 // This is the group number of the 'default' group when a choice wasn't forced
198 // by a call to FieldTrialList::CreateFieldTrial. It is kept private so that
199 // consumers don't use it by mistake in cases where the group was forced.
200 static const int kDefaultGroupNumber;
201
[email protected]8826a6e82012-05-11 02:16:32202 FieldTrial(const std::string& name,
203 Probability total_probability,
204 const std::string& default_group_name);
[email protected]9b2331d92010-10-04 23:11:19205 virtual ~FieldTrial();
[email protected]877d55d2009-11-05 21:53:08206
[email protected]2d4729582012-04-12 07:08:07207 // Return the default group name of the FieldTrial.
208 std::string default_group_name() const { return default_group_name_; }
209
[email protected]bb65ef72012-11-19 20:07:59210 // Sets the chosen group name and number.
211 void SetGroupChoice(const std::string& group_name, int number);
[email protected]2d4729582012-04-12 07:08:07212
[email protected]7aea1172012-11-01 18:59:47213 // Ensures that a group is chosen, if it hasn't yet been. The field trial
214 // might yet be disabled, so this call will *not* notify observers of the
215 // status.
216 void FinalizeGroupChoice();
217
[email protected]0c8b7ad2012-11-06 07:08:14218 // Returns the trial name and selected group name for this field trial via
219 // the output parameter |active_group|, but only if the group has already
[email protected]8b18dd42012-11-10 03:19:43220 // been chosen and has been externally observed via |group()| and the trial
221 // has not been disabled. In that case, true is returned and |active_group|
222 // is filled in; otherwise, the result is false and |active_group| is left
223 // untouched.
[email protected]0c8b7ad2012-11-06 07:08:14224 bool GetActiveGroup(ActiveGroup* active_group) const;
225
[email protected]933729bc2011-01-19 18:52:32226 // Returns the group_name. A winner need not have been chosen.
227 std::string group_name_internal() const { return group_name_; }
228
[email protected]9660b972009-03-02 19:02:56229 // The name of the field trial, as can be found via the FieldTrialList.
[email protected]bb65ef72012-11-19 20:07:59230 const std::string trial_name_;
[email protected]9660b972009-03-02 19:02:56231
[email protected]58d2d2d2010-08-05 22:46:33232 // The maximum sum of all probabilities supplied, which corresponds to 100%.
[email protected]9660b972009-03-02 19:02:56233 // This is the scaling factor used to adjust supplied probabilities.
[email protected]933729bc2011-01-19 18:52:32234 const Probability divisor_;
235
236 // The name of the default group.
237 const std::string default_group_name_;
[email protected]9660b972009-03-02 19:02:56238
239 // The randomly selected probability that is used to select a group (or have
240 // the instance not participate). It is the product of divisor_ and a random
241 // number between [0, 1).
[email protected]edafd4c2011-05-10 17:18:53242 Probability random_;
[email protected]9660b972009-03-02 19:02:56243
244 // Sum of the probabilities of all appended groups.
245 Probability accumulated_group_probability_;
246
247 int next_group_number_;
248
249 // The pseudo-randomly assigned group number.
[email protected]933729bc2011-01-19 18:52:32250 // This is kNotFinalized if no group has been assigned.
[email protected]9660b972009-03-02 19:02:56251 int group_;
252
[email protected]edafd4c2011-05-10 17:18:53253 // A textual name for the randomly selected group. Valid after |group()|
254 // has been called.
[email protected]9660b972009-03-02 19:02:56255 std::string group_name_;
[email protected]ac262c9f2008-10-19 17:45:21256
[email protected]edafd4c2011-05-10 17:18:53257 // When enable_field_trial_ is false, field trial reverts to the 'default'
[email protected]933729bc2011-01-19 18:52:32258 // group.
[email protected]edafd4c2011-05-10 17:18:53259 bool enable_field_trial_;
[email protected]933729bc2011-01-19 18:52:32260
[email protected]2d4729582012-04-12 07:08:07261 // When forced_ is true, we return the chosen group from AppendGroup when
262 // appropriate.
263 bool forced_;
264
[email protected]7aea1172012-11-01 18:59:47265 // Specifies whether the group choice has been reported to observers.
266 bool group_reported_;
267
[email protected]e8d82c612010-12-07 22:54:27268 // When benchmarking is enabled, field trials all revert to the 'default'
[email protected]933729bc2011-01-19 18:52:32269 // group.
[email protected]e8d82c612010-12-07 22:54:27270 static bool enable_benchmarking_;
271
[email protected]ac262c9f2008-10-19 17:45:21272 DISALLOW_COPY_AND_ASSIGN(FieldTrial);
273};
274
[email protected]9660b972009-03-02 19:02:56275//------------------------------------------------------------------------------
[email protected]ac262c9f2008-10-19 17:45:21276// Class with a list of all active field trials. A trial is active if it has
277// been registered, which includes evaluating its state based on its probaility.
278// Only one instance of this class exists.
[email protected]0bea7252011-08-05 15:34:00279class BASE_EXPORT FieldTrialList {
[email protected]ac262c9f2008-10-19 17:45:21280 public:
[email protected]2d4729582012-04-12 07:08:07281 // Define a separator character to use when creating a persistent form of an
[email protected]e695fbd62009-06-30 16:31:54282 // instance. This is intended for use as a command line argument, passed to a
283 // second process to mimic our state (i.e., provide the same group name).
284 static const char kPersistentStringSeparator; // Currently a slash.
285
[email protected]d0c69292013-01-09 18:15:26286 // Year that is guaranteed to not be expired when instantiating a field trial
287 // via |FactoryGetFieldTrial()|. Set to two years from the build date.
288 static int kNoExpirationYear;
[email protected]63a8ba12011-04-29 05:42:22289
290 // Observer is notified when a FieldTrial's group is selected.
[email protected]8cffde0e2012-05-04 01:14:14291 class BASE_EXPORT Observer {
[email protected]63a8ba12011-04-29 05:42:22292 public:
293 // Notify observers when FieldTrials's group is selected.
294 virtual void OnFieldTrialGroupFinalized(const std::string& trial_name,
295 const std::string& group_name) = 0;
296
297 protected:
[email protected]20f999b52012-08-24 22:32:59298 virtual ~Observer();
[email protected]63a8ba12011-04-29 05:42:22299 };
300
[email protected]ac262c9f2008-10-19 17:45:21301 // This singleton holds the global list of registered FieldTrials.
[email protected]edafd4c2011-05-10 17:18:53302 //
[email protected]20f999b52012-08-24 22:32:59303 // To support one-time randomized field trials, specify a non-NULL
304 // |entropy_provider| which should be a source of uniformly distributed
305 // entropy values. Takes ownership of |entropy_provider|. If one time
306 // randomization is not desired, pass in NULL for |entropy_provider|.
307 explicit FieldTrialList(const FieldTrial::EntropyProvider* entropy_provider);
308
[email protected]ac262c9f2008-10-19 17:45:21309 // Destructor Release()'s references to all registered FieldTrial instances.
310 ~FieldTrialList();
311
[email protected]2d4729582012-04-12 07:08:07312 // Get a FieldTrial instance from the factory.
313 //
314 // |name| is used to register the instance with the FieldTrialList class,
315 // and can be used to find the trial (only one trial can be present for each
316 // name). |default_group_name| is the name of the default group which will
317 // be chosen if none of the subsequent appended groups get to be chosen.
318 // |default_group_number| can receive the group number of the default group as
[email protected]bb65ef72012-11-19 20:07:59319 // AppendGroup returns the number of the subsequence groups. |trial_name| and
[email protected]2d4729582012-04-12 07:08:07320 // |default_group_name| may not be empty but |default_group_number| can be
321 // NULL if the value is not needed.
322 //
323 // Group probabilities that are later supplied must sum to less than or equal
324 // to the |total_probability|. Arguments |year|, |month| and |day_of_month|
325 // specify the expiration time. If the build time is after the expiration time
326 // then the field trial reverts to the 'default' group.
327 //
328 // Use this static method to get a startup-randomized FieldTrial or a
329 // previously created forced FieldTrial. If you want a one-time randomized
330 // trial, call UseOneTimeRandomization() right after creation.
331 static FieldTrial* FactoryGetFieldTrial(
[email protected]bb65ef72012-11-19 20:07:59332 const std::string& trial_name,
[email protected]2d4729582012-04-12 07:08:07333 FieldTrial::Probability total_probability,
334 const std::string& default_group_name,
335 const int year,
336 const int month,
337 const int day_of_month,
338 int* default_group_number);
[email protected]ac262c9f2008-10-19 17:45:21339
340 // The Find() method can be used to test to see if a named Trial was already
341 // registered, or to retrieve a pointer to it from the global map.
[email protected]9660b972009-03-02 19:02:56342 static FieldTrial* Find(const std::string& name);
343
[email protected]edafd4c2011-05-10 17:18:53344 // Returns the group number chosen for the named trial, or
345 // FieldTrial::kNotFinalized if the trial does not exist.
[email protected]9660b972009-03-02 19:02:56346 static int FindValue(const std::string& name);
347
[email protected]edafd4c2011-05-10 17:18:53348 // Returns the group name chosen for the named trial, or the
349 // empty string if the trial does not exist.
[email protected]9660b972009-03-02 19:02:56350 static std::string FindFullName(const std::string& name);
[email protected]ac262c9f2008-10-19 17:45:21351
[email protected]edafd4c2011-05-10 17:18:53352 // Returns true if the named trial has been registered.
353 static bool TrialExists(const std::string& name);
354
[email protected]e6ade47d2012-11-07 19:46:28355 // Creates a persistent representation of active FieldTrial instances for
[email protected]2d4729582012-04-12 07:08:07356 // resurrection in another process. This allows randomization to be done in
357 // one process, and secondary processes can be synchronized on the result.
[email protected]e6ade47d2012-11-07 19:46:28358 // The resulting string contains the name and group name pairs of all
359 // registered FieldTrials for which the group has been chosen and externally
[email protected]8b18dd42012-11-10 03:19:43360 // observed (via |group()|) and which have not been disabled, with "/" used
361 // to separate all names and to terminate the string. This string is parsed
362 // by |CreateTrialsFromString()|.
[email protected]e695fbd62009-06-30 16:31:54363 static void StatesToString(std::string* output);
364
[email protected]0c8b7ad2012-11-06 07:08:14365 // Fills in the supplied vector |active_groups| (which must be empty when
366 // called) with a snapshot of all registered FieldTrials for which the group
[email protected]8b18dd42012-11-10 03:19:43367 // has been chosen and externally observed (via |group()|) and which have
368 // not been disabled.
[email protected]0c8b7ad2012-11-06 07:08:14369 static void GetActiveFieldTrialGroups(
370 FieldTrial::ActiveGroups* active_groups);
[email protected]25655dd2012-01-27 13:50:26371
[email protected]2d4729582012-04-12 07:08:07372 // Use a state string (re: StatesToString()) to augment the current list of
[email protected]e290b032012-11-13 06:31:09373 // field trials to include the supplied trials, and using a 100% probability
374 // for each trial, force them to have the same group string. This is commonly
375 // used in a non-browser process, to carry randomly selected state in a
376 // browser process into this non-browser process, but could also be invoked
377 // through a command line argument to the browser process. The created field
378 // trials are marked as "used" for the purposes of active trial reporting.
[email protected]2d4729582012-04-12 07:08:07379 static bool CreateTrialsFromString(const std::string& prior_trials);
[email protected]e695fbd62009-06-30 16:31:54380
[email protected]63a8ba12011-04-29 05:42:22381 // Create a FieldTrial with the given |name| and using 100% probability for
382 // the FieldTrial, force FieldTrial to have the same group string as
383 // |group_name|. This is commonly used in a non-browser process, to carry
384 // randomly selected state in a browser process into this non-browser process.
[email protected]2d4729582012-04-12 07:08:07385 // It returns NULL if there is a FieldTrial that is already registered with
386 // the same |name| but has different finalized group string (|group_name|).
[email protected]63a8ba12011-04-29 05:42:22387 static FieldTrial* CreateFieldTrial(const std::string& name,
388 const std::string& group_name);
389
390 // Add an observer to be notified when a field trial is irrevocably committed
391 // to being part of some specific field_group (and hence the group_name is
392 // also finalized for that field_trial).
393 static void AddObserver(Observer* observer);
394
395 // Remove an observer.
396 static void RemoveObserver(Observer* observer);
397
[email protected]907432e2012-12-12 04:16:21398 // Notify all observers that a group has been finalized for |field_trial|.
399 static void NotifyFieldTrialGroupSelection(FieldTrial* field_trial);
[email protected]63a8ba12011-04-29 05:42:22400
[email protected]e8d82c612010-12-07 22:54:27401 // Return the number of active field trials.
402 static size_t GetFieldTrialCount();
403
[email protected]20f999b52012-08-24 22:32:59404 // If one-time randomization is enabled, returns a weak pointer to the
405 // corresponding EntropyProvider. Otherwise, returns NULL.
406 static const FieldTrial::EntropyProvider*
407 GetEntropyProviderForOneTimeRandomization();
[email protected]edafd4c2011-05-10 17:18:53408
[email protected]ac262c9f2008-10-19 17:45:21409 private:
[email protected]e695fbd62009-06-30 16:31:54410 // A map from FieldTrial names to the actual instances.
[email protected]9660b972009-03-02 19:02:56411 typedef std::map<std::string, FieldTrial*> RegistrationList;
[email protected]ac262c9f2008-10-19 17:45:21412
[email protected]a502bbe72011-01-07 18:06:45413 // Helper function should be called only while holding lock_.
414 FieldTrial* PreLockedFind(const std::string& name);
415
[email protected]2d4729582012-04-12 07:08:07416 // Register() stores a pointer to the given trial in a global map.
417 // This method also AddRef's the indicated trial.
418 // This should always be called after creating a new FieldTrial instance.
419 static void Register(FieldTrial* trial);
420
[email protected]ac262c9f2008-10-19 17:45:21421 static FieldTrialList* global_; // The singleton of this class.
[email protected]ac262c9f2008-10-19 17:45:21422
[email protected]0f1373a2011-06-22 23:51:24423 // This will tell us if there is an attempt to register a field
424 // trial or check if one-time randomization is enabled without
425 // creating the FieldTrialList. This is not an error, unless a
426 // FieldTrialList is created after that.
427 static bool used_without_global_;
[email protected]7e05f6c42009-07-11 01:50:48428
[email protected]0b48db42009-03-23 02:45:11429 // Lock for access to registered_.
[email protected]20305ec2011-01-21 04:55:52430 base::Lock lock_;
[email protected]ac262c9f2008-10-19 17:45:21431 RegistrationList registered_;
432
[email protected]20f999b52012-08-24 22:32:59433 // Entropy provider to be used for one-time randomized field trials. If NULL,
434 // one-time randomization is not supported.
435 scoped_ptr<const FieldTrial::EntropyProvider> entropy_provider_;
[email protected]edafd4c2011-05-10 17:18:53436
[email protected]63a8ba12011-04-29 05:42:22437 // List of observers to be notified when a group is selected for a FieldTrial.
[email protected]8cffde0e2012-05-04 01:14:14438 scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_;
[email protected]63a8ba12011-04-29 05:42:22439
[email protected]ac262c9f2008-10-19 17:45:21440 DISALLOW_COPY_AND_ASSIGN(FieldTrialList);
441};
442
[email protected]835d7c82010-10-14 04:38:38443} // namespace base
444
445#endif // BASE_METRICS_FIELD_TRIAL_H_