40 #include <px4_platform_common/px4_config.h> 41 #include <px4_platform_common/posix.h> 42 #include <px4_platform_common/tasks.h> 62 const unsigned iterations = 2000;
63 const unsigned alignments = 10;
65 const char *cmd_filename =
"/fs/microsd/mount_test_cmds.txt";
71 if (stat(PX4_STORAGEDIR
"/", &buffer)) {
72 PX4_ERR(
"no microSD card mounted, aborting file test");
79 d = opendir(PX4_STORAGEDIR);
83 while ((dir = readdir(d)) != NULL) {
89 PX4_INFO(
"directory listing ok (FS mounted and readable)");
93 PX4_ERR(
"FAILED LISTING MICROSD ROOT DIRECTORY");
95 if (stat(cmd_filename, &buffer) ==
OK) {
96 (void)unlink(cmd_filename);
110 if (stat(cmd_filename, &buffer) ==
OK) {
113 cmd_fd = open(cmd_filename, O_RDWR | O_NONBLOCK);
115 int ret =
read(cmd_fd, buf,
sizeof(buf));
119 ret = sscanf(buf,
"TEST: %u %u %n", &it_left_fsync, &it_left_abort, &count);
133 PX4_INFO(
"Iterations left: #%d / #%d of %d / %d\n(%s)", it_left_fsync, it_left_abort,
136 int it_left_fsync_prev = it_left_fsync;
139 if (it_left_fsync > 0) {
143 if (it_left_fsync == 0 && it_left_abort > 0) {
148 if (it_left_fsync_prev != it_left_fsync && it_left_fsync == 0) {
149 PX4_INFO(
"\n SUCCESSFULLY PASSED FSYNC'ED WRITES, CONTINUTING WITHOUT FSYNC");
150 fsync(fileno(stdout));
151 fsync(fileno(stderr));
157 if (it_left_abort == 0) {
159 (void)unlink(cmd_filename);
166 cmd_fd = open(cmd_filename, O_TRUNC | O_WRONLY | O_CREAT, PX4_O_MODE_666);
168 PX4_INFO(
"First iteration of file test\n");
172 (void)sprintf(buf,
"TEST: %d %d ", it_left_fsync, it_left_abort);
173 lseek(cmd_fd, 0, SEEK_SET);
174 write(cmd_fd, buf, strlen(buf) + 1);
178 unsigned chunk_sizes[] = {32, 64, 128, 256, 512, 600, 1200};
180 for (
unsigned c = 0; c < (
sizeof(chunk_sizes) /
sizeof(chunk_sizes[0])); c++) {
182 printf(
"\n\n====== FILE TEST: %u bytes chunks (%s) ======\n", chunk_sizes[c],
183 (it_left_fsync > 0) ?
"FSYNC" :
"NO FSYNC");
184 printf(
"unpower the system immediately (within 0.5s) when the hash (#) sign appears\n");
185 fsync(fileno(stdout));
186 fsync(fileno(stderr));
189 for (
unsigned a = 0; a < alignments; a++) {
193 uint8_t write_buf[chunk_sizes[c] + alignments]
__attribute__((aligned(64)));
196 for (
unsigned i = 0; i <
sizeof(write_buf); i++) {
198 write_buf[i] = i + 11;
201 uint8_t read_buf[chunk_sizes[c] + alignments]
__attribute__((aligned(64)));
203 int fd =
px4_open(PX4_STORAGEDIR
"/testfile", O_TRUNC | O_WRONLY | O_CREAT);
205 for (
unsigned i = 0; i < iterations; i++) {
207 int wret =
write(fd, write_buf + a, chunk_sizes[c]);
209 if (wret != (
int)chunk_sizes[c]) {
210 PX4_ERR(
"WRITE ERROR!");
212 if ((0x3 & (uintptr_t)(write_buf + a))) {
213 PX4_ERR(
"memory is unaligned, align shift: %d", a);
220 if (it_left_fsync > 0) {
225 fsync(fileno(stdout));
226 fsync(fileno(stderr));
230 if (it_left_fsync > 0) {
235 fsync(fileno(stdout));
236 fsync(fileno(stderr));
240 fd =
px4_open(PX4_STORAGEDIR
"/testfile", O_RDONLY);
243 for (
unsigned i = 0; i < iterations; i++) {
244 int rret =
read(fd, read_buf, chunk_sizes[c]);
246 if (rret != (
int)chunk_sizes[c]) {
247 PX4_ERR(
"READ ERROR!");
252 bool compare_ok =
true;
254 for (
unsigned j = 0; j < chunk_sizes[c]; j++) {
255 if (read_buf[j] != write_buf[j + a]) {
256 PX4_WARN(
"COMPARISON ERROR: byte %d, align shift: %d", j, a);
263 PX4_ERR(
"ABORTING FURTHER COMPARISON DUE TO ERROR");
269 int ret = unlink(PX4_STORAGEDIR
"/testfile");
274 PX4_ERR(
"UNLINKING FILE FAILED");
281 fsync(fileno(stdout));
282 fsync(fileno(stderr));
288 PX4_INFO(
"Iteration done, rebooting..");
289 fsync(fileno(stdout));
290 fsync(fileno(stderr));
292 px4_systemreset(
false);
High-resolution timer with callouts and timekeeping.
static void read(bootloader_app_shared_t *pshared)
int test_mount(int argc, char *argv[])
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
struct __attribute__((__packed__)) reading_msg
static void write(bootloader_app_shared_t *pshared)
Performance measuring tools.