[Xfce4-commits] <xfce4-systemload-plugin:master> Improve the precision

Florian Rivoal noreply at xfce.org
Thu Oct 14 17:20:04 CEST 2010


Updating branch refs/heads/master
         to 7bf887b3a7dd106b268cfb90da8a2033f582a769 (commit)
       from ff5c610d382bb0f8607e85a6d0ca75aaf8f497b8 (commit)

commit 7bf887b3a7dd106b268cfb90da8a2033f582a769
Author: Florian Rivoal <frivoal at xfce.org>
Date:   Tue Oct 12 09:42:42 2010 +0900

    Improve the precision
    
    Improve the accuracy of CPU activity measurements by taking into account
    iowait, irq, softirq and guest time on Linux, and CP_INTR time on
    FreeBSD and NetBSD.
    
    This fixes bug 1076.

 panel-plugin/cpu.c |   61 +++++++++++++++++++++++++--------------------------
 1 files changed, 30 insertions(+), 31 deletions(-)

diff --git a/panel-plugin/cpu.c b/panel-plugin/cpu.c
index bc9928d..224d0c1 100644
--- a/panel-plugin/cpu.c
+++ b/panel-plugin/cpu.c
@@ -38,6 +38,9 @@
 #include "cpu.h" 
 
 #if defined(__linux__) || defined(__FreeBSD_kernel__)
+
+#include <stdint.h>
+
 #define PROC_STAT "/proc/stat"
 
 /* user, nice, system, interrupt(BSD specific), idle */
@@ -45,25 +48,40 @@ struct cpu_load_struct {
     gulong load[5];
 };
 
-struct cpu_load_struct fresh = {{0, 0, 0, 0, 0}};
 gulong cpu_used, oldtotal, oldused;
 
 gulong read_cpuload()
 {
     FILE *fd;
+    uint64_t user, nice, system, idle, iowait, irq, softirq, guest;
     gulong used, total;
+    int nb_read;
 
     fd = fopen(PROC_STAT, "r");
     if (!fd) {
         g_warning(_("File /proc/stat not found!"));
         return 0;
     }
-    fscanf(fd, "%*s %ld %ld %ld %ld", &fresh.load[0], &fresh.load[1],
-           &fresh.load[2], &fresh.load[3]);
+
+    /* Don't count steal time. It is neither busy nor free tiime. */
+    nb_read = fscanf (fd, "%*s " "%llu %llu %llu %llu %llu %llu %llu %*llu %llu",
+	    &user, &nice, &system, &idle, &iowait, &irq, &softirq, &guest);
     fclose(fd);
+    switch (nb_read) /* fall through intentional */
+    {
+	    case 4:
+		    iowait = 0;
+	    case 5:
+		    irq = 0;
+	    case 6:
+		    softirq = 0;
+	    case 7:
+		    guest = 0;
+    }
+
+    used = user + nice + system + irq + softirq + guest;
+    total = used + idle + iowait;
 
-    used = fresh.load[0] + fresh.load[1] + fresh.load[2];
-    total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
     if ((total - oldtotal) != 0)
     {
         cpu_used = (100 * (double)(used - oldused)) / (double)(total - oldtotal);
@@ -97,7 +115,6 @@ struct cpu_load_struct {
     gulong load[5];
 };
 
-struct cpu_load_struct fresh = {{0, 0, 0, 0, 0}};
 gulong cpu_used, oldtotal, oldused;
 
 gulong read_cpuload()
@@ -111,14 +128,9 @@ gulong read_cpuload()
         return 0;
     }
 
-    fresh.load[0] = cp_time[CP_USER];
-    fresh.load[1] = cp_time[CP_NICE];
-    fresh.load[2] = cp_time[CP_SYS];
-    fresh.load[3] = cp_time[CP_IDLE];
-    fresh.load[4] = cp_time[CP_IDLE];
+    used = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS] + cp_time[CP_INTR];
+    total = used + cp_time[CP_IDLE];
 
-    used = fresh.load[0] + fresh.load[1] + fresh.load[2];
-    total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
     if ((total - oldtotal) != 0)
     {
         cpu_used = (100 * (double)(used - oldused)) / (double)(total - oldtotal);
@@ -155,7 +167,6 @@ struct cpu_load_struct {
     gulong load[5];
 };
 
-struct cpu_load_struct fresh = {{0, 0, 0, 0, 0}};
 gulong cpu_used, oldtotal, oldused;
 
 gulong read_cpuload()
@@ -170,14 +181,9 @@ gulong read_cpuload()
             return 0;
     }
 
-    fresh.load[0] = cp_time[CP_USER];
-    fresh.load[1] = cp_time[CP_NICE];
-    fresh.load[2] = cp_time[CP_SYS];
-    fresh.load[3] = cp_time[CP_IDLE];
-    fresh.load[4] = cp_time[CP_IDLE];
+    used = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS] + cp_time[CP_INTR];
+    total = used + cp_time[CP_IDLE];
 
-    used = fresh.load[0] + fresh.load[1] + fresh.load[2];
-    total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
     if ((total - oldtotal) != 0)
     {
         cpu_used = (100 * (double)(used - oldused)) / (double)(total - oldtotal);
@@ -215,7 +221,6 @@ struct cpu_load_struct {
     gulong load[5];
 };
 
-struct cpu_load_struct fresh = {{0, 0, 0, 0, 0}};
 gulong cpu_used, oldtotal, oldused;
 
 gulong read_cpuload()
@@ -230,15 +235,9 @@ gulong read_cpuload()
             return 0;
     }
 
-    fresh.load[0] = cp_time[CP_USER];
-    fresh.load[1] = cp_time[CP_NICE];
-    fresh.load[2] = cp_time[CP_SYS];
-    fresh.load[3] = cp_time[CP_INTR];
-    fresh.load[4] = cp_time[CP_IDLE];
+    used = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS] + cp_time[CP_INTR];
+    total = used + cp_time[CP_IDLE];
 
-    used = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
-    total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3] +
-            fresh.load[4];
     if ((total - oldtotal) != 0)
     {
         cpu_used = (100 * (double)(used - oldused)) / (double)(total - oldtotal);
@@ -254,5 +253,5 @@ gulong read_cpuload()
 }
 
 #else
-#error "Your plattform is not yet supported"
+#error "Your platform is not yet supported"
 #endif



More information about the Xfce4-commits mailing list