You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by pk...@apache.org on 2022/09/21 14:37:40 UTC

[incubator-nuttx-apps] 01/02: apps/nshlib: add uptime command support

This is an automated email from the ASF dual-hosted git repository.

pkarashchenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit 06f39d10f15c7e68b3bd1c528bb7bb89fe5a8bd0
Author: Junbo Zheng <zh...@xiaomi.com>
AuthorDate: Mon Sep 12 15:43:36 2022 +0800

    apps/nshlib: add uptime command support
    
    run uptime command on sim:
    nsh>
    nsh>
    nsh> uptime
    19:35:01 up  1:40, load average: 0.00, 0.00, 0.00
    nsh>
    nsh>
    nsh> uptime -s
    2022-09-16 17:54:26
    nsh>
    nsh>
    nsh> uptime -p
    up 1 hour, 40 minutes
    nsh>
    nsh>
    nsh> uptime -h
    Usage:
    uptime [options]
    Options:
    -p, show uptime in pretty format
    -h, display this help and exit
    -s, system up since
    nsh>
    nsh>
    nsh> uptime -abc
    uptime: invalid option -- -abc
    Usage:
    uptime [options]
    Options:
    -p, show uptime in pretty format
    -h, display this help and exit
    -s, system up since
    nsh>
    nsh>
    nsh> date
    Fri, Sep 16 19:35:18 2022
    nsh>
    nsh>
    
    Signed-off-by: Junbo Zheng <zh...@xiaomi.com>
---
 nshlib/Kconfig        |   4 ++
 nshlib/nsh.h          |   4 ++
 nshlib/nsh_command.c  |   4 ++
 nshlib/nsh_proccmds.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 134 insertions(+)

diff --git a/nshlib/Kconfig b/nshlib/Kconfig
index 263ab5132..24e508e16 100644
--- a/nshlib/Kconfig
+++ b/nshlib/Kconfig
@@ -518,6 +518,10 @@ config NSH_DISABLE_UNSET
 	bool "Disable unset"
 	default DEFAULT_SMALL
 
+config NSH_DISABLE_UPTIME
+	bool "Disable uptime"
+	default DEFAULT_SMALL
+
 config NSH_DISABLE_URLDECODE
 	bool "Disable urldecode"
 	default DEFAULT_SMALL
diff --git a/nshlib/nsh.h b/nshlib/nsh.h
index 2bf66591b..1b98d0b0c 100644
--- a/nshlib/nsh.h
+++ b/nshlib/nsh.h
@@ -1204,6 +1204,10 @@ int cmd_pmconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
   int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
 #endif
 
