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,
30 #include "processlist.h"
34 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
36 /* Preallocated Size of the index_to_pixmap array */
37 #define ALLOCATE_PROCESSES 1000
39 /*****************************************************************************
40 * Methods to synchronize process list *
41 *****************************************************************************/
44 gint
process_sort_func ( GtkTreeModel
*model
,
50 guint a_pid
, a_tgid
, a_ppid
, a_cpu
;
51 gulong a_birth_s
, a_birth_ns
;
55 guint b_pid
, b_tgid
, b_ppid
, b_cpu
;
56 gulong b_birth_s
, b_birth_ns
;
59 gtk_tree_model_get(model
,
61 PROCESS_COLUMN
, &a_name
,
66 BIRTH_S_COLUMN
, &a_birth_s
,
67 BIRTH_NS_COLUMN
, &a_birth_ns
,
68 TRACE_COLUMN
, &a_trace
,
71 gtk_tree_model_get(model
,
73 PROCESS_COLUMN
, &b_name
,
78 BIRTH_S_COLUMN
, &b_birth_s
,
79 BIRTH_NS_COLUMN
, &b_birth_ns
,
80 TRACE_COLUMN
, &b_trace
,
85 if(a_pid
== 0 && b_pid
== 0) {
86 /* If 0, order by CPU */
87 if(a_cpu
> b_cpu
) return 1;
88 if(a_cpu
< b_cpu
) return -1;
90 } else { /* if not 0, order by pid */
92 if(a_pid
> b_pid
) return 1;
93 if(a_pid
< b_pid
) return -1;
96 /* Order by birth second */
98 if(a_birth_s
> b_birth_s
) return 1;
99 if(a_birth_s
< b_birth_s
) return -1;
102 /* Order by birth nanosecond */
103 if(a_birth_ns
> b_birth_ns
) return 1;
104 if(a_birth_ns
< b_birth_ns
) return -1;
106 /* Order by trace_num */
107 if(a_trace
> b_trace
) return 1;
108 if(a_trace
< b_trace
) return -1;
114 static guint
process_list_hash_fct(gconstpointer key
)
116 guint pid
= ((const ProcessInfo
*)key
)->pid
;
117 return ((pid
>>8 ^ pid
>>4 ^ pid
>>2 ^ pid
) ^ ((const ProcessInfo
*)key
)->cpu
);
120 /* If hash is good, should be different */
121 static gboolean
process_list_equ_fct(gconstpointer a
, gconstpointer b
)
123 const ProcessInfo
*pa
= (const ProcessInfo
*)a
;
124 const ProcessInfo
*pb
= (const ProcessInfo
*)b
;
128 if(likely(pa
->pid
!= pb
->pid
))
130 if(likely((pa
->pid
== 0 && (pa
->cpu
!= pb
->cpu
))))
132 //TODO ybrosseau 2012-09-18 Check if we want to reenable birth comparision
133 // We currently do not have valid birth info, so don't compare it
134 /* if(unlikely(ltt_time_compare(pa->birth, pb->birth) != 0))
136 if(unlikely(pa
->trace_num
!= pb
->trace_num
))
142 void destroy_hash_key(gpointer key
);
144 void destroy_hash_data(gpointer data
);
147 gboolean
scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer data
)
149 ControlFlowData
*control_flow_data
=
150 (ControlFlowData
*)g_object_get_data(
152 "control_flow_data");
153 unsigned int cell_height
=
154 get_cell_height(GTK_TREE_VIEW(control_flow_data
->process_list
->process_list_widget
));
156 switch(event
->direction
) {
158 gtk_adjustment_set_value(control_flow_data
->v_adjust
,
159 gtk_adjustment_get_value(control_flow_data
->v_adjust
) - cell_height
);
161 case GDK_SCROLL_DOWN
:
162 gtk_adjustment_set_value(control_flow_data
->v_adjust
,
163 gtk_adjustment_get_value(control_flow_data
->v_adjust
) + cell_height
);
166 g_error("should only scroll up and down.");
172 static void update_index_to_pixmap_each(ProcessInfo
*key
,
173 HashedProcessData
*value
,
174 ProcessList
*process_list
)
176 guint array_index
= processlist_get_index_from_data(process_list
, value
);
178 g_assert(array_index
< process_list
->index_to_pixmap
->len
);
181 (GdkPixmap
**)&g_ptr_array_index(process_list
->index_to_pixmap
, array_index
);
183 *pixmap
= value
->pixmap
;
187 void update_index_to_pixmap(ProcessList
*process_list
)
189 g_ptr_array_set_size(process_list
->index_to_pixmap
,
190 g_hash_table_size(process_list
->process_hash
));
191 g_hash_table_foreach(process_list
->process_hash
,
192 (GHFunc
)update_index_to_pixmap_each
,
197 static void update_pixmap_size_each(ProcessInfo
*key
,
198 HashedProcessData
*value
,
201 GdkPixmap
*old_pixmap
= value
->pixmap
;
204 gdk_pixmap_new(old_pixmap
,
209 gdk_pixmap_unref(old_pixmap
);
213 void update_pixmap_size(ProcessList
*process_list
, guint width
)
215 g_hash_table_foreach(process_list
->process_hash
,
216 (GHFunc
)update_pixmap_size_each
,
217 GUINT_TO_POINTER(width
));
221 typedef struct _CopyPixmap
{
225 gint xsrc
, ysrc
, xdest
, ydest
, width
, height
;
228 static void copy_pixmap_region_each(ProcessInfo
*key
,
229 HashedProcessData
*value
,
232 GdkPixmap
*src
= cp
->src
;
233 GdkPixmap
*dest
= cp
->dest
;
236 dest
= value
->pixmap
;
240 gdk_draw_drawable (dest
,
244 cp
->xdest
, cp
->ydest
,
245 cp
->width
, cp
->height
);
251 void copy_pixmap_region(ProcessList
*process_list
, GdkDrawable
*dest
,
252 GdkGC
*gc
, GdkDrawable
*src
,
253 gint xsrc
, gint ysrc
,
254 gint xdest
, gint ydest
, gint width
, gint height
)
256 CopyPixmap cp
= { dest
, gc
, src
, xsrc
, ysrc
, xdest
, ydest
, width
, height
};
258 g_hash_table_foreach(process_list
->process_hash
,
259 (GHFunc
)copy_pixmap_region_each
,
265 typedef struct _RectanglePixmap
{
267 gint x
, y
, width
, height
;
271 static void rectangle_pixmap_each(ProcessInfo
*key
,
272 HashedProcessData
*value
,
276 rp
->height
= value
->height
;
278 gdk_draw_rectangle (value
->pixmap
,
282 rp
->width
, rp
->height
);
288 void rectangle_pixmap(ProcessList
*process_list
, GdkGC
*gc
,
289 gboolean filled
, gint x
, gint y
, gint width
, gint height
)
291 RectanglePixmap rp
= { filled
, x
, y
, width
, height
, gc
};
293 g_hash_table_foreach(process_list
->process_hash
,
294 (GHFunc
)rectangle_pixmap_each
,
299 /* Renders each pixmaps into on big drawable */
300 void copy_pixmap_to_screen(ProcessList
*process_list
,
304 gint width
, gint height
)
306 if(process_list
->index_to_pixmap
->len
== 0) return;
307 guint cell_height
= process_list
->cell_height
;
310 gint begin
= floor(y
/(double)cell_height
);
311 gint end
= MIN(ceil((y
+height
)/(double)cell_height
),
312 process_list
->index_to_pixmap
->len
);
315 for(i
=begin
; i
<end
; i
++) {
316 g_assert(i
<process_list
->index_to_pixmap
->len
);
317 /* Render the pixmap to the screen */
319 //(GdkPixmap*)g_ptr_array_index(process_list->index_to_pixmap, i);
320 GDK_PIXMAP(g_ptr_array_index(process_list
->index_to_pixmap
, i
));
322 gdk_draw_drawable (dest
,
342 ProcessList
*processlist_construct(void)
344 GtkTreeViewColumn
*column
;
345 GtkCellRenderer
*renderer
;
347 ProcessList
* process_list
= g_new(ProcessList
,1);
349 process_list
->number_of_process
= 0;
351 process_list
->current_hash_data
= NULL
;
353 /* Create the Process list */
354 process_list
->list_store
= gtk_list_store_new ( N_COLUMNS
,
365 process_list
->process_list_widget
=
366 gtk_tree_view_new_with_model
367 (GTK_TREE_MODEL (process_list
->list_store
));
369 g_object_unref (G_OBJECT (process_list
->list_store
));
371 gtk_tree_sortable_set_default_sort_func(
372 GTK_TREE_SORTABLE(process_list
->list_store
),
378 gtk_tree_sortable_set_sort_column_id(
379 GTK_TREE_SORTABLE(process_list
->list_store
),
380 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID
,
384 process_list
->process_hash
= g_hash_table_new_full(
385 process_list_hash_fct
, process_list_equ_fct
,
386 destroy_hash_key
, destroy_hash_data
390 gtk_tree_view_set_headers_visible(
391 GTK_TREE_VIEW(process_list
->process_list_widget
), TRUE
);
393 /* Create a column, associating the "text" attribute of the
394 * cell_renderer to the first column of the model */
395 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
396 renderer
= gtk_cell_renderer_text_new ();
397 process_list
->renderer
= renderer
;
399 /* Add a temporary row to the model to get the cell size when the first
400 * real process is added. */
403 path
= gtk_tree_path_new_first();
404 gtk_tree_model_get_iter (gtk_tree_view_get_model(GTK_TREE_VIEW(process_list
->process_list_widget
)), &iter
, path
);
405 gtk_list_store_append(process_list
->list_store
, &iter
);
406 gtk_tree_path_free(path
);
408 process_list
->cell_height
= 0; // not ready to get size yet.
410 column
= gtk_tree_view_column_new_with_attributes ( "Process",
415 gtk_tree_view_column_set_alignment (column
, 0.0);
416 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
417 gtk_tree_view_column_set_resizable(column
, TRUE
);
418 gtk_tree_view_append_column (
419 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
421 process_list
->button
= column
->button
;
423 column
= gtk_tree_view_column_new_with_attributes ( "PID",
428 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
429 gtk_tree_view_column_set_resizable(column
, TRUE
);
430 gtk_tree_view_append_column (
431 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
433 column
= gtk_tree_view_column_new_with_attributes ( "TGID",
438 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
439 gtk_tree_view_column_set_resizable(column
, TRUE
);
440 gtk_tree_view_append_column (
441 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
443 column
= gtk_tree_view_column_new_with_attributes ( "PPID",
448 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
449 gtk_tree_view_column_set_resizable(column
, TRUE
);
450 gtk_tree_view_append_column (
451 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
453 column
= gtk_tree_view_column_new_with_attributes ( "CPU",
458 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
459 gtk_tree_view_column_set_resizable(column
, TRUE
);
460 gtk_tree_view_append_column (
461 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
463 column
= gtk_tree_view_column_new_with_attributes ( "Birth sec",
468 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
469 gtk_tree_view_column_set_resizable(column
, TRUE
);
470 gtk_tree_view_append_column (
471 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
473 //gtk_tree_view_column_set_visible(column, 0);
475 column
= gtk_tree_view_column_new_with_attributes ( "Birth nsec",
480 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
481 gtk_tree_view_column_set_resizable(column
, TRUE
);
482 gtk_tree_view_append_column (
483 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
485 column
= gtk_tree_view_column_new_with_attributes ( "TRACE",
490 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
491 gtk_tree_view_column_set_resizable(column
, TRUE
);
492 gtk_tree_view_append_column (
493 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
496 //gtk_tree_view_column_set_visible(column, 0);
498 g_object_set_data_full(
499 G_OBJECT(process_list
->process_list_widget
),
502 (GDestroyNotify
)processlist_destroy
);
504 process_list
->index_to_pixmap
= g_ptr_array_sized_new(ALLOCATE_PROCESSES
);
509 void processlist_destroy(ProcessList
*process_list
)
511 g_debug("processlist_destroy %p", process_list
);
512 g_hash_table_destroy(process_list
->process_hash
);
513 process_list
->process_hash
= NULL
;
514 g_ptr_array_free(process_list
->index_to_pixmap
, TRUE
);
516 g_free(process_list
);
517 g_debug("processlist_destroy end");
520 static gboolean
remove_hash_item(ProcessInfo
*process_info
,
521 HashedProcessData
*hashed_process_data
,
522 ProcessList
*process_list
)
526 iter
= hashed_process_data
->y_iter
;
528 gtk_list_store_remove (process_list
->list_store
, &iter
);
529 gdk_pixmap_unref(hashed_process_data
->pixmap
);
531 if(likely(process_list
->current_hash_data
!= NULL
)) {
532 if(likely(hashed_process_data
==
533 process_list
->current_hash_data
[process_info
->trace_num
][process_info
->cpu
]))
534 process_list
->current_hash_data
[process_info
->trace_num
][process_info
->cpu
] = NULL
;
536 return TRUE
; /* remove the element from the hash table */
539 void processlist_clear(ProcessList
*process_list
)
541 g_info("processlist_clear %p", process_list
);
543 g_hash_table_foreach_remove(process_list
->process_hash
,
544 (GHRFunc
)remove_hash_item
,
545 (gpointer
)process_list
);
546 process_list
->number_of_process
= 0;
547 update_index_to_pixmap(process_list
);
551 GtkWidget
*processlist_get_widget(ProcessList
*process_list
)
553 return process_list
->process_list_widget
;
557 void destroy_hash_key(gpointer key
)
562 void destroy_hash_data(gpointer data
)
568 void processlist_set_name(ProcessList
*process_list
,
570 HashedProcessData
*hashed_process_data
)
572 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
573 PROCESS_COLUMN
, g_quark_to_string(name
),
577 void processlist_set_tgid(ProcessList
*process_list
,
579 HashedProcessData
*hashed_process_data
)
581 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
586 void processlist_set_ppid(ProcessList
*process_list
,
588 HashedProcessData
*hashed_process_data
)
590 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
596 int processlist_add( ProcessList
*process_list
,
606 ProcessInfo
**pm_process_info
,
607 HashedProcessData
**pm_hashed_process_data
)
609 ProcessInfo
*Process_Info
= g_new(ProcessInfo
, 1);
610 HashedProcessData
*hashed_process_data
= g_new(HashedProcessData
, 1);
611 *pm_hashed_process_data
= hashed_process_data
;
612 *pm_process_info
= Process_Info
;
614 Process_Info
->pid
= pid
;
615 Process_Info
->tgid
= tgid
;
617 Process_Info
->cpu
= cpu
;
619 Process_Info
->cpu
= 0;
620 Process_Info
->ppid
= ppid
;
621 Process_Info
->birth
= *birth
;
622 Process_Info
->trace_num
= trace_num
;
624 /* When we create it from before state update, we are sure that the
625 * last event occured before the beginning of the global area.
627 * If it is created after state update, this value (0) will be
628 * overriden by the new state before anything is drawn.
630 hashed_process_data
->x
.over
= 0;
631 hashed_process_data
->x
.over_used
= FALSE
;
632 hashed_process_data
->x
.over_marked
= FALSE
;
633 hashed_process_data
->x
.middle
= 0;
634 hashed_process_data
->x
.middle_used
= FALSE
;
635 hashed_process_data
->x
.middle_marked
= FALSE
;
636 hashed_process_data
->x
.under
= 0;
637 hashed_process_data
->x
.under_used
= FALSE
;
638 hashed_process_data
->x
.under_marked
= FALSE
;
639 hashed_process_data
->next_good_time
= ltt_time_zero
;
641 if (process_list
->cell_height
== 0) {
646 path
= gtk_tree_path_new_first();
647 gtk_tree_model_get_iter (gtk_tree_view_get_model(GTK_TREE_VIEW(process_list
->process_list_widget
)), &iter
, path
);
648 gtk_tree_view_get_background_area(
649 GTK_TREE_VIEW(process_list
->process_list_widget
),
651 gtk_list_store_remove(process_list
->list_store
, &iter
);
652 gtk_tree_path_free (path
);
653 process_list
->cell_height
= rect
.height
;
656 /* Add a new row to the model */
657 gtk_list_store_append ( process_list
->list_store
,
658 &hashed_process_data
->y_iter
);
660 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
661 PROCESS_COLUMN
, g_quark_to_string(name
),
666 BIRTH_S_COLUMN
, birth
->tv_sec
,
667 BIRTH_NS_COLUMN
, birth
->tv_nsec
,
668 TRACE_COLUMN
, trace_num
,
670 //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget),
671 // GTK_TREE_MODEL(process_list->list_store));
672 //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget));
674 g_hash_table_insert(process_list
->process_hash
,
675 (gpointer
)Process_Info
,
676 (gpointer
)hashed_process_data
);
678 process_list
->number_of_process
++;
682 gtk_widget_queue_draw(process_list
->process_list_widget
);
683 path
= gtk_tree_path_new_first();
684 gtk_tree_view_get_background_area(GTK_TREE_VIEW(process_list
->process_list_widget
),
686 gtk_tree_path_free (path
);
687 process_list
->cell_height
= rect
.height
;
691 hashed_process_data
->height
= process_list
->cell_height
;
692 g_assert(hashed_process_data
->height
!= 0);
694 *height
= hashed_process_data
->height
* process_list
->number_of_process
;
696 hashed_process_data
->pixmap
=
697 gdk_pixmap_new(drawing
->drawing_area
->window
,
698 drawing
->alloc_width
,
699 hashed_process_data
->height
,
703 gdk_draw_rectangle (hashed_process_data
->pixmap
,
704 drawing
->drawing_area
->style
->black_gc
,
707 drawing
->alloc_width
,
708 hashed_process_data
->height
);
710 update_index_to_pixmap(process_list
);
716 int processlist_remove( ProcessList
*process_list
,
722 ProcessInfo process_info
;
723 HashedProcessData
*hashed_process_data
;
726 process_info
.pid
= pid
;
728 process_info
.cpu
= cpu
;
730 process_info
.cpu
= 0;
731 process_info
.birth
= *birth
;
732 process_info
.trace_num
= trace_num
;
735 hashed_process_data
=
736 (HashedProcessData
*)g_hash_table_lookup(
737 process_list
->process_hash
,
739 if(likely(hashed_process_data
!= NULL
))
741 iter
= hashed_process_data
->y_iter
;
743 gtk_list_store_remove (process_list
->list_store
, &iter
);
745 g_hash_table_remove(process_list
->process_hash
,
748 if(likely(process_list
->current_hash_data
!= NULL
)) {
749 if(likely(hashed_process_data
== process_list
->current_hash_data
[trace_num
][cpu
])) {
750 process_list
->current_hash_data
[trace_num
][cpu
] = NULL
;
754 gdk_pixmap_unref(hashed_process_data
->pixmap
);
756 update_index_to_pixmap(process_list
);
758 process_list
->number_of_process
--;
768 static inline guint
get_cpu_number_from_name(GQuark name
)
774 string
= g_quark_to_string(name
);
776 begin
= strrchr(string
, '/');
779 g_assert(begin
!= '\0');
781 cpu
= strtoul(begin
, NULL
, 10);