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.
This commit is contained in:
Mike Massonnet
2010-05-02 16:56:33 +02:00
parent 4f04ca5cb7
commit 71d1684696
7 changed files with 26 additions and 26 deletions

View File

@@ -24,7 +24,7 @@ static gboolean
timeout_cb () timeout_cb ()
{ {
guint num_processes; 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_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); xtm_process_window_set_system_info (XTM_PROCESS_WINDOW (window), num_processes, cpu, memory, swap);
} }

View File

@@ -40,9 +40,9 @@ struct _XtmProcessStatusbar
GtkWidget * label_memory; GtkWidget * label_memory;
GtkWidget * label_swap; GtkWidget * label_swap;
gushort cpu; gfloat cpu;
gushort memory; gfloat memory;
gushort swap; gfloat swap;
guint num_processes; guint num_processes;
}; };
G_DEFINE_TYPE (XtmProcessStatusbar, xtm_process_statusbar, GTK_TYPE_STATUSBAR) 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); xtm_process_statusbar_parent_class = g_type_class_peek_parent (klass);
class->set_property = xtm_process_statusbar_set_property; class->set_property = xtm_process_statusbar_set_property;
g_object_class_install_property (class, PROP_CPU, 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_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_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_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)); 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) switch (property_id)
{ {
case PROP_CPU: case PROP_CPU:
statusbar->cpu = g_value_get_uint (value); statusbar->cpu = g_value_get_float (value);
text = g_strdup_printf (_("CPU: %d%%"), statusbar->cpu); text = g_strdup_printf (_("CPU: %.2f%%"), statusbar->cpu);
gtk_label_set_text (GTK_LABEL (statusbar->label_cpu), text); gtk_label_set_text (GTK_LABEL (statusbar->label_cpu), text);
g_free (text); g_free (text);
break; break;
case PROP_MEMORY: case PROP_MEMORY:
statusbar->memory = g_value_get_uint (value); statusbar->memory = g_value_get_float (value);
text = g_strdup_printf (_("Memory: %d%%"), statusbar->memory); text = g_strdup_printf (_("Memory: %.2f%%"), statusbar->memory);
gtk_label_set_text (GTK_LABEL (statusbar->label_memory), text); gtk_label_set_text (GTK_LABEL (statusbar->label_memory), text);
g_free (text); g_free (text);
break; break;
case PROP_SWAP: case PROP_SWAP:
statusbar->swap = g_value_get_uint (value); statusbar->swap = g_value_get_float (value);
text = g_strdup_printf (_("Swap: %d%%"), statusbar->swap); text = g_strdup_printf (_("Swap: %.2f%%"), statusbar->swap);
gtk_label_set_text (GTK_LABEL (statusbar->label_swap), text); gtk_label_set_text (GTK_LABEL (statusbar->label_swap), text);
g_free (text); g_free (text);
break; break;

View File

@@ -370,7 +370,7 @@ xtm_process_window_get_model (XtmProcessWindow *window)
} }
void 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 (XTM_IS_PROCESS_WINDOW (window)));
g_return_if_fail (G_LIKELY (GTK_IS_STATUSBAR (window->priv->statusbar))); g_return_if_fail (G_LIKELY (GTK_IS_STATUSBAR (window->priv->statusbar)));

View File

@@ -29,6 +29,6 @@ typedef struct _XtmProcessWindow XtmProcessWindow;
GType xtm_process_window_get_type (void); GType xtm_process_window_get_type (void);
GtkWidget * xtm_process_window_new (); GtkWidget * xtm_process_window_new ();
GtkTreeModel * xtm_process_window_get_model (XtmProcessWindow *window); 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 */ #endif /* !PROCESS_WINDOW_H */

View File

@@ -49,7 +49,7 @@ get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_c
} }
gboolean 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; FILE *file;
gchar *filename = "/proc/stat"; 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_system = system;
cur_jiffies = cur_jiffies_user + cur_jiffies_system + idle; 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_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 / (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; *cpu_count = (count != 0) ? count : 1;
return TRUE; return TRUE;

View File

@@ -37,8 +37,8 @@ struct _XtmTaskManager
gchar * owner_uid_name; gchar * owner_uid_name;
gchar * hostname; gchar * hostname;
gushort cpu_count; gushort cpu_count;
gushort cpu_user; gfloat cpu_user;
gushort cpu_system; gfloat cpu_system;
guint64 memory_total; guint64 memory_total;
guint64 memory_free; guint64 memory_free;
guint64 memory_cache; guint64 memory_cache;
@@ -134,7 +134,7 @@ 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, gfloat *cpu, gfloat *memory, gfloat *swap)
{ {
guint64 memory_used, swap_used; 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; memory_used = manager->memory_total - manager->memory_free - manager->memory_cache - manager->memory_buffers;
swap_used = manager->swap_total - manager->swap_free; swap_used = manager->swap_total - manager->swap_free;
*memory = (manager->memory_total != 0) ? memory_used * 100 / manager->memory_total : 0; *memory = (manager->memory_total != 0) ? memory_used * 100 / (gdouble)manager->memory_total : 0;
*swap = (manager->swap_total != 0) ? swap_used * 100 / manager->swap_total : 0; *swap = (manager->swap_total != 0) ? swap_used * 100 / (gdouble)manager->swap_total : 0;
/* Set CPU usage */ /* Set CPU usage */
get_cpu_usage (&manager->cpu_count, &manager->cpu_user, &manager->cpu_system); get_cpu_usage (&manager->cpu_count, &manager->cpu_user, &manager->cpu_system);

View File

@@ -30,7 +30,7 @@ struct _Task
gchar program_name[64]; gchar program_name[64];
gchar full_cmdline[255]; gchar full_cmdline[255];
gchar state[16]; gchar state[16];
gushort cpu; gfloat cpu;
guint64 memory_vsz; guint64 memory_vsz;
guint64 memory_rss; guint64 memory_rss;
gushort priority; 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_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); //gboolean get_task_list (GArray *task_list);
//void send_signal_to_task (gint task_id, gint signal); //void send_signal_to_task (gint task_id, gint signal);
//void set_priority_to_task (gint task_id, gint prio); //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 (); 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); void xtm_task_manager_get_system_info (XtmTaskManager *manager, guint *num_processes, gfloat *cpu, gfloat *memory, gfloat *swap);
#endif /* !TASK_MANAGER_H */ #endif /* !TASK_MANAGER_H */