[Xfce4-commits] <xfce4-taskmanager:master> Read information from memory/swap and CPU

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


Updating branch refs/heads/master
         to 4f04ca5cb776403629634f69acdbf8ccfcb4ccbe (commit)
       from 9a49bf015a418bc8b4447293b37b4742f615992e (commit)

commit 4f04ca5cb776403629634f69acdbf8ccfcb4ccbe
Author: Mike Massonnet <mmassonnet at xfce.org>
Date:   Sun May 2 15:15:15 2010 +0200

    Read information from memory/swap and CPU
    
    This commit is the second of an incremental serie. There will be other
    commits with the information about tasks and the support for BSD and
    Solaris once things are settled.
    
    The TaskManager class is build with a new source task-manager-linux.c
    that provides functions specific to the Linux implementation. It has
    functions to read the usage of the memory/swap and the CPU.

 src/Makefile.am          |    1 +
 src/main.c               |   17 +++++++-
 src/process-window.c     |    7 +++
 src/task-manager-linux.c |   93 ++++++++++++++++++++++++++++++++++++++++++++++
 src/task-manager.c       |   18 +++++++++
 src/task-manager.h       |   19 +++++++++
 6 files changed, 152 insertions(+), 3 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 5d8a03a..6ba4093 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -20,6 +20,7 @@ xfce4_taskmanager_SOURCES =						\
 	process-tree-view.c		process-tree-view.h		\
 	process-statusbar.c		process-statusbar.h		\
 	task-manager.c			task-manager.h			\
+	task-manager-linux.c						\
 	settings.c			settings.h			\
 	$(NULL)
 
diff --git a/src/main.c b/src/main.c
index 36b1403..3f41dc3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -17,11 +17,20 @@
 #include "process-window.h"
 #include "task-manager.h"
 
-int main (int argc, char *argv[])
+static GtkWidget *window;
+static XtmTaskManager *task_manager;
+
+static gboolean
+timeout_cb ()
 {
-	GtkWidget *window;
-	XtmTaskManager *task_manager;
+	guint num_processes;
+	gushort 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);
+}
 
+int main (int argc, char *argv[])
+{
 #ifdef ENABLE_NLS
 	bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
@@ -36,6 +45,8 @@ int main (int argc, char *argv[])
 	task_manager = xtm_task_manager_new ();
 	g_message ("Running as %s on %s", xtm_task_manager_get_username (task_manager), xtm_task_manager_get_hostname (task_manager));
 
+	g_timeout_add (1000, timeout_cb, NULL);
+
 	g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
 
 	gtk_main ();
diff --git a/src/process-window.c b/src/process-window.c
index 9ffd722..fec17be 100644
--- a/src/process-window.c
+++ b/src/process-window.c
@@ -39,6 +39,8 @@ struct _XtmProcessWindowPriv
 {
 	GtkBuilder *		builder;
 	GtkWidget *		window;
+	GtkWidget *		cpu_monitor;
+	GtkWidget *		memory_monitor;
 	GtkWidget *		treeview;
 	GtkWidget *		statusbar;
 	XtmSettings *		settings;
@@ -96,6 +98,9 @@ xtm_process_window_init (XtmProcessWindow *window)
 		gtk_window_resize (GTK_WINDOW (window->priv->window), width, height);
 	g_signal_connect_swapped (window->priv->window, "destroy", G_CALLBACK (emit_destroy_signal), window);
 
+	window->priv->cpu_monitor = GTK_WIDGET (gtk_builder_get_object (window->priv->builder, "cpu-monitor"));
+	window->priv->memory_monitor = GTK_WIDGET (gtk_builder_get_object (window->priv->builder, "mem-monitor"));
+
 	window->priv->treeview = xtm_process_tree_view_new ();
 	gtk_widget_show (window->priv->treeview);
 	gtk_container_add (GTK_CONTAINER (gtk_builder_get_object (window->priv->builder, "scrolledwindow")), window->priv->treeview);
@@ -371,5 +376,7 @@ xtm_process_window_set_system_info (XtmProcessWindow *window, guint num_processe
 	g_return_if_fail (G_LIKELY (GTK_IS_STATUSBAR (window->priv->statusbar)));
 	g_object_set (window->priv->statusbar, "num-processes", num_processes, "cpu", cpu, "memory", memory, "swap", swap, NULL);
 	// TODO update cpu/memory monitors
+	gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (window->priv->memory_monitor), memory / 100.0);
+	gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (window->priv->cpu_monitor), cpu / 100.0);
 }
 