+#ifndef CONFIG_NSH_DISABLE_UPTIME
+  int cmd_uptime(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
+#endif
+
 #if defined(CONFIG_NETUTILS_CODECS) && defined(CONFIG_CODECS_BASE64)
 #  ifndef CONFIG_NSH_DISABLE_BASE64DEC
   int cmd_base64decode(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c
index d577e21b1..5e53ea6f4 100644
--- a/nshlib/nsh_command.c
+++ b/nshlib/nsh_command.c
@@ -564,6 +564,10 @@ static const struct cmdmap_s g_cmdmap[] =
   { "unset",    cmd_unset,    2, 2, "<name>" },
 #endif
 
+#ifndef CONFIG_NSH_DISABLE_UPTIME
+  { "uptime",   cmd_uptime,   1, 2, "[-sph]" },
+#endif
+
 #if defined(CONFIG_NETUTILS_CODECS) && defined(CONFIG_CODECS_URLCODE)
 #  ifndef CONFIG_NSH_DISABLE_URLDECODE
   { "urldecode", cmd_urldecode, 2, 3, "[-f] <string or filepath>" },
diff --git a/nshlib/nsh_proccmds.c b/nshlib/nsh_proccmds.c
index 099f40d3c..ef413ed98 100644
--- a/nshlib/nsh_proccmds.c
+++ b/nshlib/nsh_proccmds.c
@@ -33,6 +33,8 @@
 #include <dirent.h>
 #include <errno.h>
 #include <signal.h>
+#include <sys/sysinfo.h>
+#include <time.h>
 
 #include "nsh.h"
 #include "nsh_console.h"
@@ -45,6 +47,15 @@
 #  define CONFIG_NSH_PROC_MOUNTPOINT "/proc"
 #endif
 
+#ifndef CONFIG_NSH_DISABLE_UPTIME
+  #ifndef FSHIFT
+    #  define FSHIFT SI_LOAD_SHIFT
+  #endif
+#  define FIXED_1      (1 << FSHIFT)     /* 1.0 as fixed-point */
+#  define LOAD_INT(x)  ((x) >> FSHIFT)
+#  define LOAD_FRAC(x) (LOAD_INT(((x) & (FIXED_1 - 1)) * 100))
+#endif
+
 /****************************************************************************
  * Private Types
  ****************************************************************************/
@@ -758,3 +769,114 @@ int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_uptime
+ ****************************************************************************/
+
+#ifndef CONFIG_NSH_DISABLE_UPTIME
+int cmd_uptime(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
+{
+  uint32_t updays;
+  uint32_t uphours;
+  uint32_t upminutes;
+
+  time_t current_time_seconds;
+  FAR struct tm *current_time;
+
+  struct sysinfo sys_info;
+
+  time_t uptime = 0;
+
+  bool pretty_format_opt = false;
+  bool system_load_opt = false;
+
+  if (argc < 2)
+    {
+      system_load_opt = true;
+
+      current_time_seconds = time(NULL);
+      current_time = localtime(&current_time_seconds);
+      nsh_output(vtbl, "%02u:%02u:%02u ", current_time->tm_hour,
+                 current_time->tm_min, current_time->tm_sec);
+    }
+  else if (strcmp(argv[1], "-p") == 0)
+    {
+      pretty_format_opt = true;
+    }
+  else if (strcmp(argv[1], "-s") == 0)
+    {
+      sysinfo(&sys_info);
+      time(&current_time_seconds);
+      current_time_seconds -= sys_info.uptime;
+      current_time = localtime(&current_time_seconds);
+      nsh_output(vtbl, "%04u-%02u-%02u %02u:%02u:%02u\n",
+                 current_time->tm_year + 1900, current_time->tm_mon + 1,
+                 current_time->tm_mday, current_time->tm_hour,
+                 current_time->tm_min, current_time->tm_sec);
+      return OK;
+    }
+  else
+    {
+      if (strcmp(argv[1], "-h") != 0)
+        {
+          nsh_output(vtbl, "uptime: invalid option -- %s\n", argv[1]);
+        }
+
+      nsh_output(vtbl, "Usage:\n");
+      nsh_output(vtbl, "uptime [options]\n");
+
+      nsh_output(vtbl, "Options:\n");
+      nsh_output(vtbl, "-p, show uptime in pretty format\n");
+      nsh_output(vtbl, "-h, display this help and exit\n");
+      nsh_output(vtbl, "-s, system up since\n");
+
+      return ERROR;
+    }
+
+  sysinfo(&sys_info);
+  uptime = sys_info.uptime;
+
+  updays = uptime / 86400;
+  uptime -= updays * 86400;
+  uphours = uptime / 3600;
+  uptime -= uphours * 3600;
+  upminutes = uptime / 60;
+
+  nsh_output(vtbl, "up ");
+
+  if (updays)
+    {
+      nsh_output(vtbl, "%" PRIu32 " day%s, ", updays,
+                 (updays > 1) ? "s" : "");
+    }
+
+  if (pretty_format_opt)
+    {
+      if (uphours)
+        {
+          nsh_output(vtbl, "%" PRIu32 " hour%s, ", uphours,
+                     (uphours > 1) ? "s" : "");
+        }
+
+      nsh_output(vtbl, "%" PRIu32 " minute%s", upminutes,
+                 (upminutes > 1) ? "s" : "");
+    }
+  else
+    {
+      nsh_output(vtbl, "%2" PRIu32 ":" "%02" PRIu32, uphours, upminutes);
+    }
+
+  if (system_load_opt)
+    {
+      nsh_output(vtbl, ", load average: %lu.%02lu, %lu.%02lu, %lu.%02lu",
+                 LOAD_INT(sys_info.loads[0]), LOAD_FRAC(sys_info.loads[0]),
+                 LOAD_INT(sys_info.loads[1]), LOAD_FRAC(sys_info.loads[1]),
+                 LOAD_INT(sys_info.loads[2]), LOAD_FRAC(sys_info.loads[2]));
+    }
+
+  nsh_output(vtbl, "\n");
+
+  return OK;
+}
+#endif