| 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 | |
| 22 | #include "processlist.h" |
| 23 | #include "drawitem.h" |
| 24 | |
| 25 | /***************************************************************************** |
| 26 | * Methods to synchronize process list * |
| 27 | *****************************************************************************/ |
| 28 | |
| 29 | /* Enumeration of the columns */ |
| 30 | enum |
| 31 | { |
| 32 | PROCESS_COLUMN, |
| 33 | PID_COLUMN, |
| 34 | BIRTH_S_COLUMN, |
| 35 | BIRTH_NS_COLUMN, |
| 36 | N_COLUMNS |
| 37 | }; |
| 38 | |
| 39 | |
| 40 | gint process_sort_func ( GtkTreeModel *model, |
| 41 | GtkTreeIter *it_a, |
| 42 | GtkTreeIter *it_b, |
| 43 | gpointer user_data) |
| 44 | { |
| 45 | GValue a, b; |
| 46 | |
| 47 | memset(&a, 0, sizeof(GValue)); |
| 48 | memset(&b, 0, sizeof(GValue)); |
| 49 | |
| 50 | /* Order by PID */ |
| 51 | gtk_tree_model_get_value( model, |
| 52 | it_a, |
| 53 | PID_COLUMN, |
| 54 | &a); |
| 55 | |
| 56 | gtk_tree_model_get_value( model, |
| 57 | it_b, |
| 58 | PID_COLUMN, |
| 59 | &b); |
| 60 | |
| 61 | if(G_VALUE_TYPE(&a) == G_TYPE_UINT |
| 62 | && G_VALUE_TYPE(&b) == G_TYPE_UINT ) |
| 63 | { |
| 64 | if(g_value_get_uint(&a) > g_value_get_uint(&b)) |
| 65 | { |
| 66 | g_value_unset(&a); |
| 67 | g_value_unset(&b); |
| 68 | return 1; |
| 69 | } |
| 70 | if(g_value_get_uint(&a) < g_value_get_uint(&b)) |
| 71 | { |
| 72 | g_value_unset(&a); |
| 73 | g_value_unset(&b); |
| 74 | return 0; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | g_value_unset(&a); |
| 79 | g_value_unset(&b); |
| 80 | |
| 81 | |
| 82 | /* Order by birth second */ |
| 83 | gtk_tree_model_get_value( model, |
| 84 | it_a, |
| 85 | BIRTH_S_COLUMN, |
| 86 | &a); |
| 87 | |
| 88 | gtk_tree_model_get_value( model, |
| 89 | it_b, |
| 90 | BIRTH_S_COLUMN, |
| 91 | &b); |
| 92 | |
| 93 | |
| 94 | if(G_VALUE_TYPE(&a) == G_TYPE_ULONG |
| 95 | && G_VALUE_TYPE(&b) == G_TYPE_ULONG ) |
| 96 | { |
| 97 | if(g_value_get_ulong(&a) > g_value_get_ulong(&b)) |
| 98 | { |
| 99 | g_value_unset(&a); |
| 100 | g_value_unset(&b); |
| 101 | return 1; |
| 102 | } |
| 103 | if(g_value_get_ulong(&a) < g_value_get_ulong(&b)) |
| 104 | { |
| 105 | g_value_unset(&a); |
| 106 | g_value_unset(&b); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |
| 112 | g_value_unset(&a); |
| 113 | g_value_unset(&b); |
| 114 | |
| 115 | /* Order by birth nanosecond */ |
| 116 | gtk_tree_model_get_value( model, |
| 117 | it_a, |
| 118 | BIRTH_NS_COLUMN, |
| 119 | &a); |
| 120 | |
| 121 | gtk_tree_model_get_value( model, |
| 122 | it_b, |
| 123 | BIRTH_NS_COLUMN, |
| 124 | &b); |
| 125 | |
| 126 | |
| 127 | if(G_VALUE_TYPE(&a) == G_TYPE_ULONG |
| 128 | && G_VALUE_TYPE(&b) == G_TYPE_ULONG ) |
| 129 | { |
| 130 | if(g_value_get_ulong(&a) > g_value_get_ulong(&b)) |
| 131 | { |
| 132 | g_value_unset(&a); |
| 133 | g_value_unset(&b); |
| 134 | return 1; |
| 135 | } |
| 136 | // Final condition |
| 137 | //if(g_value_get_ulong(&a) < g_value_get_ulong(&b)) |
| 138 | //{ |
| 139 | // g_value_unset(&a); |
| 140 | // g_value_unset(&b); |
| 141 | // return 0; |
| 142 | //} |
| 143 | |
| 144 | } |
| 145 | |
| 146 | g_value_unset(&a); |
| 147 | g_value_unset(&b); |
| 148 | |
| 149 | return 0; |
| 150 | |
| 151 | } |
| 152 | |
| 153 | guint hash_fct(gconstpointer key) |
| 154 | { |
| 155 | return ((ProcessInfo*)key)->pid; |
| 156 | } |
| 157 | |
| 158 | gboolean equ_fct(gconstpointer a, gconstpointer b) |
| 159 | { |
| 160 | if(((ProcessInfo*)a)->pid != ((ProcessInfo*)b)->pid) |
| 161 | return 0; |
| 162 | // g_critical("compare %u and %u",((ProcessInfo*)a)->pid,((ProcessInfo*)b)->pid); |
| 163 | if(((ProcessInfo*)a)->birth.tv_sec != ((ProcessInfo*)b)->birth.tv_sec) |
| 164 | return 0; |
| 165 | // g_critical("compare %u and %u",((ProcessInfo*)a)->birth.tv_sec,((ProcessInfo*)b)->birth.tv_sec); |
| 166 | |
| 167 | if(((ProcessInfo*)a)->birth.tv_nsec != ((ProcessInfo*)b)->birth.tv_nsec) |
| 168 | return 0; |
| 169 | // g_critical("compare %u and %u",((ProcessInfo*)a)->birth.tv_nsec,((ProcessInfo*)b)->birth.tv_nsec); |
| 170 | |
| 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | void destroy_hash_key(gpointer key); |
| 175 | |
| 176 | void destroy_hash_data(gpointer data); |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | ProcessList *processlist_construct(void) |
| 182 | { |
| 183 | GtkTreeViewColumn *column; |
| 184 | GtkCellRenderer *renderer; |
| 185 | |
| 186 | ProcessList* process_list = g_new(ProcessList,1); |
| 187 | |
| 188 | process_list->number_of_process = 0; |
| 189 | |
| 190 | /* Create the Process list */ |
| 191 | process_list->list_store = gtk_list_store_new ( N_COLUMNS, |
| 192 | G_TYPE_STRING, |
| 193 | G_TYPE_UINT, |
| 194 | G_TYPE_ULONG, |
| 195 | G_TYPE_ULONG); |
| 196 | |
| 197 | |
| 198 | process_list->process_list_widget = |
| 199 | gtk_tree_view_new_with_model |
| 200 | (GTK_TREE_MODEL (process_list->list_store)); |
| 201 | |
| 202 | g_object_unref (G_OBJECT (process_list->list_store)); |
| 203 | |
| 204 | gtk_tree_sortable_set_sort_func( |
| 205 | GTK_TREE_SORTABLE(process_list->list_store), |
| 206 | PID_COLUMN, |
| 207 | process_sort_func, |
| 208 | NULL, |
| 209 | NULL); |
| 210 | |
| 211 | gtk_tree_sortable_set_sort_column_id( |
| 212 | GTK_TREE_SORTABLE(process_list->list_store), |
| 213 | PID_COLUMN, |
| 214 | GTK_SORT_ASCENDING); |
| 215 | |
| 216 | process_list->process_hash = g_hash_table_new_full( |
| 217 | hash_fct, equ_fct, |
| 218 | destroy_hash_key, destroy_hash_data |
| 219 | ); |
| 220 | |
| 221 | |
| 222 | gtk_tree_view_set_headers_visible( |
| 223 | GTK_TREE_VIEW(process_list->process_list_widget), FALSE); |
| 224 | |
| 225 | /* Create a column, associating the "text" attribute of the |
| 226 | * cell_renderer to the first column of the model */ |
| 227 | /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */ |
| 228 | renderer = gtk_cell_renderer_text_new (); |
| 229 | column = gtk_tree_view_column_new_with_attributes ( "Process", |
| 230 | renderer, |
| 231 | "text", |
| 232 | PROCESS_COLUMN, |
| 233 | NULL); |
| 234 | gtk_tree_view_column_set_alignment (column, 0.0); |
| 235 | gtk_tree_view_column_set_fixed_width (column, 45); |
| 236 | gtk_tree_view_append_column ( |
| 237 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 238 | |
| 239 | column = gtk_tree_view_column_new_with_attributes ( "PID", |
| 240 | renderer, |
| 241 | "text", |
| 242 | PID_COLUMN, |
| 243 | NULL); |
| 244 | gtk_tree_view_append_column ( |
| 245 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 246 | |
| 247 | |
| 248 | column = gtk_tree_view_column_new_with_attributes ( "Birth sec", |
| 249 | renderer, |
| 250 | "text", |
| 251 | BIRTH_S_COLUMN, |
| 252 | NULL); |
| 253 | gtk_tree_view_append_column ( |
| 254 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 255 | |
| 256 | //gtk_tree_view_column_set_visible(column, 0); |
| 257 | // |
| 258 | column = gtk_tree_view_column_new_with_attributes ( "Birth nsec", |
| 259 | renderer, |
| 260 | "text", |
| 261 | BIRTH_NS_COLUMN, |
| 262 | NULL); |
| 263 | gtk_tree_view_append_column ( |
| 264 | GTK_TREE_VIEW (process_list->process_list_widget), column); |
| 265 | |
| 266 | //gtk_tree_view_column_set_visible(column, 0); |
| 267 | |
| 268 | g_object_set_data_full( |
| 269 | G_OBJECT(process_list->process_list_widget), |
| 270 | "process_list_Data", |
| 271 | process_list, |
| 272 | (GDestroyNotify)processlist_destroy); |
| 273 | |
| 274 | return process_list; |
| 275 | } |
| 276 | void processlist_destroy(ProcessList *process_list) |
| 277 | { |
| 278 | g_hash_table_destroy(process_list->process_hash); |
| 279 | process_list->process_hash = NULL; |
| 280 | |
| 281 | g_free(process_list); |
| 282 | } |
| 283 | |
| 284 | GtkWidget *processlist_get_widget(ProcessList *process_list) |
| 285 | { |
| 286 | return process_list->process_list_widget; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | |
| 291 | gint get_cell_height(GtkTreeView *tree_view) |
| 292 | { |
| 293 | gint height; |
| 294 | GtkTreeViewColumn *Column = gtk_tree_view_get_column(tree_view, 0); |
| 295 | //GList *Render_List = gtk_tree_view_column_get_cell_renderers(Column); |
| 296 | //GtkCellRenderer *Renderer = g_list_first(Render_List)->data; |
| 297 | |
| 298 | //g_list_free(Render_List); |
| 299 | gtk_tree_view_column_cell_get_size(Column, NULL, NULL, NULL, NULL, &height); |
| 300 | //g_critical("cell 0 height : %u",height); |
| 301 | |
| 302 | return height; |
| 303 | } |
| 304 | |
| 305 | void destroy_hash_key(gpointer key) |
| 306 | { |
| 307 | g_free(key); |
| 308 | } |
| 309 | |
| 310 | void destroy_hash_data(gpointer data) |
| 311 | { |
| 312 | g_free(data); |
| 313 | } |
| 314 | |
| 315 | int processlist_add( ProcessList *process_list, |
| 316 | guint pid, |
| 317 | LttTime *birth, |
| 318 | gchar *name, |
| 319 | guint *height, |
| 320 | HashedProcessData **pm_hashed_process_data) |
| 321 | { |
| 322 | GtkTreeIter iter ; |
| 323 | ProcessInfo *Process_Info = g_new(ProcessInfo, 1); |
| 324 | HashedProcessData *hashed_process_data = g_new(HashedProcessData, 1); |
| 325 | *pm_hashed_process_data = hashed_process_data; |
| 326 | |
| 327 | Process_Info->pid = pid; |
| 328 | Process_Info->birth = *birth; |
| 329 | |
| 330 | hashed_process_data->draw_context = g_new(DrawContext, 1); |
| 331 | hashed_process_data->draw_context->drawable = NULL; |
| 332 | hashed_process_data->draw_context->gc = NULL; |
| 333 | hashed_process_data->draw_context->pango_layout = NULL; |
| 334 | hashed_process_data->draw_context->current = g_new(DrawInfo,1); |
| 335 | hashed_process_data->draw_context->current->over = g_new(ItemInfo,1); |
| 336 | hashed_process_data->draw_context->current->over->x = -1; |
| 337 | hashed_process_data->draw_context->current->over->y = -1; |
| 338 | hashed_process_data->draw_context->current->middle = g_new(ItemInfo,1); |
| 339 | hashed_process_data->draw_context->current->middle->x = -1; |
| 340 | hashed_process_data->draw_context->current->middle->y = -1; |
| 341 | hashed_process_data->draw_context->current->under = g_new(ItemInfo,1); |
| 342 | hashed_process_data->draw_context->current->under->x = -1; |
| 343 | hashed_process_data->draw_context->current->under->y = -1; |
| 344 | hashed_process_data->draw_context->current->modify_over = g_new(ItemInfo,1); |
| 345 | hashed_process_data->draw_context->current->modify_over->x = -1; |
| 346 | hashed_process_data->draw_context->current->modify_over->y = -1; |
| 347 | hashed_process_data->draw_context->current->modify_middle = g_new(ItemInfo,1); |
| 348 | hashed_process_data->draw_context->current->modify_middle->x = -1; |
| 349 | hashed_process_data->draw_context->current->modify_middle->y = -1; |
| 350 | hashed_process_data->draw_context->current->modify_under = g_new(ItemInfo,1); |
| 351 | hashed_process_data->draw_context->current->modify_under->x = -1; |
| 352 | hashed_process_data->draw_context->current->modify_under->y = -1; |
| 353 | hashed_process_data->draw_context->current->status = LTTV_STATE_UNNAMED; |
| 354 | hashed_process_data->draw_context->previous = g_new(DrawInfo,1); |
| 355 | hashed_process_data->draw_context->previous->over = g_new(ItemInfo,1); |
| 356 | hashed_process_data->draw_context->previous->over->x = -1; |
| 357 | hashed_process_data->draw_context->previous->over->y = -1; |
| 358 | hashed_process_data->draw_context->previous->middle = g_new(ItemInfo,1); |
| 359 | hashed_process_data->draw_context->previous->middle->x = -1; |
| 360 | hashed_process_data->draw_context->previous->middle->y = -1; |
| 361 | hashed_process_data->draw_context->previous->under = g_new(ItemInfo,1); |
| 362 | hashed_process_data->draw_context->previous->under->x = -1; |
| 363 | hashed_process_data->draw_context->previous->under->y = -1; |
| 364 | hashed_process_data->draw_context->previous->modify_over = g_new(ItemInfo,1); |
| 365 | hashed_process_data->draw_context->previous->modify_over->x = -1; |
| 366 | hashed_process_data->draw_context->previous->modify_over->y = -1; |
| 367 | hashed_process_data->draw_context->previous->modify_middle = g_new(ItemInfo,1); |
| 368 | hashed_process_data->draw_context->previous->modify_middle->x = -1; |
| 369 | hashed_process_data->draw_context->previous->modify_middle->y = -1; |
| 370 | hashed_process_data->draw_context->previous->modify_under = g_new(ItemInfo,1); |
| 371 | hashed_process_data->draw_context->previous->modify_under->x = -1; |
| 372 | hashed_process_data->draw_context->previous->modify_under->y = -1; |
| 373 | hashed_process_data->draw_context->previous->status = LTTV_STATE_UNNAMED; |
| 374 | |
| 375 | /* Add a new row to the model */ |
| 376 | gtk_list_store_append ( process_list->list_store, &iter); |
| 377 | //g_critical ( "iter before : %s", gtk_tree_path_to_string ( |
| 378 | // gtk_tree_model_get_path ( |
| 379 | // GTK_TREE_MODEL(process_list->list_store), |
| 380 | // &iter))); |
| 381 | gtk_list_store_set ( process_list->list_store, &iter, |
| 382 | PROCESS_COLUMN, name, |
| 383 | PID_COLUMN, pid, |
| 384 | BIRTH_S_COLUMN, birth->tv_sec, |
| 385 | BIRTH_NS_COLUMN, birth->tv_nsec, |
| 386 | -1); |
| 387 | hashed_process_data->row_ref = gtk_tree_row_reference_new ( |
| 388 | GTK_TREE_MODEL(process_list->list_store), |
| 389 | gtk_tree_model_get_path( |
| 390 | GTK_TREE_MODEL(process_list->list_store), |
| 391 | &iter)); |
| 392 | g_hash_table_insert( process_list->process_hash, |
| 393 | (gpointer)Process_Info, |
| 394 | (gpointer)hashed_process_data); |
| 395 | |
| 396 | //g_critical ( "iter after : %s", gtk_tree_path_to_string ( |
| 397 | // gtk_tree_model_get_path ( |
| 398 | // GTK_TREE_MODEL(process_list->list_store), |
| 399 | // &iter))); |
| 400 | process_list->number_of_process++; |
| 401 | |
| 402 | *height = get_cell_height(GTK_TREE_VIEW(process_list->process_list_widget)) |
| 403 | * process_list->number_of_process ; |
| 404 | |
| 405 | |
| 406 | return 0; |
| 407 | |
| 408 | } |
| 409 | |
| 410 | int processlist_remove( ProcessList *process_list, |
| 411 | guint pid, |
| 412 | LttTime *birth) |
| 413 | { |
| 414 | ProcessInfo Process_Info; |
| 415 | gint *path_indices; |
| 416 | HashedProcessData *hashed_process_data; |
| 417 | GtkTreeIter iter; |
| 418 | |
| 419 | Process_Info.pid = pid; |
| 420 | Process_Info.birth = *birth; |
| 421 | |
| 422 | |
| 423 | if(hashed_process_data = |
| 424 | (HashedProcessData*)g_hash_table_lookup( |
| 425 | process_list->process_hash, |
| 426 | &Process_Info)) |
| 427 | { |
| 428 | gtk_tree_model_get_iter ( |
| 429 | GTK_TREE_MODEL(process_list->list_store), |
| 430 | &iter, |
| 431 | gtk_tree_row_reference_get_path( |
| 432 | (GtkTreeRowReference*)hashed_process_data->row_ref) |
| 433 | ); |
| 434 | |
| 435 | gtk_list_store_remove (process_list->list_store, &iter); |
| 436 | |
| 437 | g_free(hashed_process_data->draw_context->previous->modify_under); |
| 438 | g_free(hashed_process_data->draw_context->previous->modify_middle); |
| 439 | g_free(hashed_process_data->draw_context->previous->modify_over); |
| 440 | g_free(hashed_process_data->draw_context->previous->under); |
| 441 | g_free(hashed_process_data->draw_context->previous->middle); |
| 442 | g_free(hashed_process_data->draw_context->previous->over); |
| 443 | g_free(hashed_process_data->draw_context->previous); |
| 444 | g_free(hashed_process_data->draw_context->current->modify_under); |
| 445 | g_free(hashed_process_data->draw_context->current->modify_middle); |
| 446 | g_free(hashed_process_data->draw_context->current->modify_over); |
| 447 | g_free(hashed_process_data->draw_context->current->under); |
| 448 | g_free(hashed_process_data->draw_context->current->middle); |
| 449 | g_free(hashed_process_data->draw_context->current->over); |
| 450 | g_free(hashed_process_data->draw_context->current); |
| 451 | g_free(hashed_process_data->draw_context); |
| 452 | g_free(hashed_process_data); |
| 453 | |
| 454 | g_hash_table_remove(process_list->process_hash, |
| 455 | &Process_Info); |
| 456 | |
| 457 | process_list->number_of_process--; |
| 458 | |
| 459 | return 0; |
| 460 | } else { |
| 461 | return 1; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | |
| 466 | guint processlist_get_height(ProcessList *process_list) |
| 467 | { |
| 468 | return get_cell_height(GTK_TREE_VIEW(process_list->process_list_widget)) |
| 469 | * process_list->number_of_process ; |
| 470 | } |
| 471 | |
| 472 | |
| 473 | gint processlist_get_process_pixels( ProcessList *process_list, |
| 474 | guint pid, LttTime *birth, |
| 475 | guint *y, |
| 476 | guint *height, |
| 477 | HashedProcessData **pm_hashed_process_data) |
| 478 | { |
| 479 | ProcessInfo Process_Info; |
| 480 | gint *path_indices; |
| 481 | GtkTreePath *tree_path; |
| 482 | HashedProcessData *hashed_process_data = NULL; |
| 483 | |
| 484 | Process_Info.pid = pid; |
| 485 | Process_Info.birth = *birth; |
| 486 | |
| 487 | if(hashed_process_data = |
| 488 | (HashedProcessData*)g_hash_table_lookup( |
| 489 | process_list->process_hash, |
| 490 | &Process_Info)) |
| 491 | { |
| 492 | tree_path = gtk_tree_row_reference_get_path( |
| 493 | hashed_process_data->row_ref); |
| 494 | path_indices = gtk_tree_path_get_indices (tree_path); |
| 495 | |
| 496 | *height = get_cell_height( |
| 497 | GTK_TREE_VIEW(process_list->process_list_widget)); |
| 498 | *y = *height * path_indices[0]; |
| 499 | *pm_hashed_process_data = hashed_process_data; |
| 500 | return 0; |
| 501 | } else { |
| 502 | *pm_hashed_process_data = hashed_process_data; |
| 503 | return 1; |
| 504 | } |
| 505 | |
| 506 | } |
| 507 | |
| 508 | |
| 509 | gint processlist_get_pixels_from_data( ProcessList *process_list, |
| 510 | ProcessInfo *process_info, |
| 511 | HashedProcessData *hashed_process_data, |
| 512 | guint *y, |
| 513 | guint *height) |
| 514 | { |
| 515 | gint *path_indices; |
| 516 | GtkTreePath *tree_path; |
| 517 | |
| 518 | tree_path = gtk_tree_row_reference_get_path( |
| 519 | hashed_process_data->row_ref); |
| 520 | path_indices = gtk_tree_path_get_indices (tree_path); |
| 521 | |
| 522 | *height = get_cell_height( |
| 523 | GTK_TREE_VIEW(process_list->process_list_widget)); |
| 524 | *y = *height * path_indices[0]; |
| 525 | |
| 526 | return 0; |
| 527 | |
| 528 | } |