Display the memory usage as bytes instead of a percentage when show-memory-in-xbytes is set (#6503)

- 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
This commit is contained in:
Landry Breuil
2014-12-04 20:52:12 +01:00
parent 7e7521cf2f
commit c6c74cd959
6 changed files with 57 additions and 37 deletions

View File

@@ -86,12 +86,35 @@ static gboolean
init_timeout (void) init_timeout (void)
{ {
guint num_processes; guint num_processes;
gfloat cpu, memory, swap; gfloat cpu, memory_percent, swap_percent;
guint64 swap_free, swap_total; guint64 swap_used, swap_free, swap_total, memory_used, memory_total;
gchar tooltip[1024]; 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_task_manager_get_system_info (task_manager, &num_processes, &cpu, &memory_used, &memory_total, &swap_used, &swap_total);
xtm_process_window_set_system_info (XTM_PROCESS_WINDOW (window), num_processes, cpu, memory, swap);
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_task_manager_get_swap_usage (task_manager, &swap_free, &swap_total);
xtm_process_window_show_swap_usage (XTM_PROCESS_WINDOW (window), (swap_total > 0)); xtm_process_window_show_swap_usage (XTM_PROCESS_WINDOW (window), (swap_total > 0));
@@ -102,17 +125,17 @@ init_timeout (void)
g_snprintf (tooltip, 1024, g_snprintf (tooltip, 1024,
_("<b>Processes:</b> %u\n" _("<b>Processes:</b> %u\n"
"<b>CPU:</b> %.0f%%\n" "<b>CPU:</b> %.0f%%\n"
"<b>Memory:</b> %.0f%%\n" "<b>Memory:</b> %s\n"
"<b>Swap:</b> %.0f%%"), "<b>Swap:</b> %s"),
num_processes, cpu, memory, swap); num_processes, cpu, memory_info, swap_info);
gtk_status_icon_set_tooltip_markup (GTK_STATUS_ICON (status_icon), tooltip); gtk_status_icon_set_tooltip_markup (GTK_STATUS_ICON (status_icon), tooltip);
#else #else
g_snprintf (tooltip, 1024, g_snprintf (tooltip, 1024,
_("Processes: %u\n" _("Processes: %u\n"
"CPU: %.0f%%\n" "CPU: %.0f%%\n"
"Memory: %.0f%%\n" "Memory: %s\n"
"Swap: %.0f%%"), "Swap: %s"),
num_processes, cpu, memory, swap); num_processes, cpu, memory_info, swap_info);
gtk_status_icon_set_tooltip (GTK_STATUS_ICON (status_icon), tooltip); gtk_status_icon_set_tooltip (GTK_STATUS_ICON (status_icon), tooltip);
#endif #endif
} }

View File

@@ -45,8 +45,8 @@ struct _XtmProcessStatusbar
GtkWidget * label_swap; GtkWidget * label_swap;
gfloat cpu; gfloat cpu;
gfloat memory; gchar memory[64];
gfloat swap; gchar swap[64];
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)
@@ -66,9 +66,9 @@ xtm_process_statusbar_class_init (XtmProcessStatusbarClass *klass)
g_object_class_install_property (class, PROP_CPU, 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_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_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_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_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_param_spec_boolean ("show-swap", "ShowSwap", "Show or hide swap usage", TRUE, G_PARAM_WRITABLE));
g_object_class_install_property (class, PROP_NUM_PROCESSES, 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; break;
case PROP_MEMORY: case PROP_MEMORY:
statusbar->memory = g_value_get_float (value); g_strlcpy(statusbar->memory, g_value_get_string (value), 64);
float_value = rounded_float_value (statusbar->memory, statusbar->settings); text = g_strdup_printf (_("Memory: %s"), statusbar->memory);
text = g_strdup_printf (_("Memory: %s%%"), float_value);
gtk_label_set_text (GTK_LABEL (statusbar->label_memory), text); gtk_label_set_text (GTK_LABEL (statusbar->label_memory), text);
g_free (float_value);
g_free (text); g_free (text);
break; break;
case PROP_SWAP: case PROP_SWAP:
statusbar->swap = g_value_get_float (value); g_strlcpy(statusbar->swap, g_value_get_string (value), 64);
float_value = rounded_float_value (statusbar->swap, statusbar->settings); text = g_strdup_printf (_("Swap: %s"), statusbar->swap);
text = g_strdup_printf (_("Swap: %s%%"), float_value);
gtk_label_set_text (GTK_LABEL (statusbar->label_swap), text); gtk_label_set_text (GTK_LABEL (statusbar->label_swap), text);
g_free (float_value);
g_free (text); g_free (text);
break; break;

View File

@@ -408,7 +408,7 @@ xtm_process_window_get_model (XtmProcessWindow *window)
} }
void 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 text[100];
gchar value[4]; 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 (XTM_IS_PROCESS_WINDOW (window));
g_return_if_fail (GTK_IS_STATUSBAR (window->statusbar)); 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); xtm_process_monitor_add_peak (XTM_PROCESS_MONITOR (window->cpu_monitor), cpu / 100.0);
g_snprintf (value, 4, "%.0f", cpu); 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); gtk_widget_set_tooltip_text (window->cpu_monitor, text);
xtm_process_monitor_add_peak (XTM_PROCESS_MONITOR (window->mem_monitor), memory / 100.0); 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"), memory_str);
g_snprintf (text, 100, _("Memory: %s%%"), value);
gtk_widget_set_tooltip_text (window->mem_monitor, text); gtk_widget_set_tooltip_text (window->mem_monitor, text);
} }

