From c6c74cd959c0b6818740306338e16b77970b82e1 Mon Sep 17 00:00:00 2001 From: Landry Breuil Date: Thu, 4 Dec 2014 20:52:12 +0100 Subject: [PATCH] Display the memory usage as bytes instead of a percentage when show-memory-in-xbytes is set (#6503) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - move the percentage calculation to init_timeout(), use g_format_size() here too - carry memory/swap used vs total all around - make the XtmProcessStatusbar swap and memory properties strings instead of float Based on a diff from Ján Sucan, thanks! Note to translators: this changes some strings, mostly removing % in format printing --- src/main.c | 45 +++++++++++++++++++++++++++++++---------- src/process-statusbar.c | 20 ++++++++---------- src/process-window.c | 7 +++---- src/process-window.h | 2 +- src/task-manager.c | 16 +++++++-------- src/task-manager.h | 4 +++- 6 files changed, 57 insertions(+), 37 deletions(-) diff --git a/src/main.c b/src/main.c index c93a109..f9645c8 100644 --- a/src/main.c +++ b/src/main.c @@ -86,12 +86,35 @@ static gboolean init_timeout (void) { guint num_processes; - gfloat cpu, memory, swap; - guint64 swap_free, swap_total; - gchar tooltip[1024]; + gfloat cpu, memory_percent, swap_percent; + guint64 swap_used, swap_free, swap_total, memory_used, memory_total; + gchar *used, *total, tooltip[1024], memory_info[64], swap_info[64]; + gboolean show_memory_in_xbytes; - 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_task_manager_get_system_info (task_manager, &num_processes, &cpu, &memory_used, &memory_total, &swap_used, &swap_total); + + memory_percent = (memory_total != 0) ? memory_used * 100 / (gdouble)memory_total : 0; + swap_percent = (swap_total != 0) ? swap_used * 100 / (gdouble)swap_total : 0; + + g_object_get (settings, "show-memory-in-xbytes", &show_memory_in_xbytes, NULL); + if (show_memory_in_xbytes) { + used = g_format_size(memory_used); + total = g_format_size(memory_total); + g_snprintf (memory_info, 64,"%s / %s", used, total); + g_free(used); + g_free(total); + + used = g_format_size(swap_used); + total = g_format_size(swap_total); + g_snprintf (swap_info, 64,"%s / %s", used, total); + g_free(used); + g_free(total); + } else { + g_snprintf (memory_info, 64, "%.0f%%", memory_percent); + g_snprintf (swap_info, 64, "%.0f%%", swap_percent); + } + + xtm_process_window_set_system_info (XTM_PROCESS_WINDOW (window), num_processes, cpu, memory_percent, memory_info, swap_percent, swap_info); xtm_task_manager_get_swap_usage (task_manager, &swap_free, &swap_total); xtm_process_window_show_swap_usage (XTM_PROCESS_WINDOW (window), (swap_total > 0)); @@ -102,17 +125,17 @@ init_timeout (void) g_snprintf (tooltip, 1024, _("Processes: %u\n" "CPU: %.0f%%\n" - "Memory: %.0f%%\n" - "Swap: %.0f%%"), - num_processes, cpu, memory, swap); + "Memory: %s\n" + "Swap: %s"), + num_processes, cpu, memory_info, swap_info); gtk_status_icon_set_tooltip_markup (GTK_STATUS_ICON (status_icon), tooltip); #else g_snprintf (tooltip, 1024, _("Processes: %u\n" "CPU: %.0f%%\n" - "Memory: %.0f%%\n" - "Swap: %.0f%%"), - num_processes, cpu, memory, swap); + "Memory: %s\n" + "Swap: %s"), + num_processes, cpu, memory_info, swap_info); gtk_status_icon_set_tooltip (GTK_STATUS_ICON (status_icon), tooltip); #endif } diff --git a/src/process-statusbar.c b/src/process-statusbar.c index 8535728..23cb022 100644 --- a/src/process-statusbar.c +++ b/src/process-statusbar.c @@ -45,8 +45,8 @@ struct _XtmProcessStatusbar GtkWidget * label_swap; gfloat cpu; - gfloat memory; - gfloat swap; + gchar memory[64]; + gchar swap[64]; guint num_processes; }; G_DEFINE_TYPE (XtmProcessStatusbar, xtm_process_statusbar, GTK_TYPE_STATUSBAR) @@ -66,9 +66,9 @@ xtm_process_statusbar_class_init (XtmProcessStatusbarClass *klass) g_object_class_install_property (class, PROP_CPU, 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_float ("memory", "Memory", "Memory usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE)); + g_param_spec_string ("memory", "Memory", "Memory usage", "", G_PARAM_CONSTRUCT|G_PARAM_WRITABLE)); g_object_class_install_property (class, PROP_SWAP, - g_param_spec_float ("swap", "Swap", "Swap usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE)); + g_param_spec_string ("swap", "Swap", "Swap usage", "", G_PARAM_CONSTRUCT|G_PARAM_WRITABLE)); g_object_class_install_property (class, PROP_SHOW_SWAP, g_param_spec_boolean ("show-swap", "ShowSwap", "Show or hide swap usage", TRUE, G_PARAM_WRITABLE)); g_object_class_install_property (class, PROP_NUM_PROCESSES, @@ -152,20 +152,16 @@ xtm_process_statusbar_set_property (GObject *object, guint property_id, const GV break; case PROP_MEMORY: - statusbar->memory = g_value_get_float (value); - float_value = rounded_float_value (statusbar->memory, statusbar->settings); - text = g_strdup_printf (_("Memory: %s%%"), float_value); + g_strlcpy(statusbar->memory, g_value_get_string (value), 64); + text = g_strdup_printf (_("Memory: %s"), statusbar->memory); gtk_label_set_text (GTK_LABEL (statusbar->label_memory), text); - g_free (float_value); g_free (text); break; case PROP_SWAP: - statusbar->swap = g_value_get_float (value); - float_value = rounded_float_value (statusbar->swap, statusbar->settings); - text = g_strdup_printf (_("Swap: %s%%"), float_value); + g_strlcpy(statusbar->swap, g_value_get_string (value), 64); + text = g_strdup_printf (_("Swap: %s"), statusbar->swap); gtk_label_set_text (GTK_LABEL (statusbar->label_swap), text); - g_free (float_value); g_free (text); break; diff --git a/src/process-window.c b/src/process-window.c index 30002d9..126cd69 100644 --- a/src/process-window.c +++ b/src/process-window.c @@ -408,7 +408,7 @@ xtm_process_window_get_model (XtmProcessWindow *window) } void -xtm_process_window_set_system_info (XtmProcessWindow *window, guint num_processes, gfloat cpu, gfloat memory, gfloat swap) +xtm_process_window_set_system_info (XtmProcessWindow *window, guint num_processes, gfloat cpu, gfloat memory, gchar* memory_str, gfloat swap, gchar* swap_str) { gchar text[100]; gchar value[4]; @@ -416,7 +416,7 @@ xtm_process_window_set_system_info (XtmProcessWindow *window, guint num_processe g_return_if_fail (XTM_IS_PROCESS_WINDOW (window)); g_return_if_fail (GTK_IS_STATUSBAR (window->statusbar)); - g_object_set (window->statusbar, "num-processes", num_processes, "cpu", cpu, "memory", memory, "swap", swap, NULL); + g_object_set (window->statusbar, "num-processes", num_processes, "cpu", cpu, "memory", memory_str, "swap", swap_str, NULL); xtm_process_monitor_add_peak (XTM_PROCESS_MONITOR (window->cpu_monitor), cpu / 100.0); g_snprintf (value, 4, "%.0f", cpu); @@ -424,8 +424,7 @@ xtm_process_window_set_system_info (XtmProcessWindow *window, guint num_processe gtk_widget_set_tooltip_text (window->cpu_monitor, text); xtm_process_monitor_add_peak (XTM_PROCESS_MONITOR (window->mem_monitor), memory / 100.0); - g_snprintf (value, 4, "%.0f", memory); - g_snprintf (text, 100, _("Memory: %s%%"), value); + g_snprintf (text, 100, _("Memory: %s"), memory_str); gtk_widget_set_tooltip_text (window->mem_monitor, text); } diff --git a/src/process-window.h b/src/process-window.h index 61f7464..1203186 100644 --- a/src/process-window.h +++ b/src/process-window.h @@ -30,7 +30,7 @@ GType xtm_process_window_get_type (void); GtkWidget * xtm_process_window_new (void); void xtm_process_window_show (GtkWidget *widget); GtkTreeModel * xtm_process_window_get_model (XtmProcessWindow *window); -void xtm_process_window_set_system_info (XtmProcessWindow *window, guint num_processes, gfloat cpu, gfloat memory, gfloat swap); +void xtm_process_window_set_system_info (XtmProcessWindow *window, guint num_processes, gfloat cpu, gfloat memory, gchar* memory_str, gfloat swap, gchar* swap_str); void xtm_process_window_show_swap_usage (XtmProcessWindow *window, gboolean show_swap_usage); #endif /* !PROCESS_WINDOW_H */ diff --git a/src/task-manager.c b/src/task-manager.c index 77d86ce..32fe28a 100644 --- a/src/task-manager.c +++ b/src/task-manager.c @@ -376,10 +376,11 @@ xtm_task_manager_get_hostname (XtmTaskManager *manager) } void -xtm_task_manager_get_system_info (XtmTaskManager *manager, guint *num_processes, gfloat *cpu, gfloat *memory, gfloat *swap) -{ - guint64 memory_used, swap_used; +xtm_task_manager_get_system_info (XtmTaskManager *manager, guint *num_processes, gfloat *cpu, + guint64 *memory_used, guint64 *memory_total, + guint64 *swap_used, guint64 *swap_total) +{ g_return_if_fail (XTM_IS_TASK_MANAGER (manager)); /* Set number of processes */ @@ -389,11 +390,10 @@ xtm_task_manager_get_system_info (XtmTaskManager *manager, guint *num_processes, 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 / (gdouble)manager->memory_total : 0; - *swap = (manager->swap_total != 0) ? swap_used * 100 / (gdouble)manager->swap_total : 0; + *memory_used = manager->memory_total - manager->memory_free - manager->memory_cache - manager->memory_buffers; + *memory_total = manager->memory_total; + *swap_used = manager->swap_total - manager->swap_free; + *swap_total = manager->swap_total; /* 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 fa90f8f..e74acd0 100644 --- a/src/task-manager.h +++ b/src/task-manager.h @@ -64,7 +64,9 @@ GType xtm_task_manager_get_type (void); XtmTaskManager * xtm_task_manager_new (GtkTreeModel *model); 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, gfloat *cpu, gfloat *memory, gfloat *swap); +void xtm_task_manager_get_system_info (XtmTaskManager *manager, guint *num_processes, gfloat *cpu, + guint64 *memory_used, guint64 *memory_total, + guint64 *swap_used, guint64 *swap_total); void xtm_task_manager_get_swap_usage (XtmTaskManager *manager, guint64 *swap_free, guint64 *swap_total); const GArray * xtm_task_manager_get_task_list (XtmTaskManager *manager); void xtm_task_manager_update_model (XtmTaskManager *manager);