diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2011-03-16 21:32:05 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2011-08-03 09:16:29 +0200 |
commit | 5de95712d99eff5c628b19681b12a88dd20ec021 (patch) | |
tree | 9ff3127bb886623c7bf2d319294660bb6736dee7 /commands/time.c | |
parent | 39825b591ceb7beaaed8a1c424235db48d79e4d1 (diff) | |
download | barebox-5de95712d99eff5c628b19681b12a88dd20ec021.tar.gz barebox-5de95712d99eff5c628b19681b12a88dd20ec021.tar.xz |
Add 'time' command to measure execution time of a command
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands/time.c')
-rw-r--r-- | commands/time.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/commands/time.c b/commands/time.c new file mode 100644 index 0000000000..9a769451aa --- /dev/null +++ b/commands/time.c @@ -0,0 +1,57 @@ +#include <common.h> +#include <command.h> +#include <clock.h> +#include <asm-generic/div64.h> +#include <malloc.h> + +static int do_time(struct command *cmdtp, int argc, char *argv[]) +{ + int i; + unsigned char *buf; + u64 start, end, diff64; + unsigned long diff; + int len = 0; + + if (argc < 2) + return COMMAND_ERROR_USAGE; + + for (i = 1; i < argc; i++) + len += strlen(argv[i]) + 1; + + buf = xzalloc(len); + + for (i = 1; i < argc; i++) { + strcat(buf, argv[i]); + strcat(buf, " "); + } + + start = get_time_ns(); + + run_command(buf, 0); + + end = get_time_ns(); + + diff64 = end - start; + + do_div(diff64, 1000000); + + diff = diff64; + + printf("time: %ldms\n", diff); + + free(buf); + + return 0; +} + +BAREBOX_CMD_HELP_START(time) +BAREBOX_CMD_HELP_USAGE("time <command>\n") +BAREBOX_CMD_HELP_SHORT("note: This command depends on <command> being interruptible,\n") +BAREBOX_CMD_HELP_SHORT("Otherwise the timer may overrun resulting in incorrect results\n") +BAREBOX_CMD_HELP_END + +BAREBOX_CMD_START(time) + .cmd = do_time, + .usage = "measure execution time of a command", + BAREBOX_CMD_HELP(cmd_time_help) +BAREBOX_CMD_END |