| 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., 59 Temple Place - Suite 330, Boston, |
| 16 | * MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <gtk/gtk.h> |
| 20 | #include <glib.h> |
| 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | #include "processlist.h" |
| 25 | #include "drawitem.h" |
| 26 | |
| 27 | #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format) |
| 28 | #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format) |
| 29 | |
| 30 | |
| 31 | /***************************************************************************** |
| 32 | * Methods to synchronize process list * |
| 33 | *****************************************************************************/ |
| 34 | |
| 35 | //static inline guint get_cpu_number_from_name(GQuark name); |
| 36 | |
| 37 | /* Enumeration of the columns */ |
| 38 | enum |
| 39 | { |
| 40 | PROCESS_COLUMN, |
| 41 | PID_COLUMN, |
| 42 | PPID_COLUMN, |
| 43 | CPU_COLUMN, |
| 44 | BIRTH_S_COLUMN, |
| 45 | BIRTH_NS_COLUMN, |
| 46 | TRACE_COLUMN, |
| 47 | N_COLUMNS |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | gint process_sort_func ( GtkTreeModel *model, |
| 52 | GtkTreeIter *it_a, |
| 53 | GtkTreeIter *it_b, |
| 54 | gpointer user_data) |
| 55 | { |
| 56 | GValue a, b; |
| 57 | |
| 58 | memset(&a, 0, sizeof(GValue)); |
| 59 | memset(&b, 0, sizeof(GValue)); |
| 60 | |
| 61 | /* Order by PID */ |
| 62 | gtk_tree_model_get_value( model, |
| 63 | it_a, |
| 64 | PID_COLUMN, |
| 65 | &a); |
| 66 | |
| 67 | gtk_tree_model_get_value( model, |
| 68 | it_b, |
| 69 | PID_COLUMN, |
| 70 | &b); |
| 71 | |
| 72 | if(G_VALUE_TYPE(&a) == G_TYPE_UINT |
| 73 | && G_VALUE_TYPE(&b) == G_TYPE_UINT ) |
| 74 | { |
| 75 | { |
| 76 | |
| 77 | if(g_value_get_uint(&a) == 0 && g_value_get_uint(&b) == 0) { |
| 78 | |
| 79 | GValue cpua, cpub; |
| 80 | |
| 81 | memset(&cpua, 0, sizeof(GValue)); |
| 82 | memset(&cpub, 0, sizeof(GValue)); |
| 83 | |
| 84 | /* If 0, order by CPU */ |
| 85 | gtk_tree_model_get_value( model, |
| 86 | it_a, |
| 87 | CPU_COLUMN, |
| 88 | &cpua); |
| 89 | |
| 90 | gtk_tree_model_get_value( model, |
| 91 | it_b, |
| 92 | CPU_COLUMN, |
| 93 | &cpub); |
| 94 | |
| 95 | if(G_VALUE_TYPE(&cpua) == G_TYPE_UINT |
| 96 | && G_VALUE_TYPE(&cpub) == G_TYPE_UINT ) |
| 97 | { |
| 98 | if(g_value_get_uint(&cpua) > g_value_get_uint(&cpub)) |
| 99 | { |
| 100 | g_value_unset(&cpua); |
| 101 | g_value_unset(&cpub); |
| 102 | return 1; |
| 103 | } |
| 104 | if(g_value_get_uint(&cpua) < g_value_get_uint(&cpub)) |
| 105 | { |
| 106 | g_value_unset(&cpua); |
| 107 | g_value_unset(&cpub); |
| 108 | return 0; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | g_value_unset(&cpua); |
| 113 | g_value_unset(&cpub); |
| 114 | |
| 115 | } else { /* if not 0, order by pid */ |
| 116 | |
| 117 | if(g_value_get_uint(&a) > g_value_get_uint(&b)) |
| 118 | { |
| 119 | g_value_unset(&a); |
| 120 | g_value_unset(&b); |
| 121 | return 1; |
| 122 | } |
| 123 | if(g_value_get_uint(&a) < g_value_get_uint(&b)) |
| 124 | { |
| 125 | g_value_unset(&a); |
| 126 | g_value_unset(&b); |
| 127 | return 0; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | g_value_unset(&a); |
| 134 | g_value_unset(&b); |
| 135 | |
| 136 | |
| 137 | /* Order by birth second */ |
| 138 | gtk_tree_model_get_value( model, |
| 139 | it_a, |
| 140 | BIRTH_S_COLUMN, |
| 141 | &a); |
| 142 | |
| 143 | gtk_tree_model_get_value( model, |
| 144 | it_b, |
| 145 | BIRTH_S_COLUMN, |
| 146 | &b); |
| 147 | |
| 148 | |
| 149 | if(G_VALUE_TYPE(&a) == G_TYPE_ULONG |
| 150 | && G_VALUE_TYPE(&b) == G_TYPE_ULONG ) |
| 151 | { |
| 152 | if(g_value_get_ulong(&a) > g_value_get_ulong(&b)) |
| 153 | { |
| 154 | g_value_unset(&a); |
| 155 | g_value_unset(&b); |
| 156 | return 1; |
| 157 | } |
| 158 | if(g_value_get_ulong(&a) < g_value_get_ulong(&b)) |
| 159 | { |
| 160 | g_value_unset(&a); |
| 161 | g_value_unset(&b); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
| 167 | g_value_unset(&a); |
| 168 | g_value_unset(&b); |
| 169 | |
| 170 | /* Order by birth nanosecond */ |
| 171 | gtk_tree_model_get_value( model, |
| 172 | it_a, |
| 173 | BIRTH_NS_COLUMN, |
| 174 | &a); |
| 175 | |
| 176 | gtk_tree_model_get_value( model, |
| 177 | it_b, |
| 178 | BIRTH_NS_COLUMN, |
| 179 | &b); |
| 180 | |
| 181 | |
| 182 | if(G_VALUE_TYPE(&a) == G_TYPE_ULONG |
| 183 | && G_VALUE_TYPE(&b) == G_TYPE_ULONG ) |
| 184 | { |
| 185 | if(g_value_get_ulong(&a) > g_value_get_ulong(&b)) |
| 186 | { |
| 187 | g_value_unset(&a); |
| 188 | g_value_unset(&b); |
| 189 | return 1; |
| 190 | } |
| 191 | if(g_value_get_ulong(&a) < g_value_get_ulong(&b)) |
| 192 | { |
| 193 | g_value_unset(&a); |
| 194 | g_value_unset(&b); |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | } |
| 199 | |
| 200 | g_value_unset(&a); |
| 201 | g_value_unset(&b); |
| 202 | |
| 203 | /* Order by trace_num */ |
| 204 | gtk_tree_model_get_value( model, |
| 205 | it_a, |
| 206 | TRACE_COLUMN, |
| 207 | &a); |
| 208 | |
| 209 | gtk_tree_model_get_value( model, |
| 210 | it_b, |
| 211 | TRACE_COLUMN, |
| 212 | &b); |
| 213 | |
| 214 | if(G_VALUE_TYPE(&a) == G_TYPE_ULONG |
| 215 | && G_VALUE_TYPE(&b) == G_TYPE_ULONG ) |
| 216 | { |
| 217 | if(g_value_get_ulong(&a) > g_value_get_ulong(&b)) |
| 218 | { |
| 219 | g_value_unset(&a); |
| 220 | g_value_unset(&b); |
| 221 | return 1; |
| 222 | } |
| 223 | if(g_value_get_ulong(&a) < g_value_get_ulong(&b)) |
| 224 | { |
| 225 | g_value_unset(&a); |
| 226 | g_value_unset(&b); |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | |
| 234 | } |
| 235 | |
| 236 | static guint process_list_hash_fct(gconstpointer key) |
| 237 | { |
| 238 | guint pid = ((const ProcessInfo*)key)->pid; |
| 239 | return ((pid>>8 ^ pid>>4 ^ pid>>2 ^ pid) ^ ((const ProcessInfo*)key)->cpu); |
| 240 | } |
| 241 | |
| 242 | /* If hash is good, should be different */ |
| 243 | static gboolean process_list_equ_fct(gconstpointer a, gconstpointer b) |
| 244 | { |
| 245 | const ProcessInfo *pa = (const ProcessInfo*)a; |
| 246 | const ProcessInfo *pb = (const ProcessInfo*)b; |
| 247 | |
| 248 | gboolean ret = TRUE; |
| 249 | |
| 250 | if(likely(pa->pid != pb->pid)) |
| 251 | ret = FALSE; |
| 252 | else if(likely((pa->pid == 0 && (pa->cpu != pb->cpu)))) |
| 253 | ret = FALSE; |
| 254 | else if(unlikely(ltt_time_compare(pa->birth, pb->birth) != 0)) |
| 255 | ret = FALSE; |
| 256 | else if(unlikely(pa->trace_num != pb->trace_num)) |
| 257 | ret = FALSE; |
| 258 | |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | void destroy_hash_key(gpointer key); |
| 263 | |
| 264 | void destroy_hash_data(gpointer data); |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | ProcessList *processlist_construct(void) |
| 270 | { |
| 271 | GtkTreeViewColumn *column; |
| 272 | GtkCellRenderer *renderer; |
| 273 | |
| 274 | ProcessList* process_list = g_new(ProcessList,1); |
| 275 | |
| 276 | process_list->number_of_process = 0; |
| 277 | process_list->cell_height_cache = -1; |
| 278 | |
| 279 | process_list->current_hash_data = NULL; |
| 280 | |
| 281 | /* Create the Process list */ |
| 282 | process_list->list_store = gtk_list_store_new ( N_COLUMNS, |
| 283 | G_TYPE_STRING, |
| 284 | G_TYPE_UINT, |
| 285 | G_TYPE_UINT, |
| 286 | G_TYPE_UINT, |
| 287 | G_TYPE_ULONG, |
| 288 | G_TYPE_ULONG, |
| 289 | G_TYPE_ULONG); |
| 290 | |
| 291 | |
| 292 | process_list->process_list_widget = |
| 293 | gtk_tree_view_new_with_model |
| 294 | (GTK_TREE_MODEL (process_list->list_store)); |
| 295 | g_object_unref (G_OBJECT (process_list->list_store)); |
| 296 | |
| 297 | gtk_tree_sortable_set_sort_func( |
| 298 | GTK_TREE_SORTABLE(process_list->list_store), |
| 299 | PID_COLUMN, |
| 300 | process_sort_func, |
| 301 | NULL, |
| 302 | NULL); |
| 303 | |
| 304 | gtk_tree_sortable_set_sort_column_id( |
| 305 | GTK_TREE_SORTABLE(process_list->list_store), |
| 306 | PID_COLUMN, |
| 307 | GTK_SORT_ASCENDING); |
| 308 | |
| 309 | process_list->process_hash = g_hash_table_new_full( |
| 310 | process_list_hash_fct, process_list_equ_fct, |
| 311 | destroy_hash_key, destroy_hash_data |
| 312 | ); |
| 313 | |
| 314 | |
| 315 | gtk_tree_view_set_headers_visible( |
| 316 | GTK_TREE_VIEW(process_list->process_list_widget), TRUE); |
| 317 | |
| 318 | /* Create a column, associating the "text" attribute of the |
| 319 | * cell_renderer to the first column of the model */ |
| 320 | /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */ |
| 321 | renderer = gtk_cell_renderer_text_new (); |
| 322 | column = gtk_tree_view_column_new_with_attributes ( "Process", |
| 323 | renderer, |
| 324 | "text", |
| 325 | PROCESS_COLUMN, |
| 326 | NULL); |
| 327 | gtk_tree_view_column_set_alignment (column, 0.0); |
| 328 | gtk_tree_view_column_set_fixed_width (column, 45); |
| 329 | gtk_tree_view_append_column ( |
| 330 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 331 | |
| 332 | process_list->button = column->button; |
| 333 | |
| 334 | column = gtk_tree_view_column_new_with_attributes ( "PID", |
| 335 | renderer, |
| 336 | "text", |
| 337 | PID_COLUMN, |
| 338 | NULL); |
| 339 | gtk_tree_view_append_column ( |
| 340 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 341 | |
| 342 | column = gtk_tree_view_column_new_with_attributes ( "PPID", |
| 343 | renderer, |
| 344 | "text", |
| 345 | PPID_COLUMN, |
| 346 | NULL); |
| 347 | gtk_tree_view_append_column ( |
| 348 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 349 | |
| 350 | column = gtk_tree_view_column_new_with_attributes ( "CPU", |
| 351 | renderer, |
| 352 | "text", |
| 353 | CPU_COLUMN, |
| 354 | NULL); |
| 355 | gtk_tree_view_append_column ( |
| 356 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 357 | |
| 358 | column = gtk_tree_view_column_new_with_attributes ( "Birth sec", |
| 359 | renderer, |
| 360 | "text", |
| 361 | BIRTH_S_COLUMN, |
| 362 | NULL); |
| 363 | gtk_tree_view_append_column ( |
| 364 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 365 | |
| 366 | //gtk_tree_view_column_set_visible(column, 0); |
| 367 | // |
| 368 | column = gtk_tree_view_column_new_with_attributes ( "Birth nsec", |
| 369 | renderer, |
| 370 | "text", |
| 371 | BIRTH_NS_COLUMN, |
| 372 | NULL); |
| 373 | gtk_tree_view_append_column ( |
| 374 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 375 | |
| 376 | column = gtk_tree_view_column_new_with_attributes ( "TRACE", |
| 377 | renderer, |
| 378 | "text", |
| 379 | TRACE_COLUMN, |
| 380 | NULL); |
| 381 | gtk_tree_view_append_column ( |
| 382 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 383 | |
| 384 | |
| 385 | //gtk_tree_view_column_set_visible(column, 0); |
| 386 | |
| 387 | g_object_set_data_full( |
| 388 | G_OBJECT(process_list->process_list_widget), |
| 389 | "process_list_Data", |
| 390 | process_list, |
| 391 | (GDestroyNotify)processlist_destroy); |
| 392 | |
| 393 | return process_list; |
| 394 | } |
| 395 | |
| 396 | void processlist_destroy(ProcessList *process_list) |
| 397 | { |
| 398 | g_debug("processlist_destroy %p", process_list); |
| 399 | g_hash_table_destroy(process_list->process_hash); |
| 400 | process_list->process_hash = NULL; |
| 401 | |
| 402 | g_free(process_list); |
| 403 | g_debug("processlist_destroy end"); |
| 404 | } |
| 405 | |
| 406 | static gboolean remove_hash_item(ProcessInfo *process_info, |
| 407 | HashedProcessData *hashed_process_data, |
| 408 | ProcessList *process_list) |
| 409 | { |
| 410 | GtkTreeIter iter; |
| 411 | |
| 412 | iter = hashed_process_data->y_iter; |
| 413 | |
| 414 | gtk_list_store_remove (process_list->list_store, &iter); |
| 415 | |
| 416 | if(likely(process_list->current_hash_data != NULL)) { |
| 417 | if(likely(hashed_process_data == |
| 418 | process_list->current_hash_data[process_info->cpu])) |
| 419 | process_list->current_hash_data[process_info->cpu] = NULL; |
| 420 | } |
| 421 | return TRUE; /* remove the element from the hash table */ |
| 422 | } |
| 423 | |
| 424 | void processlist_clear(ProcessList *process_list) |
| 425 | { |
| 426 | g_info("processlist_clear %p", process_list); |
| 427 | |
| 428 | g_hash_table_foreach_remove(process_list->process_hash, |
| 429 | (GHRFunc)remove_hash_item, |
| 430 | (gpointer)process_list); |
| 431 | process_list->number_of_process = 0; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | GtkWidget *processlist_get_widget(ProcessList *process_list) |
| 436 | { |
| 437 | return process_list->process_list_widget; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | void destroy_hash_key(gpointer key) |
| 442 | { |
| 443 | g_free(key); |
| 444 | } |
| 445 | |
| 446 | void destroy_hash_data(gpointer data) |
| 447 | { |
| 448 | g_free(data); |
| 449 | } |
| 450 | |
| 451 | int processlist_add( ProcessList *process_list, |
| 452 | guint pid, |
| 453 | guint cpu, |
| 454 | guint ppid, |
| 455 | LttTime *birth, |
| 456 | guint trace_num, |
| 457 | const gchar *name, |
| 458 | guint *height, |
| 459 | ProcessInfo **pm_process_info, |
| 460 | HashedProcessData **pm_hashed_process_data) |
| 461 | { |
| 462 | ProcessInfo *Process_Info = g_new(ProcessInfo, 1); |
| 463 | HashedProcessData *hashed_process_data = g_new(HashedProcessData, 1); |
| 464 | *pm_hashed_process_data = hashed_process_data; |
| 465 | *pm_process_info = Process_Info; |
| 466 | |
| 467 | Process_Info->pid = pid; |
| 468 | if(pid == 0) |
| 469 | Process_Info->cpu = cpu; |
| 470 | else |
| 471 | Process_Info->cpu = 0; |
| 472 | Process_Info->ppid = ppid; |
| 473 | Process_Info->birth = *birth; |
| 474 | Process_Info->trace_num = trace_num; |
| 475 | |
| 476 | /* When we create it from before state update, we are sure that the |
| 477 | * last event occured before the beginning of the global area. |
| 478 | * |
| 479 | * If it is created after state update, this value (0) will be |
| 480 | * overriden by the new state before anything is drawn. |
| 481 | */ |
| 482 | hashed_process_data->x.over = 0; |
| 483 | hashed_process_data->x.over_used = FALSE; |
| 484 | hashed_process_data->x.over_marked = FALSE; |
| 485 | hashed_process_data->x.middle = 0; |
| 486 | hashed_process_data->x.middle_used = FALSE; |
| 487 | hashed_process_data->x.middle_marked = FALSE; |
| 488 | hashed_process_data->x.under = 0; |
| 489 | hashed_process_data->x.under_used = FALSE; |
| 490 | hashed_process_data->x.under_marked = FALSE; |
| 491 | hashed_process_data->next_good_time = ltt_time_zero; |
| 492 | |
| 493 | /* Add a new row to the model */ |
| 494 | gtk_list_store_append ( process_list->list_store, |
| 495 | &hashed_process_data->y_iter); |
| 496 | |
| 497 | gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter, |
| 498 | PROCESS_COLUMN, name, |
| 499 | PID_COLUMN, pid, |
| 500 | PPID_COLUMN, ppid, |
| 501 | CPU_COLUMN, cpu, |
| 502 | BIRTH_S_COLUMN, birth->tv_sec, |
| 503 | BIRTH_NS_COLUMN, birth->tv_nsec, |
| 504 | TRACE_COLUMN, trace_num, |
| 505 | -1); |
| 506 | #if 0 |
| 507 | hashed_process_data->row_ref = gtk_tree_row_reference_new ( |
| 508 | GTK_TREE_MODEL(process_list->list_store), |
| 509 | gtk_tree_model_get_path( |
| 510 | GTK_TREE_MODEL(process_list->list_store), |
| 511 | &iter)); |
| 512 | #endif //0 |
| 513 | g_hash_table_insert(process_list->process_hash, |
| 514 | (gpointer)Process_Info, |
| 515 | (gpointer)hashed_process_data); |
| 516 | |
| 517 | //g_critical ( "iter after : %s", gtk_tree_path_to_string ( |
| 518 | // gtk_tree_model_get_path ( |
| 519 | // GTK_TREE_MODEL(process_list->list_store), |
| 520 | // &iter))); |
| 521 | process_list->number_of_process++; |
| 522 | |
| 523 | *height = get_cell_height(process_list, |
| 524 | GTK_TREE_VIEW(process_list->process_list_widget)) |
| 525 | * process_list->number_of_process ; |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | int processlist_remove( ProcessList *process_list, |
| 531 | guint pid, |
| 532 | guint cpu, |
| 533 | LttTime *birth, |
| 534 | guint trace_num) |
| 535 | { |
| 536 | ProcessInfo process_info; |
| 537 | HashedProcessData *hashed_process_data; |
| 538 | GtkTreeIter iter; |
| 539 | |
| 540 | process_info.pid = pid; |
| 541 | if(pid == 0) |
| 542 | process_info.cpu = cpu; |
| 543 | else |
| 544 | process_info.cpu = 0; |
| 545 | process_info.birth = *birth; |
| 546 | process_info.trace_num = trace_num; |
| 547 | |
| 548 | |
| 549 | hashed_process_data = |
| 550 | (HashedProcessData*)g_hash_table_lookup( |
| 551 | process_list->process_hash, |
| 552 | &process_info); |
| 553 | if(likely(hashed_process_data != NULL)) |
| 554 | { |
| 555 | iter = hashed_process_data->y_iter; |
| 556 | |
| 557 | gtk_list_store_remove (process_list->list_store, &iter); |
| 558 | |
| 559 | g_hash_table_remove(process_list->process_hash, |
| 560 | &process_info); |
| 561 | |
| 562 | if(likely(process_list->current_hash_data != NULL)) { |
| 563 | if(likely(hashed_process_data == process_list->current_hash_data[cpu])) { |
| 564 | process_list->current_hash_data[cpu] = NULL; |
| 565 | } |
| 566 | } |
| 567 | process_list->number_of_process--; |
| 568 | |
| 569 | return 0; |
| 570 | } else { |
| 571 | return 1; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | |
| 576 | #if 0 |
| 577 | static inline guint get_cpu_number_from_name(GQuark name) |
| 578 | { |
| 579 | const gchar *string; |
| 580 | char *begin; |
| 581 | guint cpu; |
| 582 | |
| 583 | string = g_quark_to_string(name); |
| 584 | |
| 585 | begin = strrchr(string, '/'); |
| 586 | begin++; |
| 587 | |
| 588 | g_assert(begin != '\0'); |
| 589 | |
| 590 | cpu = strtoul(begin, NULL, 10); |
| 591 | |
| 592 | return cpu; |
| 593 | } |
| 594 | #endif //0 |