[Xfce4-commits] <xfce4-taskmanager:master> Add precision to memory/swap and CPU

Mike Massonnet noreply at xfce.org
Wed May 5 09:08:07 CEST 2010


Updating branch refs/heads/master
         to 71d1684696c49be2d73ebe98bd44177a33f8e9f8 (commit)
       from 4f04ca5cb776403629634f69acdbf8ccfcb4ccbe (commit)

commit 71d1684696c49be2d73ebe98bd44177a33f8e9f8
Author: Mike Massonnet <mmassonnet at xfce.org>
Date:   Sun May 2 16:56:33 2010 +0200

    Add precision to memory/swap and CPU
    
    Switched all the gushort against gfloat data types for the memory, swap
    and CPU usages. Show them with a precision of two decimals in the GUI.

 src/main.c               |    2 +-
 src/process-statusbar.c  |   24 ++++++++++++------------
 src/process-window.c     |    2 +-
 src/process-window.h     |    2 +-
 src/task-manager-linux.c |    6 +++---
 src/task-manager.c       |   10 +++++-----
 src/task-manager.h       |    6 +++---
 7 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/src/main.c b/src/main.c
index 3f41dc3..5fac70f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -24,7 +24,7 @@ static gboolean
 timeout_cb ()
 {
 	guint num_processes;
-	gushort cpu, memory, swap;
+	gfloat cpu, memory, swap;
 	xtm_task_manager_get_system_info (task_manager, &num_processes, &cpu, &memory, &swap);
 	xtm_process_window_set_system_info (XTM_PROCESS_WINDOW (window), num_processes, cpu, memory, swap);
 }
diff --git a/src/process-statusbar.c b/src/process-statusbar.c
index 6c14232..48d191b 100644
--- a/src/process-statusbar.c
+++ b/src/process-statusbar.c
@@ -40,9 +40,9 @@ struct _XtmProcessStatusbar
 	GtkWidget *		label_memory;
 	GtkWidget *		label_swap;
 
-	gushort			cpu;
-	gushort			memory;
-	gushort			swap;
+	gfloat			cpu;
+	gfloat			memory;
+	gfloat			swap;
 	guint			num_processes;
 };
 G_DEFINE_TYPE (XtmProcessStatusbar, xtm_process_statusbar, GTK_TYPE_STATUSBAR)
