From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 2/2] Add 'time' command to measure execution time of a command
Date: Wed, 3 Aug 2011 09:19:59 +0200 [thread overview]
Message-ID: <1312355999-19496-3-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1312355999-19496-1-git-send-email-s.hauer@pengutronix.de>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
commands/Kconfig | 10 +++++++++
commands/Makefile | 1 +
commands/time.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 68 insertions(+), 0 deletions(-)
create mode 100644 commands/time.c
diff --git a/commands/Kconfig b/commands/Kconfig
index 5700dc2..e922260 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -97,6 +97,16 @@ endchoice
endif
+config CMD_TIME
+ bool "time"
+ help
+ Just like the unix time command this command allows to measure the
+ execution time of a command. Note: barebox does not use interrupts,
+ so the system timer can overrun during the execution of the command
+ resulting in incorrect results. The timer gets updated in the function
+ checking for ctrl-c, so the time command can be used with commands
+ which are interruptible with ctrl-c.
+
endmenu
menu "file commands "
diff --git a/commands/Makefile b/commands/Makefile
index 8ee4aba..7c88b48 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -57,3 +57,4 @@ obj-$(CONFIG_CMD_LOGIN) += login.o
obj-$(CONFIG_CMD_LED) += led.o
obj-$(CONFIG_CMD_LED_TRIGGER) += trigger.o
obj-$(CONFIG_CMD_USB) += usb.o
+obj-$(CONFIG_CMD_TIME) += time.o
diff --git a/commands/time.c b/commands/time.c
new file mode 100644
index 0000000..9a76945
--- /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
--
1.7.5.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
prev parent reply other threads:[~2011-08-03 7:20 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-08-03 7:19 Add a 'time' command Sascha Hauer
2011-08-03 7:19 ` [PATCH 1/2] memcpy command: allow to interrupt Sascha Hauer
2011-08-03 7:19 ` Sascha Hauer [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1312355999-19496-3-git-send-email-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox