mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] commands: time: add -n option for nanoseconds output
@ 2022-01-03 12:03 Ahmad Fatoum
  2022-01-05  8:50 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2022-01-03 12:03 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The current millisecond output may be too coarse for debugging timing
of barebox functionality. Add an optional nanosecond output.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/time.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/commands/time.c b/commands/time.c
index 25ba2da15ed5..2d9fb75e7a8c 100644
--- a/commands/time.c
+++ b/commands/time.c
@@ -9,7 +9,7 @@ static int do_time(int argc, char *argv[])
 	int i;
 	unsigned char *buf;
 	u64 start, end, diff64;
-	unsigned long diff;
+	bool nanoseconds = false;
 	int len = 1; /* '\0' */
 
 	if (argc < 2)
@@ -20,7 +20,13 @@ static int do_time(int argc, char *argv[])
 
 	buf = xzalloc(len);
 
-	for (i = 1; i < argc; i++) {
+	i = 1;
+	if (!strcmp(argv[i], "-n")) {
+		nanoseconds = true;
+		i++;
+	}
+
+	for (; i < argc; i++) {
 		strcat(buf, argv[i]);
 		strcat(buf, " ");
 	}
@@ -33,11 +39,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 +52,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
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] commands: time: add -n option for nanoseconds output
  2022-01-03 12:03 [PATCH] commands: time: add -n option for nanoseconds output Ahmad Fatoum
@ 2022-01-05  8:50 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2022-01-05  8:50 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Mon, Jan 03, 2022 at 01:03:10PM +0100, Ahmad Fatoum wrote:
> The current millisecond output may be too coarse for debugging timing
> of barebox functionality. Add an optional nanosecond output.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  commands/time.c | 21 ++++++++++++++-------
>  1 file changed, 14 insertions(+), 7 deletions(-)
> 
> diff --git a/commands/time.c b/commands/time.c
> index 25ba2da15ed5..2d9fb75e7a8c 100644
> --- a/commands/time.c
> +++ b/commands/time.c
> @@ -9,7 +9,7 @@ static int do_time(int argc, char *argv[])
>  	int i;
>  	unsigned char *buf;
>  	u64 start, end, diff64;
> -	unsigned long diff;
> +	bool nanoseconds = false;
>  	int len = 1; /* '\0' */
>  
>  	if (argc < 2)
> @@ -20,7 +20,13 @@ static int do_time(int argc, char *argv[])
>  
>  	buf = xzalloc(len);

The argv[] elements are copied into a string allocated here. The number
of bytes needed is calculated before the option parsing, so the space
allocated here unnecessarily includes the space for the options. Not a
big deal, but I think it's more consistent to move the option parsing
before calculating the needed buffer space.

>  
> -	for (i = 1; i < argc; i++) {
> +	i = 1;
> +	if (!strcmp(argv[i], "-n")) {
> +		nanoseconds = true;
> +		i++;
> +	}

I just created a patch to support '+' as the first character of
optstring. This allows you to use regular getopt for option parsing
here.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-05  8:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-03 12:03 [PATCH] commands: time: add -n option for nanoseconds output Ahmad Fatoum
2022-01-05  8:50 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox