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
,
51 guint a_pid
, a_tgid
, a_ppid
, a_cpu
;
52 gulong a_birth_s
, a_birth_ns
;
57 guint b_pid
, b_tgid
, b_ppid
, b_cpu
;
58 gulong b_birth_s
, b_birth_ns
;
61 gtk_tree_model_get(model
,
63 PROCESS_COLUMN
, &a_name
,
64 BRAND_COLUMN
, &a_brand
,
69 BIRTH_S_COLUMN
, &a_birth_s
,
70 BIRTH_NS_COLUMN
, &a_birth_ns
,
71 TRACE_COLUMN
, &a_trace
,
74 gtk_tree_model_get(model
,
76 PROCESS_COLUMN
, &b_name
,
77 BRAND_COLUMN
, &b_brand
,
82 BIRTH_S_COLUMN
, &b_birth_s
,
83 BIRTH_NS_COLUMN
, &b_birth_ns
,
84 TRACE_COLUMN
, &b_trace
,
89 if(a_pid
== 0 && b_pid
== 0) {
90 /* If 0, order by CPU */
91 if(a_cpu
> b_cpu
) return 1;
92 if(a_cpu
< b_cpu
) return -1;
94 } else { /* if not 0, order by pid */
96 if(a_pid
> b_pid
) return 1;
97 if(a_pid
< b_pid
) return -1;
100 /* Order by birth second */
102 if(a_birth_s
> b_birth_s
) return 1;
103 if(a_birth_s
< b_birth_s
) return -1;
106 /* Order by birth nanosecond */
107 if(a_birth_ns
> b_birth_ns
) return 1;
108 if(a_birth_ns
< b_birth_ns
) return -1;
110 /* Order by trace_num */
111 if(a_trace
> b_trace
) return 1;
112 if(a_trace
< b_trace
) return -1;
118 static guint
process_list_hash_fct(gconstpointer key
)
120 guint pid
= ((const ProcessInfo
*)key
)->pid
;
121 return ((pid
>>8 ^ pid
>>4 ^ pid
>>2 ^ pid
) ^ ((const ProcessInfo
*)key
)->cpu
);
124 /* If hash is good, should be different */
125 static gboolean
process_list_equ_fct(gconstpointer a
, gconstpointer b
)
127 const ProcessInfo
*pa
= (const ProcessInfo
*)a
;
128 const ProcessInfo
*pb
= (const ProcessInfo
*)b
;
132 if(likely(pa
->pid
!= pb
->pid
))
134 if(likely((pa
->pid
== 0 && (pa
->cpu
!= pb
->cpu
))))
136 if(unlikely(ltt_time_compare(pa
->birth
, pb
->birth
) != 0))
138 if(unlikely(pa
->trace_num
!= pb
->trace_num
))
144 void destroy_hash_key(gpointer key
);
146 void destroy_hash_data(gpointer data
);
149 gboolean
scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer data
)
151 ControlFlowData
*control_flow_data
=
152 (ControlFlowData
*)g_object_get_data(
154 "control_flow_data");
155 Drawing_t
*drawing
= control_flow_data
->drawing
;
156 unsigned int cell_height
=
157 get_cell_height(GTK_TREE_VIEW(control_flow_data
->process_list
->process_list_widget
));
159 switch(event
->direction
) {
161 gtk_adjustment_set_value(control_flow_data
->v_adjust
,
162 gtk_adjustment_get_value(control_flow_data
->v_adjust
) - cell_height
);
164 case GDK_SCROLL_DOWN
:
165 gtk_adjustment_set_value(control_flow_data
->v_adjust
,
166 gtk_adjustment_get_value(control_flow_data
->v_adjust
) + cell_height
);
169 g_error("should only scroll up and down.");
175 static void update_index_to_pixmap_each(ProcessInfo
*key
,
176 HashedProcessData
*value
,
177 ProcessList
*process_list
)
179 guint array_index
= processlist_get_index_from_data(process_list
, value
);
181 g_assert(array_index
< process_list
->index_to_pixmap
->len
);
184 (GdkPixmap
**)&g_ptr_array_index(process_list
->index_to_pixmap
, array_index
);
186 *pixmap
= value
->pixmap
;
190 void update_index_to_pixmap(ProcessList
*process_list
)
192 g_ptr_array_set_size(process_list
->index_to_pixmap
,
193 g_hash_table_size(process_list
->process_hash
));
194 g_hash_table_foreach(process_list
->process_hash
,
195 (GHFunc
)update_index_to_pixmap_each
,
200 static void update_pixmap_size_each(ProcessInfo
*key
,
201 HashedProcessData
*value
,
204 GdkPixmap
*old_pixmap
= value
->pixmap
;
207 gdk_pixmap_new(old_pixmap
,
212 gdk_pixmap_unref(old_pixmap
);
216 void update_pixmap_size(ProcessList
*process_list
, guint width
)
218 g_hash_table_foreach(process_list
->process_hash
,
219 (GHFunc
)update_pixmap_size_each
,
220 GUINT_TO_POINTER(width
));
224 typedef struct _CopyPixmap
{
228 gint xsrc
, ysrc
, xdest
, ydest
, width
, height
;
231 static void copy_pixmap_region_each(ProcessInfo
*key
,
232 HashedProcessData
*value
,
235 GdkPixmap
*src
= cp
->src
;
236 GdkPixmap
*dest
= cp
->dest
;
239 dest
= value
->pixmap
;
243 gdk_draw_drawable (dest
,
247 cp
->xdest
, cp
->ydest
,
248 cp
->width
, cp
->height
);
254 void copy_pixmap_region(ProcessList
*process_list
, GdkDrawable
*dest
,
255 GdkGC
*gc
, GdkDrawable
*src
,
256 gint xsrc
, gint ysrc
,
257 gint xdest
, gint ydest
, gint width
, gint height
)
259 CopyPixmap cp
= { dest
, gc
, src
, xsrc
, ysrc
, xdest
, ydest
, width
, height
};
261 g_hash_table_foreach(process_list
->process_hash
,
262 (GHFunc
)copy_pixmap_region_each
,
268 typedef struct _RectanglePixmap
{
270 gint x
, y
, width
, height
;
274 static void rectangle_pixmap_each(ProcessInfo
*key
,
275 HashedProcessData
*value
,
279 rp
->height
= value
->height
;
281 gdk_draw_rectangle (value
->pixmap
,
285 rp
->width
, rp
->height
);
291 void rectangle_pixmap(ProcessList
*process_list
, GdkGC
*gc
,
292 gboolean filled
, gint x
, gint y
, gint width
, gint height
)
294 RectanglePixmap rp
= { filled
, x
, y
, width
, height
, gc
};
296 g_hash_table_foreach(process_list
->process_hash
,
297 (GHFunc
)rectangle_pixmap_each
,
302 /* Renders each pixmaps into on big drawable */
303 void copy_pixmap_to_screen(ProcessList
*process_list
,
307 gint width
, gint height
)
309 if(process_list
->index_to_pixmap
->len
== 0) return;
310 guint cell_height
= process_list
->cell_height
;
313 gint begin
= floor(y
/(double)cell_height
);
314 gint end
= MIN(ceil((y
+height
)/(double)cell_height
),
315 process_list
->index_to_pixmap
->len
);
318 for(i
=begin
; i
<end
; i
++) {
319 g_assert(i
<process_list
->index_to_pixmap
->len
);
320 /* Render the pixmap to the screen */
322 //(GdkPixmap*)g_ptr_array_index(process_list->index_to_pixmap, i);
323 GDK_PIXMAP(g_ptr_array_index(process_list
->index_to_pixmap
, i
));
325 gdk_draw_drawable (dest
,
345 ProcessList
*processlist_construct(void)
347 GtkTreeViewColumn
*column
;
348 GtkCellRenderer
*renderer
;
350 ProcessList
* process_list
= g_new(ProcessList
,1);
352 process_list
->number_of_process
= 0;
354 process_list
->current_hash_data
= NULL
;
356 /* Create the Process list */
357 process_list
->list_store
= gtk_list_store_new ( N_COLUMNS
,
369 process_list
->process_list_widget
=
370 gtk_tree_view_new_with_model
371 (GTK_TREE_MODEL (process_list
->list_store
));
373 g_object_unref (G_OBJECT (process_list
->list_store
));
375 gtk_tree_sortable_set_default_sort_func(
376 GTK_TREE_SORTABLE(process_list
->list_store
),
382 gtk_tree_sortable_set_sort_column_id(
383 GTK_TREE_SORTABLE(process_list
->list_store
),
384 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID
,
388 process_list
->process_hash
= g_hash_table_new_full(
389 process_list_hash_fct
, process_list_equ_fct
,
390 destroy_hash_key
, destroy_hash_data
394 gtk_tree_view_set_headers_visible(
395 GTK_TREE_VIEW(process_list
->process_list_widget
), TRUE
);
397 /* Create a column, associating the "text" attribute of the
398 * cell_renderer to the first column of the model */
399 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
400 renderer
= gtk_cell_renderer_text_new ();
401 process_list
->renderer
= renderer
;
403 /* Add a temporary row to the model to get the cell size when the first
404 * real process is added. */
407 path
= gtk_tree_path_new_first();
408 gtk_tree_model_get_iter (gtk_tree_view_get_model(GTK_TREE_VIEW(process_list
->process_list_widget
)), &iter
, path
);
409 gtk_list_store_append(process_list
->list_store
, &iter
);
410 gtk_tree_path_free(path
);
412 process_list
->cell_height
= 0; // not ready to get size yet.
414 column
= gtk_tree_view_column_new_with_attributes ( "Process",
419 gtk_tree_view_column_set_alignment (column
, 0.0);
420 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
421 gtk_tree_view_column_set_resizable(column
, TRUE
);
422 gtk_tree_view_append_column (
423 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
425 process_list
->button
= column
->button
;
427 column
= gtk_tree_view_column_new_with_attributes ( "Brand",
432 gtk_tree_view_column_set_alignment (column
, 0.0);
433 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
434 gtk_tree_view_column_set_resizable(column
, TRUE
);
435 gtk_tree_view_append_column (
436 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
438 column
= gtk_tree_view_column_new_with_attributes ( "PID",
443 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
444 gtk_tree_view_column_set_resizable(column
, TRUE
);
445 gtk_tree_view_append_column (
446 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
448 column
= gtk_tree_view_column_new_with_attributes ( "TGID",
453 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
454 gtk_tree_view_column_set_resizable(column
, TRUE
);
455 gtk_tree_view_append_column (
456 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
458 column
= gtk_tree_view_column_new_with_attributes ( "PPID",
463 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
464 gtk_tree_view_column_set_resizable(column
, TRUE
);
465 gtk_tree_view_append_column (
466 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
468 column
= gtk_tree_view_column_new_with_attributes ( "CPU",
473 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
474 gtk_tree_view_column_set_resizable(column
, TRUE
);
475 gtk_tree_view_append_column (
476 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
478 column
= gtk_tree_view_column_new_with_attributes ( "Birth sec",
483 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
484 gtk_tree_view_column_set_resizable(column
, TRUE
);
485 gtk_tree_view_append_column (
486 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
488 //gtk_tree_view_column_set_visible(column, 0);
490 column
= gtk_tree_view_column_new_with_attributes ( "Birth nsec",
495 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
496 gtk_tree_view_column_set_resizable(column
, TRUE
);
497 gtk_tree_view_append_column (
498 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
500 column
= gtk_tree_view_column_new_with_attributes ( "TRACE",
505 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_GROW_ONLY
);
506 gtk_tree_view_column_set_resizable(column
, TRUE
);
507 gtk_tree_view_append_column (
508 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
511 //gtk_tree_view_column_set_visible(column, 0);
513 g_object_set_data_full(
514 G_OBJECT(process_list
->process_list_widget
),
517 (GDestroyNotify
)processlist_destroy
);
519 process_list
->index_to_pixmap
= g_ptr_array_sized_new(ALLOCATE_PROCESSES
);
524 void processlist_destroy(ProcessList
*process_list
)
526 g_debug("processlist_destroy %p", process_list
);
527 g_hash_table_destroy(process_list
->process_hash
);
528 process_list
->process_hash
= NULL
;
529 g_ptr_array_free(process_list
->index_to_pixmap
, TRUE
);
531 g_free(process_list
);
532 g_debug("processlist_destroy end");
535 static gboolean
remove_hash_item(ProcessInfo
*process_info
,
536 HashedProcessData
*hashed_process_data
,
537 ProcessList
*process_list
)
541 iter
= hashed_process_data
->y_iter
;
543 gtk_list_store_remove (process_list
->list_store
, &iter
);
544 gdk_pixmap_unref(hashed_process_data
->pixmap
);
546 if(likely(process_list
->current_hash_data
!= NULL
)) {
547 if(likely(hashed_process_data
==
548 process_list
->current_hash_data
[process_info
->trace_num
][process_info
->cpu
]))
549 process_list
->current_hash_data
[process_info
->trace_num
][process_info
->cpu
] = NULL
;
551 return TRUE
; /* remove the element from the hash table */
554 void processlist_clear(ProcessList
*process_list
)
556 g_info("processlist_clear %p", process_list
);
558 g_hash_table_foreach_remove(process_list
->process_hash
,
559 (GHRFunc
)remove_hash_item
,
560 (gpointer
)process_list
);
561 process_list
->number_of_process
= 0;
562 update_index_to_pixmap(process_list
);
566 GtkWidget
*processlist_get_widget(ProcessList
*process_list
)
568 return process_list
->process_list_widget
;
572 void destroy_hash_key(gpointer key
)
577 void destroy_hash_data(gpointer data
)
583 void processlist_set_name(ProcessList
*process_list
,
585 HashedProcessData
*hashed_process_data
)
587 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
588 PROCESS_COLUMN
, g_quark_to_string(name
),
592 void processlist_set_brand(ProcessList
*process_list
,
594 HashedProcessData
*hashed_process_data
)
596 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
597 BRAND_COLUMN
, g_quark_to_string(brand
),
601 void processlist_set_tgid(ProcessList
*process_list
,
603 HashedProcessData
*hashed_process_data
)
605 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
610 void processlist_set_ppid(ProcessList
*process_list
,
612 HashedProcessData
*hashed_process_data
)
614 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
620 int processlist_add( ProcessList
*process_list
,
631 ProcessInfo
**pm_process_info
,
632 HashedProcessData
**pm_hashed_process_data
)
634 ProcessInfo
*Process_Info
= g_new(ProcessInfo
, 1);
635 HashedProcessData
*hashed_process_data
= g_new(HashedProcessData
, 1);
636 *pm_hashed_process_data
= hashed_process_data
;
637 *pm_process_info
= Process_Info
;
639 Process_Info
->pid
= pid
;
640 Process_Info
->tgid
= tgid
;
642 Process_Info
->cpu
= cpu
;
644 Process_Info
->cpu
= 0;
645 Process_Info
->ppid
= ppid
;
646 Process_Info
->birth
= *birth
;
647 Process_Info
->trace_num
= trace_num
;
649 /* When we create it from before state update, we are sure that the
650 * last event occured before the beginning of the global area.
652 * If it is created after state update, this value (0) will be
653 * overriden by the new state before anything is drawn.
655 hashed_process_data
->x
.over
= 0;
656 hashed_process_data
->x
.over_used
= FALSE
;
657 hashed_process_data
->x
.over_marked
= FALSE
;
658 hashed_process_data
->x
.middle
= 0;
659 hashed_process_data
->x
.middle_used
= FALSE
;
660 hashed_process_data
->x
.middle_marked
= FALSE
;
661 hashed_process_data
->x
.under
= 0;
662 hashed_process_data
->x
.under_used
= FALSE
;
663 hashed_process_data
->x
.under_marked
= FALSE
;
664 hashed_process_data
->next_good_time
= ltt_time_zero
;
666 if (process_list
->cell_height
== 0) {
671 path
= gtk_tree_path_new_first();
672 gtk_tree_model_get_iter (gtk_tree_view_get_model(GTK_TREE_VIEW(process_list
->process_list_widget
)), &iter
, path
);
673 gtk_tree_view_get_background_area(
674 GTK_TREE_VIEW(process_list
->process_list_widget
),
676 gtk_list_store_remove(process_list
->list_store
, &iter
);
677 gtk_tree_path_free (path
);
678 process_list
->cell_height
= rect
.height
;
681 /* Add a new row to the model */
682 gtk_list_store_append ( process_list
->list_store
,
683 &hashed_process_data
->y_iter
);
685 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
686 PROCESS_COLUMN
, g_quark_to_string(name
),
687 BRAND_COLUMN
, g_quark_to_string(brand
),
692 BIRTH_S_COLUMN
, birth
->tv_sec
,
693 BIRTH_NS_COLUMN
, birth
->tv_nsec
,
694 TRACE_COLUMN
, trace_num
,
696 //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget),
697 // GTK_TREE_MODEL(process_list->list_store));
698 //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget));
700 g_hash_table_insert(process_list
->process_hash
,
701 (gpointer
)Process_Info
,
702 (gpointer
)hashed_process_data
);
704 process_list
->number_of_process
++;
708 gtk_widget_queue_draw(process_list
->process_list_widget
);
709 path
= gtk_tree_path_new_first();
710 gtk_tree_view_get_background_area(GTK_TREE_VIEW(process_list
->process_list_widget
),
712 gtk_tree_path_free (path
);
713 process_list
->cell_height
= rect
.height
;
717 hashed_process_data
->height
= process_list
->cell_height
;
718 g_assert(hashed_process_data
->height
!= 0);
720 *height
= hashed_process_data
->height
* process_list
->number_of_process
;
722 hashed_process_data
->pixmap
=
723 gdk_pixmap_new(drawing
->drawing_area
->window
,
724 drawing
->alloc_width
,
725 hashed_process_data
->height
,
729 gdk_draw_rectangle (hashed_process_data
->pixmap
,
730 drawing
->drawing_area
->style
->black_gc
,
733 drawing
->alloc_width
,
734 hashed_process_data
->height
);
736 update_index_to_pixmap(process_list
);
742 int processlist_remove( ProcessList
*process_list
,
748 ProcessInfo process_info
;
749 HashedProcessData
*hashed_process_data
;
752 process_info
.pid
= pid
;
754 process_info
.cpu
= cpu
;
756 process_info
.cpu
= 0;
757 process_info
.birth
= *birth
;
758 process_info
.trace_num
= trace_num
;
761 hashed_process_data
=
762 (HashedProcessData
*)g_hash_table_lookup(
763 process_list
->process_hash
,
765 if(likely(hashed_process_data
!= NULL
))
767 iter
= hashed_process_data
->y_iter
;
769 gtk_list_store_remove (process_list
->list_store
, &iter
);
771 g_hash_table_remove(process_list
->process_hash
,
774 if(likely(process_list
->current_hash_data
!= NULL
)) {
775 if(likely(hashed_process_data
== process_list
->current_hash_data
[trace_num
][cpu
])) {
776 process_list
->current_hash_data
[trace_num
][cpu
] = NULL
;
780 gdk_pixmap_unref(hashed_process_data
->pixmap
);
782 update_index_to_pixmap(process_list
);
784 process_list
->number_of_process
--;
794 static inline guint
get_cpu_number_from_name(GQuark name
)
800 string
= g_quark_to_string(name
);
802 begin
= strrchr(string
, '/');
805 g_assert(begin
!= '\0');
807 cpu
= strtoul(begin
, NULL
, 10);