[PATCH] ksysguard+linux 2.5

Dirk Mueller mueller at kde.org
Sun Nov 3 04:36:10 GMT 2002


Hi, 

the appended patch makes ksysguard work with Kernel 2.5. IMHO it should be 
applied before KDE 3.1 release. 

Please review. 


-- 
Dirk (received 104 mails today)
-------------- next part --------------
Index: ksysguardd/ksysguardd.c
===================================================================
RCS file: /home/kde/kdebase/ksysguard/ksysguardd/ksysguardd.c,v
retrieving revision 1.8
diff -u -5 -d -p -r1.8 ksysguardd.c
--- ksysguardd/ksysguardd.c	2002/07/15 06:33:22	1.8
+++ ksysguardd/ksysguardd.c	2002/11/03 04:33:36
@@ -114,11 +114,11 @@ printWelcome(FILE* out)
 	fprintf(out,
 			"ksysguardd %s\n"
 			"(c) 1999, 2000, 2001, 2002 Chris Schlaeger <cs at kde.org> and\n"
 			"(c) 2001 Tobias Koenig <tokoe at kde.org>\n"
 			"This program is part of the KDE Project and licensed under\n"
-			"the GNU GPL version 2. See www.kde.org for details!\n", 
+			"the GNU GPL version 2. See http://www.kde.org for details.\n", 
 			KSYSGUARD_VERSION);
 	fflush(out);
 }
 
 static int
Index: ksysguardd/Linux/Memory.c
===================================================================
RCS file: /home/kde/kdebase/ksysguard/ksysguardd/Linux/Memory.c,v
retrieving revision 1.24
diff -u -5 -d -p -r1.24 Memory.c
--- ksysguardd/Linux/Memory.c	2002/05/19 00:13:15	1.24
+++ ksysguardd/Linux/Memory.c	2002/11/03 04:33:36
@@ -23,10 +23,11 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "Command.h"
 #include "Memory.h"
 #include "ksysguardd.h"
 
@@ -42,28 +43,30 @@ static unsigned long Buffers = 0;
 static unsigned long Cached = 0;
 static unsigned long STotal = 0;
 static unsigned long SFree = 0;
 static unsigned long SUsed = 0;
 
+static void 
+scan_one(const char* buff, const char *key, unsigned long int* val)
+{   
+	int o;
+        char *b = strstr(buff, key);
+        if (b)  
+		o = sscanf(b + strlen(key), ": %lu", val);
+}
+
 static void
 processMemInfo()
 {
-	sscanf(MemInfoBuf, "%*[^\n]\n"
-		   "%*s %ld %ld %ld %*d %ld %ld\n"
-		   "%*s %ld %ld %ld\n", 
-		   &Total, &Used, &MFree, &Buffers, &Cached,
-		   &STotal, &SUsed, &SFree);
-
-	Appl = (Used - (Buffers + Cached)) / 1024;
-	Total /= 1024;
-	MFree /= 1024;
-	Used /= 1024;
-	Buffers /= 1024;
-	Cached /= 1024;
-	STotal /= 1024;
-	SFree /= 1024;
-	SUsed /= 1024;
+	scan_one(MemInfoBuf, "MemTotal", &Total);
+	scan_one(MemInfoBuf, "MemFree", &MFree);
+	Used = Total - MFree;
+	scan_one(MemInfoBuf, "Buffers", &Buffers);
+	scan_one(MemInfoBuf, "Cached", &Cached);
+	scan_one(MemInfoBuf, "SwapTotal", &STotal);
+	scan_one(MemInfoBuf, "SwapFree", &SFree);
+        SUsed = STotal - SFree;
 
 	Dirty = 0;
 }
 
 /*
@@ -103,21 +106,32 @@ updateMemory(void)
 {
 	/*
 	 * The amount of total and used memory is read from the /proc/meminfo.
 	 * It also contains the information about the swap space.
 	 * The 'file' looks like this:
-	 *
-	 *         total:    used:    free:  shared: buffers:  cached:
-	 * Mem:  64593920 60219392  4374528 49426432  6213632 33689600
-	 * Swap: 69636096   761856 68874240
-	 * MemTotal:     63080 kB
-	 * MeMFree:       4272 kB
-	 * MemShared:    48268 kB
-	 * Buffers:       6068 kB
-	 * Cached:       32900 kB
-	 * SwapTotal:    68004 kB
-	 * SwapFree:     67260 kB
+
+	 * MemTotal:       516560 kB
+	 * MemFree:          7812 kB
+	 * MemShared:           0 kB
+	 * Buffers:         80312 kB
+	 * Cached:         236432 kB
+	 * SwapCached:        468 kB
+	 * Active:         291992 kB
+	 * Inactive:       133556 kB
+	 * HighTotal:           0 kB
+	 * HighFree:            0 kB
+	 * LowTotal:       516560 kB
+	 * LowFree:          7812 kB
+	 * SwapTotal:      899632 kB
+	 * SwapFree:       898932 kB
+	 * Dirty:            2736 kB
+	 * Writeback:           0 kB
+	 * Mapped:         155996 kB
+	 * Slab:            73920 kB
+	 * Committed_AS:   315588 kB
+	 * PageTables:       1764 kB
+	 * ReverseMaps:    103458
 	 */
 
 	int fd;
 	size_t n;
 
