[BusyBox] top.c diff for linux-2.6.0

Paul Dickson dickson at permanentmail.com
Thu Oct 30 23:58:50 UTC 2003


For linux 2.6, the format of /proc/meminfo has changed.  HZ is now 1000 on
x86 and meminfo presents data in kB.

I'll leave it for others to make these changes compile-time selectable
between 2.6 and 2.4.

Not having top work was my biggest problem with using busybox-1.00-pre3 on
2.6 Linux (by which I mean I haven't had much trouble at all).

	-Paul


--- b/router/src/busybox-1.00-pre3/procps/top.c.orig	2003-07-03 03:07:03.000000000 -0700
+++ b/router/src/busybox-1.00-pre3/procps/top.c	2003-10-30 16:46:41.220233225 -0700
@@ -222,8 +222,8 @@
   case 1095 ... 1104 :  Hertz = 1100; break; /* 11 cpus */
   case 1180 ... 1220 :  Hertz = 1200; break; /* Alpha */
   default:
-    /* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
-    Hertz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;
+    /* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 1000. */
+    Hertz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 1000UL : 1024UL;
   }
 }
 
@@ -308,18 +308,26 @@
 static unsigned long display_generic(void)
 {
 	FILE *fp;
-	char buf[80];
 	float avg1, avg2, avg3;
-	unsigned long total, used, mfree, shared, buffers, cached;
+	unsigned long total, used, mfree, buffers, cached;
 
 	/* read memory info */
 	fp = bb_xfopen("meminfo", "r");
-	fgets(buf, sizeof(buf), fp);	/* skip first line */
 
-	if (fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
-		   &total, &used, &mfree, &shared, &buffers, &cached) != 6) {
+	if (fscanf(fp, "MemTotal: %lu kB\n", &total) != 1) {
 		bb_error_msg_and_die("failed to read '%s'", "meminfo");
 	}
+        if (fscanf(fp, "MemFree: %lu kB\n", &mfree) != 1) {
+                bb_error_msg_and_die("failed to read '%s'", "meminfo");
+        }
+        if (fscanf(fp, "Buffers: %lu kB\n", &buffers) != 1) {
+                bb_error_msg_and_die("failed to read '%s'", "meminfo");
+        }
+        if (fscanf(fp, "Cached: %lu kB\n", &cached) != 1) {
+                bb_error_msg_and_die("failed to read '%s'", "meminfo");
+        }
+	used = total - mfree;
+	
 	fclose(fp);
 	
 	/* read load average */
@@ -329,22 +337,15 @@
 	}
 	fclose(fp);
 
-	/* convert to kilobytes */
-	used /= 1024;
-	mfree /= 1024;
-	shared /= 1024;
-	buffers /= 1024;
-	cached /= 1024;
-	
 	/* output memory info and load average */
 	/* clear screen & go to top */
-	printf("\e[H\e[J" "Mem: "
-	       "%ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached\n",
-	       used, mfree, shared, buffers, cached);
+	printf("\e[H\e[J" "Mem:	"
+	       "%ldk av, %ldk used, %ldk free, %ldk shrd, %ldk buff, %ldk cached\n",
+	       total, used, mfree, 0UL, buffers, cached);
 	printf("Load average: %.2f, %.2f, %.2f    "
 			"(State: S=sleeping R=running, W=waiting)\n",
 	       avg1, avg2, avg3);
-	return total / 1024;
+	return total;
 }
 
 
	-Paul




More information about the busybox mailing list