| 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 #include "components/metrics/persistent_system_profile.h" | 5 #include "components/metrics/persistent_system_profile.h" |
| 6 | 6 |
| 7 #include "base/atomicops.h" | 7 #include "base/atomicops.h" |
| 8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
| 9 #include "base/metrics/persistent_memory_allocator.h" | 9 #include "base/metrics/persistent_memory_allocator.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 } | 94 } |
| 95 // Write out as much of the data as possible. |data| and |remaining_size| | 95 // Write out as much of the data as possible. |data| and |remaining_size| |
| 96 // are updated in place. | 96 // are updated in place. |
| 97 if (!WriteData(type, &data, &remaining_size)) | 97 if (!WriteData(type, &data, &remaining_size)) |
| 98 return false; | 98 return false; |
| 99 } while (remaining_size > 0); | 99 } while (remaining_size > 0); |
| 100 | 100 |
| 101 return true; | 101 return true; |
| 102 } | 102 } |
| 103 | 103 |
| 104 bool PersistentSystemProfile::RecordAllocator::HasMoreData() const { |
| 105 if (alloc_reference_ == 0 && !NextSegment()) |
| 106 return false; |
| 107 |
| 108 char* block = |
| 109 allocator_->GetAsArray<char>(alloc_reference_, kTypeIdSystemProfile, |
| 110 base::PersistentMemoryAllocator::kSizeAny); |
| 111 if (!block) |
| 112 return false; |
| 113 |
| 114 RecordHeader header; |
| 115 header.as_atomic = base::subtle::Acquire_Load( |
| 116 reinterpret_cast<base::subtle::Atomic32*>(block + end_offset_)); |
| 117 return header.as_parts.type != kUnusedSpace; |
| 118 } |
| 119 |
| 104 bool PersistentSystemProfile::RecordAllocator::Read(RecordType* type, | 120 bool PersistentSystemProfile::RecordAllocator::Read(RecordType* type, |
| 105 std::string* record) const { | 121 std::string* record) const { |
| 106 *type = kUnusedSpace; | 122 *type = kUnusedSpace; |
| 107 record->clear(); | 123 record->clear(); |
| 108 | 124 |
| 109 // Access data and read records until everything has been loaded. | 125 // Access data and read records until everything has been loaded. |
| 110 while (true) { | 126 while (true) { |
| 111 if (end_offset_ == alloc_size_) { | 127 if (end_offset_ == alloc_size_) { |
| 112 if (!NextSegment()) | 128 if (!NextSegment()) |
| 113 return false; | 129 return false; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 return; | 285 return; |
| 270 | 286 |
| 271 for (auto& allocator : allocators_) { | 287 for (auto& allocator : allocators_) { |
| 272 // A full system profile always starts fresh. | 288 // A full system profile always starts fresh. |
| 273 allocator.Reset(); | 289 allocator.Reset(); |
| 274 // Write out the serialized profile. | 290 // Write out the serialized profile. |
| 275 allocator.Write(kSystemProfileProto, serialized_profile); | 291 allocator.Write(kSystemProfileProto, serialized_profile); |
| 276 } | 292 } |
| 277 } | 293 } |
| 278 | 294 |
| 295 void PersistentSystemProfile::SetSystemProfile( |
| 296 const SystemProfileProto& profile) { |
| 297 std::string serialized_profile; |
| 298 if (!profile.SerializeToString(&serialized_profile)) |
| 299 return; |
| 300 SetSystemProfile(serialized_profile); |
| 301 } |
| 302 |
| 303 // static |
| 304 bool PersistentSystemProfile::HasSystemProfile( |
| 305 const base::PersistentMemoryAllocator& memory_allocator) { |
| 306 const RecordAllocator records(&memory_allocator); |
| 307 return records.HasMoreData(); |
| 308 } |
| 309 |
| 279 // static | 310 // static |
| 280 bool PersistentSystemProfile::GetSystemProfile( | 311 bool PersistentSystemProfile::GetSystemProfile( |
| 281 const base::PersistentMemoryAllocator& memory_allocator, | 312 const base::PersistentMemoryAllocator& memory_allocator, |
| 282 SystemProfileProto* system_profile) { | 313 SystemProfileProto* system_profile) { |
| 283 const RecordAllocator records(&memory_allocator); | 314 const RecordAllocator records(&memory_allocator); |
| 284 | 315 |
| 285 RecordType type; | 316 RecordType type; |
| 286 std::string record; | 317 std::string record; |
| 287 if (!records.Read(&type, &record)) | 318 if (!records.Read(&type, &record)) |
| 288 return false; | 319 return false; |
| 289 if (type != kSystemProfileProto) | 320 if (type != kSystemProfileProto) |
| 290 return false; | 321 return false; |
| 291 | 322 |
| 292 return system_profile->ParseFromString(record); | 323 return system_profile->ParseFromString(record); |
| 293 } | 324 } |
| 294 | 325 |
| 295 GlobalPersistentSystemProfile* GlobalPersistentSystemProfile::GetInstance() { | 326 GlobalPersistentSystemProfile* GlobalPersistentSystemProfile::GetInstance() { |
| 296 return base::Singleton< | 327 return base::Singleton< |
| 297 GlobalPersistentSystemProfile, | 328 GlobalPersistentSystemProfile, |
| 298 base::LeakySingletonTraits<GlobalPersistentSystemProfile>>::get(); | 329 base::LeakySingletonTraits<GlobalPersistentSystemProfile>>::get(); |
| 299 } | 330 } |
| 300 | 331 |
| 301 } // namespace metrics | 332 } // namespace metrics |
| OLD | NEW |