1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 2000
7 * ---------------------------------------------------------------------------*/
9 #ifdef RTS_GTK_FRONTPANEL
11 /* Alas, not Posix. */
12 /* #include "PosixSource.h" */
17 #include "FrontPanel.h"
26 #include "VisSupport.h"
27 #include "VisWindow.h"
29 static GtkWidget *window, *map_drawing_area, *gen_drawing_area;
30 static GtkWidget *res_drawing_area;
31 static GtkWidget *continue_but, *stop_but, *quit_but;
32 static GtkWidget *statusbar;
33 static GtkWidget *live_label, *allocated_label;
34 static GtkWidget *footprint_label, *alloc_rate_label;
35 static GtkWidget *map_ruler, *gen_ruler;
36 static GtkWidget *res_vruler, *res_hruler;
37 static GtkWidget *running_label, *b_read_label, *b_write_label, *total_label;
38 static GtkWidget *b_mvar_label, *b_bh_label, *b_throwto_label, *sleeping_label;
40 static guint status_context_id;
42 gboolean continue_now = FALSE, stop_now = FALSE, quit = FALSE;
43 UpdateMode update_mode = Continuous;
45 static GdkPixmap *map_pixmap = NULL;
46 static GdkPixmap *gen_pixmap = NULL;
47 static GdkPixmap *res_pixmap = NULL;
52 bdescr_color = { 0, 0xffff, 0, 0 }, /* red */
53 free_color = { 0, 0, 0, 0xffff }, /* blue */
54 gen_colors[N_GENS] = {
69 static void *mem_start = (void *) 0x50000000;
71 static void colorBlock( void *addr, GdkColor *color,
72 nat block_width, nat block_height,
73 nat blocks_per_line );
75 static void residencyCensus( void );
76 static void updateResidencyGraph( void );
77 static void updateThreadsPanel( void );
79 /* Some code pinched from examples/scribble-simple in the GTK+
83 /* Create a new backing pixmap of the appropriate size */
85 configure_event( GtkWidget *widget, GdkEventConfigure *event STG_UNUSED,
89 gdk_pixmap_unref(*pixmap);
91 *pixmap = gdk_pixmap_new(widget->window,
92 widget->allocation.width,
93 widget->allocation.height,
96 gdk_draw_rectangle (*pixmap,
97 widget->style->white_gc,
100 widget->allocation.width,
101 widget->allocation.height);
103 debugBelch("configure!\n");
108 /* Redraw the screen from the backing pixmap */
110 expose_event( GtkWidget *widget, GdkEventExpose *event, GdkPixmap **pixmap )
112 gdk_draw_pixmap(widget->window,
113 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
115 event->area.x, event->area.y,
116 event->area.x, event->area.y,
117 event->area.width, event->area.height);
123 initFrontPanel( void )
125 GdkColormap *colormap;
128 gtk_init( &prog_argc, &prog_argv );
130 window = create_GHC_Front_Panel();
131 map_drawing_area = lookup_widget(window, "memmap");
132 gen_drawing_area = lookup_widget(window, "generations");
133 res_drawing_area = lookup_widget(window, "res_drawingarea");
134 stop_but = lookup_widget(window, "stop_but");
135 continue_but = lookup_widget(window, "continue_but");
136 quit_but = lookup_widget(window, "quit_but");
137 statusbar = lookup_widget(window, "statusbar");
138 live_label = lookup_widget(window, "live_label");
139 footprint_label = lookup_widget(window, "footprint_label");
140 allocated_label = lookup_widget(window, "allocated_label");
141 alloc_rate_label = lookup_widget(window, "alloc_rate_label");
142 gen_hbox = lookup_widget(window, "gen_hbox");
143 gen_ruler = lookup_widget(window, "gen_ruler");
144 map_ruler = lookup_widget(window, "map_ruler");
145 res_vruler = lookup_widget(window, "res_vruler");
146 res_hruler = lookup_widget(window, "res_hruler");
147 running_label = lookup_widget(window, "running_label");
148 b_read_label = lookup_widget(window, "blockread_label");
149 b_write_label = lookup_widget(window, "blockwrite_label");
150 b_mvar_label = lookup_widget(window, "blockmvar_label");
151 b_bh_label = lookup_widget(window, "blockbh_label");
152 b_throwto_label = lookup_widget(window, "blockthrowto_label");
153 sleeping_label = lookup_widget(window, "sleeping_label");
154 total_label = lookup_widget(window, "total_label");
157 gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "context" );
159 /* hook up some signals for the mem map drawing area */
160 gtk_signal_connect (GTK_OBJECT(map_drawing_area), "expose_event",
161 (GtkSignalFunc)expose_event, &map_pixmap);
162 gtk_signal_connect (GTK_OBJECT(map_drawing_area), "configure_event",
163 (GtkSignalFunc)configure_event, &map_pixmap);
165 gtk_widget_set_events(map_drawing_area, GDK_EXPOSURE_MASK);
167 /* hook up some signals for the gen drawing area */
168 gtk_signal_connect (GTK_OBJECT(gen_drawing_area), "expose_event",
169 (GtkSignalFunc)expose_event, &gen_pixmap);
170 gtk_signal_connect (GTK_OBJECT(gen_drawing_area), "configure_event",
171 (GtkSignalFunc)configure_event, &gen_pixmap);
173 gtk_widget_set_events(gen_drawing_area, GDK_EXPOSURE_MASK);
175 /* hook up some signals for the res drawing area */
176 gtk_signal_connect (GTK_OBJECT(res_drawing_area), "expose_event",
177 (GtkSignalFunc)expose_event, &res_pixmap);
178 gtk_signal_connect (GTK_OBJECT(res_drawing_area), "configure_event",
179 (GtkSignalFunc)configure_event, &res_pixmap);
181 gtk_widget_set_events(res_drawing_area, GDK_EXPOSURE_MASK);
183 /* allocate our colors */
184 colormap = gdk_colormap_get_system();
185 gdk_colormap_alloc_color(colormap, &bdescr_color, TRUE, TRUE);
186 gdk_colormap_alloc_color(colormap, &free_color, TRUE, TRUE);
189 gboolean success[N_GENS];
190 gdk_colormap_alloc_colors(colormap, gen_colors, N_GENS, TRUE,
192 if (!success) { barf("can't allocate colors"); }
195 /* set the labels on the generation histogram */
201 for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
202 for(s = 0; s < generations[g].n_steps; s++) {
203 g_snprintf( buf, 64, "%d.%d", g, s );
204 label = gtk_label_new( buf );
205 gtk_box_pack_start( GTK_BOX(gen_hbox), label,
207 gtk_widget_show(label);
212 gtk_widget_show(window);
214 /* wait for the user to press "Continue" before getting going... */
215 gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id,
217 gtk_widget_set_sensitive( stop_but, FALSE );
218 continue_now = FALSE;
219 while (continue_now == FALSE) {
220 gtk_main_iteration();
222 gtk_statusbar_pop( GTK_STATUSBAR(statusbar), status_context_id );
223 gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id,
226 gtk_widget_set_sensitive( continue_but, FALSE );
227 gtk_widget_set_sensitive( stop_but, TRUE );
228 gtk_widget_set_sensitive( quit_but, FALSE );
230 while (gtk_events_pending()) {
231 gtk_main_iteration();
236 stopFrontPanel( void )
238 gtk_widget_set_sensitive( quit_but, TRUE );
239 gtk_widget_set_sensitive( continue_but, FALSE );
240 gtk_widget_set_sensitive( stop_but, FALSE );
244 gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id,
248 while (quit == FALSE) {
249 gtk_main_iteration();
254 waitForContinue( void )
256 gtk_widget_set_sensitive( continue_but, TRUE );
257 gtk_widget_set_sensitive( stop_but, FALSE );
259 continue_now = FALSE;
260 while (continue_now == FALSE) {
261 gtk_main_iteration();
263 gtk_widget_set_sensitive( continue_but, FALSE );
264 gtk_widget_set_sensitive( stop_but, TRUE );
268 updateFrontPanelBeforeGC( nat N )
274 if (update_mode == BeforeGC
275 || update_mode == BeforeAfterGC
276 || stop_now == TRUE) {
277 g_snprintf( buf, 1000, "Stopped (before GC, generation %d)", N );
278 gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id, buf );
280 gtk_statusbar_pop( GTK_STATUSBAR(statusbar), status_context_id );
283 g_snprintf( buf, 1000, "Garbage collecting (generation %d)", N );
284 gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id, buf);
286 while (gtk_events_pending()) {
287 gtk_main_iteration();
292 numLabel( GtkWidget *lbl, nat n )
295 g_snprintf(buf, 64, "%d", n);
296 gtk_label_set_text( GTK_LABEL(lbl), buf );
300 updateFrontPanelAfterGC( nat N, lnat live )
304 gtk_statusbar_pop( GTK_STATUSBAR(statusbar), status_context_id );
307 if (N == RtsFlags.GcFlags.generations-1) {
313 if (update_mode == AfterGC
314 || update_mode == BeforeAfterGC
315 || stop_now == TRUE) {
316 snprintf( buf, 1000, "Stopped (after GC, generation %d)", N );
317 gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id, buf );
319 gtk_statusbar_pop( GTK_STATUSBAR(statusbar), status_context_id );
323 double words_to_megs = (1024 * 1024) / sizeof(W_);
324 double time = mut_user_time();
326 snprintf( buf, 1000, "%.2f", (double)live / words_to_megs );
327 gtk_label_set_text( GTK_LABEL(live_label), buf );
329 snprintf( buf, 1000, "%.2f", (double)total_allocated / words_to_megs );
330 gtk_label_set_text( GTK_LABEL(allocated_label), buf );
332 snprintf( buf, 1000, "%.2f",
333 (double)(mblocks_allocated * MBLOCK_SIZE_W) / words_to_megs );
334 gtk_label_set_text( GTK_LABEL(footprint_label), buf );
337 snprintf( buf, 1000, "%.2f", time );
339 snprintf( buf, 1000, "%.2f",
340 (double)(total_allocated / words_to_megs) / time );
341 gtk_label_set_text( GTK_LABEL(alloc_rate_label), buf );
344 while (gtk_events_pending()) {
345 gtk_main_iteration();
350 updateFrontPanel( void )
355 updateThreadsPanel();
358 my_gc = gdk_gc_new( window->window );
361 if (map_pixmap != NULL) {
362 nat height, width, blocks_per_line,
363 block_height, block_width, mblock_height;
365 height = map_drawing_area->allocation.height;
366 width = map_drawing_area->allocation.width;
368 mblock_height = height / mblocks_allocated;
369 blocks_per_line = 16;
370 block_height = mblock_height /
371 ((MBLOCK_SIZE/BLOCK_SIZE) / blocks_per_line);
372 while (block_height == 0) {
373 blocks_per_line *= 2;
374 block_height = mblock_height /
375 ((MBLOCK_SIZE/BLOCK_SIZE) / blocks_per_line);
377 block_width = width / blocks_per_line;
379 gdk_draw_rectangle (map_pixmap,
380 map_drawing_area->style->bg_gc[GTK_STATE_NORMAL],
383 map_drawing_area->allocation.width,
384 map_drawing_area->allocation.height);
387 (char *)m < (char *)mem_start +
388 (mblocks_allocated * MBLOCK_SIZE);
389 (char *)m += MBLOCK_SIZE ) {
391 /* color the bdescr area first */
392 for (a = m; a < FIRST_BLOCK(m); (char *)a += BLOCK_SIZE) {
393 colorBlock( a, &bdescr_color,
394 block_width, block_height, blocks_per_line );
397 #if 0 /* Segfaults because bd appears to be bogus but != NULL. stolz, 2003-06-24 */
398 /* color each block */
399 for (; a <= LAST_BLOCK(m); (char *)a += BLOCK_SIZE) {
401 ASSERT(bd->start == a);
402 if (bd->flags & BF_FREE) {
403 colorBlock( a, &free_color,
404 block_width, block_height, blocks_per_line );
406 colorBlock( a, &gen_colors[bd->gen_no],
407 block_width, block_height, blocks_per_line );
415 nat height = map_drawing_area->allocation.height,
416 block_height, mblock_height;
418 block_height = (height / mblocks_allocated) /
419 ((MBLOCK_SIZE/BLOCK_SIZE) / blocks_per_line);
420 if (block_height < 1) block_height = 1;
421 mblock_height = block_height *
422 ((MBLOCK_SIZE/BLOCK_SIZE) / blocks_per_line);
424 gtk_ruler_set_range( GTK_RULER(map_ruler), 0,
425 (double)(height * mblocks_allocated) /
426 (double)((mblock_height * mblocks_allocated)),
428 (double)(height * mblocks_allocated) /
429 (double)((mblock_height * mblocks_allocated))
433 gtk_widget_draw( map_drawing_area, NULL );
436 if (gen_pixmap != NULL) {
439 nat g, s, columns, column, max_blocks, height_blocks,
442 gdk_draw_rectangle (gen_pixmap,
443 gen_drawing_area->style->white_gc,
446 gen_drawing_area->allocation.width,
447 gen_drawing_area->allocation.height);
449 height = gen_drawing_area->allocation.height;
450 width = gen_drawing_area->allocation.width;
452 columns = 0; max_blocks = 0;
453 for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
454 columns += generations[g].n_steps;
455 for(s = 0; s < generations[g].n_steps; s++) {
456 if (generations[g].steps[s].n_blocks > max_blocks) {
457 max_blocks = generations[g].steps[s].n_blocks;
462 /* find a reasonable height value larger than max_blocks */
465 while (max_blocks != 0) {
466 max_blocks >>= 1; n++;
468 height_blocks = 1 << n;
472 for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
473 for(s = 0; s < generations[g].n_steps; s++, column++) {
474 gdk_gc_set_foreground(my_gc, &gen_colors[g]);
476 rect.x = column * (width / columns);
478 if (generations[g].steps[s].n_blocks == 0)
482 (height * generations[g].steps[s].n_blocks
485 rect.width = (width / columns);
486 rect.height = height - rect.y;
488 gdk_draw_rectangle( gen_pixmap, my_gc, TRUE/*filled*/,
489 rect.x, rect.y, rect.width,
494 gtk_ruler_set_range( GTK_RULER(gen_ruler),
495 height_blocks * BLOCK_SIZE / (1024 * 1024),
497 height_blocks * BLOCK_SIZE / (1024 * 1024)
500 gtk_widget_draw( gen_drawing_area, NULL );
503 if (res_pixmap != NULL) {
504 updateResidencyGraph();
507 while (gtk_events_pending()) {
508 gtk_main_iteration_do(FALSE/*don't block*/);
513 colorBlock( void *addr, GdkColor *color,
514 nat block_width, nat block_height, nat blocks_per_line )
519 gdk_gc_set_foreground(my_gc, color);
521 block_no = ((char *)addr - (char *)mem_start) / BLOCK_SIZE;
523 rect.x = (block_no % blocks_per_line) * block_width;
524 rect.y = block_no / blocks_per_line * block_height;
525 rect.width = block_width;
526 rect.height = block_height;
527 gdk_draw_rectangle( map_pixmap, my_gc, TRUE/*filled*/,
528 rect.x, rect.y, rect.width, rect.height );
532 updateThreadsPanel( void )
545 for (t = all_threads; t != END_TSO_QUEUE; t = t->global_link) {
546 switch (t->what_next) {
547 case ThreadKilled: break;
548 case ThreadComplete: break;
550 switch (t->why_blocked) {
551 case BlockedOnRead: b_read++; break;
552 case BlockedOnWrite: b_write++; break;
553 case BlockedOnDelay: sleeping++; break;
554 case BlockedOnMVar: b_mvar++; break;
555 case BlockedOnException: b_throwto++; break;
556 case BlockedOnBlackHole: b_bh++; break;
557 case NotBlocked: running++; break;
561 total = running + b_read + b_write + b_mvar + b_throwto + b_bh + sleeping;
562 numLabel(running_label, running);
563 numLabel(b_read_label, b_read);
564 numLabel(b_write_label, b_write);
565 numLabel(b_mvar_label, b_mvar);
566 numLabel(b_bh_label, b_bh);
567 numLabel(b_throwto_label, b_throwto);
568 numLabel(sleeping_label, sleeping);
569 numLabel(total_label, total);
572 typedef enum { Thunk, Fun, Constr, BlackHole,
573 Array, Thread, Other, N_Cats } ClosureCategory;
577 static nat *res_prof[N_SLICES];
578 static double res_time[N_SLICES];
579 static nat next_slice = 0;
582 residencyCensus( void )
584 nat slice = next_slice++, *prof;
586 nat g, s, size, type;
590 if (slice >= N_SLICES) {
591 barf("too many slices");
593 res_prof[slice] = stgMallocBytes(N_Cats * sizeof(nat), "residencyCensus");
594 prof = res_prof[slice];
595 memset(prof, 0, N_Cats * sizeof(nat));
597 res_time[slice] = mut_user_time();
599 for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
600 for(s = 0; s < generations[g].n_steps; s++) {
602 /* skip over g0s0 if multi-generational */
603 if (RtsFlags.GcFlags.generations > 1 &&
604 g == 0 && s == 0) continue;
606 if (RtsFlags.GcFlags.generations == 1) {
607 /* bd = generations[g].steps[s].to_blocks; FIXME to_blocks does not exist */
609 bd = generations[g].steps[s].blocks;
612 for (; bd != NULL; bd = bd->link) {
616 while (p < bd->free) {
617 info = get_itbl((StgClosure *)p);
620 switch (info->type) {
624 if (((StgClosure *)p)->header.info == &stg_DEAD_WEAK_info) {
625 size = sizeofW(StgWeak);
629 /* else, fall through... */
635 size = sizeW_fromITBL(info);
641 size = sizeofW(StgHeader) + 1;
647 size = sizeW_fromITBL(info);
655 size = sizeofW(StgHeader) + 2;
661 size = sizeW_fromITBL(info);
667 case SE_CAF_BLACKHOLE:
670 /* case BLACKHOLE_BQ: FIXME: case does not exist */
671 size = sizeW_fromITBL(info);
676 size = pap_sizeW((StgPAP *)p);
681 size = pap_sizeW((StgPAP *)p);
686 size = arr_words_sizeW(stgCast(StgArrWords*,p));
691 case MUT_ARR_PTRS_FROZEN:
692 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
697 size = tso_sizeW((StgTSO *)p);
705 /* case MUT_CONS: FIXME: case does not exist */
707 case IND_OLDGEN_PERM:
708 size = sizeW_fromITBL(info);
713 barf("updateResidencyGraph: strange closure "
727 updateResidencyGraph( void )
729 nat total, prev_total, i, max_res;
731 double time_scale = 1;
732 nat last_slice = next_slice-1;
733 double res_scale = 1; /* in megabytes, doubles */
738 gdk_draw_rectangle (res_pixmap,
739 res_drawing_area->style->bg_gc[GTK_STATE_NORMAL],
742 res_drawing_area->allocation.width,
743 res_drawing_area->allocation.height);
745 if (next_slice == 0) return;
747 time = res_time[last_slice];
748 while (time > time_scale) {
753 for (i = 0; i < next_slice; i++) {
755 total = prof[Thunk] + prof[Fun] + prof[Constr] +
756 prof[BlackHole] + prof[Array] + prof[Other];
757 if (total > max_res) {
761 while (max_res > res_scale) {
765 height = res_drawing_area->allocation.height;
766 width = res_drawing_area->allocation.width;
769 points[0].y = height;
770 points[1].y = height;
772 points[3].y = height;
774 gdk_gc_set_foreground(my_gc, &free_color);
777 for (i = 0; i < next_slice; i++) {
779 total = prof[Thunk] + prof[Fun] + prof[Constr] +
780 prof[BlackHole] + prof[Array] + prof[Other];
781 points[1].x = width * res_time[i] / time_scale;
782 points[2].x = points[1].x;
783 points[2].y = height - ((height * total) / res_scale);
784 gdk_draw_polygon(res_pixmap, my_gc, TRUE/*filled*/, points, 4);
785 points[3] = points[2];
786 points[0] = points[1];
789 gtk_ruler_set_range( GTK_RULER(res_vruler),
790 res_scale / ((1024*1024)/sizeof(W_)),
792 res_scale / ((1024*1024)/sizeof(W_)) );
794 gtk_ruler_set_range( GTK_RULER(res_hruler),
795 0, time_scale, 0, time_scale );
798 gtk_widget_draw( res_drawing_area, NULL );
801 #endif /* RTS_GTK_FRONTPANEL */