Index: callbacks.c =================================================================== --- callbacks.c (revision 2332) +++ callbacks.c (working copy) @@ -85,9 +85,15 @@ else show_other_tasks = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem)); - change_task_view(); + change_task_view(); } +void on_show_cached_as_free_toggled (GtkMenuItem *menuitem, gint uid) +{ + show_cached_as_free = !show_cached_as_free; + change_task_view(); +} + void on_quit(void) { save_config(); Index: interface.c =================================================================== --- interface.c (revision 2332) +++ interface.c (working copy) @@ -212,6 +212,7 @@ GtkWidget *show_user_tasks1; GtkWidget *show_root_tasks1; GtkWidget *show_other_tasks1; + GtkWidget *show_cached_as_free1; GtkAccelGroup *accel_group; accel_group = gtk_accel_group_new (); @@ -242,10 +243,16 @@ gtk_widget_show (show_other_tasks1); gtk_menu_shell_append(GTK_MENU_SHELL(mainmenu), show_other_tasks1); + show_cached_as_free1 = gtk_check_menu_item_new_with_mnemonic (_("Show memory used by cache as free")); + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(show_cached_as_free1), show_cached_as_free); + gtk_widget_show (show_cached_as_free1); + gtk_menu_shell_append(GTK_MENU_SHELL(mainmenu), show_cached_as_free1); + g_signal_connect ((gpointer) info1, "activate", G_CALLBACK (on_info1_activate), NULL); g_signal_connect ((gpointer) show_user_tasks1, "toggled", G_CALLBACK (on_show_tasks_toggled), (void *)own_uid); g_signal_connect ((gpointer) show_root_tasks1, "toggled", G_CALLBACK (on_show_tasks_toggled), (void *)0); g_signal_connect ((gpointer) show_other_tasks1, "toggled", G_CALLBACK (on_show_tasks_toggled), (void *)-1); + g_signal_connect ((gpointer) show_other_tasks1, "toggled", G_CALLBACK (on_show_cached_as_free_toggled), (void *)0); gtk_menu_set_accel_group (GTK_MENU (mainmenu), accel_group); @@ -289,7 +296,7 @@ gchar *rss = g_strdup_printf("%i kB", task->rss); gchar *name = g_strdup_printf("%s", task->name); gchar *uname = g_strdup_printf("%s", task->uname); - gchar *time = g_strdup_printf("%.1f%%", task->time_percentage); + gchar *time = g_strdup_printf("%0d%%", (guint)task->time_percentage); gtk_tree_store_set(GTK_TREE_STORE(list_store), iter, COLUMN_NAME, name, -1); gtk_tree_store_set(GTK_TREE_STORE(list_store), iter, COLUMN_PID, pid, -1); Index: xfce-taskmanager-linux.h =================================================================== --- xfce-taskmanager-linux.h (revision 2332) +++ xfce-taskmanager-linux.h (working copy) @@ -14,5 +14,6 @@ struct task get_task_details(gint pid); GArray *get_task_list(); gboolean get_system_status(system_status *sys_stat); +gboolean get_cpu_usage_from_proc(system_status *sys_stat); #endif Index: callbacks.h =================================================================== --- callbacks.h (revision 2332) +++ callbacks.h (working copy) @@ -36,6 +36,7 @@ void on_info1_activate (GtkMenuItem *menuitem, gpointer user_data); void handle_task_menu(GtkWidget *widget, gchar *signal); void on_show_tasks_toggled (GtkMenuItem *menuitem, gint uid); +void on_show_cached_as_free_toggled (GtkMenuItem *menuitem, gint uid); void on_quit(void); Index: functions.c =================================================================== --- functions.c (revision 2332) +++ functions.c (working copy) @@ -20,13 +20,15 @@ #include "functions.h" +static system_status *sys_stat =NULL; + gboolean refresh_task_list(void) { gint i, j; GArray *new_task_list; gchar *cpu_tooltip, *mem_tooltip; gdouble cpu_usage; - system_status *sys_stat; + guint memory_used; /* gets the new task list */ new_task_list = (GArray*) get_task_list(); @@ -49,7 +51,6 @@ tmp->time_percentage = (gdouble)(tmp->time - tmp->old_time) * (gdouble)(1000.0 / REFRESH_INTERVAL); - if((gint)tmp->ppid != (gint)new_tmp->ppid || strcmp(tmp->state,new_tmp->state) || (unsigned int)tmp->size != (unsigned int)new_tmp->size || (unsigned int)tmp->rss != (unsigned int)new_tmp->rss || (unsigned int)tmp->time != (unsigned int)tmp->old_time) { tmp->ppid = new_tmp->ppid; @@ -106,21 +107,26 @@ g_array_free(new_task_list, TRUE); /* update the CPU and memory progress bars */ - sys_stat = g_new (system_status, 1); + if (sys_stat == NULL) + sys_stat = g_new (system_status, 1); get_system_status (sys_stat); - mem_tooltip = g_strdup_printf (_("%d kB of %d kB used"), sys_stat->mem_total - sys_stat->mem_free, sys_stat->mem_total); + memory_used = sys_stat->mem_total - sys_stat->mem_free; + if ( show_cached_as_free ) + { + memory_used-=sys_stat->mem_cached; + } + mem_tooltip = g_strdup_printf (_("%d kB of %d kB used"), memory_used, sys_stat->mem_total); gtk_tooltips_set_tip (tooltips, mem_usage_progress_bar_box, mem_tooltip, NULL); - gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (mem_usage_progress_bar), 1.0 - ( (gdouble) sys_stat->mem_free / sys_stat->mem_total )); + gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (mem_usage_progress_bar), (gdouble)memory_used / sys_stat->mem_total); cpu_usage = get_cpu_usage (sys_stat); - cpu_tooltip = g_strdup_printf (_("%0.0f %%"), cpu_usage * 100); + cpu_tooltip = g_strdup_printf (_("%0.0f %%"), cpu_usage * 100.0); gtk_tooltips_set_tip (tooltips, cpu_usage_progress_bar_box, cpu_tooltip, NULL); gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (cpu_usage_progress_bar), cpu_usage); g_free (mem_tooltip); g_free (cpu_tooltip); - g_free (sys_stat); return TRUE; } @@ -128,16 +134,38 @@ gdouble get_cpu_usage(system_status *sys_stat) { gdouble cpu_usage = 0.0; - gint i = 0; - - for(i = 0; i < task_array->len; i++) + guint current_jiffies; + guint current_used; + guint delta_jiffies; + + if ( get_cpu_usage_from_proc( sys_stat ) == FALSE ) { - struct task *tmp = &g_array_index(task_array, struct task, i); - cpu_usage += tmp->time_percentage; + gint i = 0; + + for(i = 0; i < task_array->len; i++) + { + struct task *tmp = &g_array_index(task_array, struct task, i); + cpu_usage += tmp->time_percentage; + } + + cpu_usage = cpu_usage / (sys_stat->cpu_count * 100.0); + } else { + + if ( sys_stat->cpu_old_jiffies > 0 ) { + current_used = + sys_stat->cpu_user + + sys_stat->cpu_nice + + sys_stat->cpu_system; + current_jiffies = + current_used + + sys_stat->cpu_idle; + delta_jiffies = + current_jiffies - (gdouble)sys_stat->cpu_old_jiffies; + + cpu_usage = (gdouble)( current_used - sys_stat->cpu_old_used ) / + (gdouble)delta_jiffies ; + } } - - cpu_usage = cpu_usage / (sys_stat->cpu_count * 100.0); - return cpu_usage; } @@ -153,6 +181,7 @@ show_user_tasks = xfce_rc_read_bool_entry(rc_file, "show_user_tasks", TRUE); show_root_tasks = xfce_rc_read_bool_entry(rc_file, "show_root_tasks", FALSE); show_other_tasks = xfce_rc_read_bool_entry(rc_file, "show_other_tasks", FALSE); + show_cached_as_free = xfce_rc_read_bool_entry(rc_file, "show_cached_as_free", TRUE); full_view = xfce_rc_read_bool_entry(rc_file, "full_view", FALSE); @@ -171,6 +200,7 @@ xfce_rc_write_bool_entry(rc_file, "show_user_tasks", show_user_tasks); xfce_rc_write_bool_entry(rc_file, "show_root_tasks", show_root_tasks); xfce_rc_write_bool_entry(rc_file, "show_other_tasks", show_other_tasks); + xfce_rc_write_bool_entry(rc_file, "show_cached_as_free", show_cached_as_free); xfce_rc_write_bool_entry(rc_file, "full_view", full_view); Index: types.h =================================================================== --- types.h (revision 2332) +++ types.h (working copy) @@ -43,9 +43,17 @@ typedef struct { - gint mem_total; - gint mem_free; - gint cpu_count; + guint mem_total; + guint mem_free; + guint mem_cached; + guint cpu_count; + guint cpu_idle; + guint cpu_user; + guint cpu_nice; + guint cpu_system; + guint cpu_old_jiffies; + guint cpu_old_used; + gboolean valid_proc_reading; } system_status; GtkWidget *main_window; @@ -60,6 +68,8 @@ gboolean show_root_tasks; gboolean show_other_tasks; +gboolean show_cached_as_free; /* Show memory used Cache as free memory */ + gboolean full_view; guint win_width; Index: xfce-taskmanager-linux.c =================================================================== --- xfce-taskmanager-linux.c (revision 2332) +++ xfce-taskmanager-linux.c (working copy) @@ -153,6 +153,58 @@ return task_list; } +gboolean get_cpu_usage_from_proc(system_status *sys_stat) +{ + const gchar *file_name = "/proc/stat"; + gchar buffer[100]; + guint iddummy; + gboolean retval = FALSE; + FILE *file; + + if ( sys_stat->valid_proc_reading == TRUE ) { + sys_stat->cpu_old_jiffies = + sys_stat->cpu_user + + sys_stat->cpu_nice + + sys_stat->cpu_system+ + sys_stat->cpu_idle; + sys_stat->cpu_old_used = + sys_stat->cpu_user + + sys_stat->cpu_nice + + sys_stat->cpu_system; + } else { + sys_stat->cpu_old_jiffies = 0; + } + + sys_stat->valid_proc_reading = FALSE; + + if (!g_file_test (file_name, G_FILE_TEST_EXISTS)) + { + return FALSE; + } + + + file = fopen (file_name, "r"); + + if (file) + { + if ( fgets (buffer, 100, file) != NULL ) + { + if ( sscanf (buffer, "cpu\t%u %u %u %u", + &sys_stat->cpu_user, + &sys_stat->cpu_nice, + &sys_stat->cpu_system, + &sys_stat->cpu_idle + ) == 4 ) + { + sys_stat->valid_proc_reading = TRUE; + retval = TRUE; + } + } + close( file ); + } + return retval; +} + gboolean get_system_status (system_status *sys_stat) { FILE *file; @@ -175,8 +227,9 @@ { while (fgets (buffer, 100, file) != NULL) { - sscanf (buffer, "MemTotal:\t%i kB", &sys_stat->mem_total); - sscanf (buffer, "MemFree:\t%i kB", &sys_stat->mem_free); + sscanf (buffer, "MemTotal:\t%u kB", &sys_stat->mem_total); + sscanf (buffer, "MemFree:\t%u kB", &sys_stat->mem_free); + sscanf (buffer, "Cached:\t%u kB", &sys_stat->mem_cached); } fclose (file); }