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