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,
31 #include "processlist.h"
35 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
36 //#define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
38 /* Preallocated Size of the index_to_pixmap array */
39 #define ALLOCATE_PROCESSES 1000
41 /*****************************************************************************
42 * Methods to synchronize process list *
43 *****************************************************************************/
46 //gint process_sort_func ( GtkTreeModel *model,
49 // gpointer user_data)
53 // guint a_pid, a_tgid, a_ppid, a_cpu;
54 // gulong a_birth_s, a_birth_ns;
59 // guint b_pid, b_tgid, b_ppid, b_cpu;
60 // gulong b_birth_s, b_birth_ns;
63 // gtk_tree_model_get(model,
65 // PROCESS_COLUMN, &a_name,
66 // BRAND_COLUMN, &a_brand,
67 // PID_COLUMN, &a_pid,
68 // TGID_COLUMN, &a_tgid,
69 // PPID_COLUMN, &a_ppid,
70 // CPU_COLUMN, &a_cpu,
71 // BIRTH_S_COLUMN, &a_birth_s,
72 // BIRTH_NS_COLUMN, &a_birth_ns,
73 // TRACE_COLUMN, &a_trace,
76 // gtk_tree_model_get(model,
78 // PROCESS_COLUMN, &b_name,
79 // BRAND_COLUMN, &b_brand,
80 // PID_COLUMN, &b_pid,
81 // TGID_COLUMN, &b_tgid,
82 // PPID_COLUMN, &b_ppid,
83 // CPU_COLUMN, &b_cpu,
84 // BIRTH_S_COLUMN, &b_birth_s,
85 // BIRTH_NS_COLUMN, &b_birth_ns,
86 // TRACE_COLUMN, &b_trace,
91 // if(a_pid == 0 && b_pid == 0) {
92 // /* If 0, order by CPU */
93 // if(a_cpu > b_cpu) return 1;
94 // if(a_cpu < b_cpu) return -1;
96 // } else { /* if not 0, order by pid */
98 // if(a_pid > b_pid) return 1;
99 // if(a_pid < b_pid) return -1;
102 // /* Order by birth second */
104 // if(a_birth_s > b_birth_s) return 1;
105 // if(a_birth_s < b_birth_s) return -1;
108 // /* Order by birth nanosecond */
109 // if(a_birth_ns > b_birth_ns) return 1;
110 // if(a_birth_ns < b_birth_ns) return -1;
112 // /* Order by trace_num */
113 // if(a_trace > b_trace) return 1;
114 // if(a_trace < b_trace) return -1;
120 //static guint process_list_hash_fct(gconstpointer key)
122 // guint pid = ((const ResourceInfo*)key)->pid;
123 // return ((pid>>8 ^ pid>>4 ^ pid>>2 ^ pid) ^ ((const ResourceInfo*)key)->cpu);
126 ///* If hash is good, should be different */
127 //static gboolean process_list_equ_fct(gconstpointer a, gconstpointer b)
129 // const ResourceInfo *pa = (const ResourceInfo*)a;
130 // const ResourceInfo *pb = (const ResourceInfo*)b;
132 // gboolean ret = TRUE;
134 // if(likely(pa->pid != pb->pid))
136 // if(likely((pa->pid == 0 && (pa->cpu != pb->cpu))))
138 // if(unlikely(ltt_time_compare(pa->birth, pb->birth) != 0))
140 // if(unlikely(pa->trace_num != pb->trace_num))
146 static guint
resource_list_hash_fct(gconstpointer key
)
148 gchar
*name
= g_quark_to_string(((const ResourceInfo
*)key
)->name
);
149 return g_str_hash(name
);
152 static gboolean
resource_list_equ_fct(gconstpointer a
, gconstpointer b
)
154 const ResourceInfo
*pa
= (const ResourceInfo
*)a
;
155 const ResourceInfo
*pb
= (const ResourceInfo
*)b
;
159 /* TODO pmf: add some else's here to make it faster */
160 /* TODO pmf: this is highly inefficient */
162 if(likely(strcmp(g_quark_to_string(pa
->name
), g_quark_to_string(pb
->name
)) != 0))
164 if(unlikely(pa
->trace_num
!= pb
->trace_num
))
170 void destroy_hash_key(gpointer key
);
172 void destroy_hash_data(gpointer data
);
175 gboolean
scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer data
)
177 ControlFlowData
*control_flow_data
=
178 (ControlFlowData
*)g_object_get_data(
180 "control_flow_data");
181 Drawing_t
*drawing
= control_flow_data
->drawing
;
182 unsigned int cell_height
=
183 get_cell_height(GTK_TREE_VIEW(control_flow_data
->process_list
->process_list_widget
));
185 switch(event
->direction
) {
187 gtk_adjustment_set_value(control_flow_data
->v_adjust
,
188 gtk_adjustment_get_value(control_flow_data
->v_adjust
) - cell_height
);
190 case GDK_SCROLL_DOWN
:
191 gtk_adjustment_set_value(control_flow_data
->v_adjust
,
192 gtk_adjustment_get_value(control_flow_data
->v_adjust
) + cell_height
);
195 g_error("should only scroll up and down.");
201 static void update_index_to_pixmap_each(ResourceInfo
*key
,
202 HashedResourceData
*value
,
203 ProcessList
*process_list
)
205 guint array_index
= processlist_get_index_from_data(process_list
, value
);
207 g_assert(array_index
< process_list
->index_to_pixmap
->len
);
210 (GdkPixmap
**)&g_ptr_array_index(process_list
->index_to_pixmap
, array_index
);
212 *pixmap
= value
->pixmap
;
216 void update_index_to_pixmap(ProcessList
*process_list
)
218 g_ptr_array_set_size(process_list
->index_to_pixmap
,
219 g_hash_table_size(process_list
->process_hash
));
220 g_hash_table_foreach(process_list
->process_hash
,
221 (GHFunc
)update_index_to_pixmap_each
,
226 static void update_pixmap_size_each(ResourceInfo
*key
,
227 HashedResourceData
*value
,
230 GdkPixmap
*old_pixmap
= value
->pixmap
;
233 gdk_pixmap_new(old_pixmap
,
238 gdk_pixmap_unref(old_pixmap
);
242 void update_pixmap_size(ProcessList
*process_list
, guint width
)
244 g_hash_table_foreach(process_list
->process_hash
,
245 (GHFunc
)update_pixmap_size_each
,
250 typedef struct _CopyPixmap
{
254 gint xsrc
, ysrc
, xdest
, ydest
, width
, height
;
257 static void copy_pixmap_region_each(ResourceInfo
*key
,
258 HashedResourceData
*value
,
261 GdkPixmap
*src
= cp
->src
;
262 GdkPixmap
*dest
= cp
->dest
;
265 dest
= value
->pixmap
;
269 gdk_draw_drawable (dest
,
273 cp
->xdest
, cp
->ydest
,
274 cp
->width
, cp
->height
);
277 void copy_pixmap_region(ProcessList
*process_list
, GdkDrawable
*dest
,
278 GdkGC
*gc
, GdkDrawable
*src
,
279 gint xsrc
, gint ysrc
,
280 gint xdest
, gint ydest
, gint width
, gint height
)
282 CopyPixmap cp
= { dest
, gc
, src
, xsrc
, ysrc
, xdest
, ydest
, width
, height
};
284 g_hash_table_foreach(process_list
->process_hash
,
285 (GHFunc
)copy_pixmap_region_each
,
291 typedef struct _RectanglePixmap
{
293 gint x
, y
, width
, height
;
297 static void rectangle_pixmap_each(ResourceInfo
*key
,
298 HashedResourceData
*value
,
302 rp
->height
= value
->height
;
304 gdk_draw_rectangle (value
->pixmap
,
308 rp
->width
, rp
->height
);
314 void rectangle_pixmap(ProcessList
*process_list
, GdkGC
*gc
,
315 gboolean filled
, gint x
, gint y
, gint width
, gint height
)
317 RectanglePixmap rp
= { filled
, x
, y
, width
, height
, gc
};
319 g_hash_table_foreach(process_list
->process_hash
,
320 (GHFunc
)rectangle_pixmap_each
,
325 /* Renders each pixmaps into on big drawable */
326 void copy_pixmap_to_screen(ProcessList
*process_list
,
330 gint width
, gint height
)
332 if(process_list
->index_to_pixmap
->len
== 0) return;
333 guint cell_height
= process_list
->cell_height
;
336 gint begin
= floor(y
/(double)cell_height
);
337 gint end
= MIN(ceil((y
+height
)/(double)cell_height
),
338 process_list
->index_to_pixmap
->len
);
341 for(i
=begin
; i
<end
; i
++) {
342 g_assert(i
<process_list
->index_to_pixmap
->len
);
343 /* Render the pixmap to the screen */
345 //(GdkPixmap*)g_ptr_array_index(process_list->index_to_pixmap, i);
346 GDK_PIXMAP(g_ptr_array_index(process_list
->index_to_pixmap
, i
));
348 gdk_draw_drawable (dest
,
358 ProcessList
*processlist_construct(void)
360 GtkTreeViewColumn
*column
;
361 GtkCellRenderer
*renderer
;
363 ProcessList
* process_list
= g_new(ProcessList
,1);
365 process_list
->number_of_process
= 0;
367 process_list
->current_hash_data
= NULL
;
369 /* Create the Process list */
370 process_list
->list_store
= gtk_list_store_new ( N_COLUMNS
,
382 process_list
->process_list_widget
=
383 gtk_tree_view_new_with_model
384 (GTK_TREE_MODEL (process_list
->list_store
));
386 g_object_unref (G_OBJECT (process_list
->list_store
));
388 // gtk_tree_sortable_set_default_sort_func(
389 // GTK_TREE_SORTABLE(process_list->list_store),
390 // process_sort_func,
395 // gtk_tree_sortable_set_sort_column_id(
396 // GTK_TREE_SORTABLE(process_list->list_store),
397 // GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
398 // GTK_SORT_ASCENDING);
401 process_list
->process_hash
= g_hash_table_new_full(
402 resource_list_hash_fct
, resource_list_equ_fct
,
403 destroy_hash_key
, destroy_hash_data
407 gtk_tree_view_set_headers_visible(
408 GTK_TREE_VIEW(process_list
->process_list_widget
), TRUE
);
410 /* Create a column, associating the "text" attribute of the
411 * cell_renderer to the first column of the model */
412 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
413 renderer
= gtk_cell_renderer_text_new ();
414 process_list
->renderer
= renderer
;
416 gint vertical_separator
;
417 gtk_widget_style_get (GTK_WIDGET (process_list
->process_list_widget
),
418 "vertical-separator", &vertical_separator
,
420 gtk_cell_renderer_get_size(renderer
,
421 GTK_WIDGET(process_list
->process_list_widget
),
426 &process_list
->cell_height
);
428 #if GTK_CHECK_VERSION(2,4,15)
430 g_object_get(G_OBJECT(renderer
), "ypad", &ypad
, NULL
);
432 process_list
->cell_height
+= ypad
;
434 process_list
->cell_height
+= vertical_separator
;
437 column
= gtk_tree_view_column_new_with_attributes ( "Resource",
442 gtk_tree_view_column_set_alignment (column
, 0.0);
443 gtk_tree_view_column_set_fixed_width (column
, 45);
444 gtk_tree_view_append_column (
445 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
447 process_list
->button
= column
->button
;
449 // column = gtk_tree_view_column_new_with_attributes ( "Brand",
454 // gtk_tree_view_column_set_alignment (column, 0.0);
455 // gtk_tree_view_column_set_fixed_width (column, 45);
456 // gtk_tree_view_append_column (
457 // GTK_TREE_VIEW (process_list->process_list_widget), column);
459 // column = gtk_tree_view_column_new_with_attributes ( "PID",
464 // gtk_tree_view_append_column (
465 // GTK_TREE_VIEW (process_list->process_list_widget), column);
467 // column = gtk_tree_view_column_new_with_attributes ( "TGID",
472 // gtk_tree_view_append_column (
473 // GTK_TREE_VIEW (process_list->process_list_widget), column);
475 // column = gtk_tree_view_column_new_with_attributes ( "PPID",
480 // gtk_tree_view_append_column (
481 // GTK_TREE_VIEW (process_list->process_list_widget), column);
483 // column = gtk_tree_view_column_new_with_attributes ( "CPU",
488 // gtk_tree_view_append_column (
489 // GTK_TREE_VIEW (process_list->process_list_widget), column);
491 // column = gtk_tree_view_column_new_with_attributes ( "Birth sec",
496 // gtk_tree_view_append_column (
497 // GTK_TREE_VIEW (process_list->process_list_widget), column);
499 // //gtk_tree_view_column_set_visible(column, 0);
501 // column = gtk_tree_view_column_new_with_attributes ( "Birth nsec",
506 // gtk_tree_view_append_column (
507 // GTK_TREE_VIEW (process_list->process_list_widget), column);
509 // column = gtk_tree_view_column_new_with_attributes ( "TRACE",
514 // gtk_tree_view_append_column (
515 // GTK_TREE_VIEW (process_list->process_list_widget), column);
518 //gtk_tree_view_column_set_visible(column, 0);
520 g_object_set_data_full(
521 G_OBJECT(process_list
->process_list_widget
),
524 (GDestroyNotify
)processlist_destroy
);
526 process_list
->index_to_pixmap
= g_ptr_array_sized_new(ALLOCATE_PROCESSES
);
531 void processlist_destroy(ProcessList
*process_list
)
533 g_debug("processlist_destroy %p", process_list
);
534 g_hash_table_destroy(process_list
->process_hash
);
535 process_list
->process_hash
= NULL
;
536 g_ptr_array_free(process_list
->index_to_pixmap
, TRUE
);
538 g_free(process_list
);
539 g_debug("processlist_destroy end");
542 static gboolean
remove_hash_item(ResourceInfo
*process_info
,
543 HashedResourceData
*hashed_process_data
,
544 ProcessList
*process_list
)
548 iter
= hashed_process_data
->y_iter
;
550 gtk_list_store_remove (process_list
->list_store
, &iter
);
551 gdk_pixmap_unref(hashed_process_data
->pixmap
);
553 // TODO pmf: check this; might be needed
554 // if(likely(process_list->current_hash_data != NULL)) {
555 // if(likely(hashed_process_data ==
556 // process_list->current_hash_data[process_info->trace_num][process_info->cpu]))
557 // process_list->current_hash_data[process_info->trace_num][process_info->cpu] = NULL;
559 return TRUE
; /* remove the element from the hash table */
562 void processlist_clear(ProcessList
*process_list
)
564 g_info("processlist_clear %p", process_list
);
566 g_hash_table_foreach_remove(process_list
->process_hash
,
567 (GHRFunc
)remove_hash_item
,
568 (gpointer
)process_list
);
569 process_list
->number_of_process
= 0;
570 update_index_to_pixmap(process_list
);
574 GtkWidget
*processlist_get_widget(ProcessList
*process_list
)
576 return process_list
->process_list_widget
;
580 void destroy_hash_key(gpointer key
)
585 void destroy_hash_data(gpointer data
)
591 //void processlist_set_name(ProcessList *process_list,
593 // HashedResourceData *hashed_process_data)
595 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
596 // PROCESS_COLUMN, g_quark_to_string(name),
600 //void processlist_set_brand(ProcessList *process_list,
602 // HashedResourceData *hashed_process_data)
604 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
605 // BRAND_COLUMN, g_quark_to_string(brand),
609 //void processlist_set_tgid(ProcessList *process_list,
611 // HashedResourceData *hashed_process_data)
613 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
614 // TGID_COLUMN, tgid,
618 //void processlist_set_ppid(ProcessList *process_list,
620 // HashedResourceData *hashed_process_data)
622 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
623 // PPID_COLUMN, ppid,
627 int resourcelist_add( ProcessList
*process_list
,
634 ResourceInfo
**pm_resource_info
,
635 HashedResourceData
**pm_hashed_resource_data
)
637 ResourceInfo
*Resource_Info
= g_new(ResourceInfo
, 1);
638 HashedResourceData
*hashed_resource_data
= g_new(HashedResourceData
, 1);
639 *pm_hashed_resource_data
= hashed_resource_data
;
640 *pm_resource_info
= Resource_Info
;
642 Resource_Info
->name
= name
;
644 // Process_Info->pid = pid;
645 // Process_Info->tgid = tgid;
647 // Process_Info->cpu = cpu;
649 // Process_Info->cpu = 0;
650 // Process_Info->ppid = ppid;
651 // Process_Info->birth = *birth;
652 Resource_Info
->trace_num
= trace_num
;
653 Resource_Info
->type
= type
;
654 Resource_Info
->id
= id
;
656 /* When we create it from before state update, we are sure that the
657 * last event occured before the beginning of the global area.
659 * If it is created after state update, this value (0) will be
660 * overriden by the new state before anything is drawn.
662 * There are 3 potential lines for the each process: one in the middle,
663 * one under it and one over it. The {over,middle,under} fields tell us
664 * the x pixel on the pixmap where we are. The _used fields tell us
665 * whether that pixel was used. The _marked field tells us if we marked a
668 hashed_resource_data
->x
.over
= 0;
669 hashed_resource_data
->x
.over_used
= FALSE
;
670 hashed_resource_data
->x
.over_marked
= FALSE
;
671 hashed_resource_data
->x
.middle
= 0; // last
672 hashed_resource_data
->x
.middle_used
= FALSE
;
673 hashed_resource_data
->x
.middle_marked
= FALSE
;
674 hashed_resource_data
->x
.under
= 0;
675 hashed_resource_data
->x
.under_used
= FALSE
;
676 hashed_resource_data
->x
.under_marked
= FALSE
;
677 hashed_resource_data
->next_good_time
= ltt_time_zero
;
679 /* Add a new row to the model */
680 gtk_list_store_append ( process_list
->list_store
,
681 &hashed_resource_data
->y_iter
);
683 gtk_list_store_set ( process_list
->list_store
, &hashed_resource_data
->y_iter
,
684 NAME_COLUMN
, g_quark_to_string(name
),
687 g_hash_table_insert(process_list
->process_hash
,
688 (gpointer
)Resource_Info
,
689 (gpointer
)hashed_resource_data
);
691 process_list
->number_of_process
++; // of resources
693 hashed_resource_data
->height
= process_list
->cell_height
;
695 g_assert(hashed_resource_data
->height
!= 0);
697 *height
= hashed_resource_data
->height
* process_list
->number_of_process
;
699 hashed_resource_data
->pixmap
=
700 gdk_pixmap_new(drawing
->drawing_area
->window
,
701 drawing
->alloc_width
,
702 hashed_resource_data
->height
,
705 // Clear the image with black background
706 gdk_draw_rectangle (hashed_resource_data
->pixmap
,
707 drawing
->drawing_area
->style
->black_gc
,
710 drawing
->alloc_width
,
711 hashed_resource_data
->height
);
713 update_index_to_pixmap(process_list
);
717 //int processlist_add( ProcessList *process_list,
718 // Drawing_t *drawing,
728 // ResourceInfo **pm_process_info,
729 // HashedResourceData **pm_hashed_process_data)
731 // ResourceInfo *Process_Info = g_new(ResourceInfo, 1);
732 // HashedResourceData *hashed_process_data = g_new(HashedResourceData, 1);
733 // *pm_hashed_process_data = hashed_process_data;
734 // *pm_process_info = Process_Info;
736 // Process_Info->pid = pid;
737 // Process_Info->tgid = tgid;
739 // Process_Info->cpu = cpu;
741 // Process_Info->cpu = 0;
742 // Process_Info->ppid = ppid;
743 // Process_Info->birth = *birth;
744 // Process_Info->trace_num = trace_num;
746 // /* When we create it from before state update, we are sure that the
747 // * last event occured before the beginning of the global area.
749 // * If it is created after state update, this value (0) will be
750 // * overriden by the new state before anything is drawn.
752 // hashed_process_data->x.over = 0;
753 // hashed_process_data->x.over_used = FALSE;
754 // hashed_process_data->x.over_marked = FALSE;
755 // hashed_process_data->x.middle = 0;
756 // hashed_process_data->x.middle_used = FALSE;
757 // hashed_process_data->x.middle_marked = FALSE;
758 // hashed_process_data->x.under = 0;
759 // hashed_process_data->x.under_used = FALSE;
760 // hashed_process_data->x.under_marked = FALSE;
761 // hashed_process_data->next_good_time = ltt_time_zero;
763 // /* Add a new row to the model */
764 // gtk_list_store_append ( process_list->list_store,
765 // &hashed_process_data->y_iter);
767 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
768 // PROCESS_COLUMN, g_quark_to_string(name),
769 // BRAND_COLUMN, g_quark_to_string(brand),
771 // TGID_COLUMN, tgid,
772 // PPID_COLUMN, ppid,
774 // BIRTH_S_COLUMN, birth->tv_sec,
775 // BIRTH_NS_COLUMN, birth->tv_nsec,
776 // TRACE_COLUMN, trace_num,
778 // //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget),
779 // // GTK_TREE_MODEL(process_list->list_store));
780 // //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget));
782 // g_hash_table_insert(process_list->process_hash,
783 // (gpointer)Process_Info,
784 // (gpointer)hashed_process_data);
786 // process_list->number_of_process++;
788 // hashed_process_data->height = process_list->cell_height;
790 // g_assert(hashed_process_data->height != 0);
792 // *height = hashed_process_data->height * process_list->number_of_process;
794 // hashed_process_data->pixmap =
795 // gdk_pixmap_new(drawing->drawing_area->window,
796 // drawing->alloc_width,
797 // hashed_process_data->height,
800 // // Clear the image
801 // gdk_draw_rectangle (hashed_process_data->pixmap,
802 // drawing->drawing_area->style->black_gc,
805 // drawing->alloc_width,
806 // hashed_process_data->height);
808 // update_index_to_pixmap(process_list);
814 // TODO pmf: make this work once again
815 //int processlist_remove( ProcessList *process_list,
821 // ResourceInfo process_info;
822 // HashedResourceData *hashed_process_data;
825 // process_info.pid = pid;
827 // process_info.cpu = cpu;
829 // process_info.cpu = 0;
830 // process_info.birth = *birth;
831 // process_info.trace_num = trace_num;
834 // hashed_process_data =
835 // (HashedResourceData*)g_hash_table_lookup(
836 // process_list->process_hash,
838 // if(likely(hashed_process_data != NULL))
840 // iter = hashed_process_data->y_iter;
842 // gtk_list_store_remove (process_list->list_store, &iter);
844 // g_hash_table_remove(process_list->process_hash,
847 // if(likely(process_list->current_hash_data != NULL)) {
848 // if(likely(hashed_process_data == process_list->current_hash_data[trace_num][cpu])) {
849 // process_list->current_hash_data[trace_num][cpu] = NULL;
853 // gdk_pixmap_unref(hashed_process_data->pixmap);
855 // update_index_to_pixmap(process_list);
857 // process_list->number_of_process--;
867 static inline guint
get_cpu_number_from_name(GQuark name
)
873 string
= g_quark_to_string(name
);
875 begin
= strrchr(string
, '/');
878 g_assert(begin
!= '\0');
880 cpu
= strtoul(begin
, NULL
, 10);