[PATCH] top '-c' switch to enable output of SMP cpu lines.

Jonathan Sambrook jonathan at hmmn.org
Tue Jan 25 17:44:16 UTC 2022


 From e20b5585049c3fe3172740035222af1ffb1033a5 Mon Sep 17 00:00:00 2001
From: Jonathan Sambrook <jonathan at hmmn.org>
Date: Tue, 25 Jan 2022 17:07:13 +0000
Subject: [PATCH] top '-c' switch to enable output of SMP cpu lines.

This allows access to SMP cpu lines for batch output, as well as
displaying SMP info in interactive mode without having to press '1'.

My motivation here is to be able to access the SMP information when
graphing BusyBox top batch output with topplot
(see: https://gitlab.com/eBardie/topplot).
---
  procps/top.c | 19 ++++++++++++++++---
  1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/procps/top.c b/procps/top.c
index 804d6f258..23bff1fe8 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -224,7 +224,8 @@ enum {
  	OPT_b = (1 << 2),
  	OPT_H = (1 << 3),
  	OPT_m = (1 << 4),
-	OPT_EOF = (1 << 5), /* pseudo: "we saw EOF in stdin" */
+	OPT_c = (1 << 5),
+	OPT_EOF = (1 << 6), /* pseudo: "we saw EOF in stdin" */
  };
  #define OPT_BATCH_MODE (option_mask32 & OPT_b)

@@ -1078,6 +1079,9 @@ static unsigned handle_input(unsigned scan_mask, 
duration_t interval)
  //usage:   "\n""Options:"
  //usage:	)
  //usage:   "\n""	-b	Batch mode"
+//usage:	IF_FEATURE_TOP_SMP_CPU(
+//usage:   "\n""	-c	Same as '1' key"
+//usage:	)
  //usage:   "\n""	-n N	Exit after N iterations"
  //usage:   "\n""	-d SEC	Delay between updates"
  //usage:	IF_FEATURE_TOPMEM(
@@ -1120,8 +1124,8 @@ int top_main(int argc UNUSED_PARAM, char **argv)

  	/* all args are options; -n NUM */
  	make_all_argv_opts(argv); /* options can be specified w/o dash */
-	col = getopt32(argv, "d:n:bHm", &str_interval, &str_iterations);
-	/* NB: -m and -H are accepted even if not configured */
+	col = getopt32(argv, "d:n:bHmc", &str_interval, &str_iterations);
+	/* NB: -c, -m, and -H are accepted even if not configured */
  #if ENABLE_FEATURE_TOPMEM
  	if (col & OPT_m) /* -m (busybox specific) */
  		scan_mask = TOPMEM_MASK;
@@ -1149,6 +1153,15 @@ int top_main(int argc UNUSED_PARAM, char **argv)
  	/* change to /proc */
  	xchdir("/proc");

+#if ENABLE_FEATURE_TOP_SMP_CPU
+	if (col & OPT_c) {
+		cpu_jif = cpu_prev_jif = NULL;
+        num_cpus = 0;
+		smp_cpu_info = !smp_cpu_info;
+		get_jiffy_counts();
+	}
+#endif
+
  #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
  	sort_function[0] = pcpu_sort;
  	sort_function[1] = mem_sort;
-- 
2.34.1



More information about the busybox mailing list