1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Mathieu Desnoyers
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;
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.
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,
24 #include "processlist.h"
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)
31 /*****************************************************************************
32 * Methods to synchronize process list *
33 *****************************************************************************/
35 static guint
get_cpu_number_from_name(GQuark name
);
37 /* Enumeration of the columns */
51 gint
process_sort_func ( GtkTreeModel
*model
,
58 memset(&a
, 0, sizeof(GValue
));
59 memset(&b
, 0, sizeof(GValue
));
62 gtk_tree_model_get_value( model
,
67 gtk_tree_model_get_value( model
,
72 if(G_VALUE_TYPE(&a
) == G_TYPE_UINT
73 && G_VALUE_TYPE(&b
) == G_TYPE_UINT
)
77 if(g_value_get_uint(&a
) == 0 && g_value_get_uint(&b
) == 0) {
81 memset(&cpua
, 0, sizeof(GValue
));
82 memset(&cpub
, 0, sizeof(GValue
));
84 /* If 0, order by CPU */
85 gtk_tree_model_get_value( model
,
90 gtk_tree_model_get_value( model
,
95 if(G_VALUE_TYPE(&cpua
) == G_TYPE_UINT
96 && G_VALUE_TYPE(&cpub
) == G_TYPE_UINT
)
98 if(g_value_get_uint(&cpua
) > g_value_get_uint(&cpub
))
100 g_value_unset(&cpua
);
101 g_value_unset(&cpub
);
104 if(g_value_get_uint(&cpua
) < g_value_get_uint(&cpub
))
106 g_value_unset(&cpua
);
107 g_value_unset(&cpub
);
112 g_value_unset(&cpua
);
113 g_value_unset(&cpub
);
115 } else { /* if not 0, order by pid */
117 if(g_value_get_uint(&a
) > g_value_get_uint(&b
))
123 if(g_value_get_uint(&a
) < g_value_get_uint(&b
))
137 /* Order by birth second */
138 gtk_tree_model_get_value( model
,
143 gtk_tree_model_get_value( model
,
149 if(G_VALUE_TYPE(&a
) == G_TYPE_ULONG
150 && G_VALUE_TYPE(&b
) == G_TYPE_ULONG
)
152 if(g_value_get_ulong(&a
) > g_value_get_ulong(&b
))
158 if(g_value_get_ulong(&a
) < g_value_get_ulong(&b
))
170 /* Order by birth nanosecond */
171 gtk_tree_model_get_value( model
,
176 gtk_tree_model_get_value( model
,
182 if(G_VALUE_TYPE(&a
) == G_TYPE_ULONG
183 && G_VALUE_TYPE(&b
) == G_TYPE_ULONG
)
185 if(g_value_get_ulong(&a
) > g_value_get_ulong(&b
))
191 if(g_value_get_ulong(&a
) < g_value_get_ulong(&b
))
203 /* Order by trace_num */
204 gtk_tree_model_get_value( model
,
209 gtk_tree_model_get_value( model
,
214 if(G_VALUE_TYPE(&a
) == G_TYPE_ULONG
215 && G_VALUE_TYPE(&b
) == G_TYPE_ULONG
)
217 if(g_value_get_ulong(&a
) > g_value_get_ulong(&b
))
223 if(g_value_get_ulong(&a
) < g_value_get_ulong(&b
))
236 guint
hash_fct(gconstpointer key
)
238 return ((ProcessInfo
*)key
)->pid
;
241 gboolean
equ_fct(gconstpointer a
, gconstpointer b
)
243 const ProcessInfo
*pa
= (const ProcessInfo
*)a
;
244 const ProcessInfo
*pb
= (const ProcessInfo
*)b
;
246 if(pa
->pid
!= pb
->pid
)
249 if((pa
->pid
== 0 && (pa
->cpu
!= pb
->cpu
)))
252 if(pa
->birth
.tv_sec
!= pb
->birth
.tv_sec
)
255 if(pa
->birth
.tv_nsec
!= pb
->birth
.tv_nsec
)
258 if(pa
->trace_num
!= pb
->trace_num
)
264 void destroy_hash_key(gpointer key
);
266 void destroy_hash_data(gpointer data
);
271 ProcessList
*processlist_construct(void)
273 GtkTreeViewColumn
*column
;
274 GtkCellRenderer
*renderer
;
276 ProcessList
* process_list
= g_new(ProcessList
,1);
278 process_list
->number_of_process
= 0;
280 /* Create the Process list */
281 process_list
->list_store
= gtk_list_store_new ( N_COLUMNS
,
291 process_list
->process_list_widget
=
292 gtk_tree_view_new_with_model
293 (GTK_TREE_MODEL (process_list
->list_store
));
294 g_object_unref (G_OBJECT (process_list
->list_store
));
296 gtk_tree_sortable_set_sort_func(
297 GTK_TREE_SORTABLE(process_list
->list_store
),
303 gtk_tree_sortable_set_sort_column_id(
304 GTK_TREE_SORTABLE(process_list
->list_store
),
308 process_list
->process_hash
= g_hash_table_new_full(
310 destroy_hash_key
, destroy_hash_data
314 gtk_tree_view_set_headers_visible(
315 GTK_TREE_VIEW(process_list
->process_list_widget
), TRUE
);
317 /* Create a column, associating the "text" attribute of the
318 * cell_renderer to the first column of the model */
319 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
320 renderer
= gtk_cell_renderer_text_new ();
321 column
= gtk_tree_view_column_new_with_attributes ( "Process",
326 gtk_tree_view_column_set_alignment (column
, 0.0);
327 gtk_tree_view_column_set_fixed_width (column
, 45);
328 gtk_tree_view_append_column (
329 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
331 process_list
->button
= column
->button
;
333 column
= gtk_tree_view_column_new_with_attributes ( "PID",
338 gtk_tree_view_append_column (
339 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
341 column
= gtk_tree_view_column_new_with_attributes ( "PPID",
346 gtk_tree_view_append_column (
347 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
349 column
= gtk_tree_view_column_new_with_attributes ( "CPU",
354 gtk_tree_view_append_column (
355 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
357 column
= gtk_tree_view_column_new_with_attributes ( "Birth sec",
362 gtk_tree_view_append_column (
363 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
365 //gtk_tree_view_column_set_visible(column, 0);
367 column
= gtk_tree_view_column_new_with_attributes ( "Birth nsec",
372 gtk_tree_view_append_column (
373 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
375 column
= gtk_tree_view_column_new_with_attributes ( "TRACE",
380 gtk_tree_view_append_column (
381 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
384 //gtk_tree_view_column_set_visible(column, 0);
386 g_object_set_data_full(
387 G_OBJECT(process_list
->process_list_widget
),
390 (GDestroyNotify
)processlist_destroy
);
395 void processlist_destroy(ProcessList
*process_list
)
397 g_debug("processlist_destroy %p", process_list
);
398 g_hash_table_destroy(process_list
->process_hash
);
399 process_list
->process_hash
= NULL
;
401 g_free(process_list
);
402 g_debug("processlist_destroy end");
405 static gboolean
remove_hash_item(ProcessInfo
*process_info
,
406 HashedProcessData
*hashed_process_data
,
407 ProcessList
*process_list
)
409 GtkTreePath
*tree_path
;
412 tree_path
= gtk_tree_row_reference_get_path(
413 hashed_process_data
->row_ref
);
415 gtk_tree_model_get_iter (
416 GTK_TREE_MODEL(process_list
->list_store
),
419 gtk_tree_path_free(tree_path
);
421 gtk_list_store_remove (process_list
->list_store
, &iter
);
423 return TRUE
; /* remove the element from the hash table */
426 void processlist_clear(ProcessList
*process_list
)
428 g_info("processlist_clear %p", process_list
);
430 g_hash_table_foreach_remove(process_list
->process_hash
,
431 (GHRFunc
)remove_hash_item
,
432 (gpointer
)process_list
);
433 process_list
->number_of_process
= 0;
437 GtkWidget
*processlist_get_widget(ProcessList
*process_list
)
439 return process_list
->process_list_widget
;
444 gint
get_cell_height(GtkTreeView
*tree_view
)
447 GtkTreeViewColumn
*Column
= gtk_tree_view_get_column(tree_view
, 0);
448 //GList *Render_List = gtk_tree_view_column_get_cell_renderers(Column);
449 //GtkCellRenderer *Renderer = g_list_first(Render_List)->data;
451 //g_list_free(Render_List);
452 gtk_tree_view_column_cell_get_size(Column
, NULL
, NULL
, NULL
, NULL
, &height
);
453 //g_critical("cell 0 height : %u",height);
458 void destroy_hash_key(gpointer key
)
463 void destroy_hash_data(gpointer data
)
468 int processlist_add( ProcessList
*process_list
,
476 HashedProcessData
**pm_hashed_process_data
)
479 ProcessInfo
*Process_Info
= g_new(ProcessInfo
, 1);
480 HashedProcessData
*hashed_process_data
= g_new(HashedProcessData
, 1);
481 *pm_hashed_process_data
= hashed_process_data
;
483 Process_Info
->pid
= pid
;
485 Process_Info
->cpu
= cpu
;
487 Process_Info
->cpu
= 0;
488 Process_Info
->ppid
= ppid
;
489 Process_Info
->birth
= *birth
;
490 Process_Info
->trace_num
= trace_num
;
492 /* When we create it from before state update, we are sure that the
493 * last event occured before the beginning of the global area.
495 * If it is created after state update, this value (0) will be
496 * overriden by the new state before anything is drawn.
498 hashed_process_data
->x
.over
= 0;
499 hashed_process_data
->x
.middle
= 0;
500 hashed_process_data
->x
.under
= 0;
502 /* Add a new row to the model */
503 gtk_list_store_append ( process_list
->list_store
, &iter
);
504 //g_critical ( "iter before : %s", gtk_tree_path_to_string (
505 // gtk_tree_model_get_path (
506 // GTK_TREE_MODEL(process_list->list_store),
508 gtk_list_store_set ( process_list
->list_store
, &iter
,
509 PROCESS_COLUMN
, name
,
512 CPU_COLUMN
, get_cpu_number_from_name(cpu
),
513 BIRTH_S_COLUMN
, birth
->tv_sec
,
514 BIRTH_NS_COLUMN
, birth
->tv_nsec
,
515 TRACE_COLUMN
, trace_num
,
517 hashed_process_data
->row_ref
= gtk_tree_row_reference_new (
518 GTK_TREE_MODEL(process_list
->list_store
),
519 gtk_tree_model_get_path(
520 GTK_TREE_MODEL(process_list
->list_store
),
522 g_hash_table_insert(process_list
->process_hash
,
523 (gpointer
)Process_Info
,
524 (gpointer
)hashed_process_data
);
526 //g_critical ( "iter after : %s", gtk_tree_path_to_string (
527 // gtk_tree_model_get_path (
528 // GTK_TREE_MODEL(process_list->list_store),
530 process_list
->number_of_process
++;
532 *height
= get_cell_height(GTK_TREE_VIEW(process_list
->process_list_widget
))
533 * process_list
->number_of_process
;
540 int processlist_remove( ProcessList
*process_list
,
546 ProcessInfo Process_Info
;
548 HashedProcessData
*hashed_process_data
;
551 Process_Info
.pid
= pid
;
552 Process_Info
.cpu
= cpu
;
553 Process_Info
.birth
= *birth
;
554 Process_Info
.trace_num
= trace_num
;
557 if(hashed_process_data
=
558 (HashedProcessData
*)g_hash_table_lookup(
559 process_list
->process_hash
,
562 GtkTreePath
*tree_path
;
564 tree_path
= gtk_tree_row_reference_get_path(
565 hashed_process_data
->row_ref
);
567 gtk_tree_model_get_iter (
568 GTK_TREE_MODEL(process_list
->list_store
),
571 gtk_tree_path_free(tree_path
);
573 gtk_list_store_remove (process_list
->list_store
, &iter
);
575 g_hash_table_remove(process_list
->process_hash
,
578 process_list
->number_of_process
--;
587 guint
processlist_get_height(ProcessList
*process_list
)
589 return get_cell_height(GTK_TREE_VIEW(process_list
->process_list_widget
))
590 * process_list
->number_of_process
;
594 gint
processlist_get_process_pixels( ProcessList
*process_list
,
595 guint pid
, guint cpu
, LttTime
*birth
, guint trace_num
,
598 HashedProcessData
**pm_hashed_process_data
)
600 ProcessInfo Process_Info
;
602 GtkTreePath
*tree_path
;
603 HashedProcessData
*hashed_process_data
= NULL
;
605 Process_Info
.pid
= pid
;
606 Process_Info
.cpu
= cpu
;
607 Process_Info
.birth
= *birth
;
608 Process_Info
.trace_num
= trace_num
;
610 if(hashed_process_data
=
611 (HashedProcessData
*)g_hash_table_lookup(
612 process_list
->process_hash
,
615 tree_path
= gtk_tree_row_reference_get_path(
616 hashed_process_data
->row_ref
);
617 path_indices
= gtk_tree_path_get_indices (tree_path
);
619 *height
= get_cell_height(
620 GTK_TREE_VIEW(process_list
->process_list_widget
));
621 *y
= *height
* path_indices
[0];
622 *pm_hashed_process_data
= hashed_process_data
;
623 gtk_tree_path_free(tree_path
);
627 *pm_hashed_process_data
= hashed_process_data
;
634 gint
processlist_get_pixels_from_data( ProcessList
*process_list
,
635 ProcessInfo
*process_info
,
636 HashedProcessData
*hashed_process_data
,
641 GtkTreePath
*tree_path
;
643 tree_path
= gtk_tree_row_reference_get_path(
644 hashed_process_data
->row_ref
);
645 path_indices
= gtk_tree_path_get_indices (tree_path
);
647 *height
= get_cell_height(
648 GTK_TREE_VIEW(process_list
->process_list_widget
));
649 *y
= *height
* path_indices
[0];
650 gtk_tree_path_free(tree_path
);
656 static guint
get_cpu_number_from_name(GQuark name
)
663 string
= g_quark_to_string(name
);
665 begin
= strrchr(string
, '/');
668 g_assert(begin
!= '\0');
670 cpu
= strtoul(begin
, NULL
, 10);