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)
35 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
37 /* Preallocated Size of the index_to_pixmap array */
38 #define ALLOCATE_PROCESSES 1000
40 /*****************************************************************************
41 * Methods to synchronize process list *
42 *****************************************************************************/
45 gint
process_sort_func ( GtkTreeModel
*model
,
51 guint a_pid
, a_ppid
, a_cpu
;
52 gulong a_birth_s
, a_birth_ns
;
56 guint b_pid
, b_ppid
, b_cpu
;
57 gulong b_birth_s
, b_birth_ns
;
60 gtk_tree_model_get(model
,
71 gtk_tree_model_get(model
,
84 if(a_pid
== 0 && b_pid
== 0) {
85 /* If 0, order by CPU */
86 if(a_cpu
> b_cpu
) return 1;
87 if(a_cpu
< b_cpu
) return -1;
89 } else { /* if not 0, order by pid */
91 if(a_pid
> b_pid
) return 1;
92 if(a_pid
< b_pid
) return -1;
95 /* Order by birth second */
97 if(a_birth_s
> b_birth_s
) return 1;
98 if(a_birth_s
< b_birth_s
) return -1;
101 /* Order by birth nanosecond */
102 if(a_birth_ns
> b_birth_ns
) return 1;
103 if(a_birth_ns
< b_birth_ns
) return -1;
105 /* Order by trace_num */
106 if(a_trace
> b_trace
) return 1;
107 if(a_trace
< b_trace
) return -1;
113 static guint
process_list_hash_fct(gconstpointer key
)
115 guint pid
= ((const ProcessInfo
*)key
)->pid
;
116 return ((pid
>>8 ^ pid
>>4 ^ pid
>>2 ^ pid
) ^ ((const ProcessInfo
*)key
)->cpu
);
119 /* If hash is good, should be different */
120 static gboolean
process_list_equ_fct(gconstpointer a
, gconstpointer b
)
122 const ProcessInfo
*pa
= (const ProcessInfo
*)a
;
123 const ProcessInfo
*pb
= (const ProcessInfo
*)b
;
127 if(likely(pa
->pid
!= pb
->pid
))
129 if(likely((pa
->pid
== 0 && (pa
->cpu
!= pb
->cpu
))))
131 if(unlikely(ltt_time_compare(pa
->birth
, pb
->birth
) != 0))
133 if(unlikely(pa
->trace_num
!= pb
->trace_num
))
139 void destroy_hash_key(gpointer key
);
141 void destroy_hash_data(gpointer data
);
144 static void update_index_to_pixmap_each(ProcessInfo
*key
,
145 HashedProcessData
*value
,
146 ProcessList
*process_list
)
148 guint array_index
= processlist_get_index_from_data(process_list
, value
);
150 g_assert(array_index
< process_list
->index_to_pixmap
->len
);
153 (GdkPixmap
**)&g_ptr_array_index(process_list
->index_to_pixmap
, array_index
);
155 *pixmap
= value
->pixmap
;
159 void update_index_to_pixmap(ProcessList
*process_list
)
161 g_ptr_array_set_size(process_list
->index_to_pixmap
,
162 g_hash_table_size(process_list
->process_hash
));
163 g_hash_table_foreach(process_list
->process_hash
,
164 (GHFunc
)update_index_to_pixmap_each
,
169 static void update_pixmap_size_each(ProcessInfo
*key
,
170 HashedProcessData
*value
,
173 GdkPixmap
*old_pixmap
= value
->pixmap
;
176 gdk_pixmap_new(old_pixmap
,
181 gdk_pixmap_unref(old_pixmap
);
185 void update_pixmap_size(ProcessList
*process_list
, guint width
)
187 g_hash_table_foreach(process_list
->process_hash
,
188 (GHFunc
)update_pixmap_size_each
,
193 typedef struct _CopyPixmap
{
197 gint xsrc
, ysrc
, xdest
, ydest
, width
, height
;
200 static void copy_pixmap_region_each(ProcessInfo
*key
,
201 HashedProcessData
*value
,
204 GdkPixmap
*src
= cp
->src
;
205 GdkPixmap
*dest
= cp
->dest
;
208 dest
= value
->pixmap
;
212 gdk_draw_drawable (dest
,
216 cp
->xdest
, cp
->ydest
,
217 cp
->width
, cp
->height
);
223 void copy_pixmap_region(ProcessList
*process_list
, GdkDrawable
*dest
,
224 GdkGC
*gc
, GdkDrawable
*src
,
225 gint xsrc
, gint ysrc
,
226 gint xdest
, gint ydest
, gint width
, gint height
)
228 CopyPixmap cp
= { dest
, gc
, src
, xsrc
, ysrc
, xdest
, ydest
, width
, height
};
230 g_hash_table_foreach(process_list
->process_hash
,
231 (GHFunc
)copy_pixmap_region_each
,
237 typedef struct _RectanglePixmap
{
239 gint x
, y
, width
, height
;
243 static void rectangle_pixmap_each(ProcessInfo
*key
,
244 HashedProcessData
*value
,
248 rp
->height
= value
->height
;
250 gdk_draw_rectangle (value
->pixmap
,
254 rp
->width
, rp
->height
);
260 void rectangle_pixmap(ProcessList
*process_list
, GdkGC
*gc
,
261 gboolean filled
, gint x
, gint y
, gint width
, gint height
)
263 RectanglePixmap rp
= { filled
, x
, y
, width
, height
, gc
};
265 g_hash_table_foreach(process_list
->process_hash
,
266 (GHFunc
)rectangle_pixmap_each
,
271 /* Renders each pixmaps into on big drawable */
272 void copy_pixmap_to_screen(ProcessList
*process_list
,
276 gint width
, gint height
)
278 if(process_list
->index_to_pixmap
->len
== 0) return;
279 guint cell_height
= process_list
->cell_height
;
282 gint begin
= floor(y
/(double)cell_height
);
283 gint end
= MIN(ceil((y
+height
)/(double)cell_height
),
284 process_list
->index_to_pixmap
->len
);
287 for(i
=begin
; i
<end
; i
++) {
288 g_assert(i
<process_list
->index_to_pixmap
->len
);
289 /* Render the pixmap to the screen */
291 //(GdkPixmap*)g_ptr_array_index(process_list->index_to_pixmap, i);
292 GDK_PIXMAP(g_ptr_array_index(process_list
->index_to_pixmap
, i
));
294 gdk_draw_drawable (dest
,
314 ProcessList
*processlist_construct(void)
316 GtkTreeViewColumn
*column
;
317 GtkCellRenderer
*renderer
;
319 ProcessList
* process_list
= g_new(ProcessList
,1);
321 process_list
->number_of_process
= 0;
323 process_list
->current_hash_data
= NULL
;
325 /* Create the Process list */
326 process_list
->list_store
= gtk_list_store_new ( N_COLUMNS
,
336 process_list
->process_list_widget
=
337 gtk_tree_view_new_with_model
338 (GTK_TREE_MODEL (process_list
->list_store
));
340 g_object_unref (G_OBJECT (process_list
->list_store
));
342 gtk_tree_sortable_set_default_sort_func(
343 GTK_TREE_SORTABLE(process_list
->list_store
),
349 gtk_tree_sortable_set_sort_column_id(
350 GTK_TREE_SORTABLE(process_list
->list_store
),
351 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID
,
355 process_list
->process_hash
= g_hash_table_new_full(
356 process_list_hash_fct
, process_list_equ_fct
,
357 destroy_hash_key
, destroy_hash_data
361 gtk_tree_view_set_headers_visible(
362 GTK_TREE_VIEW(process_list
->process_list_widget
), TRUE
);
364 /* Create a column, associating the "text" attribute of the
365 * cell_renderer to the first column of the model */
366 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
367 renderer
= gtk_cell_renderer_text_new ();
368 process_list
->renderer
= renderer
;
370 gint vertical_separator
;
371 gtk_widget_style_get (GTK_WIDGET (process_list
->process_list_widget
),
372 "vertical-separator", &vertical_separator
,
374 gtk_cell_renderer_get_size(renderer
,
375 GTK_WIDGET(process_list
->process_list_widget
),
380 &process_list
->cell_height
);
383 g_object_get(G_OBJECT(renderer
), "ypad", &ypad
, NULL
);
385 process_list
->cell_height
+= ypad
;
386 process_list
->cell_height
+= vertical_separator
;
389 column
= gtk_tree_view_column_new_with_attributes ( "Process",
394 gtk_tree_view_column_set_alignment (column
, 0.0);
395 gtk_tree_view_column_set_fixed_width (column
, 45);
396 gtk_tree_view_append_column (
397 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
399 process_list
->button
= column
->button
;
401 column
= gtk_tree_view_column_new_with_attributes ( "PID",
406 gtk_tree_view_append_column (
407 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
409 column
= gtk_tree_view_column_new_with_attributes ( "PPID",
414 gtk_tree_view_append_column (
415 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
417 column
= gtk_tree_view_column_new_with_attributes ( "CPU",
422 gtk_tree_view_append_column (
423 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
425 column
= gtk_tree_view_column_new_with_attributes ( "Birth sec",
430 gtk_tree_view_append_column (
431 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
433 //gtk_tree_view_column_set_visible(column, 0);
435 column
= gtk_tree_view_column_new_with_attributes ( "Birth nsec",
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 ( "TRACE",
448 gtk_tree_view_append_column (
449 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
452 //gtk_tree_view_column_set_visible(column, 0);
454 g_object_set_data_full(
455 G_OBJECT(process_list
->process_list_widget
),
458 (GDestroyNotify
)processlist_destroy
);
460 process_list
->index_to_pixmap
= g_ptr_array_sized_new(ALLOCATE_PROCESSES
);
465 void processlist_destroy(ProcessList
*process_list
)
467 g_debug("processlist_destroy %p", process_list
);
468 g_hash_table_destroy(process_list
->process_hash
);
469 process_list
->process_hash
= NULL
;
470 g_ptr_array_free(process_list
->index_to_pixmap
, TRUE
);
472 g_free(process_list
);
473 g_debug("processlist_destroy end");
476 static gboolean
remove_hash_item(ProcessInfo
*process_info
,
477 HashedProcessData
*hashed_process_data
,
478 ProcessList
*process_list
)
482 iter
= hashed_process_data
->y_iter
;
484 gtk_list_store_remove (process_list
->list_store
, &iter
);
485 gdk_pixmap_unref(hashed_process_data
->pixmap
);
487 if(likely(process_list
->current_hash_data
!= NULL
)) {
488 if(likely(hashed_process_data
==
489 process_list
->current_hash_data
[process_info
->cpu
]))
490 process_list
->current_hash_data
[process_info
->cpu
] = NULL
;
492 return TRUE
; /* remove the element from the hash table */
495 void processlist_clear(ProcessList
*process_list
)
497 g_info("processlist_clear %p", process_list
);
499 g_hash_table_foreach_remove(process_list
->process_hash
,
500 (GHRFunc
)remove_hash_item
,
501 (gpointer
)process_list
);
502 process_list
->number_of_process
= 0;
503 update_index_to_pixmap(process_list
);
507 GtkWidget
*processlist_get_widget(ProcessList
*process_list
)
509 return process_list
->process_list_widget
;
513 void destroy_hash_key(gpointer key
)
518 void destroy_hash_data(gpointer data
)
524 void processlist_set_name(ProcessList
*process_list
,
526 HashedProcessData
*hashed_process_data
)
528 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
529 PROCESS_COLUMN
, g_quark_to_string(name
),
533 void processlist_set_ppid(ProcessList
*process_list
,
535 HashedProcessData
*hashed_process_data
)
537 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
543 int processlist_add( ProcessList
*process_list
,
552 ProcessInfo
**pm_process_info
,
553 HashedProcessData
**pm_hashed_process_data
)
555 ProcessInfo
*Process_Info
= g_new(ProcessInfo
, 1);
556 HashedProcessData
*hashed_process_data
= g_new(HashedProcessData
, 1);
557 *pm_hashed_process_data
= hashed_process_data
;
558 *pm_process_info
= Process_Info
;
560 Process_Info
->pid
= pid
;
562 Process_Info
->cpu
= cpu
;
564 Process_Info
->cpu
= 0;
565 Process_Info
->ppid
= ppid
;
566 Process_Info
->birth
= *birth
;
567 Process_Info
->trace_num
= trace_num
;
569 /* When we create it from before state update, we are sure that the
570 * last event occured before the beginning of the global area.
572 * If it is created after state update, this value (0) will be
573 * overriden by the new state before anything is drawn.
575 hashed_process_data
->x
.over
= 0;
576 hashed_process_data
->x
.over_used
= FALSE
;
577 hashed_process_data
->x
.over_marked
= FALSE
;
578 hashed_process_data
->x
.middle
= 0;
579 hashed_process_data
->x
.middle_used
= FALSE
;
580 hashed_process_data
->x
.middle_marked
= FALSE
;
581 hashed_process_data
->x
.under
= 0;
582 hashed_process_data
->x
.under_used
= FALSE
;
583 hashed_process_data
->x
.under_marked
= FALSE
;
584 hashed_process_data
->next_good_time
= ltt_time_zero
;
586 /* Add a new row to the model */
587 gtk_list_store_append ( process_list
->list_store
,
588 &hashed_process_data
->y_iter
);
590 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
591 PROCESS_COLUMN
, g_quark_to_string(name
),
595 BIRTH_S_COLUMN
, birth
->tv_sec
,
596 BIRTH_NS_COLUMN
, birth
->tv_nsec
,
597 TRACE_COLUMN
, trace_num
,
599 //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget),
600 // GTK_TREE_MODEL(process_list->list_store));
601 //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget));
603 g_hash_table_insert(process_list
->process_hash
,
604 (gpointer
)Process_Info
,
605 (gpointer
)hashed_process_data
);
607 process_list
->number_of_process
++;
609 hashed_process_data
->height
= process_list
->cell_height
;
611 g_assert(hashed_process_data
->height
!= 0);
613 *height
= hashed_process_data
->height
* process_list
->number_of_process
;
615 hashed_process_data
->pixmap
=
616 gdk_pixmap_new(drawing
->drawing_area
->window
,
617 drawing
->alloc_width
,
618 hashed_process_data
->height
,
622 gdk_draw_rectangle (hashed_process_data
->pixmap
,
623 drawing
->drawing_area
->style
->black_gc
,
626 drawing
->alloc_width
,
627 hashed_process_data
->height
);
629 update_index_to_pixmap(process_list
);
635 int processlist_remove( ProcessList
*process_list
,
641 ProcessInfo process_info
;
642 HashedProcessData
*hashed_process_data
;
645 process_info
.pid
= pid
;
647 process_info
.cpu
= cpu
;
649 process_info
.cpu
= 0;
650 process_info
.birth
= *birth
;
651 process_info
.trace_num
= trace_num
;
654 hashed_process_data
=
655 (HashedProcessData
*)g_hash_table_lookup(
656 process_list
->process_hash
,
658 if(likely(hashed_process_data
!= NULL
))
660 iter
= hashed_process_data
->y_iter
;
662 gtk_list_store_remove (process_list
->list_store
, &iter
);
664 g_hash_table_remove(process_list
->process_hash
,
667 if(likely(process_list
->current_hash_data
!= NULL
)) {
668 if(likely(hashed_process_data
== process_list
->current_hash_data
[cpu
])) {
669 process_list
->current_hash_data
[cpu
] = NULL
;
673 gdk_pixmap_unref(hashed_process_data
->pixmap
);
675 update_index_to_pixmap(process_list
);
677 process_list
->number_of_process
--;
687 static inline guint
get_cpu_number_from_name(GQuark name
)
693 string
= g_quark_to_string(name
);
695 begin
= strrchr(string
, '/');
698 g_assert(begin
!= '\0');
700 cpu
= strtoul(begin
, NULL
, 10);