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