View File

@@ -30,7 +30,7 @@ GType xtm_process_window_get_type (void);
GtkWidget * xtm_process_window_new (void); GtkWidget * xtm_process_window_new (void);
void xtm_process_window_show (GtkWidget *widget); void xtm_process_window_show (GtkWidget *widget);
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, 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); void xtm_process_window_show_swap_usage (XtmProcessWindow *window, gboolean show_swap_usage);
#endif /* !PROCESS_WINDOW_H */ #endif /* !PROCESS_WINDOW_H */

View File

@@ -376,10 +376,11 @@ xtm_task_manager_get_hostname (XtmTaskManager *manager)
} }
void void
xtm_task_manager_get_system_info (XtmTaskManager *manager, guint *num_processes, gfloat *cpu, gfloat *memory, gfloat *swap) xtm_task_manager_get_system_info (XtmTaskManager *manager, guint *num_processes, gfloat *cpu,
{ guint64 *memory_used, guint64 *memory_total,
guint64 memory_used, swap_used; guint64 *swap_used, guint64 *swap_total)
{
g_return_if_fail (XTM_IS_TASK_MANAGER (manager)); g_return_if_fail (XTM_IS_TASK_MANAGER (manager));
/* Set number of processes */ /* 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, get_memory_usage (&manager->memory_total, &manager->memory_free, &manager->memory_cache, &manager->memory_buffers,
&manager->swap_total, &manager->swap_free); &manager->swap_total, &manager->swap_free);
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; *memory_total = manager->memory_total;
*swap_used = manager->swap_total - manager->swap_free;
*memory = (manager->memory_total != 0) ? memory_used * 100 / (gdouble)manager->memory_total : 0; *swap_total = manager->swap_total;
*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

@@ -64,7 +64,9 @@ GType xtm_task_manager_get_type (void);
XtmTaskManager * xtm_task_manager_new (GtkTreeModel *model); XtmTaskManager * xtm_task_manager_new (GtkTreeModel *model);
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, 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); 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); const GArray * xtm_task_manager_get_task_list (XtmTaskManager *manager);
void xtm_task_manager_update_model (XtmTaskManager *manager); void xtm_task_manager_update_model (XtmTaskManager *manager);