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
resource_sort_func ( GtkTreeModel
*model
,
54 gtk_tree_model_get(model
, it_a
, NAME_COLUMN
, &a_name
, -1);
56 gtk_tree_model_get(model
, it_b
, NAME_COLUMN
, &b_name
, -1);
58 return strcmp(a_name
, b_name
);
61 //static guint process_list_hash_fct(gconstpointer key)
63 // guint pid = ((const ResourceInfo*)key)->pid;
64 // return ((pid>>8 ^ pid>>4 ^ pid>>2 ^ pid) ^ ((const ResourceInfo*)key)->cpu);
67 ///* If hash is good, should be different */
68 //static gboolean process_list_equ_fct(gconstpointer a, gconstpointer b)
70 // const ResourceInfo *pa = (const ResourceInfo*)a;
71 // const ResourceInfo *pb = (const ResourceInfo*)b;
73 // gboolean ret = TRUE;
75 // if(likely(pa->pid != pb->pid))
77 // if(likely((pa->pid == 0 && (pa->cpu != pb->cpu))))
79 // if(unlikely(ltt_time_compare(pa->birth, pb->birth) != 0))
81 // if(unlikely(pa->trace_num != pb->trace_num))
87 static guint
resource_list_hash_fct(gconstpointer key
)
89 gchar
*name
= g_quark_to_string(((const ResourceInfo
*)key
)->name
);
90 return g_str_hash(name
);
93 static gboolean
resource_list_equ_fct(gconstpointer a
, gconstpointer b
)
95 const ResourceInfo
*pa
= (const ResourceInfo
*)a
;
96 const ResourceInfo
*pb
= (const ResourceInfo
*)b
;
100 /* TODO pmf: add some else's here to make it faster */
101 /* TODO pmf: this is highly inefficient */
103 if(likely(strcmp(g_quark_to_string(pa
->name
), g_quark_to_string(pb
->name
)) != 0))
105 if(unlikely(pa
->trace_num
!= pb
->trace_num
))
111 void destroy_hash_key(gpointer key
);
113 void destroy_hash_data(gpointer data
);
116 gboolean
scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer data
)
118 ControlFlowData
*control_flow_data
=
119 (ControlFlowData
*)g_object_get_data(
121 "control_flow_data");
122 Drawing_t
*drawing
= control_flow_data
->drawing
;
123 unsigned int cell_height
=
124 get_cell_height(GTK_TREE_VIEW(control_flow_data
->process_list
->process_list_widget
));
126 switch(event
->direction
) {
128 gtk_adjustment_set_value(control_flow_data
->v_adjust
,
129 gtk_adjustment_get_value(control_flow_data
->v_adjust
) - cell_height
);
131 case GDK_SCROLL_DOWN
:
132 gtk_adjustment_set_value(control_flow_data
->v_adjust
,
133 gtk_adjustment_get_value(control_flow_data
->v_adjust
) + cell_height
);
136 g_error("should only scroll up and down.");
142 static void update_index_to_pixmap_each(ResourceInfo
*key
,
143 HashedResourceData
*value
,
144 ProcessList
*process_list
)
146 guint array_index
= processlist_get_index_from_data(process_list
, value
);
148 g_assert(array_index
< process_list
->index_to_pixmap
->len
);
151 (GdkPixmap
**)&g_ptr_array_index(process_list
->index_to_pixmap
, array_index
);
153 *pixmap
= value
->pixmap
;
157 void update_index_to_pixmap(ProcessList
*process_list
)
159 g_ptr_array_set_size(process_list
->index_to_pixmap
,
160 g_hash_table_size(process_list
->process_hash
));
161 g_hash_table_foreach(process_list
->process_hash
,
162 (GHFunc
)update_index_to_pixmap_each
,
167 static void update_pixmap_size_each(ResourceInfo
*key
,
168 HashedResourceData
*value
,
171 GdkPixmap
*old_pixmap
= value
->pixmap
;
174 gdk_pixmap_new(old_pixmap
,
179 gdk_pixmap_unref(old_pixmap
);
183 void update_pixmap_size(ProcessList
*process_list
, guint width
)
185 g_hash_table_foreach(process_list
->process_hash
,
186 (GHFunc
)update_pixmap_size_each
,
191 typedef struct _CopyPixmap
{
195 gint xsrc
, ysrc
, xdest
, ydest
, width
, height
;
198 static void copy_pixmap_region_each(ResourceInfo
*key
,
199 HashedResourceData
*value
,
202 GdkPixmap
*src
= cp
->src
;
203 GdkPixmap
*dest
= cp
->dest
;
206 dest
= value
->pixmap
;
210 gdk_draw_drawable (dest
,
214 cp
->xdest
, cp
->ydest
,
215 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(ResourceInfo
*key
,
239 HashedResourceData
*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);
287 GDK_PIXMAP(g_ptr_array_index(process_list
->index_to_pixmap
, i
));
289 gdk_draw_drawable (dest
,
299 ProcessList
*processlist_construct(void)
301 GtkTreeViewColumn
*column
;
302 GtkCellRenderer
*renderer
;
304 ProcessList
* process_list
= g_new(ProcessList
,1);
306 process_list
->number_of_process
= 0;
308 process_list
->current_hash_data
= NULL
;
310 /* Create the Process list */
311 process_list
->list_store
= gtk_list_store_new ( N_COLUMNS
, G_TYPE_STRING
);
314 process_list
->process_list_widget
=
315 gtk_tree_view_new_with_model
316 (GTK_TREE_MODEL (process_list
->list_store
));
318 g_object_unref (G_OBJECT (process_list
->list_store
));
320 gtk_tree_sortable_set_default_sort_func(
321 GTK_TREE_SORTABLE(process_list
->list_store
),
327 gtk_tree_sortable_set_sort_column_id(
328 GTK_TREE_SORTABLE(process_list
->list_store
),
329 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID
,
333 process_list
->process_hash
= g_hash_table_new_full(
334 resource_list_hash_fct
, resource_list_equ_fct
,
335 destroy_hash_key
, destroy_hash_data
339 gtk_tree_view_set_headers_visible(
340 GTK_TREE_VIEW(process_list
->process_list_widget
), TRUE
);
342 /* Create a column, associating the "text" attribute of the
343 * cell_renderer to the first column of the model */
344 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
345 renderer
= gtk_cell_renderer_text_new ();
346 process_list
->renderer
= renderer
;
348 gint vertical_separator
;
349 gtk_widget_style_get (GTK_WIDGET (process_list
->process_list_widget
),
350 "vertical-separator", &vertical_separator
,
352 gtk_cell_renderer_get_size(renderer
,
353 GTK_WIDGET(process_list
->process_list_widget
),
358 &process_list
->cell_height
);
360 #if GTK_CHECK_VERSION(2,4,15)
362 g_object_get(G_OBJECT(renderer
), "ypad", &ypad
, NULL
);
364 process_list
->cell_height
+= ypad
;
366 process_list
->cell_height
+= vertical_separator
;
369 column
= gtk_tree_view_column_new_with_attributes ( "Resource",
374 gtk_tree_view_column_set_alignment (column
, 0.0);
375 gtk_tree_view_column_set_fixed_width (column
, 45);
376 gtk_tree_view_append_column (
377 GTK_TREE_VIEW (process_list
->process_list_widget
), column
);
379 process_list
->button
= column
->button
;
381 // column = gtk_tree_view_column_new_with_attributes ( "Brand",
386 // gtk_tree_view_column_set_alignment (column, 0.0);
387 // gtk_tree_view_column_set_fixed_width (column, 45);
388 // gtk_tree_view_append_column (
389 // GTK_TREE_VIEW (process_list->process_list_widget), column);
391 // column = gtk_tree_view_column_new_with_attributes ( "PID",
396 // gtk_tree_view_append_column (
397 // GTK_TREE_VIEW (process_list->process_list_widget), column);
399 // column = gtk_tree_view_column_new_with_attributes ( "TGID",
404 // gtk_tree_view_append_column (
405 // GTK_TREE_VIEW (process_list->process_list_widget), column);
407 // column = gtk_tree_view_column_new_with_attributes ( "PPID",
412 // gtk_tree_view_append_column (
413 // GTK_TREE_VIEW (process_list->process_list_widget), column);
415 // column = gtk_tree_view_column_new_with_attributes ( "CPU",
420 // gtk_tree_view_append_column (
421 // GTK_TREE_VIEW (process_list->process_list_widget), column);
423 // column = gtk_tree_view_column_new_with_attributes ( "Birth sec",
428 // gtk_tree_view_append_column (
429 // GTK_TREE_VIEW (process_list->process_list_widget), column);
431 // //gtk_tree_view_column_set_visible(column, 0);
433 // column = gtk_tree_view_column_new_with_attributes ( "Birth nsec",
438 // gtk_tree_view_append_column (
439 // GTK_TREE_VIEW (process_list->process_list_widget), column);
441 // column = gtk_tree_view_column_new_with_attributes ( "TRACE",
446 // gtk_tree_view_append_column (
447 // GTK_TREE_VIEW (process_list->process_list_widget), column);
450 //gtk_tree_view_column_set_visible(column, 0);
452 g_object_set_data_full(
453 G_OBJECT(process_list
->process_list_widget
),
456 (GDestroyNotify
)processlist_destroy
);
458 process_list
->index_to_pixmap
= g_ptr_array_sized_new(ALLOCATE_PROCESSES
);
463 void processlist_destroy(ProcessList
*process_list
)
465 g_debug("processlist_destroy %p", process_list
);
466 g_hash_table_destroy(process_list
->process_hash
);
467 process_list
->process_hash
= NULL
;
468 g_ptr_array_free(process_list
->index_to_pixmap
, TRUE
);
470 g_free(process_list
);
471 g_debug("processlist_destroy end");
474 static gboolean
remove_hash_item(ResourceInfo
*process_info
,
475 HashedResourceData
*hashed_process_data
,
476 ProcessList
*process_list
)
480 iter
= hashed_process_data
->y_iter
;
482 gtk_list_store_remove (process_list
->list_store
, &iter
);
483 gdk_pixmap_unref(hashed_process_data
->pixmap
);
485 // TODO pmf: check this; might be needed
486 // if(likely(process_list->current_hash_data != NULL)) {
487 // if(likely(hashed_process_data ==
488 // process_list->current_hash_data[process_info->trace_num][process_info->cpu]))
489 // process_list->current_hash_data[process_info->trace_num][process_info->cpu] = NULL;
491 return TRUE
; /* remove the element from the hash table */
494 void processlist_clear(ProcessList
*process_list
)
496 g_info("processlist_clear %p", process_list
);
498 g_hash_table_foreach_remove(process_list
->process_hash
,
499 (GHRFunc
)remove_hash_item
,
500 (gpointer
)process_list
);
501 process_list
->number_of_process
= 0;
502 update_index_to_pixmap(process_list
);
506 GtkWidget
*processlist_get_widget(ProcessList
*process_list
)
508 return process_list
->process_list_widget
;
512 void destroy_hash_key(gpointer key
)
517 void destroy_hash_data(gpointer data
)
523 //void processlist_set_name(ProcessList *process_list,
525 // HashedResourceData *hashed_process_data)
527 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
528 // PROCESS_COLUMN, g_quark_to_string(name),
532 //void processlist_set_brand(ProcessList *process_list,
534 // HashedResourceData *hashed_process_data)
536 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
537 // BRAND_COLUMN, g_quark_to_string(brand),
541 //void processlist_set_tgid(ProcessList *process_list,
543 // HashedResourceData *hashed_process_data)
545 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
546 // TGID_COLUMN, tgid,
550 //void processlist_set_ppid(ProcessList *process_list,
552 // HashedResourceData *hashed_process_data)
554 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
555 // PPID_COLUMN, ppid,
559 int resourcelist_add( ProcessList
*process_list
,
566 ResourceInfo
**pm_resource_info
,
567 HashedResourceData
**pm_hashed_resource_data
)
569 ResourceInfo
*Resource_Info
= g_new(ResourceInfo
, 1);
570 HashedResourceData
*hashed_resource_data
= g_new(HashedResourceData
, 1);
571 *pm_hashed_resource_data
= hashed_resource_data
;
572 *pm_resource_info
= Resource_Info
;
574 Resource_Info
->name
= name
;
576 // Process_Info->pid = pid;
577 // Process_Info->tgid = tgid;
579 // Process_Info->cpu = cpu;
581 // Process_Info->cpu = 0;
582 // Process_Info->ppid = ppid;
583 // Process_Info->birth = *birth;
584 Resource_Info
->trace_num
= trace_num
;
585 Resource_Info
->type
= type
;
586 Resource_Info
->id
= id
;
588 /* When we create it from before state update, we are sure that the
589 * last event occured before the beginning of the global area.
591 * If it is created after state update, this value (0) will be
592 * overriden by the new state before anything is drawn.
594 * There are 3 potential lines for the each process: one in the middle,
595 * one under it and one over it. The {over,middle,under} fields tell us
596 * the x pixel on the pixmap where we are. The _used fields tell us
597 * whether that pixel was used. The _marked field tells us if we marked a
600 hashed_resource_data
->x
.over
= 0;
601 hashed_resource_data
->x
.over_used
= FALSE
;
602 hashed_resource_data
->x
.over_marked
= FALSE
;
603 hashed_resource_data
->x
.middle
= 0; // last
604 hashed_resource_data
->x
.middle_used
= FALSE
;
605 hashed_resource_data
->x
.middle_marked
= FALSE
;
606 hashed_resource_data
->x
.under
= 0;
607 hashed_resource_data
->x
.under_used
= FALSE
;
608 hashed_resource_data
->x
.under_marked
= FALSE
;
609 hashed_resource_data
->next_good_time
= ltt_time_zero
;
611 /* Add a new row to the model */
612 gtk_list_store_append ( process_list
->list_store
,
613 &hashed_resource_data
->y_iter
);
615 gtk_list_store_set ( process_list
->list_store
, &hashed_resource_data
->y_iter
,
616 NAME_COLUMN
, g_quark_to_string(name
),
619 g_hash_table_insert(process_list
->process_hash
,
620 (gpointer
)Resource_Info
,
621 (gpointer
)hashed_resource_data
);
623 process_list
->number_of_process
++; // of resources
625 hashed_resource_data
->height
= process_list
->cell_height
;
627 g_assert(hashed_resource_data
->height
!= 0);
629 *height
= hashed_resource_data
->height
* process_list
->number_of_process
;
631 hashed_resource_data
->pixmap
=
632 gdk_pixmap_new(drawing
->drawing_area
->window
,
633 drawing
->alloc_width
,
634 hashed_resource_data
->height
,
637 // Clear the image with black background
638 gdk_draw_rectangle (hashed_resource_data
->pixmap
,
639 drawing
->drawing_area
->style
->black_gc
,
642 drawing
->alloc_width
,
643 hashed_resource_data
->height
);
645 update_index_to_pixmap(process_list
);
649 //int processlist_add( ProcessList *process_list,
650 // Drawing_t *drawing,
660 // ResourceInfo **pm_process_info,
661 // HashedResourceData **pm_hashed_process_data)
663 // ResourceInfo *Process_Info = g_new(ResourceInfo, 1);
664 // HashedResourceData *hashed_process_data = g_new(HashedResourceData, 1);
665 // *pm_hashed_process_data = hashed_process_data;
666 // *pm_process_info = Process_Info;
668 // Process_Info->pid = pid;
669 // Process_Info->tgid = tgid;
671 // Process_Info->cpu = cpu;
673 // Process_Info->cpu = 0;
674 // Process_Info->ppid = ppid;
675 // Process_Info->birth = *birth;
676 // Process_Info->trace_num = trace_num;
678 // /* When we create it from before state update, we are sure that the
679 // * last event occured before the beginning of the global area.
681 // * If it is created after state update, this value (0) will be
682 // * overriden by the new state before anything is drawn.
684 // hashed_process_data->x.over = 0;
685 // hashed_process_data->x.over_used = FALSE;
686 // hashed_process_data->x.over_marked = FALSE;
687 // hashed_process_data->x.middle = 0;
688 // hashed_process_data->x.middle_used = FALSE;
689 // hashed_process_data->x.middle_marked = FALSE;
690 // hashed_process_data->x.under = 0;
691 // hashed_process_data->x.under_used = FALSE;
692 // hashed_process_data->x.under_marked = FALSE;
693 // hashed_process_data->next_good_time = ltt_time_zero;
695 // /* Add a new row to the model */
696 // gtk_list_store_append ( process_list->list_store,
697 // &hashed_process_data->y_iter);
699 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
700 // PROCESS_COLUMN, g_quark_to_string(name),
701 // BRAND_COLUMN, g_quark_to_string(brand),
703 // TGID_COLUMN, tgid,
704 // PPID_COLUMN, ppid,
706 // BIRTH_S_COLUMN, birth->tv_sec,
707 // BIRTH_NS_COLUMN, birth->tv_nsec,
708 // TRACE_COLUMN, trace_num,
710 // //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget),
711 // // GTK_TREE_MODEL(process_list->list_store));
712 // //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget));
714 // g_hash_table_insert(process_list->process_hash,
715 // (gpointer)Process_Info,
716 // (gpointer)hashed_process_data);
718 // process_list->number_of_process++;
720 // hashed_process_data->height = process_list->cell_height;
722 // g_assert(hashed_process_data->height != 0);
724 // *height = hashed_process_data->height * process_list->number_of_process;
726 // hashed_process_data->pixmap =
727 // gdk_pixmap_new(drawing->drawing_area->window,
728 // drawing->alloc_width,
729 // hashed_process_data->height,
732 // // Clear the image
733 // gdk_draw_rectangle (hashed_process_data->pixmap,
734 // drawing->drawing_area->style->black_gc,
737 // drawing->alloc_width,
738 // hashed_process_data->height);
740 // update_index_to_pixmap(process_list);
746 // TODO pmf: make this work once again
747 //int processlist_remove( ProcessList *process_list,
753 // ResourceInfo process_info;
754 // HashedResourceData *hashed_process_data;
757 // process_info.pid = pid;
759 // process_info.cpu = cpu;
761 // process_info.cpu = 0;
762 // process_info.birth = *birth;
763 // process_info.trace_num = trace_num;
766 // hashed_process_data =
767 // (HashedResourceData*)g_hash_table_lookup(
768 // process_list->process_hash,
770 // if(likely(hashed_process_data != NULL))
772 // iter = hashed_process_data->y_iter;
774 // gtk_list_store_remove (process_list->list_store, &iter);
776 // g_hash_table_remove(process_list->process_hash,
779 // if(likely(process_list->current_hash_data != NULL)) {
780 // if(likely(hashed_process_data == process_list->current_hash_data[trace_num][cpu])) {
781 // process_list->current_hash_data[trace_num][cpu] = NULL;
785 // gdk_pixmap_unref(hashed_process_data->pixmap);
787 // update_index_to_pixmap(process_list);
789 // process_list->number_of_process--;
799 static inline guint
get_cpu_number_from_name(GQuark name
)
805 string
= g_quark_to_string(name
);
807 begin
= strrchr(string
, '/');
810 g_assert(begin
!= '\0');
812 cpu
= strtoul(begin
, NULL
, 10);