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 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" |
| 73 #include "base/strings/string_piece.h" | 74 #include "base/strings/string_piece.h" |
| 74 #include "base/synchronization/lock.h" | 75 #include "base/synchronization/lock.h" |
| 75 #include "base/time/time.h" | 76 #include "base/time/time.h" |
| 76 | 77 |
| 77 namespace base { | 78 namespace base { |
| 78 | 79 |
| 79 class FieldTrialList; | 80 class FieldTrialList; |
| 80 | 81 |
| 81 class BASE_EXPORT FieldTrial : public RefCounted<FieldTrial> { | 82 class BASE_EXPORT FieldTrial : public RefCounted<FieldTrial> { |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 | 318 |
| 318 DISALLOW_COPY_AND_ASSIGN(FieldTrial); | 319 DISALLOW_COPY_AND_ASSIGN(FieldTrial); |
| 319 }; | 320 }; |
| 320 | 321 |
| 321 //------------------------------------------------------------------------------ | 322 //------------------------------------------------------------------------------ |
| 322 // Class with a list of all active field trials. A trial is active if it has | 323 // Class with a list of all active field trials. A trial is active if it has |
| 323 // been registered, which includes evaluating its state based on its probaility. | 324 // been registered, which includes evaluating its state based on its probaility. |
| 324 // Only one instance of this class exists. | 325 // Only one instance of this class exists. |
| 325 class BASE_EXPORT FieldTrialList { | 326 class BASE_EXPORT FieldTrialList { |
| 326 public: | 327 public: |
| 328 // Allocator used instantiate field trial in child processes. | |
| 329 static base::SharedPersistentMemoryAllocator* field_trial_allocator; | |
|
bcwhite
2016/10/13 14:28:02
I don't suppose there is a more generic place to h
lawrencewu
2016/10/14 04:54:10
While that would be nice, I'm reluctant to do that
bcwhite
2016/10/14 13:56:56
That's fine. Add a comment saying that such _coul
lawrencewu
2016/10/19 16:21:28
Done.
| |
| 330 | |
| 327 // Year that is guaranteed to not be expired when instantiating a field trial | 331 // Year that is guaranteed to not be expired when instantiating a field trial |
| 328 // via |FactoryGetFieldTrial()|. Set to two years from the build date. | 332 // via |FactoryGetFieldTrial()|. Set to two years from the build date. |
| 329 static int kNoExpirationYear; | 333 static int kNoExpirationYear; |
| 330 | 334 |
| 331 // Observer is notified when a FieldTrial's group is selected. | 335 // Observer is notified when a FieldTrial's group is selected. |
| 332 class BASE_EXPORT Observer { | 336 class BASE_EXPORT Observer { |
| 333 public: | 337 public: |
| 334 // Notify observers when FieldTrials's group is selected. | 338 // Notify observers when FieldTrials's group is selected. |
| 335 virtual void OnFieldTrialGroupFinalized(const std::string& trial_name, | 339 virtual void OnFieldTrialGroupFinalized(const std::string& trial_name, |
| 336 const std::string& group_name) = 0; | 340 const std::string& group_name) = 0; |
| (...skipping 135 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 | 476 // If using shared memory to pass around the list of field trials, then |
| 473 // expects |field_trial_handle_switch| command line argument to | 477 // expects |field_trial_handle_switch| command line argument to |
| 474 // contain the shared memory handle. | 478 // contain the shared memory handle. |
| 475 // If not, then create the trials as before (using the kForceFieldTrials | 479 // If not, then create the trials as before (using the kForceFieldTrials |
| 476 // switch). Needs the |field_trial_handle_switch| argument to be passed in | 480 // switch). Needs the |field_trial_handle_switch| argument to be passed in |
| 477 // since base/ can't depend on content/. | 481 // since base/ can't depend on content/. |
| 478 static void CreateTrialsFromCommandLine( | 482 static void CreateTrialsFromCommandLine( |
| 479 const base::CommandLine& cmd_line, | 483 const base::CommandLine& cmd_line, |
| 480 const char* field_trial_handle_switch); | 484 const char* field_trial_handle_switch); |
| 481 | 485 |
| 486 // Expects a mapped piece of shared memory |shm| that was created from the | |
| 487 // browser process's field_trial_allocator and shared via the command line. | |
| 488 // This function recreates the allocator, iterates through all the field | |
| 489 // trials in it, and creates them via CreateFieldTrial(). | |
| 490 static void CreateTrialsFromSharedMemory( | |
| 491 std::unique_ptr<base::SharedMemory> shm); | |
|
Alexei Svitkine (slow)
2016/10/12 20:41:00
If you're only calling this from the .cc file, it
lawrencewu
2016/10/14 04:54:09
It needs to reference regstration_ when creating t
| |
| 492 | |
| 482 // Adds a switch to the command line containing the field trial state as a | 493 // 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 | 494 // string (if not using shared memory to share field trial state), or the |
| 484 // shared memory handle + length. | 495 // shared memory handle + length. |
| 485 // Needs the |field_trial_handle_switch| argument to be passed in since base/ | 496 // Needs the |field_trial_handle_switch| argument to be passed in since base/ |
| 486 // can't depend on content/. | 497 // can't depend on content/. |
| 487 static std::unique_ptr<base::SharedMemory> CopyFieldTrialStateToFlags( | 498 static void CopyFieldTrialStateToFlags(const char* field_trial_handle_switch, |
| 488 const char* field_trial_handle_switch, | 499 base::CommandLine* cmd_line); |
| 489 base::CommandLine* cmd_line); | |
| 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 |
| 521 // Called in NotifyFieldTrialGroupSelection(). Adds a new entry to | |
| 522 // field_trial_allocator containing the newly-selected field trial and group. | |
| 523 static void UpdateFieldTrialAllocator(FieldTrial* field_trial); | |
|
bcwhite
2016/10/13 14:28:02
Your comment says "adds" but the name says "update
lawrencewu
2016/10/14 04:54:09
It is kind of an "update the list of field trials
| |
| 524 | |
| 511 // Return the number of active field trials. | 525 // Return the number of active field trials. |
| 512 static size_t GetFieldTrialCount(); | 526 static size_t GetFieldTrialCount(); |
| 513 | 527 |
| 514 private: | 528 private: |
| 515 // A map from FieldTrial names to the actual instances. | 529 // A map from FieldTrial names to the actual instances. |
| 516 typedef std::map<std::string, FieldTrial*> RegistrationMap; | 530 typedef std::map<std::string, FieldTrial*> RegistrationMap; |
| 517 | 531 |
| 518 // If one-time randomization is enabled, returns a weak pointer to the | 532 // If one-time randomization is enabled, returns a weak pointer to the |
| 519 // corresponding EntropyProvider. Otherwise, returns NULL. | 533 // corresponding EntropyProvider. Otherwise, returns NULL. |
| 520 static const FieldTrial::EntropyProvider* | 534 static const FieldTrial::EntropyProvider* |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 548 | 562 |
| 549 // List of observers to be notified when a group is selected for a FieldTrial. | 563 // List of observers to be notified when a group is selected for a FieldTrial. |
| 550 scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; | 564 scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; |
| 551 | 565 |
| 552 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); | 566 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
| 553 }; | 567 }; |
| 554 | 568 |
| 555 } // namespace base | 569 } // namespace base |
| 556 | 570 |
| 557 #endif // BASE_METRICS_FIELD_TRIAL_H_ | 571 #endif // BASE_METRICS_FIELD_TRIAL_H_ |
| OLD | NEW |