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