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
,
62 memset(&a
, 0, sizeof(GValue
));
63 memset(&b
, 0, sizeof(GValue
));
66 gtk_tree_model_get_value( model
,
71 gtk_tree_model_get_value( model
,
76 if(G_VALUE_TYPE(&a
) == G_TYPE_UINT
77 && G_VALUE_TYPE(&b
) == G_TYPE_UINT
)
81 if(g_value_get_uint(&a
) == 0 && g_value_get_uint(&b
) == 0) {
85 memset(&cpua
, 0, sizeof(GValue
));
86 memset(&cpub
, 0, sizeof(GValue
));
88 /* If 0, order by CPU */
89 gtk_tree_model_get_value( model
,
94 gtk_tree_model_get_value( model
,
99 if(G_VALUE_TYPE(&cpua
) == G_TYPE_UINT
100 && G_VALUE_TYPE(&cpub
) == G_TYPE_UINT
)
102 if(g_value_get_uint(&cpua
) > g_value_get_uint(&cpub
))
104 g_value_unset(&cpua
);
105 g_value_unset(&cpub
);
108 if(g_value_get_uint(&cpua
) < g_value_get_uint(&cpub
))
110 g_value_unset(&cpua
);
111 g_value_unset(&cpub
);
116 g_value_unset(&cpua
);
117 g_value_unset(&cpub
);
119 } else { /* if not 0, order by pid */
121 if(g_value_get_uint(&a
) > g_value_get_uint(&b
))
127 if(g_value_get_uint(&a
) < g_value_get_uint(&b
))
141 /* Order by birth second */
142 gtk_tree_model_get_value( model
,
147 gtk_tree_model_get_value( model
,
153 if(G_VALUE_TYPE(&a
) == G_TYPE_ULONG
154 && G_VALUE_TYPE(&b
) == G_TYPE_ULONG
)
156 if(g_value_get_ulong(&a
) > g_value_get_ulong(&b
))
162 if(g_value_get_ulong(&a
) < g_value_get_ulong(&b
))
174 /* Order by birth nanosecond */
175 gtk_tree_model_get_value( model
,
180 gtk_tree_model_get_value( model
,
186 if(G_VALUE_TYPE(&a
) == G_TYPE_ULONG
187 && G_VALUE_TYPE(&b
) == G_TYPE_ULONG
)
189 if(g_value_get_ulong(&a
) > g_value_get_ulong(&b
))
195 if(g_value_get_ulong(&a
) < g_value_get_ulong(&b
))
207 /* Order by trace_num */
208 gtk_tree_model_get_value( model
,
213 gtk_tree_model_get_value( model
,
218 if(G_VALUE_TYPE(&a
) == G_TYPE_ULONG
219 && G_VALUE_TYPE(&b
) == G_TYPE_ULONG
)
221 if(g_value_get_ulong(&a
) > g_value_get_ulong(&b
))
227 if(g_value_get_ulong(&a
) < g_value_get_ulong(&b
))
240 static guint
process_list_hash_fct(gconstpointer key
)
242 guint pid
= ((const ProcessInfo
*)key
)->pid
;
243 return ((pid
>>8 ^ pid
>>4 ^ pid
>>2 ^ pid
) ^ ((const ProcessInfo
*)key
)->cpu
);
246 /* If hash is good, should be different */
247 static gboolean
process_list_equ_fct(gconstpointer a
, gconstpointer b
)
249 const ProcessInfo
*pa
= (const ProcessInfo
*)a
;
250 const ProcessInfo
*pb
= (const ProcessInfo
*)b
;
254 if(likely(pa
->pid
!= pb
->pid
))
256 else if(likely((pa
->pid
== 0 && (pa
->cpu
!= pb
->cpu
))))
258 else if(unlikely(ltt_time_compare(pa
->birth
, pb
->birth
) != 0))
260 else if(unlikely(pa
->trace_num
!= pb
->trace_num
))
266 void destroy_hash_key(gpointer key
);
268 void destroy_hash_data(gpointer data
);
271 static void update_index_to_pixmap_each(ProcessInfo
*key
,
272 HashedProcessData
*value
,
273 ProcessList
*process_list
)
275 guint array_index
= processlist_get_index_from_data(process_list
, value
);
277 g_assert(array_index
< process_list
->index_to_pixmap
->len
);
280 (GdkPixmap
**)&g_ptr_array_index(process_list
->index_to_pixmap
, array_index
);
282 *pixmap
= value
->pixmap
;
286 static void update_index_to_pixmap(ProcessList
*process_list
)
288 g_ptr_array_set_size(process_list
->index_to_pixmap
,
289 g_hash_table_size(process_list
->process_hash
));
290 g_hash_table_foreach(process_list
->process_hash
,
291 (GHFunc
)update_index_to_pixmap_each
,
296 static void update_pixmap_size_each(ProcessInfo
*key
,
297 HashedProcessData
*value
,
300 GdkPixmap
*old_pixmap
= value
->pixmap
;
303 gdk_pixmap_new(old_pixmap
,
308 gdk_pixmap_unref(old_pixmap
);
312 void update_pixmap_size(ProcessList
*process_list
, guint width
)
314 g_hash_table_foreach(process_list
->process_hash
,
315 (GHFunc
)update_pixmap_size_each
,
320 typedef struct _CopyPixmap
{
324 gint xsrc
, ysrc
, xdest
, ydest
, width
, height
;
327 static void copy_pixmap_region_each(ProcessInfo
*key
,
328 HashedProcessData
*value
,
331 GdkPixmap
*src
= cp
->src
;
332 GdkPixmap
*dest
= cp
->dest
;
335 dest
= value
->pixmap
;
339 gdk_draw_drawable (dest
,
343 cp
->xdest
, cp
->ydest
,
344 cp
->width
, cp
->height
);
350 void copy_pixmap_region(ProcessList
*process_list
, GdkDrawable
*dest
,
351 GdkGC
*gc
, GdkDrawable
*src
,
352 gint xsrc
, gint ysrc
,
353 gint xdest
, gint ydest
, gint width
, gint height
)
355 CopyPixmap cp
= { dest
, gc
, src
, xsrc
, ysrc
, xdest
, ydest
, width
, height
};
357 g_hash_table_foreach(process_list
->process_hash
,
358 (GHFunc
)copy_pixmap_region_each
,
364 typedef struct _RectanglePixmap
{
366 gint x
, y
, width
, height
;
370 static void rectangle_pixmap_each(ProcessInfo
*key
,
371 HashedProcessData
*value
,
375 rp
->height
= value
->height
;
377 gdk_draw_rectangle (value
->pixmap
,
381 rp
->width
, rp
->height
);
387 void rectangle_pixmap(ProcessList
*process_list
, GdkGC
*gc
,
388 gboolean filled
, gint x
, gint y
, gint width
, gint height
)
390 RectanglePixmap rp
= { filled
, x
, y
, width
, height
, gc
};
392 g_hash_table_foreach(process_list
->process_hash
,
393 (GHFunc
)rectangle_pixmap_each
,
398 /* Renders each pixmaps into on big drawable */
399 void copy_pixmap_to_screen(ProcessList
*process_list
,
403 gint width
, gint height
)
406 get_cell_height(process_list
,
407 (GtkTreeView
*)process_list
->process_list_widget
);
408 cell_height
= 24; //FIXME
410 gint begin
= floor(y
/(double)cell_height
);
411 gint end
= MIN(ceil((y
+height
)/(double)cell_height
),
412 process_list
->index_to_pixmap
->len
);
414 g_warning("begin : %i, end : %i", begin
,end
);
415 for(i
=begin
; i
<end
; i
++) {
416 g_assert(i
<process_list
->index_to_pixmap
->len
);
417 /* Render the pixmap to the screen */
419 (GdkPixmap
*)g_ptr_array_index(process_list
->index_to_pixmap
, i
);
421 gdk_draw_drawable (dest
,
441 ProcessList
*processlist_construct(void)
443 GtkTreeViewColumn
*column
;
444 GtkCellRenderer
*renderer
;
446 ProcessList
* process_list
= g_new(ProcessList
,1);
448 process_list
->number_of_process
= 0;
450 process_list
->current_hash_data
= NULL
;
452 /* Create the Process list */
453 process_list
->list_store
= gtk_list_store_new ( N_COLUMNS
,
463 process_list
->process_list_widget
=
464 gtk_tree_view_new_with_model
465 (GTK_TREE_MODEL (process_list
->list_store
));
466 g_object_unref (G_OBJECT (process_list
->list_store
));
468 gtk_tree_sortable_set_sort_func(
469 GTK_TREE_SORTABLE(process_list
->list_store
),
475 gtk_tree_sortable_set_sort_column_id(
476 GTK_TREE_SORTABLE(process_list
->list_store
),
480 process_list
->process_hash
= g_hash_table_new_full(
481 process_list_hash_fct
, process_list_equ_fct
,
482 destroy_hash_key
, destroy_hash_data
486 gtk_tree_view_set_headers_visible(
487 GTK_TREE_VIEW(process_list
->process_list_widget
), TRUE
);
489 /* Create a column, associating the "text" attribute of the
490 * cell_renderer to the first column of the model */
491 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
492 renderer
= gtk_cell_renderer_text_new ();
493 column
= gtk_tree_view_column_new_with_attributes ( "Process",
498 gtk_tree_view_column_set_alignment (column
, 0.0);
499 gtk_tree_view_column_set_fixed_width (column
, 45);
500 gtk_tree_view_append_column (
501 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
503 process_list
->button
= column
->button
;
505 column
= gtk_tree_view_column_new_with_attributes ( "PID",
510 gtk_tree_view_append_column (
511 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
513 column
= gtk_tree_view_column_new_with_attributes ( "PPID",
518 gtk_tree_view_append_column (
519 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
521 column
= gtk_tree_view_column_new_with_attributes ( "CPU",
526 gtk_tree_view_append_column (
527 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
529 column
= gtk_tree_view_column_new_with_attributes ( "Birth sec",
534 gtk_tree_view_append_column (
535 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
537 //gtk_tree_view_column_set_visible(column, 0);
539 column
= gtk_tree_view_column_new_with_attributes ( "Birth nsec",
544 gtk_tree_view_append_column (
545 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
547 column
= gtk_tree_view_column_new_with_attributes ( "TRACE",
552 gtk_tree_view_append_column (
553 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
556 //gtk_tree_view_column_set_visible(column, 0);
558 g_object_set_data_full(
559 G_OBJECT(process_list
->process_list_widget
),
562 (GDestroyNotify
)processlist_destroy
);
564 process_list
->index_to_pixmap
= g_ptr_array_sized_new(ALLOCATE_PROCESSES
);
569 void processlist_destroy(ProcessList
*process_list
)
571 g_debug("processlist_destroy %p", process_list
);
572 g_hash_table_destroy(process_list
->process_hash
);
573 process_list
->process_hash
= NULL
;
574 g_ptr_array_free(process_list
->index_to_pixmap
, TRUE
);
576 g_free(process_list
);
577 g_debug("processlist_destroy end");
580 static gboolean
remove_hash_item(ProcessInfo
*process_info
,
581 HashedProcessData
*hashed_process_data
,
582 ProcessList
*process_list
)
586 iter
= hashed_process_data
->y_iter
;
588 gtk_list_store_remove (process_list
->list_store
, &iter
);
589 gdk_pixmap_unref(hashed_process_data
->pixmap
);
591 if(likely(process_list
->current_hash_data
!= NULL
)) {
592 if(likely(hashed_process_data
==
593 process_list
->current_hash_data
[process_info
->cpu
]))
594 process_list
->current_hash_data
[process_info
->cpu
] = NULL
;
596 return TRUE
; /* remove the element from the hash table */
599 void processlist_clear(ProcessList
*process_list
)
601 g_info("processlist_clear %p", process_list
);
603 g_hash_table_foreach_remove(process_list
->process_hash
,
604 (GHRFunc
)remove_hash_item
,
605 (gpointer
)process_list
);
606 process_list
->number_of_process
= 0;
607 update_index_to_pixmap(process_list
);
611 GtkWidget
*processlist_get_widget(ProcessList
*process_list
)
613 return process_list
->process_list_widget
;
617 void destroy_hash_key(gpointer key
)
622 void destroy_hash_data(gpointer data
)
627 int processlist_add( ProcessList
*process_list
,
636 ProcessInfo
**pm_process_info
,
637 HashedProcessData
**pm_hashed_process_data
)
639 ProcessInfo
*Process_Info
= g_new(ProcessInfo
, 1);
640 HashedProcessData
*hashed_process_data
= g_new(HashedProcessData
, 1);
641 *pm_hashed_process_data
= hashed_process_data
;
642 *pm_process_info
= Process_Info
;
644 Process_Info
->pid
= pid
;
646 Process_Info
->cpu
= cpu
;
648 Process_Info
->cpu
= 0;
649 Process_Info
->ppid
= ppid
;
650 Process_Info
->birth
= *birth
;
651 Process_Info
->trace_num
= trace_num
;
653 /* When we create it from before state update, we are sure that the
654 * last event occured before the beginning of the global area.
656 * If it is created after state update, this value (0) will be
657 * overriden by the new state before anything is drawn.
659 hashed_process_data
->x
.over
= 0;
660 hashed_process_data
->x
.over_used
= FALSE
;
661 hashed_process_data
->x
.over_marked
= FALSE
;
662 hashed_process_data
->x
.middle
= 0;
663 hashed_process_data
->x
.middle_used
= FALSE
;
664 hashed_process_data
->x
.middle_marked
= FALSE
;
665 hashed_process_data
->x
.under
= 0;
666 hashed_process_data
->x
.under_used
= FALSE
;
667 hashed_process_data
->x
.under_marked
= FALSE
;
668 hashed_process_data
->next_good_time
= ltt_time_zero
;
670 /* Add a new row to the model */
671 gtk_list_store_append ( process_list
->list_store
,
672 &hashed_process_data
->y_iter
);
674 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
675 PROCESS_COLUMN
, name
,
679 BIRTH_S_COLUMN
, birth
->tv_sec
,
680 BIRTH_NS_COLUMN
, birth
->tv_nsec
,
681 TRACE_COLUMN
, trace_num
,
684 g_hash_table_insert(process_list
->process_hash
,
685 (gpointer
)Process_Info
,
686 (gpointer
)hashed_process_data
);
688 process_list
->number_of_process
++;
690 hashed_process_data
->height
= get_cell_height(process_list
,
691 (GtkTreeView
*)process_list
->process_list_widget
);
692 hashed_process_data
->height
= 24; // FIXME
693 g_assert(hashed_process_data
->height
!= 0);
695 *height
= hashed_process_data
->height
* process_list
->number_of_process
;
697 hashed_process_data
->pixmap
=
698 gdk_pixmap_new(drawing
->drawing_area
->window
,
699 drawing
->alloc_width
,
700 hashed_process_data
->height
,
704 gdk_draw_rectangle (hashed_process_data
->pixmap
,
705 drawing
->drawing_area
->style
->black_gc
,
708 drawing
->alloc_width
,
709 hashed_process_data
->height
);
711 update_index_to_pixmap(process_list
);
717 int processlist_remove( ProcessList
*process_list
,
723 ProcessInfo process_info
;
724 HashedProcessData
*hashed_process_data
;
727 process_info
.pid
= pid
;
729 process_info
.cpu
= cpu
;
731 process_info
.cpu
= 0;
732 process_info
.birth
= *birth
;
733 process_info
.trace_num
= trace_num
;
736 hashed_process_data
=
737 (HashedProcessData
*)g_hash_table_lookup(
738 process_list
->process_hash
,
740 if(likely(hashed_process_data
!= NULL
))
742 iter
= hashed_process_data
->y_iter
;
744 gtk_list_store_remove (process_list
->list_store
, &iter
);
746 g_hash_table_remove(process_list
->process_hash
,
749 if(likely(process_list
->current_hash_data
!= NULL
)) {
750 if(likely(hashed_process_data
== process_list
->current_hash_data
[cpu
])) {
751 process_list
->current_hash_data
[cpu
] = NULL
;
755 gdk_pixmap_unref(hashed_process_data
->pixmap
);
757 update_index_to_pixmap(process_list
);
759 process_list
->number_of_process
--;
769 static inline guint
get_cpu_number_from_name(GQuark name
)
775 string
= g_quark_to_string(name
);
777 begin
= strrchr(string
, '/');
780 g_assert(begin
!= '\0');
782 cpu
= strtoul(begin
, NULL
, 10);