diff options
Diffstat (limited to 'tests/mtd-tests/flash_speed.c')
-rw-r--r-- | tests/mtd-tests/flash_speed.c | 177 |
1 files changed, 154 insertions, 23 deletions
diff --git a/tests/mtd-tests/flash_speed.c b/tests/mtd-tests/flash_speed.c index 0058269..3489233 100644 --- a/tests/mtd-tests/flash_speed.c +++ b/tests/mtd-tests/flash_speed.c @@ -33,6 +33,7 @@ #include <stdlib.h> #include <libmtd.h> #include <getopt.h> +#include <pthread.h> #include <stdio.h> #include <fcntl.h> #include <time.h> @@ -46,7 +47,7 @@ static const char *mtddev; static libmtd_t mtd_desc; static int fd; -static int peb=-1, count=-1, skip=-1, flags=0; +static int peb=-1, count=-1, skip=-1, flags=0, speb=-1; static struct timespec start, finish; static int pgsize, pgcnt; static int goodebcnt; @@ -57,6 +58,7 @@ static const struct option options[] = { { "peb", required_argument, NULL, 'b' }, { "count", required_argument, NULL, 'c' }, { "skip", required_argument, NULL, 's' }, + { "sec-peb", required_argument, NULL, 'k' }, { NULL, 0, NULL, 0 }, }; @@ -69,7 +71,8 @@ static NORETURN void usage(int status) " -b, --peb <num> Start from this physical erase block\n" " -c, --count <num> Number of erase blocks to use (default: all)\n" " -s, --skip <num> Number of blocks to skip\n" - " -d, --destructive Run destructive (erase and write speed) tests\n", + " -d, --destructive Run destructive (erase and write speed) tests\n" + " -k, --sec-peb <num> Start of secondary block to measure RWW latency (requires -d)\n", status==EXIT_SUCCESS ? stdout : stderr); exit(status); } @@ -93,7 +96,7 @@ static void process_options(int argc, char **argv) int c; while (1) { - c = getopt_long(argc, argv, "hb:c:s:d", options, NULL); + c = getopt_long(argc, argv, "hb:c:s:dk:", options, NULL); if (c == -1) break; @@ -126,15 +129,26 @@ static void process_options(int argc, char **argv) goto failmulti; flags |= DESTRUCTIVE; break; + case 'k': + if (speb >= 0) + goto failmulti; + speb = read_num(c, optarg); + if (speb < 0) + goto failarg; + break; default: exit(EXIT_FAILURE); } } - if (optind < argc) - mtddev = argv[optind++]; - else + if (optind < argc) { + mtddev = mtd_find_dev_node(argv[optind]); + if (!mtddev) + errmsg_die("Can't find MTD device %s", argv[optind]); + optind++; + } else { errmsg_die("No device specified!\n"); + } if (optind < argc) usage(EXIT_FAILURE); @@ -144,11 +158,15 @@ static void process_options(int argc, char **argv) skip = 0; if (count < 0) count = 1; + if (speb >= 0 && !(flags & DESTRUCTIVE)) + goto faildestr; return; failmulti: errmsg_die("'-%c' specified more than once!\n", c); failarg: errmsg_die("Invalid argument for '-%c'!\n", c); +faildestr: + errmsg_die("'-k' specified, -d is missing!\n"); } static int write_eraseblock(int ebnum) @@ -258,22 +276,29 @@ static int read_eraseblock_by_2pages(int ebnum) return err; } -static void start_timing(void) +static void start_timing(struct timespec *start) { - clock_gettime(CLOCK_MONOTONIC_RAW, &start); + clock_gettime(CLOCK_MONOTONIC_RAW, start); } -static void stop_timing(void) +static void stop_timing(struct timespec *finish) { - clock_gettime(CLOCK_MONOTONIC_RAW, &finish); + clock_gettime(CLOCK_MONOTONIC_RAW, finish); } -static long calc_speed(void) +static long calc_duration(struct timespec *start, struct timespec *finish) { long ms; - ms = (finish.tv_sec - start.tv_sec) * 1000L; - ms += (finish.tv_nsec - start.tv_nsec) / 1000000L; + ms = (finish->tv_sec - start->tv_sec) * 1000L; + ms += (finish->tv_nsec - start->tv_nsec) / 1000000L; + + return ms; +} + +static long calc_speed(struct timespec *start, struct timespec *finish) +{ + long ms = calc_duration(start, finish); if (ms <= 0) return 0; @@ -313,8 +338,34 @@ static int erase_good_eraseblocks(unsigned int eb, int ebcnt, int ebskip) return err; } +struct thread_arg { + int (*op)(int peb); + int peb; + struct timespec start; + struct timespec finish; +}; + +static void *op_thread(void *ptr) +{ + struct thread_arg *args = ptr; + unsigned long err = 0; + int i; + + start_timing(&args->start); + for (i = 0; i < count; ++i) { + if (bbt[i]) + continue; + err = args->op(args->peb + i * (skip + 1)); + if (err) + break; + } + stop_timing(&args->finish); + + return (void *)err; +} + #define TIME_OP_PER_PEB( op )\ - start_timing();\ + start_timing(&start);\ for (i = 0; i < count; ++i) {\ if (bbt[i])\ continue;\ @@ -322,8 +373,8 @@ static int erase_good_eraseblocks(unsigned int eb, int ebcnt, int ebskip) if (err)\ goto out;\ }\ - stop_timing();\ - speed = calc_speed() + stop_timing(&finish);\ + speed = calc_speed(&start, &finish) int main(int argc, char **argv) { @@ -343,7 +394,7 @@ int main(int argc, char **argv) puts("not NAND flash, assume page size is 512 bytes."); pgsize = 512; } else { - pgsize = mtd.subpage_size; + pgsize = mtd.min_io_size; } pgcnt = mtd.eb_size / pgsize; @@ -428,12 +479,12 @@ int main(int argc, char **argv) /* Erase all eraseblocks */ if (flags & DESTRUCTIVE) { puts("Testing erase speed"); - start_timing(); + start_timing(&start); err = erase_good_eraseblocks(peb, count, skip); if (err) goto out; - stop_timing(); - speed = calc_speed(); + stop_timing(&finish); + speed = calc_speed(&start, &finish); printf("erase speed is %ld KiB/s\n", speed); } @@ -442,7 +493,7 @@ int main(int argc, char **argv) for (k = 1; k < 7; ++k) { blocks = 1 << k; printf("Testing %dx multi-block erase speed\n", blocks); - start_timing(); + start_timing(&start); for (i = 0; i < count; ) { for (j = 0; j < blocks && (i + j) < count; ++j) if (bbt[i + j]) @@ -456,13 +507,93 @@ int main(int argc, char **argv) goto out; i += j; } - stop_timing(); - speed = calc_speed(); + stop_timing(&finish); + speed = calc_speed(&start, &finish); printf("%dx multi-block erase speed is %ld KiB/s\n", blocks, speed); } } + /* Write a page and immediately after try to read another page. Report + * the latency difference when performed on different banks (NOR only). + */ + if (speb >= 0 && mtd.subpage_size == 1) { + long rww_duration_w, rww_latency_end; + long rww_duration_rnw, rww_duration_r_end; + bool rww_r_end_first; + struct thread_arg write_args_peb = { + .op = write_eraseblock, + .peb = peb, + }; + struct thread_arg read_args_speb = { + .op = read_eraseblock, + .peb = speb, + }; + struct sched_param param_write, param_read; + pthread_attr_t attr_write, attr_read; + pthread_t write_thread, read_thread; + void *retval; + + puts("testing read while write latency"); + + /* Change scheduling priorities so that the write thread gets + *scheduled more aggressively than the read thread. + */ + pthread_attr_init(&attr_write); + pthread_attr_setinheritsched(&attr_write, PTHREAD_EXPLICIT_SCHED); + pthread_attr_setschedpolicy(&attr_write, SCHED_FIFO); + param_write.sched_priority = 42; + pthread_attr_setschedparam(&attr_write, ¶m_write); + + pthread_attr_init(&attr_read); + pthread_attr_setinheritsched(&attr_read, PTHREAD_EXPLICIT_SCHED); + pthread_attr_setschedpolicy(&attr_read, SCHED_FIFO); + param_read.sched_priority = 41; + pthread_attr_setschedparam(&attr_read, ¶m_read); + + err = pthread_create(&write_thread, &attr_write, + (void *)op_thread, &write_args_peb); + if (err) { + errmsg("parallel write pthread create failed"); + goto out; + } + + err = pthread_create(&read_thread, &attr_read, + (void *)op_thread, &read_args_speb); + if (err) { + errmsg("parallel read pthread create failed"); + goto out; + } + + pthread_join(read_thread, &retval); + if ((long)retval) { + errmsg("parallel read pthread failed"); + goto out; + } + + pthread_join(write_thread, &retval); + if ((long)retval) { + errmsg("parallel write pthread failed"); + goto out; + } + + rww_duration_w = calc_duration(&write_args_peb.start, + &write_args_peb.finish); + rww_latency_end = calc_duration(&write_args_peb.finish, + &read_args_speb.finish); + rww_r_end_first = rww_latency_end < 0; + if (rww_r_end_first) + rww_duration_rnw = rww_duration_w; + else + rww_duration_rnw = calc_duration(&write_args_peb.start, + &read_args_speb.finish); + + rww_duration_r_end = calc_duration(&write_args_peb.start, + &read_args_speb.finish); + printf("read while write took %ldms, read ended after %ldms\n", + rww_duration_rnw, rww_duration_r_end); + } + puts("finished"); status = EXIT_SUCCESS; out: |