@@ -144,113 +158,132 @@ updateMemory(void)
 }
 
 void
 printMFree(const char* cmd)
 {
+	(void)cmd;
+
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "%ld\n", MFree);
 }
 
 void
 printMFreeInfo(const char* cmd)
 {
+	(void)cmd;
+
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "Free Memory\t0\t%ld\tKB\n", Total);
 }
 
 void
 printUsed(const char* cmd)
 {
+	(void)cmd;
+
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "%ld\n", Used);
 }
 
 void
 printUsedInfo(const char* cmd)
 {
+	(void)cmd;
+
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "Used Memory\t0\t%ld\tKB\n", Total);
 }
 
 void
 printAppl(const char* cmd)
 {
+	(void) cmd;
+
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "%ld\n", Appl);
 }
 
 void
 printApplInfo(const char* cmd)
 {
+	(void) cmd;
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "Application Memory\t0\t%ld\tKB\n", Total);
 }
 
 void
 printBuffers(const char* cmd)
 {
+	(void) cmd;
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "%ld\n", Buffers);
 }
 
 void
 printBuffersInfo(const char* cmd)
 {
+	(void) cmd;
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "Buffer Memory\t0\t%ld\tKB\n", Total);
 }
 
 void
 printCached(const char* cmd)
 {
+	(void) cmd;
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "%ld\n", Cached);
 }
 
 void
 printCachedInfo(const char* cmd)
 {
+	(void) cmd;
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "Cached Memory\t0\t%ld\tKB\n", Total);
 }
 
 void
 printSwapUsed(const char* cmd)
 {
+	(void) cmd;
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "%ld\n", SUsed);
 }
 
 void
 printSwapUsedInfo(const char* cmd)
 {
+	(void) cmd;
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "Used Swap Memory\t0\t%ld\tKB\n", STotal);
 }
 
 void
 printSwapFree(const char* cmd)
 {
+	(void) cmd;
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "%ld\n", SFree);
 }
 
 void
 printSwapFreeInfo(const char* cmd)
 {
+	(void) cmd;
 	if (Dirty)
 		processMemInfo();
 	fprintf(CurrentClient, "Free Swap Memory\t0\t%ld\tKB\n", STotal);
 }
Index: ksysguardd/Linux/stat.c
===================================================================
RCS file: /home/kde/kdebase/ksysguard/ksysguardd/Linux/stat.c,v
retrieving revision 1.23
diff -u -5 -d -p -r1.23 stat.c
--- ksysguardd/Linux/stat.c	2002/05/29 13:38:26	1.23
+++ ksysguardd/Linux/stat.c	2002/11/03 04:33:38
@@ -77,10 +77,11 @@ typedef struct DiskIOInfo
 	struct DiskIOInfo* next;
 } DiskIOInfo;
 
 #define STATBUFSIZE 4096
 static char StatBuf[STATBUFSIZE];