@@ -58,11 +58,11 @@ xtm_process_statusbar_class_init (XtmProcessStatusbarClass *klass)
 	xtm_process_statusbar_parent_class = g_type_class_peek_parent (klass);
 	class->set_property = xtm_process_statusbar_set_property;
 	g_object_class_install_property (class, PROP_CPU,
-		g_param_spec_uint ("cpu", "CPU", "CPU usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
+		g_param_spec_float ("cpu", "CPU", "CPU usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
 	g_object_class_install_property (class, PROP_MEMORY,
-		g_param_spec_uint ("memory", "Memory", "Memory usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
+		g_param_spec_float ("memory", "Memory", "Memory usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
 	g_object_class_install_property (class, PROP_SWAP,
-		g_param_spec_uint ("swap", "Swap", "Swap usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
+		g_param_spec_float ("swap", "Swap", "Swap usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
 	g_object_class_install_property (class, PROP_NUM_PROCESSES,
 		g_param_spec_uint ("num-processes", "NumProcesses", "Number of processes", 0, G_MAXUINT, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
 }
@@ -117,22 +117,22 @@ xtm_process_statusbar_set_property (GObject *object, guint property_id, const GV
 	switch (property_id)
 	{
 		case PROP_CPU:
-		statusbar->cpu = g_value_get_uint (value);
-		text = g_strdup_printf (_("CPU: %d%%"), statusbar->cpu);
+		statusbar->cpu = g_value_get_float (value);
+		text = g_strdup_printf (_("CPU: %.2f%%"), statusbar->cpu);
 		gtk_label_set_text (GTK_LABEL (statusbar->label_cpu), text);
 		g_free (text);
 		break;
 
 		case PROP_MEMORY:
-		statusbar->memory = g_value_get_uint (value);
-		text = g_strdup_printf (_("Memory: %d%%"), statusbar->memory);
+		statusbar->memory = g_value_get_float (value);
+		text = g_strdup_printf (_("Memory: %.2f%%"), statusbar->memory);
 		gtk_label_set_text (GTK_LABEL (statusbar->label_memory), text);
 		g_free (text);
 		break;
 
 		case PROP_SWAP:
-		statusbar->swap = g_value_get_uint (value);
-		text = g_strdup_printf (_("Swap: %d%%"), statusbar->swap);
+		statusbar->swap = g_value_get_float (value);
+		text = g_strdup_printf (_("Swap: %.2f%%"), statusbar->swap);
 		gtk_label_set_text (GTK_LABEL (statusbar->label_swap), text);
 		g_free (text);
 		break;
diff --git a/src/process-window.c b/src/process-window.c
index fec17be..38aa419 100644
--- a/src/process-window.c
+++ b/src/process-window.c
@@ -370,7 +370,7 @@ xtm_process_window_get_model (XtmProcessWindow *window)
 }
 
 void
-xtm_process_window_set_system_info (XtmProcessWindow *window, guint num_processes, gushort cpu, gushort memory, gushort swap)
+xtm_process_window_set_system_info (XtmProcessWindow *window, guint num_processes, gfloat cpu, gfloat memory, gfloat swap)
 {
 	g_return_if_fail (G_LIKELY (XTM_IS_PROCESS_WINDOW (window)));
 	g_return_if_fail (G_LIKELY (GTK_IS_STATUSBAR (window->priv->statusbar)));
diff --git a/src/process-window.h b/src/process-window.h
index 4e3cee2..8dfaeaa 100644
--- a/src/process-window.h
+++ b/src/process-window.h
@@ -29,6 +29,6 @@ typedef struct _XtmProcessWindow XtmProcessWindow;
 GType		xtm_process_window_get_type			(void);
 GtkWidget *	xtm_process_window_new				();
 GtkTreeModel *	xtm_process_window_get_model			(XtmProcessWindow *window);
-void		xtm_process_window_set_system_info		(XtmProcessWindow *window, guint num_processes, gushort cpu, gushort memory, gushort swap);
+void		xtm_process_window_set_system_info		(XtmProcessWindow *window, guint num_processes, gfloat cpu, gfloat memory, gfloat swap);
 
 #endif /* !PROCESS_WINDOW_H */
diff --git a/src/task-manager-linux.c b/src/task-manager-linux.c
index 0ba0356..c207a11 100644
--- a/src/task-manager-linux.c
+++ b/src/task-manager-linux.c
@@ -49,7 +49,7 @@ get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_c
 }
 
 gboolean
-get_cpu_usage (gushort *cpu_count, gushort *cpu_user, gushort *cpu_system)
+get_cpu_usage (gushort *cpu_count, gfloat *cpu_user, gfloat *cpu_system)
 {
 	FILE *file;
 	gchar *filename = "/proc/stat";
@@ -84,8 +84,8 @@ get_cpu_usage (gushort *cpu_count, gushort *cpu_user, gushort *cpu_system)
 	cur_jiffies_system = system;
 	cur_jiffies = cur_jiffies_user + cur_jiffies_system + idle;
 
-	*cpu_user = (old_jiffies > 0) ? (cur_jiffies_user - old_jiffies_user) * 100 / (cur_jiffies - old_jiffies) : 0;
-	*cpu_system = (old_jiffies > 0) ? (cur_jiffies_system - old_jiffies_system) * 100 / (cur_jiffies - old_jiffies) : 0;
+	*cpu_user = (old_jiffies > 0) ? (cur_jiffies_user - old_jiffies_user) * 100 / (gdouble)(cur_jiffies - old_jiffies) : 0;
+	*cpu_system = (old_jiffies > 0) ? (cur_jiffies_system - old_jiffies_system) * 100 / (gdouble)(cur_jiffies - old_jiffies) : 0;
 	*cpu_count = (count != 0) ? count : 1;
 
 	return TRUE;
diff --git a/src/task-manager.c b/src/task-manager.c
index 3df42fb..ca5de7d 100644
--- a/src/task-manager.c
+++ b/src/task-manager.c
@@ -37,8 +37,8 @@ struct _XtmTaskManager
 	gchar *			owner_uid_name;
 	gchar *			hostname;
 	gushort			cpu_count;
-	gushort			cpu_user;
-	gushort			cpu_system;
+	gfloat			cpu_user;
+	gfloat			cpu_system;
 	guint64			memory_total;
 	guint64			memory_free;
 	guint64			memory_cache;
@@ -134,7 +134,7 @@ xtm_task_manager_get_tasklist (XtmTaskManager *manager)
 }
 
 void
-xtm_task_manager_get_system_info (XtmTaskManager *manager, guint *num_processes, gushort *cpu, gushort *memory, gushort *swap)
+xtm_task_manager_get_system_info (XtmTaskManager *manager, guint *num_processes, gfloat *cpu, gfloat *memory, gfloat *swap)
 {
 	guint64 memory_used, swap_used;
 
@@ -148,8 +148,8 @@ xtm_task_manager_get_system_info (XtmTaskManager *manager, guint *num_processes,
 	memory_used = manager->memory_total - manager->memory_free - manager->memory_cache - manager->memory_buffers;
 	swap_used = manager->swap_total - manager->swap_free;
 
-	*memory = (manager->memory_total != 0) ? memory_used * 100 / manager->memory_total : 0;
-	*swap = (manager->swap_total != 0) ? swap_used * 100 / manager->swap_total : 0;
+	*memory = (manager->memory_total != 0) ? memory_used * 100 / (gdouble)manager->memory_total : 0;
+	*swap = (manager->swap_total != 0) ? swap_used * 100 / (gdouble)manager->swap_total : 0;
 
 	/* Set CPU usage */
 	get_cpu_usage (&manager->cpu_count, &manager->cpu_user, &manager->cpu_system);
diff --git a/src/task-manager.h b/src/task-manager.h
index 4c88f94..b1b05bd 100644
--- a/src/task-manager.h
+++ b/src/task-manager.h
@@ -30,7 +30,7 @@ struct _Task
 	gchar program_name[64];
 	gchar full_cmdline[255];
 	gchar state[16];
-	gushort cpu;
+	gfloat cpu;
 	guint64 memory_vsz;
 	guint64 memory_rss;
 	gushort priority;
@@ -41,7 +41,7 @@ struct _Task
  */
 
 gboolean	get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free);
-gboolean	get_cpu_usage (gushort *cpu_count, gushort *cpu_user, gushort *cpu_system);
+gboolean	get_cpu_usage (gushort *cpu_count, gfloat *cpu_user, gfloat *cpu_system);
 //gboolean	get_task_list (GArray *task_list);
 //void		send_signal_to_task (gint task_id, gint signal);
 //void		set_priority_to_task (gint task_id, gint prio);
@@ -63,6 +63,6 @@ GType			xtm_task_manager_get_type			(void);
 XtmTaskManager *	xtm_task_manager_new				();
 const gchar *		xtm_task_manager_get_username			(XtmTaskManager *manager);
 const gchar *		xtm_task_manager_get_hostname			(XtmTaskManager *manager);
-void			xtm_task_manager_get_system_info		(XtmTaskManager *manager, guint *num_processes, gushort *cpu, gushort *memory, gushort *swap);
+void			xtm_task_manager_get_system_info		(XtmTaskManager *manager, guint *num_processes, gfloat *cpu, gfloat *memory, gfloat *swap);
 
 #endif /* !TASK_MANAGER_H */



More information about the Xfce4-commits mailing list