[Xfce4-commits] <xfce4-cpugraph-plugin:master> Clean up the OS dependent code

Florian noreply at xfce.org
Sat Apr 17 16:16:36 CEST 2010


Updating branch refs/heads/master
         to 40d3a89b75febda48e91af6c8c4a4ef1ba8a10e0 (commit)
       from 85a30ad7d54438ada913826f5cd6bde6f721420a (commit)

commit 40d3a89b75febda48e91af6c8c4a4ef1ba8a10e0
Author: Florian <frivoal at gmail.com>
Date:   Thu Mar 11 00:35:50 2010 +0900

    Clean up the OS dependent code
    
    Make some legiility and code corectness improvements in the os dependant
    code, and depening areas.

 panel-plugin/cpu.c  |   29 +++++++--
 panel-plugin/cpu.h  |    4 +-
 panel-plugin/mode.c |   10 ++--
 panel-plugin/os.c   |  168 +++++++++++++++++++++------------------------------
 panel-plugin/os.h   |   14 ++---
 5 files changed, 102 insertions(+), 123 deletions(-)

diff --git a/panel-plugin/cpu.c b/panel-plugin/cpu.c
index 3c951c4..f493754 100644
--- a/panel-plugin/cpu.c
+++ b/panel-plugin/cpu.c
@@ -5,6 +5,7 @@
 
 static void cpugraph_construct( XfcePanelPlugin *plugin );
 static CPUGraph *create_gui( XfcePanelPlugin *plugin );
+static guint init_cpu_data( CpuData **data );
 static void shutdown( XfcePanelPlugin *plugin, CPUGraph *base );
 static gboolean size_cb( XfcePanelPlugin *plugin, int size, CPUGraph *base );
 static void orientation_cb( XfcePanelPlugin *plugin, GtkOrientation orientation, CPUGraph *base );
@@ -41,7 +42,7 @@ static CPUGraph * create_gui( XfcePanelPlugin * plugin )
 	CPUGraph *base = g_new0( CPUGraph, 1 );
 
 	orientation = xfce_panel_plugin_get_orientation(plugin);
-	if((base->nr_cores = init_cpu_data() - 1) < 0)
+	if( (base->nr_cores = init_cpu_data( &base->cpu_data )) == 0)
 		fprintf(stderr,"Cannot init cpu data !\n");
 
 	base->plugin = plugin;
@@ -75,10 +76,23 @@ static CPUGraph * create_gui( XfcePanelPlugin * plugin )
 	return base;
 }
 