+static char VmStatBuf[STATBUFSIZE];
 static int Dirty = 0;
 
 /* We have observed deviations of up to 5% in the accuracy of the timer
  * interrupts. So we try to measure the interrupt interval and use this
  * value to calculate timing dependant values. */
@@ -354,10 +355,11 @@ processStat(void)
 	char format[32];
 	char tagFormat[16];
 	char buf[1024];
 	char tag[32];
 	char* statBufP = StatBuf;
+        char* vmstatBufP = VmStatBuf;
 
 	sprintf(format, "%%%d[^\n]\n", (int) sizeof(buf) - 1);
 	sprintf(tagFormat, "%%%ds", (int) sizeof(tag) - 1);
 
 	while (sscanf(statBufP, format, buf) == 1)
@@ -424,10 +426,33 @@ processStat(void)
 			Ctxt = val - OldCtxt;
 			OldCtxt = val;
 		}
 	}
 
+        /* Read Linux 2.5.x /proc/vmstat */
+	while (sscanf(vmstatBufP, format, buf) == 1)
+	{
+		buf[sizeof(buf) - 1] = '\0';
+		vmstatBufP += strlen(buf) + 1;	/* move vmstatBufP to next line */
+		sscanf(buf, tagFormat, tag);
+
+		if (strcmp("pgpgin", tag) == 0)
+		{
+			unsigned long v1;
+			sscanf(buf + 7, "%lu", &v1);
+			PageIn = v1 - OldPageIn;
+			OldPageIn = v1;
+                }
+		else if (strcmp("pgpgout", tag) == 0)
+		{
+	                unsigned long v1;
+                        sscanf(buf + 7, "%lu", &v1);
+			PageOut = v1 - OldPageOut;
+			OldPageOut = v1;
+		}
+	}
+
 	/* save exact time inverval between this and the last read of /proc/stat */
 	timeInterval = currSampling.tv_sec - lastSampling.tv_sec +
 		(currSampling.tv_usec - lastSampling.tv_usec) / 1000000.0;
 	lastSampling = currSampling;
 
@@ -474,10 +499,11 @@ initStat(struct SensorModul* sm)
 	char format[32];
 	char tagFormat[16];
 	char buf[1024];
 	char tag[32];
 	char* statBufP = StatBuf;
+	char* vmstatBufP = VmStatBuf;
 
 	StatSM = sm;
 
 	updateStat();
 
@@ -600,10 +626,31 @@ initStat(struct SensorModul* sm)
 			sscanf(buf + 5, "%lu", &OldCtxt);
 			registerMonitor("cpu/context", "integer", printCtxt,
 							printCtxtInfo, StatSM);
 		}
 	}
+
+	while (sscanf(vmstatBufP, format, buf) == 1)
+	{
+		buf[sizeof(buf) - 1] = '\0';
+		vmstatBufP += strlen(buf) + 1;	/* move vmstatBufP to next line */
+		sscanf(buf, tagFormat, tag);
+
+		if (strcmp("pgpgin", tag) == 0)
+		{
+			sscanf(buf + 7, "%lu", &OldPageIn);
+			registerMonitor("cpu/pageIn", "integer", printPageIn,
+							printPageInInfo, StatSM);
+		}
+		else if (strcmp("pgpgout", tag) == 0) 
+		{
+			sscanf(buf + 7, "%lu", &OldPageOut);
+			registerMonitor("cpu/pageOut", "integer", printPageOut,
+							printPageOutInfo, StatSM);
+		}
+	}
+	
 	if (CPUCount > 0)
 		SMPLoad = (CPULoadInfo*) malloc(sizeof(CPULoadInfo) * CPUCount);
 
 	/* Call processStat to eliminate initial peek values. */
 	processStat();
@@ -645,77 +692,98 @@ updateStat(void)
 		return (-1);
 	}
 	gettimeofday(&currSampling, 0);
 	close(fd);
 	StatBuf[n] = '\0';
-
 	Dirty = 1;
 
