OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #ifndef BASE_METRICS_PERSISTENT_SYSTEM_PROFILE_H_ | 5 #ifndef BASE_METRICS_PERSISTENT_SYSTEM_PROFILE_H_ |
6 #define BASE_METRICS_PERSISTENT_SYSTEM_PROFILE_H_ | 6 #define BASE_METRICS_PERSISTENT_SYSTEM_PROFILE_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
| 10 #include "base/strings/string_piece.h" |
10 #include "base/threading/thread_checker.h" | 11 #include "base/threading/thread_checker.h" |
11 #include "components/metrics/proto/system_profile.pb.h" | 12 #include "components/metrics/proto/system_profile.pb.h" |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 template <typename T> | 15 template <typename T> |
15 struct DefaultSingletonTraits; | 16 struct DefaultSingletonTraits; |
16 class PersistentMemoryAllocator; | 17 class PersistentMemoryAllocator; |
17 } // namespace base | 18 } // namespace base |
18 | 19 |
19 namespace metrics { | 20 namespace metrics { |
(...skipping 11 matching lines...) Expand all Loading... |
31 base::PersistentMemoryAllocator* memory_allocator); | 32 base::PersistentMemoryAllocator* memory_allocator); |
32 | 33 |
33 // Stores a complete system profile. Use the version taking the serialized | 34 // Stores a complete system profile. Use the version taking the serialized |
34 // version if available to avoid multiple serialization actions. The | 35 // version if available to avoid multiple serialization actions. The |
35 // |complete| flag indicates that this profile contains all known information | 36 // |complete| flag indicates that this profile contains all known information |
36 // and can replace whatever exists. If the flag is false, the profile will be | 37 // and can replace whatever exists. If the flag is false, the profile will be |
37 // stored only if there is nothing else already present. | 38 // stored only if there is nothing else already present. |
38 void SetSystemProfile(const std::string& serialized_profile, bool complete); | 39 void SetSystemProfile(const std::string& serialized_profile, bool complete); |
39 void SetSystemProfile(const SystemProfileProto& profile, bool complete); | 40 void SetSystemProfile(const SystemProfileProto& profile, bool complete); |
40 | 41 |
| 42 // Records the existence of a field trial. |
| 43 void AddFieldTrial(base::StringPiece trial, base::StringPiece group); |
| 44 |
41 // Tests if a persistent memory allocator contains an system profile. | 45 // Tests if a persistent memory allocator contains an system profile. |
42 static bool HasSystemProfile( | 46 static bool HasSystemProfile( |
43 const base::PersistentMemoryAllocator& memory_allocator); | 47 const base::PersistentMemoryAllocator& memory_allocator); |
44 | 48 |
45 // Retrieves the system profile from a persistent memory allocator. Returns | 49 // Retrieves the system profile from a persistent memory allocator. Returns |
46 // true if a profile was successfully retrieved. | 50 // true if a profile was successfully retrieved. |
47 static bool GetSystemProfile( | 51 static bool GetSystemProfile( |
48 const base::PersistentMemoryAllocator& memory_allocator, | 52 const base::PersistentMemoryAllocator& memory_allocator, |
49 SystemProfileProto* system_profile); | 53 SystemProfileProto* system_profile); |
50 | 54 |
51 private: | 55 private: |
52 friend class PersistentSystemProfileTest; | 56 friend class PersistentSystemProfileTest; |
53 | 57 |
54 // Defines record types that can be stored inside our local Allocators. | 58 // Defines record types that can be stored inside our local Allocators. |
55 enum RecordType : uint8_t { | 59 enum RecordType : uint8_t { |
56 kUnusedSpace = 0, // The default value for empty memory. | 60 kUnusedSpace = 0, // The default value for empty memory. |
57 kSystemProfileProto, | 61 kSystemProfileProto, |
| 62 kFieldTrialInfo, |
58 }; | 63 }; |
59 | 64 |
60 // A class for managing record allocations inside a persistent memory segment. | 65 // A class for managing record allocations inside a persistent memory segment. |
61 class RecordAllocator { | 66 class RecordAllocator { |
62 public: | 67 public: |
63 // Construct an allocator for writing. | 68 // Construct an allocator for writing. |
64 RecordAllocator(base::PersistentMemoryAllocator* memory_allocator, | 69 RecordAllocator(base::PersistentMemoryAllocator* memory_allocator, |
65 size_t min_size); | 70 size_t min_size); |
66 | 71 |
67 // Construct an allocator for reading. | 72 // Construct an allocator for reading. |
68 RecordAllocator(const base::PersistentMemoryAllocator* memory_allocator); | 73 RecordAllocator(const base::PersistentMemoryAllocator* memory_allocator); |
69 | 74 |
70 // These methods manage writing records to the allocator. Do not mix these | 75 // These methods manage writing records to the allocator. Do not mix these |
71 // with "read" calls; it's one or the other. | 76 // with "read" calls; it's one or the other. |
72 void Reset(); | 77 void Reset(); |
73 bool Write(RecordType type, const std::string& record); | 78 bool Write(RecordType type, base::StringPiece record); |
74 | 79 |
75 // Read a record from the allocator. Do not mix this with "write" calls; | 80 // Read a record from the allocator. Do not mix this with "write" calls; |
76 // it's one or the other. | 81 // it's one or the other. |
77 bool HasMoreData() const; | 82 bool HasMoreData() const; |
78 bool Read(RecordType* type, std::string* record) const; | 83 bool Read(RecordType* type, std::string* record) const; |
79 | 84 |
80 base::PersistentMemoryAllocator* allocator() { return allocator_; } | 85 base::PersistentMemoryAllocator* allocator() { return allocator_; } |
81 | 86 |
82 bool has_complete_profile() { return has_complete_profile_; } | 87 bool has_complete_profile() { return has_complete_profile_; } |
83 void set_complete_profile() { has_complete_profile_ = true; } | 88 void set_complete_profile() { has_complete_profile_ = true; } |
84 | 89 |
85 private: | 90 private: |
86 // Advance to the next record segment in the memory allocator. | 91 // Advance to the next record segment in the memory allocator. |
87 bool NextSegment() const; | 92 bool NextSegment() const; |
88 | 93 |
89 // Advance to the next record segment, creating a new one if necessary with | 94 // Advance to the next record segment, creating a new one if necessary with |
90 // sufficent |min_size| space. | 95 // sufficent |min_size| space. |
91 bool AddSegment(size_t min_size); | 96 bool AddSegment(size_t min_size); |
92 | 97 |
93 // Writes data to the current position, updating the passed values past | 98 // Writes data to the current position, updating the passed values past |
94 // the amount written. Returns false in case of an error. | 99 // the amount written. Returns false in case of an error. |
95 bool WriteData(RecordType type, const char** data, size_t* remaining_size); | 100 bool WriteData(RecordType type, const char** data, size_t* data_size); |
96 | 101 |
97 // Reads data from the current position, updating the passed string | 102 // Reads data from the current position, updating the passed string |
98 // in-place. |type| must be initialized to kUnusedSpace and |record| must | 103 // in-place. |type| must be initialized to kUnusedSpace and |record| must |
99 // be an empty string before the first call but unchanged thereafter. | 104 // be an empty string before the first call but unchanged thereafter. |
100 // Returns true when record is complete. | 105 // Returns true when record is complete. |
101 bool ReadData(RecordType* type, std::string* record) const; | 106 bool ReadData(RecordType* type, std::string* record) const; |
102 | 107 |
103 // This never changes but can't be "const" because vector calls operator=(). | 108 // This never changes but can't be "const" because vector calls operator=(). |
104 base::PersistentMemoryAllocator* allocator_; // Storage location. | 109 base::PersistentMemoryAllocator* allocator_; // Storage location. |
105 | 110 |
106 // Indicates if a complete profile has been stored. | 111 // Indicates if a complete profile has been stored. |
107 bool has_complete_profile_; | 112 bool has_complete_profile_; |
108 | 113 |
109 // These change even though the underlying data may be "const". | 114 // These change even though the underlying data may be "const". |
110 mutable uint32_t alloc_reference_; // Last storage block. | 115 mutable uint32_t alloc_reference_; // Last storage block. |
111 mutable size_t alloc_size_; // Size of the block. | 116 mutable size_t alloc_size_; // Size of the block. |
112 mutable size_t end_offset_; // End of data in block. | 117 mutable size_t end_offset_; // End of data in block. |
113 | 118 |
114 // Copy and assign are allowed for easy use with STL containers. | 119 // Copy and assign are allowed for easy use with STL containers. |
115 }; | 120 }; |
116 | 121 |
| 122 // Write a record to all registered allocators. |
| 123 void WriteToAll(RecordType type, base::StringPiece record); |
| 124 |
| 125 // Merges all "update" records into a system profile. |
| 126 static void MergeUpdateRecords( |
| 127 const base::PersistentMemoryAllocator& memory_allocator, |
| 128 SystemProfileProto* system_profile); |
| 129 |
117 // The list of registered persistent allocators, described by RecordAllocator | 130 // The list of registered persistent allocators, described by RecordAllocator |
118 // instances. | 131 // instances. |
119 std::vector<RecordAllocator> allocators_; | 132 std::vector<RecordAllocator> allocators_; |
120 | 133 |
121 // Indicates if a complete profile has been stored to all allocators. | 134 // Indicates if a complete profile has been stored to all allocators. |
122 bool all_have_complete_profile_ = false; | 135 bool all_have_complete_profile_ = false; |
123 | 136 |
124 THREAD_CHECKER(thread_checker_); | 137 THREAD_CHECKER(thread_checker_); |
125 | 138 |
126 DISALLOW_COPY_AND_ASSIGN(PersistentSystemProfile); | 139 DISALLOW_COPY_AND_ASSIGN(PersistentSystemProfile); |
127 }; | 140 }; |
128 | 141 |
129 // A singleton instance of the above. | 142 // A singleton instance of the above. |
130 class GlobalPersistentSystemProfile : public PersistentSystemProfile { | 143 class GlobalPersistentSystemProfile : public PersistentSystemProfile { |
131 public: | 144 public: |
132 static GlobalPersistentSystemProfile* GetInstance(); | 145 static GlobalPersistentSystemProfile* GetInstance(); |
133 | 146 |
134 private: | 147 private: |
135 friend struct base::DefaultSingletonTraits<GlobalPersistentSystemProfile>; | 148 friend struct base::DefaultSingletonTraits<GlobalPersistentSystemProfile>; |
136 | 149 |
137 GlobalPersistentSystemProfile() {} | 150 GlobalPersistentSystemProfile() {} |
138 ~GlobalPersistentSystemProfile() {} | 151 ~GlobalPersistentSystemProfile() {} |
139 | 152 |
140 DISALLOW_COPY_AND_ASSIGN(GlobalPersistentSystemProfile); | 153 DISALLOW_COPY_AND_ASSIGN(GlobalPersistentSystemProfile); |
141 }; | 154 }; |
142 | 155 |
143 } // namespace metrics | 156 } // namespace metrics |
144 | 157 |
145 #endif // BASE_METRICS_PERSISTENT_SYSTEM_PROFILE_H_ | 158 #endif // BASE_METRICS_PERSISTENT_SYSTEM_PROFILE_H_ |
OLD | NEW |