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 inline 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 static guint
process_list_hash_fct(gconstpointer key
)
238 guint pid
= ((ProcessInfo
*)key
)->pid
;
239 return ((pid
>>8 ^ pid
>>4 ^ pid
>>2 ^ pid
) ^ ((ProcessInfo
*)key
)->cpu
);
242 static gboolean
process_list_equ_fct(gconstpointer a
, gconstpointer b
)
244 const ProcessInfo
*pa
= (const ProcessInfo
*)a
;
245 const ProcessInfo
*pb
= (const ProcessInfo
*)b
;
247 if(pa
->pid
!= pb
->pid
)
250 if((pa
->pid
== 0 && (pa
->cpu
!= pb
->cpu
)))
253 if(pa
->birth
.tv_sec
!= pb
->birth
.tv_sec
)
256 if(pa
->birth
.tv_nsec
!= pb
->birth
.tv_nsec
)
259 if(pa
->trace_num
!= pb
->trace_num
)
265 void destroy_hash_key(gpointer key
);
267 void destroy_hash_data(gpointer data
);
272 ProcessList
*processlist_construct(void)
274 GtkTreeViewColumn
*column
;
275 GtkCellRenderer
*renderer
;
277 ProcessList
* process_list
= g_new(ProcessList
,1);
279 process_list
->number_of_process
= 0;
280 process_list
->cell_height_cache
= -1;
282 process_list
->current_hash_data
= NULL
;
284 /* Create the Process list */
285 process_list
->list_store
= gtk_list_store_new ( N_COLUMNS
,
295 process_list
->process_list_widget
=
296 gtk_tree_view_new_with_model
297 (GTK_TREE_MODEL (process_list
->list_store
));
298 g_object_unref (G_OBJECT (process_list
->list_store
));
300 gtk_tree_sortable_set_sort_func(
301 GTK_TREE_SORTABLE(process_list
->list_store
),
307 gtk_tree_sortable_set_sort_column_id(
308 GTK_TREE_SORTABLE(process_list
->list_store
),
312 process_list
->process_hash
= g_hash_table_new_full(
313 process_list_hash_fct
, process_list_equ_fct
,
314 destroy_hash_key
, destroy_hash_data
318 gtk_tree_view_set_headers_visible(
319 GTK_TREE_VIEW(process_list
->process_list_widget
), TRUE
);
321 /* Create a column, associating the "text" attribute of the
322 * cell_renderer to the first column of the model */
323 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
324 renderer
= gtk_cell_renderer_text_new ();
325 column
= gtk_tree_view_column_new_with_attributes ( "Process",
330 gtk_tree_view_column_set_alignment (column
, 0.0);
331 gtk_tree_view_column_set_fixed_width (column
, 45);
332 gtk_tree_view_append_column (
333 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
335 process_list
->button
= column
->button
;
337 column
= gtk_tree_view_column_new_with_attributes ( "PID",
342 gtk_tree_view_append_column (
343 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
345 column
= gtk_tree_view_column_new_with_attributes ( "PPID",
350 gtk_tree_view_append_column (
351 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
353 column
= gtk_tree_view_column_new_with_attributes ( "CPU",
358 gtk_tree_view_append_column (
359 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
361 column
= gtk_tree_view_column_new_with_attributes ( "Birth sec",
366 gtk_tree_view_append_column (
367 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
369 //gtk_tree_view_column_set_visible(column, 0);
371 column
= gtk_tree_view_column_new_with_attributes ( "Birth nsec",
376 gtk_tree_view_append_column (
377 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
379 column
= gtk_tree_view_column_new_with_attributes ( "TRACE",
384 gtk_tree_view_append_column (
385 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
388 //gtk_tree_view_column_set_visible(column, 0);
390 g_object_set_data_full(
391 G_OBJECT(process_list
->process_list_widget
),
394 (GDestroyNotify
)processlist_destroy
);
399 void processlist_destroy(ProcessList
*process_list
)
401 g_debug("processlist_destroy %p", process_list
);
402 g_hash_table_destroy(process_list
->process_hash
);
403 process_list
->process_hash
= NULL
;
405 g_free(process_list
);
406 g_debug("processlist_destroy end");
409 static gboolean
remove_hash_item(ProcessInfo
*process_info
,
410 HashedProcessData
*hashed_process_data
,
411 ProcessList
*process_list
)
415 iter
= hashed_process_data
->y_iter
;
417 gtk_list_store_remove (process_list
->list_store
, &iter
);
419 if(process_list
->current_hash_data
!= NULL
) {
420 if(hashed_process_data
==
421 process_list
->current_hash_data
[process_info
->cpu
])
422 process_list
->current_hash_data
[process_info
->cpu
] = NULL
;
424 return TRUE
; /* remove the element from the hash table */
427 void processlist_clear(ProcessList
*process_list
)
429 g_info("processlist_clear %p", process_list
);
431 g_hash_table_foreach_remove(process_list
->process_hash
,
432 (GHRFunc
)remove_hash_item
,
433 (gpointer
)process_list
);
434 process_list
->number_of_process
= 0;
438 GtkWidget
*processlist_get_widget(ProcessList
*process_list
)
440 return process_list
->process_list_widget
;
444 void destroy_hash_key(gpointer key
)
449 void destroy_hash_data(gpointer data
)
454 int processlist_add( ProcessList
*process_list
,
462 ProcessInfo
**pm_process_info
,
463 HashedProcessData
**pm_hashed_process_data
)
465 ProcessInfo
*Process_Info
= g_new(ProcessInfo
, 1);
466 HashedProcessData
*hashed_process_data
= g_new(HashedProcessData
, 1);
467 *pm_hashed_process_data
= hashed_process_data
;
468 *pm_process_info
= Process_Info
;
470 Process_Info
->pid
= pid
;
472 Process_Info
->cpu
= cpu
;
474 Process_Info
->cpu
= 0;
475 Process_Info
->ppid
= ppid
;
476 Process_Info
->birth
= *birth
;
477 Process_Info
->trace_num
= trace_num
;
479 /* When we create it from before state update, we are sure that the
480 * last event occured before the beginning of the global area.
482 * If it is created after state update, this value (0) will be
483 * overriden by the new state before anything is drawn.
485 hashed_process_data
->x
.over
= 0;
486 hashed_process_data
->x
.over_used
= FALSE
;
487 hashed_process_data
->x
.over_marked
= FALSE
;
488 hashed_process_data
->x
.middle
= 0;
489 hashed_process_data
->x
.middle_used
= FALSE
;
490 hashed_process_data
->x
.middle_marked
= FALSE
;
491 hashed_process_data
->x
.under
= 0;
492 hashed_process_data
->x
.under_used
= FALSE
;
493 hashed_process_data
->x
.under_marked
= FALSE
;
494 hashed_process_data
->next_good_time
= ltt_time_zero
;
496 /* Add a new row to the model */
497 gtk_list_store_append ( process_list
->list_store
,
498 &hashed_process_data
->y_iter
);
500 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
501 PROCESS_COLUMN
, name
,
505 BIRTH_S_COLUMN
, birth
->tv_sec
,
506 BIRTH_NS_COLUMN
, birth
->tv_nsec
,
507 TRACE_COLUMN
, trace_num
,
510 hashed_process_data
->row_ref
= gtk_tree_row_reference_new (
511 GTK_TREE_MODEL(process_list
->list_store
),
512 gtk_tree_model_get_path(
513 GTK_TREE_MODEL(process_list
->list_store
),
516 g_hash_table_insert(process_list
->process_hash
,
517 (gpointer
)Process_Info
,
518 (gpointer
)hashed_process_data
);
520 //g_critical ( "iter after : %s", gtk_tree_path_to_string (
521 // gtk_tree_model_get_path (
522 // GTK_TREE_MODEL(process_list->list_store),
524 process_list
->number_of_process
++;
526 *height
= get_cell_height(process_list
,
527 GTK_TREE_VIEW(process_list
->process_list_widget
))
528 * process_list
->number_of_process
;
533 int processlist_remove( ProcessList
*process_list
,
539 ProcessInfo process_info
;
541 HashedProcessData
*hashed_process_data
;
544 process_info
.pid
= pid
;
546 process_info
.cpu
= cpu
;
548 process_info
.cpu
= 0;
549 process_info
.birth
= *birth
;
550 process_info
.trace_num
= trace_num
;
553 if(hashed_process_data
=
554 (HashedProcessData
*)g_hash_table_lookup(
555 process_list
->process_hash
,
558 iter
= hashed_process_data
->y_iter
;
560 gtk_list_store_remove (process_list
->list_store
, &iter
);
562 g_hash_table_remove(process_list
->process_hash
,
565 if(process_list
->current_hash_data
!= NULL
) {
566 if(hashed_process_data
== process_list
->current_hash_data
[cpu
]) {
567 process_list
->current_hash_data
[cpu
] = NULL
;
570 process_list
->number_of_process
--;
580 static inline guint
get_cpu_number_from_name(GQuark name
)
586 string
= g_quark_to_string(name
);
588 begin
= strrchr(string
, '/');
591 g_assert(begin
!= '\0');
593 cpu
= strtoul(begin
, NULL
, 10);