OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_METRICS_PERSISTENT_SYSTEM_PROFILE_H_ |
| 6 #define BASE_METRICS_PERSISTENT_SYSTEM_PROFILE_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/threading/thread_checker.h" |
| 11 #include "components/metrics/proto/system_profile.pb.h" |
| 12 |
| 13 namespace base { |
| 14 template <typename T> |
| 15 struct DefaultSingletonTraits; |
| 16 class PersistentMemoryAllocator; |
| 17 } // namespace base |
| 18 |
| 19 namespace metrics { |
| 20 |
| 21 // Manages a copy of the system profile inside persistent memory segments. |
| 22 class PersistentSystemProfile { |
| 23 public: |
| 24 PersistentSystemProfile(); |
| 25 ~PersistentSystemProfile(); |
| 26 |
| 27 // This object can store records in multiple memory allocators. |
| 28 void RegisterPersistentAllocator( |
| 29 base::PersistentMemoryAllocator* memory_allocator); |
| 30 void DeregisterPersistentAllocator( |
| 31 base::PersistentMemoryAllocator* memory_allocator); |
| 32 |
| 33 // Stores a complete system profile. |
| 34 void SetSystemProfile(const std::string& serialized_profile); |
| 35 |
| 36 // Retrieves the system profile from a persistent memory allocator. Returns |
| 37 // true if a profile was successfully retrieved. |
| 38 static bool GetSystemProfile( |
| 39 const base::PersistentMemoryAllocator& memory_allocator, |
| 40 SystemProfileProto* system_profile); |
| 41 |
| 42 private: |
| 43 friend class PersistentSystemProfileTest; |
| 44 |
| 45 // Defines record types that can be stored inside our local Allocators. |
| 46 enum RecordType : uint8_t { |
| 47 kUnusedSpace = 0, // The default value for empty memory. |
| 48 kSystemProfileProto, |
| 49 }; |
| 50 |
| 51 // A class for managing record allocations inside a persistent memory segment. |
| 52 class RecordAllocator { |
| 53 public: |
| 54 // Construct an allocator for writing. |
| 55 RecordAllocator(base::PersistentMemoryAllocator* memory_allocator, |
| 56 size_t min_size); |
| 57 |
| 58 // Construct an allocator for reading. |
| 59 RecordAllocator(const base::PersistentMemoryAllocator* memory_allocator); |
| 60 |
| 61 // These methods manage writing records to the allocator. Do not mix these |
| 62 // with "read" calls; it's one or the other. |
| 63 void Reset(); |
| 64 bool Write(RecordType type, const std::string& record); |
| 65 |
| 66 // Read a record from the allocator. Do not mix this with "write" calls; |
| 67 // it's one or the other. |
| 68 bool Read(RecordType* type, std::string* record) const; |
| 69 |
| 70 base::PersistentMemoryAllocator* allocator() { return allocator_; } |
| 71 |
| 72 private: |
| 73 // Advance to the next record segment in the memory allocator. |
| 74 bool NextSegment() const; |
| 75 |
| 76 // Advance to the next record segment, creating a new one if necessary with |
| 77 // sufficent |min_size| space. |
| 78 bool AddSegment(size_t min_size); |
| 79 |
| 80 // Writes data to the current position, updating the passed values past |
| 81 // the amount written. Returns false in case of an error. |
| 82 bool WriteData(RecordType type, const char** data, size_t* remaining_size); |
| 83 |
| 84 // Reads data from the current position, updating the passed string |
| 85 // in-place. |type| must be initialized to kUnusedSpace and |record| must |
| 86 // be an empty string before the first call but unchanged thereafter. |
| 87 // Returns true when record is complete. |
| 88 bool ReadData(RecordType* type, std::string* record) const; |
| 89 |
| 90 // This never changes but can't be "const" because vector calls operator=(). |
| 91 base::PersistentMemoryAllocator* allocator_; // Storage location. |
| 92 |
| 93 // These change even though the underlying data may be "const". |
| 94 mutable uint32_t alloc_reference_; // Last storage block. |
| 95 mutable size_t alloc_size_; // Size of the block. |
| 96 mutable size_t end_offset_; // End of data in block. |
| 97 |
| 98 // Copy and assign are allowed for easy use with STL containers. |
| 99 }; |
| 100 |
| 101 // The list of registered persistent allocators, described by RecordAllocator |
| 102 // instances. |
| 103 std::vector<RecordAllocator> allocators_; |
| 104 |
| 105 THREAD_CHECKER(thread_checker_); |
| 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(PersistentSystemProfile); |
| 108 }; |
| 109 |
| 110 // A singleton instance of the above. |
| 111 class GlobalPersistentSystemProfile : public PersistentSystemProfile { |
| 112 public: |
| 113 static GlobalPersistentSystemProfile* GetInstance(); |
| 114 |
| 115 private: |
| 116 friend struct base::DefaultSingletonTraits<GlobalPersistentSystemProfile>; |
| 117 |
| 118 GlobalPersistentSystemProfile() {} |
| 119 ~GlobalPersistentSystemProfile() {} |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(GlobalPersistentSystemProfile); |
| 122 }; |
| 123 |
| 124 } // namespace metrics |
| 125 |
| 126 #endif // BASE_METRICS_PERSISTENT_SYSTEM_PROFILE_H_ |
OLD | NEW |