gnome-power-manager r2810 - in trunk: . src



Author: simonz
Date: Thu May 29 06:10:17 2008
New Revision: 2810
URL: http://svn.gnome.org/viewvc/gnome-power-manager?rev=2810&view=rev

Log:
2008-05-29  simon.zheng  <simon zheng sun com>

	* configure.ac:
	* src/Makefile.am:
	* src/gpm-load.c: (gpm_load_get_cpu_values): 
	Fix #534335, make cpu load calculation work on Solaris.



Modified:
   trunk/ChangeLog
   trunk/configure.ac
   trunk/src/Makefile.am
   trunk/src/gpm-load.c

Modified: trunk/configure.ac
==============================================================================
--- trunk/configure.ac	(original)
+++ trunk/configure.ac	Thu May 29 06:10:17 2008
@@ -480,6 +480,14 @@
   exit 1
 fi
 
+dnl ---------------------------------------------------------------------------
+dnl - Check for Solaris kstat support
+dnl ---------------------------------------------------------------------------
+AC_MSG_CHECKING(for Solaris kstat)
+AC_CHECK_LIB(kstat, kstat_open,
+             [AC_DEFINE(HAVE_KSTAT, 1, [Define if Solaris kstat is support])
+             GPM_EXTRA_LIBS="$GPM_EXTRA_LIBS -lkstat"])
+
 AC_SUBST(GPM_EXTRA_LIBS)
 
 dnl ---------------------------------------------------------------------------

Modified: trunk/src/Makefile.am
==============================================================================
--- trunk/src/Makefile.am	(original)
+++ trunk/src/Makefile.am	Thu May 29 06:10:17 2008
@@ -302,6 +302,7 @@
 	$(LIBNOTIFY_LIBS)				\
 	$(LOCAL_LIBDBUS_LIBS)				\
 	$(LOCAL_LIBHAL_LIBS)				\
+	$(GPM_EXTRA_LIBS)				\
 	$(NULL)
 
 gnome_power_self_test_CPPFLAGS=	\

Modified: trunk/src/gpm-load.c
==============================================================================
--- trunk/src/gpm-load.c	(original)
+++ trunk/src/gpm-load.c	Thu May 29 06:10:17 2008
@@ -29,6 +29,10 @@
 #include <string.h>
 #include <sys/time.h>
 #include <sys/types.h>
+#if defined(sun) && defined(__SVR4)
+#include <kstat.h>
+#include <sys/sysinfo.h>
+#endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif /* HAVE_UNISTD_H */
@@ -75,6 +79,88 @@
 	g_type_class_add_private (klass, sizeof (GpmLoadPrivate));
 }
 
+#if defined(sun) && defined(__SVR4)
+
+/**
+ * gpm_load_get_cpu_values:
+ * @cpu_idle: The idle time reported by the CPU
+ * @cpu_total: The total time reported by the CPU
+ * Return value: Success of reading /proc/stat.
+ **/
+static gboolean
+gpm_load_get_cpu_values (long unsigned *cpu_idle, long unsigned *cpu_total)
+{	
+	long unsigned cpu_user = 0;
+	long unsigned cpu_kernel = 0;
+	long unsigned cpu_wait = 0;
+	kstat_ctl_t *kc = NULL;
+	kstat_named_t *kn = NULL;
+	kstat_t     *ks = NULL;
+	cpu_stat_t  data;
+	int ncpus;
+	int count;
+	   	
+	kc = kstat_open();
+	if (!kc){
+		gpm_warning ("Cannot open kstat!\n");
+		return FALSE;
+	}
+
+	ks = kstat_lookup(kc, "unix", 0, "system_misc");
+  	if (kstat_read(kc, ks, NULL) == -1) {
+		gpm_warning ("Cannot read kstat on module unix!\n");
+		goto out;
+	}
+	kn = kstat_data_lookup (ks, "ncpus");
+	if (!kn) {
+		gpm_warning ("Cannot get number of cpus in current system!\n");
+		goto out;
+	}
+	ncpus = kn->value.ui32;
+
+	/* 
+ 	 * To aggresive ticks used of all cpus,
+	 * traverse kstat chain to access very cpu_stat instane.
+	 */
+	for(count = 0, *cpu_idle =0, *cpu_total = 0; count < ncpus; count++){
+		
+		ks = kstat_lookup(kc, "cpu_stat", count, NULL);
+		if (ks == NULL) {
+			gpm_warning ("Null output for kstat on cpu%d\n", count);
+			goto out;
+		}
+     
+		if (kstat_read(kc, ks, &data) == -1) {
+			gpm_warning ("Cannot read kstat entry on cpu%d\n", count);
+			goto out;
+		}
+
+		gpm_debug ("cpu%d:\t%lu\t%lu\t%lu\t%lu\n", count,
+					data.cpu_sysinfo.cpu[CPU_IDLE],
+					data.cpu_sysinfo.cpu[CPU_USER],
+					data.cpu_sysinfo.cpu[CPU_KERNEL],
+					data.cpu_sysinfo.cpu[CPU_WAIT]);
+
+		*cpu_idle += data.cpu_sysinfo.cpu[CPU_IDLE];
+		cpu_user += data.cpu_sysinfo.cpu[CPU_USER];
+		cpu_kernel += data.cpu_sysinfo.cpu[CPU_KERNEL];
+		cpu_wait += data.cpu_sysinfo.cpu[CPU_WAIT];
+	}
+	kstat_close(kc);
+	/*
+         * Summing up all these times gives you the system uptime.
+         * This is what the uptime command does.
+         */
+        *cpu_total = cpu_user + cpu_kernel + cpu_wait + *cpu_idle;
+        return TRUE;
+
+out:
+	kstat_close(kc);
+	return FALSE;
+}
+
+#else
+
 /**
  * gpm_load_get_cpu_values:
  * @cpu_idle: The idle time reported by the CPU
@@ -108,6 +194,7 @@
 	*cpu_total = cpu_user + cpu_nice + cpu_system + *cpu_idle;
 	return TRUE;
 }
+#endif /* sun & __SVR4 */
 
 /**
  * gpm_load_get_current:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]