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 *****************************************************************************/
40 gint
process_sort_func ( GtkTreeModel
*model
,
46 guint a_pid
, a_ppid
, a_cpu
;
47 gulong a_birth_s
, a_birth_ns
;
51 guint b_pid
, b_ppid
, b_cpu
;
52 gulong b_birth_s
, b_birth_ns
;
55 gtk_tree_model_get(model
,
66 gtk_tree_model_get(model
,
79 if(a_pid
== 0 && b_pid
== 0) {
80 /* If 0, order by CPU */
81 if(a_cpu
> b_cpu
) return 1;
82 if(a_cpu
< b_cpu
) return -1;
84 } else { /* if not 0, order by pid */
86 if(a_pid
> b_pid
) return 1;
87 if(a_pid
< b_pid
) return -1;
90 /* Order by birth second */
92 if(a_birth_s
> b_birth_s
) return 1;
93 if(a_birth_s
< b_birth_s
) return -1;
96 /* Order by birth nanosecond */
97 if(a_birth_ns
> b_birth_ns
) return 1;
98 if(a_birth_ns
< b_birth_ns
) return -1;
100 /* Order by trace_num */
101 if(a_trace
> b_trace
) return 1;
102 if(a_trace
< b_trace
) return -1;
108 static guint
process_list_hash_fct(gconstpointer key
)
110 guint pid
= ((const ProcessInfo
*)key
)->pid
;
111 return ((pid
>>8 ^ pid
>>4 ^ pid
>>2 ^ pid
) ^ ((const ProcessInfo
*)key
)->cpu
);
114 /* If hash is good, should be different */
115 static gboolean
process_list_equ_fct(gconstpointer a
, gconstpointer b
)
117 const ProcessInfo
*pa
= (const ProcessInfo
*)a
;
118 const ProcessInfo
*pb
= (const ProcessInfo
*)b
;
122 if(likely(pa
->pid
!= pb
->pid
))
124 else if(likely((pa
->pid
== 0 && (pa
->cpu
!= pb
->cpu
))))
126 else if(unlikely(ltt_time_compare(pa
->birth
, pb
->birth
) != 0))
128 else if(unlikely(pa
->trace_num
!= pb
->trace_num
))
134 void destroy_hash_key(gpointer key
);
136 void destroy_hash_data(gpointer data
);
139 static void update_index_to_pixmap_each(ProcessInfo
*key
,
140 HashedProcessData
*value
,
141 ProcessList
*process_list
)
143 guint array_index
= processlist_get_index_from_data(process_list
, value
);
145 g_assert(array_index
< process_list
->index_to_pixmap
->len
);
148 (GdkPixmap
**)&g_ptr_array_index(process_list
->index_to_pixmap
, array_index
);
150 *pixmap
= value
->pixmap
;
154 void update_index_to_pixmap(ProcessList
*process_list
)
156 g_ptr_array_set_size(process_list
->index_to_pixmap
,
157 g_hash_table_size(process_list
->process_hash
));
158 g_hash_table_foreach(process_list
->process_hash
,
159 (GHFunc
)update_index_to_pixmap_each
,
164 static void update_pixmap_size_each(ProcessInfo
*key
,
165 HashedProcessData
*value
,
168 GdkPixmap
*old_pixmap
= value
->pixmap
;
171 gdk_pixmap_new(old_pixmap
,
176 gdk_pixmap_unref(old_pixmap
);
180 void update_pixmap_size(ProcessList
*process_list
, guint width
)
182 g_hash_table_foreach(process_list
->process_hash
,
183 (GHFunc
)update_pixmap_size_each
,
188 typedef struct _CopyPixmap
{
192 gint xsrc
, ysrc
, xdest
, ydest
, width
, height
;
195 static void copy_pixmap_region_each(ProcessInfo
*key
,
196 HashedProcessData
*value
,
199 GdkPixmap
*src
= cp
->src
;
200 GdkPixmap
*dest
= cp
->dest
;
203 dest
= value
->pixmap
;
207 gdk_draw_drawable (dest
,
211 cp
->xdest
, cp
->ydest
,
212 cp
->width
, cp
->height
);
218 void copy_pixmap_region(ProcessList
*process_list
, GdkDrawable
*dest
,
219 GdkGC
*gc
, GdkDrawable
*src
,
220 gint xsrc
, gint ysrc
,
221 gint xdest
, gint ydest
, gint width
, gint height
)
223 CopyPixmap cp
= { dest
, gc
, src
, xsrc
, ysrc
, xdest
, ydest
, width
, height
};
225 g_hash_table_foreach(process_list
->process_hash
,
226 (GHFunc
)copy_pixmap_region_each
,
232 typedef struct _RectanglePixmap
{
234 gint x
, y
, width
, height
;
238 static void rectangle_pixmap_each(ProcessInfo
*key
,
239 HashedProcessData
*value
,
243 rp
->height
= value
->height
;
245 gdk_draw_rectangle (value
->pixmap
,
249 rp
->width
, rp
->height
);
255 void rectangle_pixmap(ProcessList
*process_list
, GdkGC
*gc
,
256 gboolean filled
, gint x
, gint y
, gint width
, gint height
)
258 RectanglePixmap rp
= { filled
, x
, y
, width
, height
, gc
};
260 g_hash_table_foreach(process_list
->process_hash
,
261 (GHFunc
)rectangle_pixmap_each
,
266 /* Renders each pixmaps into on big drawable */
267 void copy_pixmap_to_screen(ProcessList
*process_list
,
271 gint width
, gint height
)
273 if(process_list
->index_to_pixmap
->len
== 0) return;
274 guint cell_height
= process_list
->cell_height
;
277 gint begin
= floor(y
/(double)cell_height
);
278 gint end
= MIN(ceil((y
+height
)/(double)cell_height
),
279 process_list
->index_to_pixmap
->len
);
282 for(i
=begin
; i
<end
; i
++) {
283 g_assert(i
<process_list
->index_to_pixmap
->len
);
284 /* Render the pixmap to the screen */
286 (GdkPixmap
*)g_ptr_array_index(process_list
->index_to_pixmap
, i
);
288 gdk_draw_drawable (dest
,
308 ProcessList
*processlist_construct(void)
310 GtkTreeViewColumn
*column
;
311 GtkCellRenderer
*renderer
;
313 ProcessList
* process_list
= g_new(ProcessList
,1);
315 process_list
->number_of_process
= 0;
317 process_list
->current_hash_data
= NULL
;
319 /* Create the Process list */
320 process_list
->list_store
= gtk_list_store_new ( N_COLUMNS
,
330 process_list
->process_list_widget
=
331 gtk_tree_view_new_with_model
332 (GTK_TREE_MODEL (process_list
->list_store
));
334 g_object_unref (G_OBJECT (process_list
->list_store
));
336 gtk_tree_sortable_set_default_sort_func(
337 GTK_TREE_SORTABLE(process_list
->list_store
),
343 gtk_tree_sortable_set_sort_column_id(
344 GTK_TREE_SORTABLE(process_list
->list_store
),
345 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID
,
349 process_list
->process_hash
= g_hash_table_new_full(
350 process_list_hash_fct
, process_list_equ_fct
,
351 destroy_hash_key
, destroy_hash_data
355 gtk_tree_view_set_headers_visible(
356 GTK_TREE_VIEW(process_list
->process_list_widget
), TRUE
);
358 /* Create a column, associating the "text" attribute of the
359 * cell_renderer to the first column of the model */
360 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
361 renderer
= gtk_cell_renderer_text_new ();
362 process_list
->renderer
= renderer
;
364 gtk_cell_renderer_get_size(renderer
,
365 GTK_WIDGET(process_list
->process_list_widget
),
370 &process_list
->cell_height
);
373 g_object_get(G_OBJECT(renderer
), "ypad", &ypad
, NULL
);
375 process_list
->cell_height
+= ypad
;
377 column
= gtk_tree_view_column_new_with_attributes ( "Process",
382 gtk_tree_view_column_set_alignment (column
, 0.0);
383 gtk_tree_view_column_set_fixed_width (column
, 45);
384 gtk_tree_view_append_column (
385 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
387 process_list
->button
= column
->button
;
389 column
= gtk_tree_view_column_new_with_attributes ( "PID",
394 gtk_tree_view_append_column (
395 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
397 column
= gtk_tree_view_column_new_with_attributes ( "PPID",
402 gtk_tree_view_append_column (
403 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
405 column
= gtk_tree_view_column_new_with_attributes ( "CPU",
410 gtk_tree_view_append_column (
411 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
413 column
= gtk_tree_view_column_new_with_attributes ( "Birth sec",
418 gtk_tree_view_append_column (
419 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
421 //gtk_tree_view_column_set_visible(column, 0);
423 column
= gtk_tree_view_column_new_with_attributes ( "Birth nsec",
428 gtk_tree_view_append_column (
429 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
431 column
= gtk_tree_view_column_new_with_attributes ( "TRACE",
436 gtk_tree_view_append_column (
437 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
440 //gtk_tree_view_column_set_visible(column, 0);
442 g_object_set_data_full(
443 G_OBJECT(process_list
->process_list_widget
),
446 (GDestroyNotify
)processlist_destroy
);
448 process_list
->index_to_pixmap
= g_ptr_array_sized_new(ALLOCATE_PROCESSES
);
453 void processlist_destroy(ProcessList
*process_list
)
455 g_debug("processlist_destroy %p", process_list
);
456 g_hash_table_destroy(process_list
->process_hash
);
457 process_list
->process_hash
= NULL
;
458 g_ptr_array_free(process_list
->index_to_pixmap
, TRUE
);
460 g_free(process_list
);
461 g_debug("processlist_destroy end");
464 static gboolean
remove_hash_item(ProcessInfo
*process_info
,
465 HashedProcessData
*hashed_process_data
,
466 ProcessList
*process_list
)
470 iter
= hashed_process_data
->y_iter
;
472 gtk_list_store_remove (process_list
->list_store
, &iter
);
473 gdk_pixmap_unref(hashed_process_data
->pixmap
);
475 if(likely(process_list
->current_hash_data
!= NULL
)) {
476 if(likely(hashed_process_data
==
477 process_list
->current_hash_data
[process_info
->cpu
]))
478 process_list
->current_hash_data
[process_info
->cpu
] = NULL
;
480 return TRUE
; /* remove the element from the hash table */
483 void processlist_clear(ProcessList
*process_list
)
485 g_info("processlist_clear %p", process_list
);
487 g_hash_table_foreach_remove(process_list
->process_hash
,
488 (GHRFunc
)remove_hash_item
,
489 (gpointer
)process_list
);
490 process_list
->number_of_process
= 0;
491 update_index_to_pixmap(process_list
);
495 GtkWidget
*processlist_get_widget(ProcessList
*process_list
)
497 return process_list
->process_list_widget
;
501 void destroy_hash_key(gpointer key
)
506 void destroy_hash_data(gpointer data
)
511 int processlist_add( ProcessList
*process_list
,
520 ProcessInfo
**pm_process_info
,
521 HashedProcessData
**pm_hashed_process_data
)
523 ProcessInfo
*Process_Info
= g_new(ProcessInfo
, 1);
524 HashedProcessData
*hashed_process_data
= g_new(HashedProcessData
, 1);
525 *pm_hashed_process_data
= hashed_process_data
;
526 *pm_process_info
= Process_Info
;
528 Process_Info
->pid
= pid
;
530 Process_Info
->cpu
= cpu
;
532 Process_Info
->cpu
= 0;
533 Process_Info
->ppid
= ppid
;
534 Process_Info
->birth
= *birth
;
535 Process_Info
->trace_num
= trace_num
;
537 /* When we create it from before state update, we are sure that the
538 * last event occured before the beginning of the global area.
540 * If it is created after state update, this value (0) will be
541 * overriden by the new state before anything is drawn.
543 hashed_process_data
->x
.over
= 0;
544 hashed_process_data
->x
.over_used
= FALSE
;
545 hashed_process_data
->x
.over_marked
= FALSE
;
546 hashed_process_data
->x
.middle
= 0;
547 hashed_process_data
->x
.middle_used
= FALSE
;
548 hashed_process_data
->x
.middle_marked
= FALSE
;
549 hashed_process_data
->x
.under
= 0;
550 hashed_process_data
->x
.under_used
= FALSE
;
551 hashed_process_data
->x
.under_marked
= FALSE
;
552 hashed_process_data
->next_good_time
= ltt_time_zero
;
554 /* Add a new row to the model */
555 gtk_list_store_append ( process_list
->list_store
,
556 &hashed_process_data
->y_iter
);
558 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
559 PROCESS_COLUMN
, name
,
563 BIRTH_S_COLUMN
, birth
->tv_sec
,
564 BIRTH_NS_COLUMN
, birth
->tv_nsec
,
565 TRACE_COLUMN
, trace_num
,
567 //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget),
568 // GTK_TREE_MODEL(process_list->list_store));
569 //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget));
571 g_hash_table_insert(process_list
->process_hash
,
572 (gpointer
)Process_Info
,
573 (gpointer
)hashed_process_data
);
575 process_list
->number_of_process
++;
577 hashed_process_data
->height
= process_list
->cell_height
;
579 g_assert(hashed_process_data
->height
!= 0);
581 *height
= hashed_process_data
->height
* process_list
->number_of_process
;
583 hashed_process_data
->pixmap
=
584 gdk_pixmap_new(drawing
->drawing_area
->window
,
585 drawing
->alloc_width
,
586 hashed_process_data
->height
,
590 gdk_draw_rectangle (hashed_process_data
->pixmap
,
591 drawing
->drawing_area
->style
->black_gc
,
594 drawing
->alloc_width
,
595 hashed_process_data
->height
);
597 update_index_to_pixmap(process_list
);
603 int processlist_remove( ProcessList
*process_list
,
609 ProcessInfo process_info
;
610 HashedProcessData
*hashed_process_data
;
613 process_info
.pid
= pid
;
615 process_info
.cpu
= cpu
;
617 process_info
.cpu
= 0;
618 process_info
.birth
= *birth
;
619 process_info
.trace_num
= trace_num
;
622 hashed_process_data
=
623 (HashedProcessData
*)g_hash_table_lookup(
624 process_list
->process_hash
,
626 if(likely(hashed_process_data
!= NULL
))
628 iter
= hashed_process_data
->y_iter
;
630 gtk_list_store_remove (process_list
->list_store
, &iter
);
632 g_hash_table_remove(process_list
->process_hash
,
635 if(likely(process_list
->current_hash_data
!= NULL
)) {
636 if(likely(hashed_process_data
== process_list
->current_hash_data
[cpu
])) {
637 process_list
->current_hash_data
[cpu
] = NULL
;
641 gdk_pixmap_unref(hashed_process_data
->pixmap
);
643 update_index_to_pixmap(process_list
);
645 process_list
->number_of_process
--;
655 static inline guint
get_cpu_number_from_name(GQuark name
)
661 string
= g_quark_to_string(name
);
663 begin
= strrchr(string
, '/');
666 g_assert(begin
!= '\0');
668 cpu
= strtoul(begin
, NULL
, 10);