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,
323 process_list->process_list_widget =
324 gtk_tree_view_new_with_model
325 (GTK_TREE_MODEL (process_list->list_store));
327 g_object_unref (G_OBJECT (process_list->list_store));
329 gtk_tree_sortable_set_default_sort_func(
330 GTK_TREE_SORTABLE(process_list->list_store),
336 gtk_tree_sortable_set_sort_column_id(
337 GTK_TREE_SORTABLE(process_list->list_store),
338 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
342 process_list->process_hash = g_hash_table_new_full(
343 resource_list_hash_fct, resource_list_equ_fct,
344 destroy_hash_key, destroy_hash_data
348 gtk_tree_view_set_headers_visible(
349 GTK_TREE_VIEW(process_list->process_list_widget), TRUE);
351 /* Create a column, associating the "text" attribute of the
352 * cell_renderer to the first column of the model */
353 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
354 renderer = gtk_cell_renderer_text_new ();
355 process_list->renderer = renderer;
357 gint vertical_separator;
358 gtk_widget_style_get (GTK_WIDGET (process_list->process_list_widget),
359 "vertical-separator", &vertical_separator,
361 gtk_cell_renderer_get_size(renderer,
362 GTK_WIDGET(process_list->process_list_widget),
367 &process_list->cell_height);
369 #if GTK_CHECK_VERSION(2,4,15)
371 g_object_get(G_OBJECT(renderer), "ypad", &ypad, NULL);
373 process_list->cell_height += ypad;
375 process_list->cell_height += vertical_separator;
378 column = gtk_tree_view_column_new_with_attributes ( "Resource",
383 gtk_tree_view_column_set_alignment (column, 0.0);
384 gtk_tree_view_column_set_fixed_width (column, 45);
385 gtk_tree_view_append_column (
386 GTK_TREE_VIEW (process_list->process_list_widget), column);
388 process_list->button = column->button;
390 // column = gtk_tree_view_column_new_with_attributes ( "Brand",
395 // gtk_tree_view_column_set_alignment (column, 0.0);
396 // gtk_tree_view_column_set_fixed_width (column, 45);
397 // gtk_tree_view_append_column (
398 // GTK_TREE_VIEW (process_list->process_list_widget), column);
400 // column = gtk_tree_view_column_new_with_attributes ( "PID",
405 // gtk_tree_view_append_column (
406 // GTK_TREE_VIEW (process_list->process_list_widget), column);
408 // column = gtk_tree_view_column_new_with_attributes ( "TGID",
413 // gtk_tree_view_append_column (
414 // GTK_TREE_VIEW (process_list->process_list_widget), column);
416 // column = gtk_tree_view_column_new_with_attributes ( "PPID",
421 // gtk_tree_view_append_column (
422 // GTK_TREE_VIEW (process_list->process_list_widget), column);
424 // column = gtk_tree_view_column_new_with_attributes ( "CPU",
429 // gtk_tree_view_append_column (
430 // GTK_TREE_VIEW (process_list->process_list_widget), column);
432 // column = gtk_tree_view_column_new_with_attributes ( "Birth sec",
437 // gtk_tree_view_append_column (
438 // GTK_TREE_VIEW (process_list->process_list_widget), column);
440 // //gtk_tree_view_column_set_visible(column, 0);
442 // column = gtk_tree_view_column_new_with_attributes ( "Birth nsec",
447 // gtk_tree_view_append_column (
448 // GTK_TREE_VIEW (process_list->process_list_widget), column);
450 // column = gtk_tree_view_column_new_with_attributes ( "TRACE",
455 // gtk_tree_view_append_column (
456 // GTK_TREE_VIEW (process_list->process_list_widget), column);
459 //gtk_tree_view_column_set_visible(column, 0);
461 g_object_set_data_full(
462 G_OBJECT(process_list->process_list_widget),
465 (GDestroyNotify)processlist_destroy);
467 process_list->index_to_pixmap = g_ptr_array_sized_new(ALLOCATE_PROCESSES);
472 void processlist_destroy(ProcessList *process_list)
474 g_debug("processlist_destroy %p", process_list);
475 g_hash_table_destroy(process_list->process_hash);
476 process_list->process_hash = NULL;
477 g_ptr_array_free(process_list->index_to_pixmap, TRUE);
479 g_free(process_list);
480 g_debug("processlist_destroy end");
483 static gboolean remove_hash_item(ResourceInfo *process_info,
484 HashedResourceData *hashed_process_data,
485 ProcessList *process_list)
489 iter = hashed_process_data->y_iter;
491 gtk_list_store_remove (process_list->list_store, &iter);
492 gdk_pixmap_unref(hashed_process_data->pixmap);
494 // TODO pmf: check this; might be needed
495 // if(likely(process_list->current_hash_data != NULL)) {
496 // if(likely(hashed_process_data ==
497 // process_list->current_hash_data[process_info->trace_num][process_info->cpu]))
498 // process_list->current_hash_data[process_info->trace_num][process_info->cpu] = NULL;
500 return TRUE; /* remove the element from the hash table */
503 void processlist_clear(ProcessList *process_list)
505 g_info("processlist_clear %p", process_list);
507 g_hash_table_foreach_remove(process_list->process_hash,
508 (GHRFunc)remove_hash_item,
509 (gpointer)process_list);
510 process_list->number_of_process = 0;
511 update_index_to_pixmap(process_list);
515 GtkWidget *processlist_get_widget(ProcessList *process_list)
517 return process_list->process_list_widget;
521 void destroy_hash_key(gpointer key)
526 void destroy_hash_data(gpointer data)
532 //void processlist_set_name(ProcessList *process_list,
534 // HashedResourceData *hashed_process_data)
536 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
537 // PROCESS_COLUMN, g_quark_to_string(name),
541 //void processlist_set_brand(ProcessList *process_list,
543 // HashedResourceData *hashed_process_data)
545 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
546 // BRAND_COLUMN, g_quark_to_string(brand),
550 //void processlist_set_tgid(ProcessList *process_list,
552 // HashedResourceData *hashed_process_data)
554 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
555 // TGID_COLUMN, tgid,
559 //void processlist_set_ppid(ProcessList *process_list,
561 // HashedResourceData *hashed_process_data)
563 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
564 // PPID_COLUMN, ppid,
568 int resourcelist_add( ProcessList *process_list,
575 ResourceInfo **pm_resource_info,
576 HashedResourceData **pm_hashed_resource_data)
578 ResourceInfo *Resource_Info = g_new(ResourceInfo, 1);
579 HashedResourceData *hashed_resource_data = g_new(HashedResourceData, 1);
580 *pm_hashed_resource_data = hashed_resource_data;
581 *pm_resource_info = Resource_Info;
583 Resource_Info->name = name;
585 // Process_Info->pid = pid;
586 // Process_Info->tgid = tgid;
588 // Process_Info->cpu = cpu;
590 // Process_Info->cpu = 0;
591 // Process_Info->ppid = ppid;
592 // Process_Info->birth = *birth;
593 Resource_Info->trace_num = trace_num;
594 Resource_Info->type = type;
595 Resource_Info->id = id;
597 /* When we create it from before state update, we are sure that the
598 * last event occured before the beginning of the global area.
600 * If it is created after state update, this value (0) will be
601 * overriden by the new state before anything is drawn.
603 * There are 3 potential lines for the each process: one in the middle,
604 * one under it and one over it. The {over,middle,under} fields tell us
605 * the x pixel on the pixmap where we are. The _used fields tell us
606 * whether that pixel was used. The _marked field tells us if we marked a
609 hashed_resource_data->x.over = 0;
610 hashed_resource_data->x.over_used = FALSE;
611 hashed_resource_data->x.over_marked = FALSE;
612 hashed_resource_data->x.middle = 0; // last
613 hashed_resource_data->x.middle_used = FALSE;
614 hashed_resource_data->x.middle_marked = FALSE;
615 hashed_resource_data->x.under = 0;
616 hashed_resource_data->x.under_used = FALSE;
617 hashed_resource_data->x.under_marked = FALSE;
618 hashed_resource_data->next_good_time = ltt_time_zero;
620 /* Add a new row to the model */
621 gtk_list_store_append ( process_list->list_store,
622 &hashed_resource_data->y_iter);
624 gtk_list_store_set ( process_list->list_store, &hashed_resource_data->y_iter,
625 NAME_COLUMN, g_quark_to_string(name),
628 g_hash_table_insert(process_list->process_hash,
629 (gpointer)Resource_Info,
630 (gpointer)hashed_resource_data);
632 process_list->number_of_process++; // of resources
634 hashed_resource_data->height = process_list->cell_height;
636 g_assert(hashed_resource_data->height != 0);
638 *height = hashed_resource_data->height * process_list->number_of_process;
640 hashed_resource_data->pixmap =
641 gdk_pixmap_new(drawing->drawing_area->window,
642 drawing->alloc_width,
643 hashed_resource_data->height,
646 // Clear the image with black background
647 gdk_draw_rectangle (hashed_resource_data->pixmap,
648 drawing->drawing_area->style->black_gc,
651 drawing->alloc_width,
652 hashed_resource_data->height);
654 update_index_to_pixmap(process_list);
658 //int processlist_add( ProcessList *process_list,
659 // Drawing_t *drawing,
669 // ResourceInfo **pm_process_info,
670 // HashedResourceData **pm_hashed_process_data)
672 // ResourceInfo *Process_Info = g_new(ResourceInfo, 1);
673 // HashedResourceData *hashed_process_data = g_new(HashedResourceData, 1);
674 // *pm_hashed_process_data = hashed_process_data;
675 // *pm_process_info = Process_Info;
677 // Process_Info->pid = pid;
678 // Process_Info->tgid = tgid;
680 // Process_Info->cpu = cpu;
682 // Process_Info->cpu = 0;
683 // Process_Info->ppid = ppid;
684 // Process_Info->birth = *birth;
685 // Process_Info->trace_num = trace_num;
687 // /* When we create it from before state update, we are sure that the
688 // * last event occured before the beginning of the global area.
690 // * If it is created after state update, this value (0) will be
691 // * overriden by the new state before anything is drawn.
693 // hashed_process_data->x.over = 0;
694 // hashed_process_data->x.over_used = FALSE;
695 // hashed_process_data->x.over_marked = FALSE;
696 // hashed_process_data->x.middle = 0;
697 // hashed_process_data->x.middle_used = FALSE;
698 // hashed_process_data->x.middle_marked = FALSE;
699 // hashed_process_data->x.under = 0;
700 // hashed_process_data->x.under_used = FALSE;
701 // hashed_process_data->x.under_marked = FALSE;
702 // hashed_process_data->next_good_time = ltt_time_zero;
704 // /* Add a new row to the model */
705 // gtk_list_store_append ( process_list->list_store,
706 // &hashed_process_data->y_iter);
708 // gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
709 // PROCESS_COLUMN, g_quark_to_string(name),
710 // BRAND_COLUMN, g_quark_to_string(brand),
712 // TGID_COLUMN, tgid,
713 // PPID_COLUMN, ppid,
715 // BIRTH_S_COLUMN, birth->tv_sec,
716 // BIRTH_NS_COLUMN, birth->tv_nsec,
717 // TRACE_COLUMN, trace_num,
719 // //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget),
720 // // GTK_TREE_MODEL(process_list->list_store));
721 // //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget));
723 // g_hash_table_insert(process_list->process_hash,
724 // (gpointer)Process_Info,
725 // (gpointer)hashed_process_data);
727 // process_list->number_of_process++;
729 // hashed_process_data->height = process_list->cell_height;
731 // g_assert(hashed_process_data->height != 0);
733 // *height = hashed_process_data->height * process_list->number_of_process;
735 // hashed_process_data->pixmap =
736 // gdk_pixmap_new(drawing->drawing_area->window,
737 // drawing->alloc_width,
738 // hashed_process_data->height,
741 // // Clear the image
742 // gdk_draw_rectangle (hashed_process_data->pixmap,
743 // drawing->drawing_area->style->black_gc,
746 // drawing->alloc_width,
747 // hashed_process_data->height);
749 // update_index_to_pixmap(process_list);
755 // TODO pmf: make this work once again
756 //int processlist_remove( ProcessList *process_list,
762 // ResourceInfo process_info;
763 // HashedResourceData *hashed_process_data;
766 // process_info.pid = pid;
768 // process_info.cpu = cpu;
770 // process_info.cpu = 0;
771 // process_info.birth = *birth;
772 // process_info.trace_num = trace_num;
775 // hashed_process_data =
776 // (HashedResourceData*)g_hash_table_lookup(
777 // process_list->process_hash,
779 // if(likely(hashed_process_data != NULL))
781 // iter = hashed_process_data->y_iter;
783 // gtk_list_store_remove (process_list->list_store, &iter);
785 // g_hash_table_remove(process_list->process_hash,
788 // if(likely(process_list->current_hash_data != NULL)) {
789 // if(likely(hashed_process_data == process_list->current_hash_data[trace_num][cpu])) {
790 // process_list->current_hash_data[trace_num][cpu] = NULL;
794 // gdk_pixmap_unref(hashed_process_data->pixmap);
796 // update_index_to_pixmap(process_list);
798 // process_list->number_of_process--;
808 static inline guint get_cpu_number_from_name(GQuark name)
814 string = g_quark_to_string(name);
816 begin = strrchr(string, '/');
819 g_assert(begin != '\0');
821 cpu = strtoul(begin, NULL, 10);