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.
This commit is contained in:
@@ -20,6 +20,7 @@ xfce4_taskmanager_SOURCES = \
|
|||||||
process-tree-view.c process-tree-view.h \
|
process-tree-view.c process-tree-view.h \
|
||||||
process-statusbar.c process-statusbar.h \
|
process-statusbar.c process-statusbar.h \
|
||||||
task-manager.c task-manager.h \
|
task-manager.c task-manager.h \
|
||||||
|
task-manager-linux.c \
|
||||||
settings.c settings.h \
|
settings.c settings.h \
|
||||||
$(NULL)
|
$(NULL)
|
||||||
|
|
||||||
|
|||||||
17
src/main.c
17
src/main.c
@@ -17,11 +17,20 @@
|
|||||||
#include "process-window.h"
|
#include "process-window.h"
|
||||||
#include "task-manager.h"
|
#include "task-manager.h"
|
||||||
|
|
||||||
|
static GtkWidget *window;
|
||||||
|
static XtmTaskManager *task_manager;
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
timeout_cb ()
|
||||||
|
{
|
||||||
|
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[])
|
int main (int argc, char *argv[])
|
||||||
{
|
{
|
||||||
GtkWidget *window;
|
|
||||||
XtmTaskManager *task_manager;
|
|
||||||
|
|
||||||
#ifdef ENABLE_NLS
|
#ifdef ENABLE_NLS
|
||||||
bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
|
bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
|
||||||
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
|
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
|
||||||
@@ -36,6 +45,8 @@ int main (int argc, char *argv[])
|
|||||||
task_manager = xtm_task_manager_new ();
|
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_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);
|
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
|
||||||
|
|
||||||
gtk_main ();
|
gtk_main ();
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ struct _XtmProcessWindowPriv
|
|||||||
{
|
{
|
||||||
GtkBuilder * builder;
|
GtkBuilder * builder;
|
||||||
GtkWidget * window;
|
GtkWidget * window;
|
||||||
|
GtkWidget * cpu_monitor;
|
||||||
|
GtkWidget * memory_monitor;
|
||||||
GtkWidget * treeview;
|
GtkWidget * treeview;
|
||||||
GtkWidget * statusbar;
|
GtkWidget * statusbar;
|
||||||
XtmSettings * settings;
|
XtmSettings * settings;
|
||||||
@@ -96,6 +98,9 @@ xtm_process_window_init (XtmProcessWindow *window)
|
|||||||
gtk_window_resize (GTK_WINDOW (window->priv->window), width, height);
|
gtk_window_resize (GTK_WINDOW (window->priv->window), width, height);
|
||||||
g_signal_connect_swapped (window->priv->window, "destroy", G_CALLBACK (emit_destroy_signal), window);
|
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 ();
|
window->priv->treeview = xtm_process_tree_view_new ();
|
||||||
gtk_widget_show (window->priv->treeview);
|
gtk_widget_show (window->priv->treeview);
|
||||||
gtk_container_add (GTK_CONTAINER (gtk_builder_get_object (window->priv->builder, "scrolledwindow")), 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_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);
|
g_object_set (window->priv->statusbar, "num-processes", num_processes, "cpu", cpu, "memory", memory, "swap", swap, NULL);
|
||||||
// TODO update cpu/memory monitors
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
93
src/task-manager-linux.c
Normal file
93
src/task-manager-linux.c
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2008-2010 Mike Massonnet <mmassonnet@xfce.org>
|
||||||
|
* Copyright (c) 2006 Johannes Zellner <webmaster@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;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -136,6 +136,24 @@ xtm_task_manager_get_tasklist (XtmTaskManager *manager)
|
|||||||
void
|
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, 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
|
void
|
||||||
|
|||||||
@@ -16,6 +16,10 @@
|
|||||||
|
|
||||||
#include <glib-object.h>
|
#include <glib-object.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Task struct used as elements of a task list GArray.
|
||||||
|
*/
|
||||||
|
|
||||||
typedef struct _Task Task;
|
typedef struct _Task Task;
|
||||||
struct _Task
|
struct _Task
|
||||||
{
|
{
|
||||||
@@ -32,6 +36,20 @@ struct _Task
|
|||||||
gushort priority;
|
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_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(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))
|
#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 ();
|
XtmTaskManager * xtm_task_manager_new ();
|
||||||
const gchar * xtm_task_manager_get_username (XtmTaskManager *manager);
|
const gchar * xtm_task_manager_get_username (XtmTaskManager *manager);
|
||||||
const gchar * xtm_task_manager_get_hostname (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 */
|
#endif /* !TASK_MANAGER_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user