| 1 | /* This file is part of the Linux Trace Toolkit viewer |
| 2 | * Copyright (C) 2003-2004 Mathieu Desnoyers |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License Version 2 as |
| 6 | * published by the Free Software Foundation; |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 16 | * MA 02110-1301, USA. |
| 17 | */ |
| 18 | |
| 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
| 21 | #endif |
| 22 | |
| 23 | #include <gtk/gtk.h> |
| 24 | #include <gdk/gdk.h> |
| 25 | #include <glib.h> |
| 26 | #include <string.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <math.h> |
| 29 | |
| 30 | #include "processlist.h" |
| 31 | #include "drawing.h" |
| 32 | #include "drawitem.h" |
| 33 | |
| 34 | #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format) |
| 35 | |
| 36 | /* Preallocated Size of the index_to_pixmap array */ |
| 37 | #define ALLOCATE_PROCESSES 1000 |
| 38 | |
| 39 | /***************************************************************************** |
| 40 | * Methods to synchronize process list * |
| 41 | *****************************************************************************/ |
| 42 | |
| 43 | |
| 44 | gint process_sort_func ( GtkTreeModel *model, |
| 45 | GtkTreeIter *it_a, |
| 46 | GtkTreeIter *it_b, |
| 47 | gpointer user_data) |
| 48 | { |
| 49 | gchar *a_name; |
| 50 | guint a_pid, a_tgid, a_ppid, a_cpu; |
| 51 | gulong a_birth_s, a_birth_ns; |
| 52 | guint a_trace; |
| 53 | |
| 54 | gchar *b_name; |
| 55 | guint b_pid, b_tgid, b_ppid, b_cpu; |
| 56 | gulong b_birth_s, b_birth_ns; |
| 57 | guint b_trace; |
| 58 | |
| 59 | gtk_tree_model_get(model, |
| 60 | it_a, |
| 61 | PROCESS_COLUMN, &a_name, |
| 62 | PID_COLUMN, &a_pid, |
| 63 | TGID_COLUMN, &a_tgid, |
| 64 | PPID_COLUMN, &a_ppid, |
| 65 | CPU_COLUMN, &a_cpu, |
| 66 | BIRTH_S_COLUMN, &a_birth_s, |
| 67 | BIRTH_NS_COLUMN, &a_birth_ns, |
| 68 | TRACE_COLUMN, &a_trace, |
| 69 | -1); |
| 70 | |
| 71 | gtk_tree_model_get(model, |
| 72 | it_b, |
| 73 | PROCESS_COLUMN, &b_name, |
| 74 | PID_COLUMN, &b_pid, |
| 75 | TGID_COLUMN, &b_tgid, |
| 76 | PPID_COLUMN, &b_ppid, |
| 77 | CPU_COLUMN, &b_cpu, |
| 78 | BIRTH_S_COLUMN, &b_birth_s, |
| 79 | BIRTH_NS_COLUMN, &b_birth_ns, |
| 80 | TRACE_COLUMN, &b_trace, |
| 81 | -1); |
| 82 | |
| 83 | |
| 84 | /* Order by PID */ |
| 85 | if(a_pid == 0 && b_pid == 0) { |
| 86 | /* If 0, order by CPU */ |
| 87 | if(a_cpu > b_cpu) return 1; |
| 88 | if(a_cpu < b_cpu) return -1; |
| 89 | |
| 90 | } else { /* if not 0, order by pid */ |
| 91 | |
| 92 | if(a_pid > b_pid) return 1; |
| 93 | if(a_pid < b_pid) return -1; |
| 94 | } |
| 95 | |
| 96 | /* Order by birth second */ |
| 97 | |
| 98 | if(a_birth_s > b_birth_s) return 1; |
| 99 | if(a_birth_s < b_birth_s) return -1; |
| 100 | |
| 101 | |
| 102 | /* Order by birth nanosecond */ |
| 103 | if(a_birth_ns > b_birth_ns) return 1; |
| 104 | if(a_birth_ns < b_birth_ns) return -1; |
| 105 | |
| 106 | /* Order by trace_num */ |
| 107 | if(a_trace > b_trace) return 1; |
| 108 | if(a_trace < b_trace) return -1; |
| 109 | |
| 110 | return 0; |
| 111 | |
| 112 | } |
| 113 | |
| 114 | static guint process_list_hash_fct(gconstpointer key) |
| 115 | { |
| 116 | guint pid = ((const ProcessInfo*)key)->pid; |
| 117 | return ((pid>>8 ^ pid>>4 ^ pid>>2 ^ pid) ^ ((const ProcessInfo*)key)->cpu); |
| 118 | } |
| 119 | |
| 120 | /* If hash is good, should be different */ |
| 121 | static gboolean process_list_equ_fct(gconstpointer a, gconstpointer b) |
| 122 | { |
| 123 | const ProcessInfo *pa = (const ProcessInfo*)a; |
| 124 | const ProcessInfo *pb = (const ProcessInfo*)b; |
| 125 | |
| 126 | gboolean ret = TRUE; |
| 127 | |
| 128 | if(likely(pa->pid != pb->pid)) |
| 129 | ret = FALSE; |
| 130 | if(likely((pa->pid == 0 && (pa->cpu != pb->cpu)))) |
| 131 | ret = FALSE; |
| 132 | //TODO ybrosseau 2012-09-18 Check if we want to reenable birth comparision |
| 133 | // We currently do not have valid birth info, so don't compare it |
| 134 | /* if(unlikely(ltt_time_compare(pa->birth, pb->birth) != 0)) |
| 135 | ret = FALSE;*/ |
| 136 | if(unlikely(pa->trace_num != pb->trace_num)) |
| 137 | ret = FALSE; |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | void destroy_hash_key(gpointer key); |
| 143 | |
| 144 | void destroy_hash_data(gpointer data); |
| 145 | |
| 146 | |
| 147 | gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data) |
| 148 | { |
| 149 | ControlFlowData *control_flow_data = |
| 150 | (ControlFlowData*)g_object_get_data( |
| 151 | G_OBJECT(widget), |
| 152 | "control_flow_data"); |
| 153 | unsigned int cell_height = |
| 154 | get_cell_height(GTK_TREE_VIEW(control_flow_data->process_list->process_list_widget)); |
| 155 | |
| 156 | switch(event->direction) { |
| 157 | case GDK_SCROLL_UP: |
| 158 | gtk_adjustment_set_value(control_flow_data->v_adjust, |
| 159 | gtk_adjustment_get_value(control_flow_data->v_adjust) - cell_height); |
| 160 | break; |
| 161 | case GDK_SCROLL_DOWN: |
| 162 | gtk_adjustment_set_value(control_flow_data->v_adjust, |
| 163 | gtk_adjustment_get_value(control_flow_data->v_adjust) + cell_height); |
| 164 | break; |
| 165 | default: |
| 166 | g_error("should only scroll up and down."); |
| 167 | } |
| 168 | return TRUE; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | static void update_index_to_pixmap_each(ProcessInfo *key, |
| 173 | HashedProcessData *value, |
| 174 | ProcessList *process_list) |
| 175 | { |
| 176 | guint array_index = processlist_get_index_from_data(process_list, value); |
| 177 | |
| 178 | g_assert(array_index < process_list->index_to_pixmap->len); |
| 179 | |
| 180 | GdkPixmap **pixmap = |
| 181 | (GdkPixmap**)&g_ptr_array_index(process_list->index_to_pixmap, array_index); |
| 182 | |
| 183 | *pixmap = value->pixmap; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | void update_index_to_pixmap(ProcessList *process_list) |
| 188 | { |
| 189 | g_ptr_array_set_size(process_list->index_to_pixmap, |
| 190 | g_hash_table_size(process_list->process_hash)); |
| 191 | g_hash_table_foreach(process_list->process_hash, |
| 192 | (GHFunc)update_index_to_pixmap_each, |
| 193 | process_list); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | static void update_pixmap_size_each(ProcessInfo *key, |
| 198 | HashedProcessData *value, |
| 199 | guint width) |
| 200 | { |
| 201 | GdkPixmap *old_pixmap = value->pixmap; |
| 202 | |
| 203 | value->pixmap = |
| 204 | gdk_pixmap_new(old_pixmap, |
| 205 | width, |
| 206 | value->height, |
| 207 | -1); |
| 208 | |
| 209 | gdk_pixmap_unref(old_pixmap); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | void update_pixmap_size(ProcessList *process_list, guint width) |
| 214 | { |
| 215 | g_hash_table_foreach(process_list->process_hash, |
| 216 | (GHFunc)update_pixmap_size_each, |
| 217 | GUINT_TO_POINTER(width)); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | typedef struct _CopyPixmap { |
| 222 | GdkDrawable *dest; |
| 223 | GdkGC *gc; |
| 224 | GdkDrawable *src; |
| 225 | gint xsrc, ysrc, xdest, ydest, width, height; |
| 226 | } CopyPixmap; |
| 227 | |
| 228 | static void copy_pixmap_region_each(ProcessInfo *key, |
| 229 | HashedProcessData *value, |
| 230 | CopyPixmap *cp) |
| 231 | { |
| 232 | GdkPixmap *src = cp->src; |
| 233 | GdkPixmap *dest = cp->dest; |
| 234 | |
| 235 | if(dest == NULL) |
| 236 | dest = value->pixmap; |
| 237 | if(src == NULL) |
| 238 | src = value->pixmap; |
| 239 | |
| 240 | gdk_draw_drawable (dest, |
| 241 | cp->gc, |
| 242 | src, |
| 243 | cp->xsrc, cp->ysrc, |
| 244 | cp->xdest, cp->ydest, |
| 245 | cp->width, cp->height); |
| 246 | } |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | void copy_pixmap_region(ProcessList *process_list, GdkDrawable *dest, |
| 252 | GdkGC *gc, GdkDrawable *src, |
| 253 | gint xsrc, gint ysrc, |
| 254 | gint xdest, gint ydest, gint width, gint height) |
| 255 | { |
| 256 | CopyPixmap cp = { dest, gc, src, xsrc, ysrc, xdest, ydest, width, height }; |
| 257 | |
| 258 | g_hash_table_foreach(process_list->process_hash, |
| 259 | (GHFunc)copy_pixmap_region_each, |
| 260 | &cp); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | typedef struct _RectanglePixmap { |
| 266 | gboolean filled; |
| 267 | gint x, y, width, height; |
| 268 | GdkGC *gc; |
| 269 | } RectanglePixmap; |
| 270 | |
| 271 | static void rectangle_pixmap_each(ProcessInfo *key, |
| 272 | HashedProcessData *value, |
| 273 | RectanglePixmap *rp) |
| 274 | { |
| 275 | if(rp->height == -1) |
| 276 | rp->height = value->height; |
| 277 | |
| 278 | gdk_draw_rectangle (value->pixmap, |
| 279 | rp->gc, |
| 280 | rp->filled, |
| 281 | rp->x, rp->y, |
| 282 | rp->width, rp->height); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | void rectangle_pixmap(ProcessList *process_list, GdkGC *gc, |
| 289 | gboolean filled, gint x, gint y, gint width, gint height) |
| 290 | { |
| 291 | RectanglePixmap rp = { filled, x, y, width, height, gc }; |
| 292 | |
| 293 | g_hash_table_foreach(process_list->process_hash, |
| 294 | (GHFunc)rectangle_pixmap_each, |
| 295 | &rp); |
| 296 | } |
| 297 | |
| 298 | |
| 299 | /* Renders each pixmaps into on big drawable */ |
| 300 | void copy_pixmap_to_screen(ProcessList *process_list, |
| 301 | GdkDrawable *dest, |
| 302 | GdkGC *gc, |
| 303 | gint x, gint y, |
| 304 | gint width, gint height) |
| 305 | { |
| 306 | if(process_list->index_to_pixmap->len == 0) return; |
| 307 | guint cell_height = process_list->cell_height; |
| 308 | |
| 309 | /* Get indexes */ |
| 310 | gint begin = floor(y/(double)cell_height); |
| 311 | gint end = MIN(ceil((y+height)/(double)cell_height), |
| 312 | process_list->index_to_pixmap->len); |
| 313 | gint i; |
| 314 | |
| 315 | for(i=begin; i<end; i++) { |
| 316 | g_assert(i<process_list->index_to_pixmap->len); |
| 317 | /* Render the pixmap to the screen */ |
| 318 | GdkPixmap *pixmap = |
| 319 | //(GdkPixmap*)g_ptr_array_index(process_list->index_to_pixmap, i); |
| 320 | GDK_PIXMAP(g_ptr_array_index(process_list->index_to_pixmap, i)); |
| 321 | |
| 322 | gdk_draw_drawable (dest, |
| 323 | gc, |
| 324 | pixmap, |
| 325 | x, 0, |
| 326 | x, i*cell_height, |
| 327 | width, cell_height); |
| 328 | |
| 329 | } |
| 330 | |
| 331 | |
| 332 | } |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | ProcessList *processlist_construct(void) |
| 343 | { |
| 344 | GtkTreeViewColumn *column; |
| 345 | GtkCellRenderer *renderer; |
| 346 | |
| 347 | ProcessList* process_list = g_new(ProcessList,1); |
| 348 | |
| 349 | process_list->number_of_process = 0; |
| 350 | |
| 351 | process_list->current_hash_data = NULL; |
| 352 | |
| 353 | /* Create the Process list */ |
| 354 | process_list->list_store = gtk_list_store_new ( N_COLUMNS, |
| 355 | G_TYPE_STRING, |
| 356 | G_TYPE_UINT, |
| 357 | G_TYPE_UINT, |
| 358 | G_TYPE_UINT, |
| 359 | G_TYPE_UINT, |
| 360 | G_TYPE_ULONG, |
| 361 | G_TYPE_ULONG, |
| 362 | G_TYPE_UINT); |
| 363 | |
| 364 | |
| 365 | process_list->process_list_widget = |
| 366 | gtk_tree_view_new_with_model |
| 367 | (GTK_TREE_MODEL (process_list->list_store)); |
| 368 | |
| 369 | g_object_unref (G_OBJECT (process_list->list_store)); |
| 370 | |
| 371 | gtk_tree_sortable_set_default_sort_func( |
| 372 | GTK_TREE_SORTABLE(process_list->list_store), |
| 373 | process_sort_func, |
| 374 | NULL, |
| 375 | NULL); |
| 376 | |
| 377 | |
| 378 | gtk_tree_sortable_set_sort_column_id( |
| 379 | GTK_TREE_SORTABLE(process_list->list_store), |
| 380 | GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, |
| 381 | GTK_SORT_ASCENDING); |
| 382 | |
| 383 | |
| 384 | process_list->process_hash = g_hash_table_new_full( |
| 385 | process_list_hash_fct, process_list_equ_fct, |
| 386 | destroy_hash_key, destroy_hash_data |
| 387 | ); |
| 388 | |
| 389 | |
| 390 | gtk_tree_view_set_headers_visible( |
| 391 | GTK_TREE_VIEW(process_list->process_list_widget), TRUE); |
| 392 | |
| 393 | /* Create a column, associating the "text" attribute of the |
| 394 | * cell_renderer to the first column of the model */ |
| 395 | /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */ |
| 396 | renderer = gtk_cell_renderer_text_new (); |
| 397 | process_list->renderer = renderer; |
| 398 | |
| 399 | /* Add a temporary row to the model to get the cell size when the first |
| 400 | * real process is added. */ |
| 401 | GtkTreeIter iter; |
| 402 | GtkTreePath *path; |
| 403 | path = gtk_tree_path_new_first(); |
| 404 | gtk_tree_model_get_iter (gtk_tree_view_get_model(GTK_TREE_VIEW(process_list->process_list_widget)), &iter, path); |
| 405 | gtk_list_store_append(process_list->list_store, &iter); |
| 406 | gtk_tree_path_free(path); |
| 407 | |
| 408 | process_list->cell_height = 0; // not ready to get size yet. |
| 409 | |
| 410 | column = gtk_tree_view_column_new_with_attributes ( "Process", |
| 411 | renderer, |
| 412 | "text", |
| 413 | PROCESS_COLUMN, |
| 414 | NULL); |
| 415 | gtk_tree_view_column_set_alignment (column, 0.0); |
| 416 | gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY); |
| 417 | gtk_tree_view_column_set_resizable(column, TRUE); |
| 418 | gtk_tree_view_append_column ( |
| 419 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 420 | |
| 421 | process_list->button = column->button; |
| 422 | |
| 423 | column = gtk_tree_view_column_new_with_attributes ( "PID", |
| 424 | renderer, |
| 425 | "text", |
| 426 | PID_COLUMN, |
| 427 | NULL); |
| 428 | gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY); |
| 429 | gtk_tree_view_column_set_resizable(column, TRUE); |
| 430 | gtk_tree_view_append_column ( |
| 431 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 432 | |
| 433 | column = gtk_tree_view_column_new_with_attributes ( "TGID", |
| 434 | renderer, |
| 435 | "text", |
| 436 | TGID_COLUMN, |
| 437 | NULL); |
| 438 | gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY); |
| 439 | gtk_tree_view_column_set_resizable(column, TRUE); |
| 440 | gtk_tree_view_append_column ( |
| 441 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 442 | |
| 443 | column = gtk_tree_view_column_new_with_attributes ( "PPID", |
| 444 | renderer, |
| 445 | "text", |
| 446 | PPID_COLUMN, |
| 447 | NULL); |
| 448 | gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY); |
| 449 | gtk_tree_view_column_set_resizable(column, TRUE); |
| 450 | gtk_tree_view_append_column ( |
| 451 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 452 | |
| 453 | column = gtk_tree_view_column_new_with_attributes ( "CPU", |
| 454 | renderer, |
| 455 | "text", |
| 456 | CPU_COLUMN, |
| 457 | NULL); |
| 458 | gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY); |
| 459 | gtk_tree_view_column_set_resizable(column, TRUE); |
| 460 | gtk_tree_view_append_column ( |
| 461 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 462 | |
| 463 | column = gtk_tree_view_column_new_with_attributes ( "Birth sec", |
| 464 | renderer, |
| 465 | "text", |
| 466 | BIRTH_S_COLUMN, |
| 467 | NULL); |
| 468 | gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY); |
| 469 | gtk_tree_view_column_set_resizable(column, TRUE); |
| 470 | gtk_tree_view_append_column ( |
| 471 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 472 | |
| 473 | //gtk_tree_view_column_set_visible(column, 0); |
| 474 | // |
| 475 | column = gtk_tree_view_column_new_with_attributes ( "Birth nsec", |
| 476 | renderer, |
| 477 | "text", |
| 478 | BIRTH_NS_COLUMN, |
| 479 | NULL); |
| 480 | gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY); |
| 481 | gtk_tree_view_column_set_resizable(column, TRUE); |
| 482 | gtk_tree_view_append_column ( |
| 483 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 484 | |
| 485 | column = gtk_tree_view_column_new_with_attributes ( "TRACE", |
| 486 | renderer, |
| 487 | "text", |
| 488 | TRACE_COLUMN, |
| 489 | NULL); |
| 490 | gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY); |
| 491 | gtk_tree_view_column_set_resizable(column, TRUE); |
| 492 | gtk_tree_view_append_column ( |
| 493 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 494 | |
| 495 | |
| 496 | //gtk_tree_view_column_set_visible(column, 0); |
| 497 | |
| 498 | g_object_set_data_full( |
| 499 | G_OBJECT(process_list->process_list_widget), |
| 500 | "process_list_Data", |
| 501 | process_list, |
| 502 | (GDestroyNotify)processlist_destroy); |
| 503 | |
| 504 | process_list->index_to_pixmap = g_ptr_array_sized_new(ALLOCATE_PROCESSES); |
| 505 | |
| 506 | return process_list; |
| 507 | } |
| 508 | |
| 509 | void processlist_destroy(ProcessList *process_list) |
| 510 | { |
| 511 | g_debug("processlist_destroy %p", process_list); |
| 512 | g_hash_table_destroy(process_list->process_hash); |
| 513 | process_list->process_hash = NULL; |
| 514 | g_ptr_array_free(process_list->index_to_pixmap, TRUE); |
| 515 | |
| 516 | g_free(process_list); |
| 517 | g_debug("processlist_destroy end"); |
| 518 | } |
| 519 | |
| 520 | static gboolean remove_hash_item(ProcessInfo *process_info, |
| 521 | HashedProcessData *hashed_process_data, |
| 522 | ProcessList *process_list) |
| 523 | { |
| 524 | GtkTreeIter iter; |
| 525 | |
| 526 | iter = hashed_process_data->y_iter; |
| 527 | |
| 528 | gtk_list_store_remove (process_list->list_store, &iter); |
| 529 | gdk_pixmap_unref(hashed_process_data->pixmap); |
| 530 | |
| 531 | if(likely(process_list->current_hash_data != NULL)) { |
| 532 | if(likely(hashed_process_data == |
| 533 | process_list->current_hash_data[process_info->trace_num][process_info->cpu])) |
| 534 | process_list->current_hash_data[process_info->trace_num][process_info->cpu] = NULL; |
| 535 | } |
| 536 | return TRUE; /* remove the element from the hash table */ |
| 537 | } |
| 538 | |
| 539 | void processlist_clear(ProcessList *process_list) |
| 540 | { |
| 541 | g_info("processlist_clear %p", process_list); |
| 542 | |
| 543 | g_hash_table_foreach_remove(process_list->process_hash, |
| 544 | (GHRFunc)remove_hash_item, |
| 545 | (gpointer)process_list); |
| 546 | process_list->number_of_process = 0; |
| 547 | update_index_to_pixmap(process_list); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | GtkWidget *processlist_get_widget(ProcessList *process_list) |
| 552 | { |
| 553 | return process_list->process_list_widget; |
| 554 | } |
| 555 | |
| 556 | |
| 557 | void destroy_hash_key(gpointer key) |
| 558 | { |
| 559 | g_free(key); |
| 560 | } |
| 561 | |
| 562 | void destroy_hash_data(gpointer data) |
| 563 | { |
| 564 | g_free(data); |
| 565 | } |
| 566 | |
| 567 | |
| 568 | void processlist_set_name(ProcessList *process_list, |
| 569 | GQuark name, |
| 570 | HashedProcessData *hashed_process_data) |
| 571 | { |
| 572 | gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter, |
| 573 | PROCESS_COLUMN, g_quark_to_string(name), |
| 574 | -1); |
| 575 | } |
| 576 | |
| 577 | void processlist_set_tgid(ProcessList *process_list, |
| 578 | guint tgid, |
| 579 | HashedProcessData *hashed_process_data) |
| 580 | { |
| 581 | gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter, |
| 582 | TGID_COLUMN, tgid, |
| 583 | -1); |
| 584 | } |
| 585 | |
| 586 | void processlist_set_ppid(ProcessList *process_list, |
| 587 | guint ppid, |
| 588 | HashedProcessData *hashed_process_data) |
| 589 | { |
| 590 | gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter, |
| 591 | PPID_COLUMN, ppid, |
| 592 | -1); |
| 593 | } |
| 594 | |
| 595 | |
| 596 | int processlist_add( ProcessList *process_list, |
| 597 | Drawing_t *drawing, |
| 598 | guint pid, |
| 599 | guint tgid, |
| 600 | guint cpu, |
| 601 | guint ppid, |
| 602 | LttTime *birth, |
| 603 | guint trace_num, |
| 604 | GQuark name, |
| 605 | guint *height, |
| 606 | ProcessInfo **pm_process_info, |
| 607 | HashedProcessData **pm_hashed_process_data) |
| 608 | { |
| 609 | ProcessInfo *Process_Info = g_new(ProcessInfo, 1); |
| 610 | HashedProcessData *hashed_process_data = g_new(HashedProcessData, 1); |
| 611 | *pm_hashed_process_data = hashed_process_data; |
| 612 | *pm_process_info = Process_Info; |
| 613 | |
| 614 | Process_Info->pid = pid; |
| 615 | Process_Info->tgid = tgid; |
| 616 | if(pid == 0) |
| 617 | Process_Info->cpu = cpu; |
| 618 | else |
| 619 | Process_Info->cpu = 0; |
| 620 | Process_Info->ppid = ppid; |
| 621 | Process_Info->birth = *birth; |
| 622 | Process_Info->trace_num = trace_num; |
| 623 | |
| 624 | /* When we create it from before state update, we are sure that the |
| 625 | * last event occured before the beginning of the global area. |
| 626 | * |
| 627 | * If it is created after state update, this value (0) will be |
| 628 | * overriden by the new state before anything is drawn. |
| 629 | */ |
| 630 | hashed_process_data->x.over = 0; |
| 631 | hashed_process_data->x.over_used = FALSE; |
| 632 | hashed_process_data->x.over_marked = FALSE; |
| 633 | hashed_process_data->x.middle = 0; |
| 634 | hashed_process_data->x.middle_used = FALSE; |
| 635 | hashed_process_data->x.middle_marked = FALSE; |
| 636 | hashed_process_data->x.under = 0; |
| 637 | hashed_process_data->x.under_used = FALSE; |
| 638 | hashed_process_data->x.under_marked = FALSE; |
| 639 | hashed_process_data->next_good_time = ltt_time_zero; |
| 640 | |
| 641 | if (process_list->cell_height == 0) { |
| 642 | GtkTreePath *path; |
| 643 | GdkRectangle rect; |
| 644 | GtkTreeIter iter; |
| 645 | |
| 646 | path = gtk_tree_path_new_first(); |
| 647 | gtk_tree_model_get_iter (gtk_tree_view_get_model(GTK_TREE_VIEW(process_list->process_list_widget)), &iter, path); |
| 648 | gtk_tree_view_get_background_area( |
| 649 | GTK_TREE_VIEW(process_list->process_list_widget), |
| 650 | path, NULL, &rect); |
| 651 | gtk_list_store_remove(process_list->list_store, &iter); |
| 652 | gtk_tree_path_free (path); |
| 653 | process_list->cell_height = rect.height; |
| 654 | } |
| 655 | |
| 656 | /* Add a new row to the model */ |
| 657 | gtk_list_store_append ( process_list->list_store, |
| 658 | &hashed_process_data->y_iter); |
| 659 | |
| 660 | gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter, |
| 661 | PROCESS_COLUMN, g_quark_to_string(name), |
| 662 | PID_COLUMN, pid, |
| 663 | TGID_COLUMN, tgid, |
| 664 | PPID_COLUMN, ppid, |
| 665 | CPU_COLUMN, cpu, |
| 666 | BIRTH_S_COLUMN, birth->tv_sec, |
| 667 | BIRTH_NS_COLUMN, birth->tv_nsec, |
| 668 | TRACE_COLUMN, trace_num, |
| 669 | -1); |
| 670 | //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget), |
| 671 | // GTK_TREE_MODEL(process_list->list_store)); |
| 672 | //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget)); |
| 673 | |
| 674 | g_hash_table_insert(process_list->process_hash, |
| 675 | (gpointer)Process_Info, |
| 676 | (gpointer)hashed_process_data); |
| 677 | |
| 678 | process_list->number_of_process++; |
| 679 | #if 0 |
| 680 | GtkTreePath *path; |
| 681 | GdkRectangle rect; |
| 682 | gtk_widget_queue_draw(process_list->process_list_widget); |
| 683 | path = gtk_tree_path_new_first(); |
| 684 | gtk_tree_view_get_background_area(GTK_TREE_VIEW(process_list->process_list_widget), |
| 685 | path, NULL, &rect); |
| 686 | gtk_tree_path_free (path); |
| 687 | process_list->cell_height = rect.height; |
| 688 | #endif //0 |
| 689 | |
| 690 | |
| 691 | hashed_process_data->height = process_list->cell_height; |
| 692 | g_assert(hashed_process_data->height != 0); |
| 693 | |
| 694 | *height = hashed_process_data->height * process_list->number_of_process; |
| 695 | |
| 696 | hashed_process_data->pixmap = |
| 697 | gdk_pixmap_new(drawing->drawing_area->window, |
| 698 | drawing->alloc_width, |
| 699 | hashed_process_data->height, |
| 700 | -1); |
| 701 | |
| 702 | // Clear the image |
| 703 | gdk_draw_rectangle (hashed_process_data->pixmap, |
| 704 | drawing->drawing_area->style->black_gc, |
| 705 | TRUE, |
| 706 | 0, 0, |
| 707 | drawing->alloc_width, |
| 708 | hashed_process_data->height); |
| 709 | |
| 710 | update_index_to_pixmap(process_list); |
| 711 | |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | int processlist_remove( ProcessList *process_list, |
| 717 | guint pid, |
| 718 | guint cpu, |
| 719 | LttTime *birth, |
| 720 | guint trace_num) |
| 721 | { |
| 722 | ProcessInfo process_info; |
| 723 | HashedProcessData *hashed_process_data; |
| 724 | GtkTreeIter iter; |
| 725 | |
| 726 | process_info.pid = pid; |
| 727 | if(pid == 0) |
| 728 | process_info.cpu = cpu; |
| 729 | else |
| 730 | process_info.cpu = 0; |
| 731 | process_info.birth = *birth; |
| 732 | process_info.trace_num = trace_num; |
| 733 | |
| 734 | |
| 735 | hashed_process_data = |
| 736 | (HashedProcessData*)g_hash_table_lookup( |
| 737 | process_list->process_hash, |
| 738 | &process_info); |
| 739 | if(likely(hashed_process_data != NULL)) |
| 740 | { |
| 741 | iter = hashed_process_data->y_iter; |
| 742 | |
| 743 | gtk_list_store_remove (process_list->list_store, &iter); |
| 744 | |
| 745 | g_hash_table_remove(process_list->process_hash, |
| 746 | &process_info); |
| 747 | |
| 748 | if(likely(process_list->current_hash_data != NULL)) { |
| 749 | if(likely(hashed_process_data == process_list->current_hash_data[trace_num][cpu])) { |
| 750 | process_list->current_hash_data[trace_num][cpu] = NULL; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | gdk_pixmap_unref(hashed_process_data->pixmap); |
| 755 | |
| 756 | update_index_to_pixmap(process_list); |
| 757 | |
| 758 | process_list->number_of_process--; |
| 759 | |
| 760 | return 0; |
| 761 | } else { |
| 762 | return 1; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | |
| 767 | #if 0 |
| 768 | static inline guint get_cpu_number_from_name(GQuark name) |
| 769 | { |
| 770 | const gchar *string; |
| 771 | char *begin; |
| 772 | guint cpu; |
| 773 | |
| 774 | string = g_quark_to_string(name); |
| 775 | |
| 776 | begin = strrchr(string, '/'); |
| 777 | begin++; |
| 778 | |
| 779 | g_assert(begin != '\0'); |
| 780 | |
| 781 | cpu = strtoul(begin, NULL, 10); |
| 782 | |
| 783 | return cpu; |
| 784 | } |
| 785 | #endif //0 |