| 1 | /* Procman tree view and process updating |
| 2 | * Copyright (C) 2001 Kevin Vandersloot |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public |
| 15 | * License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | #include <config.h> |
| 21 | |
| 22 | #include <string.h> |
| 23 | #include <math.h> |
| 24 | #include <glib/gi18n.h> |
| 25 | #include <glib/gprintf.h> |
| 26 | #include <glibtop.h> |
| 27 | #include <glibtop/proclist.h> |
| 28 | #include <glibtop/procstate.h> |
| 29 | #include <glibtop/procio.h> |
| 30 | #include <glibtop/procmem.h> |
| 31 | #include <glibtop/procmap.h> |
| 32 | #include <glibtop/proctime.h> |
| 33 | #include <glibtop/procuid.h> |
| 34 | #include <glibtop/procargs.h> |
| 35 | #include <glibtop/prockernel.h> |
| 36 | #include <glibtop/mem.h> |
| 37 | #include <glibtop/swap.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <pwd.h> |
| 40 | #include <time.h> |
| 41 | |
| 42 | #include <set> |
| 43 | #include <list> |
| 44 | |
| 45 | #include "application.h" |
| 46 | #include "proctable.h" |
| 47 | #include "prettytable.h" |
| 48 | #include "util.h" |
| 49 | #include "interface.h" |
| 50 | #include "procinfo.h" |
| 51 | #include "gsm-selinux.h" |
| 52 | #include "settings-keys.h" |
| 53 | #include "gsm-cgroups.h" |
| 54 | #include "legacy/treeview.h" |
| 55 | |
| 56 | #ifdef HAVE_SYSTEMD |
| 57 | #include "gsm-systemd.h" |
| 58 | #endif |
| 59 | |
| 60 | |
| 61 | static void |
| 62 | cb_save_tree_state (gpointer, |
| 63 | gpointer data) |
| 64 | { |
| 65 | GsmApplication * const app = static_cast<GsmApplication *>(data); |
| 66 | |
| 67 | gsm_tree_view_save_state (app->tree); |
| 68 | } |
| 69 | |
| 70 | static void |
| 71 | cb_proctree_destroying (GtkTreeView *self, |
| 72 | gpointer data) |
| 73 | { |
| 74 | g_signal_handlers_disconnect_by_func (self, |
| 75 | (gpointer) cb_save_tree_state, |
| 76 | data); |
| 77 | |
| 78 | g_signal_handlers_disconnect_by_func (gtk_tree_view_get_model (self), |
| 79 | (gpointer) cb_save_tree_state, |
| 80 | data); |
| 81 | } |
| 82 | |
| 83 | static gboolean |
| 84 | cb_tree_button_pressed (GtkGestureClick*, |
| 85 | gint, |
| 86 | gdouble x, |
| 87 | gdouble y, |
| 88 | GsmApplication *app) |
| 89 | { |
| 90 | GtkTreePath *path = NULL; |
| 91 | GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (app->tree)); |
| 92 | int bwx, bwy; |
| 93 | |
| 94 | gtk_tree_view_convert_widget_to_bin_window_coords (GTK_TREE_VIEW (app->tree), x, y, &bwx, &bwy); |
| 95 | |
| 96 | if (!gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (app->tree), bwx, bwy, &path, NULL, NULL, NULL)) |
| 97 | return FALSE; |
| 98 | |
| 99 | if (!gtk_tree_selection_path_is_selected (selection, path)) |
| 100 | gtk_tree_selection_select_path (selection, path); |
| 101 | gtk_tree_path_free (path); |
| 102 | |
| 103 | GdkRectangle rect = { (gint)x, (gint)y, 1, 1 }; |
| 104 | |
| 105 | gtk_popover_set_pointing_to (GTK_POPOVER (app->proc_popover_menu), &rect); |
| 106 | gtk_popover_popup (GTK_POPOVER (app->proc_popover_menu)); |
| 107 | |
| 108 | return TRUE; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | get_last_selected (GtkTreeModel *model, |
| 113 | GtkTreePath*, |
| 114 | GtkTreeIter *iter, |
| 115 | gpointer data) |
| 116 | { |
| 117 | ProcInfo **info = (ProcInfo**) data; |
| 118 | |
| 119 | gtk_tree_model_get (model, iter, COL_POINTER, info, -1); |
| 120 | } |
| 121 | |
| 122 | static void |
| 123 | cb_row_selected (GtkTreeSelection *selection, |
| 124 | gpointer data) |
| 125 | { |
| 126 | GsmApplication *app = (GsmApplication *) data; |
| 127 | |
| 128 | ProcInfo *selected_process = NULL; |
| 129 | gint selected_count = gtk_tree_selection_count_selected_rows (selection); |
| 130 | |
| 131 | app->selection = selection; |
| 132 | |
| 133 | gchar *button_text = ngettext ("_End Process…", "_End Processes…", selected_count); |
| 134 | |
| 135 | gtk_button_set_label (GTK_BUTTON (app->end_process_button), button_text); |
| 136 | |
| 137 | /* get the most recent selected process and determine if there are |
| 138 | ** no selected processes |
| 139 | */ |
| 140 | gtk_tree_selection_selected_foreach (selection, get_last_selected, |
| 141 | &selected_process); |
| 142 | if (selected_process) |
| 143 | { |
| 144 | GVariant *priority; |
| 145 | gint nice = selected_process->nice; |
| 146 | if (nice < -7) |
| 147 | priority = g_variant_new_int32 (-20); |
| 148 | else if (nice < -2) |
| 149 | priority = g_variant_new_int32 (-5); |
| 150 | else if (nice < 3) |
| 151 | priority = g_variant_new_int32 (0); |
| 152 | else if (nice < 7) |
| 153 | priority = g_variant_new_int32 (5); |
| 154 | else |
| 155 | priority = g_variant_new_int32 (19); |
| 156 | |
| 157 | GAction *action = g_action_map_lookup_action (G_ACTION_MAP (app->main_window), |
| 158 | "priority"); |
| 159 | |
| 160 | g_action_change_state (action, priority); |
| 161 | } |
| 162 | update_sensitivity (app); |
| 163 | } |
| 164 | |
| 165 | static gint |
| 166 | cb_timeout (gpointer data) |
| 167 | { |
| 168 | GsmApplication *app = (GsmApplication *) data; |
| 169 | guint new_interval; |
| 170 | |
| 171 | proctable_update (app); |
| 172 | |
| 173 | if (app->smooth_refresh->get (new_interval)) |
| 174 | { |
| 175 | app->timeout = g_timeout_add (new_interval, |
| 176 | cb_timeout, |
| 177 | app); |
| 178 | return G_SOURCE_REMOVE; |
| 179 | } |
| 180 | |
| 181 | return G_SOURCE_CONTINUE; |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | cb_refresh_icons (GtkIconTheme*, |
| 186 | gpointer data) |
| 187 | { |
| 188 | GsmApplication *app = (GsmApplication *) data; |
| 189 | |
| 190 | for (auto&v : app->processes) |
| 191 | app->pretty_table->set_icon (v.second); |
| 192 | |
| 193 | proctable_update (app); |
| 194 | } |
| 195 | |
| 196 | static gboolean |
| 197 | iter_matches_search_key (GtkTreeModel *model, |
| 198 | GtkTreeIter *iter, |
| 199 | const gchar *search_text) |
| 200 | { |
| 201 | char *name; |
| 202 | char *user; |
| 203 | pid_t pid; |
| 204 | char *pids; |
| 205 | char *args; |
| 206 | gboolean found; |
| 207 | char *search_pattern; |
| 208 | char **keys; |
| 209 | Glib::RefPtr<Glib::Regex> regex; |
| 210 | |
| 211 | gtk_tree_model_get (model, iter, |
| 212 | COL_NAME, &name, |
| 213 | COL_USER, &user, |
| 214 | COL_PID, &pid, |
| 215 | COL_ARGS, &args, |
| 216 | -1); |
| 217 | |
| 218 | pids = g_strdup_printf ("%d", pid); |
| 219 | |
| 220 | keys = g_strsplit_set (search_text, " |", -1); |
| 221 | search_pattern = g_strjoinv ("|", keys); |
| 222 | try { |
| 223 | regex = Glib::Regex::create (search_pattern, Glib::Regex::CompileFlags::CASELESS); |
| 224 | } catch (const Glib::Error&ex) { |
| 225 | regex = Glib::Regex::create (Glib::Regex::escape_string (search_pattern), Glib::Regex::CompileFlags::CASELESS); |
| 226 | } |
| 227 | |
| 228 | found = (name && regex->match (name)) || (user && regex->match (user)) |
| 229 | || (pids && regex->match (pids)) || (args && regex->match (args)); |
| 230 | |
| 231 | g_strfreev (keys); |
| 232 | g_free (search_pattern); |
| 233 | g_free (name); |
| 234 | g_free (user); |
| 235 | g_free (args); |
| 236 | g_free (pids); |
| 237 | |
| 238 | return found; |
| 239 | } |
| 240 | |
| 241 | static gboolean |
| 242 | process_visibility_func (GtkTreeModel *model, |
| 243 | GtkTreeIter *iter, |
| 244 | gpointer data) |
| 245 | { |
| 246 | GsmApplication * const app = static_cast<GsmApplication *>(data); |
| 247 | const gchar *search_text = app->search_entry == NULL ? "" : gtk_editable_get_text (GTK_EDITABLE (app->search_entry)); |
| 248 | GtkTreePath *tree_path = gtk_tree_model_get_path (model, iter); |
| 249 | |
| 250 | if (strcmp (search_text, "") == 0) |
| 251 | { |
| 252 | gtk_tree_path_free (tree_path); |
| 253 | return TRUE; |
| 254 | } |
| 255 | |
| 256 | // in case we are in dependencies view, we show (and expand) rows not matching the text, but having a matching child |
| 257 | gboolean match = false; |
| 258 | |
| 259 | if (app->settings->get_boolean (GSM_SETTING_SHOW_DEPENDENCIES)) |
| 260 | { |
| 261 | GtkTreeIter child; |
| 262 | if (gtk_tree_model_iter_children (model, &child, iter)) |
| 263 | { |
| 264 | gboolean child_match = FALSE; |
| 265 | do |
| 266 | { |
| 267 | child_match = process_visibility_func (model, &child, data); |
| 268 | } while (gtk_tree_model_iter_next (model, &child) && !child_match); |
| 269 | match = child_match; |
| 270 | } |
| 271 | |
| 272 | match |= iter_matches_search_key (model, iter, search_text); |
| 273 | if (match && (strlen (search_text) > 0)) |
| 274 | gtk_tree_view_expand_to_path (GTK_TREE_VIEW (app->tree), tree_path); |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | match = iter_matches_search_key (model, iter, search_text); |
| 279 | } |
| 280 | |
| 281 | gtk_tree_path_free (tree_path); |
| 282 | |
| 283 | return match; |
| 284 | } |
| 285 | |
| 286 | static void |
| 287 | proctable_clear_tree (GsmApplication * const app) |
| 288 | { |
| 289 | GtkTreeModel *model; |
| 290 | |
| 291 | model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER ( |
| 292 | gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT ( |
| 293 | gtk_tree_view_get_model (GTK_TREE_VIEW (app->tree)))))); |
| 294 | |
| 295 | gtk_tree_store_clear (GTK_TREE_STORE (model)); |
| 296 | |
| 297 | proctable_free_table (app); |
| 298 | |
| 299 | update_sensitivity (app); |
| 300 | } |
| 301 | |
| 302 | static void |
| 303 | cb_show_dependencies_changed (Gio::Settings& settings, |
| 304 | Glib::ustring, |
| 305 | GsmApplication*app) |
| 306 | { |
| 307 | if (app->timeout) |
| 308 | { |
| 309 | gtk_tree_view_set_show_expanders (GTK_TREE_VIEW (app->tree), |
| 310 | settings.get_boolean (GSM_SETTING_SHOW_DEPENDENCIES)); |
| 311 | |
| 312 | proctable_clear_tree (app); |
| 313 | proctable_update (app); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | static void |
| 318 | cb_show_whose_processes_changed (Gio::Settings&, |
| 319 | Glib::ustring, |
| 320 | GsmApplication *app) |
| 321 | { |
| 322 | if (app->timeout) |
| 323 | { |
| 324 | proctable_clear_tree (app); |
| 325 | proctable_update (app); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | GsmTreeView * |
| 330 | proctable_new (GsmApplication * const app) |
| 331 | { |
| 332 | GsmTreeView *proctree; |
| 333 | GtkTreeStore *model; |
| 334 | GtkTreeModelFilter *model_filter; |
| 335 | GtkTreeModelSort *model_sort; |
| 336 | GtkTreeSelection *selection; |
| 337 | GtkGesture *click_controller; |
| 338 | GtkTreeViewColumn *column; |
| 339 | GtkCellRenderer *cell_renderer; |
| 340 | const gchar *titles[] = { |
| 341 | N_("Name"), |
| 342 | N_("User"), |
| 343 | N_("Status"), |
| 344 | N_("Virtual Memory"), |
| 345 | N_("Resident Memory"), |
| 346 | N_("Writable Memory"), |
| 347 | N_("Shared Memory"), |
| 348 | N_("X Server Memory"), |
| 349 | /* xgettext:no-c-format */ N_("CPU"), |
| 350 | N_("CPU Time"), |
| 351 | N_("Started"), |
| 352 | N_("Nice"), |
| 353 | N_("ID"), |
| 354 | N_("Security Context"), |
| 355 | N_("Command Line"), |
| 356 | N_("Memory"), |
| 357 | /* xgettext: combined noun, the function the process is waiting in, see wchan ps(1) */ |
| 358 | N_("Waiting Channel"), |
| 359 | N_("Control Group"), |
| 360 | N_("Unit"), |
| 361 | N_("Session"), |
| 362 | /* TRANSLATORS: Seat = i.e. the physical seat the session of the process belongs to, only |
| 363 | for multi-seat environments. See http://en.wikipedia.org/wiki/Multiseat_configuration */ |
| 364 | N_("Seat"), |
| 365 | N_("Owner"), |
| 366 | N_("Disk Read Total"), |
| 367 | N_("Disk Write Total"), |
| 368 | N_("Disk Read"), |
| 369 | N_("Disk Write"), |
| 370 | N_("Priority"), |
| 371 | NULL, |
| 372 | "POINTER" |
| 373 | }; |
| 374 | gint i; |
| 375 | auto settings = g_settings_get_child (app->settings->gobj (), GSM_SETTINGS_CHILD_PROCESSES); |
| 376 | |
| 377 | model = gtk_tree_store_new (NUM_COLUMNS, |
| 378 | G_TYPE_STRING, /* Process Name */ |
| 379 | G_TYPE_STRING, /* User */ |
| 380 | G_TYPE_UINT, /* Status */ |
| 381 | G_TYPE_ULONG, /* VM Size */ |
| 382 | G_TYPE_ULONG, /* Resident Memory */ |
| 383 | G_TYPE_ULONG, /* Writable Memory */ |
| 384 | G_TYPE_ULONG, /* Shared Memory */ |
| 385 | G_TYPE_ULONG, /* X Server Memory */ |
| 386 | G_TYPE_DOUBLE, /* % CPU */ |
| 387 | G_TYPE_UINT64, /* CPU time */ |
| 388 | G_TYPE_ULONG, /* Started */ |
| 389 | G_TYPE_INT, /* Nice */ |
| 390 | G_TYPE_UINT, /* ID */ |
| 391 | G_TYPE_STRING, /* Security Context */ |
| 392 | G_TYPE_STRING, /* Arguments */ |
| 393 | G_TYPE_ULONG, /* Memory */ |
| 394 | G_TYPE_STRING, /* wchan */ |
| 395 | G_TYPE_STRING, /* Cgroup */ |
| 396 | G_TYPE_STRING, /* Unit */ |
| 397 | G_TYPE_STRING, /* Session */ |
| 398 | G_TYPE_STRING, /* Seat */ |
| 399 | G_TYPE_STRING, /* Owner */ |
| 400 | G_TYPE_UINT64, /* Disk read total */ |
| 401 | G_TYPE_UINT64, /* Disk write total*/ |
| 402 | G_TYPE_UINT64, /* Disk read */ |
| 403 | G_TYPE_UINT64, /* Disk write */ |
| 404 | G_TYPE_STRING, /* Priority */ |
| 405 | GDK_TYPE_TEXTURE, /* Icon */ |
| 406 | G_TYPE_POINTER, /* ProcInfo */ |
| 407 | G_TYPE_STRING /* Sexy tooltip */ |
| 408 | ); |
| 409 | |
| 410 | model_filter = GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (model), NULL)); |
| 411 | gtk_tree_model_filter_set_visible_func (model_filter, process_visibility_func, app, NULL); |
| 412 | |
| 413 | model_sort = GTK_TREE_MODEL_SORT (gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (model_filter))); |
| 414 | |
| 415 | proctree = gsm_tree_view_new (settings, TRUE); |
| 416 | gtk_tree_view_set_model (GTK_TREE_VIEW (proctree), GTK_TREE_MODEL (model_sort)); |
| 417 | gtk_tree_view_set_tooltip_column (GTK_TREE_VIEW (proctree), COL_TOOLTIP); |
| 418 | gtk_tree_view_set_show_expanders (GTK_TREE_VIEW (proctree), app->settings->get_boolean (GSM_SETTING_SHOW_DEPENDENCIES)); |
| 419 | gtk_tree_view_set_enable_search (GTK_TREE_VIEW (proctree), FALSE); |
| 420 | g_object_unref (G_OBJECT (model)); |
| 421 | |
| 422 | column = gtk_tree_view_column_new (); |
| 423 | |
| 424 | cell_renderer = gtk_cell_renderer_pixbuf_new (); |
| 425 | gtk_tree_view_column_pack_start (column, cell_renderer, FALSE); |
| 426 | gtk_tree_view_column_set_attributes (column, cell_renderer, |
| 427 | "texture", COL_ICON, |
| 428 | NULL); |
| 429 | |
| 430 | cell_renderer = gtk_cell_renderer_text_new (); |
| 431 | gtk_tree_view_column_pack_start (column, cell_renderer, FALSE); |
| 432 | gtk_tree_view_column_set_attributes (column, cell_renderer, |
| 433 | "text", COL_NAME, |
| 434 | NULL); |
| 435 | gtk_tree_view_column_set_title (column, _(titles[0])); |
| 436 | |
| 437 | gtk_tree_view_column_set_sort_column_id (column, COL_NAME); |
| 438 | gtk_tree_view_column_set_resizable (column, TRUE); |
| 439 | gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED); |
| 440 | gtk_tree_view_column_set_min_width (column, 1); |
| 441 | gtk_tree_view_column_set_reorderable (column, TRUE); |
| 442 | |
| 443 | gsm_tree_view_append_and_bind_column (proctree, column); |
| 444 | gtk_tree_view_set_expander_column (GTK_TREE_VIEW (proctree), column); |
| 445 | |
| 446 | gtk_tree_view_column_set_expand (column, TRUE); |
| 447 | |
| 448 | for (i = COL_USER; i <= COL_PRIORITY; i++) |
| 449 | { |
| 450 | GtkWidget *box; |
| 451 | GtkWidget *title_label; |
| 452 | GtkWidget *value_label; |
| 453 | GtkTreeViewColumn *col; |
| 454 | GtkCellRenderer *cell; |
| 455 | PangoAttrList *attrs = NULL; |
| 456 | |
| 457 | if (i == COL_MEMWRITABLE) |
| 458 | continue; |
| 459 | |
| 460 | cell = gtk_cell_renderer_text_new (); |
| 461 | col = gtk_tree_view_column_new (); |
| 462 | gtk_tree_view_column_pack_start (col, cell, TRUE); |
| 463 | |
| 464 | gtk_tree_view_column_set_title (col, _(titles[i])); |
| 465 | |
| 466 | switch (i) { |
| 467 | case COL_CPU: |
| 468 | case COL_MEM: |
| 469 | case COL_VMSIZE: |
| 470 | case COL_MEMRES: |
| 471 | case COL_MEMWRITABLE: |
| 472 | case COL_MEMSHARED: |
| 473 | case COL_DISK_READ_TOTAL: |
| 474 | case COL_DISK_WRITE_TOTAL: |
| 475 | case COL_DISK_READ_CURRENT: |
| 476 | case COL_DISK_WRITE_CURRENT: |
| 477 | /* Insert a ‘total’ label */ |
| 478 | box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); |
| 479 | title_label = gtk_label_new (_(titles[i])); |
| 480 | value_label = gtk_label_new (""); |
| 481 | gtk_widget_add_css_class (value_label, "numeric"); |
| 482 | gtk_box_append (GTK_BOX (box), title_label); |
| 483 | gtk_box_append (GTK_BOX (box), value_label); |
| 484 | gtk_tree_view_column_set_widget (col, box); |
| 485 | break; |
| 486 | default: |
| 487 | /* Don't insert a second label */ |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | gtk_tree_view_column_set_resizable (col, TRUE); |
| 492 | gtk_tree_view_column_set_sort_column_id (col, i); |
| 493 | gtk_tree_view_column_set_reorderable (col, TRUE); |
| 494 | gsm_tree_view_append_and_bind_column (proctree, col); |
| 495 | |
| 496 | // type |
| 497 | switch (i) |
| 498 | { |
| 499 | case COL_VMSIZE: |
| 500 | case COL_MEMRES: |
| 501 | case COL_MEMSHARED: |
| 502 | case COL_MEM: |
| 503 | gtk_tree_view_column_set_cell_data_func (col, cell, |
| 504 | &procman::size_na_cell_data_func, |
| 505 | GUINT_TO_POINTER (i), |
| 506 | NULL); |
| 507 | break; |
| 508 | |
| 509 | case COL_CPU: |
| 510 | gtk_tree_view_column_set_cell_data_func (col, cell, |
| 511 | &procman::percentage_cell_data_func, |
| 512 | GUINT_TO_POINTER (i), |
| 513 | NULL); |
| 514 | break; |
| 515 | |
| 516 | case COL_CPU_TIME: |
| 517 | gtk_tree_view_column_set_cell_data_func (col, cell, |
| 518 | &procman::duration_cell_data_func, |
| 519 | GUINT_TO_POINTER (i), |
| 520 | NULL); |
| 521 | break; |
| 522 | |
| 523 | case COL_START_TIME: |
| 524 | gtk_tree_view_column_set_cell_data_func (col, cell, |
| 525 | &procman::time_cell_data_func, |
| 526 | GUINT_TO_POINTER (i), |
| 527 | NULL); |
| 528 | break; |
| 529 | |
| 530 | case COL_STATUS: |
| 531 | gtk_tree_view_column_set_cell_data_func (col, cell, |
| 532 | &procman::status_cell_data_func, |
| 533 | GUINT_TO_POINTER (i), |
| 534 | NULL); |
| 535 | break; |
| 536 | |
| 537 | case COL_DISK_READ_TOTAL: |
| 538 | case COL_DISK_WRITE_TOTAL: |
| 539 | gtk_tree_view_column_set_cell_data_func (col, cell, |
| 540 | &procman::size_na_cell_data_func, |
| 541 | GUINT_TO_POINTER (i), |
| 542 | NULL); |
| 543 | break; |
| 544 | |
| 545 | case COL_DISK_READ_CURRENT: |
| 546 | case COL_DISK_WRITE_CURRENT: |
| 547 | gtk_tree_view_column_set_cell_data_func (col, cell, |
| 548 | &procman::io_rate_cell_data_func, |
| 549 | GUINT_TO_POINTER (i), |
| 550 | NULL); |
| 551 | |
| 552 | break; |
| 553 | |
| 554 | case COL_PRIORITY: |
| 555 | gtk_tree_view_column_set_cell_data_func (col, cell, |
| 556 | &procman::priority_cell_data_func, |
| 557 | GUINT_TO_POINTER (COL_NICE), |
| 558 | NULL); |
| 559 | break; |
| 560 | |
| 561 | default: |
| 562 | gtk_tree_view_column_set_attributes (col, cell, "text", i, NULL); |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | // Tabular Numbers |
| 567 | switch (i) |
| 568 | { |
| 569 | case COL_PID: |
| 570 | case COL_VMSIZE: |
| 571 | case COL_MEMRES: |
| 572 | case COL_MEMSHARED: |
| 573 | case COL_MEM: |
| 574 | case COL_CPU: |
| 575 | case COL_CPU_TIME: |
| 576 | case COL_DISK_READ_TOTAL: |
| 577 | case COL_DISK_WRITE_TOTAL: |
| 578 | case COL_DISK_READ_CURRENT: |
| 579 | case COL_DISK_WRITE_CURRENT: |
| 580 | case COL_START_TIME: |
| 581 | case COL_NICE: |
| 582 | case COL_WCHAN: |
| 583 | attrs = make_tnum_attr_list (); |
| 584 | g_object_set (cell, "attributes", attrs, NULL); |
| 585 | g_clear_pointer (&attrs, pango_attr_list_unref); |
| 586 | break; |
| 587 | |
| 588 | default: |
| 589 | break; |
| 590 | } |
| 591 | |
| 592 | // sorting |
| 593 | switch (i) |
| 594 | { |
| 595 | case COL_VMSIZE: |
| 596 | case COL_MEMRES: |
| 597 | case COL_MEMSHARED: |
| 598 | case COL_MEM: |
| 599 | case COL_CPU: |
| 600 | case COL_CPU_TIME: |
| 601 | case COL_DISK_READ_TOTAL: |
| 602 | case COL_DISK_WRITE_TOTAL: |
| 603 | case COL_DISK_READ_CURRENT: |
| 604 | case COL_DISK_WRITE_CURRENT: |
| 605 | case COL_START_TIME: |
| 606 | gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (model_sort), i, |
| 607 | procman::number_compare_func, |
| 608 | GUINT_TO_POINTER (i), |
| 609 | NULL); |
| 610 | break; |
| 611 | |
| 612 | case COL_PRIORITY: |
| 613 | gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (model_sort), i, |
| 614 | procman::priority_compare_func, |
| 615 | GUINT_TO_POINTER (COL_NICE), NULL); |
| 616 | break; |
| 617 | |
| 618 | default: |
| 619 | break; |
| 620 | } |
| 621 | |
| 622 | // xalign |
| 623 | switch (i) |
| 624 | { |
| 625 | case COL_VMSIZE: |
| 626 | case COL_MEMRES: |
| 627 | case COL_MEMSHARED: |
| 628 | case COL_CPU: |
| 629 | case COL_NICE: |
| 630 | case COL_PID: |
| 631 | case COL_DISK_READ_TOTAL: |
| 632 | case COL_DISK_WRITE_TOTAL: |
| 633 | case COL_DISK_READ_CURRENT: |
| 634 | case COL_DISK_WRITE_CURRENT: |
| 635 | case COL_CPU_TIME: |
| 636 | case COL_MEM: |
| 637 | g_object_set (G_OBJECT (cell), "xalign", 1.0f, NULL); |
| 638 | break; |
| 639 | } |
| 640 | |
| 641 | gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED); |
| 642 | // sizing |
| 643 | switch (i) |
| 644 | { |
| 645 | case COL_ARGS: |
| 646 | gtk_tree_view_column_set_min_width (col, 150); |
| 647 | break; |
| 648 | |
| 649 | default: |
| 650 | gtk_tree_view_column_set_min_width (column, 20); |
| 651 | break; |
| 652 | } |
| 653 | |
| 654 | if (i == COL_ARGS) |
| 655 | gtk_tree_view_column_set_expand (col, TRUE); |
| 656 | else |
| 657 | gtk_tree_view_column_set_expand (col, FALSE); |
| 658 | } |
| 659 | app->tree = proctree; |
| 660 | app->top_of_tree = NULL; |
| 661 | app->last_vscroll_max = 0; |
| 662 | app->last_vscroll_value = 0; |
| 663 | |
| 664 | if (!gsm_cgroups_is_enabled ()) { |
| 665 | gsm_tree_view_add_excluded_column (proctree, COL_CGROUP); |
| 666 | } |
| 667 | |
| 668 | #ifdef HAVE_SYSTEMD |
| 669 | if (!gsm_systemd_is_running ()) { |
| 670 | #else |
| 671 | { |
| 672 | #endif |
| 673 | gsm_tree_view_add_excluded_column (proctree, COL_UNIT); |
| 674 | gsm_tree_view_add_excluded_column (proctree, COL_SESSION); |
| 675 | gsm_tree_view_add_excluded_column (proctree, COL_SEAT); |
| 676 | gsm_tree_view_add_excluded_column (proctree, COL_OWNER); |
| 677 | } |
| 678 | |
| 679 | if (!gsm_selinux_is_enabled ()) { |
| 680 | gsm_tree_view_add_excluded_column (proctree, COL_SECURITYCONTEXT); |
| 681 | } |
| 682 | |
| 683 | gsm_tree_view_load_state (proctree); |
| 684 | |
| 685 | GtkIconTheme*theme = gtk_icon_theme_get_for_display (gdk_display_get_default ()); |
| 686 | g_signal_connect (G_OBJECT (theme), "changed", G_CALLBACK (cb_refresh_icons), app); |
| 687 | |
| 688 | selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (proctree)); |
| 689 | gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE); |
| 690 | app->selection = selection; |
| 691 | |
| 692 | click_controller = gtk_gesture_click_new (); |
| 693 | gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (click_controller), GDK_BUTTON_SECONDARY); |
| 694 | gtk_widget_add_controller (GTK_WIDGET (proctree), GTK_EVENT_CONTROLLER (click_controller)); |
| 695 | |
| 696 | g_signal_connect (G_OBJECT (click_controller), "pressed", |
| 697 | G_CALLBACK (cb_tree_button_pressed), app); |
| 698 | |
| 699 | g_signal_connect (G_OBJECT (selection), |
| 700 | "changed", |
| 701 | G_CALLBACK (cb_row_selected), app); |
| 702 | |
| 703 | g_signal_connect (G_OBJECT (proctree), "destroy", |
| 704 | G_CALLBACK (cb_proctree_destroying), app); |
| 705 | |
| 706 | g_signal_connect (G_OBJECT (proctree), "columns-changed", |
| 707 | G_CALLBACK (cb_save_tree_state), app); |
| 708 | |
| 709 | g_signal_connect (G_OBJECT (model_sort), "sort-column-changed", |
| 710 | G_CALLBACK (cb_save_tree_state), app); |
| 711 | |
| 712 | app->settings->signal_changed (GSM_SETTING_SHOW_DEPENDENCIES).connect ([app](const Glib::ustring&key) { |
| 713 | cb_show_dependencies_changed (*app->settings.operator-> (), key, app); |
| 714 | }); |
| 715 | |
| 716 | app->settings->signal_changed (GSM_SETTING_SHOW_WHOSE_PROCESSES).connect ([app](const Glib::ustring&key) { |
| 717 | cb_show_whose_processes_changed (*app->settings.operator-> (), key, app); |
| 718 | }); |
| 719 | |
| 720 | return proctree; |
| 721 | } |
| 722 | |
| 723 | void |
| 724 | get_process_memory_writable (ProcInfo *info) |
| 725 | { |
| 726 | glibtop_proc_map buf; |
| 727 | glibtop_map_entry *maps; |
| 728 | |
| 729 | maps = glibtop_get_proc_map (&buf, info->pid); |
| 730 | |
| 731 | const bool use_private_dirty = buf.flags & (1 << GLIBTOP_MAP_ENTRY_PRIVATE_DIRTY); |
| 732 | |
| 733 | gulong memwritable = 0; |
| 734 | const unsigned number = buf.number; |
| 735 | |
| 736 | for (unsigned i = 0; i < number; ++i) |
| 737 | { |
| 738 | if (use_private_dirty) |
| 739 | // clang++ 3.4 is not smart enough to move this invariant out of the loop |
| 740 | // but who cares ? |
| 741 | memwritable += maps[i].private_dirty; |
| 742 | else if (maps[i].perm & GLIBTOP_MAP_PERM_WRITE) |
| 743 | memwritable += maps[i].size; |
| 744 | } |
| 745 | |
| 746 | info->memwritable = memwritable; |
| 747 | |
| 748 | g_free (maps); |
| 749 | } |
| 750 | |
| 751 | static void |
| 752 | get_process_memory_info (ProcInfo *info) |
| 753 | { |
| 754 | glibtop_proc_mem procmem; |
| 755 | |
| 756 | glibtop_get_proc_mem (&procmem, info->pid); |
| 757 | |
| 758 | info->vmsize = procmem.vsize; |
| 759 | info->memres = procmem.resident; |
| 760 | info->memshared = procmem.share; |
| 761 | |
| 762 | info->mem = info->memres - info->memshared; |
| 763 | } |
| 764 | |
| 765 | static void |
| 766 | update_info_mutable_cols (ProcInfo *info) |
| 767 | { |
| 768 | GtkTreeModel *model; |
| 769 | |
| 770 | model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER ( |
| 771 | gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT ( |
| 772 | gtk_tree_view_get_model (GTK_TREE_VIEW (GsmApplication::get ().tree)))))); |
| 773 | |
| 774 | using procman::tree_store_update; |
| 775 | |
| 776 | tree_store_update (model, &info->node, COL_STATUS, info->status); |
| 777 | tree_store_update (model, &info->node, COL_USER, info->user.c_str ()); |
| 778 | tree_store_update (model, &info->node, COL_VMSIZE, info->vmsize); |
| 779 | tree_store_update (model, &info->node, COL_MEMRES, info->memres); |
| 780 | tree_store_update (model, &info->node, COL_MEMSHARED, info->memshared); |
| 781 | tree_store_update (model, &info->node, COL_CPU, info->pcpu); |
| 782 | tree_store_update (model, &info->node, COL_CPU_TIME, info->cpu_time); |
| 783 | tree_store_update (model, &info->node, COL_DISK_READ_TOTAL, info->disk_read_bytes_total); |
| 784 | tree_store_update (model, &info->node, COL_DISK_WRITE_TOTAL, info->disk_write_bytes_total); |
| 785 | tree_store_update (model, &info->node, COL_DISK_READ_CURRENT, info->disk_read_bytes_current); |
| 786 | tree_store_update (model, &info->node, COL_DISK_WRITE_CURRENT, info->disk_write_bytes_current); |
| 787 | tree_store_update (model, &info->node, COL_START_TIME, info->start_time); |
| 788 | tree_store_update (model, &info->node, COL_NICE, info->nice); |
| 789 | tree_store_update (model, &info->node, COL_MEM, info->mem); |
| 790 | tree_store_update (model, &info->node, COL_WCHAN, info->wchan.c_str ()); |
| 791 | tree_store_update (model, &info->node, COL_CGROUP, info->cgroup_name.c_str ()); |
| 792 | tree_store_update (model, &info->node, COL_UNIT, info->unit.c_str ()); |
| 793 | tree_store_update (model, &info->node, COL_SESSION, info->session.c_str ()); |
| 794 | tree_store_update (model, &info->node, COL_SEAT, info->seat.c_str ()); |
| 795 | tree_store_update (model, &info->node, COL_OWNER, info->owner.c_str ()); |
| 796 | } |
| 797 | |
| 798 | static void |
| 799 | insert_info_to_tree (ProcInfo *info, |
| 800 | GsmApplication *app, |
| 801 | bool forced = false) |
| 802 | { |
| 803 | GtkTreeModel *model; |
| 804 | GtkTreeModel *filtered; |
| 805 | GtkTreeModel *sorted; |
| 806 | |
| 807 | sorted = gtk_tree_view_get_model (GTK_TREE_VIEW (app->tree)); |
| 808 | filtered = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sorted)); |
| 809 | model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filtered)); |
| 810 | |
| 811 | if (app->settings->get_boolean (GSM_SETTING_SHOW_DEPENDENCIES)) |
| 812 | { |
| 813 | ProcInfo *parent = 0; |
| 814 | |
| 815 | if (not forced) |
| 816 | parent = app->processes.find (info->ppid); |
| 817 | |
| 818 | if (parent) |
| 819 | { |
| 820 | GtkTreePath *parent_node = gtk_tree_model_get_path (model, &parent->node); |
| 821 | gtk_tree_store_insert (GTK_TREE_STORE (model), &info->node, &parent->node, 0); |
| 822 | |
| 823 | GtkTreePath *filtered_parent = gtk_tree_model_filter_convert_child_path_to_path (GTK_TREE_MODEL_FILTER (filtered), parent_node); |
| 824 | if (filtered_parent != NULL) |
| 825 | { |
| 826 | GtkTreePath *sorted_parent = gtk_tree_model_sort_convert_child_path_to_path (GTK_TREE_MODEL_SORT (sorted), filtered_parent); |
| 827 | |
| 828 | if (sorted_parent != NULL) |
| 829 | { |
| 830 | if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (app->tree), sorted_parent) |
| 831 | #ifdef __linux__ |
| 832 | // on linuxes we don't want to expand kthreadd by default (always has pid 2) |
| 833 | && (parent->pid != 2) |
| 834 | #endif |
| 835 | ) |
| 836 | gtk_tree_view_expand_row (GTK_TREE_VIEW (app->tree), sorted_parent, FALSE); |
| 837 | gtk_tree_path_free (sorted_parent); |
| 838 | } |
| 839 | gtk_tree_path_free (filtered_parent); |
| 840 | } |
| 841 | gtk_tree_path_free (parent_node); |
| 842 | } |
| 843 | else |
| 844 | { |
| 845 | gtk_tree_store_insert (GTK_TREE_STORE (model), &info->node, NULL, 0); |
| 846 | } |
| 847 | } |
| 848 | else |
| 849 | { |
| 850 | gtk_tree_store_insert (GTK_TREE_STORE (model), &info->node, NULL, 0); |
| 851 | } |
| 852 | |
| 853 | gtk_tree_store_set (GTK_TREE_STORE (model), &info->node, |
| 854 | COL_POINTER, info, |
| 855 | COL_NAME, info->name.c_str (), |
| 856 | COL_ARGS, info->arguments.c_str (), |
| 857 | COL_TOOLTIP, info->tooltip.c_str (), |
| 858 | COL_PID, info->pid, |
| 859 | COL_SECURITYCONTEXT, info->security_context.c_str (), |
| 860 | -1); |
| 861 | |
| 862 | app->pretty_table->set_icon (*info); |
| 863 | |
| 864 | procman_debug ("inserted %d%s", info->pid, (forced ? " (forced)" : "")); |
| 865 | } |
| 866 | |
| 867 | /* Removing a node with children - make sure the children are queued |
| 868 | ** to be readded. |
| 869 | */ |
| 870 | template<typename List> |
| 871 | static void |
| 872 | remove_info_from_tree (GsmApplication *app, |
| 873 | GtkTreeModel *model, |
| 874 | ProcInfo& current, |
| 875 | List & orphans, |
| 876 | unsigned lvl = 0) |
| 877 | { |
| 878 | GtkTreeIter child_node; |
| 879 | |
| 880 | if (std::find (orphans.begin (), orphans.end (), ¤t) != orphans.end ()) |
| 881 | { |
| 882 | procman_debug ("[%u] %d already removed from tree", lvl, int(current.pid)); |
| 883 | return; |
| 884 | } |
| 885 | |
| 886 | procman_debug ("[%u] pid %d, %d children", lvl, int(current.pid), |
| 887 | gtk_tree_model_iter_n_children (model, ¤t.node)); |
| 888 | |
| 889 | // it is not possible to iterate&erase over a treeview so instead we |
| 890 | // just pop one child after another and recursively remove it and |
| 891 | // its children |
| 892 | |
| 893 | while (gtk_tree_model_iter_children (model, &child_node, ¤t.node)) |
| 894 | { |
| 895 | ProcInfo *child = 0; |
| 896 | gtk_tree_model_get (model, &child_node, COL_POINTER, &child, -1); |
| 897 | remove_info_from_tree (app, model, *child, orphans, lvl + 1); |
| 898 | } |
| 899 | |
| 900 | g_assert (not gtk_tree_model_iter_has_child (model, ¤t.node)); |
| 901 | |
| 902 | orphans.push_back (¤t); |
| 903 | gtk_tree_store_remove (GTK_TREE_STORE (model), ¤t.node); |
| 904 | procman::poison (current.node, 0x69); |
| 905 | } |
| 906 | |
| 907 | |
| 908 | static std::string |
| 909 | get_proc_kernel_wchan (glibtop_proc_kernel&obj) |
| 910 | { |
| 911 | char buf[40] = { 0 }; |
| 912 | |
| 913 | g_strlcpy (buf, obj.wchan, sizeof(buf)); |
| 914 | buf[sizeof(buf) - 1] = '\0'; |
| 915 | return buf; |
| 916 | } |
| 917 | |
| 918 | static void |
| 919 | update_info (GsmApplication *app, |
| 920 | ProcInfo *info) |
| 921 | { |
| 922 | glibtop_proc_state procstate; |
| 923 | glibtop_proc_uid procuid; |
| 924 | glibtop_proc_time proctime; |
| 925 | glibtop_proc_kernel prockernel; |
| 926 | glibtop_proc_io procio; |
| 927 | gdouble update_interval_seconds = app->config.update_interval / 1000; |
| 928 | |
| 929 | glibtop_get_proc_kernel (&prockernel, info->pid); |
| 930 | info->wchan = get_proc_kernel_wchan (prockernel); |
| 931 | |
| 932 | glibtop_get_proc_state (&procstate, info->pid); |
| 933 | info->status = procstate.state; |
| 934 | |
| 935 | glibtop_get_proc_uid (&procuid, info->pid); |
| 936 | glibtop_get_proc_time (&proctime, info->pid); |
| 937 | glibtop_get_proc_io (&procio, info->pid); |
| 938 | |
| 939 | get_process_memory_info (info); |
| 940 | |
| 941 | info->set_user (procstate.uid); |
| 942 | |
| 943 | // if the cpu time has increased reset the status to running |
| 944 | // regardless of kernel state (#606579) |
| 945 | guint64 difference = proctime.rtime - info->cpu_time; |
| 946 | |
| 947 | if (difference > 0) |
| 948 | info->status = GLIBTOP_PROCESS_RUNNING; |
| 949 | |
| 950 | guint cpu_scale = 100; |
| 951 | |
| 952 | if (not app->config.solaris_mode) |
| 953 | cpu_scale *= app->config.num_cpus; |
| 954 | |
| 955 | info->pcpu = (gdouble)difference * cpu_scale / app->cpu_total_time; |
| 956 | info->pcpu = MIN (info->pcpu, cpu_scale); |
| 957 | |
| 958 | app->processes.cpu_times[info->pid] = info->cpu_time = proctime.rtime; |
| 959 | info->nice = procuid.nice; |
| 960 | |
| 961 | info->disk_write_bytes_current = (procio.disk_wbytes - info->disk_write_bytes_total) / update_interval_seconds; |
| 962 | info->disk_read_bytes_current = (procio.disk_rbytes - info->disk_read_bytes_total) / update_interval_seconds; |
| 963 | |
| 964 | info->disk_write_bytes_total = procio.disk_wbytes; |
| 965 | info->disk_read_bytes_total = procio.disk_rbytes; |
| 966 | |
| 967 | // set the ppid only if one can exist |
| 968 | // i.e. pid=0 can never have a parent |
| 969 | if (info->pid > 0) |
| 970 | info->ppid = procuid.ppid; |
| 971 | |
| 972 | g_assert (info->pid != info->ppid); |
| 973 | g_assert (info->ppid != -1 || info->pid == 0); |
| 974 | |
| 975 | gsm_proc_info_load_cgroups (info); |
| 976 | gsm_proc_info_load_systemd (info); |
| 977 | } |
| 978 | |
| 979 | |
| 980 | void |
| 981 | proctable_refresh_summary_headers(GsmApplication * app) |
| 982 | { |
| 983 | GtkTreeIter iter; |
| 984 | GList *columns, *it; |
| 985 | double total_cpu = 0; |
| 986 | uint64_t total_mem = 0; |
| 987 | uint64_t total_memres = 0; |
| 988 | uint64_t total_vmsize = 0; |
| 989 | uint64_t total_memshared = 0; |
| 990 | uint64_t total_memwritable = 0; |
| 991 | uint64_t total_disk_read_bytes_total = 0; |
| 992 | uint64_t total_disk_write_bytes_total = 0; |
| 993 | uint64_t total_disk_read_bytes_current = 0; |
| 994 | uint64_t total_disk_write_bytes_current = 0; |
| 995 | std::function<void(GtkTreeIter&)> calc_summary; |
| 996 | GtkTreeView *treeview = GTK_TREE_VIEW (app->tree); |
| 997 | GtkTreeModel *model = GTK_TREE_MODEL( |
| 998 | GTK_TREE_MODEL_FILTER ( |
| 999 | gtk_tree_model_sort_get_model ( |
| 1000 | GTK_TREE_MODEL_SORT ( |
| 1001 | gtk_tree_view_get_model ( |
| 1002 | treeview |
| 1003 | ) |
| 1004 | ) |
| 1005 | ) |
| 1006 | ) |
| 1007 | ); |
| 1008 | |
| 1009 | // Helper: convert bytes to readable units |
| 1010 | auto format_bytes = [app](uint64_t bytes) -> std::string |
| 1011 | { |
| 1012 | const GFormatSizeFlags flags = |
| 1013 | app->config.process_memory_in_iec ? G_FORMAT_SIZE_IEC_UNITS : G_FORMAT_SIZE_DEFAULT; |
| 1014 | |
| 1015 | return make_string (g_format_size_full (bytes, flags)); |
| 1016 | }; |
| 1017 | |
| 1018 | // Helper: get second label from column header |
| 1019 | auto get_value_label = [](GtkTreeViewColumn* col) -> GtkLabel * |
| 1020 | { |
| 1021 | GtkWidget *box = gtk_tree_view_column_get_widget(col); |
| 1022 | if (!GTK_IS_BOX(box)) |
| 1023 | return nullptr; |
| 1024 | |
| 1025 | GtkWidget *child = gtk_widget_get_first_child(box); |
| 1026 | if (!child) |
| 1027 | return nullptr; |
| 1028 | |
| 1029 | GtkWidget *second_child = gtk_widget_get_next_sibling(child); |
| 1030 | if (GTK_IS_LABEL(second_child)) |
| 1031 | { |
| 1032 | return GTK_LABEL(second_child); |
| 1033 | } |
| 1034 | |
| 1035 | return nullptr; |
| 1036 | }; |
| 1037 | |
| 1038 | // Accumulate totals |
| 1039 | calc_summary = [&model, |
| 1040 | &total_mem, |
| 1041 | &total_cpu, |
| 1042 | &total_vmsize, |
| 1043 | &total_memres, |
| 1044 | &total_memshared, |
| 1045 | &total_memwritable, |
| 1046 | &total_disk_read_bytes_total, |
| 1047 | &total_disk_write_bytes_total, |
| 1048 | &total_disk_read_bytes_current, |
| 1049 | &total_disk_write_bytes_current, |
| 1050 | &calc_summary](GtkTreeIter &iter) |
| 1051 | { |
| 1052 | GtkTreeIter child_iter; |
| 1053 | // take out value |
| 1054 | gpointer node_ptr; |
| 1055 | gtk_tree_model_get(model, &iter, COL_POINTER, &node_ptr, -1); |
| 1056 | |
| 1057 | ProcInfo *proc = static_cast<ProcInfo *>(node_ptr); |
| 1058 | if (!proc) |
| 1059 | return; |
| 1060 | |
| 1061 | total_mem += proc->mem; |
| 1062 | total_cpu += round(proc->pcpu * 10.0f) / 10.0f; |
| 1063 | total_memres += proc->memres; |
| 1064 | total_vmsize += proc->vmsize; |
| 1065 | total_memshared += proc->memshared; |
| 1066 | total_memwritable += proc->memwritable; |
| 1067 | total_disk_read_bytes_total += proc->disk_read_bytes_total; |
| 1068 | total_disk_write_bytes_total += proc->disk_write_bytes_total; |
| 1069 | total_disk_read_bytes_current += proc->disk_read_bytes_current; |
| 1070 | total_disk_write_bytes_current += proc->disk_write_bytes_current; |
| 1071 | |
| 1072 | // child |
| 1073 | if (gtk_tree_model_iter_has_child(model, &iter)) { |
| 1074 | gtk_tree_model_iter_children(model, &child_iter, &iter); |
| 1075 | calc_summary(child_iter); |
| 1076 | } |
| 1077 | |
| 1078 | // next sibling |
| 1079 | // recerssive check |
| 1080 | gboolean valid = gtk_tree_model_iter_next(model, &iter); |
| 1081 | if (!valid) return; |
| 1082 | else calc_summary(iter); |
| 1083 | }; |
| 1084 | |
| 1085 | |
| 1086 | gboolean valid = gtk_tree_model_get_iter_first(model, &iter); |
| 1087 | if(valid) calc_summary(iter); |
| 1088 | |
| 1089 | columns = gtk_tree_view_get_columns (GTK_TREE_VIEW (treeview)); |
| 1090 | |
| 1091 | for (it = columns; it; it = it->next) |
| 1092 | { |
| 1093 | std::string v_str; |
| 1094 | GtkTreeViewColumn *column = static_cast<GtkTreeViewColumn *>(it->data); |
| 1095 | GtkLabel *label = get_value_label(column); |
| 1096 | |
| 1097 | /* Not every column has a header */ |
| 1098 | if (!label) { |
| 1099 | continue; |
| 1100 | } |
| 1101 | |
| 1102 | gint column_id = gtk_tree_view_column_get_sort_column_id(column); |
| 1103 | switch (column_id) |
| 1104 | { |
| 1105 | case COL_CPU: |
| 1106 | v_str = make_string (g_strdup_printf ("%.01f%%", total_cpu)); |
| 1107 | break; |
| 1108 | case COL_MEM: |
| 1109 | v_str = format_bytes(total_mem); |
| 1110 | break; |
| 1111 | case COL_VMSIZE: |
| 1112 | v_str = format_bytes(total_vmsize); |
| 1113 | break; |
| 1114 | case COL_MEMRES: |
| 1115 | v_str = format_bytes(total_memres); |
| 1116 | break; |
| 1117 | case COL_MEMWRITABLE: |
| 1118 | v_str = format_bytes(total_memwritable); |
| 1119 | break; |
| 1120 | case COL_MEMSHARED: |
| 1121 | v_str = format_bytes(total_memshared); |
| 1122 | break; |
| 1123 | case COL_DISK_READ_TOTAL: |
| 1124 | v_str = format_bytes(total_disk_read_bytes_total); |
| 1125 | break; |
| 1126 | case COL_DISK_WRITE_TOTAL: |
| 1127 | v_str = format_bytes(total_disk_write_bytes_total); |
| 1128 | break; |
| 1129 | case COL_DISK_READ_CURRENT: |
| 1130 | v_str = procman::format_rate(total_disk_read_bytes_current, false); |
| 1131 | break; |
| 1132 | case COL_DISK_WRITE_CURRENT: |
| 1133 | v_str = procman::format_rate(total_disk_write_bytes_current, false); |
| 1134 | break; |
| 1135 | default: |
| 1136 | v_str = ""; |
| 1137 | } |
| 1138 | gtk_label_set_text(label, v_str.c_str()); |
| 1139 | } |
| 1140 | g_list_free(columns); |
| 1141 | } |
| 1142 | |
| 1143 | static void |
| 1144 | refresh_list (GsmApplication *app, |
| 1145 | const pid_t *pid_list, |
| 1146 | const guint n) |
| 1147 | { |
| 1148 | typedef std::list<ProcInfo*> ProcList; |
| 1149 | ProcList addition; |
| 1150 | |
| 1151 | GtkTreeModel *model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER ( |
| 1152 | gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT ( |
| 1153 | gtk_tree_view_get_model (GTK_TREE_VIEW (app->tree)))))); |
| 1154 | guint i; |
| 1155 | |
| 1156 | // Add or update processes in the process list |
| 1157 | for (i = 0; i < n; ++i) |
| 1158 | { |
| 1159 | ProcInfo *info = app->processes.find (pid_list[i]); |
| 1160 | |
| 1161 | if (!info) |
| 1162 | { |
| 1163 | info = app->processes.add (pid_list[i]); |
| 1164 | addition.push_back (info); |
| 1165 | } |
| 1166 | |
| 1167 | update_info (app, info); |
| 1168 | } |
| 1169 | |
| 1170 | |
| 1171 | // Remove dead processes from the process list and from the |
| 1172 | // tree. children are queued to be readded at the right place |
| 1173 | // in the tree. |
| 1174 | |
| 1175 | const std::set<pid_t> pids (pid_list, pid_list + n); |
| 1176 | |
| 1177 | auto it = std::begin (app->processes); |
| 1178 | |
| 1179 | while (it != std::end (app->processes)) |
| 1180 | { |
| 1181 | auto&info = it->second; |
| 1182 | if (pids.find (info.pid) == pids.end ()) |
| 1183 | { |
| 1184 | procman_debug ("ripping %d", info.pid); |
| 1185 | remove_info_from_tree (app, model, info, addition); |
| 1186 | addition.remove (&info); |
| 1187 | it = app->processes.erase (it); |
| 1188 | } |
| 1189 | else |
| 1190 | { |
| 1191 | ++it; |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | // INVARIANT |
| 1196 | // pid_list == ProcInfo::all + addition |
| 1197 | |
| 1198 | |
| 1199 | if (app->settings->get_boolean (GSM_SETTING_SHOW_DEPENDENCIES)) |
| 1200 | { |
| 1201 | // insert process in the tree. walk through the addition list |
| 1202 | // (new process + process that have a new parent). This loop |
| 1203 | // handles the dependencies because we cannot insert a process |
| 1204 | // until its parent is in the tree. |
| 1205 | |
| 1206 | std::set<pid_t> in_tree (pids); |
| 1207 | |
| 1208 | for (ProcList::iterator it (addition.begin ()); it != addition.end (); ++it) |
| 1209 | in_tree.erase ((*it)->pid); |
| 1210 | |
| 1211 | |
| 1212 | while (not addition.empty ()) |
| 1213 | { |
| 1214 | procman_debug ("looking for %d parents", int(addition.size ())); |
| 1215 | ProcList::iterator it (addition.begin ()); |
| 1216 | |
| 1217 | while (it != addition.end ()) |
| 1218 | { |
| 1219 | procman_debug ("looking for %d's parent with ppid %d", |
| 1220 | int((*it)->pid), int((*it)->ppid)); |
| 1221 | |
| 1222 | |
| 1223 | // inserts the process in the treeview if : |
| 1224 | // - it has no parent (ppid = -1), |
| 1225 | // ie it is for example the [kernel] on FreeBSD |
| 1226 | // - it is init |
| 1227 | // - its parent is already in tree |
| 1228 | // - its parent is unreachable |
| 1229 | // |
| 1230 | // rounds == 2 means that addition contains processes with |
| 1231 | // unreachable parents |
| 1232 | // |
| 1233 | // FIXME: this is broken if the unreachable parent becomes active |
| 1234 | // i.e. it gets active or changes ower |
| 1235 | // so we just clear the tree on __each__ update |
| 1236 | // see proctable_update (ProcData * const procdata) |
| 1237 | |
| 1238 | |
| 1239 | if ((*it)->ppid <= 0 or in_tree.find ((*it)->ppid) != in_tree.end ()) |
| 1240 | { |
| 1241 | insert_info_to_tree (*it, app); |
| 1242 | in_tree.insert ((*it)->pid); |
| 1243 | it = addition.erase (it); |
| 1244 | continue; |
| 1245 | } |
| 1246 | |
| 1247 | ProcInfo *parent = app->processes.find ((*it)->ppid); |
| 1248 | // if the parent is unreachable |
| 1249 | if (not parent) |
| 1250 | { |
| 1251 | // or std::find(addition.begin(), addition.end(), parent) == addition.end()) { |
| 1252 | insert_info_to_tree (*it, app, true); |
| 1253 | in_tree.insert ((*it)->pid); |
| 1254 | it = addition.erase (it); |
| 1255 | continue; |
| 1256 | } |
| 1257 | |
| 1258 | ++it; |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | else |
| 1263 | { |
| 1264 | // don't care of the tree |
| 1265 | for (auto&v : addition) |
| 1266 | insert_info_to_tree (v, app); |
| 1267 | } |
| 1268 | |
| 1269 | // update Header |
| 1270 | proctable_refresh_summary_headers(app); |
| 1271 | |
| 1272 | for (auto&v : app->processes) |
| 1273 | update_info_mutable_cols (&v.second); |
| 1274 | } |
| 1275 | |
| 1276 | void |
| 1277 | proctable_update (GsmApplication *app) |
| 1278 | { |
| 1279 | pid_t*pid_list; |
| 1280 | glibtop_proclist proclist; |
| 1281 | glibtop_cpu cpu; |
| 1282 | int which = 0; |
| 1283 | int arg = 0; |
| 1284 | auto whose_processes = app->settings->get_string (GSM_SETTING_SHOW_WHOSE_PROCESSES); |
| 1285 | |
| 1286 | if (whose_processes == "all") |
| 1287 | { |
| 1288 | which = GLIBTOP_KERN_PROC_ALL; |
| 1289 | arg = 0; |
| 1290 | } |
| 1291 | else if (whose_processes == "active") |
| 1292 | { |
| 1293 | which = GLIBTOP_KERN_PROC_ALL | GLIBTOP_EXCLUDE_IDLE; |
| 1294 | arg = 0; |
| 1295 | } |
| 1296 | else if (whose_processes == "user") |
| 1297 | { |
| 1298 | which = GLIBTOP_KERN_PROC_UID; |
| 1299 | arg = getuid (); |
| 1300 | } |
| 1301 | |
| 1302 | pid_list = glibtop_get_proclist (&proclist, which, arg); |
| 1303 | |
| 1304 | /* FIXME: total cpu time elapsed should be calculated on an individual basis here |
| 1305 | ** should probably have a total_time_last gint in the ProcInfo structure */ |
| 1306 | glibtop_get_cpu (&cpu); |
| 1307 | app->cpu_total_time = MAX (cpu.total - app->cpu_total_time_last, 1); |
| 1308 | app->cpu_total_time_last = cpu.total; |
| 1309 | |
| 1310 | // FIXME: not sure if glibtop always returns a sorted list of pid |
| 1311 | // but it is important otherwise refresh_list won't find the parent |
| 1312 | std::sort (pid_list, pid_list + proclist.number); |
| 1313 | refresh_list (app, pid_list, proclist.number); |
| 1314 | |
| 1315 | // juggling with tree scroll position to fix https://bugzilla.gnome.org/show_bug.cgi?id=92724 |
| 1316 | GtkTreePath*current_top; |
| 1317 | |
| 1318 | if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (app->tree), 0, 0, ¤t_top, NULL, NULL, NULL)) |
| 1319 | { |
| 1320 | GtkAdjustment *vadjustment = GTK_ADJUSTMENT (gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (app->tree))); |
| 1321 | gdouble current_max = gtk_adjustment_get_upper (vadjustment); |
| 1322 | gdouble current_value = gtk_adjustment_get_value (vadjustment); |
| 1323 | |
| 1324 | if (app->top_of_tree) |
| 1325 | { |
| 1326 | // if the visible cell from the top of the tree is still the same, as last time |
| 1327 | if (gtk_tree_path_compare (app->top_of_tree, current_top) == 0) |
| 1328 | { |
| 1329 | //but something from the scroll parameters has changed compared to the last values |
| 1330 | if (app->last_vscroll_value == 0 && current_value != 0) |
| 1331 | // the tree was scrolled to top, and something has been added above the current top row |
| 1332 | gtk_tree_view_scroll_to_point (GTK_TREE_VIEW (app->tree), -1, 0); |
| 1333 | else if (current_max > app->last_vscroll_max && app->last_vscroll_max == app->last_vscroll_value) |
| 1334 | // the tree was scrolled to bottom, something has been added below the current bottom row |
| 1335 | gtk_tree_view_scroll_to_point (GTK_TREE_VIEW (app->tree), -1, current_max); |
| 1336 | } |
| 1337 | |
| 1338 | gtk_tree_path_free (app->top_of_tree); |
| 1339 | } |
| 1340 | |
| 1341 | app->top_of_tree = current_top; |
| 1342 | app->last_vscroll_value = current_value; |
| 1343 | app->last_vscroll_max = current_max; |
| 1344 | } |
| 1345 | |
| 1346 | g_free (pid_list); |
| 1347 | |
| 1348 | /* proclist.number == g_list_length(procdata->info) == g_hash_table_size(procdata->pids) */ |
| 1349 | } |
| 1350 | |
| 1351 | void |
| 1352 | proctable_free_table (GsmApplication * const app) |
| 1353 | { |
| 1354 | app->processes.clear (); |
| 1355 | } |
| 1356 | |
| 1357 | void |
| 1358 | proctable_freeze (GsmApplication *app) |
| 1359 | { |
| 1360 | if (app->timeout) |
| 1361 | { |
| 1362 | g_source_remove (app->timeout); |
| 1363 | app->timeout = 0; |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | void |
| 1368 | proctable_thaw (GsmApplication *app) |
| 1369 | { |
| 1370 | if (app->timeout) |
| 1371 | return; |
| 1372 | |
| 1373 | app->timeout = g_timeout_add (app->config.update_interval, |
| 1374 | cb_timeout, |
| 1375 | app); |
| 1376 | } |
| 1377 | |
| 1378 | void |
| 1379 | proctable_reset_timeout (GsmApplication *app) |
| 1380 | { |
| 1381 | proctable_freeze (app); |
| 1382 | proctable_thaw (app); |
| 1383 | } |