Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 15 | #include "pw_kvs/key_value_store.h" |
| 16 | |
| 17 | #include <cstring> |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 18 | #include <type_traits> |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 19 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 20 | #define PW_LOG_USE_ULTRA_SHORT_NAMES 1 |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 21 | #include "pw_kvs_private/format.h" |
| 22 | #include "pw_kvs_private/macros.h" |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 23 | #include "pw_log/log.h" |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 24 | |
Wyatt Hepler | 2ad6067 | 2020-01-21 08:00:16 -0800 | [diff] [blame] | 25 | namespace pw::kvs { |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 26 | |
Wyatt Hepler | acaacf9 | 2020-01-24 10:58:30 -0800 | [diff] [blame] | 27 | using std::byte; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 28 | using std::string_view; |
Wyatt Hepler | acaacf9 | 2020-01-24 10:58:30 -0800 | [diff] [blame] | 29 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | // constexpr uint32_t SixFiveFiveNineNine(std::string_view string) { |
| 33 | constexpr uint32_t HashKey(std::string_view string) { |
| 34 | uint32_t hash = 0; |
| 35 | uint32_t coefficient = 65599u; |
| 36 | |
| 37 | for (char ch : string) { |
| 38 | hash += coefficient * unsigned(ch); |
| 39 | coefficient *= 65599u; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 42 | return hash; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 45 | // TODO: Remove this when checksums in test are implemented. |
| 46 | const uint32_t kNoChecksum = 0x12345678; |
| 47 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 48 | constexpr size_t EntrySize(string_view key, span<const byte> value) { |
| 49 | return sizeof(EntryHeader) + key.size() + value.size(); |
| 50 | } |
| 51 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 52 | typedef FlashPartition::Address Address; |
| 53 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 54 | } // namespace |
| 55 | |
| 56 | Status KeyValueStore::Init() { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 57 | // Reset the number of occupied key descriptors; we will fill them later. |
| 58 | key_descriptor_list_size_ = 0; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 59 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 60 | const size_t sector_size_bytes = partition_.sector_size_bytes(); |
| 61 | const size_t sector_count = partition_.sector_count(); |
| 62 | |
| 63 | DBG("First pass: Read all entries from all sectors"); |
| 64 | for (size_t sector_id = 0; sector_id < sector_count; ++sector_id) { |
| 65 | // Track writable bytes in this sector. Updated after reading each entry. |
| 66 | sector_map_[sector_id].tail_free_bytes = sector_size_bytes; |
| 67 | |
| 68 | const Address sector_address = sector_id * sector_size_bytes; |
| 69 | Address entry_address = sector_address; |
| 70 | |
| 71 | for (int num_entries_in_sector = 0;; num_entries_in_sector++) { |
| 72 | DBG("Load entry: sector=%zu, entry#=%d, address=%zu", |
| 73 | sector_id, |
| 74 | num_entries_in_sector, |
| 75 | size_t(entry_address)); |
| 76 | |
| 77 | if (!AddressInSector(sector_map_[sector_id], entry_address)) { |
| 78 | DBG("Fell off end of sector; moving to the next sector"); |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | Address next_entry_address; |
| 83 | Status status = LoadEntry(entry_address, &next_entry_address); |
| 84 | if (status == Status::NOT_FOUND) { |
| 85 | DBG("Hit un-written data in sector; moving to the next sector"); |
| 86 | break; |
| 87 | } |
| 88 | if (status == Status::DATA_LOSS) { |
| 89 | // It's not clear KVS can make a unilateral decision about what to do |
| 90 | // in corruption cases. It's an application decision, for which we |
| 91 | // should offer some configurability. For now, entirely bail out of |
| 92 | // loading and give up. |
| 93 | // |
| 94 | // Later, scan for remaining valid keys; since it's entirely possible |
| 95 | // that there is a duplicate of the key elsewhere and everything is |
| 96 | // fine. Later, we can wipe and maybe recover the sector. |
| 97 | // |
| 98 | // TODO: Implement rest-of-sector scanning for valid entries. |
| 99 | return Status::DATA_LOSS; |
| 100 | } |
| 101 | TRY(status); |
| 102 | |
| 103 | // Entry loaded successfully; so get ready to load the next one. |
| 104 | entry_address = next_entry_address; |
| 105 | |
| 106 | // Update of the number of writable bytes in this sector. |
| 107 | sector_map_[sector_id].tail_free_bytes = |
| 108 | sector_size_bytes - (entry_address - sector_address); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | DBG("Second pass: Count valid bytes in each sector"); |
| 113 | // Initialize the sector sizes. |
| 114 | for (size_t sector_id = 0; sector_id < sector_count; ++sector_id) { |
| 115 | sector_map_[sector_id].valid_bytes = 0; |
| 116 | } |
| 117 | // For every valid key, increment the valid bytes for that sector. |
| 118 | for (size_t key_id = 0; key_id < key_descriptor_list_size_; ++key_id) { |
| 119 | uint32_t sector_id = |
| 120 | key_descriptor_list_[key_id].address / sector_size_bytes; |
| 121 | KeyDescriptor key_descriptor; |
| 122 | key_descriptor.address = key_descriptor_list_[key_id].address; |
| 123 | EntryHeader header; |
| 124 | TRY(ReadEntryHeader(key_descriptor, &header)); |
| 125 | sector_map_[sector_id].valid_bytes += header.entry_size(); |
| 126 | } |
| 127 | enabled_ = true; |
| 128 | return Status::OK; |
| 129 | } |
| 130 | |
| 131 | Status KeyValueStore::LoadEntry(Address entry_address, |
| 132 | Address* next_entry_address) { |
| 133 | const size_t alignment_bytes = partition_.alignment_bytes(); |
| 134 | |
| 135 | KeyDescriptor key_descriptor; |
| 136 | key_descriptor.address = entry_address; |
| 137 | |
| 138 | EntryHeader header; |
| 139 | TRY(ReadEntryHeader(key_descriptor, &header)); |
| 140 | // TODO: Should likely add a "LogHeader" method or similar. |
| 141 | DBG("Header: "); |
| 142 | DBG(" Address = 0x%zx", size_t(entry_address)); |
| 143 | DBG(" Magic = 0x%zx", size_t(header.magic())); |
| 144 | DBG(" Checksum = 0x%zx", size_t(header.checksum_as_uint32())); |
| 145 | DBG(" Key length = 0x%zx", size_t(header.key_length())); |
| 146 | DBG(" Value length = 0x%zx", size_t(header.value_length())); |
| 147 | DBG(" Entry size = 0x%zx", size_t(header.entry_size())); |
| 148 | DBG(" Padded size = 0x%zx", |
| 149 | size_t(AlignUp(header.entry_size(), alignment_bytes))); |
| 150 | |
| 151 | if (HeaderLooksLikeUnwrittenData(header)) { |
| 152 | return Status::NOT_FOUND; |
| 153 | } |
| 154 | key_descriptor.key_version = header.key_version(); |
| 155 | |
| 156 | // TODO: Handle multiple magics for formats that have changed. |
| 157 | if (header.magic() != entry_header_format_.magic) { |
| 158 | // TODO: It may be cleaner to have some logging helpers for these cases. |
| 159 | CRT("Found corrupt magic: %zx; expecting %zx; at address %zx", |
| 160 | size_t(header.magic()), |
| 161 | size_t(entry_header_format_.magic), |
| 162 | size_t(entry_address)); |
| 163 | return Status::DATA_LOSS; |
| 164 | } |
| 165 | |
| 166 | // Read the key from flash & validate the entry (which reads the value). |
| 167 | KeyBuffer key_buffer; |
| 168 | TRY(ReadEntryKey(key_descriptor, header.key_length(), key_buffer.data())); |
| 169 | TRY(ValidateEntryChecksum( |
| 170 | header, string_view(key_buffer.data()), key_descriptor)); |
| 171 | key_descriptor.key_hash = HashKey(string_view(key_buffer.data())); |
| 172 | |
| 173 | DBG("Key hash: %zx (%zu)", |
| 174 | size_t(key_descriptor.key_hash), |
| 175 | size_t(key_descriptor.key_hash)); |
| 176 | |
| 177 | TRY(AppendNewOrOverwriteStaleExistingDescriptor(key_descriptor)); |
| 178 | |
| 179 | // TODO: Extract this to something like "NextValidEntryAddress". |
| 180 | *next_entry_address = |
| 181 | AlignUp(key_descriptor.address + header.entry_size(), alignment_bytes); |
| 182 | |
| 183 | return Status::OK; |
| 184 | } |
| 185 | |
| 186 | // TODO: This method is the trigger of the O(valid_entries * all_entries) time |
| 187 | // complexity for reading. At some cost to memory, this could be optimized by |
| 188 | // using a hash table instead of scanning, but in practice this should be fine |
| 189 | // for a small number of keys |
| 190 | Status KeyValueStore::AppendNewOrOverwriteStaleExistingDescriptor( |
| 191 | const KeyDescriptor& key_descriptor) { |
| 192 | // With the new key descriptor, either add it to the descriptor table or |
| 193 | // overwrite an existing entry with an older version of the key. |
| 194 | KeyDescriptor* existing_descriptor = FindDescriptor(key_descriptor.key_hash); |
| 195 | if (existing_descriptor) { |
| 196 | if (existing_descriptor->key_version < key_descriptor.key_version) { |
| 197 | // Existing entry is old; replace the existing entry with the new one. |
| 198 | *existing_descriptor = key_descriptor; |
| 199 | } else { |
| 200 | // Otherwise, check for data integrity and leave the existing entry. |
| 201 | if (existing_descriptor->key_version == key_descriptor.key_version) { |
| 202 | ERR("Data loss: Duplicated old(=%zu) and new(=%zu) version", |
| 203 | size_t(existing_descriptor->key_version), |
| 204 | size_t(key_descriptor.key_version)); |
| 205 | return Status::DATA_LOSS; |
| 206 | } |
| 207 | DBG("Found stale entry when appending; ignoring"); |
| 208 | } |
| 209 | return Status::OK; |
| 210 | } |
| 211 | // Write new entry. |
| 212 | KeyDescriptor* newly_allocated_key_descriptor; |
| 213 | TRY(AppendEmptyDescriptor(&newly_allocated_key_descriptor)); |
| 214 | *newly_allocated_key_descriptor = key_descriptor; |
| 215 | return Status::OK; |
| 216 | } |
| 217 | |
| 218 | // TODO: Need a better name. |
| 219 | Status KeyValueStore::AppendEmptyDescriptor(KeyDescriptor** new_descriptor) { |
| 220 | if (KeyListFull()) { |
| 221 | // TODO: Is this the right return code? |
| 222 | return Status::RESOURCE_EXHAUSTED; |
| 223 | } |
| 224 | *new_descriptor = &key_descriptor_list_[key_descriptor_list_size_++]; |
| 225 | return Status::OK; |
| 226 | } |
| 227 | |
| 228 | // TODO: Finish. |
| 229 | bool KeyValueStore::HeaderLooksLikeUnwrittenData( |
| 230 | const EntryHeader& header) const { |
| 231 | // TODO: This is not correct; it should call through to flash memory. |
| 232 | return header.magic() == 0xffffffff; |
| 233 | } |
| 234 | |
| 235 | KeyValueStore::KeyDescriptor* KeyValueStore::FindDescriptor(uint32_t hash) { |
| 236 | for (size_t key_id = 0; key_id < key_descriptor_list_size_; key_id++) { |
| 237 | if (key_descriptor_list_[key_id].key_hash == hash) { |
| 238 | return &(key_descriptor_list_[key_id]); |
| 239 | } |
| 240 | } |
| 241 | return nullptr; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | StatusWithSize KeyValueStore::Get(string_view key, |
| 245 | span<byte> value_buffer) const { |
| 246 | TRY(InvalidOperation(key)); |
| 247 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 248 | const KeyDescriptor* key_descriptor; |
| 249 | TRY(FindKeyDescriptor(key, &key_descriptor)); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 250 | |
| 251 | EntryHeader header; |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 252 | TRY(ReadEntryHeader(*key_descriptor, &header)); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 253 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 254 | StatusWithSize result = ReadEntryValue(*key_descriptor, header, value_buffer); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 255 | if (result.ok() && options_.verify_on_read) { |
| 256 | return ValidateEntryChecksum(header, key, value_buffer); |
| 257 | } |
| 258 | return result; |
| 259 | } |
| 260 | |
| 261 | Status KeyValueStore::Put(string_view key, span<const byte> value) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 262 | DBG("Writing key/value; key length=%zu, value length=%zu", |
| 263 | key.size(), |
| 264 | value.size()); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 265 | TRY(InvalidOperation(key)); |
| 266 | |
| 267 | if (value.size() > (1 << 24)) { |
| 268 | // TODO: Reject sizes that are larger than the maximum? |
| 269 | } |
| 270 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 271 | KeyDescriptor* key_descriptor; |
| 272 | if (FindKeyDescriptor(key, &key_descriptor).ok()) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 273 | DBG("Writing over existing entry"); |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 274 | return WriteEntryForExistingKey(key_descriptor, key, value); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 275 | } |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 276 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 277 | DBG("Writing new entry"); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 278 | return WriteEntryForNewKey(key, value); |
| 279 | } |
| 280 | |
| 281 | Status KeyValueStore::Delete(string_view key) { |
| 282 | TRY(InvalidOperation(key)); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 283 | return Status::UNIMPLEMENTED; |
| 284 | } |
| 285 | |
| 286 | const KeyValueStore::Entry& KeyValueStore::Iterator::operator*() { |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 287 | const KeyDescriptor& key_descriptor = |
| 288 | entry_.kvs_.key_descriptor_list_[index_]; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 289 | |
| 290 | EntryHeader header; |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 291 | if (entry_.kvs_.ReadEntryHeader(key_descriptor, &header).ok()) { |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 292 | entry_.kvs_.ReadEntryKey( |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 293 | key_descriptor, header.key_length(), entry_.key_buffer_.data()); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | return entry_; |
| 297 | } |
| 298 | |
Wyatt Hepler | ed163b0 | 2020-02-03 17:49:32 -0800 | [diff] [blame^] | 299 | StatusWithSize KeyValueStore::ValueSize(std::string_view key) const { |
| 300 | TRY(InvalidOperation(key)); |
| 301 | |
| 302 | const KeyDescriptor* key_descriptor; |
| 303 | TRY(FindKeyDescriptor(key, &key_descriptor)); |
| 304 | |
| 305 | EntryHeader header; |
| 306 | TRY(ReadEntryHeader(*key_descriptor, &header)); |
| 307 | |
| 308 | return StatusWithSize(header.value_length()); |
| 309 | } |
| 310 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 311 | Status KeyValueStore::ValidateEntryChecksum(const EntryHeader& header, |
| 312 | string_view key, |
| 313 | const KeyDescriptor& entry) const { |
| 314 | // TODO: Remove this block once the checksums are implemented in test. |
| 315 | if (entry_header_format_.checksum == nullptr) { |
| 316 | const auto header_checksum = header.checksum(); |
| 317 | const auto fixed_checksum = as_bytes(span(&kNoChecksum, 1)); |
| 318 | if (std::memcmp(header_checksum.data(), |
| 319 | fixed_checksum.data(), |
| 320 | header_checksum.size()) == 0) { |
| 321 | return Status::OK; |
| 322 | } |
| 323 | return Status::DATA_LOSS; |
| 324 | |
| 325 | // TODO: It's unclear why the below version doesn't work. |
| 326 | // return header.checksum() == as_bytes(span(&kNoChecksum, 1)); |
| 327 | } |
| 328 | (void)(header); |
| 329 | (void)(key); |
| 330 | (void)(entry); |
| 331 | // TODO: Do incremental checksum verification by reading the value from |
| 332 | // flash a few blocks at a time. |
| 333 | return Status::OK; |
| 334 | } |
| 335 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 336 | Status KeyValueStore::InvalidOperation(string_view key) const { |
Wyatt Hepler | acaacf9 | 2020-01-24 10:58:30 -0800 | [diff] [blame] | 337 | if (InvalidKey(key)) { |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 338 | return Status::INVALID_ARGUMENT; |
| 339 | } |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 340 | if (!enabled_) { |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 341 | return Status::FAILED_PRECONDITION; |
| 342 | } |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 343 | return Status::OK; |
| 344 | } |
| 345 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 346 | Status KeyValueStore::FindKeyDescriptor(string_view key, |
| 347 | const KeyDescriptor** result) const { |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 348 | char key_buffer[kMaxKeyLength]; |
| 349 | const uint32_t hash = HashKey(key); |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 350 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 351 | for (auto& descriptor : key_descriptors()) { |
| 352 | if (descriptor.key_hash == hash) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 353 | DBG("Found match! For hash: %zx", size_t(hash)); |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 354 | TRY(ReadEntryKey(descriptor, key.size(), key_buffer)); |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 355 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 356 | if (key == string_view(key_buffer, key.size())) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 357 | DBG("Keys matched too"); |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 358 | *result = &descriptor; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 359 | return Status::OK; |
| 360 | } |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 361 | } |
| 362 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 363 | return Status::NOT_FOUND; |
| 364 | } |
| 365 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 366 | Status KeyValueStore::ReadEntryHeader(const KeyDescriptor& descriptor, |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 367 | EntryHeader* header) const { |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 368 | return partition_.Read(descriptor.address, sizeof(*header), header).status(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 369 | } |
| 370 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 371 | Status KeyValueStore::ReadEntryKey(const KeyDescriptor& descriptor, |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 372 | size_t key_length, |
| 373 | char* key) const { |
| 374 | // TODO: This check probably shouldn't be here; this is like |
| 375 | // checking that the Cortex M's RAM isn't corrupt. This should be |
| 376 | // done at boot time. |
| 377 | // ^^ This argument sometimes comes from EntryHeader::key_value_len, |
| 378 | // which is read directly from flash. If it's corrupted, we shouldn't try |
| 379 | // to read a bunch of extra data. |
| 380 | if (key_length == 0u || key_length > kMaxKeyLength) { |
| 381 | return Status::DATA_LOSS; |
| 382 | } |
| 383 | // The key is immediately after the entry header. |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 384 | return partition_ |
| 385 | .Read(descriptor.address + sizeof(EntryHeader), key_length, key) |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 386 | .status(); |
| 387 | } |
| 388 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 389 | StatusWithSize KeyValueStore::ReadEntryValue( |
| 390 | const KeyDescriptor& key_descriptor, |
| 391 | const EntryHeader& header, |
| 392 | span<byte> value) const { |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 393 | const size_t read_size = std::min(header.value_length(), value.size()); |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 394 | StatusWithSize result = partition_.Read( |
| 395 | key_descriptor.address + sizeof(header) + header.key_length(), |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 396 | value.subspan(0, read_size)); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 397 | TRY(result); |
| 398 | if (read_size != header.value_length()) { |
| 399 | return StatusWithSize(Status::RESOURCE_EXHAUSTED, read_size); |
| 400 | } |
| 401 | return StatusWithSize(read_size); |
| 402 | } |
| 403 | |
| 404 | Status KeyValueStore::ValidateEntryChecksum(const EntryHeader& header, |
| 405 | string_view key, |
| 406 | span<const byte> value) const { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 407 | // TODO: Re-enable this when we have checksum implementations. |
| 408 | return Status::OK; |
| 409 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 410 | CalculateEntryChecksum(header, key, value); |
| 411 | return entry_header_format_.checksum->Verify(header.checksum()); |
| 412 | } |
| 413 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 414 | Status KeyValueStore::WriteEntryForExistingKey(KeyDescriptor* key_descriptor, |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 415 | string_view key, |
| 416 | span<const byte> value) { |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 417 | SectorDescriptor* sector; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 418 | TRY(FindOrRecoverSectorWithSpace(§or, EntrySize(key, value))); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 419 | DBG("Writing existing entry; found sector: %d", |
| 420 | static_cast<int>(sector - sector_map_.data())); |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 421 | return AppendEntry(sector, key_descriptor, key, value); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | Status KeyValueStore::WriteEntryForNewKey(string_view key, |
| 425 | span<const byte> value) { |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 426 | if (KeyListFull()) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 427 | WRN("KVS full: trying to store a new entry, but can't. Have %zu entries", |
| 428 | key_descriptor_list_size_); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 429 | return Status::RESOURCE_EXHAUSTED; |
| 430 | } |
| 431 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 432 | // Modify the key descriptor at the end of the array, without bumping the map |
| 433 | // size so the key descriptor is prepared and written without committing |
| 434 | // first. |
| 435 | KeyDescriptor& key_descriptor = |
| 436 | key_descriptor_list_[key_descriptor_list_size_]; |
| 437 | key_descriptor.key_hash = HashKey(key); |
| 438 | key_descriptor.key_version = 0; // will be incremented by AppendEntry() |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 439 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 440 | SectorDescriptor* sector; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 441 | TRY(FindOrRecoverSectorWithSpace(§or, EntrySize(key, value))); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 442 | DBG("Writing new entry; found sector: %d", |
| 443 | static_cast<int>(sector - sector_map_.data())); |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 444 | TRY(AppendEntry(sector, &key_descriptor, key, value)); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 445 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 446 | // Only increment bump our size when we are certain the write succeeded. |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 447 | key_descriptor_list_size_ += 1; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 448 | return Status::OK; |
| 449 | } |
| 450 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 451 | Status KeyValueStore::RelocateEntry(KeyDescriptor& key_descriptor) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 452 | // TODO: Implement this function! |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 453 | (void)key_descriptor; |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 454 | return Status::OK; |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 455 | return Status::UNIMPLEMENTED; |
| 456 | } |
| 457 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 458 | // Find either an existing sector with enough space, or an empty sector. |
| 459 | // Maintains the invariant that there is always at least 1 empty sector. |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 460 | KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorWithSpace( |
| 461 | size_t size) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 462 | const size_t sector_count = partition_.sector_count(); |
| 463 | |
| 464 | // TODO: Ignore last written sector for now and scan from the beginning. |
| 465 | last_written_sector_ = sector_count - 1; |
| 466 | |
| 467 | size_t start = (last_written_sector_ + 1) % sector_count; |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 468 | SectorDescriptor* first_empty_sector = nullptr; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 469 | bool at_least_two_empty_sectors = false; |
| 470 | |
| 471 | for (size_t i = start; i != last_written_sector_; |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 472 | i = (i + 1) % sector_count) { |
| 473 | DBG("Examining sector %zu", i); |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 474 | SectorDescriptor& sector = sector_map_[i]; |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 475 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 476 | if (!SectorEmpty(sector) && sector.HasSpace(size)) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 477 | DBG("Partially occupied sector with enough space; done!"); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 478 | return §or; |
| 479 | } |
| 480 | |
| 481 | if (SectorEmpty(sector)) { |
| 482 | if (first_empty_sector == nullptr) { |
| 483 | first_empty_sector = §or; |
| 484 | } else { |
| 485 | at_least_two_empty_sectors = true; |
Wyatt Hepler | 2ad6067 | 2020-01-21 08:00:16 -0800 | [diff] [blame] | 486 | } |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 487 | } |
| 488 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 489 | |
| 490 | if (at_least_two_empty_sectors) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 491 | DBG("Found a empty sector and a spare one; returning the first found (%d)", |
| 492 | static_cast<int>(first_empty_sector - sector_map_.data())); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 493 | return first_empty_sector; |
| 494 | } |
| 495 | return nullptr; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 496 | } |
| 497 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 498 | Status KeyValueStore::FindOrRecoverSectorWithSpace(SectorDescriptor** sector, |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 499 | size_t size) { |
| 500 | *sector = FindSectorWithSpace(size); |
| 501 | if (*sector != nullptr) { |
| 502 | return Status::OK; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 503 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 504 | if (options_.partial_gc_on_write) { |
| 505 | return GarbageCollectOneSector(sector); |
| 506 | } |
| 507 | return Status::RESOURCE_EXHAUSTED; |
| 508 | } |
| 509 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 510 | KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorToGarbageCollect() { |
| 511 | SectorDescriptor* sector_candidate = nullptr; |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 512 | size_t candidate_bytes = 0; |
| 513 | |
| 514 | // Step 1: Try to find a sectors with stale keys and no valid keys (no |
| 515 | // relocation needed). If any such sectors are found, use the sector with the |
| 516 | // most reclaimable bytes. |
| 517 | for (auto& sector : sector_map_) { |
| 518 | if ((sector.valid_bytes == 0) && |
| 519 | (RecoverableBytes(sector) > candidate_bytes)) { |
| 520 | sector_candidate = §or; |
| 521 | candidate_bytes = RecoverableBytes(sector); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // Step 2: If step 1 yields no sectors, just find the sector with the most |
| 526 | // reclaimable bytes. |
| 527 | if (sector_candidate == nullptr) { |
| 528 | for (auto& sector : sector_map_) { |
| 529 | if (RecoverableBytes(sector) > candidate_bytes) { |
| 530 | sector_candidate = §or; |
| 531 | candidate_bytes = RecoverableBytes(sector); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | return sector_candidate; |
| 537 | } |
| 538 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 539 | Status KeyValueStore::GarbageCollectOneSector(SectorDescriptor** sector) { |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 540 | // Step 1: Find the sector to garbage collect |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 541 | SectorDescriptor* sector_to_gc = FindSectorToGarbageCollect(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 542 | |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 543 | if (sector_to_gc == nullptr) { |
| 544 | return Status::RESOURCE_EXHAUSTED; |
| 545 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 546 | |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 547 | // Step 2: Move any valid entries in the GC sector to other sectors |
| 548 | if (sector_to_gc->valid_bytes != 0) { |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 549 | for (auto& descriptor : key_descriptors()) { |
| 550 | if (AddressInSector(*sector_to_gc, descriptor.address)) { |
| 551 | TRY(RelocateEntry(descriptor)); |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 552 | } |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 555 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 556 | if (sector_to_gc->valid_bytes != 0) { |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 557 | return Status::INTERNAL; |
| 558 | } |
| 559 | |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 560 | // Step 3: Reinitialize the sector |
| 561 | sector_to_gc->tail_free_bytes = 0; |
| 562 | TRY(partition_.Erase(SectorBaseAddress(sector_to_gc), 1)); |
| 563 | sector_to_gc->tail_free_bytes = partition_.sector_size_bytes(); |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 564 | |
David Rogers | a12786b | 2020-01-31 16:02:33 -0800 | [diff] [blame] | 565 | *sector = sector_to_gc; |
| 566 | return Status::OK; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 567 | } |
| 568 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 569 | Status KeyValueStore::AppendEntry(SectorDescriptor* sector, |
| 570 | KeyDescriptor* key_descriptor, |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 571 | const string_view key, |
| 572 | span<const byte> value) { |
| 573 | // write header, key, and value |
| 574 | const EntryHeader header(entry_header_format_.magic, |
| 575 | CalculateEntryChecksum(header, key, value), |
| 576 | key.size(), |
| 577 | value.size(), |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 578 | key_descriptor->key_version + 1); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 579 | DBG("Appending entry with key version: %zx", size_t(header.key_version())); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 580 | |
| 581 | // Handles writing multiple concatenated buffers, while breaking up the writes |
| 582 | // into alignment-sized blocks. |
| 583 | Address address = NextWritableAddress(sector); |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 584 | DBG("Appending to address: %zx", size_t(address)); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 585 | TRY_ASSIGN( |
| 586 | size_t written, |
| 587 | partition_.Write( |
| 588 | address, {as_bytes(span(&header, 1)), as_bytes(span(key)), value})); |
| 589 | |
| 590 | if (options_.verify_on_write) { |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 591 | TRY(VerifyEntry(sector, key_descriptor)); |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 592 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 593 | |
| 594 | // TODO: UPDATE last_written_sector_ appropriately |
| 595 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 596 | key_descriptor->address = address; |
| 597 | key_descriptor->key_version = header.key_version(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 598 | sector->valid_bytes += written; |
| 599 | sector->tail_free_bytes -= written; |
| 600 | return Status::OK; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 601 | } |
| 602 | |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 603 | Status KeyValueStore::VerifyEntry(SectorDescriptor* sector, |
| 604 | KeyDescriptor* key_descriptor) { |
Wyatt Hepler | ed163b0 | 2020-02-03 17:49:32 -0800 | [diff] [blame^] | 605 | // TODO: Remove this once checksums are fully implemented. |
| 606 | return Status::OK; |
| 607 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 608 | if (entry_header_format_.checksum == nullptr) { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 609 | return Status::OK; |
| 610 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 611 | // TODO: Implement me! |
| 612 | (void)sector; |
David Rogers | 2761aeb | 2020-01-31 17:09:00 -0800 | [diff] [blame] | 613 | (void)key_descriptor; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 614 | return Status::UNIMPLEMENTED; |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 617 | span<const byte> KeyValueStore::CalculateEntryChecksum( |
| 618 | const EntryHeader& header, |
| 619 | const string_view key, |
| 620 | span<const byte> value) const { |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 621 | if (entry_header_format_.checksum == nullptr) { |
| 622 | return as_bytes(span(&kNoChecksum, 1)); |
| 623 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 624 | auto& checksum = *entry_header_format_.checksum; |
| 625 | |
| 626 | checksum.Reset(); |
| 627 | checksum.Update(header.DataForChecksum()); |
| 628 | checksum.Update(as_bytes(span(key))); |
| 629 | checksum.Update(value.data(), value.size_bytes()); |
| 630 | return checksum.state(); |
Wyatt Hepler | b760954 | 2020-01-24 10:29:54 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Keir Mierle | 8c352dc | 2020-02-02 13:58:19 -0800 | [diff] [blame] | 633 | void KeyValueStore::LogDebugInfo() { |
| 634 | const size_t sector_count = partition_.sector_count(); |
| 635 | const size_t sector_size_bytes = partition_.sector_size_bytes(); |
| 636 | DBG("====================== KEY VALUE STORE DUMP ========================="); |
| 637 | DBG(" "); |
| 638 | DBG("Flash partition:"); |
| 639 | DBG(" Sector count = %zu", sector_count); |
| 640 | DBG(" Sector max count = %zu", kUsableSectors); |
| 641 | DBG(" Sector size = %zu", sector_size_bytes); |
| 642 | DBG(" Total size = %zu", partition_.size_bytes()); |
| 643 | DBG(" Alignment = %zu", partition_.alignment_bytes()); |
| 644 | DBG(" "); |
| 645 | DBG("Key descriptors:"); |
| 646 | DBG(" Entry count = %zu", key_descriptor_list_size_); |
| 647 | DBG(" Max entry count = %zu", kMaxEntries); |
| 648 | DBG(" "); |
| 649 | DBG(" # hash version address address (hex)"); |
| 650 | for (size_t i = 0; i < key_descriptor_list_size_; ++i) { |
| 651 | const KeyDescriptor& kd = key_descriptor_list_[i]; |
| 652 | DBG(" |%3zu: | %8zx |%8zu | %8zu | %8zx", |
| 653 | i, |
| 654 | size_t(kd.key_hash), |
| 655 | size_t(kd.key_version), |
| 656 | size_t(kd.address), |
| 657 | size_t(kd.address)); |
| 658 | } |
| 659 | DBG(" "); |
| 660 | |
| 661 | DBG("Sector descriptors:"); |
| 662 | DBG(" # tail free valid has_space"); |
| 663 | for (size_t sector_id = 0; sector_id < sector_count; ++sector_id) { |
| 664 | const SectorDescriptor& sd = sector_map_[sector_id]; |
| 665 | DBG(" |%3zu: | %8zu |%8zu | %s", |
| 666 | sector_id, |
| 667 | size_t(sd.tail_free_bytes), |
| 668 | size_t(sd.valid_bytes), |
| 669 | sd.tail_free_bytes ? "YES" : ""); |
| 670 | } |
| 671 | DBG(" "); |
| 672 | |
| 673 | // TODO: This should stop logging after some threshold. |
| 674 | // size_t dumped_bytes = 0; |
| 675 | DBG("Sector raw data:"); |
| 676 | for (size_t sector_id = 0; sector_id < sector_count; ++sector_id) { |
| 677 | // Read sector data. Yes, this will blow the stack on embedded. |
| 678 | std::array<byte, 500> raw_sector_data; // TODO |
| 679 | StatusWithSize sws = |
| 680 | partition_.Read(sector_id * sector_size_bytes, raw_sector_data); |
| 681 | DBG("Read: %zu bytes", sws.size()); |
| 682 | |
| 683 | DBG(" base addr offs 0 1 2 3 4 5 6 7"); |
| 684 | for (size_t i = 0; i < sector_size_bytes; i += 8) { |
| 685 | DBG(" %3zu %8zx %5zu | %02x %02x %02x %02x %02x %02x %02x %02x", |
| 686 | sector_id, |
| 687 | (sector_id * sector_size_bytes) + i, |
| 688 | i, |
| 689 | static_cast<unsigned int>(raw_sector_data[i + 0]), |
| 690 | static_cast<unsigned int>(raw_sector_data[i + 1]), |
| 691 | static_cast<unsigned int>(raw_sector_data[i + 2]), |
| 692 | static_cast<unsigned int>(raw_sector_data[i + 3]), |
| 693 | static_cast<unsigned int>(raw_sector_data[i + 4]), |
| 694 | static_cast<unsigned int>(raw_sector_data[i + 5]), |
| 695 | static_cast<unsigned int>(raw_sector_data[i + 6]), |
| 696 | static_cast<unsigned int>(raw_sector_data[i + 7])); |
| 697 | |
| 698 | // TODO: Fix exit condition. |
| 699 | if (i > 128) { |
| 700 | break; |
| 701 | } |
| 702 | } |
| 703 | DBG(" "); |
| 704 | } |
| 705 | |
| 706 | DBG("////////////////////// KEY VALUE STORE DUMP END /////////////////////"); |
| 707 | } |
| 708 | |
Wyatt Hepler | 2ad6067 | 2020-01-21 08:00:16 -0800 | [diff] [blame] | 709 | } // namespace pw::kvs |