All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sascha Hauer <sha@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH 1/6] commands: add new uptime command
Date: Thu, 27 Oct 2022 09:11:50 +0200	[thread overview]
Message-ID: <20221027071150.GR6702@pengutronix.de> (raw)
In-Reply-To: <20221026064205.2360041-1-a.fatoum@pengutronix.de>

On Wed, Oct 26, 2022 at 08:42:00AM +0200, Ahmad Fatoum wrote:
> We have a time command to record the delta of get_time_ns() to time
> command execution, but have no command to just print get_time_ns().
> 
> Such a command can be useful to debug clocksources or to verify that a
> system is still running and hasn't been reset by a yet unhandled
> watchdog in-between.
> 
> Make development a bit easier by providing a new uptime command.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  commands/Kconfig  | 13 +++++++++
>  commands/Makefile |  1 +
>  commands/uptime.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 86 insertions(+)
>  create mode 100644 commands/uptime.c
> 
> diff --git a/commands/Kconfig b/commands/Kconfig
> index 2ce990b5616a..a59616ad1474 100644
> --- a/commands/Kconfig
> +++ b/commands/Kconfig
> @@ -2289,6 +2289,19 @@ config CMD_TIME
>  	  Note: This command depends on COMMAND being interruptible,
>  	  otherwise the timer may overrun resulting in incorrect results
>  
> +config CMD_UPTIME
> +	bool "uptime"
> +	help
> +	  uptime - Tell how long barebox has been running
> +
> +	  Usage: uptime [-n]
> +
> +	  This command formats the number of elapsed nanoseconds
> +	  as measured with the current clocksource.
> +
> +	  Options:
> +	    -n		output elapsed time in nanoseconds
> +
>  config CMD_STATE
>  	tristate
>  	depends on STATE
> diff --git a/commands/Makefile b/commands/Makefile
> index 68d0d05365a5..cac1d4f2535b 100644
> --- a/commands/Makefile
> +++ b/commands/Makefile
> @@ -80,6 +80,7 @@ obj-$(CONFIG_CMD_WD)		+= wd.o
>  obj-$(CONFIG_CMD_LED_TRIGGER)	+= trigger.o
>  obj-$(CONFIG_CMD_USB)		+= usb.o
>  obj-$(CONFIG_CMD_TIME)		+= time.o
> +obj-$(CONFIG_CMD_UPTIME)	+= uptime.o
>  obj-$(CONFIG_CMD_OFTREE)	+= oftree.o
>  obj-$(CONFIG_CMD_OF_DIFF)	+= of_diff.o
>  obj-$(CONFIG_CMD_OF_PROPERTY)	+= of_property.o
> diff --git a/commands/uptime.c b/commands/uptime.c
> new file mode 100644
> index 000000000000..e220fec72139
> --- /dev/null
> +++ b/commands/uptime.c
> @@ -0,0 +1,72 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +
> +#include <common.h>
> +#include <command.h>
> +#include <clock.h>
> +#include <linux/math64.h>
> +
> +#define NSEC_PER_MINUTE	(NSEC_PER_SEC * 60LL)
> +#define NSEC_PER_HOUR	(NSEC_PER_MINUTE * 60LL)
> +#define NSEC_PER_DAY	(NSEC_PER_HOUR * 24LL)
> +#define NSEC_PER_WEEK	(NSEC_PER_DAY * 7LL)
> +
> +static bool print_with_unit(u64 val, const char *unit, bool comma)
> +{
> +	if (!val)
> +		return comma;
> +
> +	printf("%s%llu %s%s", comma ? ", " : "", val, unit, val > 1 ? "s" : "");
> +	return true;
> +}
> +
> +static int do_uptime(int argc, char *argv[])
> +{
> +	u64 timestamp, weeks, days, hours, minutes;
> +	bool comma = false;
> +
> +	timestamp = get_time_ns();
> +
> +	if (argc == 2 && !strcmp(argv[1], "-n")) {
> +		printf("up %lluns\n", timestamp);
> +		return 0;
> +	}

Use getopt(), that's what we have it for.

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 |



      parent reply	other threads:[~2022-10-27  7:13 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-26  6:42 [PATCH 1/6] commands: add new uptime command Ahmad Fatoum
2022-10-26  6:42 ` [PATCH 2/6] commands: time: refactor into new strjoin Ahmad Fatoum
2022-10-26  6:42 ` [PATCH 3/6] string: reduce strjoin runtime, drop trailing separator Ahmad Fatoum
2022-10-27  6:56   ` Sascha Hauer
2022-10-27  7:24     ` Ahmad Fatoum
2022-10-27  7:33       ` Sascha Hauer
2022-10-27  7:53         ` Ahmad Fatoum
2022-10-26  6:42 ` [PATCH 4/6] test: self: add strjoin tests Ahmad Fatoum
2022-10-26  6:42 ` [PATCH 5/6] commands: drvinfo: support filtering by driver Ahmad Fatoum
2022-10-27  7:29   ` Sascha Hauer
2022-10-27  7:51     ` Ahmad Fatoum
2022-10-27  8:49       ` Sascha Hauer
2022-10-26  6:42 ` [PATCH 6/6] test: self: only include ramfs selftest when CONFIG_SELFTEST_FS_RAMFS=y Ahmad Fatoum
2022-10-27  7:11 ` 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=20221027071150.GR6702@pengutronix.de \
    --to=sha@pengutronix.de \
    --cc=a.fatoum@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.