Chromium Code Reviews| 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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/feature_list.h" | 68 #include "base/feature_list.h" |
| 69 #include "base/files/file.h" | 69 #include "base/files/file.h" |
| 70 #include "base/gtest_prod_util.h" | 70 #include "base/gtest_prod_util.h" |
| 71 #include "base/macros.h" | 71 #include "base/macros.h" |
| 72 #include "base/memory/ref_counted.h" | 72 #include "base/memory/ref_counted.h" |
| 73 #include "base/memory/shared_memory.h" | 73 #include "base/memory/shared_memory.h" |
| 74 #include "base/metrics/persistent_memory_allocator.h" | 74 #include "base/metrics/persistent_memory_allocator.h" |
| 75 #include "base/observer_list_threadsafe.h" | 75 #include "base/observer_list_threadsafe.h" |
| 76 #include "base/pickle.h" | |
| 76 #include "base/process/launch.h" | 77 #include "base/process/launch.h" |
| 77 #include "base/strings/string_piece.h" | 78 #include "base/strings/string_piece.h" |
| 78 #include "base/synchronization/lock.h" | 79 #include "base/synchronization/lock.h" |
| 79 #include "base/time/time.h" | 80 #include "base/time/time.h" |
| 80 | 81 |
| 81 namespace base { | 82 namespace base { |
| 82 | 83 |
| 83 class FieldTrialList; | 84 class FieldTrialList; |
| 84 | 85 |
| 85 class BASE_EXPORT FieldTrial : public RefCounted<FieldTrial> { | 86 class BASE_EXPORT FieldTrial : public RefCounted<FieldTrial> { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 struct BASE_EXPORT State { | 127 struct BASE_EXPORT State { |
| 127 StringPiece trial_name; | 128 StringPiece trial_name; |
| 128 StringPiece group_name; | 129 StringPiece group_name; |
| 129 bool activated; | 130 bool activated; |
| 130 | 131 |
| 131 State(); | 132 State(); |
| 132 State(const State& other); | 133 State(const State& other); |
| 133 ~State(); | 134 ~State(); |
| 134 }; | 135 }; |
| 135 | 136 |
| 137 // We create one FieldTrialEntry per field trial in shared memory, via | |
| 138 // AddToAllocatorWhileLocked. The FieldTrialEntry is followed by a | |
| 139 // base::Pickle object that we unpickle and read from. Any changes to this | |
| 140 // structure requires a bump in kFieldTrialType id defined above. | |
| 141 struct FieldTrialEntry { | |
| 142 // Expected size for 32/64-bit check. | |
| 143 static constexpr size_t kExpectedInstanceSize = 8; | |
| 144 | |
| 145 // Whether or not this field trial is activated. This is really just a | |
| 146 // boolean but marked as a uint32_t for portability reasons. | |
| 147 uint32_t activated; | |
| 148 | |
| 149 // Size of the pickled structure, NOT the total size of this entry. | |
| 150 uint32_t pickle_size; | |
| 151 | |
| 152 // Returns an iterator over the data containing names and params. | |
| 153 PickleIterator GetPickleIterator() const; | |
|
bcwhite
2016/12/09 19:40:28
Can this method be private?
lawrencewu
2016/12/09 19:47:59
Done.
| |
| 154 | |
| 155 // Takes the iterator and writes out the first two items into |trial_name| | |
| 156 // and |group_name|. | |
| 157 bool ReadStringPair(PickleIterator* iter, | |
|
bcwhite
2016/12/09 19:40:28
Can this method be private?
lawrencewu
2016/12/09 19:47:59
Done.
| |
| 158 StringPiece* trial_name, | |
| 159 StringPiece* group_name) const; | |
| 160 | |
| 161 // Calling this is only valid when the entry is initialized. That is, it | |
| 162 // resides in shared memory and has a pickle containing the trial name and | |
| 163 // group name following it. | |
| 164 bool GetTrialAndGroupName(StringPiece* trial_name, | |
| 165 StringPiece* group_name) const; | |
| 166 | |
| 167 // Calling this is only valid when the entry is initialized as well. Reads | |
| 168 // the parameters following the trial and group name and stores them as | |
| 169 // key-value mappings in |params|. | |
| 170 bool GetParams(std::map<std::string, std::string>* params) const; | |
| 171 }; | |
| 172 | |
| 136 typedef std::vector<ActiveGroup> ActiveGroups; | 173 typedef std::vector<ActiveGroup> ActiveGroups; |
| 137 | 174 |
| 138 // A return value to indicate that a given instance has not yet had a group | 175 // A return value to indicate that a given instance has not yet had a group |
| 139 // assignment (and hence is not yet participating in the trial). | 176 // assignment (and hence is not yet participating in the trial). |
| 140 static const int kNotFinalized; | 177 static const int kNotFinalized; |
| 141 | 178 |
| 142 // Disables this trial, meaning it always determines the default group | 179 // Disables this trial, meaning it always determines the default group |
| 143 // has been selected. May be called immediately after construction, or | 180 // has been selected. May be called immediately after construction, or |
| 144 // at any time after initialization (should not be interleaved with | 181 // at any time after initialization (should not be interleaved with |
| 145 // AppendGroup calls). Once disabled, there is no way to re-enable a | 182 // AppendGroup calls). Once disabled, there is no way to re-enable a |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 570 // Gets the parameters for |field_trial| from shared memory and stores them in | 607 // Gets the parameters for |field_trial| from shared memory and stores them in |
| 571 // |params|. This is only exposed for use by FieldTrialParamAssociator and | 608 // |params|. This is only exposed for use by FieldTrialParamAssociator and |
| 572 // shouldn't be used by anything else. | 609 // shouldn't be used by anything else. |
| 573 static bool GetParamsFromSharedMemory( | 610 static bool GetParamsFromSharedMemory( |
| 574 FieldTrial* field_trial, | 611 FieldTrial* field_trial, |
| 575 std::map<std::string, std::string>* params); | 612 std::map<std::string, std::string>* params); |
| 576 | 613 |
| 577 // Clears all the params in the allocator. | 614 // Clears all the params in the allocator. |
| 578 static void ClearParamsFromSharedMemoryForTesting(); | 615 static void ClearParamsFromSharedMemoryForTesting(); |
| 579 | 616 |
| 617 // We want to be able to dump field trial state to an allocator so that it | |
| 618 // can be analyzed after a crash. | |
| 619 static void DumpAllFieldTrialsToPersistentAllocator( | |
| 620 PersistentMemoryAllocator* allocator); | |
| 621 | |
| 622 // We want to be able to retrieve field trial state from an allocator so that | |
| 623 // it can be analyzed after a crash. | |
|
bcwhite
2016/12/09 19:40:28
Comment that the pointers are into the persistent
| |
| 624 static std::vector<FieldTrial::FieldTrialEntry*> | |
| 625 GetAllFieldTrialsFromPersistentAllocator( | |
| 626 PersistentMemoryAllocator* allocator); | |
|
bcwhite
2016/12/09 19:40:28
const PMA*
| |
| 627 | |
| 580 private: | 628 private: |
| 581 // Allow tests to access our innards for testing purposes. | 629 // Allow tests to access our innards for testing purposes. |
| 582 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, InstantiateAllocator); | 630 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, InstantiateAllocator); |
| 583 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AddTrialsToAllocator); | 631 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AddTrialsToAllocator); |
| 584 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, | 632 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, |
| 585 DoNotAddSimulatedFieldTrialsToAllocator); | 633 DoNotAddSimulatedFieldTrialsToAllocator); |
| 586 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AssociateFieldTrialParams); | 634 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AssociateFieldTrialParams); |
| 587 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, ClearParamsFromSharedMemory); | 635 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, ClearParamsFromSharedMemory); |
| 588 | 636 |
| 589 #if defined(OS_WIN) | 637 #if defined(OS_WIN) |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 613 static bool CreateTrialsFromSharedMemory( | 661 static bool CreateTrialsFromSharedMemory( |
| 614 std::unique_ptr<base::SharedMemory> shm); | 662 std::unique_ptr<base::SharedMemory> shm); |
| 615 | 663 |
| 616 // Instantiate the field trial allocator, add all existing field trials to it, | 664 // Instantiate the field trial allocator, add all existing field trials to it, |
| 617 // and duplicates its handle to a read-only handle, which gets stored in | 665 // and duplicates its handle to a read-only handle, which gets stored in |
| 618 // |readonly_allocator_handle|. | 666 // |readonly_allocator_handle|. |
| 619 static void InstantiateFieldTrialAllocatorIfNeeded(); | 667 static void InstantiateFieldTrialAllocatorIfNeeded(); |
| 620 | 668 |
| 621 // Adds the field trial to the allocator. Caller must hold a lock before | 669 // Adds the field trial to the allocator. Caller must hold a lock before |
| 622 // calling this. | 670 // calling this. |
| 623 static void AddToAllocatorWhileLocked(FieldTrial* field_trial); | 671 static void AddToAllocatorWhileLocked(PersistentMemoryAllocator* allocator, |
| 672 FieldTrial* field_trial); | |
| 624 | 673 |
| 625 // Activate the corresponding field trial entry struct in shared memory. | 674 // Activate the corresponding field trial entry struct in shared memory. |
| 626 static void ActivateFieldTrialEntryWhileLocked(FieldTrial* field_trial); | 675 static void ActivateFieldTrialEntryWhileLocked(FieldTrial* field_trial); |
| 627 | 676 |
| 628 // A map from FieldTrial names to the actual instances. | 677 // A map from FieldTrial names to the actual instances. |
| 629 typedef std::map<std::string, FieldTrial*> RegistrationMap; | 678 typedef std::map<std::string, FieldTrial*> RegistrationMap; |
| 630 | 679 |
| 631 // If one-time randomization is enabled, returns a weak pointer to the | 680 // If one-time randomization is enabled, returns a weak pointer to the |
| 632 // corresponding EntropyProvider. Otherwise, returns NULL. | 681 // corresponding EntropyProvider. Otherwise, returns NULL. |
| 633 static const FieldTrial::EntropyProvider* | 682 static const FieldTrial::EntropyProvider* |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 675 | 724 |
| 676 // Tracks whether CreateTrialsFromCommandLine() has been called. | 725 // Tracks whether CreateTrialsFromCommandLine() has been called. |
| 677 bool create_trials_from_command_line_called_ = false; | 726 bool create_trials_from_command_line_called_ = false; |
| 678 | 727 |
| 679 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); | 728 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
| 680 }; | 729 }; |
| 681 | 730 |
| 682 } // namespace base | 731 } // namespace base |
| 683 | 732 |
| 684 #endif // BASE_METRICS_FIELD_TRIAL_H_ | 733 #endif // BASE_METRICS_FIELD_TRIAL_H_ |
| OLD | NEW |