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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 | 65 |
66 #include "base/atomicops.h" | 66 #include "base/atomicops.h" |
67 #include "base/base_export.h" | 67 #include "base/base_export.h" |
68 #include "base/command_line.h" | 68 #include "base/command_line.h" |
69 #include "base/feature_list.h" | 69 #include "base/feature_list.h" |
70 #include "base/files/file.h" | 70 #include "base/files/file.h" |
71 #include "base/gtest_prod_util.h" | 71 #include "base/gtest_prod_util.h" |
72 #include "base/macros.h" | 72 #include "base/macros.h" |
73 #include "base/memory/ref_counted.h" | 73 #include "base/memory/ref_counted.h" |
74 #include "base/memory/shared_memory.h" | 74 #include "base/memory/shared_memory.h" |
75 #include "base/memory/shared_memory_handle.h" | |
75 #include "base/metrics/persistent_memory_allocator.h" | 76 #include "base/metrics/persistent_memory_allocator.h" |
76 #include "base/observer_list_threadsafe.h" | 77 #include "base/observer_list_threadsafe.h" |
77 #include "base/pickle.h" | 78 #include "base/pickle.h" |
78 #include "base/process/launch.h" | 79 #include "base/process/launch.h" |
79 #include "base/strings/string_piece.h" | 80 #include "base/strings/string_piece.h" |
80 #include "base/synchronization/lock.h" | 81 #include "base/synchronization/lock.h" |
81 #include "base/time/time.h" | 82 #include "base/time/time.h" |
82 | 83 |
83 namespace base { | 84 namespace base { |
84 | 85 |
(...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
566 #if defined(OS_WIN) | 567 #if defined(OS_WIN) |
567 // On Windows, we need to explicitly pass down any handles to be inherited. | 568 // On Windows, we need to explicitly pass down any handles to be inherited. |
568 // This function adds the shared memory handle to field trial state to the | 569 // This function adds the shared memory handle to field trial state to the |
569 // list of handles to be inherited. | 570 // list of handles to be inherited. |
570 static void AppendFieldTrialHandleIfNeeded( | 571 static void AppendFieldTrialHandleIfNeeded( |
571 base::HandlesToInheritVector* handles); | 572 base::HandlesToInheritVector* handles); |
572 #endif | 573 #endif |
573 | 574 |
574 #if defined(OS_POSIX) && !defined(OS_NACL) | 575 #if defined(OS_POSIX) && !defined(OS_NACL) |
575 // On POSIX, we also need to explicitly pass down this file descriptor that | 576 // On POSIX, we also need to explicitly pass down this file descriptor that |
576 // should be shared with the child process. Returns kInvalidPlatformFile if no | 577 // should be shared with the child process. Returns an invalid handle if it |
577 // handle exists or was not initialized properly. | 578 // was not initialized properly. |
578 static PlatformFile GetFieldTrialHandle(); | 579 static base::SharedMemoryHandle GetFieldTrialHandle(); |
579 #endif | 580 #endif |
580 | 581 |
581 // Adds a switch to the command line containing the field trial state as a | 582 // Adds a switch to the command line containing the field trial state as a |
582 // string (if not using shared memory to share field trial state), or the | 583 // string (if not using shared memory to share field trial state), or the |
583 // shared memory handle + length. | 584 // shared memory handle + length. |
584 // Needs the |field_trial_handle_switch| argument to be passed in since base/ | 585 // Needs the |field_trial_handle_switch| argument to be passed in since base/ |
585 // can't depend on content/. | 586 // can't depend on content/. |
586 static void CopyFieldTrialStateToFlags(const char* field_trial_handle_switch, | 587 static void CopyFieldTrialStateToFlags(const char* field_trial_handle_switch, |
587 const char* enable_features_switch, | 588 const char* enable_features_switch, |
588 const char* disable_features_switch, | 589 const char* disable_features_switch, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
638 PersistentMemoryAllocator const& allocator); | 639 PersistentMemoryAllocator const& allocator); |
639 | 640 |
640 private: | 641 private: |
641 // Allow tests to access our innards for testing purposes. | 642 // Allow tests to access our innards for testing purposes. |
642 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, InstantiateAllocator); | 643 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, InstantiateAllocator); |
643 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AddTrialsToAllocator); | 644 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AddTrialsToAllocator); |
644 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, | 645 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, |
645 DoNotAddSimulatedFieldTrialsToAllocator); | 646 DoNotAddSimulatedFieldTrialsToAllocator); |
646 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AssociateFieldTrialParams); | 647 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AssociateFieldTrialParams); |
647 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, ClearParamsFromSharedMemory); | 648 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, ClearParamsFromSharedMemory); |
649 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, | |
650 SerializeSharedMemoryHandleMetadata); | |
651 | |
652 // Serialization passes two pieces of information. If passes a reference to | |
Alexei Svitkine (slow)
2017/05/05 22:37:51
Nit: How about "Serialization is used to pass info
erikchen
2017/05/08 23:37:49
Done.
| |
653 // the relevant OS resource, and it passes a GUID | |
Alexei Svitkine (slow)
2017/05/05 22:37:51
Nit: Add a .
erikchen
2017/05/08 23:37:49
Done.
| |
654 // Serialization and deserialization doesn't actually transport the underlying | |
655 // OS resource - that must be done by the Process launcher. | |
656 static std::string SerializeSharedMemoryHandleMetadata( | |
657 const SharedMemoryHandle& shm); | |
658 #if defined(OS_WIN) | |
659 static SharedMemoryHandle DeserializeSharedMemoryHandleMetadata( | |
660 const std::string& switch_value); | |
661 #elif defined(OS_POSIX) && !defined(OS_NACL) | |
662 static SharedMemoryHandle DeserializeSharedMemoryHandleMetadata( | |
663 int fd, | |
664 const std::string& switch_value); | |
665 #endif | |
648 | 666 |
649 #if defined(OS_WIN) | 667 #if defined(OS_WIN) |
650 // Takes in |handle_switch| from the command line which represents the shared | 668 // Takes in |handle_switch| from the command line which represents the shared |
651 // memory handle for field trials, parses it, and creates the field trials. | 669 // memory handle for field trials, parses it, and creates the field trials. |
652 // Returns true on success, false on failure. | 670 // Returns true on success, false on failure. |
653 static bool CreateTrialsFromHandleSwitch(const std::string& handle_switch); | 671 // |switch_value| also contains the serialized GUID. |
672 static bool CreateTrialsFromSwitchValue(const std::string& switch_value); | |
654 #endif | 673 #endif |
655 | 674 |
656 #if defined(OS_POSIX) && !defined(OS_NACL) | 675 #if defined(OS_POSIX) && !defined(OS_NACL) |
657 // On POSIX systems that use the zygote, we look up the correct fd that backs | 676 // On POSIX systems that use the zygote, we look up the correct fd that backs |
658 // the shared memory segment containing the field trials by looking it up via | 677 // the shared memory segment containing the field trials by looking it up via |
659 // an fd key in GlobalDescriptors. Returns true on success, false on failure. | 678 // an fd key in GlobalDescriptors. Returns true on success, false on failure. |
660 static bool CreateTrialsFromDescriptor(int fd_key); | 679 // |switch_value| also contains the serialized GUID. |
680 static bool CreateTrialsFromDescriptor(int fd_key, | |
681 const std::string& switch_value); | |
661 #endif | 682 #endif |
662 | 683 |
663 // Takes an unmapped SharedMemoryHandle, creates a SharedMemory object from it | 684 // Takes an unmapped SharedMemoryHandle, creates a SharedMemory object from it |
664 // and maps it with the correct size. | 685 // and maps it with the correct size. |
665 static bool CreateTrialsFromSharedMemoryHandle(SharedMemoryHandle shm_handle); | 686 static bool CreateTrialsFromSharedMemoryHandle(SharedMemoryHandle shm_handle); |
666 | 687 |
667 // Expects a mapped piece of shared memory |shm| that was created from the | 688 // Expects a mapped piece of shared memory |shm| that was created from the |
668 // browser process's field_trial_allocator and shared via the command line. | 689 // browser process's field_trial_allocator and shared via the command line. |
669 // This function recreates the allocator, iterates through all the field | 690 // This function recreates the allocator, iterates through all the field |
670 // trials in it, and creates them via CreateFieldTrial(). Returns true if | 691 // trials in it, and creates them via CreateFieldTrial(). Returns true if |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
724 | 745 |
725 // Allocator in shared memory containing field trial data. Used in both | 746 // Allocator in shared memory containing field trial data. Used in both |
726 // browser and child processes, but readonly in the child. | 747 // browser and child processes, but readonly in the child. |
727 // In the future, we may want to move this to a more generic place if we want | 748 // In the future, we may want to move this to a more generic place if we want |
728 // to start passing more data other than field trials. | 749 // to start passing more data other than field trials. |
729 std::unique_ptr<FieldTrialAllocator> field_trial_allocator_ = nullptr; | 750 std::unique_ptr<FieldTrialAllocator> field_trial_allocator_ = nullptr; |
730 | 751 |
731 // Readonly copy of the handle to the allocator. Needs to be a member variable | 752 // Readonly copy of the handle to the allocator. Needs to be a member variable |
732 // because it's needed from both CopyFieldTrialStateToFlags() and | 753 // because it's needed from both CopyFieldTrialStateToFlags() and |
733 // AppendFieldTrialHandleIfNeeded(). | 754 // AppendFieldTrialHandleIfNeeded(). |
734 PlatformFile readonly_allocator_handle_ = kInvalidPlatformFile; | 755 base::SharedMemoryHandle readonly_allocator_handle_; |
735 | 756 |
736 // Tracks whether CreateTrialsFromCommandLine() has been called. | 757 // Tracks whether CreateTrialsFromCommandLine() has been called. |
737 bool create_trials_from_command_line_called_ = false; | 758 bool create_trials_from_command_line_called_ = false; |
738 | 759 |
739 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); | 760 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
740 }; | 761 }; |
741 | 762 |
742 } // namespace base | 763 } // namespace base |
743 | 764 |
744 #endif // BASE_METRICS_FIELD_TRIAL_H_ | 765 #endif // BASE_METRICS_FIELD_TRIAL_H_ |
OLD | NEW |