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