45 #include <px4_platform_common/px4_config.h> 46 #include <px4_platform_common/module.h> 47 #include <px4_platform_common/getopt.h> 48 #include <px4_platform_common/log.h> 52 static void usage(
void);
70 static bool synchronized;
75 PRINT_MODULE_DESCRIPTION(
"Test the speed of an SD Card");
77 PRINT_MODULE_USAGE_NAME_SIMPLE(
"sd_bench",
"command");
78 PRINT_MODULE_USAGE_PARAM_INT(
'b', 4096, 1, 1000000,
"Block size for each read/write",
true);
79 PRINT_MODULE_USAGE_PARAM_INT(
'r', 5, 1, 1000,
"Number of runs",
true);
80 PRINT_MODULE_USAGE_PARAM_INT(
'd', 2000, 1, 100000,
"Duration of a run in ms",
true);
81 PRINT_MODULE_USAGE_PARAM_FLAG(
's',
"Call fsync after each block (default=at end of each run)",
true);
87 int block_size = 4096;
90 const char *myoptarg = NULL;
95 while ((ch = px4_getopt(argc, argv,
"b:r:d:s", &myoptind, &myoptarg)) != EOF) {
98 block_size = strtol(myoptarg, NULL, 0);
102 num_runs = strtol(myoptarg, NULL, 0);
120 if (block_size <= 0 ||
num_runs <= 0) {
121 PX4_ERR(
"invalid argument");
125 int bench_fd = open(
BENCHMARK_FILE, O_CREAT | O_WRONLY | O_TRUNC, PX4_O_MODE_666);
133 uint8_t *
block = (uint8_t *)malloc(block_size);
136 PX4_ERR(
"Failed to allocate memory block");
141 for (
int i = 0; i < block_size; ++i) {
142 block[i] = (uint8_t)i;
145 PX4_INFO(
"Using block size = %i bytes, sync=%i", block_size, (
int)
synchronized);
165 PX4_INFO(
"Testing Sequential Write Speed...");
166 double total_elapsed = 0.;
167 unsigned int total_blocks = 0;
169 for (
int run = 0; run <
num_runs; ++run) {
171 unsigned int num_blocks = 0;
172 unsigned int max_write_time = 0;
173 unsigned int fsync_time = 0;
178 size_t written =
write(fd, block, block_size);
181 if (write_time > max_write_time) {
182 max_write_time = write_time;
185 if ((
int)written != block_size) {
186 PX4_ERR(
"Write error");
204 PX4_INFO(
" Run %2i: %8.2lf KB/s, max write time: %i ms (=%7.2lf KB/s), fsync: %i ms", run,
205 (
double)block_size * num_blocks / elapsed / 1024.,
206 max_write_time, (
double)block_size / max_write_time * 1000. / 1024., fsync_time);
208 total_elapsed += elapsed;
209 total_blocks += num_blocks;
212 PX4_INFO(
" Avg : %8.2lf KB/s", (
double)block_size * total_blocks / total_elapsed / 1024.);
static int run_duration
duration of a single run [ms]
static const char * BENCHMARK_FILE
static void write_test(int fd, uint8_t *block, int block_size)
sequential write speed test
High-resolution timer with callouts and timekeeping.
static int num_runs
number of runs
static hrt_abstime hrt_elapsed_time(const hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.
static unsigned int time_fsync(int fd)
Measure the time for fsync.
__EXPORT int sd_bench_main(int argc, char *argv[])
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
static void write(bootloader_app_shared_t *pshared)
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).