svn commit: trunk/busybox: include libbb procps

vda at busybox.net vda at busybox.net
Sun Nov 5 00:38:52 UTC 2006


Author: vda
Date: 2006-11-04 16:38:51 -0800 (Sat, 04 Nov 2006)
New Revision: 16508

Log:
top: improve CPU% calculation
style fixes


Modified:
   trunk/busybox/include/libbb.h
   trunk/busybox/libbb/procps.c
   trunk/busybox/procps/kill.c
   trunk/busybox/procps/ps.c
   trunk/busybox/procps/top.c


Changeset:
Modified: trunk/busybox/include/libbb.h
===================================================================
--- trunk/busybox/include/libbb.h	2006-11-04 20:17:03 UTC (rev 16507)
+++ trunk/busybox/include/libbb.h	2006-11-05 00:38:51 UTC (rev 16508)
@@ -494,10 +494,10 @@
 
 #ifndef COMM_LEN
 #ifdef TASK_COMM_LEN
-#define COMM_LEN TASK_COMM_LEN
+enum { COMM_LEN = TASK_COMM_LEN };
 #else
 /* synchronize with sizeof(task_struct.comm) in /usr/include/linux/sched.h */
-#define COMM_LEN 16
+enum { COMM_LEN = 16 };
 #endif
 #endif
 typedef struct {

Modified: trunk/busybox/libbb/procps.c
===================================================================
--- trunk/busybox/libbb/procps.c	2006-11-04 20:17:03 UTC (rev 16507)
+++ trunk/busybox/libbb/procps.c	2006-11-05 00:38:51 UTC (rev 16508)
@@ -8,13 +8,6 @@
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
-#include <dirent.h>
-#include <string.h>
-#include <stdlib.h>
-#include <sys/param.h>
-#include <unistd.h>
-#include <fcntl.h>
-
 #include "libbb.h"
 
 

Modified: trunk/busybox/procps/kill.c
===================================================================
--- trunk/busybox/procps/kill.c	2006-11-04 20:17:03 UTC (rev 16507)
+++ trunk/busybox/procps/kill.c	2006-11-05 00:38:51 UTC (rev 16508)
@@ -15,26 +15,26 @@
 	char *arg;
 	pid_t pid;
 	int signo = SIGTERM, errors = 0, quiet = 0;
-	const int killall = (ENABLE_KILLALL && applet_name[4]=='a'
-	               && (!ENABLE_KILLALL5 || applet_name[7]!='5'));
-	const int killall5 = (ENABLE_KILLALL5 && applet_name[4]=='a'
-	                  && (!ENABLE_KILLALL || applet_name[7]=='5'));
+	const int killall = (ENABLE_KILLALL && applet_name[4] == 'a'
+	               && (!ENABLE_KILLALL5 || applet_name[7] != '5'));
+	const int killall5 = (ENABLE_KILLALL5 && applet_name[4] == 'a'
+	                  && (!ENABLE_KILLALL || applet_name[7] == '5'));
 
 	/* Parse any options */
 	argc--;
 	arg = *++argv;
 
-	if (argc<1 || arg[0]!='-') {
+	if (argc < 1 || arg[0] != '-') {
 		goto do_it_now;
 	}
 
 	/* The -l option, which prints out signal names. */
-	if (arg[1]=='l' && arg[2]=='\0') {
+	if (arg[1] == 'l' && arg[2] == '\0') {
 		const char *name;
-		if (argc==1) {
+		if (argc == 1) {
 			/* Print the whole signal list */
 			int col = 0;
-			for (signo = 1; signo<32; signo++) {
+			for (signo = 1; signo < 32; signo++) {
 				name = get_signame(signo);
 				if (isdigit(name[0])) continue;
 				if (col > 66) {
@@ -45,13 +45,13 @@
 			}
 			puts("");
 		} else { /* -l <sig list> */
-			while ((arg = *++argv)!=NULL) {
+			while ((arg = *++argv)) {
 				if (isdigit(arg[0])) {
 					signo = xatoi_u(arg);
 					name = get_signame(signo);
 				} else {
 					signo = get_signum(arg);
-					if (signo<0)
+					if (signo < 0)
 						bb_error_msg_and_die("unknown signal '%s'", arg);
 					name = get_signame(signo);
 				}
@@ -63,17 +63,17 @@
 	}
 
 	/* The -q quiet option */
-	if (killall && arg[1]=='q' && arg[2]=='\0') {
+	if (killall && arg[1] == 'q' && arg[2] == '\0') {
 		quiet = 1;
 		arg = *++argv;
 		argc--;
-		if (argc<1) bb_show_usage();
-		if (arg[0]!='-') goto do_it_now;
+		if (argc < 1) bb_show_usage();
+		if (arg[0] != '-') goto do_it_now;
 	}
 
 	/* -SIG */
 	signo = get_signum(&arg[1]);
-	if (signo<0)
+	if (signo < 0)
 		bb_error_msg_and_die("bad signal name '%s'", &arg[1]);
 	arg = *++argv;
 	argc--;

Modified: trunk/busybox/procps/ps.c
===================================================================
--- trunk/busybox/procps/ps.c	2006-11-04 20:17:03 UTC (rev 16507)
+++ trunk/busybox/procps/ps.c	2006-11-05 00:38:51 UTC (rev 16508)
@@ -64,7 +64,7 @@
 
 			if (sid) {
 				/* I assume sid initialized with NULL */
-				len = strlen(sid)+1;
+				len = strlen(sid) + 1;
 				safe_strncpy(sbuf, sid, len);
 				freecon(sid);
 				sid = NULL;

Modified: trunk/busybox/procps/top.c
===================================================================
--- trunk/busybox/procps/top.c	2006-11-04 20:17:03 UTC (rev 16507)
+++ trunk/busybox/procps/top.c	2006-11-05 00:38:51 UTC (rev 16508)
@@ -36,7 +36,7 @@
 static int ntop;
 #define OPT_BATCH_MODE (option_mask32 & 0x4)
 
-#ifdef CONFIG_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_USE_TERMIOS
 static int pid_sort(procps_status_t *P, procps_status_t *Q)
 {
 	return (Q->pid - P->pid);
@@ -48,10 +48,10 @@
 	return (int)(Q->rss - P->rss);
 }
 
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
 
-#define sort_depth 3
-static cmp_t sort_function[sort_depth];
+enum { SORT_DEPTH = 3 };
+static cmp_t sort_function[SORT_DEPTH];
 
 static int pcpu_sort(procps_status_t *P, procps_status_t *Q)
 {
@@ -66,7 +66,7 @@
 static int mult_lvl_cmp(void* a, void* b) {
 	int i, cmp_val;
 
-	for (i = 0; i < sort_depth; i++) {
+	for (i = 0; i < SORT_DEPTH; i++) {
 		cmp_val = (*sort_function[i])(a, b);
 		if (cmp_val != 0)
 			return cmp_val;
@@ -93,13 +93,12 @@
 static unsigned total_pcpu;
 /* static unsigned long total_rss; */
 
-struct jiffy_counts {
+typedef struct {
 	unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
 	unsigned long long total;
 	unsigned long long busy;
-};
-static struct jiffy_counts jif, prev_jif;
-
+} jiffy_counts_t;
+static jiffy_counts_t jif, prev_jif;
 static void get_jiffy_counts(void)
 {
 	FILE* fp = xfopen("stat", "r");
@@ -170,7 +169,7 @@
 }
 #else
 static cmp_t sort_function;
-#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
+#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
 
 /* display generic info (meminfo / loadavg) */
 static unsigned long display_generic(int scr_width)
@@ -271,8 +270,9 @@
 	unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */
 	unsigned pmem_shift, pmem_scale;
 
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
 	unsigned pcpu_shift, pcpu_scale;
+	unsigned busy_jifs;
 
 	/* what info of the processes is shown */
 	printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
@@ -296,7 +296,12 @@
 		pmem_scale /= 4;
 		pmem_shift -= 2;
 	}
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+	busy_jifs = jif.busy - prev_jif.busy;
+	/* This happens if there were lots of short-lived processes
+	 * between two top updates (e.g. compilation) */
+	if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
+
 	/*
 	 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
 	 * (pcpu is delta of sys+user time between samples)
@@ -306,7 +311,7 @@
 	 * we assume that unsigned is at least 32-bit.
 	 */
 	pcpu_shift = 6;
-	pcpu_scale = (1000*64*(uint16_t)(jif.busy-prev_jif.busy) ? : 1);
+	pcpu_scale = (1000*64*(uint16_t)busy_jifs ? : 1);
 	while (pcpu_scale < (1U<<(bits_per_int-2))) {
 		pcpu_scale *= 4;
 		pcpu_shift += 2;
@@ -352,7 +357,7 @@
 	ntop = 0;
 }
 
-#ifdef CONFIG_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_USE_TERMIOS
 #include <termios.h>
 #include <signal.h>
 
@@ -362,12 +367,12 @@
 static void reset_term(void)
 {
 	tcsetattr(0, TCSANOW, (void *) &initial_settings);
-#ifdef CONFIG_FEATURE_CLEAN_UP
+#if ENABLE_FEATURE_CLEAN_UP
 	clearmems();
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
 	free(prev_hist);
 #endif
-#endif /* CONFIG_FEATURE_CLEAN_UP */
+#endif /* FEATURE_CLEAN_UP */
 }
 
 static void sig_catcher(int sig ATTRIBUTE_UNUSED)
@@ -375,7 +380,7 @@
 	reset_term();
 	exit(1);
 }
-#endif /* CONFIG_FEATURE_USE_TERMIOS */
+#endif /* FEATURE_USE_TERMIOS */
 
 
 int top_main(int argc, char **argv)
@@ -384,25 +389,24 @@
 	unsigned interval = 5; /* default update rate is 5 seconds */
 	unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */
 	char *sinterval, *siterations;
-#ifdef CONFIG_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_USE_TERMIOS
 	struct termios new_settings;
 	struct timeval tv;
 	fd_set readfds;
 	unsigned char c;
-#endif /* CONFIG_FEATURE_USE_TERMIOS */
+#endif /* FEATURE_USE_TERMIOS */
 
 	/* do normal option parsing */
 	interval = 5;
 	opt_complementary = "-";
-	getopt32(argc, argv, "d:n:b", 
-		&sinterval, &siterations);
+	getopt32(argc, argv, "d:n:b", &sinterval, &siterations);
 	if (option_mask32 & 0x1) interval = xatou(sinterval); // -d
 	if (option_mask32 & 0x2) iterations = xatou(siterations); // -n
 	//if (option_mask32 & 0x4) // -b
 
 	/* change to /proc */
 	xchdir("/proc");
-#ifdef CONFIG_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_USE_TERMIOS
 	tcgetattr(0, (void *) &initial_settings);
 	memcpy(&new_settings, &initial_settings, sizeof(struct termios));
 	/* unbuffered input, turn off echo */
@@ -412,15 +416,15 @@
 	signal(SIGINT, sig_catcher);
 	tcsetattr(0, TCSANOW, (void *) &new_settings);
 	atexit(reset_term);
-#endif /* CONFIG_FEATURE_USE_TERMIOS */
+#endif /* FEATURE_USE_TERMIOS */
 
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
 	sort_function[0] = pcpu_sort;
 	sort_function[1] = mem_sort;
 	sort_function[2] = time_sort;
 #else
 	sort_function = mem_sort;
-#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
+#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
 
 	while (1) {
 		procps_status_t *p;
@@ -428,14 +432,14 @@
 		/* Default to 25 lines - 5 lines for status */
 		lines = 24 - 3;
 		col = 79;
-#ifdef CONFIG_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_USE_TERMIOS
 		get_terminal_width_height(0, &col, &lines);
 		if (lines < 5 || col < MIN_WIDTH) {
 			sleep(interval);
 			continue;
 		}
 		lines -= 3;
-#endif /* CONFIG_FEATURE_USE_TERMIOS */
+#endif /* FEATURE_USE_TERMIOS */
 
 		/* read process IDs & status for all the processes */
 		while ((p = procps_scan(0)) != 0) {
@@ -447,7 +451,7 @@
 		if (ntop == 0) {
 			bb_error_msg_and_die("can't find process info in /proc");
 		}
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
 		if (!prev_hist_count) {
 			do_stats();
 			sleep(1);
@@ -458,14 +462,14 @@
 		qsort(top, ntop, sizeof(procps_status_t), (void*)mult_lvl_cmp);
 #else
 		qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);
-#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
+#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
 		count = lines;
 		if (OPT_BATCH_MODE || count > ntop) {
 			count = ntop;
 		}
 		/* show status for each of the processes */
 		display_status(count, col);
-#ifdef CONFIG_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_USE_TERMIOS
 		tv.tv_sec = interval;
 		tv.tv_usec = 0;
 		FD_ZERO(&readfds);
@@ -478,7 +482,7 @@
 			if (c == 'q' || c == initial_settings.c_cc[VINTR])
 				break;
 			if (c == 'M') {
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
 				sort_function[0] = mem_sort;
 				sort_function[1] = pcpu_sort;
 				sort_function[2] = time_sort;
@@ -486,7 +490,7 @@
 				sort_function = mem_sort;
 #endif
 			}
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
 			if (c == 'P') {
 				sort_function[0] = pcpu_sort;
 				sort_function[1] = mem_sort;
@@ -499,7 +503,7 @@
 			}
 #endif
 			if (c == 'N') {
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
 				sort_function[0] = pid_sort;
 #else
 				sort_function = pid_sort;
@@ -510,7 +514,7 @@
 			break;
 #else
 		sleep(interval);
-#endif /* CONFIG_FEATURE_USE_TERMIOS */
+#endif /* FEATURE_USE_TERMIOS */
 		clearmems();
 	}
 	if (ENABLE_FEATURE_CLEAN_UP)




More information about the busybox-cvs mailing list