Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_COMMON_METRICS_ENTROPY_PROVIDER_H_ | |
| 6 #define CHROME_COMMON_METRICS_ENTROPY_PROVIDER_H_ | |
| 7 | |
| 8 #include <functional> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/base_export.h" | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/compiler_specific.h" | |
| 14 #include "base/metrics/field_trial.h" | |
| 15 #include "third_party/mt19937ar/mt19937ar.h" | |
| 16 | |
| 17 // Internals of entropy_provider.cc exposed for testing. | |
| 18 namespace internal { | |
|
jar (doing other things)
2012/08/17 19:06:19
This seems like a strange choice of global namespa
Alexei Svitkine (slow)
2012/08/20 15:57:40
Done.
| |
| 19 | |
| 20 // A functor that generates random numbers based on a seed, using the Mersenne | |
| 21 // Twister algorithm. Suitable for use with std::random_shuffle(). | |
| 22 struct SeededRandGenerator : std::unary_function<uint32, uint32> { | |
| 23 explicit SeededRandGenerator(uint32 seed); | |
| 24 ~SeededRandGenerator(); | |
| 25 | |
| 26 // Returns a random number in range [0, range). | |
| 27 uint32 operator()(uint32 range); | |
| 28 | |
| 29 MersenneTwister mersenne_twister_; | |
| 30 }; | |
| 31 | |
| 32 // Creates unique identifier for the trial by hashing a name string, whether | |
| 33 // it's for the field trial or the group name. | |
| 34 // TODO(asvitkine): Share the implementation with variations_util.cc. | |
| 35 uint32 HashName(const std::string& name); | |
| 36 | |
| 37 } // namespace internal | |
| 38 | |
| 39 // SHA1EntropyProvider is an entropy provider suitable for high entropy | |
| 40 // sources. It works by taking the first 64 bits of the SHA1 hash of the | |
| 41 // entropy source concatenated with the trial name and using that for the | |
| 42 // final entropy value. | |
| 43 class SHA1EntropyProvider : public base::FieldTrial::EntropyProvider { | |
| 44 public: | |
| 45 // Creates a SHA1EntropyProvider with the given |entropy_source|, which | |
| 46 // should contain a large amount of entropy - for example, a textual | |
| 47 // representation of a persistent randomly-generated 64-bit value. | |
| 48 explicit SHA1EntropyProvider(const std::string& entropy_source); | |
| 49 virtual ~SHA1EntropyProvider(); | |
| 50 | |
| 51 // base::FieldTrial::EntropyProvider implementation: | |
| 52 virtual double GetEntropyForTrial(const std::string& trial_name) const | |
| 53 OVERRIDE; | |
| 54 | |
| 55 private: | |
| 56 std::string entropy_source_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(SHA1EntropyProvider); | |
| 59 }; | |
| 60 | |
| 61 // PermutedEntropyProvider is an entropy provider suitable for low entropy | |
| 62 // sources (below 16 bits). It uses the field trial name to generate a | |
| 63 // permutation of a mapping array from an initial entropy value to a new value. | |
| 64 // Note: This provider's performance is O(n^2), where n is the number of bits | |
|
hfung
2012/08/17 17:35:40
Isn't it O(low_entropy_source_max), since it has t
Alexei Svitkine (slow)
2012/08/17 17:57:40
That's correct - I accidentally transposed the two
| |
| 65 // in the entropy source. | |
| 66 class PermutedEntropyProvider : public base::FieldTrial::EntropyProvider { | |
| 67 public: | |
| 68 // Creates a PermutedEntropyProvider with the given |low_entropy_source|, | |
| 69 // which should have a value in the range of [0, low_entropy_source_max). | |
| 70 PermutedEntropyProvider(uint16 low_entropy_source, | |
| 71 size_t low_entropy_source_max); | |
| 72 virtual ~PermutedEntropyProvider(); | |
| 73 | |
| 74 // base::FieldTrial::EntropyProvider implementation: | |
| 75 virtual double GetEntropyForTrial(const std::string& trial_name) const | |
| 76 OVERRIDE; | |
| 77 | |
| 78 private: | |
| 79 uint16 low_entropy_source_; | |
| 80 size_t low_entropy_source_max_; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(PermutedEntropyProvider); | |
| 83 }; | |
| 84 | |
| 85 #endif // CHROME_COMMON_METRICS_ENTROPY_PROVIDER_H_ | |
| OLD | NEW |