[Xfce4-commits] <xfce4-cpugraph-plugin:master> Fix support for OpenBSD

Florian Rivoal noreply at xfce.org
Thu Apr 29 11:32:01 CEST 2010


Updating branch refs/heads/master
         to d5077564dbe61e86ef7a0504daa4a41ddb6a3a1c (commit)
       from af1261b66a362c04c0678b0156578957e037f892 (commit)

commit d5077564dbe61e86ef7a0504daa4a41ddb6a3a1c
Author: Florian Rivoal <frivoal at gmail.com>
Date:   Thu Apr 29 13:07:12 2010 +0900

    Fix support for OpenBSD
    
    Fix the code that reads the cpu state for OpenBSD, and include support
    for multiple CPUs. Patch by Landry Breuil.

 panel-plugin/os.c |   52 ++++++++++++++++++++++++++--------------------------
 panel-plugin/os.h |    4 ++--
 2 files changed, 28 insertions(+), 28 deletions(-)

diff --git a/panel-plugin/os.c b/panel-plugin/os.c
index c58e44a..1459ec9 100644
--- a/panel-plugin/os.c
+++ b/panel-plugin/os.c
@@ -38,9 +38,6 @@
 #include <sys/param.h>
 #include <sys/sched.h>
 #include <sys/sysctl.h>
-#include <sys/dkstat.h>
-#include <fcntl.h>
-#include <nlist.h>
 #endif
 
 #if defined (__linux__) || defined (__FreeBSD_kernel__)
@@ -188,37 +185,40 @@ gboolean read_cpu_data( CpuData *data, guint nb_cpu)
 #elif defined (__OpenBSD__)
 guint detect_cpu_number()
 {
-	return 1;
+	static gint mib[] = {CTL_HW, HW_NCPU};
+	gint ncpu;
+	gsize len = sizeof( gint );
+	if( sysctl( mib, 2, &ncpu, &len, NULL, 0 ) < 0 )
+		return 0;
+	else
+		return ncpu;
 }
 
 gboolean read_cpu_data( CpuData *data, guint nb_cpu)
 {
-	guint user, nice, sys, bsdidle, idle;
-	guint used, total;
-	static gint mib[] = {CTL_KERN, KERN_CPTIME };
+	guint64 used, total;
 	guint64 cp_time[CPUSTATES];
-	gsize len = sizeof( cp_time );
-	if( sysctl( mib, 2, &cp_time, &len, NULL, 0) < 0 )
+	gint i;
+	data[0].load = 0;
+	for( i = 1 ; i <= nb_cpu ; i++ )
 	{
-		return FALSE;
-	}
-
-	user = cp_time[CP_USER];
-	nice = cp_time[CP_NICE];
-	sys = cp_time[CP_SYS];
-	bsdidle = cp_time[CP_INTR];
-	idle = cp_time[CP_IDLE];
-
-	used = user+nice+sys;
-	total = used+bsdidle;
+		gsize len = CPUSTATES * sizeof( guint64 );
+		gint mib[] = {CTL_KERN, KERN_CPTIME2, i - 1};
+		if( sysctl( mib, 3, &cp_time, &len, NULL, 0 ) < 0 )
+			return FALSE;
 
-	if( total - data[0].previous_total != 0 )
-		data[0].load = (CPU_SCALE * (used - data[0].previous_total))/(total - data[0].previous_total);
-	else
-		data[0].load = 0;
-	data[0].previous_used = used;
-	data[0].previous_total = total;
+		used = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS] + cp_time[CP_INTR];
+		total = used + cp_time[CP_IDLE];
 
+		if( total - data[i].previous_total != 0 )
+			data[i].load = (CPU_SCALE * (used - data[i].previous_used))/(total - data[i].previous_total);
+		else
+			data[i].load = 0;
+		data[i].previous_used = used;
+		data[i].previous_total = total;
+		data[0].load += data[i].load;
+	}
+	data[0].load /= nb_cpu;
 	return TRUE;
 }
 #else
diff --git a/panel-plugin/os.h b/panel-plugin/os.h
index 726ec01..18d54c1 100644
--- a/panel-plugin/os.h
+++ b/panel-plugin/os.h
@@ -8,8 +8,8 @@
 typedef struct
 {
 	guint load;
-	guint previous_used;
-	guint previous_total;
+	guint64 previous_used;
+	guint64 previous_total;
 } CpuData;
 
 guint detect_cpu_number();



More information about the Xfce4-commits mailing list