fix cell height
[lttv.git] / ltt / branches / poly / lttv / modules / gui / controlflow / processlist.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Mathieu Desnoyers
3 *
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;
7 *
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.
12 *
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,
16 * MA 02111-1307, USA.
17 */
18
19 #include <gtk/gtk.h>
20 #include <glib.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <math.h>
24
25 #include "processlist.h"
26 #include "drawing.h"
27 #include "drawitem.h"
28
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)
31
32 /* Preallocated Size of the index_to_pixmap array */
33 #define ALLOCATE_PROCESSES 1000
34
35 /*****************************************************************************
36 * Methods to synchronize process list *
37 *****************************************************************************/
38
39 //static inline guint get_cpu_number_from_name(GQuark name);
40
41 /* Enumeration of the columns */
42 enum
43 {
44 PROCESS_COLUMN,
45 PID_COLUMN,
46 PPID_COLUMN,
47 CPU_COLUMN,
48 BIRTH_S_COLUMN,
49 BIRTH_NS_COLUMN,
50 TRACE_COLUMN,
51 N_COLUMNS
52 };
53
54
55 gint process_sort_func ( GtkTreeModel *model,
56 GtkTreeIter *it_a,
57 GtkTreeIter *it_b,
58 gpointer user_data)
59 {
60 GValue a, b;
61
62 memset(&a, 0, sizeof(GValue));
63 memset(&b, 0, sizeof(GValue));
64
65 /* Order by PID */
66 gtk_tree_model_get_value( model,
67 it_a,
68 PID_COLUMN,
69 &a);
70
71 gtk_tree_model_get_value( model,
72 it_b,
73 PID_COLUMN,
74 &b);
75
76 if(G_VALUE_TYPE(&a) == G_TYPE_UINT
77 && G_VALUE_TYPE(&b) == G_TYPE_UINT )
78 {
79 {
80
81 if(g_value_get_uint(&a) == 0 && g_value_get_uint(&b) == 0) {
82
83 GValue cpua, cpub;
84
85 memset(&cpua, 0, sizeof(GValue));
86 memset(&cpub, 0, sizeof(GValue));
87
88 /* If 0, order by CPU */
89 gtk_tree_model_get_value( model,
90 it_a,
91 CPU_COLUMN,
92 &cpua);
93
94 gtk_tree_model_get_value( model,
95 it_b,
96 CPU_COLUMN,
97 &cpub);
98
99 if(G_VALUE_TYPE(&cpua) == G_TYPE_UINT
100 && G_VALUE_TYPE(&cpub) == G_TYPE_UINT )
101 {
102 if(g_value_get_uint(&cpua) > g_value_get_uint(&cpub))
103 {
104 g_value_unset(&cpua);
105 g_value_unset(&cpub);
106 return 1;
107 }
108 if(g_value_get_uint(&cpua) < g_value_get_uint(&cpub))
109 {
110 g_value_unset(&cpua);
111 g_value_unset(&cpub);
112 return 0;
113 }
114 }
115
116 g_value_unset(&cpua);
117 g_value_unset(&cpub);
118
119 } else { /* if not 0, order by pid */
120
121 if(g_value_get_uint(&a) > g_value_get_uint(&b))
122 {
123 g_value_unset(&a);
124 g_value_unset(&b);
125 return 1;
126 }
127 if(g_value_get_uint(&a) < g_value_get_uint(&b))
128 {
129 g_value_unset(&a);
130 g_value_unset(&b);
131 return 0;
132 }
133 }
134 }
135 }
136
137 g_value_unset(&a);
138 g_value_unset(&b);
139
140
141 /* Order by birth second */
142 gtk_tree_model_get_value( model,
143 it_a,
144 BIRTH_S_COLUMN,
145 &a);
146
147 gtk_tree_model_get_value( model,
148 it_b,
149 BIRTH_S_COLUMN,
150 &b);
151
152
153 if(G_VALUE_TYPE(&a) == G_TYPE_ULONG
154 && G_VALUE_TYPE(&b) == G_TYPE_ULONG )
155 {
156 if(g_value_get_ulong(&a) > g_value_get_ulong(&b))
157 {
158 g_value_unset(&a);
159 g_value_unset(&b);
160 return 1;
161 }
162 if(g_value_get_ulong(&a) < g_value_get_ulong(&b))
163 {
164 g_value_unset(&a);
165 g_value_unset(&b);
166 return 0;
167 }
168
169 }
170
171 g_value_unset(&a);
172 g_value_unset(&b);
173
174 /* Order by birth nanosecond */
175 gtk_tree_model_get_value( model,
176 it_a,
177 BIRTH_NS_COLUMN,
178 &a);
179
180 gtk_tree_model_get_value( model,
181 it_b,
182 BIRTH_NS_COLUMN,
183 &b);
184
185
186 if(G_VALUE_TYPE(&a) == G_TYPE_ULONG
187 && G_VALUE_TYPE(&b) == G_TYPE_ULONG )
188 {
189 if(g_value_get_ulong(&a) > g_value_get_ulong(&b))
190 {
191 g_value_unset(&a);
192 g_value_unset(&b);
193 return 1;
194 }
195 if(g_value_get_ulong(&a) < g_value_get_ulong(&b))
196 {
197 g_value_unset(&a);
198 g_value_unset(&b);
199 return 0;
200 }
201
202 }
203
204 g_value_unset(&a);
205 g_value_unset(&b);
206
207 /* Order by trace_num */
208 gtk_tree_model_get_value( model,
209 it_a,
210 TRACE_COLUMN,
211 &a);
212
213 gtk_tree_model_get_value( model,
214 it_b,
215 TRACE_COLUMN,
216 &b);
217
218 if(G_VALUE_TYPE(&a) == G_TYPE_ULONG
219 && G_VALUE_TYPE(&b) == G_TYPE_ULONG )
220 {
221 if(g_value_get_ulong(&a) > g_value_get_ulong(&b))
222 {
223 g_value_unset(&a);
224 g_value_unset(&b);
225 return 1;
226 }
227 if(g_value_get_ulong(&a) < g_value_get_ulong(&b))
228 {
229 g_value_unset(&a);
230 g_value_unset(&b);
231 return 0;
232 }
233
234 }
235
236 return 0;
237
238 }
239
240 static guint process_list_hash_fct(gconstpointer key)
241 {
242 guint pid = ((const ProcessInfo*)key)->pid;
243 return ((pid>>8 ^ pid>>4 ^ pid>>2 ^ pid) ^ ((const ProcessInfo*)key)->cpu);
244 }
245
246 /* If hash is good, should be different */
247 static gboolean process_list_equ_fct(gconstpointer a, gconstpointer b)
248 {
249 const ProcessInfo *pa = (const ProcessInfo*)a;
250 const ProcessInfo *pb = (const ProcessInfo*)b;
251
252 gboolean ret = TRUE;
253
254 if(likely(pa->pid != pb->pid))
255 ret = FALSE;
256 else if(likely((pa->pid == 0 && (pa->cpu != pb->cpu))))
257 ret = FALSE;
258 else if(unlikely(ltt_time_compare(pa->birth, pb->birth) != 0))
259 ret = FALSE;
260 else if(unlikely(pa->trace_num != pb->trace_num))
261 ret = FALSE;
262
263 return ret;
264 }
265
266 void destroy_hash_key(gpointer key);
267
268 void destroy_hash_data(gpointer data);
269
270
271 static void update_index_to_pixmap_each(ProcessInfo *key,
272 HashedProcessData *value,
273 ProcessList *process_list)
274 {
275 guint array_index = processlist_get_index_from_data(process_list, value);
276
277 g_assert(array_index < process_list->index_to_pixmap->len);
278
279 GdkPixmap **pixmap =
280 (GdkPixmap**)&g_ptr_array_index(process_list->index_to_pixmap, array_index);
281
282 *pixmap = value->pixmap;
283 }
284
285
286 static void update_index_to_pixmap(ProcessList *process_list)
287 {
288 g_ptr_array_set_size(process_list->index_to_pixmap,
289 g_hash_table_size(process_list->process_hash));
290 g_hash_table_foreach(process_list->process_hash,
291 (GHFunc)update_index_to_pixmap_each,
292 process_list);
293 }
294
295
296 static void update_pixmap_size_each(ProcessInfo *key,
297 HashedProcessData *value,
298 guint width)
299 {
300 GdkPixmap *old_pixmap = value->pixmap;
301
302 value->pixmap =
303 gdk_pixmap_new(old_pixmap,
304 width,
305 value->height,
306 -1);
307
308 gdk_pixmap_unref(old_pixmap);
309 }
310
311
312 void update_pixmap_size(ProcessList *process_list, guint width)
313 {
314 g_hash_table_foreach(process_list->process_hash,
315 (GHFunc)update_pixmap_size_each,
316 (gpointer)width);
317 }
318
319
320 typedef struct _CopyPixmap {
321 GdkDrawable *dest;
322 GdkGC *gc;
323 GdkDrawable *src;
324 gint xsrc, ysrc, xdest, ydest, width, height;
325 } CopyPixmap;
326
327 static void copy_pixmap_region_each(ProcessInfo *key,
328 HashedProcessData *value,
329 CopyPixmap *cp)
330 {
331 GdkPixmap *src = cp->src;
332 GdkPixmap *dest = cp->dest;
333
334 if(dest == NULL)
335 dest = value->pixmap;
336 if(src == NULL)
337 src = value->pixmap;
338
339 gdk_draw_drawable (dest,
340 cp->gc,
341 src,
342 cp->xsrc, cp->ysrc,
343 cp->xdest, cp->ydest,
344 cp->width, cp->height);
345 }
346
347
348
349
350 void copy_pixmap_region(ProcessList *process_list, GdkDrawable *dest,
351 GdkGC *gc, GdkDrawable *src,
352 gint xsrc, gint ysrc,
353 gint xdest, gint ydest, gint width, gint height)
354 {
355 CopyPixmap cp = { dest, gc, src, xsrc, ysrc, xdest, ydest, width, height };
356
357 g_hash_table_foreach(process_list->process_hash,
358 (GHFunc)copy_pixmap_region_each,
359 &cp);
360 }
361
362
363
364 typedef struct _RectanglePixmap {
365 gboolean filled;
366 gint x, y, width, height;
367 GdkGC *gc;
368 } RectanglePixmap;
369
370 static void rectangle_pixmap_each(ProcessInfo *key,
371 HashedProcessData *value,
372 RectanglePixmap *rp)
373 {
374 if(rp->height == -1)
375 rp->height = value->height;
376
377 gdk_draw_rectangle (value->pixmap,
378 rp->gc,
379 rp->filled,
380 rp->x, rp->y,
381 rp->width, rp->height);
382 }
383
384
385
386
387 void rectangle_pixmap(ProcessList *process_list, GdkGC *gc,
388 gboolean filled, gint x, gint y, gint width, gint height)
389 {
390 RectanglePixmap rp = { filled, x, y, width, height, gc };
391
392 g_hash_table_foreach(process_list->process_hash,
393 (GHFunc)rectangle_pixmap_each,
394 &rp);
395 }
396
397
398 /* Renders each pixmaps into on big drawable */
399 void copy_pixmap_to_screen(ProcessList *process_list,
400 GdkDrawable *dest,
401 GdkGC *gc,
402 gint x, gint y,
403 gint width, gint height)
404 {
405 if(process_list->index_to_pixmap->len == 0) return;
406 guint cell_height = process_list->cell_height;
407
408 //cell_height = 24; //FIXME
409 /* Get indexes */
410 gint begin = floor(y/(double)cell_height);
411 gint end = MIN(ceil((y+height)/(double)cell_height),
412 process_list->index_to_pixmap->len);
413 gint i;
414
415 for(i=begin; i<end; i++) {
416 g_assert(i<process_list->index_to_pixmap->len);
417 /* Render the pixmap to the screen */
418 GdkPixmap *pixmap =
419 (GdkPixmap*)g_ptr_array_index(process_list->index_to_pixmap, i);
420
421 gdk_draw_drawable (dest,
422 gc,
423 pixmap,
424 x, 0,
425 x, i*cell_height,
426 width, cell_height);
427
428 }
429
430
431 }
432
433
434
435
436
437
438
439
440
441 ProcessList *processlist_construct(void)
442 {
443 GtkTreeViewColumn *column;
444 GtkCellRenderer *renderer;
445
446 ProcessList* process_list = g_new(ProcessList,1);
447
448 process_list->number_of_process = 0;
449
450 process_list->current_hash_data = NULL;
451
452 /* Create the Process list */
453 process_list->list_store = gtk_list_store_new ( N_COLUMNS,
454 G_TYPE_STRING,
455 G_TYPE_UINT,
456 G_TYPE_UINT,
457 G_TYPE_UINT,
458 G_TYPE_ULONG,
459 G_TYPE_ULONG,
460 G_TYPE_ULONG);
461
462
463 process_list->process_list_widget =
464 gtk_tree_view_new_with_model
465 (GTK_TREE_MODEL (process_list->list_store));
466 g_object_unref (G_OBJECT (process_list->list_store));
467
468 gtk_tree_sortable_set_sort_func(
469 GTK_TREE_SORTABLE(process_list->list_store),
470 PID_COLUMN,
471 process_sort_func,
472 NULL,
473 NULL);
474
475 gtk_tree_sortable_set_sort_column_id(
476 GTK_TREE_SORTABLE(process_list->list_store),
477 PID_COLUMN,
478 GTK_SORT_ASCENDING);
479
480 process_list->process_hash = g_hash_table_new_full(
481 process_list_hash_fct, process_list_equ_fct,
482 destroy_hash_key, destroy_hash_data
483 );
484
485
486 gtk_tree_view_set_headers_visible(
487 GTK_TREE_VIEW(process_list->process_list_widget), TRUE);
488
489 /* Create a column, associating the "text" attribute of the
490 * cell_renderer to the first column of the model */
491 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
492 renderer = gtk_cell_renderer_text_new ();
493 process_list->renderer = renderer;
494
495 gtk_cell_renderer_get_size(renderer,
496 GTK_WIDGET(process_list->process_list_widget),
497 NULL,
498 NULL,
499 NULL,
500 NULL,
501 &process_list->cell_height);
502
503 column = gtk_tree_view_column_new_with_attributes ( "Process",
504 renderer,
505 "text",
506 PROCESS_COLUMN,
507 NULL);
508 gtk_tree_view_column_set_alignment (column, 0.0);
509 gtk_tree_view_column_set_fixed_width (column, 45);
510 gtk_tree_view_append_column (
511 GTK_TREE_VIEW (process_list->process_list_widget), column);
512
513 process_list->button = column->button;
514
515 column = gtk_tree_view_column_new_with_attributes ( "PID",
516 renderer,
517 "text",
518 PID_COLUMN,
519 NULL);
520 gtk_tree_view_append_column (
521 GTK_TREE_VIEW (process_list->process_list_widget), column);
522
523 column = gtk_tree_view_column_new_with_attributes ( "PPID",
524 renderer,
525 "text",
526 PPID_COLUMN,
527 NULL);
528 gtk_tree_view_append_column (
529 GTK_TREE_VIEW (process_list->process_list_widget), column);
530
531 column = gtk_tree_view_column_new_with_attributes ( "CPU",
532 renderer,
533 "text",
534 CPU_COLUMN,
535 NULL);
536 gtk_tree_view_append_column (
537 GTK_TREE_VIEW (process_list->process_list_widget), column);
538
539 column = gtk_tree_view_column_new_with_attributes ( "Birth sec",
540 renderer,
541 "text",
542 BIRTH_S_COLUMN,
543 NULL);
544 gtk_tree_view_append_column (
545 GTK_TREE_VIEW (process_list->process_list_widget), column);
546
547 //gtk_tree_view_column_set_visible(column, 0);
548 //
549 column = gtk_tree_view_column_new_with_attributes ( "Birth nsec",
550 renderer,
551 "text",
552 BIRTH_NS_COLUMN,
553 NULL);
554 gtk_tree_view_append_column (
555 GTK_TREE_VIEW (process_list->process_list_widget), column);
556
557 column = gtk_tree_view_column_new_with_attributes ( "TRACE",
558 renderer,
559 "text",
560 TRACE_COLUMN,
561 NULL);
562 gtk_tree_view_append_column (
563 GTK_TREE_VIEW (process_list->process_list_widget), column);
564
565
566 //gtk_tree_view_column_set_visible(column, 0);
567
568 g_object_set_data_full(
569 G_OBJECT(process_list->process_list_widget),
570 "process_list_Data",
571 process_list,
572 (GDestroyNotify)processlist_destroy);
573
574 process_list->index_to_pixmap = g_ptr_array_sized_new(ALLOCATE_PROCESSES);
575
576 return process_list;
577 }
578
579 void processlist_destroy(ProcessList *process_list)
580 {
581 g_debug("processlist_destroy %p", process_list);
582 g_hash_table_destroy(process_list->process_hash);
583 process_list->process_hash = NULL;
584 g_ptr_array_free(process_list->index_to_pixmap, TRUE);
585
586 g_free(process_list);
587 g_debug("processlist_destroy end");
588 }
589
590 static gboolean remove_hash_item(ProcessInfo *process_info,
591 HashedProcessData *hashed_process_data,
592 ProcessList *process_list)
593 {
594 GtkTreeIter iter;
595
596 iter = hashed_process_data->y_iter;
597
598 gtk_list_store_remove (process_list->list_store, &iter);
599 gdk_pixmap_unref(hashed_process_data->pixmap);
600
601 if(likely(process_list->current_hash_data != NULL)) {
602 if(likely(hashed_process_data ==
603 process_list->current_hash_data[process_info->cpu]))
604 process_list->current_hash_data[process_info->cpu] = NULL;
605 }
606 return TRUE; /* remove the element from the hash table */
607 }
608
609 void processlist_clear(ProcessList *process_list)
610 {
611 g_info("processlist_clear %p", process_list);
612
613 g_hash_table_foreach_remove(process_list->process_hash,
614 (GHRFunc)remove_hash_item,
615 (gpointer)process_list);
616 process_list->number_of_process = 0;
617 update_index_to_pixmap(process_list);
618 }
619
620
621 GtkWidget *processlist_get_widget(ProcessList *process_list)
622 {
623 return process_list->process_list_widget;
624 }
625
626
627 void destroy_hash_key(gpointer key)
628 {
629 g_free(key);
630 }
631
632 void destroy_hash_data(gpointer data)
633 {
634 g_free(data);
635 }
636
637 int processlist_add( ProcessList *process_list,
638 Drawing_t *drawing,
639 guint pid,
640 guint cpu,
641 guint ppid,
642 LttTime *birth,
643 guint trace_num,
644 const gchar *name,
645 guint *height,
646 ProcessInfo **pm_process_info,
647 HashedProcessData **pm_hashed_process_data)
648 {
649 ProcessInfo *Process_Info = g_new(ProcessInfo, 1);
650 HashedProcessData *hashed_process_data = g_new(HashedProcessData, 1);
651 *pm_hashed_process_data = hashed_process_data;
652 *pm_process_info = Process_Info;
653
654 Process_Info->pid = pid;
655 if(pid == 0)
656 Process_Info->cpu = cpu;
657 else
658 Process_Info->cpu = 0;
659 Process_Info->ppid = ppid;
660 Process_Info->birth = *birth;
661 Process_Info->trace_num = trace_num;
662
663 /* When we create it from before state update, we are sure that the
664 * last event occured before the beginning of the global area.
665 *
666 * If it is created after state update, this value (0) will be
667 * overriden by the new state before anything is drawn.
668 */
669 hashed_process_data->x.over = 0;
670 hashed_process_data->x.over_used = FALSE;
671 hashed_process_data->x.over_marked = FALSE;
672 hashed_process_data->x.middle = 0;
673 hashed_process_data->x.middle_used = FALSE;
674 hashed_process_data->x.middle_marked = FALSE;
675 hashed_process_data->x.under = 0;
676 hashed_process_data->x.under_used = FALSE;
677 hashed_process_data->x.under_marked = FALSE;
678 hashed_process_data->next_good_time = ltt_time_zero;
679
680 /* Add a new row to the model */
681 gtk_list_store_append ( process_list->list_store,
682 &hashed_process_data->y_iter);
683
684 gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
685 PROCESS_COLUMN, name,
686 PID_COLUMN, pid,
687 PPID_COLUMN, ppid,
688 CPU_COLUMN, cpu,
689 BIRTH_S_COLUMN, birth->tv_sec,
690 BIRTH_NS_COLUMN, birth->tv_nsec,
691 TRACE_COLUMN, trace_num,
692 -1);
693 //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget),
694 // GTK_TREE_MODEL(process_list->list_store));
695 //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget));
696
697 g_hash_table_insert(process_list->process_hash,
698 (gpointer)Process_Info,
699 (gpointer)hashed_process_data);
700
701 process_list->number_of_process++;
702
703 hashed_process_data->height = process_list->cell_height;
704
705 //hashed_process_data->height = 24; // FIXME
706 g_assert(hashed_process_data->height != 0);
707
708 *height = hashed_process_data->height * process_list->number_of_process;
709
710 hashed_process_data->pixmap =
711 gdk_pixmap_new(drawing->drawing_area->window,
712 drawing->alloc_width,
713 hashed_process_data->height,
714 -1);
715
716 // Clear the image
717 gdk_draw_rectangle (hashed_process_data->pixmap,
718 drawing->drawing_area->style->black_gc,
719 TRUE,
720 0, 0,
721 drawing->alloc_width,
722 hashed_process_data->height);
723
724 update_index_to_pixmap(process_list);
725
726
727 return 0;
728 }
729
730 int processlist_remove( ProcessList *process_list,
731 guint pid,
732 guint cpu,
733 LttTime *birth,
734 guint trace_num)
735 {
736 ProcessInfo process_info;
737 HashedProcessData *hashed_process_data;
738 GtkTreeIter iter;
739
740 process_info.pid = pid;
741 if(pid == 0)
742 process_info.cpu = cpu;
743 else
744 process_info.cpu = 0;
745 process_info.birth = *birth;
746 process_info.trace_num = trace_num;
747
748
749 hashed_process_data =
750 (HashedProcessData*)g_hash_table_lookup(
751 process_list->process_hash,
752 &process_info);
753 if(likely(hashed_process_data != NULL))
754 {
755 iter = hashed_process_data->y_iter;
756
757 gtk_list_store_remove (process_list->list_store, &iter);
758
759 g_hash_table_remove(process_list->process_hash,
760 &process_info);
761
762 if(likely(process_list->current_hash_data != NULL)) {
763 if(likely(hashed_process_data == process_list->current_hash_data[cpu])) {
764 process_list->current_hash_data[cpu] = NULL;
765 }
766 }
767
768 gdk_pixmap_unref(hashed_process_data->pixmap);
769
770 update_index_to_pixmap(process_list);
771
772 process_list->number_of_process--;
773
774 return 0;
775 } else {
776 return 1;
777 }
778 }
779
780
781 #if 0
782 static inline guint get_cpu_number_from_name(GQuark name)
783 {
784 const gchar *string;
785 char *begin;
786 guint cpu;
787
788 string = g_quark_to_string(name);
789
790 begin = strrchr(string, '/');
791 begin++;
792
793 g_assert(begin != '\0');
794
795 cpu = strtoul(begin, NULL, 10);
796
797 return cpu;
798 }
799 #endif //0
This page took 0.044978 seconds and 5 git commands to generate.