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
,
52 guint a_pid
, a_ppid
, a_cpu
;
53 gulong a_birth_s
, a_birth_ns
;
58 guint b_pid
, b_ppid
, b_cpu
;
59 gulong b_birth_s
, b_birth_ns
;
62 gtk_tree_model_get(model
,
74 gtk_tree_model_get(model
,
88 if(a_pid
== 0 && b_pid
== 0) {
89 /* If 0, order by CPU */
90 if(a_cpu
> b_cpu
) return 1;
91 if(a_cpu
< b_cpu
) return -1;
93 } else { /* if not 0, order by pid */
95 if(a_pid
> b_pid
) return 1;
96 if(a_pid
< b_pid
) return -1;
99 /* Order by birth second */
101 if(a_birth_s
> b_birth_s
) return 1;
102 if(a_birth_s
< b_birth_s
) return -1;
105 /* Order by birth nanosecond */
106 if(a_birth_ns
> b_birth_ns
) return 1;
107 if(a_birth_ns
< b_birth_ns
) return -1;
109 /* Order by trace_num */
110 if(a_trace
> b_trace
) return 1;
111 if(a_trace
< b_trace
) return -1;
117 static guint
process_list_hash_fct(gconstpointer key
)
119 guint pid
= ((const ProcessInfo
*)key
)->pid
;
120 return ((pid
>>8 ^ pid
>>4 ^ pid
>>2 ^ pid
) ^ ((const ProcessInfo
*)key
)->cpu
);
123 /* If hash is good, should be different */
124 static gboolean
process_list_equ_fct(gconstpointer a
, gconstpointer b
)
126 const ProcessInfo
*pa
= (const ProcessInfo
*)a
;
127 const ProcessInfo
*pb
= (const ProcessInfo
*)b
;
131 if(likely(pa
->pid
!= pb
->pid
))
133 if(likely((pa
->pid
== 0 && (pa
->cpu
!= pb
->cpu
))))
135 if(unlikely(ltt_time_compare(pa
->birth
, pb
->birth
) != 0))
137 if(unlikely(pa
->trace_num
!= pb
->trace_num
))
143 void destroy_hash_key(gpointer key
);
145 void destroy_hash_data(gpointer data
);
148 gboolean
scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer data
)
150 ControlFlowData
*control_flow_data
=
151 (ControlFlowData
*)g_object_get_data(
153 "control_flow_data");
154 Drawing_t
*drawing
= control_flow_data
->drawing
;
155 unsigned int cell_height
=
156 get_cell_height(GTK_TREE_VIEW(control_flow_data
->process_list
->process_list_widget
));
158 switch(event
->direction
) {
160 gtk_adjustment_set_value(control_flow_data
->v_adjust
,
161 gtk_adjustment_get_value(control_flow_data
->v_adjust
) - cell_height
);
163 case GDK_SCROLL_DOWN
:
164 gtk_adjustment_set_value(control_flow_data
->v_adjust
,
165 gtk_adjustment_get_value(control_flow_data
->v_adjust
) + cell_height
);
168 g_error("should only scroll up and down.");
174 static void update_index_to_pixmap_each(ProcessInfo
*key
,
175 HashedProcessData
*value
,
176 ProcessList
*process_list
)
178 guint array_index
= processlist_get_index_from_data(process_list
, value
);
180 g_assert(array_index
< process_list
->index_to_pixmap
->len
);
183 (GdkPixmap
**)&g_ptr_array_index(process_list
->index_to_pixmap
, array_index
);
185 *pixmap
= value
->pixmap
;
189 void update_index_to_pixmap(ProcessList
*process_list
)
191 g_ptr_array_set_size(process_list
->index_to_pixmap
,
192 g_hash_table_size(process_list
->process_hash
));
193 g_hash_table_foreach(process_list
->process_hash
,
194 (GHFunc
)update_index_to_pixmap_each
,
199 static void update_pixmap_size_each(ProcessInfo
*key
,
200 HashedProcessData
*value
,
203 GdkPixmap
*old_pixmap
= value
->pixmap
;
206 gdk_pixmap_new(old_pixmap
,
211 gdk_pixmap_unref(old_pixmap
);
215 void update_pixmap_size(ProcessList
*process_list
, guint width
)
217 g_hash_table_foreach(process_list
->process_hash
,
218 (GHFunc
)update_pixmap_size_each
,
223 typedef struct _CopyPixmap
{
227 gint xsrc
, ysrc
, xdest
, ydest
, width
, height
;
230 static void copy_pixmap_region_each(ProcessInfo
*key
,
231 HashedProcessData
*value
,
234 GdkPixmap
*src
= cp
->src
;
235 GdkPixmap
*dest
= cp
->dest
;
238 dest
= value
->pixmap
;
242 gdk_draw_drawable (dest
,
246 cp
->xdest
, cp
->ydest
,
247 cp
->width
, cp
->height
);
253 void copy_pixmap_region(ProcessList
*process_list
, GdkDrawable
*dest
,
254 GdkGC
*gc
, GdkDrawable
*src
,
255 gint xsrc
, gint ysrc
,
256 gint xdest
, gint ydest
, gint width
, gint height
)
258 CopyPixmap cp
= { dest
, gc
, src
, xsrc
, ysrc
, xdest
, ydest
, width
, height
};
260 g_hash_table_foreach(process_list
->process_hash
,
261 (GHFunc
)copy_pixmap_region_each
,
267 typedef struct _RectanglePixmap
{
269 gint x
, y
, width
, height
;
273 static void rectangle_pixmap_each(ProcessInfo
*key
,
274 HashedProcessData
*value
,
278 rp
->height
= value
->height
;
280 gdk_draw_rectangle (value
->pixmap
,
284 rp
->width
, rp
->height
);
290 void rectangle_pixmap(ProcessList
*process_list
, GdkGC
*gc
,
291 gboolean filled
, gint x
, gint y
, gint width
, gint height
)
293 RectanglePixmap rp
= { filled
, x
, y
, width
, height
, gc
};
295 g_hash_table_foreach(process_list
->process_hash
,
296 (GHFunc
)rectangle_pixmap_each
,
301 /* Renders each pixmaps into on big drawable */
302 void copy_pixmap_to_screen(ProcessList
*process_list
,
306 gint width
, gint height
)
308 if(process_list
->index_to_pixmap
->len
== 0) return;
309 guint cell_height
= process_list
->cell_height
;
312 gint begin
= floor(y
/(double)cell_height
);
313 gint end
= MIN(ceil((y
+height
)/(double)cell_height
),
314 process_list
->index_to_pixmap
->len
);
317 for(i
=begin
; i
<end
; i
++) {
318 g_assert(i
<process_list
->index_to_pixmap
->len
);
319 /* Render the pixmap to the screen */
321 //(GdkPixmap*)g_ptr_array_index(process_list->index_to_pixmap, i);
322 GDK_PIXMAP(g_ptr_array_index(process_list
->index_to_pixmap
, i
));
324 gdk_draw_drawable (dest
,
344 ProcessList
*processlist_construct(void)
346 GtkTreeViewColumn
*column
;
347 GtkCellRenderer
*renderer
;
349 ProcessList
* process_list
= g_new(ProcessList
,1);
351 process_list
->number_of_process
= 0;
353 process_list
->current_hash_data
= NULL
;
355 /* Create the Process list */
356 process_list
->list_store
= gtk_list_store_new ( N_COLUMNS
,
367 process_list
->process_list_widget
=
368 gtk_tree_view_new_with_model
369 (GTK_TREE_MODEL (process_list
->list_store
));
371 g_object_unref (G_OBJECT (process_list
->list_store
));
373 gtk_tree_sortable_set_default_sort_func(
374 GTK_TREE_SORTABLE(process_list
->list_store
),
380 gtk_tree_sortable_set_sort_column_id(
381 GTK_TREE_SORTABLE(process_list
->list_store
),
382 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID
,
386 process_list
->process_hash
= g_hash_table_new_full(
387 process_list_hash_fct
, process_list_equ_fct
,
388 destroy_hash_key
, destroy_hash_data
392 gtk_tree_view_set_headers_visible(
393 GTK_TREE_VIEW(process_list
->process_list_widget
), TRUE
);
395 /* Create a column, associating the "text" attribute of the
396 * cell_renderer to the first column of the model */
397 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
398 renderer
= gtk_cell_renderer_text_new ();
399 process_list
->renderer
= renderer
;
401 gint vertical_separator
;
402 gtk_widget_style_get (GTK_WIDGET (process_list
->process_list_widget
),
403 "vertical-separator", &vertical_separator
,
405 gtk_cell_renderer_get_size(renderer
,
406 GTK_WIDGET(process_list
->process_list_widget
),
411 &process_list
->cell_height
);
413 #if GTK_CHECK_VERSION(2,4,15)
415 g_object_get(G_OBJECT(renderer
), "ypad", &ypad
, NULL
);
417 process_list
->cell_height
+= ypad
;
419 process_list
->cell_height
+= vertical_separator
;
422 column
= gtk_tree_view_column_new_with_attributes ( "Process",
427 gtk_tree_view_column_set_alignment (column
, 0.0);
428 gtk_tree_view_column_set_fixed_width (column
, 45);
429 gtk_tree_view_append_column (
430 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
432 process_list
->button
= column
->button
;
434 column
= gtk_tree_view_column_new_with_attributes ( "Brand",
439 gtk_tree_view_column_set_alignment (column
, 0.0);
440 gtk_tree_view_column_set_fixed_width (column
, 45);
441 gtk_tree_view_append_column (
442 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
444 column
= gtk_tree_view_column_new_with_attributes ( "PID",
449 gtk_tree_view_append_column (
450 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
452 column
= gtk_tree_view_column_new_with_attributes ( "PPID",
457 gtk_tree_view_append_column (
458 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
460 column
= gtk_tree_view_column_new_with_attributes ( "CPU",
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 ( "Birth sec",
473 gtk_tree_view_append_column (
474 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
476 //gtk_tree_view_column_set_visible(column, 0);
478 column
= gtk_tree_view_column_new_with_attributes ( "Birth nsec",
483 gtk_tree_view_append_column (
484 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
486 column
= gtk_tree_view_column_new_with_attributes ( "TRACE",
491 gtk_tree_view_append_column (
492 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
495 //gtk_tree_view_column_set_visible(column, 0);
497 g_object_set_data_full(
498 G_OBJECT(process_list
->process_list_widget
),
501 (GDestroyNotify
)processlist_destroy
);
503 process_list
->index_to_pixmap
= g_ptr_array_sized_new(ALLOCATE_PROCESSES
);
508 void processlist_destroy(ProcessList
*process_list
)
510 g_debug("processlist_destroy %p", process_list
);
511 g_hash_table_destroy(process_list
->process_hash
);
512 process_list
->process_hash
= NULL
;
513 g_ptr_array_free(process_list
->index_to_pixmap
, TRUE
);
515 g_free(process_list
);
516 g_debug("processlist_destroy end");
519 static gboolean
remove_hash_item(ProcessInfo
*process_info
,
520 HashedProcessData
*hashed_process_data
,
521 ProcessList
*process_list
)
525 iter
= hashed_process_data
->y_iter
;
527 gtk_list_store_remove (process_list
->list_store
, &iter
);
528 gdk_pixmap_unref(hashed_process_data
->pixmap
);
530 if(likely(process_list
->current_hash_data
!= NULL
)) {
531 if(likely(hashed_process_data
==
532 process_list
->current_hash_data
[process_info
->cpu
]))
533 process_list
->current_hash_data
[process_info
->cpu
] = NULL
;
535 return TRUE
; /* remove the element from the hash table */
538 void processlist_clear(ProcessList
*process_list
)
540 g_info("processlist_clear %p", process_list
);
542 g_hash_table_foreach_remove(process_list
->process_hash
,
543 (GHRFunc
)remove_hash_item
,
544 (gpointer
)process_list
);
545 process_list
->number_of_process
= 0;
546 update_index_to_pixmap(process_list
);
550 GtkWidget
*processlist_get_widget(ProcessList
*process_list
)
552 return process_list
->process_list_widget
;
556 void destroy_hash_key(gpointer key
)
561 void destroy_hash_data(gpointer data
)
567 void processlist_set_name(ProcessList
*process_list
,
569 HashedProcessData
*hashed_process_data
)
571 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
572 PROCESS_COLUMN
, g_quark_to_string(name
),
576 void processlist_set_brand(ProcessList
*process_list
,
578 HashedProcessData
*hashed_process_data
)
580 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
581 BRAND_COLUMN
, g_quark_to_string(brand
),
584 void processlist_set_ppid(ProcessList
*process_list
,
586 HashedProcessData
*hashed_process_data
)
588 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
594 int processlist_add( ProcessList
*process_list
,
604 ProcessInfo
**pm_process_info
,
605 HashedProcessData
**pm_hashed_process_data
)
607 ProcessInfo
*Process_Info
= g_new(ProcessInfo
, 1);
608 HashedProcessData
*hashed_process_data
= g_new(HashedProcessData
, 1);
609 *pm_hashed_process_data
= hashed_process_data
;
610 *pm_process_info
= Process_Info
;
612 Process_Info
->pid
= pid
;
614 Process_Info
->cpu
= cpu
;
616 Process_Info
->cpu
= 0;
617 Process_Info
->ppid
= ppid
;
618 Process_Info
->birth
= *birth
;
619 Process_Info
->trace_num
= trace_num
;
621 /* When we create it from before state update, we are sure that the
622 * last event occured before the beginning of the global area.
624 * If it is created after state update, this value (0) will be
625 * overriden by the new state before anything is drawn.
627 hashed_process_data
->x
.over
= 0;
628 hashed_process_data
->x
.over_used
= FALSE
;
629 hashed_process_data
->x
.over_marked
= FALSE
;
630 hashed_process_data
->x
.middle
= 0;
631 hashed_process_data
->x
.middle_used
= FALSE
;
632 hashed_process_data
->x
.middle_marked
= FALSE
;
633 hashed_process_data
->x
.under
= 0;
634 hashed_process_data
->x
.under_used
= FALSE
;
635 hashed_process_data
->x
.under_marked
= FALSE
;
636 hashed_process_data
->next_good_time
= ltt_time_zero
;
638 /* Add a new row to the model */
639 gtk_list_store_append ( process_list
->list_store
,
640 &hashed_process_data
->y_iter
);
642 gtk_list_store_set ( process_list
->list_store
, &hashed_process_data
->y_iter
,
643 PROCESS_COLUMN
, g_quark_to_string(name
),
644 BRAND_COLUMN
, g_quark_to_string(brand
),
648 BIRTH_S_COLUMN
, birth
->tv_sec
,
649 BIRTH_NS_COLUMN
, birth
->tv_nsec
,
650 TRACE_COLUMN
, trace_num
,
652 //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget),
653 // GTK_TREE_MODEL(process_list->list_store));
654 //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget));
656 g_hash_table_insert(process_list
->process_hash
,
657 (gpointer
)Process_Info
,
658 (gpointer
)hashed_process_data
);
660 process_list
->number_of_process
++;
662 hashed_process_data
->height
= process_list
->cell_height
;
664 g_assert(hashed_process_data
->height
!= 0);
666 *height
= hashed_process_data
->height
* process_list
->number_of_process
;
668 hashed_process_data
->pixmap
=
669 gdk_pixmap_new(drawing
->drawing_area
->window
,
670 drawing
->alloc_width
,
671 hashed_process_data
->height
,
675 gdk_draw_rectangle (hashed_process_data
->pixmap
,
676 drawing
->drawing_area
->style
->black_gc
,
679 drawing
->alloc_width
,
680 hashed_process_data
->height
);
682 update_index_to_pixmap(process_list
);
688 int processlist_remove( ProcessList
*process_list
,
694 ProcessInfo process_info
;
695 HashedProcessData
*hashed_process_data
;
698 process_info
.pid
= pid
;
700 process_info
.cpu
= cpu
;
702 process_info
.cpu
= 0;
703 process_info
.birth
= *birth
;
704 process_info
.trace_num
= trace_num
;
707 hashed_process_data
=
708 (HashedProcessData
*)g_hash_table_lookup(
709 process_list
->process_hash
,
711 if(likely(hashed_process_data
!= NULL
))
713 iter
= hashed_process_data
->y_iter
;
715 gtk_list_store_remove (process_list
->list_store
, &iter
);
717 g_hash_table_remove(process_list
->process_hash
,
720 if(likely(process_list
->current_hash_data
!= NULL
)) {
721 if(likely(hashed_process_data
== process_list
->current_hash_data
[cpu
])) {
722 process_list
->current_hash_data
[cpu
] = NULL
;
726 gdk_pixmap_unref(hashed_process_data
->pixmap
);
728 update_index_to_pixmap(process_list
);
730 process_list
->number_of_process
--;
740 static inline guint
get_cpu_number_from_name(GQuark name
)
746 string
= g_quark_to_string(name
);
748 begin
= strrchr(string
, '/');
751 g_assert(begin
!= '\0');
753 cpu
= strtoul(begin
, NULL
, 10);