xiaoxiang781216 commented on code in PR #3628:
URL: https://github.com/apache/nuttx-apps/pull/3628#discussion_r3563063976
##########
nshlib/nsh_proccmds.c:
##########
@@ -72,6 +73,15 @@
# endif
#endif
+/* ANSI escape sequences used by the top command */
+
+#define ANSI_CLEAR_SCREEN "\033[2J"
Review Comment:
should we move to nuttx/include/nuttx/vt100.h
##########
nshlib/nsh_proccmds.c:
##########
@@ -884,6 +894,111 @@ static void top_exit(int signo, FAR siginfo_t *siginfo,
FAR void *context)
}
#endif /* CONFIG_ENABLE_ALL_SIGNALS */
+/****************************************************************************
+ * Name: top_state_is_running
+ ****************************************************************************/
+
+static bool top_state_is_running(FAR const char *state)
+{
+ return strncmp(state, "Running", 7) == 0 ||
+ strncmp(state, "Ready", 5) == 0 ||
+ strncmp(state, "Pending", 7) == 0;
+}
+
+/****************************************************************************
+ * Name: top_summary
+ *
+ * Description:
+ * Print a Linux-top-like summary header: uptime, task counts, CPU
+ * busy/idle and memory usage. Lines end with an erase-to-eol so the
+ * screen can be refreshed in place.
+ *
+ ****************************************************************************/
+
+static void top_summary(FAR struct nsh_vtbl_s *vtbl,
+ FAR struct nsh_topstatus_s *topstatus)
+{
+ struct sysinfo info;
+ unsigned long uptime;
+ unsigned int busy;
+ size_t running = 0;
+ size_t i;
+
+ for (i = 0; i < topstatus->index; i++)
+ {
+ if (top_state_is_running(topstatus->status[i]->td_state))
+ {
+ running++;
+ }
+ }
+
+ sysinfo(&info);
+ uptime = info.uptime;
+
+ if (uptime >= 86400)
+ {
+ nsh_output(vtbl, ANSI_CLEAR_LINE "top - up %lud %02lu:%02lu:%02lu\n",
+ uptime / 86400, (uptime % 86400) / 3600,
+ (uptime % 3600) / 60, uptime % 60);
+ }
+ else
+ {
+ nsh_output(vtbl, ANSI_CLEAR_LINE "top - up %02lu:%02lu:%02lu\n",
+ uptime / 3600, (uptime % 3600) / 60, uptime % 60);
+ }
+
+ nsh_output(vtbl, ANSI_CLEAR_LINE
+ "Tasks: %zu total, %zu running, %zu sleeping\n",
+ topstatus->index, running, topstatus->index - running);
+
+ busy = (unsigned int)((info.loads[0] * 1000) >> SI_LOAD_SHIFT);
+ nsh_output(vtbl, ANSI_CLEAR_LINE "%%Cpu(s): %2u.%u busy, %2u.%u idle\n",
+ busy / 10, busy % 10,
+ (1000 - busy) / 10, (1000 - busy) % 10);
+
+ nsh_output(vtbl, ANSI_CLEAR_LINE
+ "Mem : %8lu total, %8lu used, %8lu free\n",
+ (unsigned long)info.totalram,
+ (unsigned long)(info.totalram - info.freeram),
+ (unsigned long)info.freeram);
Review Comment:
remove ALL cast
##########
nshlib/nsh_proccmds.c:
##########
@@ -884,6 +894,111 @@ static void top_exit(int signo, FAR siginfo_t *siginfo,
FAR void *context)
}
#endif /* CONFIG_ENABLE_ALL_SIGNALS */
+/****************************************************************************
+ * Name: top_state_is_running
+ ****************************************************************************/
+
+static bool top_state_is_running(FAR const char *state)
+{
+ return strncmp(state, "Running", 7) == 0 ||
+ strncmp(state, "Ready", 5) == 0 ||
+ strncmp(state, "Pending", 7) == 0;
+}
+
+/****************************************************************************
+ * Name: top_summary
+ *
+ * Description:
+ * Print a Linux-top-like summary header: uptime, task counts, CPU
+ * busy/idle and memory usage. Lines end with an erase-to-eol so the
+ * screen can be refreshed in place.
+ *
+ ****************************************************************************/
+
+static void top_summary(FAR struct nsh_vtbl_s *vtbl,
+ FAR struct nsh_topstatus_s *topstatus)
+{
+ struct sysinfo info;
+ unsigned long uptime;
+ unsigned int busy;
+ size_t running = 0;
+ size_t i;
+
+ for (i = 0; i < topstatus->index; i++)
+ {
+ if (top_state_is_running(topstatus->status[i]->td_state))
+ {
+ running++;
+ }
+ }
+
+ sysinfo(&info);
+ uptime = info.uptime;
+
+ if (uptime >= 86400)
+ {
+ nsh_output(vtbl, ANSI_CLEAR_LINE "top - up %lud %02lu:%02lu:%02lu\n",
+ uptime / 86400, (uptime % 86400) / 3600,
+ (uptime % 3600) / 60, uptime % 60);
+ }
+ else
+ {
+ nsh_output(vtbl, ANSI_CLEAR_LINE "top - up %02lu:%02lu:%02lu\n",
+ uptime / 3600, (uptime % 3600) / 60, uptime % 60);
+ }
+
+ nsh_output(vtbl, ANSI_CLEAR_LINE
+ "Tasks: %zu total, %zu running, %zu sleeping\n",
+ topstatus->index, running, topstatus->index - running);
+
+ busy = (unsigned int)((info.loads[0] * 1000) >> SI_LOAD_SHIFT);
Review Comment:
remove the cast
##########
nshlib/nsh_proccmds.c:
##########
@@ -884,6 +894,111 @@ static void top_exit(int signo, FAR siginfo_t *siginfo,
FAR void *context)
}
#endif /* CONFIG_ENABLE_ALL_SIGNALS */
+/****************************************************************************
+ * Name: top_state_is_running
+ ****************************************************************************/
+
+static bool top_state_is_running(FAR const char *state)
+{
+ return strncmp(state, "Running", 7) == 0 ||
+ strncmp(state, "Ready", 5) == 0 ||
+ strncmp(state, "Pending", 7) == 0;
+}
+
+/****************************************************************************
+ * Name: top_summary
+ *
+ * Description:
+ * Print a Linux-top-like summary header: uptime, task counts, CPU
+ * busy/idle and memory usage. Lines end with an erase-to-eol so the
+ * screen can be refreshed in place.
+ *
+ ****************************************************************************/
+
+static void top_summary(FAR struct nsh_vtbl_s *vtbl,
+ FAR struct nsh_topstatus_s *topstatus)
+{
+ struct sysinfo info;
+ unsigned long uptime;
+ unsigned int busy;
+ size_t running = 0;
+ size_t i;
+
+ for (i = 0; i < topstatus->index; i++)
+ {
+ if (top_state_is_running(topstatus->status[i]->td_state))
+ {
+ running++;
+ }
+ }
+
+ sysinfo(&info);
+ uptime = info.uptime;
+
+ if (uptime >= 86400)
+ {
+ nsh_output(vtbl, ANSI_CLEAR_LINE "top - up %lud %02lu:%02lu:%02lu\n",
+ uptime / 86400, (uptime % 86400) / 3600,
+ (uptime % 3600) / 60, uptime % 60);
+ }
+ else
+ {
+ nsh_output(vtbl, ANSI_CLEAR_LINE "top - up %02lu:%02lu:%02lu\n",
+ uptime / 3600, (uptime % 3600) / 60, uptime % 60);
+ }
+
+ nsh_output(vtbl, ANSI_CLEAR_LINE
+ "Tasks: %zu total, %zu running, %zu sleeping\n",
+ topstatus->index, running, topstatus->index - running);
+
+ busy = (unsigned int)((info.loads[0] * 1000) >> SI_LOAD_SHIFT);
+ nsh_output(vtbl, ANSI_CLEAR_LINE "%%Cpu(s): %2u.%u busy, %2u.%u idle\n",
+ busy / 10, busy % 10,
+ (1000 - busy) / 10, (1000 - busy) % 10);
+
+ nsh_output(vtbl, ANSI_CLEAR_LINE
+ "Mem : %8lu total, %8lu used, %8lu free\n",
+ (unsigned long)info.totalram,
+ (unsigned long)(info.totalram - info.freeram),
+ (unsigned long)info.freeram);
+
+ nsh_output(vtbl, ANSI_CLEAR_LINE "\n");
+}
+
+/****************************************************************************
+ * Name: top_wait_key
+ *
+ * Description:
+ * Sleep for the update interval, returning true if the user asked to
+ * quit (q, ESC or Ctrl-C read from stdin).
+ *
+ ****************************************************************************/
+
+static bool top_wait_key(int delay)
+{
+ struct pollfd fds;
+ int elapsed;
+
+ fds.fd = STDIN_FILENO;
+ fds.events = POLLIN;
+
+ for (elapsed = 0; elapsed < delay * 1000; elapsed += 100)
+ {
+ if (poll(&fds, 1, 100) > 0)
Review Comment:
why not poll with delay and remove for loop?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]