diff --git a/src/task-manager-linux.c b/src/task-manager-linux.c
new file mode 100644
index 0000000..0ba0356
--- /dev/null
+++ b/src/task-manager-linux.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2008-2010  Mike Massonnet <mmassonnet at xfce.org>
+ * Copyright (c) 2006  Johannes Zellner <webmaster at nebulon.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdio.h>
+
+#include <glib.h>
+
+#include "task-manager.h"
+
+gboolean
+get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free)
+{
+	FILE *file;
+	gchar buffer[1024];
+	gchar *filename = "/proc/meminfo";
+	gushort found = 0;
+
+	if ((file = fopen (filename, "r")) == NULL)
+	{
+		return FALSE;
+	}
+
+	while (found < 6 && fgets (buffer, 1024, file) != NULL)
+	{
+		found += sscanf (buffer, "MemTotal:\t%u kB", memory_total);
+		found += sscanf (buffer, "MemFree:\t%u kB", memory_free);
+		found += sscanf (buffer, "Cached:\t%u kB", memory_cache);
+		found += sscanf (buffer, "Buffers:\t%u kB", memory_buffers);
+		found += sscanf (buffer, "SwapTotal:\t%u kB", swap_total);
+		found += sscanf (buffer, "SwapFree:\t%u kB", swap_free);
+	}
+	fclose (file);
+
+	*memory_total *= 1024;
+	*memory_free *= 1024;
+	*memory_cache *= 1024;
+	*memory_buffers *= 1024;
+	*swap_total *= 1024;
+	*swap_free *= 1024;
+
+	return TRUE;
+}
+
+gboolean
+get_cpu_usage (gushort *cpu_count, gushort *cpu_user, gushort *cpu_system)
+{
+	FILE *file;
+	gchar *filename = "/proc/stat";
+	gchar buffer[1024];
+	static gulong cur_jiffies_user = 0, old_jiffies_user = 0;
+	static gulong cur_jiffies_system = 0, old_jiffies_system = 0;
+	static gulong cur_jiffies = 0, old_jiffies = 0;
+	gulong user, user_nice, system, idle;
+	gushort count = 0;
+
+	if ((file = fopen (filename, "r")) == NULL)
+	{
+		return FALSE;
+	}
+
+	fgets (buffer, 1024, file);
+	sscanf (buffer, "cpu\t%u %u %u %u", &user, &user_nice, &system, &idle);
+
+	while (fgets (buffer, 1024, file) != NULL)
+	{
+		if (buffer[0] != 'c' && buffer[1] != 'p' && buffer[2] != 'u')
+			break;
+		count += 1;
+	}
+	fclose (file);
+
+	old_jiffies_user = cur_jiffies_user;
+	old_jiffies_system = cur_jiffies_system;
+	old_jiffies = cur_jiffies;
+
+	cur_jiffies_user = user + user_nice;
+	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_count = (count != 0) ? count : 1;
+
+	return TRUE;
+}
+
diff --git a/src/task-manager.c b/src/task-manager.c
index 36e5474..3df42fb 100644
--- a/src/task-manager.c
+++ b/src/task-manager.c
@@ -136,6 +136,24 @@ 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)
 {
+	guint64 memory_used, swap_used;
+
+	/* Set number of processes */
+	*num_processes = 0;//manager->tasks->len;
+
+	/* Set memory and swap usage */
+	get_memory_usage (&manager->memory_total, &manager->memory_free, &manager->memory_cache, &manager->memory_buffers,
+			&manager->swap_total, &manager->swap_free);
+
+	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;
+
+	/* Set CPU usage */
+	get_cpu_usage (&manager->cpu_count, &manager->cpu_user, &manager->cpu_system);
+	*cpu = manager->cpu_user + manager->cpu_system;
 }
 
 void
diff --git a/src/task-manager.h b/src/task-manager.h
index df3a14f..4c88f94 100644
--- a/src/task-manager.h
+++ b/src/task-manager.h
@@ -16,6 +16,10 @@
 
 #include <glib-object.h>
 
+/**
+ * Task struct used as elements of a task list GArray.
+ */
+
 typedef struct _Task Task;
 struct _Task
 {
@@ -32,6 +36,20 @@ struct _Task
 	gushort priority;
 };
 
+/**
+ * OS specific implementation.
+ */
+
+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_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);
+
+/**
+ * GObject class used to update the graphical widgets.
+ */
+
 #define XTM_TYPE_TASK_MANAGER			(xtm_task_manager_get_type ())
 #define XTM_TASK_MANAGER(obj)			(G_TYPE_CHECK_INSTANCE_CAST ((obj), XTM_TYPE_TASK_MANAGER, XtmTaskManager))
 #define XTM_TASK_MANAGER_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST ((klass), XTM_TYPE_TASK_MANAGER, XtmTaskManagerClass))
@@ -45,5 +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);
 
 #endif /* !TASK_MANAGER_H */



More information about the Xfce4-commits mailing list