+guint init_cpu_data( CpuData **data )
+{
+	guint cpuNr;
+
+	cpuNr = detect_cpu_number();
+	if( cpuNr == 0 )
+		return 0;
+
+	*data = (CpuData *) g_malloc0( cpuNr * sizeof( CpuData ) );
+
+	return cpuNr;
+}
+
 static void shutdown( XfcePanelPlugin * plugin, CPUGraph * base )
 {
 	gint i;
-	free_cpu_data();
+	g_free( base->cpu_data );
 	base->cpu_data = NULL;
 
 	for(i=0; i<base->nr_cores-1; i++)
@@ -125,9 +139,9 @@ static gboolean size_cb( XfcePanelPlugin *plugin, int size, CPUGraph *base )
 	for( i=0; i<base->nr_cores; i++ )
 		gtk_widget_set_size_request( GTK_WIDGET(base->m_pBar[i]), bar_h, bar_v );
 
-	base->history = (long *) g_realloc( base->history, history * sizeof( long ) );
+	base->history = (int *) g_realloc( base->history, history * sizeof( int ) );
 	if( history > base->history_size )
-		memset( base->history + base->history_size, 0, (history - base->history_size) * sizeof( long ) );
+		memset( base->history + base->history_size, 0, (history - base->history_size) * sizeof( int ) );
 	base->history_size = history;
 
 	return TRUE;
@@ -156,7 +170,8 @@ static void orientation_cb( XfcePanelPlugin * plugin, GtkOrientation orientation
 static gboolean update_cb( CPUGraph * base )
 {
 	gint i;
-	base->cpu_data = read_cpu_data();
+	if( !read_cpu_data( base->cpu_data, base->nr_cores ) )
+		return TRUE;
 	for( i=0; i<base->nr_cores; i++ )
 	{
 		gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR(base->m_pBar[i]), (gdouble)base->cpu_data[i+1].load / CPU_SCALE );
@@ -176,9 +191,9 @@ static gboolean update_cb( CPUGraph * base )
 			base->history[i--] = (a * (factor-1) + b) / factor;
 		}
 	} else {
-		memmove( base->history + 1 , base->history , (base->history_size - 1) * sizeof( long ) );
+		memmove( base->history + 1 , base->history , (base->history_size - 1) * sizeof( int ) );
 	}
-	base->history[0] = (long)base->cpu_data[0].load;
+	base->history[0] = base->cpu_data[0].load;
 
 	/* Tooltip */
 	update_tooltip( base );
diff --git a/panel-plugin/cpu.h b/panel-plugin/cpu.h
index 7edf78d..a7c8986 100644
--- a/panel-plugin/cpu.h
+++ b/panel-plugin/cpu.h
@@ -16,8 +16,6 @@
 #include <libxfce4panel/xfce-panel-plugin.h>
 #include <libxfce4panel/xfce-hvbox.h>
 
-
-
 #include "os.h"
 
 #define BORDER  8
@@ -46,7 +44,7 @@ typedef struct
 	/* Runtime data */
 	guint nr_cores;
 	guint timeout_id;
-	long *history;
+	int *history;
 	gssize history_size;
 	int orientation;
 	CpuData *cpu_data;
diff --git a/panel-plugin/mode.c b/panel-plugin/mode.c
index 67edc2a..4d83634 100644
--- a/panel-plugin/mode.c
+++ b/panel-plugin/mode.c
@@ -40,7 +40,7 @@ static void mix_colors( double ratio, GdkColor *color1, GdkColor *color2, GdkGC
 void draw_graph_normal( CPUGraph *base, GtkWidget *da, int w, int h )
 {
 	int x, y;
-	long usage;
+	int usage;
 	double t;
 	int tmp;
 	GdkGC *fg1 = gdk_gc_new( da->window );
@@ -108,7 +108,7 @@ void draw_graph_LED( CPUGraph *base, GtkWidget *da, int w, int h )
 void draw_graph_no_history( CPUGraph *base, GtkWidget *da, int w, int h )
 {
 	int y;
-	long usage = h * base->history[0] / CPU_SCALE;
+	int usage = h * base->history[0] / CPU_SCALE;
 	int tmp = 0;
 	double t;
 	GdkGC *fg1 = gdk_gc_new( da->window );
@@ -135,14 +135,14 @@ void draw_graph_no_history( CPUGraph *base, GtkWidget *da, int w, int h )
 
 typedef struct
 {
-	long x;
-	long y;
+	int x;
+	int y;
 } point;
 
 void draw_graph_grid( CPUGraph *base, GtkWidget *da, int w, int h )
 {
 	int x, y;
-	long usage;
+	int usage;
 	point last, current;
 	last.x = 0;
 	last.y = h;
diff --git a/panel-plugin/os.c b/panel-plugin/os.c
index 69894b6..6dc6088 100644
--- a/panel-plugin/os.c
+++ b/panel-plugin/os.c
@@ -44,121 +44,89 @@
 #include <nlist.h>
 #endif
 
-CpuData *cpudata = NULL;
-int nrCpus = 0;
-
-static int detect_cpu_number();
-
-void free_cpu_data()
-{
-	g_free( cpudata );
-	cpudata = NULL;
-	nrCpus = 0;
-}
-
-int init_cpu_data()
-{
-	int i, cpuNr = -1;
-
-	/* Check if previously initalized */
-	if( cpudata != NULL )
-		return -2;
-
-	cpuNr = detect_cpu_number();
-	if( cpuNr < 1 )
-		return -1;
-
-	/* Alloc storage for cpu data stuff */
-	cpudata = (CpuData *) g_malloc0( cpuNr * sizeof( CpuData ) );
-
-	return nrCpus = cpuNr;
-}
-
 #if defined (__linux__)
-static int detect_cpu_number()
+unsigned int detect_cpu_number()
 {
-	int cpuNr= -1;
+	int nb_lines= 0;
 	FILE *fstat = NULL;
 	char cpuStr[PROCMAXLNLEN];
-	/* Open proc stat file */
+
 	if( !(fstat = fopen( PROC_STAT, "r" )) )
-		return -1;
+		return 0;
 
-	/* Read each cpu line at time */
-	do
+	while( fgets( cpuStr, PROCMAXLNLEN, fstat ) )
 	{
-		if( !fgets( cpuStr, PROCMAXLNLEN, fstat ) )
-			return cpuNr;
-		cpuNr++;
-	} while( strncmp( cpuStr, "cpu", 3 ) == 0 );
+		if( strncmp( cpuStr, "cpu", 3 ) == 0 )
+			nb_lines++;
+		else
+			break;
+	}
+
 	fclose( fstat );
-	return cpuNr;
+
+	return nb_lines > 1 ? nb_lines - 1 : 0;
 }
 
-CpuData *read_cpu_data()
+int read_cpu_data( CpuData *data, unsigned int nb_cpu)
 {
-	FILE *fStat = NULL;
+	FILE *fStat;
 	char cpuStr[PROCMAXLNLEN];
-	unsigned long user, nice, system, idle, used, total;
-	unsigned long iowait=0, irq=0, softirq=0;
-	int cpuNr = 0;
-
-	/* Check if callable */
-	if( (cpudata == NULL) || (nrCpus == 0) )
-		return NULL;
+	unsigned int user, nice, system, idle, used, total, iowait, irq, softirq;
+	unsigned int line;
 
-	/* Open proc stat file */
 	if( !(fStat = fopen( PROC_STAT, "r" )) )
-		return NULL;
+		return FALSE;
 
-	/* Read each cpu line at time */
-	do
+	for( line = 0; line < nb_cpu + 1; line++ )
 	{
-		if( !fgets( cpuStr, PROCMAXLNLEN, fStat ) )
-			return cpudata;
+		if( !fgets( cpuStr, PROCMAXLNLEN, fStat ) ||
+		    strncmp( cpuStr, "cpu", 3 ) != 0
+		  )
+		{
+			fclose( fStat );
+			return FALSE;
+		}
 		if( sscanf( cpuStr, "%*s %ld %ld %ld %ld %ld %ld %ld", &user, &nice, &system, &idle, &iowait, &irq, &softirq ) < 7 )
 			iowait = irq = softirq = 0;
 		used = user + nice + system + irq + softirq;
 		total = used + idle + iowait;
-		if( (total - cpudata[cpuNr].pTotal) != 0 )
+		if( (total - data[line].pTotal) != 0 )
 		{
-			cpudata[cpuNr].load = CPU_SCALE * (float)(used - cpudata[cpuNr].pUsed) /
-			                      (float)(total - cpudata[cpuNr].pTotal);
+			data[line].load = CPU_SCALE * (used - data[line].pUsed) /
+			                      (total - data[line].pTotal);
 		}
 		else
 		{
-			cpudata[cpuNr].load = 0;
+			data[line].load = 0;
 		}
-		cpudata[cpuNr].pUsed = used;
-		cpudata[cpuNr].pTotal = total;
-		cpuNr++;
+		data[line].pUsed = used;
+		data[line].pTotal = total;
 	}
-	while( (cpuNr < nrCpus) && (strncmp( cpuStr, "cpu", 3 ) == 0) );
 
 	fclose( fStat );
 
-	return cpudata;
+	return TRUE;
 }
 
 #elif defined (__FreeBSD__)
-static int detect_cpu_number()
+unsigned int detect_cpu_number()
 {
 	return 1;
 }
 
-CpuData *read_cpu_data()
+int read_cpu_data( CpuData *data, unsigned int nb_cpu)
 {
-	unsigned long user, nice, sys, bsdidle, idle;
-	unsigned long used, total;
-	long cp_time[CPUSTATES];
+	unsigned int user, nice, sys, bsdidle, idle;
+	unsigned int used, total;
+	int cp_time[CPUSTATES];
 	size_t len = sizeof( cp_time );
 
-	long usage;
+	int usage;
 
 	if( sysctlbyname( "kern.cp_time", &cp_time, &len, NULL, 0 ) < 0 )
 	{
 		printf( "Cannot get kern.cp_time.\n" );
-		return -1;
+		return FALSE1;
 	}
 
 	user = cp_time[CP_USER];
@@ -169,27 +137,27 @@ CpuData *read_cpu_data()
 
 	used = user+nice+sys;
 	total = used+bsdidle;
-	if( (total - cpudata[0].pTotal) != 0 )
-		cpudata[0].pTotal = (CPU_SCALE.0 * (used - cpudata[0].pTotal))/(total - cpudata[0].pTotal);
+	if( (total - data[0].pTotal) != 0 )
+		data[0].load = (CPU_SCALE.0 * (used - data[0].pTotal))/(total - data[0].pTotal);
 	else
-		cpudata[0].pTotal = 0;
+		data[0].load = 0;
 
-	cpudata[0].pUsed = used;
-	cpudata[0].pTotal = total;
+	data[0].pUsed = used;
+	data[0].pTotal = total;
 
-	return cpudata;
+	return TRUE;
 }
 
 #elif defined (__NetBSD__)
-static int detect_cpu_number()
+unsigned int detect_cpu_number()
 {
 	return 1;
 }
 
-CpuData *read_cpu_data()
+int read_cpu_data( CpuData *data, unsigned int nb_cpu)
 {
-	long user, nice, sys, bsdidle, idle;
-	long used, total, usage;
+	int user, nice, sys, bsdidle, idle;
+	int used, total;
 	static int mib[] = {CTL_KERN, KERN_CP_TIME };
 	u_int64_t cp_time[CPUSTATES];
 	size_t len = sizeof( cp_time );
@@ -197,7 +165,7 @@ CpuData *read_cpu_data()
 	if( sysctl( mib, 2, &cp_time, &len, NULL, 0 ) < 0 )
 	{
 		printf( "Cannot get kern.cp_time\n" );
-		return -1;
+		return FALSE;
 	}
 
 	user = cp_time[CP_USER];
@@ -209,34 +177,34 @@ CpuData *read_cpu_data()
 	used = user+nice+sys;
 	total = used+bsdidle;
 
-	if( total - cpudata[0].pTotal != 0 )
-		usage = (CPU_SCALE * (double)(used - cpudata[0].pTotal)) / (double)(total - cpudata[0].pTotal);
+	if( total - data[0].pTotal != 0 )
+		data[0].load = (CPU_SCALE * (double)(used - data[0].pTotal)) / (double)(total - data[0].pTotal);
 	else
-		usage = 0;
+		data[0].load = 0;
 
-	cpudata[0].pUsed = used;
-	cpudata[0].pTotal = total;
+	data[0].pUsed = used;
+	data[0].pTotal = total;
 
-	return cpudata;
+	return TRUE;
 }
 
 #elif defined (__OpenBSD__)
-static int detect_cpu_number()
+unsigned int detect_cpu_number()
 {
 	return 1;
 }
 
-CpuData *read_cpu_data()
+int read_cpu_data( CpuData *data, unsigned int nb_cpu)
 {
-	unsigned long user, nice, sys, bsdidle, idle;
-	unsigned long used, total, usage;
+	unsigned int user, nice, sys, bsdidle, idle;
+	unsigned int used, total;
 	static int mib[] = {CTL_KERN, KERN_CPTIME };
 	u_int64_t cp_time[CPUSTATES];
 	size_t len = sizeof( cp_time );
 	if( sysctl( mib, 2, &cp_time, &len, NULL, 0) < 0 )
 	{
 		printf( "Cannot get kern.cp_time\n" );
-		return -1;
+		return FALSE;
 	}
 
 	user = cp_time[CP_USER];
@@ -248,14 +216,14 @@ CpuData *read_cpu_data()
 	used = user+nice+sys;
 	total = used+bsdidle;
 
-	if( total - cpudata[0].pTotal != 0 )
-		usage = (CPU_SCALE * (double)(used - cpudata[0].pTotal))/(double)(total - cpudata[0].pTotal);
+	if( total - data[0].pTotal != 0 )
+		data[0].load = (CPU_SCALE (used - data[0].pTotal))/(total - data[0].pTotal);
 	else
-		usage = 0;
-	cpudata[0].pUsed = used;
-	cpudata[0].pTotal = total;
+		data[0].load = 0;
+	data[0].pUsed = used;
+	data[0].pTotal = total;
 
-	return cpudata;
+	return TRUE;
 }
 #else
 #error "Your OS is not supported."
diff --git a/panel-plugin/os.h b/panel-plugin/os.h
index 3924419..b114ef6 100644
--- a/panel-plugin/os.h
+++ b/panel-plugin/os.h
@@ -1,18 +1,16 @@
 #ifndef _XFCE_OS_H_
 #define _XFCE_OS_H_
 
-#define CPU_SCALE 100000
+#define CPU_SCALE 256
 
 typedef struct
 {
-	float load; /* cpu utilization */
-	unsigned long pUsed; /* Previous value of used cpu time */
-	unsigned long pTotal; /* Previous value of total cpu time */
+	unsigned int load; /* cpu utilization */
+	unsigned int pUsed; /* Previous value of used cpu time */
+	unsigned int pTotal; /* Previous value of total cpu time */
 } CpuData;
 
-
-int init_cpu_data();
-void free_cpu_data();
-CpuData *read_cpu_data();
+unsigned int detect_cpu_number();
+int read_cpu_data( CpuData *data, unsigned int nb_cpu );
 
 #endif /* !_XFCE_OS_H */



More information about the Xfce4-commits mailing list