shell: modules: kernel: add human readable uptime
Added options: -p and --pretty to the kernel updtime command. Fixes: #62543 Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@verkada.com>
This commit is contained in:
parent
dccc575767
commit
c631bc7757
1 changed files with 37 additions and 4 deletions
|
@ -44,13 +44,45 @@ static int cmd_kernel_version(const struct shell *sh,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_kernel_uptime(const struct shell *sh,
|
||||
size_t argc, char **argv)
|
||||
#define MINUTES_FACTOR (MSEC_PER_SEC * SEC_PER_MIN)
|
||||
#define HOURS_FACTOR (MINUTES_FACTOR * MIN_PER_HOUR)
|
||||
#define DAYS_FACTOR (HOURS_FACTOR * HOUR_PER_DAY)
|
||||
|
||||
static int cmd_kernel_uptime(const struct shell *sh, size_t argc, char **argv)
|
||||
{
|
||||
ARG_UNUSED(argc);
|
||||
ARG_UNUSED(argv);
|
||||
|
||||
shell_print(sh, "Uptime: %u ms", k_uptime_get_32());
|
||||
int64_t milliseconds = k_uptime_get();
|
||||
int64_t days;
|
||||
int64_t hours;
|
||||
int64_t minutes;
|
||||
int64_t seconds;
|
||||
|
||||
if (argc == 1) {
|
||||
shell_print(sh, "Uptime: %llu ms", milliseconds);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* No need to enable the getopt and getopt_long for just one option. */
|
||||
if (strcmp("-p", argv[1]) && strcmp("--pretty", argv[1]) != 0) {
|
||||
shell_error(sh, "Usupported option: %s", argv[1]);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
days = milliseconds / DAYS_FACTOR;
|
||||
milliseconds %= DAYS_FACTOR;
|
||||
hours = milliseconds / HOURS_FACTOR;
|
||||
milliseconds %= HOURS_FACTOR;
|
||||
minutes = milliseconds / MINUTES_FACTOR;
|
||||
milliseconds %= MINUTES_FACTOR;
|
||||
seconds = milliseconds / MSEC_PER_SEC;
|
||||
milliseconds = milliseconds % MSEC_PER_SEC;
|
||||
|
||||
shell_print(sh,
|
||||
"uptime: %llu days, %llu hours, %llu minutes, %llu seconds, %llu milliseconds",
|
||||
days, hours, minutes, seconds, milliseconds);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -371,7 +403,8 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_kernel,
|
|||
#if defined(CONFIG_SYS_HEAP_RUNTIME_STATS) && (CONFIG_HEAP_MEM_POOL_SIZE > 0)
|
||||
SHELL_CMD(heap, NULL, "System heap usage statistics.", cmd_kernel_heap),
|
||||
#endif
|
||||
SHELL_CMD(uptime, NULL, "Kernel uptime.", cmd_kernel_uptime),
|
||||
SHELL_CMD_ARG(uptime, NULL, "Kernel uptime. Can be called with the -p or --pretty options",
|
||||
cmd_kernel_uptime, 1, 1),
|
||||
SHELL_CMD(version, NULL, "Kernel version.", cmd_kernel_version),
|
||||
SHELL_CMD_ARG(sleep, NULL, "ms", cmd_kernel_sleep, 2, 0),
|
||||
#if defined(CONFIG_LOG_RUNTIME_FILTERING)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue