correctly add -lm for math lib
[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 gint cell_height =
406 get_cell_height(process_list,
407 (GtkTreeView*)process_list->process_list_widget);
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 g_warning("begin : %i, end : %i", begin,end);
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 column = gtk_tree_view_column_new_with_attributes ( "Process",
494 renderer,
495 "text",
496 PROCESS_COLUMN,
497 NULL);
498 gtk_tree_view_column_set_alignment (column, 0.0);
499 gtk_tree_view_column_set_fixed_width (column, 45);
500 gtk_tree_view_append_column (
501 GTK_TREE_VIEW (process_list->process_list_widget), column);
502
503 process_list->button = column->button;
504
505 column = gtk_tree_view_column_new_with_attributes ( "PID",
506 renderer,
507 "text",
508 PID_COLUMN,
509 NULL);
510 gtk_tree_view_append_column (
511 GTK_TREE_VIEW (process_list->process_list_widget), column);
512
513 column = gtk_tree_view_column_new_with_attributes ( "PPID",
514 renderer,
515 "text",
516 PPID_COLUMN,
517 NULL);
518 gtk_tree_view_append_column (
519 GTK_TREE_VIEW (process_list->process_list_widget), column);
520
521 column = gtk_tree_view_column_new_with_attributes ( "CPU",
522 renderer,
523 "text",
524 CPU_COLUMN,
525 NULL);
526 gtk_tree_view_append_column (
527 GTK_TREE_VIEW (process_list->process_list_widget), column);
528
529 column = gtk_tree_view_column_new_with_attributes ( "Birth sec",
530 renderer,
531 "text",
532 BIRTH_S_COLUMN,
533 NULL);
534 gtk_tree_view_append_column (
535 GTK_TREE_VIEW (process_list->process_list_widget), column);
536
537 //gtk_tree_view_column_set_visible(column, 0);
538 //
539 column = gtk_tree_view_column_new_with_attributes ( "Birth nsec",
540 renderer,
541 "text",
542 BIRTH_NS_COLUMN,
543 NULL);
544 gtk_tree_view_append_column (
545 GTK_TREE_VIEW (process_list->process_list_widget), column);
546
547 column = gtk_tree_view_column_new_with_attributes ( "TRACE",
548 renderer,
549 "text",
550 TRACE_COLUMN,
551 NULL);
552 gtk_tree_view_append_column (
553 GTK_TREE_VIEW (process_list->process_list_widget), column);
554
555
556 //gtk_tree_view_column_set_visible(column, 0);
557
558 g_object_set_data_full(
559 G_OBJECT(process_list->process_list_widget),
560 "process_list_Data",
561 process_list,
562 (GDestroyNotify)processlist_destroy);
563
564 process_list->index_to_pixmap = g_ptr_array_sized_new(ALLOCATE_PROCESSES);
565
566 return process_list;
567 }
568
569 void processlist_destroy(ProcessList *process_list)
570 {
571 g_debug("processlist_destroy %p", process_list);
572 g_hash_table_destroy(process_list->process_hash);
573 process_list->process_hash = NULL;
574 g_ptr_array_free(process_list->index_to_pixmap, TRUE);
575
576 g_free(process_list);
577 g_debug("processlist_destroy end");
578 }
579
580 static gboolean remove_hash_item(ProcessInfo *process_info,
581 HashedProcessData *hashed_process_data,
582 ProcessList *process_list)
583 {
584 GtkTreeIter iter;
585
586 iter = hashed_process_data->y_iter;
587
588 gtk_list_store_remove (process_list->list_store, &iter);
589 gdk_pixmap_unref(hashed_process_data->pixmap);
590
591 if(likely(process_list->current_hash_data != NULL)) {
592 if(likely(hashed_process_data ==
593 process_list->current_hash_data[process_info->cpu]))
594 process_list->current_hash_data[process_info->cpu] = NULL;
595 }
596 return TRUE; /* remove the element from the hash table */
597 }
598
599 void processlist_clear(ProcessList *process_list)
600 {
601 g_info("processlist_clear %p", process_list);
602
603 g_hash_table_foreach_remove(process_list->process_hash,
604 (GHRFunc)remove_hash_item,
605 (gpointer)process_list);
606 process_list->number_of_process = 0;
607 update_index_to_pixmap(process_list);
608 }
609
610
611 GtkWidget *processlist_get_widget(ProcessList *process_list)
612 {
613 return process_list->process_list_widget;
614 }
615
616
617 void destroy_hash_key(gpointer key)
618 {
619 g_free(key);
620 }
621
622 void destroy_hash_data(gpointer data)
623 {
624 g_free(data);
625 }
626
627 int processlist_add( ProcessList *process_list,
628 Drawing_t *drawing,
629 guint pid,
630 guint cpu,
631 guint ppid,
632 LttTime *birth,
633 guint trace_num,
634 const gchar *name,
635 guint *height,
636 ProcessInfo **pm_process_info,
637 HashedProcessData **pm_hashed_process_data)
638 {
639 ProcessInfo *Process_Info = g_new(ProcessInfo, 1);
640 HashedProcessData *hashed_process_data = g_new(HashedProcessData, 1);
641 *pm_hashed_process_data = hashed_process_data;
642 *pm_process_info = Process_Info;
643
644 Process_Info->pid = pid;
645 if(pid == 0)
646 Process_Info->cpu = cpu;
647 else
648 Process_Info->cpu = 0;
649 Process_Info->ppid = ppid;
650 Process_Info->birth = *birth;
651 Process_Info->trace_num = trace_num;
652
653 /* When we create it from before state update, we are sure that the
654 * last event occured before the beginning of the global area.
655 *
656 * If it is created after state update, this value (0) will be
657 * overriden by the new state before anything is drawn.
658 */
659 hashed_process_data->x.over = 0;
660 hashed_process_data->x.over_used = FALSE;
661 hashed_process_data->x.over_marked = FALSE;
662 hashed_process_data->x.middle = 0;
663 hashed_process_data->x.middle_used = FALSE;
664 hashed_process_data->x.middle_marked = FALSE;
665 hashed_process_data->x.under = 0;
666 hashed_process_data->x.under_used = FALSE;
667 hashed_process_data->x.under_marked = FALSE;
668 hashed_process_data->next_good_time = ltt_time_zero;
669
670 /* Add a new row to the model */
671 gtk_list_store_append ( process_list->list_store,
672 &hashed_process_data->y_iter);
673
674 gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
675 PROCESS_COLUMN, name,
676 PID_COLUMN, pid,
677 PPID_COLUMN, ppid,
678 CPU_COLUMN, cpu,
679 BIRTH_S_COLUMN, birth->tv_sec,
680 BIRTH_NS_COLUMN, birth->tv_nsec,
681 TRACE_COLUMN, trace_num,
682 -1);
683
684 g_hash_table_insert(process_list->process_hash,
685 (gpointer)Process_Info,
686 (gpointer)hashed_process_data);
687
688 process_list->number_of_process++;
689
690 hashed_process_data->height = get_cell_height(process_list,
691 (GtkTreeView*)process_list->process_list_widget);
692 hashed_process_data->height = 24; // FIXME
693 g_assert(hashed_process_data->height != 0);
694
695 *height = hashed_process_data->height * process_list->number_of_process;
696
697 hashed_process_data->pixmap =
698 gdk_pixmap_new(drawing->drawing_area->window,
699 drawing->alloc_width,
700 hashed_process_data->height,
701 -1);
702
703 // Clear the image
704 gdk_draw_rectangle (hashed_process_data->pixmap,
705 drawing->drawing_area->style->black_gc,
706 TRUE,
707 0, 0,
708 drawing->alloc_width,
709 hashed_process_data->height);
710
711 update_index_to_pixmap(process_list);
712
713
714 return 0;
715 }
716
717 int processlist_remove( ProcessList *process_list,
718 guint pid,
719 guint cpu,
720 LttTime *birth,
721 guint trace_num)
722 {
723 ProcessInfo process_info;
724 HashedProcessData *hashed_process_data;
725 GtkTreeIter iter;
726
727 process_info.pid = pid;
728 if(pid == 0)
729 process_info.cpu = cpu;
730 else
731 process_info.cpu = 0;
732 process_info.birth = *birth;
733 process_info.trace_num = trace_num;
734
735
736 hashed_process_data =
737 (HashedProcessData*)g_hash_table_lookup(
738 process_list->process_hash,
739 &process_info);
740 if(likely(hashed_process_data != NULL))
741 {
742 iter = hashed_process_data->y_iter;
743
744 gtk_list_store_remove (process_list->list_store, &iter);
745
746 g_hash_table_remove(process_list->process_hash,
747 &process_info);
748
749 if(likely(process_list->current_hash_data != NULL)) {
750 if(likely(hashed_process_data == process_list->current_hash_data[cpu])) {
751 process_list->current_hash_data[cpu] = NULL;
752 }
753 }
754
755 gdk_pixmap_unref(hashed_process_data->pixmap);
756
757 update_index_to_pixmap(process_list);
758
759 process_list->number_of_process--;
760
761 return 0;
762 } else {
763 return 1;
764 }
765 }
766
767
768 #if 0
769 static inline guint get_cpu_number_from_name(GQuark name)
770 {
771 const gchar *string;
772 char *begin;
773 guint cpu;
774
775 string = g_quark_to_string(name);
776
777 begin = strrchr(string, '/');
778 begin++;
779
780 g_assert(begin != '\0');
781
782 cpu = strtoul(begin, NULL, 10);
783
784 return cpu;
785 }
786 #endif //0
This page took 0.049737 seconds and 4 git commands to generate.