| 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/threading/thread_checker.h" | 10 #include "base/threading/thread_checker.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 PersistentSystemProfile(); | 24 PersistentSystemProfile(); |
| 25 ~PersistentSystemProfile(); | 25 ~PersistentSystemProfile(); |
| 26 | 26 |
| 27 // This object can store records in multiple memory allocators. | 27 // This object can store records in multiple memory allocators. |
| 28 void RegisterPersistentAllocator( | 28 void RegisterPersistentAllocator( |
| 29 base::PersistentMemoryAllocator* memory_allocator); | 29 base::PersistentMemoryAllocator* memory_allocator); |
| 30 void DeregisterPersistentAllocator( | 30 void DeregisterPersistentAllocator( |
| 31 base::PersistentMemoryAllocator* memory_allocator); | 31 base::PersistentMemoryAllocator* memory_allocator); |
| 32 | 32 |
| 33 // Stores a complete system profile. Use the version taking the serialized | 33 // Stores a complete system profile. Use the version taking the serialized |
| 34 // version if available to avoid multiple serialization actions. | 34 // version if available to avoid multiple serialization actions. The |
| 35 void SetSystemProfile(const std::string& serialized_profile); | 35 // |complete| flag indicates that this profile contains all known information |
| 36 void SetSystemProfile(const SystemProfileProto& profile); | 36 // 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 void SetSystemProfile(const std::string& serialized_profile, bool complete); |
| 39 void SetSystemProfile(const SystemProfileProto& profile, bool complete); |
| 37 | 40 |
| 38 // Tests if a persistent memory allocator contains an system profile. | 41 // Tests if a persistent memory allocator contains an system profile. |
| 39 static bool HasSystemProfile( | 42 static bool HasSystemProfile( |
| 40 const base::PersistentMemoryAllocator& memory_allocator); | 43 const base::PersistentMemoryAllocator& memory_allocator); |
| 41 | 44 |
| 42 // Retrieves the system profile from a persistent memory allocator. Returns | 45 // Retrieves the system profile from a persistent memory allocator. Returns |
| 43 // true if a profile was successfully retrieved. | 46 // true if a profile was successfully retrieved. |
| 44 static bool GetSystemProfile( | 47 static bool GetSystemProfile( |
| 45 const base::PersistentMemoryAllocator& memory_allocator, | 48 const base::PersistentMemoryAllocator& memory_allocator, |
| 46 SystemProfileProto* system_profile); | 49 SystemProfileProto* system_profile); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 69 void Reset(); | 72 void Reset(); |
| 70 bool Write(RecordType type, const std::string& record); | 73 bool Write(RecordType type, const std::string& record); |
| 71 | 74 |
| 72 // Read a record from the allocator. Do not mix this with "write" calls; | 75 // Read a record from the allocator. Do not mix this with "write" calls; |
| 73 // it's one or the other. | 76 // it's one or the other. |
| 74 bool HasMoreData() const; | 77 bool HasMoreData() const; |
| 75 bool Read(RecordType* type, std::string* record) const; | 78 bool Read(RecordType* type, std::string* record) const; |
| 76 | 79 |
| 77 base::PersistentMemoryAllocator* allocator() { return allocator_; } | 80 base::PersistentMemoryAllocator* allocator() { return allocator_; } |
| 78 | 81 |
| 82 bool has_complete_profile() { return has_complete_profile_; } |
| 83 void set_complete_profile() { has_complete_profile_ = true; } |
| 84 |
| 79 private: | 85 private: |
| 80 // Advance to the next record segment in the memory allocator. | 86 // Advance to the next record segment in the memory allocator. |
| 81 bool NextSegment() const; | 87 bool NextSegment() const; |
| 82 | 88 |
| 83 // Advance to the next record segment, creating a new one if necessary with | 89 // Advance to the next record segment, creating a new one if necessary with |
| 84 // sufficent |min_size| space. | 90 // sufficent |min_size| space. |
| 85 bool AddSegment(size_t min_size); | 91 bool AddSegment(size_t min_size); |
| 86 | 92 |
| 87 // Writes data to the current position, updating the passed values past | 93 // Writes data to the current position, updating the passed values past |
| 88 // the amount written. Returns false in case of an error. | 94 // the amount written. Returns false in case of an error. |
| 89 bool WriteData(RecordType type, const char** data, size_t* remaining_size); | 95 bool WriteData(RecordType type, const char** data, size_t* remaining_size); |
| 90 | 96 |
| 91 // Reads data from the current position, updating the passed string | 97 // Reads data from the current position, updating the passed string |
| 92 // in-place. |type| must be initialized to kUnusedSpace and |record| must | 98 // in-place. |type| must be initialized to kUnusedSpace and |record| must |
| 93 // be an empty string before the first call but unchanged thereafter. | 99 // be an empty string before the first call but unchanged thereafter. |
| 94 // Returns true when record is complete. | 100 // Returns true when record is complete. |
| 95 bool ReadData(RecordType* type, std::string* record) const; | 101 bool ReadData(RecordType* type, std::string* record) const; |
| 96 | 102 |
| 97 // This never changes but can't be "const" because vector calls operator=(). | 103 // This never changes but can't be "const" because vector calls operator=(). |
| 98 base::PersistentMemoryAllocator* allocator_; // Storage location. | 104 base::PersistentMemoryAllocator* allocator_; // Storage location. |
| 99 | 105 |
| 106 // Indicates if a complete profile has been stored. |
| 107 bool has_complete_profile_; |
| 108 |
| 100 // These change even though the underlying data may be "const". | 109 // These change even though the underlying data may be "const". |
| 101 mutable uint32_t alloc_reference_; // Last storage block. | 110 mutable uint32_t alloc_reference_; // Last storage block. |
| 102 mutable size_t alloc_size_; // Size of the block. | 111 mutable size_t alloc_size_; // Size of the block. |
| 103 mutable size_t end_offset_; // End of data in block. | 112 mutable size_t end_offset_; // End of data in block. |
| 104 | 113 |
| 105 // Copy and assign are allowed for easy use with STL containers. | 114 // Copy and assign are allowed for easy use with STL containers. |
| 106 }; | 115 }; |
| 107 | 116 |
| 108 // The list of registered persistent allocators, described by RecordAllocator | 117 // The list of registered persistent allocators, described by RecordAllocator |
| 109 // instances. | 118 // instances. |
| 110 std::vector<RecordAllocator> allocators_; | 119 std::vector<RecordAllocator> allocators_; |
| 111 | 120 |
| 121 // Indicates if a complete profile has been stored to all allocators. |
| 122 bool all_have_complete_profile_ = false; |
| 123 |
| 112 THREAD_CHECKER(thread_checker_); | 124 THREAD_CHECKER(thread_checker_); |
| 113 | 125 |
| 114 DISALLOW_COPY_AND_ASSIGN(PersistentSystemProfile); | 126 DISALLOW_COPY_AND_ASSIGN(PersistentSystemProfile); |
| 115 }; | 127 }; |
| 116 | 128 |
| 117 // A singleton instance of the above. | 129 // A singleton instance of the above. |
| 118 class GlobalPersistentSystemProfile : public PersistentSystemProfile { | 130 class GlobalPersistentSystemProfile : public PersistentSystemProfile { |
| 119 public: | 131 public: |
| 120 static GlobalPersistentSystemProfile* GetInstance(); | 132 static GlobalPersistentSystemProfile* GetInstance(); |
| 121 | 133 |
| 122 private: | 134 private: |
| 123 friend struct base::DefaultSingletonTraits<GlobalPersistentSystemProfile>; | 135 friend struct base::DefaultSingletonTraits<GlobalPersistentSystemProfile>; |
| 124 | 136 |
| 125 GlobalPersistentSystemProfile() {} | 137 GlobalPersistentSystemProfile() {} |
| 126 ~GlobalPersistentSystemProfile() {} | 138 ~GlobalPersistentSystemProfile() {} |
| 127 | 139 |
| 128 DISALLOW_COPY_AND_ASSIGN(GlobalPersistentSystemProfile); | 140 DISALLOW_COPY_AND_ASSIGN(GlobalPersistentSystemProfile); |
| 129 }; | 141 }; |
| 130 | 142 |
| 131 } // namespace metrics | 143 } // namespace metrics |
| 132 | 144 |
| 133 #endif // BASE_METRICS_PERSISTENT_SYSTEM_PROFILE_H_ | 145 #endif // BASE_METRICS_PERSISTENT_SYSTEM_PROFILE_H_ |
| OLD | NEW |