Armando Montanez | f9e93e1 | 2020-04-22 07:44:14 -0700 | [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 | |
David Rogers | d5138f3 | 2020-04-28 15:18:56 -0700 | [diff] [blame] | 15 | // Always use stats, these tests depend on it. |
| 16 | #define PW_KVS_RECORD_PARTITION_STATS 1 |
| 17 | |
Armando Montanez | f9e93e1 | 2020-04-22 07:44:14 -0700 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
David Rogers | d64cc01 | 2020-05-26 12:37:37 -0700 | [diff] [blame] | 19 | #include "pw_kvs/fake_flash_memory.h" |
Armando Montanez | f9e93e1 | 2020-04-22 07:44:14 -0700 | [diff] [blame] | 20 | #include "pw_kvs/flash_memory.h" |
David Rogers | d5138f3 | 2020-04-28 15:18:56 -0700 | [diff] [blame] | 21 | #include "pw_kvs/flash_partition_with_stats.h" |
Armando Montanez | f9e93e1 | 2020-04-22 07:44:14 -0700 | [diff] [blame] | 22 | #include "pw_kvs/key_value_store.h" |
| 23 | #include "pw_log/log.h" |
| 24 | |
| 25 | namespace pw::kvs { |
| 26 | namespace { |
| 27 | |
Armando Montanez | f9e93e1 | 2020-04-22 07:44:14 -0700 | [diff] [blame] | 28 | constexpr EntryFormat format{.magic = 0xBAD'C0D3, .checksum = nullptr}; |
| 29 | |
David Rogers | d5138f3 | 2020-04-28 15:18:56 -0700 | [diff] [blame] | 30 | class WearTest : public ::testing::Test { |
| 31 | protected: |
| 32 | WearTest() |
| 33 | : flash_(internal::Entry::kMinAlignmentBytes), |
| 34 | partition_(&flash_, 0, flash_.sector_count()), |
| 35 | kvs_(&partition_, format) { |
| 36 | EXPECT_EQ(Status::OK, kvs_.Init()); |
| 37 | } |
| 38 | |
| 39 | static constexpr size_t kSectors = 16; |
| 40 | static constexpr size_t kMaxEntries = 256; |
| 41 | static constexpr size_t kTestPartitionSectorSize = 512; |
| 42 | |
David Rogers | d64cc01 | 2020-05-26 12:37:37 -0700 | [diff] [blame] | 43 | FakeFlashMemoryBuffer<kTestPartitionSectorSize, kSectors> flash_; |
David Rogers | d5138f3 | 2020-04-28 15:18:56 -0700 | [diff] [blame] | 44 | FlashPartitionWithStatsBuffer<kSectors> partition_; |
| 45 | |
| 46 | KeyValueStoreBuffer<kMaxEntries, kSectors> kvs_; |
| 47 | }; |
| 48 | |
| 49 | // Block of data to use for entry value. Sized to 470 so the total entry results |
| 50 | // in using most of the 512 byte sector. |
| 51 | uint8_t test_data[470] = {1, 2, 3, 4, 5, 6}; |
Armando Montanez | f9e93e1 | 2020-04-22 07:44:14 -0700 | [diff] [blame] | 52 | |
| 53 | // Write a large key (i.e. only one entry fits in each sector) enough times to |
| 54 | // fill up the KVS multiple times, and ensure every sector was garbage collected |
| 55 | // multiple additional times. |
David Rogers | d5138f3 | 2020-04-28 15:18:56 -0700 | [diff] [blame] | 56 | TEST_F(WearTest, RepeatedLargeEntry) { |
Armando Montanez | f9e93e1 | 2020-04-22 07:44:14 -0700 | [diff] [blame] | 57 | // Initialize an empty KVS, erasing flash and all tracked sector erase counts. |
David Rogers | d5138f3 | 2020-04-28 15:18:56 -0700 | [diff] [blame] | 58 | partition_.ResetCounters(); |
Armando Montanez | f9e93e1 | 2020-04-22 07:44:14 -0700 | [diff] [blame] | 59 | |
| 60 | // Add enough large entries to fill the entire KVS several times. |
David Rogers | d5138f3 | 2020-04-28 15:18:56 -0700 | [diff] [blame] | 61 | for (size_t i = 0; i < kSectors * 10; ++i) { |
| 62 | // modify the value to ensure a key-value different than was previously |
| 63 | // written. |
| 64 | test_data[0]++; |
| 65 | |
| 66 | EXPECT_TRUE(kvs_.Put("large_entry", span(test_data)).ok()); |
Armando Montanez | f9e93e1 | 2020-04-22 07:44:14 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Ensure every sector has been erased at several times due to garbage |
| 70 | // collection. |
David Rogers | d5138f3 | 2020-04-28 15:18:56 -0700 | [diff] [blame] | 71 | EXPECT_GE(partition_.min_erase_count(), 7u); |
| 72 | EXPECT_LE(partition_.max_erase_count(), partition_.min_erase_count() + 1u); |
| 73 | |
| 74 | partition_.SaveStorageStats(kvs_, "WearTest RepeatedLargeEntry"); |
Armando Montanez | f9e93e1 | 2020-04-22 07:44:14 -0700 | [diff] [blame] | 75 | } |
| 76 | |
David Rogers | d5138f3 | 2020-04-28 15:18:56 -0700 | [diff] [blame] | 77 | // Test a KVS with a number of entries, several sectors that are nearly full |
| 78 | // of stale (reclaimable) space, and not enough writable (free) space to add a |
| 79 | // redundant copy for any of the entries. Tests that the add redundancy step of |
| 80 | // repair is able to use garbage collection to free up space needed for the new |
| 81 | // copies. |
| 82 | TEST_F(WearTest, TwoPassFillWithLargeAndLarger) { |
| 83 | partition_.ResetCounters(); |
| 84 | |
| 85 | // Add a large entry that will only fit once per sector enough times to fill |
| 86 | // the KVS with mostly stale data. |
| 87 | for (size_t i = 0; i < kSectors; i++) { |
| 88 | // modify the value to ensure a key-value different than was previously |
| 89 | // written. |
| 90 | test_data[0]++; |
| 91 | |
| 92 | EXPECT_EQ( |
| 93 | Status::OK, |
| 94 | kvs_.Put("key", as_bytes(span(test_data, sizeof(test_data) - 70)))); |
| 95 | } |
| 96 | |
| 97 | // Add many copies of a differently sized entry that is larger than the |
| 98 | // previous entry. |
| 99 | for (size_t i = 0; i < kSectors * 200; i++) { |
| 100 | // Modify the value to ensure a key-value different than was previously |
| 101 | // written. |
| 102 | test_data[0]++; |
| 103 | |
| 104 | printf("Add entry %zu\n", i); |
| 105 | EXPECT_EQ(Status::OK, kvs_.Put("big_key", test_data)); |
| 106 | } |
| 107 | |
| 108 | EXPECT_EQ(2u, kvs_.size()); |
| 109 | EXPECT_LT(partition_.max_erase_count(), |
| 110 | 2u * partition_.average_erase_count()); |
| 111 | } |
| 112 | |
| 113 | } // namespace |
Armando Montanez | f9e93e1 | 2020-04-22 07:44:14 -0700 | [diff] [blame] | 114 | } // namespace pw::kvs |