blob: 4110c1651e76694de742c7edb26a820cab2ff550 [file] [log] [blame]
Armando Montanezf9e93e12020-04-22 07:44:14 -07001// 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 Rogersd5138f32020-04-28 15:18:56 -070015// Always use stats, these tests depend on it.
16#define PW_KVS_RECORD_PARTITION_STATS 1
17
Armando Montanezf9e93e12020-04-22 07:44:14 -070018#include "gtest/gtest.h"
David Rogersd64cc012020-05-26 12:37:37 -070019#include "pw_kvs/fake_flash_memory.h"
Armando Montanezf9e93e12020-04-22 07:44:14 -070020#include "pw_kvs/flash_memory.h"
David Rogersd5138f32020-04-28 15:18:56 -070021#include "pw_kvs/flash_partition_with_stats.h"
Armando Montanezf9e93e12020-04-22 07:44:14 -070022#include "pw_kvs/key_value_store.h"
23#include "pw_log/log.h"
24
25namespace pw::kvs {
26namespace {
27
David Rogers436b3aa2020-08-03 08:44:10 -070028// For KVS magic value always use a random 32 bit integer rather than a
29// human readable 4 bytes. See pw_kvs/format.h for more information.
30constexpr EntryFormat format{.magic = 0x1bce4ad5, .checksum = nullptr};
Armando Montanezf9e93e12020-04-22 07:44:14 -070031
David Rogersd5138f32020-04-28 15:18:56 -070032class WearTest : public ::testing::Test {
33 protected:
34 WearTest()
35 : flash_(internal::Entry::kMinAlignmentBytes),
36 partition_(&flash_, 0, flash_.sector_count()),
37 kvs_(&partition_, format) {
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080038 EXPECT_EQ(OkStatus(), kvs_.Init());
David Rogersd5138f32020-04-28 15:18:56 -070039 }
40
41 static constexpr size_t kSectors = 16;
42 static constexpr size_t kMaxEntries = 256;
43 static constexpr size_t kTestPartitionSectorSize = 512;
44
David Rogersd64cc012020-05-26 12:37:37 -070045 FakeFlashMemoryBuffer<kTestPartitionSectorSize, kSectors> flash_;
David Rogersd5138f32020-04-28 15:18:56 -070046 FlashPartitionWithStatsBuffer<kSectors> partition_;
47
48 KeyValueStoreBuffer<kMaxEntries, kSectors> kvs_;
49};
50
51// Block of data to use for entry value. Sized to 470 so the total entry results
52// in using most of the 512 byte sector.
53uint8_t test_data[470] = {1, 2, 3, 4, 5, 6};
Armando Montanezf9e93e12020-04-22 07:44:14 -070054
55// Write a large key (i.e. only one entry fits in each sector) enough times to
56// fill up the KVS multiple times, and ensure every sector was garbage collected
57// multiple additional times.
David Rogersd5138f32020-04-28 15:18:56 -070058TEST_F(WearTest, RepeatedLargeEntry) {
Armando Montanezf9e93e12020-04-22 07:44:14 -070059 // Initialize an empty KVS, erasing flash and all tracked sector erase counts.
David Rogersd5138f32020-04-28 15:18:56 -070060 partition_.ResetCounters();
Armando Montanezf9e93e12020-04-22 07:44:14 -070061
62 // Add enough large entries to fill the entire KVS several times.
David Rogersd5138f32020-04-28 15:18:56 -070063 for (size_t i = 0; i < kSectors * 10; ++i) {
64 // modify the value to ensure a key-value different than was previously
65 // written.
66 test_data[0]++;
67
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070068 EXPECT_TRUE(kvs_.Put("large_entry", std::span(test_data)).ok());
Armando Montanezf9e93e12020-04-22 07:44:14 -070069 }
70
71 // Ensure every sector has been erased at several times due to garbage
72 // collection.
David Rogersd5138f32020-04-28 15:18:56 -070073 EXPECT_GE(partition_.min_erase_count(), 7u);
74 EXPECT_LE(partition_.max_erase_count(), partition_.min_erase_count() + 1u);
75
76 partition_.SaveStorageStats(kvs_, "WearTest RepeatedLargeEntry");
Armando Montanezf9e93e12020-04-22 07:44:14 -070077}
78
David Rogersd5138f32020-04-28 15:18:56 -070079// Test a KVS with a number of entries, several sectors that are nearly full
80// of stale (reclaimable) space, and not enough writable (free) space to add a
81// redundant copy for any of the entries. Tests that the add redundancy step of
82// repair is able to use garbage collection to free up space needed for the new
83// copies.
84TEST_F(WearTest, TwoPassFillWithLargeAndLarger) {
85 partition_.ResetCounters();
86
87 // Add a large entry that will only fit once per sector enough times to fill
88 // the KVS with mostly stale data.
89 for (size_t i = 0; i < kSectors; i++) {
90 // modify the value to ensure a key-value different than was previously
91 // written.
92 test_data[0]++;
93
94 EXPECT_EQ(
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080095 OkStatus(),
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070096 kvs_.Put("key",
97 std::as_bytes(std::span(test_data, sizeof(test_data) - 70))));
David Rogersd5138f32020-04-28 15:18:56 -070098 }
99
100 // Add many copies of a differently sized entry that is larger than the
101 // previous entry.
102 for (size_t i = 0; i < kSectors * 200; i++) {
103 // Modify the value to ensure a key-value different than was previously
104 // written.
105 test_data[0]++;
106
107 printf("Add entry %zu\n", i);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800108 EXPECT_EQ(OkStatus(), kvs_.Put("big_key", test_data));
David Rogersd5138f32020-04-28 15:18:56 -0700109 }
110
111 EXPECT_EQ(2u, kvs_.size());
112 EXPECT_LT(partition_.max_erase_count(),
113 2u * partition_.average_erase_count());
114}
115
116} // namespace
Armando Montanezf9e93e12020-04-22 07:44:14 -0700117} // namespace pw::kvs