-	return (0);
+        VmStatBuf[0] = '\0';
+        if ((fd = open("/proc/vmstat", O_RDONLY)) < 0)
+		return 0; /* failure is okay, only exists for Linux >= 2.5.x */
+           
+        if ((n = read(fd, VmStatBuf, STATBUFSIZE - 1)) == STATBUFSIZE - 1)
+        {
+                log_error("Internal buffer too small to read \'/proc/vmstat\'");
+                close(fd);
+                return (-1);
+        }
+        close(fd);
+	VmStatBuf[n] = '\0';
+
+	return 0;
 }
 
 void
 printCPUUser(const char* cmd)
 {
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	fprintf(CurrentClient, "%d\n", CPULoad.userLoad);
 }
 
 void 
 printCPUUserInfo(const char* cmd)
 {
+	(void)cmd;
 	fprintf(CurrentClient, "CPU User Load\t0\t100\t%%\n");
 }
 
 void
 printCPUNice(const char* cmd)
 {
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	fprintf(CurrentClient, "%d\n", CPULoad.niceLoad);
 }
 
 void 
 printCPUNiceInfo(const char* cmd)
 {
+	(void)cmd;
 	fprintf(CurrentClient, "CPU Nice Load\t0\t100\t%%\n");
 }
 
 void
 printCPUSys(const char* cmd)
 {
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	fprintf(CurrentClient, "%d\n", CPULoad.sysLoad);
 }
 
 void 
 printCPUSysInfo(const char* cmd)
 {
+	(void)cmd;
 	fprintf(CurrentClient, "CPU System Load\t0\t100\t%%\n");
 }
 
 void
 printCPUIdle(const char* cmd)
 {
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	fprintf(CurrentClient, "%d\n", CPULoad.idleLoad);
 }
 
 void 
 printCPUIdleInfo(const char* cmd)
 {
+	(void)cmd;
 	fprintf(CurrentClient, "CPU Idle Load\t0\t100\t%%\n");
 }
 
 void
 printCPUxUser(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	sscanf(cmd + 3, "%d", &id);
 	fprintf(CurrentClient, "%d\n", SMPLoad[id].userLoad);
 }
@@ -723,19 +791,21 @@ printCPUxUser(const char* cmd)
 void 
 printCPUxUserInfo(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	sscanf(cmd + 3, "%d", &id);
 	fprintf(CurrentClient, "CPU%d User Load\t0\t100\t%%\n", id);
 }
 
 void
 printCPUxNice(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	sscanf(cmd + 3, "%d", &id);
 	fprintf(CurrentClient, "%d\n", SMPLoad[id].niceLoad);
 }
@@ -743,19 +813,21 @@ printCPUxNice(const char* cmd)
 void 
 printCPUxNiceInfo(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	sscanf(cmd + 3, "%d", &id);
 	fprintf(CurrentClient, "CPU%d Nice Load\t0\t100\t%%\n", id);
 }
 
 void
 printCPUxSys(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	sscanf(cmd + 3, "%d", &id);
 	fprintf(CurrentClient, "%d\n", SMPLoad[id].sysLoad);
 }
@@ -763,19 +835,21 @@ printCPUxSys(const char* cmd)
 void 
 printCPUxSysInfo(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	sscanf(cmd + 3, "%d", &id);
 	fprintf(CurrentClient, "CPU%d System Load\t0\t100\t%%\n", id);
 }
 
 void
 printCPUxIdle(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	sscanf(cmd + 3, "%d", &id);
 	fprintf(CurrentClient, "%d\n", SMPLoad[id].idleLoad);
 }
@@ -783,19 +857,21 @@ printCPUxIdle(const char* cmd)
 void 
 printCPUxIdleInfo(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	sscanf(cmd + 3, "%d", &id);
 	fprintf(CurrentClient, "CPU%d Idle Load\t0\t100\t%%\n", id);
 }
 
 void
 printDiskTotal(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	sscanf(cmd + 9, "%d", &id);
 	fprintf(CurrentClient, "%lu\n", (unsigned long) (DiskLoad[id].s[0].delta / timeInterval));
 }
