OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // FieldTrial is a class for handling details of statistical experiments | 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). | 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. | 7 // All code is called exclusively on the UI thread currently. |
8 // | 8 // |
9 // The simplest example is an experiment to see whether one of two options | 9 // 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 | 10 // produces "better" results across our user population. In that scenario, UMA |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 #include <set> | 62 #include <set> |
63 #include <string> | 63 #include <string> |
64 #include <vector> | 64 #include <vector> |
65 | 65 |
66 #include "base/base_export.h" | 66 #include "base/base_export.h" |
67 #include "base/command_line.h" | 67 #include "base/command_line.h" |
68 #include "base/gtest_prod_util.h" | 68 #include "base/gtest_prod_util.h" |
69 #include "base/macros.h" | 69 #include "base/macros.h" |
70 #include "base/memory/ref_counted.h" | 70 #include "base/memory/ref_counted.h" |
71 #include "base/memory/shared_memory.h" | 71 #include "base/memory/shared_memory.h" |
| 72 #include "base/metrics/persistent_memory_allocator.h" |
72 #include "base/observer_list_threadsafe.h" | 73 #include "base/observer_list_threadsafe.h" |
| 74 #include "base/process/launch.h" |
73 #include "base/strings/string_piece.h" | 75 #include "base/strings/string_piece.h" |
74 #include "base/synchronization/lock.h" | 76 #include "base/synchronization/lock.h" |
75 #include "base/time/time.h" | 77 #include "base/time/time.h" |
76 | 78 |
77 namespace base { | 79 namespace base { |
78 | 80 |
79 class FieldTrialList; | 81 class FieldTrialList; |
80 | 82 |
81 class BASE_EXPORT FieldTrial : public RefCounted<FieldTrial> { | 83 class BASE_EXPORT FieldTrial : public RefCounted<FieldTrial> { |
82 public: | 84 public: |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 // untouched. | 259 // untouched. |
258 bool GetActiveGroup(ActiveGroup* active_group) const; | 260 bool GetActiveGroup(ActiveGroup* active_group) const; |
259 | 261 |
260 // Returns the trial name and selected group name for this field trial via | 262 // Returns the trial name and selected group name for this field trial via |
261 // the output parameter |field_trial_state|, but only if the trial has not | 263 // the output parameter |field_trial_state|, but only if the trial has not |
262 // been disabled. In that case, true is returned and |field_trial_state| is | 264 // been disabled. In that case, true is returned and |field_trial_state| is |
263 // filled in; otherwise, the result is false and |field_trial_state| is left | 265 // filled in; otherwise, the result is false and |field_trial_state| is left |
264 // untouched. | 266 // untouched. |
265 bool GetState(State* field_trial_state); | 267 bool GetState(State* field_trial_state); |
266 | 268 |
| 269 // Adds the field trial to the allocator whose memory is to be shared with |
| 270 // child processes. Assumes the calling code has a lock around the call to |
| 271 // this function, since the check for the allocator is not thread-safe. |
| 272 void AddToAllocatorWhileLocked( |
| 273 base::SharedPersistentMemoryAllocator* allocator); |
| 274 |
267 // Returns the group_name. A winner need not have been chosen. | 275 // Returns the group_name. A winner need not have been chosen. |
268 std::string group_name_internal() const { return group_name_; } | 276 std::string group_name_internal() const { return group_name_; } |
269 | 277 |
270 // The name of the field trial, as can be found via the FieldTrialList. | 278 // The name of the field trial, as can be found via the FieldTrialList. |
271 const std::string trial_name_; | 279 const std::string trial_name_; |
272 | 280 |
273 // The maximum sum of all probabilities supplied, which corresponds to 100%. | 281 // The maximum sum of all probabilities supplied, which corresponds to 100%. |
274 // This is the scaling factor used to adjust supplied probabilities. | 282 // This is the scaling factor used to adjust supplied probabilities. |
275 const Probability divisor_; | 283 const Probability divisor_; |
276 | 284 |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 // If using shared memory to pass around the list of field trials, then | 480 // If using shared memory to pass around the list of field trials, then |
473 // expects |field_trial_handle_switch| command line argument to | 481 // expects |field_trial_handle_switch| command line argument to |
474 // contain the shared memory handle. | 482 // contain the shared memory handle. |
475 // If not, then create the trials as before (using the kForceFieldTrials | 483 // If not, then create the trials as before (using the kForceFieldTrials |
476 // switch). Needs the |field_trial_handle_switch| argument to be passed in | 484 // switch). Needs the |field_trial_handle_switch| argument to be passed in |
477 // since base/ can't depend on content/. | 485 // since base/ can't depend on content/. |
478 static void CreateTrialsFromCommandLine( | 486 static void CreateTrialsFromCommandLine( |
479 const base::CommandLine& cmd_line, | 487 const base::CommandLine& cmd_line, |
480 const char* field_trial_handle_switch); | 488 const char* field_trial_handle_switch); |
481 | 489 |
| 490 #if defined(OS_WIN) |
| 491 // On Windows, we need to explicitly pass down any handles to be inherited. |
| 492 // This function adds the shared memory handle to field trial state to the |
| 493 // list of handles to be inherited. |
| 494 static void AppendFieldTrialHandleIfNeeded( |
| 495 base::HandlesToInheritVector* handles); |
| 496 #endif |
| 497 |
482 // Adds a switch to the command line containing the field trial state as a | 498 // Adds a switch to the command line containing the field trial state as a |
483 // string (if not using shared memory to share field trial state), or the | 499 // string (if not using shared memory to share field trial state), or the |
484 // shared memory handle + length. | 500 // shared memory handle + length. |
485 // Needs the |field_trial_handle_switch| argument to be passed in since base/ | 501 // Needs the |field_trial_handle_switch| argument to be passed in since base/ |
486 // can't depend on content/. | 502 // can't depend on content/. |
487 static std::unique_ptr<base::SharedMemory> CopyFieldTrialStateToFlags( | 503 static void CopyFieldTrialStateToFlags(const char* field_trial_handle_switch, |
488 const char* field_trial_handle_switch, | 504 base::CommandLine* cmd_line); |
489 base::CommandLine* cmd_line); | |
490 | 505 |
491 // Create a FieldTrial with the given |name| and using 100% probability for | 506 // Create a FieldTrial with the given |name| and using 100% probability for |
492 // the FieldTrial, force FieldTrial to have the same group string as | 507 // the FieldTrial, force FieldTrial to have the same group string as |
493 // |group_name|. This is commonly used in a non-browser process, to carry | 508 // |group_name|. This is commonly used in a non-browser process, to carry |
494 // randomly selected state in a browser process into this non-browser process. | 509 // randomly selected state in a browser process into this non-browser process. |
495 // It returns NULL if there is a FieldTrial that is already registered with | 510 // It returns NULL if there is a FieldTrial that is already registered with |
496 // the same |name| but has different finalized group string (|group_name|). | 511 // the same |name| but has different finalized group string (|group_name|). |
497 static FieldTrial* CreateFieldTrial(const std::string& name, | 512 static FieldTrial* CreateFieldTrial(const std::string& name, |
498 const std::string& group_name); | 513 const std::string& group_name); |
499 | 514 |
500 // Add an observer to be notified when a field trial is irrevocably committed | 515 // Add an observer to be notified when a field trial is irrevocably committed |
501 // to being part of some specific field_group (and hence the group_name is | 516 // to being part of some specific field_group (and hence the group_name is |
502 // also finalized for that field_trial). | 517 // also finalized for that field_trial). |
503 static void AddObserver(Observer* observer); | 518 static void AddObserver(Observer* observer); |
504 | 519 |
505 // Remove an observer. | 520 // Remove an observer. |
506 static void RemoveObserver(Observer* observer); | 521 static void RemoveObserver(Observer* observer); |
507 | 522 |
508 // Notify all observers that a group has been finalized for |field_trial|. | 523 // Notify all observers that a group has been finalized for |field_trial|. |
509 static void NotifyFieldTrialGroupSelection(FieldTrial* field_trial); | 524 static void NotifyFieldTrialGroupSelection(FieldTrial* field_trial); |
510 | 525 |
511 // Return the number of active field trials. | 526 // Return the number of active field trials. |
512 static size_t GetFieldTrialCount(); | 527 static size_t GetFieldTrialCount(); |
513 | 528 |
514 private: | 529 private: |
| 530 // Allow tests to access our innards for testing purposes. |
| 531 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, InstantiateAllocator); |
| 532 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AddTrialsToAllocator); |
| 533 |
| 534 // Expects a mapped piece of shared memory |shm| that was created from the |
| 535 // browser process's field_trial_allocator and shared via the command line. |
| 536 // This function recreates the allocator, iterates through all the field |
| 537 // trials in it, and creates them via CreateFieldTrial(). |
| 538 static void CreateTrialsFromSharedMemory( |
| 539 std::unique_ptr<base::SharedMemory> shm); |
| 540 |
| 541 // Instantiate the field trial allocator, add all existing field trials to it, |
| 542 // and duplicates its handle to a read-only handle, which gets stored in |
| 543 // |readonly_allocator_handle|. |
| 544 static void InstantiateFieldTrialAllocatorIfNeeded(); |
| 545 |
515 // A map from FieldTrial names to the actual instances. | 546 // A map from FieldTrial names to the actual instances. |
516 typedef std::map<std::string, FieldTrial*> RegistrationMap; | 547 typedef std::map<std::string, FieldTrial*> RegistrationMap; |
517 | 548 |
518 // If one-time randomization is enabled, returns a weak pointer to the | 549 // If one-time randomization is enabled, returns a weak pointer to the |
519 // corresponding EntropyProvider. Otherwise, returns NULL. | 550 // corresponding EntropyProvider. Otherwise, returns NULL. |
520 static const FieldTrial::EntropyProvider* | 551 static const FieldTrial::EntropyProvider* |
521 GetEntropyProviderForOneTimeRandomization(); | 552 GetEntropyProviderForOneTimeRandomization(); |
522 | 553 |
523 // Helper function should be called only while holding lock_. | 554 // Helper function should be called only while holding lock_. |
524 FieldTrial* PreLockedFind(const std::string& name); | 555 FieldTrial* PreLockedFind(const std::string& name); |
(...skipping 17 matching lines...) Expand all Loading... |
542 | 573 |
543 std::map<std::string, std::string> seen_states_; | 574 std::map<std::string, std::string> seen_states_; |
544 | 575 |
545 // Entropy provider to be used for one-time randomized field trials. If NULL, | 576 // Entropy provider to be used for one-time randomized field trials. If NULL, |
546 // one-time randomization is not supported. | 577 // one-time randomization is not supported. |
547 std::unique_ptr<const FieldTrial::EntropyProvider> entropy_provider_; | 578 std::unique_ptr<const FieldTrial::EntropyProvider> entropy_provider_; |
548 | 579 |
549 // List of observers to be notified when a group is selected for a FieldTrial. | 580 // List of observers to be notified when a group is selected for a FieldTrial. |
550 scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; | 581 scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; |
551 | 582 |
| 583 // Allocator used to instantiate field trial in child processes. In the |
| 584 // future, we may want to move this to a more generic place if we want to |
| 585 // start passing more data other than field trials. |
| 586 std::unique_ptr<base::SharedPersistentMemoryAllocator> |
| 587 field_trial_allocator_ = nullptr; |
| 588 |
| 589 #if defined(OS_WIN) |
| 590 // Readonly copy of the handle to the allocator. Needs to be a member variable |
| 591 // because it's needed from both CopyFieldTrialStateToFlags() and |
| 592 // AppendFieldTrialHandleIfNeeded(). |
| 593 HANDLE readonly_allocator_handle_ = 0; |
| 594 #endif |
| 595 |
552 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); | 596 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
553 }; | 597 }; |
554 | 598 |
555 } // namespace base | 599 } // namespace base |
556 | 600 |
557 #endif // BASE_METRICS_FIELD_TRIAL_H_ | 601 #endif // BASE_METRICS_FIELD_TRIAL_H_ |
OLD | NEW |