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,
25 #include "processlist.h"
29 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
30 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
32 /* Preallocated Size of the index_to_pixmap array */
33 #define ALLOCATE_PROCESSES 1000
35 /*****************************************************************************
36 * Methods to synchronize process list *
37 *****************************************************************************/
39 //static inline guint get_cpu_number_from_name(GQuark name);
41 /* Enumeration of the columns */
55 gint
process_sort_func ( GtkTreeModel
*model
,
61 guint a_pid
, a_ppid
, a_cpu
;
62 gulong a_birth_s
, a_birth_ns
;
66 guint b_pid
, b_ppid
, b_cpu
;
67 gulong b_birth_s
, b_birth_ns
;
70 gtk_tree_model_get(model
,
81 gtk_tree_model_get(model
,
94 if(a_pid
== 0 && b_pid
== 0) {
95 /* If 0, order by CPU */
96 if(a_cpu
> b_cpu
) return 1;
97 if(a_cpu
< b_cpu
) return -1;
99 } else { /* if not 0, order by pid */
101 if(a_pid
> b_pid
) return 1;
102 if(a_pid
< b_pid
) return -1;
105 /* Order by birth second */
107 if(a_birth_s
> b_birth_s
) return 1;
108 if(a_birth_s
< b_birth_s
) return -1;
111 /* Order by birth nanosecond */
112 if(a_birth_ns
> b_birth_ns
) return 1;
113 if(a_birth_ns
< b_birth_ns
) return -1;
115 /* Order by trace_num */
116 if(a_trace
> b_trace
) return 1;
117 if(a_trace
< b_trace
) return -1;
123 static guint
process_list_hash_fct(gconstpointer key
)
125 guint pid
= ((const ProcessInfo
*)key
)->pid
;
126 return ((pid
>>8 ^ pid
>>4 ^ pid
>>2 ^ pid
) ^ ((const ProcessInfo
*)key
)->cpu
);
129 /* If hash is good, should be different */
130 static gboolean
process_list_equ_fct(gconstpointer a
, gconstpointer b
)
132 const ProcessInfo
*pa
= (const ProcessInfo
*)a
;
133 const ProcessInfo
*pb
= (const ProcessInfo
*)b
;
137 if(likely(pa
->pid
!= pb
->pid
))
139 else if(likely((pa
->pid
== 0 && (pa
->cpu
!= pb
->cpu
))))
141 else if(unlikely(ltt_time_compare(pa
->birth
, pb
->birth
) != 0))
143 else if(unlikely(pa
->trace_num
!= pb
->trace_num
))
149 void destroy_hash_key(gpointer key
);
151 void destroy_hash_data(gpointer data
);
154 static void update_index_to_pixmap_each(ProcessInfo
*key
,
155 HashedProcessData
*value
,
156 ProcessList
*process_list
)
158 guint array_index
= processlist_get_index_from_data(process_list
, value
);
160 g_assert(array_index
< process_list
->index_to_pixmap
->len
);
163 (GdkPixmap
**)&g_ptr_array_index(process_list
->index_to_pixmap
, array_index
);
165 *pixmap
= value
->pixmap
;
169 static void update_index_to_pixmap(ProcessList
*process_list
)
171 g_ptr_array_set_size(process_list
->index_to_pixmap
,
172 g_hash_table_size(process_list
->process_hash
));
173 g_hash_table_foreach(process_list
->process_hash
,
174 (GHFunc
)update_index_to_pixmap_each
,
179 static void update_pixmap_size_each(ProcessInfo
*key
,
180 HashedProcessData
*value
,
183 GdkPixmap
*old_pixmap
= value
->pixmap
;
186 gdk_pixmap_new(old_pixmap
,
191 gdk_pixmap_unref(old_pixmap
);
195 void update_pixmap_size(ProcessList
*process_list
, guint width
)
197 g_hash_table_foreach(process_list
->process_hash
,
198 (GHFunc
)update_pixmap_size_each
,
203 typedef struct _CopyPixmap
{
207 gint xsrc
, ysrc
, xdest
, ydest
, width
, height
;
210 static void copy_pixmap_region_each(ProcessInfo
*key
,
211 HashedProcessData
*value
,
214 GdkPixmap
*src
= cp
->src
;
215 GdkPixmap
*dest
= cp
->dest
;
218 dest
= value
->pixmap
;
222 gdk_draw_drawable (dest
,
226 cp
->xdest
, cp
->ydest
,
227 cp
->width
, cp
->height
);
233 void copy_pixmap_region(ProcessList
*process_list
, GdkDrawable
*dest
,
234 GdkGC
*gc
, GdkDrawable
*src
,
235 gint xsrc
, gint ysrc
,
236 gint xdest
, gint ydest
, gint width
, gint height
)
238 CopyPixmap cp
= { dest
, gc
, src
, xsrc
, ysrc
, xdest
, ydest
, width
, height
};
240 g_hash_table_foreach(process_list
->process_hash
,
241 (GHFunc
)copy_pixmap_region_each
,
247 typedef struct _RectanglePixmap
{
249 gint x
, y
, width
, height
;
253 static void rectangle_pixmap_each(ProcessInfo
*key
,
254 HashedProcessData
*value
,
258 rp
->height
= value
->height
;
260 gdk_draw_rectangle (value
->pixmap
,
264 rp
->width
, rp
->height
);
270 void rectangle_pixmap(ProcessList
*process_list
, GdkGC
*gc
,
271 gboolean filled
, gint x
, gint y
, gint width
, gint height
)
273 RectanglePixmap rp
= { filled
, x
, y
, width
, height
, gc
};
275 g_hash_table_foreach(process_list
->process_hash
,
276 (GHFunc
)rectangle_pixmap_each
,
281 /* Renders each pixmaps into on big drawable */
282 void copy_pixmap_to_screen(ProcessList
*process_list
,
286 gint width
, gint height
)
288 if(process_list
->index_to_pixmap
->len
== 0) return;
289 guint cell_height
= process_list
->cell_height
;
291 //cell_height = 24; //FIXME
293 gint begin
= floor(y
/(double)cell_height
);
294 gint end
= MIN(ceil((y
+height
)/(double)cell_height
),
295 process_list
->index_to_pixmap
->len
);
298 for(i
=begin
; i
<end
; i
++) {
299 g_assert(i
<process_list
->index_to_pixmap
->len
);
300 /* Render the pixmap to the screen */
302 (GdkPixmap
*)g_ptr_array_index(process_list
->index_to_pixmap
, i
);
304 gdk_draw_drawable (dest
,
324 ProcessList
*processlist_construct(void)
326 GtkTreeViewColumn
*column
;
327 GtkCellRenderer
*renderer
;
329 ProcessList
* process_list
= g_new(ProcessList
,1);
331 process_list
->number_of_process
= 0;
333 process_list
->current_hash_data
= NULL
;
335 /* Create the Process list */
336 process_list
->list_store
= gtk_list_store_new ( N_COLUMNS
,
346 process_list
->process_list_widget
=
347 gtk_tree_view_new_with_model
348 (GTK_TREE_MODEL (process_list
->list_store
));
349 g_object_unref (G_OBJECT (process_list
->list_store
));
351 gtk_tree_sortable_set_sort_func(
352 GTK_TREE_SORTABLE(process_list
->list_store
),
358 gtk_tree_sortable_set_sort_column_id(
359 GTK_TREE_SORTABLE(process_list
->list_store
),
363 process_list
->process_hash
= g_hash_table_new_full(
364 process_list_hash_fct
, process_list_equ_fct
,
365 destroy_hash_key
, destroy_hash_data
369 gtk_tree_view_set_headers_visible(
370 GTK_TREE_VIEW(process_list
->process_list_widget
), TRUE
);
372 /* Create a column, associating the "text" attribute of the
373 * cell_renderer to the first column of the model */
374 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
375 renderer
= gtk_cell_renderer_text_new ();
376 process_list
->renderer
= renderer
;
378 gtk_cell_renderer_get_size(renderer
,
379 GTK_WIDGET(process_list
->process_list_widget
),
384 &process_list
->cell_height
);
386 column
= gtk_tree_view_column_new_with_attributes ( "Process",
391 gtk_tree_view_column_set_alignment (column
, 0.0);
392 gtk_tree_view_column_set_fixed_width (column
, 45);
393 gtk_tree_view_append_column (
394 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
396 process_list
->button
= column
->button
;
398 column
= gtk_tree_view_column_new_with_attributes ( "PID",
403 gtk_tree_view_append_column (
404 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
406 column
= gtk_tree_view_column_new_with_attributes ( "PPID",
411 gtk_tree_view_append_column (
412 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
414 column
= gtk_tree_view_column_new_with_attributes ( "CPU",
419 gtk_tree_view_append_column (
420 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
422 column
= gtk_tree_view_column_new_with_attributes ( "Birth sec",
427 gtk_tree_view_append_column (
428 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
430 //gtk_tree_view_column_set_visible(column, 0);
432 column
= gtk_tree_view_column_new_with_attributes ( "Birth nsec",
437 gtk_tree_view_append_column (
438 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
440 column
= gtk_tree_view_column_new_with_attributes ( "TRACE",
445 gtk_tree_view_append_column (
446 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
449 //gtk_tree_view_column_set_visible(column, 0);
451 g_object_set_data_full(
452 G_OBJECT(process_list
->process_list_widget
),
455 (GDestroyNotify
)processlist_destroy
);
457 process_list
->index_to_pixmap
= g_ptr_array_sized_new(ALLOCATE_PROCESSES
);
462 void processlist_destroy(ProcessList
*process_list
)
464 g_debug("processlist_destroy %p", process_list
);
465 g_hash_table_destroy(process_list
->process_hash
);
466 process_list
->process_hash
= NULL
;
467 g_ptr_array_free(process_list
->index_to_pixmap
, TRUE
);
469 g_free(process_list
);
470 g_debug("processlist_destroy end");
473 static gboolean
remove_hash_item(ProcessInfo
*process_info
,
474 HashedProcessData
*hashed_process_data
,
475 ProcessList
*process_list
)
479 iter
= hashed_process_data
->y_iter
;
481 gtk_list_store_remove (process_list
->list_store
, &iter
);
482 gdk_pixmap_unref(hashed_process_data
->pixmap
);
484 if(likely(process_list
->current_hash_data
!= NULL
)) {
485 if(likely(hashed_process_data
==
486 process_list
->current_hash_data
[process_info
->cpu
]))
487 process_list
->current_hash_data
[process_info
->cpu
] = NULL
;
489 return TRUE
; /* remove the element from the hash table */
492 void processlist_clear(ProcessList
*process_list
)
494 g_info("processlist_clear %p", process_list
);
496 g_hash_table_foreach_remove(process_list
->process_hash
,
497 (GHRFunc
)remove_hash_item
,
498 (gpointer
)process_list
);
499 process_list
->number_of_process
= 0;
500 update_index_to_pixmap(process_list
);
504 GtkWidget
*processlist_get_widget(ProcessList
*process_list
)
506 return process_list
->process_list_widget
;
510 void destroy_hash_key(gpointer key
)
515 void destroy_hash_data(gpointer data
)
520 int processlist_add( ProcessList
*process_list
,
529 ProcessInfo
**pm_process_info
,
530 HashedProcessData
**pm_hashed_process_data
)
532 ProcessInfo
*Process_Info
= g_new(ProcessInfo
, 1);
533 HashedProcessData
*hashed_process_data
= g_new(HashedProcessData
, 1);
534 *pm_hashed_process_data
= hashed_process_data
;
535 *pm_process_info
= Process_Info
;
537 Process_Info
->pid
= pid
;
539 Process_Info
->cpu
= cpu
;
541 Process_Info
->cpu
= 0;
542 Process_Info
->ppid
= ppid
;
543 Process_Info
->birth
= *birth
;
544 Process_Info
->trace_num
= trace_num
;
546 /* When we create it from before state update, we are sure that the
547 * last event occured before the beginning of the global area.
549 * If it is created after state update, this value (0) will be
550 * overriden by the new state before anything is drawn.
552 hashed_process_data
->x
.over
= 0;
553 hashed_process_data
->x
.over_used
= FALSE
;
554 hashed_process_data
->x
.over_marked
= FALSE
;
555 hashed_process_data
->x
.middle
= 0;
556 hashed_process_data
->x
.middle_used
= FALSE
;
557 hashed_process_data
->x
.middle_marked
= FALSE
;
558 hashed_process_data
->x
.under
= 0;
559 hashed_process_data
->x
.under_used
= FALSE
;
560 hashed_process_data
->x
.under_marked
= FALSE
;
561 hashed_process_data
->next_good_time
= ltt_time_zero
;
563 /* Add a new row to the model */
564 gtk_list_store_append ( process_list
->list_store
,
565 &hashed_process_data
->y_iter
);
567 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
568 PROCESS_COLUMN
, name
,
572 BIRTH_S_COLUMN
, birth
->tv_sec
,
573 BIRTH_NS_COLUMN
, birth
->tv_nsec
,
574 TRACE_COLUMN
, trace_num
,
576 //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget),
577 // GTK_TREE_MODEL(process_list->list_store));
578 //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget));
580 g_hash_table_insert(process_list
->process_hash
,
581 (gpointer
)Process_Info
,
582 (gpointer
)hashed_process_data
);
584 process_list
->number_of_process
++;
586 hashed_process_data
->height
= process_list
->cell_height
;
588 //hashed_process_data->height = 24; // FIXME
589 g_assert(hashed_process_data
->height
!= 0);
591 *height
= hashed_process_data
->height
* process_list
->number_of_process
;
593 hashed_process_data
->pixmap
=
594 gdk_pixmap_new(drawing
->drawing_area
->window
,
595 drawing
->alloc_width
,
596 hashed_process_data
->height
,
600 gdk_draw_rectangle (hashed_process_data
->pixmap
,
601 drawing
->drawing_area
->style
->black_gc
,
604 drawing
->alloc_width
,
605 hashed_process_data
->height
);
607 update_index_to_pixmap(process_list
);
613 int processlist_remove( ProcessList
*process_list
,
619 ProcessInfo process_info
;
620 HashedProcessData
*hashed_process_data
;
623 process_info
.pid
= pid
;
625 process_info
.cpu
= cpu
;
627 process_info
.cpu
= 0;
628 process_info
.birth
= *birth
;
629 process_info
.trace_num
= trace_num
;
632 hashed_process_data
=
633 (HashedProcessData
*)g_hash_table_lookup(
634 process_list
->process_hash
,
636 if(likely(hashed_process_data
!= NULL
))
638 iter
= hashed_process_data
->y_iter
;
640 gtk_list_store_remove (process_list
->list_store
, &iter
);
642 g_hash_table_remove(process_list
->process_hash
,
645 if(likely(process_list
->current_hash_data
!= NULL
)) {
646 if(likely(hashed_process_data
== process_list
->current_hash_data
[cpu
])) {
647 process_list
->current_hash_data
[cpu
] = NULL
;
651 gdk_pixmap_unref(hashed_process_data
->pixmap
);
653 update_index_to_pixmap(process_list
);
655 process_list
->number_of_process
--;
665 static inline guint
get_cpu_number_from_name(GQuark name
)
671 string
= g_quark_to_string(name
);
673 begin
= strrchr(string
, '/');
676 g_assert(begin
!= '\0');
678 cpu
= strtoul(begin
, NULL
, 10);