@@ -803,19 +879,21 @@ printDiskTotal(const char* cmd)
 void
 printDiskTotalInfo(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	sscanf(cmd + 9, "%d", &id);
 	fprintf(CurrentClient, "Disk%d Total Load\t0\t0\tkBytes/s\n", id);
 }
 
 void
 printDiskRIO(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	sscanf(cmd + 9, "%d", &id);
 	fprintf(CurrentClient, "%lu\n", (unsigned long ) (DiskLoad[id].s[1].delta / timeInterval));
 }
@@ -823,19 +901,21 @@ printDiskRIO(const char* cmd)
 void
 printDiskRIOInfo(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	sscanf(cmd + 9, "%d", &id);
 	fprintf(CurrentClient, "Disk%d Read\t0\t0\tkBytes/s\n", id);
 }
 
 void
 printDiskWIO(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	sscanf(cmd + 9, "%d", &id);
 	fprintf(CurrentClient, "%lu\n", (unsigned long) (DiskLoad[id].s[2].delta / timeInterval));
 }
@@ -843,19 +923,21 @@ printDiskWIO(const char* cmd)
 void
 printDiskWIOInfo(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	sscanf(cmd + 9, "%d", &id);
 	fprintf(CurrentClient, "Disk%d Write\t0\t0\tkBytes/s\n", id);
 }
 
 void
 printDiskRBlk(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	sscanf(cmd + 9, "%d", &id);
 	/* a block is 512 bytes or 1/2 kBytes */
 	fprintf(CurrentClient, "%lu\n", (unsigned long)
@@ -865,19 +947,21 @@ printDiskRBlk(const char* cmd)
 void
 printDiskRBlkInfo(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	sscanf(cmd + 9, "%d", &id);
 	fprintf(CurrentClient, "Disk%d Read Data\t0\t0\tkBytes/s\n", id);
 }
 
 void
 printDiskWBlk(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	sscanf(cmd + 9, "%d", &id);
 	/* a block is 512 bytes or 1/2 kBytes */
 	fprintf(CurrentClient, "%lu\n", (unsigned long)
@@ -887,47 +971,53 @@ printDiskWBlk(const char* cmd)
 void
 printDiskWBlkInfo(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	sscanf(cmd + 9, "%d", &id);
 	fprintf(CurrentClient, "Disk%d Write Data\t0\t0\tkBytes/s\n", id);
 }
 
 void
 printPageIn(const char* cmd)
 {
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	fprintf(CurrentClient, "%lu\n", (unsigned long) (PageIn / timeInterval));
 }
 
 void
 printPageInInfo(const char* cmd)
 {
+	(void)cmd;
 	fprintf(CurrentClient, "Paged in Pages\t0\t0\t1/s\n");
 }
 
 void
 printPageOut(const char* cmd)
 {
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	fprintf(CurrentClient, "%lu\n", (unsigned long) (PageOut / timeInterval));
 }
 
 void
 printPageOutInfo(const char* cmd)
 {
+	(void)cmd;
 	fprintf(CurrentClient, "Paged out Pages\t0\t0\t1/s\n");
 }
 
 void
 printInterruptx(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	sscanf(cmd + strlen("cpu/interrupts/int"), "%d", &id);
 	fprintf(CurrentClient, "%lu\n", (unsigned long) (Intr[id] / timeInterval));
 }
@@ -935,25 +1025,28 @@ printInterruptx(const char* cmd)
 void
 printInterruptxInfo(const char* cmd)
 {
 	int id;
 
+	(void)cmd;
 	sscanf(cmd + strlen("cpu/interrupt/int"), "%d", &id);
 	fprintf(CurrentClient, "Interrupt %d\t0\t0\t1/s\n", id);
 }
 
 void
 printCtxt(const char* cmd)
 {
+	(void)cmd;
 	if (Dirty)
 		processStat();
 	fprintf(CurrentClient, "%lu\n", (unsigned long) (Ctxt / timeInterval));
 }
 
 void
 printCtxtInfo(const char* cmd)
 {
+	(void)cmd;
 	fprintf(CurrentClient, "Context switches\t0\t0\t1/s\n");
 }
 
 void
 printDiskIO(const char* cmd)


More information about the kde-core-devel mailing list