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 389 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 | 474 // If using shared memory to pass around the list of field trials, then |
473 // expects |field_trial_handle_switch| command line argument to | 475 // expects |field_trial_handle_switch| command line argument to |
474 // contain the shared memory handle. | 476 // contain the shared memory handle. |
475 // If not, then create the trials as before (using the kForceFieldTrials | 477 // If not, then create the trials as before (using the kForceFieldTrials |
476 // switch). Needs the |field_trial_handle_switch| argument to be passed in | 478 // switch). Needs the |field_trial_handle_switch| argument to be passed in |
477 // since base/ can't depend on content/. | 479 // since base/ can't depend on content/. |
478 static void CreateTrialsFromCommandLine( | 480 static void CreateTrialsFromCommandLine( |
479 const base::CommandLine& cmd_line, | 481 const base::CommandLine& cmd_line, |
480 const char* field_trial_handle_switch); | 482 const char* field_trial_handle_switch); |
481 | 483 |
484 #if defined(OS_WIN) | |
485 static void AppendFieldTrialHandleIfNeeded( | |
Alexei Svitkine (slow)
2016/10/21 18:20:26
Add a comment, please.
lawrencewu
2016/10/21 20:23:13
Done.
| |
486 base::HandlesToInheritVector* handles); | |
487 #endif | |
488 | |
482 // Adds a switch to the command line containing the field trial state as a | 489 // 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 | 490 // string (if not using shared memory to share field trial state), or the |
484 // shared memory handle + length. | 491 // shared memory handle + length. |
485 // Needs the |field_trial_handle_switch| argument to be passed in since base/ | 492 // Needs the |field_trial_handle_switch| argument to be passed in since base/ |
486 // can't depend on content/. | 493 // can't depend on content/. |
487 static std::unique_ptr<base::SharedMemory> CopyFieldTrialStateToFlags( | 494 static void CopyFieldTrialStateToFlags(const char* field_trial_handle_switch, |
488 const char* field_trial_handle_switch, | 495 base::CommandLine* cmd_line); |
489 base::CommandLine* cmd_line); | 496 |
497 // Returns a pointer to the global field trial allocator object for testing. | |
498 static base::SharedPersistentMemoryAllocator* | |
499 GetFieldTrialAllocatorForTesting(); | |
490 | 500 |
491 // Create a FieldTrial with the given |name| and using 100% probability for | 501 // Create a FieldTrial with the given |name| and using 100% probability for |
492 // the FieldTrial, force FieldTrial to have the same group string as | 502 // 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 | 503 // |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. | 504 // 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 | 505 // 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|). | 506 // the same |name| but has different finalized group string (|group_name|). |
497 static FieldTrial* CreateFieldTrial(const std::string& name, | 507 static FieldTrial* CreateFieldTrial(const std::string& name, |
498 const std::string& group_name); | 508 const std::string& group_name); |
499 | 509 |
500 // Add an observer to be notified when a field trial is irrevocably committed | 510 // 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 | 511 // to being part of some specific field_group (and hence the group_name is |
502 // also finalized for that field_trial). | 512 // also finalized for that field_trial). |
503 static void AddObserver(Observer* observer); | 513 static void AddObserver(Observer* observer); |
504 | 514 |
505 // Remove an observer. | 515 // Remove an observer. |
506 static void RemoveObserver(Observer* observer); | 516 static void RemoveObserver(Observer* observer); |
507 | 517 |
508 // Notify all observers that a group has been finalized for |field_trial|. | 518 // Notify all observers that a group has been finalized for |field_trial|. |
509 static void NotifyFieldTrialGroupSelection(FieldTrial* field_trial); | 519 static void NotifyFieldTrialGroupSelection(FieldTrial* field_trial); |
510 | 520 |
511 // Return the number of active field trials. | 521 // Return the number of active field trials. |
512 static size_t GetFieldTrialCount(); | 522 static size_t GetFieldTrialCount(); |
513 | 523 |
514 private: | 524 private: |
525 // Allow tests to access our innards for testing purposes. | |
526 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, InstantiateAllocator); | |
527 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AddTrialsToAllocator); | |
528 | |
529 // Expects a mapped piece of shared memory |shm| that was created from the | |
530 // browser process's field_trial_allocator and shared via the command line. | |
531 // This function recreates the allocator, iterates through all the field | |
532 // trials in it, and creates them via CreateFieldTrial(). | |
533 static void CreateTrialsFromSharedMemory( | |
534 std::unique_ptr<base::SharedMemory> shm); | |
535 | |
536 // Instantiate the field trial allocator, add all existing field trials to it, | |
537 // and duplicates its handle to a read-only handle, which gets stored in | |
538 // |readonly_allocator_handle|. | |
539 static void InstantiateFieldTrialAllocatorIfNeeded(); | |
540 | |
541 // Called in NotifyFieldTrialGroupSelection() and afterinstantiating the | |
Alexei Svitkine (slow)
2016/10/21 18:20:26
Nit: I suggest removing the first sentence - the c
lawrencewu
2016/10/21 20:23:13
Done.
| |
542 // allocator. Updates field_trial_allocator by adding the |field_trial|. Since | |
543 // the check for the allocator is not thread-safe, the caller must wrap this | |
544 // call in a lock. | |
545 static void UpdateFieldTrialAllocatorWhileLocked(FieldTrial* field_trial); | |
546 | |
515 // A map from FieldTrial names to the actual instances. | 547 // A map from FieldTrial names to the actual instances. |
516 typedef std::map<std::string, FieldTrial*> RegistrationMap; | 548 typedef std::map<std::string, FieldTrial*> RegistrationMap; |
517 | 549 |
518 // If one-time randomization is enabled, returns a weak pointer to the | 550 // If one-time randomization is enabled, returns a weak pointer to the |
519 // corresponding EntropyProvider. Otherwise, returns NULL. | 551 // corresponding EntropyProvider. Otherwise, returns NULL. |
520 static const FieldTrial::EntropyProvider* | 552 static const FieldTrial::EntropyProvider* |
521 GetEntropyProviderForOneTimeRandomization(); | 553 GetEntropyProviderForOneTimeRandomization(); |
522 | 554 |
523 // Helper function should be called only while holding lock_. | 555 // Helper function should be called only while holding lock_. |
524 FieldTrial* PreLockedFind(const std::string& name); | 556 FieldTrial* PreLockedFind(const std::string& name); |
(...skipping 17 matching lines...) Expand all Loading... | |
542 | 574 |
543 std::map<std::string, std::string> seen_states_; | 575 std::map<std::string, std::string> seen_states_; |
544 | 576 |
545 // Entropy provider to be used for one-time randomized field trials. If NULL, | 577 // Entropy provider to be used for one-time randomized field trials. If NULL, |
546 // one-time randomization is not supported. | 578 // one-time randomization is not supported. |
547 std::unique_ptr<const FieldTrial::EntropyProvider> entropy_provider_; | 579 std::unique_ptr<const FieldTrial::EntropyProvider> entropy_provider_; |
548 | 580 |
549 // List of observers to be notified when a group is selected for a FieldTrial. | 581 // List of observers to be notified when a group is selected for a FieldTrial. |
550 scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; | 582 scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; |
551 | 583 |
584 // Allocator used to instantiate field trial in child processes. In the | |
585 // future, we may want to move this to a more generic place if we want to | |
586 // start passing more data other than field trials. | |
587 std::unique_ptr<base::SharedPersistentMemoryAllocator> field_trial_allocator; | |
Alexei Svitkine (slow)
2016/10/21 18:20:26
Add _ at the end
lawrencewu
2016/10/21 20:23:13
Done.
| |
588 | |
589 #if defined(OS_WIN) | |
590 // Readonly copy of the handle to the allocator, stored here since we need it | |
591 // twice. | |
Alexei Svitkine (slow)
2016/10/21 18:20:26
"stored here since we need it twice" is very vague
lawrencewu
2016/10/21 20:23:13
Done.
| |
592 HANDLE readonly_allocator_handle; | |
Alexei Svitkine (slow)
2016/10/21 18:20:26
Add _ at the end
lawrencewu
2016/10/21 20:23:13
Done.
| |
593 #endif | |
594 | |
552 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); | 595 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
553 }; | 596 }; |
554 | 597 |
555 } // namespace base | 598 } // namespace base |
556 | 599 |
557 #endif // BASE_METRICS_FIELD_TRIAL_H_ | 600 #endif // BASE_METRICS_FIELD_TRIAL_H_ |
OLD | NEW |