diff options
Diffstat (limited to 'commands/time.c')
-rw-r--r-- | commands/time.c | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/commands/time.c b/commands/time.c index 29000d96d8..a3f2704071 100644 --- a/commands/time.c +++ b/commands/time.c @@ -1,29 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0-only + #include <common.h> #include <command.h> #include <clock.h> -#include <asm-generic/div64.h> +#include <linux/math64.h> #include <malloc.h> +#include <getopt.h> static int do_time(int argc, char *argv[]) { - int i; + int opt; unsigned char *buf; u64 start, end, diff64; - unsigned long diff; - int len = 1; /* '\0' */ - - if (argc < 2) - return COMMAND_ERROR_USAGE; + bool nanoseconds = false; + + while ((opt = getopt(argc, argv, "+n")) > 0) { + switch (opt) { + case 'n': + nanoseconds = true; + break; + default: + return COMMAND_ERROR_USAGE; + } + } - for (i = 1; i < argc; i++) - len += strlen(argv[i]) + 1; + argv += optind; + argc -= optind; - buf = xzalloc(len); + if (argc < 1) + return COMMAND_ERROR_USAGE; - for (i = 1; i < argc; i++) { - strcat(buf, argv[i]); - strcat(buf, " "); - } + buf = strjoin(" ", argv, argc); start = get_time_ns(); @@ -33,11 +40,10 @@ static int do_time(int argc, char *argv[]) diff64 = end - start; - do_div(diff64, 1000000); - - diff = diff64; + if (!nanoseconds) + do_div(diff64, 1000000); - printf("time: %lums\n", diff); + printf("time: %llu%cs\n", diff64, nanoseconds ? 'n' : 'm'); free(buf); @@ -47,12 +53,14 @@ static int do_time(int argc, char *argv[]) BAREBOX_CMD_HELP_START(time) BAREBOX_CMD_HELP_TEXT("Note: This command depends on COMMAND being interruptible,") BAREBOX_CMD_HELP_TEXT("otherwise the timer may overrun resulting in incorrect results") +BAREBOX_CMD_HELP_TEXT("Options:") +BAREBOX_CMD_HELP_OPT ("-n", "output elapsed time in nanoseconds") BAREBOX_CMD_HELP_END BAREBOX_CMD_START(time) .cmd = do_time, BAREBOX_CMD_DESC("measure execution duration of a command") - BAREBOX_CMD_OPTS("COMMAND") + BAREBOX_CMD_OPTS("[-n] COMMAND") BAREBOX_CMD_GROUP(CMD_GRP_MISC) BAREBOX_CMD_HELP(cmd_time_help) BAREBOX_CMD_END |