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 #include <vector> | |
11 | |
12 #include "base/base_export.h" | |
13 #include "base/basictypes.h" | |
14 #include "base/compiler_specific.h" | |
15 #include "base/metrics/field_trial.h" | |
16 #include "third_party/mt19937ar/mt19937ar.h" | |
17 | |
18 // Internals of entropy_provider.cc exposed for testing. | |
19 namespace metrics_internal { | |
Ilya Sherman
2012/08/21 03:42:27
nit: Actually, since this is all new code, let's w
Alexei Svitkine (slow)
2012/08/21 14:25:49
Done.
| |
20 | |
21 // A functor that generates random numbers based on a seed, using the Mersenne | |
22 // Twister algorithm. Suitable for use with std::random_shuffle(). | |
23 struct SeededRandGenerator : std::unary_function<uint32, uint32> { | |
24 explicit SeededRandGenerator(uint32 seed); | |
25 ~SeededRandGenerator(); | |
26 | |
27 // Returns a random number in range [0, range). | |
28 uint32 operator()(uint32 range); | |
29 | |
30 MersenneTwister mersenne_twister_; | |
31 }; | |
32 | |
33 // Creates unique identifier for the trial by hashing a name string, whether | |
34 // it's for the field trial or the group name. | |
35 // TODO(asvitkine): Share the implementation with variations_util.cc. | |
36 uint32 HashName(const std::string& name); | |
37 | |
38 // Fills |mapping| to create a bijection of values in the range of | |
39 // [0, |mapping.size()|), permuted based on |trial_name|. | |
40 void PermuteMappingUsingTrialName(const std::string& trial_name, | |
41 std::vector<uint16>* mapping); | |
42 | |
43 } // namespace metrics_internal | |
44 | |
45 // SHA1EntropyProvider is an entropy provider suitable for high entropy | |
46 // sources. It works by taking the first 64 bits of the SHA1 hash of the | |
47 // entropy source concatenated with the trial name and using that for the | |
48 // final entropy value. | |
49 class SHA1EntropyProvider : public base::FieldTrial::EntropyProvider { | |
50 public: | |
51 // Creates a SHA1EntropyProvider with the given |entropy_source|, which | |
52 // should contain a large amount of entropy - for example, a textual | |
53 // representation of a persistent randomly-generated 128-bit value. | |
54 explicit SHA1EntropyProvider(const std::string& entropy_source); | |
55 virtual ~SHA1EntropyProvider(); | |
56 | |
57 // base::FieldTrial::EntropyProvider implementation: | |
58 virtual double GetEntropyForTrial(const std::string& trial_name) const | |
59 OVERRIDE; | |
60 | |
61 private: | |
62 std::string entropy_source_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(SHA1EntropyProvider); | |
65 }; | |
66 | |
67 // PermutedEntropyProvider is an entropy provider suitable for low entropy | |
68 // sources (below 16 bits). It uses the field trial name to generate a | |
69 // permutation of a mapping array from an initial entropy value to a new value. | |
70 // Note: This provider's performance is O(2^n), where n is the number of bits | |
71 // in the entropy source. | |
72 class PermutedEntropyProvider : public base::FieldTrial::EntropyProvider { | |
73 public: | |
74 // Creates a PermutedEntropyProvider with the given |low_entropy_source|, | |
75 // which should have a value in the range of [0, low_entropy_source_max). | |
76 PermutedEntropyProvider(uint16 low_entropy_source, | |
77 size_t low_entropy_source_max); | |
78 virtual ~PermutedEntropyProvider(); | |
79 | |
80 // base::FieldTrial::EntropyProvider implementation: | |
81 virtual double GetEntropyForTrial(const std::string& trial_name) const | |
82 OVERRIDE; | |
83 | |
84 private: | |
85 uint16 low_entropy_source_; | |
86 size_t low_entropy_source_max_; | |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(PermutedEntropyProvider); | |
89 }; | |
90 | |
91 #endif // CHROME_COMMON_METRICS_ENTROPY_PROVIDER_H_ | |
OLD | NEW |