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,
29 #include "processlist.h"
33 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
34 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, 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_ppid
, a_cpu
;
51 gulong a_birth_s
, a_birth_ns
;
55 guint b_pid
, b_ppid
, b_cpu
;
56 gulong b_birth_s
, b_birth_ns
;
59 gtk_tree_model_get(model
,
70 gtk_tree_model_get(model
,
83 if(a_pid
== 0 && b_pid
== 0) {
84 /* If 0, order by CPU */
85 if(a_cpu
> b_cpu
) return 1;
86 if(a_cpu
< b_cpu
) return -1;
88 } else { /* if not 0, order by pid */
90 if(a_pid
> b_pid
) return 1;
91 if(a_pid
< b_pid
) return -1;
94 /* Order by birth second */
96 if(a_birth_s
> b_birth_s
) return 1;
97 if(a_birth_s
< b_birth_s
) return -1;
100 /* Order by birth nanosecond */
101 if(a_birth_ns
> b_birth_ns
) return 1;
102 if(a_birth_ns
< b_birth_ns
) return -1;
104 /* Order by trace_num */
105 if(a_trace
> b_trace
) return 1;
106 if(a_trace
< b_trace
) return -1;
112 static guint
process_list_hash_fct(gconstpointer key
)
114 guint pid
= ((const ProcessInfo
*)key
)->pid
;
115 return ((pid
>>8 ^ pid
>>4 ^ pid
>>2 ^ pid
) ^ ((const ProcessInfo
*)key
)->cpu
);
118 /* If hash is good, should be different */
119 static gboolean
process_list_equ_fct(gconstpointer a
, gconstpointer b
)
121 const ProcessInfo
*pa
= (const ProcessInfo
*)a
;
122 const ProcessInfo
*pb
= (const ProcessInfo
*)b
;
126 if(likely(pa
->pid
!= pb
->pid
))
128 if(likely((pa
->pid
== 0 && (pa
->cpu
!= pb
->cpu
))))
130 if(unlikely(ltt_time_compare(pa
->birth
, pb
->birth
) != 0))
132 if(unlikely(pa
->trace_num
!= pb
->trace_num
))
138 void destroy_hash_key(gpointer key
);
140 void destroy_hash_data(gpointer data
);
143 static void update_index_to_pixmap_each(ProcessInfo
*key
,
144 HashedProcessData
*value
,
145 ProcessList
*process_list
)
147 guint array_index
= processlist_get_index_from_data(process_list
, value
);
149 g_assert(array_index
< process_list
->index_to_pixmap
->len
);
152 (GdkPixmap
**)&g_ptr_array_index(process_list
->index_to_pixmap
, array_index
);
154 *pixmap
= value
->pixmap
;
158 void update_index_to_pixmap(ProcessList
*process_list
)
160 g_ptr_array_set_size(process_list
->index_to_pixmap
,
161 g_hash_table_size(process_list
->process_hash
));
162 g_hash_table_foreach(process_list
->process_hash
,
163 (GHFunc
)update_index_to_pixmap_each
,
168 static void update_pixmap_size_each(ProcessInfo
*key
,
169 HashedProcessData
*value
,
172 GdkPixmap
*old_pixmap
= value
->pixmap
;
175 gdk_pixmap_new(old_pixmap
,
180 gdk_pixmap_unref(old_pixmap
);
184 void update_pixmap_size(ProcessList
*process_list
, guint width
)
186 g_hash_table_foreach(process_list
->process_hash
,
187 (GHFunc
)update_pixmap_size_each
,
192 typedef struct _CopyPixmap
{
196 gint xsrc
, ysrc
, xdest
, ydest
, width
, height
;
199 static void copy_pixmap_region_each(ProcessInfo
*key
,
200 HashedProcessData
*value
,
203 GdkPixmap
*src
= cp
->src
;
204 GdkPixmap
*dest
= cp
->dest
;
207 dest
= value
->pixmap
;
211 gdk_draw_drawable (dest
,
215 cp
->xdest
, cp
->ydest
,
216 cp
->width
, cp
->height
);
222 void copy_pixmap_region(ProcessList
*process_list
, GdkDrawable
*dest
,
223 GdkGC
*gc
, GdkDrawable
*src
,
224 gint xsrc
, gint ysrc
,
225 gint xdest
, gint ydest
, gint width
, gint height
)
227 CopyPixmap cp
= { dest
, gc
, src
, xsrc
, ysrc
, xdest
, ydest
, width
, height
};
229 g_hash_table_foreach(process_list
->process_hash
,
230 (GHFunc
)copy_pixmap_region_each
,
236 typedef struct _RectanglePixmap
{
238 gint x
, y
, width
, height
;
242 static void rectangle_pixmap_each(ProcessInfo
*key
,
243 HashedProcessData
*value
,
247 rp
->height
= value
->height
;
249 gdk_draw_rectangle (value
->pixmap
,
253 rp
->width
, rp
->height
);
259 void rectangle_pixmap(ProcessList
*process_list
, GdkGC
*gc
,
260 gboolean filled
, gint x
, gint y
, gint width
, gint height
)
262 RectanglePixmap rp
= { filled
, x
, y
, width
, height
, gc
};
264 g_hash_table_foreach(process_list
->process_hash
,
265 (GHFunc
)rectangle_pixmap_each
,
270 /* Renders each pixmaps into on big drawable */
271 void copy_pixmap_to_screen(ProcessList
*process_list
,
275 gint width
, gint height
)
277 if(process_list
->index_to_pixmap
->len
== 0) return;
278 guint cell_height
= process_list
->cell_height
;
281 gint begin
= floor(y
/(double)cell_height
);
282 gint end
= MIN(ceil((y
+height
)/(double)cell_height
),
283 process_list
->index_to_pixmap
->len
);
286 for(i
=begin
; i
<end
; i
++) {
287 g_assert(i
<process_list
->index_to_pixmap
->len
);
288 /* Render the pixmap to the screen */
290 //(GdkPixmap*)g_ptr_array_index(process_list->index_to_pixmap, i);
291 GDK_PIXMAP(g_ptr_array_index(process_list
->index_to_pixmap
, i
));
293 gdk_draw_drawable (dest
,
313 ProcessList
*processlist_construct(void)
315 GtkTreeViewColumn
*column
;
316 GtkCellRenderer
*renderer
;
318 ProcessList
* process_list
= g_new(ProcessList
,1);
320 process_list
->number_of_process
= 0;
322 process_list
->current_hash_data
= NULL
;
324 /* Create the Process list */
325 process_list
->list_store
= gtk_list_store_new ( N_COLUMNS
,
335 process_list
->process_list_widget
=
336 gtk_tree_view_new_with_model
337 (GTK_TREE_MODEL (process_list
->list_store
));
339 g_object_unref (G_OBJECT (process_list
->list_store
));
341 gtk_tree_sortable_set_default_sort_func(
342 GTK_TREE_SORTABLE(process_list
->list_store
),
348 gtk_tree_sortable_set_sort_column_id(
349 GTK_TREE_SORTABLE(process_list
->list_store
),
350 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID
,
354 process_list
->process_hash
= g_hash_table_new_full(
355 process_list_hash_fct
, process_list_equ_fct
,
356 destroy_hash_key
, destroy_hash_data
360 gtk_tree_view_set_headers_visible(
361 GTK_TREE_VIEW(process_list
->process_list_widget
), TRUE
);
363 /* Create a column, associating the "text" attribute of the
364 * cell_renderer to the first column of the model */
365 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
366 renderer
= gtk_cell_renderer_text_new ();
367 process_list
->renderer
= renderer
;
369 gtk_cell_renderer_get_size(renderer
,
370 GTK_WIDGET(process_list
->process_list_widget
),
375 &process_list
->cell_height
);
378 g_object_get(G_OBJECT(renderer
), "ypad", &ypad
, NULL
);
380 process_list
->cell_height
+= ypad
;
382 column
= gtk_tree_view_column_new_with_attributes ( "Process",
387 gtk_tree_view_column_set_alignment (column
, 0.0);
388 gtk_tree_view_column_set_fixed_width (column
, 45);
389 gtk_tree_view_append_column (
390 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
392 process_list
->button
= column
->button
;
394 column
= gtk_tree_view_column_new_with_attributes ( "PID",
399 gtk_tree_view_append_column (
400 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
402 column
= gtk_tree_view_column_new_with_attributes ( "PPID",
407 gtk_tree_view_append_column (
408 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
410 column
= gtk_tree_view_column_new_with_attributes ( "CPU",
415 gtk_tree_view_append_column (
416 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
418 column
= gtk_tree_view_column_new_with_attributes ( "Birth sec",
423 gtk_tree_view_append_column (
424 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
426 //gtk_tree_view_column_set_visible(column, 0);
428 column
= gtk_tree_view_column_new_with_attributes ( "Birth nsec",
433 gtk_tree_view_append_column (
434 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
436 column
= gtk_tree_view_column_new_with_attributes ( "TRACE",
441 gtk_tree_view_append_column (
442 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
445 //gtk_tree_view_column_set_visible(column, 0);
447 g_object_set_data_full(
448 G_OBJECT(process_list
->process_list_widget
),
451 (GDestroyNotify
)processlist_destroy
);
453 process_list
->index_to_pixmap
= g_ptr_array_sized_new(ALLOCATE_PROCESSES
);
458 void processlist_destroy(ProcessList
*process_list
)
460 g_debug("processlist_destroy %p", process_list
);
461 g_hash_table_destroy(process_list
->process_hash
);
462 process_list
->process_hash
= NULL
;
463 g_ptr_array_free(process_list
->index_to_pixmap
, TRUE
);
465 g_free(process_list
);
466 g_debug("processlist_destroy end");
469 static gboolean
remove_hash_item(ProcessInfo
*process_info
,
470 HashedProcessData
*hashed_process_data
,
471 ProcessList
*process_list
)
475 iter
= hashed_process_data
->y_iter
;
477 gtk_list_store_remove (process_list
->list_store
, &iter
);
478 gdk_pixmap_unref(hashed_process_data
->pixmap
);
480 if(likely(process_list
->current_hash_data
!= NULL
)) {
481 if(likely(hashed_process_data
==
482 process_list
->current_hash_data
[process_info
->cpu
]))
483 process_list
->current_hash_data
[process_info
->cpu
] = NULL
;
485 return TRUE
; /* remove the element from the hash table */
488 void processlist_clear(ProcessList
*process_list
)
490 g_info("processlist_clear %p", process_list
);
492 g_hash_table_foreach_remove(process_list
->process_hash
,
493 (GHRFunc
)remove_hash_item
,
494 (gpointer
)process_list
);
495 process_list
->number_of_process
= 0;
496 update_index_to_pixmap(process_list
);
500 GtkWidget
*processlist_get_widget(ProcessList
*process_list
)
502 return process_list
->process_list_widget
;
506 void destroy_hash_key(gpointer key
)
511 void destroy_hash_data(gpointer data
)
517 void processlist_set_name(ProcessList
*process_list
,
519 HashedProcessData
*hashed_process_data
)
521 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
522 PROCESS_COLUMN
, g_quark_to_string(name
),
526 int processlist_add( ProcessList
*process_list
,
535 ProcessInfo
**pm_process_info
,
536 HashedProcessData
**pm_hashed_process_data
)
538 ProcessInfo
*Process_Info
= g_new(ProcessInfo
, 1);
539 HashedProcessData
*hashed_process_data
= g_new(HashedProcessData
, 1);
540 *pm_hashed_process_data
= hashed_process_data
;
541 *pm_process_info
= Process_Info
;
543 Process_Info
->pid
= pid
;
545 Process_Info
->cpu
= cpu
;
547 Process_Info
->cpu
= 0;
548 Process_Info
->ppid
= ppid
;
549 Process_Info
->birth
= *birth
;
550 Process_Info
->trace_num
= trace_num
;
552 /* When we create it from before state update, we are sure that the
553 * last event occured before the beginning of the global area.
555 * If it is created after state update, this value (0) will be
556 * overriden by the new state before anything is drawn.
558 hashed_process_data
->x
.over
= 0;
559 hashed_process_data
->x
.over_used
= FALSE
;
560 hashed_process_data
->x
.over_marked
= FALSE
;
561 hashed_process_data
->x
.middle
= 0;
562 hashed_process_data
->x
.middle_used
= FALSE
;
563 hashed_process_data
->x
.middle_marked
= FALSE
;
564 hashed_process_data
->x
.under
= 0;
565 hashed_process_data
->x
.under_used
= FALSE
;
566 hashed_process_data
->x
.under_marked
= FALSE
;
567 hashed_process_data
->next_good_time
= ltt_time_zero
;
569 /* Add a new row to the model */
570 gtk_list_store_append ( process_list
->list_store
,
571 &hashed_process_data
->y_iter
);
573 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
574 PROCESS_COLUMN
, g_quark_to_string(name
),
578 BIRTH_S_COLUMN
, birth
->tv_sec
,
579 BIRTH_NS_COLUMN
, birth
->tv_nsec
,
580 TRACE_COLUMN
, trace_num
,
582 //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget),
583 // GTK_TREE_MODEL(process_list->list_store));
584 //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget));
586 g_hash_table_insert(process_list
->process_hash
,
587 (gpointer
)Process_Info
,
588 (gpointer
)hashed_process_data
);
590 process_list
->number_of_process
++;
592 hashed_process_data
->height
= process_list
->cell_height
;
594 g_assert(hashed_process_data
->height
!= 0);
596 *height
= hashed_process_data
->height
* process_list
->number_of_process
;
598 hashed_process_data
->pixmap
=
599 gdk_pixmap_new(drawing
->drawing_area
->window
,
600 drawing
->alloc_width
,
601 hashed_process_data
->height
,
605 gdk_draw_rectangle (hashed_process_data
->pixmap
,
606 drawing
->drawing_area
->style
->black_gc
,
609 drawing
->alloc_width
,
610 hashed_process_data
->height
);
612 update_index_to_pixmap(process_list
);
618 int processlist_remove( ProcessList
*process_list
,
624 ProcessInfo process_info
;
625 HashedProcessData
*hashed_process_data
;
628 process_info
.pid
= pid
;
630 process_info
.cpu
= cpu
;
632 process_info
.cpu
= 0;
633 process_info
.birth
= *birth
;
634 process_info
.trace_num
= trace_num
;
637 hashed_process_data
=
638 (HashedProcessData
*)g_hash_table_lookup(
639 process_list
->process_hash
,
641 if(likely(hashed_process_data
!= NULL
))
643 iter
= hashed_process_data
->y_iter
;
645 gtk_list_store_remove (process_list
->list_store
, &iter
);
647 g_hash_table_remove(process_list
->process_hash
,
650 if(likely(process_list
->current_hash_data
!= NULL
)) {
651 if(likely(hashed_process_data
== process_list
->current_hash_data
[cpu
])) {
652 process_list
->current_hash_data
[cpu
] = NULL
;
656 gdk_pixmap_unref(hashed_process_data
->pixmap
);
658 update_index_to_pixmap(process_list
);
660 process_list
->number_of_process
--;
670 static inline guint
get_cpu_number_from_name(GQuark name
)
676 string
= g_quark_to_string(name
);
678 begin
= strrchr(string
, '/');
681 g_assert(begin
!= '\0');
683 cpu
= strtoul(begin
, NULL
, 10);