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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 #include <stdint.h> | 58 #include <stdint.h> |
| 59 | 59 |
| 60 #include <map> | 60 #include <map> |
| 61 #include <memory> | 61 #include <memory> |
| 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/files/file.h" | |
| 68 #include "base/gtest_prod_util.h" | 69 #include "base/gtest_prod_util.h" |
| 69 #include "base/macros.h" | 70 #include "base/macros.h" |
| 70 #include "base/memory/ref_counted.h" | 71 #include "base/memory/ref_counted.h" |
| 71 #include "base/memory/shared_memory.h" | 72 #include "base/memory/shared_memory.h" |
| 72 #include "base/metrics/persistent_memory_allocator.h" | 73 #include "base/metrics/persistent_memory_allocator.h" |
| 73 #include "base/observer_list_threadsafe.h" | 74 #include "base/observer_list_threadsafe.h" |
| 74 #include "base/process/launch.h" | 75 #include "base/process/launch.h" |
| 75 #include "base/strings/string_piece.h" | 76 #include "base/strings/string_piece.h" |
| 76 #include "base/synchronization/lock.h" | 77 #include "base/synchronization/lock.h" |
| 77 #include "base/time/time.h" | 78 #include "base/time/time.h" |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 489 // through a command line argument to the browser process. Created field | 490 // through a command line argument to the browser process. Created field |
| 490 // trials will be marked "used" for the purposes of active trial reporting | 491 // trials will be marked "used" for the purposes of active trial reporting |
| 491 // if they are prefixed with |kActivationMarker|. Trial names in | 492 // if they are prefixed with |kActivationMarker|. Trial names in |
| 492 // |ignored_trial_names| are ignored when parsing |trials_string|. | 493 // |ignored_trial_names| are ignored when parsing |trials_string|. |
| 493 static bool CreateTrialsFromString( | 494 static bool CreateTrialsFromString( |
| 494 const std::string& trials_string, | 495 const std::string& trials_string, |
| 495 const std::set<std::string>& ignored_trial_names); | 496 const std::set<std::string>& ignored_trial_names); |
| 496 | 497 |
| 497 // Achieves the same thing as CreateTrialsFromString, except wraps the logic | 498 // Achieves the same thing as CreateTrialsFromString, except wraps the logic |
| 498 // by taking in the trials from the command line, either via shared memory | 499 // by taking in the trials from the command line, either via shared memory |
| 499 // handle or command line argument. | 500 // handle or command line argument. A bit of a misnomer since on POSIX we |
| 500 // If using shared memory to pass around the list of field trials, then | 501 // get the trials from opening |field_trial_descriptor_id| if using shared |
| 501 // expects |field_trial_handle_switch| command line argument to | 502 // memory. On Windows, we expect the |cmd_line| switch for |
| 502 // contain the shared memory handle. | 503 // |field_trial_handle_switch| to contain the shared memory handle that |
| 503 // If not, then create the trials as before (using the kForceFieldTrials | 504 // contains the field trial allocator. |
| 504 // switch). Needs the |field_trial_handle_switch| argument to be passed in | 505 // We need the |field_trial_handle_switch| and |field_trial_descriptor_id| |
| 505 // since base/ can't depend on content/. | 506 // arguments to be passed in since base/ can't depend on content/. |
| 506 static void CreateTrialsFromCommandLine( | 507 static void CreateTrialsFromCommandLine(const base::CommandLine& cmd_line, |
| 507 const base::CommandLine& cmd_line, | 508 const char* field_trial_handle_switch, |
| 508 const char* field_trial_handle_switch); | 509 int field_trial_descriptor_id); |
| 509 | 510 |
| 510 #if defined(OS_WIN) | 511 #if defined(OS_WIN) |
| 511 // On Windows, we need to explicitly pass down any handles to be inherited. | 512 // On Windows, we need to explicitly pass down any handles to be inherited. |
| 512 // This function adds the shared memory handle to field trial state to the | 513 // This function adds the shared memory handle to field trial state to the |
| 513 // list of handles to be inherited. | 514 // list of handles to be inherited. |
| 514 static void AppendFieldTrialHandleIfNeeded( | 515 static void AppendFieldTrialHandleIfNeeded( |
| 515 base::HandlesToInheritVector* handles); | 516 base::HandlesToInheritVector* handles); |
| 517 #elif defined(OS_POSIX) | |
| 518 // On POSIX, we also need to explicitly pass down this file descriptor that | |
| 519 // should be shared with the child process. Returns kInvalidPlatformFile if no | |
| 520 // handle exists or was not initialized properly. | |
| 521 static PlatformFile GetFieldTrialHandle(); | |
| 516 #endif | 522 #endif |
| 517 | 523 |
| 518 // Adds a switch to the command line containing the field trial state as a | 524 // Adds a switch to the command line containing the field trial state as a |
| 519 // string (if not using shared memory to share field trial state), or the | 525 // string (if not using shared memory to share field trial state), or the |
| 520 // shared memory handle + length. | 526 // shared memory handle + length. |
| 521 // Needs the |field_trial_handle_switch| argument to be passed in since base/ | 527 // Needs the |field_trial_handle_switch| argument to be passed in since base/ |
| 522 // can't depend on content/. | 528 // can't depend on content/. |
| 523 static void CopyFieldTrialStateToFlags(const char* field_trial_handle_switch, | 529 static void CopyFieldTrialStateToFlags(const char* field_trial_handle_switch, |
| 524 base::CommandLine* cmd_line); | 530 base::CommandLine* cmd_line); |
| 525 | 531 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 551 static size_t GetFieldTrialCount(); | 557 static size_t GetFieldTrialCount(); |
| 552 | 558 |
| 553 private: | 559 private: |
| 554 // Allow tests to access our innards for testing purposes. | 560 // Allow tests to access our innards for testing purposes. |
| 555 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, InstantiateAllocator); | 561 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, InstantiateAllocator); |
| 556 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AddTrialsToAllocator); | 562 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, AddTrialsToAllocator); |
| 557 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, | 563 FRIEND_TEST_ALL_PREFIXES(FieldTrialListTest, |
| 558 DoNotAddSimulatedFieldTrialsToAllocator); | 564 DoNotAddSimulatedFieldTrialsToAllocator); |
| 559 | 565 |
| 560 #if defined(OS_WIN) | 566 #if defined(OS_WIN) |
| 561 // Takes in |handle| that should have been retrieved from the command line and | 567 // Takes in |handle_switch| from the command line which represents the shared |
| 562 // creates a SharedMemoryHandle from it, and then calls | 568 // memory handle for field trials, parses it, and creates the field trials. |
| 563 // CreateTrialsFromSharedMemory(). Returns true on success, false on failure. | 569 // Returns true on success, false on failure. |
| 564 static bool CreateTrialsFromWindowsHandle(HANDLE handle); | 570 static bool CreateTrialsFromHandleSwitch(const std::string& handle_switch); |
| 565 #endif | 571 #endif |
| 566 | 572 |
| 573 #if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_MACOSX) && \ | |
| 574 !defined(OS_ANDROID) | |
| 575 // On POSIX systems that use the zygote, we look up the correct fd that backs | |
| 576 // the shared memory segment containing the field trials by looking it up via | |
| 577 // an fd key in GlobalDescriptors. Returns true on success, false on failure. | |
| 578 static bool CreateTrialsFromDescriptor(int fd_key); | |
|
Alexei Svitkine (slow)
2016/11/29 19:32:47
Nit: You use fd_key here but field_trial_descripto
lawrencewu
2016/11/29 19:52:44
Done.
| |
| 579 #endif | |
| 580 | |
| 581 // Takes an unmapped SharedMemoryHandle, creates a SharedMemory object from it | |
| 582 // and maps it with the correct size. | |
| 583 static bool CreateTrialsFromSharedMemoryHandle(SharedMemoryHandle shm_handle); | |
| 584 | |
| 567 // Expects a mapped piece of shared memory |shm| that was created from the | 585 // Expects a mapped piece of shared memory |shm| that was created from the |
| 568 // browser process's field_trial_allocator and shared via the command line. | 586 // browser process's field_trial_allocator and shared via the command line. |
| 569 // This function recreates the allocator, iterates through all the field | 587 // This function recreates the allocator, iterates through all the field |
| 570 // trials in it, and creates them via CreateFieldTrial(). Returns true if | 588 // trials in it, and creates them via CreateFieldTrial(). Returns true if |
| 571 // successful and false otherwise. | 589 // successful and false otherwise. |
| 572 static bool CreateTrialsFromSharedMemory( | 590 static bool CreateTrialsFromSharedMemory( |
| 573 std::unique_ptr<base::SharedMemory> shm); | 591 std::unique_ptr<base::SharedMemory> shm); |
| 574 | 592 |
| 575 // Instantiate the field trial allocator, add all existing field trials to it, | 593 // Instantiate the field trial allocator, add all existing field trials to it, |
| 576 // and duplicates its handle to a read-only handle, which gets stored in | 594 // and duplicates its handle to a read-only handle, which gets stored in |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 620 | 638 |
| 621 // List of observers to be notified when a group is selected for a FieldTrial. | 639 // List of observers to be notified when a group is selected for a FieldTrial. |
| 622 scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; | 640 scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; |
| 623 | 641 |
| 624 // Allocator in shared memory containing field trial data. Used in both | 642 // Allocator in shared memory containing field trial data. Used in both |
| 625 // browser and child processes, but readonly in the child. | 643 // browser and child processes, but readonly in the child. |
| 626 // In the future, we may want to move this to a more generic place if we want | 644 // In the future, we may want to move this to a more generic place if we want |
| 627 // to start passing more data other than field trials. | 645 // to start passing more data other than field trials. |
| 628 std::unique_ptr<FieldTrialAllocator> field_trial_allocator_ = nullptr; | 646 std::unique_ptr<FieldTrialAllocator> field_trial_allocator_ = nullptr; |
| 629 | 647 |
| 630 #if defined(OS_WIN) | |
| 631 // Readonly copy of the handle to the allocator. Needs to be a member variable | 648 // Readonly copy of the handle to the allocator. Needs to be a member variable |
| 632 // because it's needed from both CopyFieldTrialStateToFlags() and | 649 // because it's needed from both CopyFieldTrialStateToFlags() and |
| 633 // AppendFieldTrialHandleIfNeeded(). | 650 // AppendFieldTrialHandleIfNeeded(). |
| 634 HANDLE readonly_allocator_handle_ = nullptr; | 651 PlatformFile readonly_allocator_handle_ = kInvalidPlatformFile; |
| 635 #endif | |
| 636 | 652 |
| 637 // Tracks whether CreateTrialsFromCommandLine() has been called. | 653 // Tracks whether CreateTrialsFromCommandLine() has been called. |
| 638 bool create_trials_from_command_line_called_ = false; | 654 bool create_trials_from_command_line_called_ = false; |
| 639 | 655 |
| 640 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); | 656 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
| 641 }; | 657 }; |
| 642 | 658 |
| 643 } // namespace base | 659 } // namespace base |
| 644 | 660 |
| 645 #endif // BASE_METRICS_FIELD_TRIAL_H_ | 661 #endif // BASE_METRICS_FIELD_TRIAL_H_ |
| OLD | NEW |