imix/test/linear_log_test.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.
//! Tests the log storage interface in linear mode. For testing in circular mode, see
//! log_test.rs.
//!
//! The testing framework creates a non-circular log storage interface in flash and performs a
//! series of writes and syncs to ensure that the non-circular log properly denies overly-large
//! writes once it is full. For testing all of the general capabilities of the log storage
//! interface, see log_test.rs.
//!
//! To run the test, add the following line to the imix boot sequence:
//! ```
//! test::linear_log_test::run(mux_alarm);
//! ```
//! and use the `USER` and `RESET` buttons to manually erase the log and reboot the imix,
//! respectively.
use capsules_core::virtualizers::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
use capsules_extra::log;
use core::cell::Cell;
use core::ptr::addr_of_mut;
use kernel::debug;
use kernel::hil::flash;
use kernel::hil::log::{LogRead, LogReadClient, LogWrite, LogWriteClient};
use kernel::hil::time::{Alarm, AlarmClient, ConvertTicks};
use kernel::static_init;
use kernel::storage_volume;
use kernel::utilities::cells::{NumericCellExt, TakeCell};
use kernel::ErrorCode;
use sam4l::ast::Ast;
use sam4l::flashcalw;
// Allocate 1kiB volume for log storage.
storage_volume!(LINEAR_TEST_LOG, 1);
pub unsafe fn run(
mux_alarm: &'static MuxAlarm<'static, Ast>,
flash_controller: &'static sam4l::flashcalw::FLASHCALW,
) {
// Set up flash controller.
flash_controller.configure();
let pagebuffer = static_init!(flashcalw::Sam4lPage, flashcalw::Sam4lPage::default());
// Create actual log storage abstraction on top of flash.
let log = static_init!(
Log,
log::Log::new(&LINEAR_TEST_LOG, flash_controller, pagebuffer, false)
);
kernel::deferred_call::DeferredCallClient::register(log);
flash::HasClient::set_client(flash_controller, log);
let alarm = static_init!(
VirtualMuxAlarm<'static, Ast>,
VirtualMuxAlarm::new(mux_alarm)
);
alarm.setup();
// Create and run test for log storage.
let test = static_init!(
LogTest<VirtualMuxAlarm<'static, Ast>>,
LogTest::new(log, &mut *addr_of_mut!(BUFFER), alarm, &TEST_OPS)
);
log.set_read_client(test);
log.set_append_client(test);
test.alarm.set_alarm_client(test);
test.run();
}
static TEST_OPS: [TestOp; 9] = [
TestOp::Read,
// Write to first page.
TestOp::Write(8),
TestOp::Write(300),
// Write to next page, too large to fit on first.
TestOp::Write(304),
// Write should fail, not enough space remaining.
TestOp::Write(306),
// Write should succeed, enough space for a smaller entry.
TestOp::Write(9),
// Read back everything to verify and sync.
TestOp::Read,
TestOp::Sync,
// Write should still fail after sync.
TestOp::Write(308),
];
// Buffer for reading from and writing to in the log tests.
static mut BUFFER: [u8; 310] = [0; 310];
// Time to wait in between log operations.
const WAIT_MS: u32 = 3;
// A single operation within the test.
#[derive(Clone, Copy, PartialEq)]
enum TestOp {
Read,
Write(usize),
Sync,
}
type Log = log::Log<'static, flashcalw::FLASHCALW>;
struct LogTest<A: 'static + Alarm<'static>> {
log: &'static Log,
buffer: TakeCell<'static, [u8]>,
alarm: &'static A,
ops: &'static [TestOp],
op_index: Cell<usize>,
}
impl<A: 'static + Alarm<'static>> LogTest<A> {
fn new(
log: &'static Log,
buffer: &'static mut [u8],
alarm: &'static A,
ops: &'static [TestOp],
) -> LogTest<A> {
debug!(
"Log recovered from flash (Start and end entry IDs: {:?} to {:?})",
log.log_start(),
log.log_end()
);
LogTest {
log,
buffer: TakeCell::new(buffer),
alarm,
ops,
op_index: Cell::new(0),
}
}
fn run(&self) {
let op_index = self.op_index.get();
if op_index == self.ops.len() {
debug!("Linear Log Storage test succeeded!");
return;
}
match self.ops[op_index] {
TestOp::Read => self.read(),
TestOp::Write(len) => self.write(len),
TestOp::Sync => self.sync(),
}
}
fn read(&self) {
self.buffer.take().map_or_else(
|| panic!("NO BUFFER"),
move |buffer| {
// Clear buffer first to make debugging more sane.
for e in buffer.iter_mut() {
*e = 0;
}
if let Err((error, original_buffer)) = self.log.read(buffer, buffer.len()) {
self.buffer.replace(original_buffer);
match error {
ErrorCode::FAIL => {
// No more entries, start writing again.
debug!(
"READ DONE: READ OFFSET: {:?} / WRITE OFFSET: {:?}",
self.log.next_read_entry_id(),
self.log.log_end()
);
self.op_index.increment();
self.run();
}
ErrorCode::BUSY => {
debug!("Flash busy, waiting before reattempting read");
self.wait();
}
_ => panic!("READ FAILED: {:?}", error),
}
}
},
);
}
fn write(&self, len: usize) {
self.buffer
.take()
.map(move |buffer| {
let expect_write_fail = self.log.log_end() + len > LINEAR_TEST_LOG.len();
// Set buffer value.
for i in 0..buffer.len() {
buffer[i] = if i < len {
len as u8
} else {
0
};
}
if let Err((error, original_buffer)) = self.log.append(buffer, len) {
self.buffer.replace(original_buffer);
match error {
ErrorCode::FAIL =>
if expect_write_fail {
debug!(
"Write failed on {} byte write, as expected",
len
);
self.op_index.increment();
self.run();
} else {
panic!(
"Write failed unexpectedly on {} byte write (read entry ID: {:?}, append entry ID: {:?})",
len,
self.log.next_read_entry_id(),
self.log.log_end()
);
}
ErrorCode::BUSY => self.wait(),
_ => panic!("WRITE FAILED: {:?}", error),
}
} else if expect_write_fail {
panic!(
"Write succeeded unexpectedly on {} byte write (read entry ID: {:?}, append entry ID: {:?})",
len,
self.log.next_read_entry_id(),
self.log.log_end()
);
}
})
.unwrap();
}
fn sync(&self) {
match self.log.sync() {
Ok(()) => (),
error => panic!("Sync failed: {:?}", error),
}
}
fn wait(&self) {
let delay = self.alarm.ticks_from_ms(WAIT_MS);
let now = self.alarm.now();
self.alarm.set_alarm(now, delay);
}
}
impl<A: Alarm<'static>> LogReadClient for LogTest<A> {
fn read_done(&self, buffer: &'static mut [u8], length: usize, error: Result<(), ErrorCode>) {
match error {
Ok(()) => {
// Verify correct value was read.
assert!(length > 0);
for i in 0..length {
if buffer[i] != length as u8 {
panic!(
"Read incorrect value {} at index {}, expected {}",
buffer[i], i, length
);
}
}
debug!("Successful read of size {}", length);
self.buffer.replace(buffer);
self.wait();
}
_ => {
panic!("Read failed unexpectedly!");
}
}
}
fn seek_done(&self, _error: Result<(), ErrorCode>) {
unreachable!();
}
}
impl<A: Alarm<'static>> LogWriteClient for LogTest<A> {
fn append_done(
&self,
buffer: &'static mut [u8],
length: usize,
records_lost: bool,
error: Result<(), ErrorCode>,
) {
assert!(!records_lost);
match error {
Ok(()) => {
debug!("Write succeeded on {} byte write, as expected", length);
self.buffer.replace(buffer);
self.op_index.increment();
self.wait();
}
error => panic!("WRITE FAILED IN CALLBACK: {:?}", error),
}
}
fn sync_done(&self, error: Result<(), ErrorCode>) {
if error == Ok(()) {
debug!(
"SYNC DONE: READ OFFSET: {:?} / WRITE OFFSET: {:?}",
self.log.next_read_entry_id(),
self.log.log_end()
);
} else {
panic!("Sync failed: {:?}", error);
}
self.op_index.increment();
self.run();
}
fn erase_done(&self, _error: Result<(), ErrorCode>) {
unreachable!();
}
}
impl<A: Alarm<'static>> AlarmClient for LogTest<A> {
fn alarm(&self) {
self.run();
}
}