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"
25 #include "VisSupport.h"
26 #include "VisWindow.h"
28 static GtkWidget *window, *map_drawing_area, *gen_drawing_area;
29 static GtkWidget *res_drawing_area;
30 static GtkWidget *continue_but, *stop_but, *quit_but;
31 static GtkWidget *statusbar;
32 static GtkWidget *live_label, *allocated_label;
33 static GtkWidget *footprint_label, *alloc_rate_label;
34 static GtkWidget *map_ruler, *gen_ruler;
35 static GtkWidget *res_vruler, *res_hruler;
36 static GtkWidget *running_label, *b_read_label, *b_write_label, *total_label;
37 static GtkWidget *b_mvar_label, *b_bh_label, *b_throwto_label, *sleeping_label;
39 static guint status_context_id;
41 gboolean continue_now = FALSE, stop_now = FALSE, quit = FALSE;
42 UpdateMode update_mode = Continuous;
44 static GdkPixmap *map_pixmap = NULL;
45 static GdkPixmap *gen_pixmap = NULL;
46 static GdkPixmap *res_pixmap = NULL;
51 bdescr_color = { 0, 0xffff, 0, 0 }, /* red */
52 free_color = { 0, 0, 0, 0xffff }, /* blue */
53 gen_colors[N_GENS] = {
68 static void *mem_start = (void *) 0x50000000;
70 static void colorBlock( void *addr, GdkColor *color,
71 nat block_width, nat block_height,
72 nat blocks_per_line );
74 static void residencyCensus( void );
75 static void updateResidencyGraph( void );
76 static void updateThreadsPanel( void );
78 /* Some code pinched from examples/scribble-simple in the GTK+
82 /* Create a new backing pixmap of the appropriate size */
84 configure_event( GtkWidget *widget, GdkEventConfigure *event STG_UNUSED,
88 gdk_pixmap_unref(*pixmap);
90 *pixmap = gdk_pixmap_new(widget->window,
91 widget->allocation.width,
92 widget->allocation.height,
95 gdk_draw_rectangle (*pixmap,
96 widget->style->white_gc,
99 widget->allocation.width,
100 widget->allocation.height);
102 debugBelch("configure!\n");
107 /* Redraw the screen from the backing pixmap */
109 expose_event( GtkWidget *widget, GdkEventExpose *event, GdkPixmap **pixmap )
111 gdk_draw_pixmap(widget->window,
112 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
114 event->area.x, event->area.y,
115 event->area.x, event->area.y,
116 event->area.width, event->area.height);
122 initFrontPanel( void )
124 GdkColormap *colormap;
127 gtk_init( &prog_argc, &prog_argv );
129 window = create_GHC_Front_Panel();
130 map_drawing_area = lookup_widget(window, "memmap");
131 gen_drawing_area = lookup_widget(window, "generations");
132 res_drawing_area = lookup_widget(window, "res_drawingarea");
133 stop_but = lookup_widget(window, "stop_but");
134 continue_but = lookup_widget(window, "continue_but");
135 quit_but = lookup_widget(window, "quit_but");
136 statusbar = lookup_widget(window, "statusbar");
137 live_label = lookup_widget(window, "live_label");
138 footprint_label = lookup_widget(window, "footprint_label");
139 allocated_label = lookup_widget(window, "allocated_label");
140 alloc_rate_label = lookup_widget(window, "alloc_rate_label");
141 gen_hbox = lookup_widget(window, "gen_hbox");
142 gen_ruler = lookup_widget(window, "gen_ruler");
143 map_ruler = lookup_widget(window, "map_ruler");
144 res_vruler = lookup_widget(window, "res_vruler");
145 res_hruler = lookup_widget(window, "res_hruler");
146 running_label = lookup_widget(window, "running_label");
147 b_read_label = lookup_widget(window, "blockread_label");
148 b_write_label = lookup_widget(window, "blockwrite_label");
149 b_mvar_label = lookup_widget(window, "blockmvar_label");
150 b_bh_label = lookup_widget(window, "blockbh_label");
151 b_throwto_label = lookup_widget(window, "blockthrowto_label");
152 sleeping_label = lookup_widget(window, "sleeping_label");
153 total_label = lookup_widget(window, "total_label");
156 gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "context" );
158 /* hook up some signals for the mem map drawing area */
159 gtk_signal_connect (GTK_OBJECT(map_drawing_area), "expose_event",
160 (GtkSignalFunc)expose_event, &map_pixmap);
161 gtk_signal_connect (GTK_OBJECT(map_drawing_area), "configure_event",
162 (GtkSignalFunc)configure_event, &map_pixmap);
164 gtk_widget_set_events(map_drawing_area, GDK_EXPOSURE_MASK);
166 /* hook up some signals for the gen drawing area */
167 gtk_signal_connect (GTK_OBJECT(gen_drawing_area), "expose_event",
168 (GtkSignalFunc)expose_event, &gen_pixmap);
169 gtk_signal_connect (GTK_OBJECT(gen_drawing_area), "configure_event",
170 (GtkSignalFunc)configure_event, &gen_pixmap);
172 gtk_widget_set_events(gen_drawing_area, GDK_EXPOSURE_MASK);
174 /* hook up some signals for the res drawing area */
175 gtk_signal_connect (GTK_OBJECT(res_drawing_area), "expose_event",
176 (GtkSignalFunc)expose_event, &res_pixmap);
177 gtk_signal_connect (GTK_OBJECT(res_drawing_area), "configure_event",
178 (GtkSignalFunc)configure_event, &res_pixmap);
180 gtk_widget_set_events(res_drawing_area, GDK_EXPOSURE_MASK);
182 /* allocate our colors */
183 colormap = gdk_colormap_get_system();
184 gdk_colormap_alloc_color(colormap, &bdescr_color, TRUE, TRUE);
185 gdk_colormap_alloc_color(colormap, &free_color, TRUE, TRUE);
188 gboolean success[N_GENS];
189 gdk_colormap_alloc_colors(colormap, gen_colors, N_GENS, TRUE,
191 if (!success) { barf("can't allocate colors"); }
194 /* set the labels on the generation histogram */
200 for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
201 for(s = 0; s < generations[g].n_steps; s++) {
202 g_snprintf( buf, 64, "%d.%d", g, s );
203 label = gtk_label_new( buf );
204 gtk_box_pack_start( GTK_BOX(gen_hbox), label,
206 gtk_widget_show(label);
211 gtk_widget_show(window);
213 /* wait for the user to press "Continue" before getting going... */
214 gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id,
216 gtk_widget_set_sensitive( stop_but, FALSE );
217 continue_now = FALSE;
218 while (continue_now == FALSE) {
219 gtk_main_iteration();
221 gtk_statusbar_pop( GTK_STATUSBAR(statusbar), status_context_id );
222 gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id,
225 gtk_widget_set_sensitive( continue_but, FALSE );
226 gtk_widget_set_sensitive( stop_but, TRUE );
227 gtk_widget_set_sensitive( quit_but, FALSE );
229 while (gtk_events_pending()) {
230 gtk_main_iteration();
235 stopFrontPanel( void )
237 gtk_widget_set_sensitive( quit_but, TRUE );
238 gtk_widget_set_sensitive( continue_but, FALSE );
239 gtk_widget_set_sensitive( stop_but, FALSE );
243 gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id,
247 while (quit == FALSE) {
248 gtk_main_iteration();
253 waitForContinue( void )
255 gtk_widget_set_sensitive( continue_but, TRUE );
256 gtk_widget_set_sensitive( stop_but, FALSE );
258 continue_now = FALSE;
259 while (continue_now == FALSE) {
260 gtk_main_iteration();
262 gtk_widget_set_sensitive( continue_but, FALSE );
263 gtk_widget_set_sensitive( stop_but, TRUE );
267 updateFrontPanelBeforeGC( nat N )
273 if (update_mode == BeforeGC
274 || update_mode == BeforeAfterGC
275 || stop_now == TRUE) {
276 g_snprintf( buf, 1000, "Stopped (before GC, generation %d)", N );
277 gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id, buf );
279 gtk_statusbar_pop( GTK_STATUSBAR(statusbar), status_context_id );
282 g_snprintf( buf, 1000, "Garbage collecting (generation %d)", N );
283 gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id, buf);
285 while (gtk_events_pending()) {
286 gtk_main_iteration();
291 numLabel( GtkWidget *lbl, nat n )
294 g_snprintf(buf, 64, "%d", n);
295 gtk_label_set_text( GTK_LABEL(lbl), buf );
299 updateFrontPanelAfterGC( nat N, lnat live )
303 gtk_statusbar_pop( GTK_STATUSBAR(statusbar), status_context_id );
306 if (N == RtsFlags.GcFlags.generations-1) {
312 if (update_mode == AfterGC
313 || update_mode == BeforeAfterGC
314 || stop_now == TRUE) {
315 snprintf( buf, 1000, "Stopped (after GC, generation %d)", N );
316 gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id, buf );
318 gtk_statusbar_pop( GTK_STATUSBAR(statusbar), status_context_id );
322 double words_to_megs = (1024 * 1024) / sizeof(W_);
323 double time = mut_user_time();
325 snprintf( buf, 1000, "%.2f", (double)live / words_to_megs );
326 gtk_label_set_text( GTK_LABEL(live_label), buf );
328 snprintf( buf, 1000, "%.2f", (double)total_allocated / words_to_megs );
329 gtk_label_set_text( GTK_LABEL(allocated_label), buf );
331 snprintf( buf, 1000, "%.2f",
332 (double)(mblocks_allocated * MBLOCK_SIZE_W) / words_to_megs );
333 gtk_label_set_text( GTK_LABEL(footprint_label), buf );
336 snprintf( buf, 1000, "%.2f", time );
338 snprintf( buf, 1000, "%.2f",
339 (double)(total_allocated / words_to_megs) / time );
340 gtk_label_set_text( GTK_LABEL(alloc_rate_label), buf );
343 while (gtk_events_pending()) {
344 gtk_main_iteration();
349 updateFrontPanel( void )
354 updateThreadsPanel();
357 my_gc = gdk_gc_new( window->window );
360 if (map_pixmap != NULL) {
361 nat height, width, blocks_per_line,
362 block_height, block_width, mblock_height;
364 height = map_drawing_area->allocation.height;
365 width = map_drawing_area->allocation.width;
367 mblock_height = height / mblocks_allocated;
368 blocks_per_line = 16;
369 block_height = mblock_height /
370 ((MBLOCK_SIZE/BLOCK_SIZE) / blocks_per_line);
371 while (block_height == 0) {
372 blocks_per_line *= 2;
373 block_height = mblock_height /
374 ((MBLOCK_SIZE/BLOCK_SIZE) / blocks_per_line);
376 block_width = width / blocks_per_line;
378 gdk_draw_rectangle (map_pixmap,
379 map_drawing_area->style->bg_gc[GTK_STATE_NORMAL],
382 map_drawing_area->allocation.width,
383 map_drawing_area->allocation.height);
386 (char *)m < (char *)mem_start +
387 (mblocks_allocated * MBLOCK_SIZE);
388 (char *)m += MBLOCK_SIZE ) {
390 /* color the bdescr area first */
391 for (a = m; a < FIRST_BLOCK(m); (char *)a += BLOCK_SIZE) {
392 colorBlock( a, &bdescr_color,
393 block_width, block_height, blocks_per_line );
396 #if 0 /* Segfaults because bd appears to be bogus but != NULL. stolz, 2003-06-24 */
397 /* color each block */
398 for (; a <= LAST_BLOCK(m); (char *)a += BLOCK_SIZE) {
400 ASSERT(bd->start == a);
401 if (bd->flags & BF_FREE) {
402 colorBlock( a, &free_color,
403 block_width, block_height, blocks_per_line );
405 colorBlock( a, &gen_colors[bd->gen_no],
406 block_width, block_height, blocks_per_line );
414 nat height = map_drawing_area->allocation.height,
415 block_height, mblock_height;
417 block_height = (height / mblocks_allocated) /
418 ((MBLOCK_SIZE/BLOCK_SIZE) / blocks_per_line);
419 if (block_height < 1) block_height = 1;
420 mblock_height = block_height *
421 ((MBLOCK_SIZE/BLOCK_SIZE) / blocks_per_line);
423 gtk_ruler_set_range( GTK_RULER(map_ruler), 0,
424 (double)(height * mblocks_allocated) /
425 (double)((mblock_height * mblocks_allocated)),
427 (double)(height * mblocks_allocated) /
428 (double)((mblock_height * mblocks_allocated))
432 gtk_widget_draw( map_drawing_area, NULL );
435 if (gen_pixmap != NULL) {
438 nat g, s, columns, column, max_blocks, height_blocks,
441 gdk_draw_rectangle (gen_pixmap,
442 gen_drawing_area->style->white_gc,
445 gen_drawing_area->allocation.width,
446 gen_drawing_area->allocation.height);
448 height = gen_drawing_area->allocation.height;
449 width = gen_drawing_area->allocation.width;
451 columns = 0; max_blocks = 0;
452 for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
453 columns += generations[g].n_steps;
454 for(s = 0; s < generations[g].n_steps; s++) {
455 if (generations[g].steps[s].n_blocks > max_blocks) {
456 max_blocks = generations[g].steps[s].n_blocks;
461 /* find a reasonable height value larger than max_blocks */
464 while (max_blocks != 0) {
465 max_blocks >>= 1; n++;
467 height_blocks = 1 << n;
471 for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
472 for(s = 0; s < generations[g].n_steps; s++, column++) {
473 gdk_gc_set_foreground(my_gc, &gen_colors[g]);
475 rect.x = column * (width / columns);
477 if (generations[g].steps[s].n_blocks == 0)
481 (height * generations[g].steps[s].n_blocks
484 rect.width = (width / columns);
485 rect.height = height - rect.y;
487 gdk_draw_rectangle( gen_pixmap, my_gc, TRUE/*filled*/,
488 rect.x, rect.y, rect.width,
493 gtk_ruler_set_range( GTK_RULER(gen_ruler),
494 height_blocks * BLOCK_SIZE / (1024 * 1024),
496 height_blocks * BLOCK_SIZE / (1024 * 1024)
499 gtk_widget_draw( gen_drawing_area, NULL );
502 if (res_pixmap != NULL) {
503 updateResidencyGraph();
506 while (gtk_events_pending()) {
507 gtk_main_iteration_do(FALSE/*don't block*/);
512 colorBlock( void *addr, GdkColor *color,
513 nat block_width, nat block_height, nat blocks_per_line )
518 gdk_gc_set_foreground(my_gc, color);
520 block_no = ((char *)addr - (char *)mem_start) / BLOCK_SIZE;
522 rect.x = (block_no % blocks_per_line) * block_width;
523 rect.y = block_no / blocks_per_line * block_height;
524 rect.width = block_width;
525 rect.height = block_height;
526 gdk_draw_rectangle( map_pixmap, my_gc, TRUE/*filled*/,
527 rect.x, rect.y, rect.width, rect.height );
531 updateThreadsPanel( void )
544 for (t = all_threads; t != END_TSO_QUEUE; t = t->global_link) {
545 switch (t->what_next) {
546 case ThreadKilled: break;
547 case ThreadComplete: break;
549 switch (t->why_blocked) {
550 case BlockedOnRead: b_read++; break;
551 case BlockedOnWrite: b_write++; break;
552 case BlockedOnDelay: sleeping++; break;
553 case BlockedOnMVar: b_mvar++; break;
554 case BlockedOnException: b_throwto++; break;
555 case BlockedOnBlackHole: b_bh++; break;
556 case NotBlocked: running++; break;
560 total = running + b_read + b_write + b_mvar + b_throwto + b_bh + sleeping;
561 numLabel(running_label, running);
562 numLabel(b_read_label, b_read);
563 numLabel(b_write_label, b_write);
564 numLabel(b_mvar_label, b_mvar);
565 numLabel(b_bh_label, b_bh);
566 numLabel(b_throwto_label, b_throwto);
567 numLabel(sleeping_label, sleeping);
568 numLabel(total_label, total);
571 typedef enum { Thunk, Fun, Constr, BlackHole,
572 Array, Thread, Other, N_Cats } ClosureCategory;
576 static nat *res_prof[N_SLICES];
577 static double res_time[N_SLICES];
578 static nat next_slice = 0;
581 residencyCensus( void )
583 nat slice = next_slice++, *prof;
585 nat g, s, size, type;
589 if (slice >= N_SLICES) {
590 barf("too many slices");
592 res_prof[slice] = stgMallocBytes(N_Cats * sizeof(nat), "residencyCensus");
593 prof = res_prof[slice];
594 memset(prof, 0, N_Cats * sizeof(nat));
596 res_time[slice] = mut_user_time();
598 for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
599 for(s = 0; s < generations[g].n_steps; s++) {
601 /* skip over g0s0 if multi-generational */
602 if (RtsFlags.GcFlags.generations > 1 &&
603 g == 0 && s == 0) continue;
605 if (RtsFlags.GcFlags.generations == 1) {
606 /* bd = generations[g].steps[s].to_blocks; FIXME to_blocks does not exist */
608 bd = generations[g].steps[s].blocks;
611 for (; bd != NULL; bd = bd->link) {
615 while (p < bd->free) {
616 info = get_itbl((StgClosure *)p);
619 switch (info->type) {
623 if (((StgClosure *)p)->header.info == &stg_DEAD_WEAK_info) {
624 size = sizeofW(StgWeak);
628 /* else, fall through... */
634 size = sizeW_fromITBL(info);
640 size = sizeofW(StgHeader) + 1;
646 size = sizeW_fromITBL(info);
654 size = sizeofW(StgHeader) + 2;
660 size = sizeW_fromITBL(info);
666 /* case BLACKHOLE_BQ: FIXME: case does not exist */
667 size = sizeW_fromITBL(info);
672 size = pap_sizeW((StgPAP *)p);
677 size = pap_sizeW((StgPAP *)p);
682 size = arr_words_sizeW(stgCast(StgArrWords*,p));
687 case MUT_ARR_PTRS_FROZEN:
688 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
693 size = tso_sizeW((StgTSO *)p);
701 /* case MUT_CONS: FIXME: case does not exist */
703 size = sizeW_fromITBL(info);
708 barf("updateResidencyGraph: strange closure "
722 updateResidencyGraph( void )
724 nat total, prev_total, i, max_res;
726 double time_scale = 1;
727 nat last_slice = next_slice-1;
728 double res_scale = 1; /* in megabytes, doubles */
733 gdk_draw_rectangle (res_pixmap,
734 res_drawing_area->style->bg_gc[GTK_STATE_NORMAL],
737 res_drawing_area->allocation.width,
738 res_drawing_area->allocation.height);
740 if (next_slice == 0) return;
742 time = res_time[last_slice];
743 while (time > time_scale) {
748 for (i = 0; i < next_slice; i++) {
750 total = prof[Thunk] + prof[Fun] + prof[Constr] +
751 prof[BlackHole] + prof[Array] + prof[Other];
752 if (total > max_res) {
756 while (max_res > res_scale) {
760 height = res_drawing_area->allocation.height;
761 width = res_drawing_area->allocation.width;
764 points[0].y = height;
765 points[1].y = height;
767 points[3].y = height;
769 gdk_gc_set_foreground(my_gc, &free_color);
772 for (i = 0; i < next_slice; i++) {
774 total = prof[Thunk] + prof[Fun] + prof[Constr] +
775 prof[BlackHole] + prof[Array] + prof[Other];
776 points[1].x = width * res_time[i] / time_scale;
777 points[2].x = points[1].x;
778 points[2].y = height - ((height * total) / res_scale);
779 gdk_draw_polygon(res_pixmap, my_gc, TRUE/*filled*/, points, 4);
780 points[3] = points[2];
781 points[0] = points[1];
784 gtk_ruler_set_range( GTK_RULER(res_vruler),
785 res_scale / ((1024*1024)/sizeof(W_)),
787 res_scale / ((1024*1024)/sizeof(W_)) );
789 gtk_ruler_set_range( GTK_RULER(res_hruler),
790 0, time_scale, 0, time_scale );
793 gtk_widget_draw( res_drawing_area, NULL );
796 #endif /* RTS_GTK_FRONTPANEL */