[project @ 2004-08-13 13:04:50 by simonmar]
[ghc-hetmet.git] / ghc / rts / FrontPanel.c
1 /* -----------------------------------------------------------------------------
2  * $Id: FrontPanel.c,v 1.9 2004/08/13 13:09:49 simonmar Exp $
3  *
4  * (c) The GHC Team 2000
5  *
6  * RTS GTK Front Panel
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #ifdef RTS_GTK_FRONTPANEL
11
12 /* Alas, not Posix. */
13 /* #include "PosixSource.h" */
14
15 #include "Rts.h"
16 #include "RtsUtils.h"
17 #include "MBlock.h"
18 #include "FrontPanel.h"
19 #include "Storage.h"
20 #include "Stats.h"
21 #include "RtsFlags.h"
22 #include "Schedule.h"
23
24 #include <unistd.h>
25 #include <gdk/gdktypes.h>
26 #include <gtk/gtk.h>
27
28 #include "VisSupport.h"
29 #include "VisWindow.h"
30
31 static GtkWidget *window, *map_drawing_area, *gen_drawing_area;
32 static GtkWidget *res_drawing_area;
33 static GtkWidget *continue_but, *stop_but, *quit_but;
34 static GtkWidget *statusbar;
35 static GtkWidget *live_label, *allocated_label;
36 static GtkWidget *footprint_label, *alloc_rate_label;
37 static GtkWidget *map_ruler, *gen_ruler;
38 static GtkWidget *res_vruler, *res_hruler;
39 static GtkWidget *running_label, *b_read_label, *b_write_label, *total_label;
40 static GtkWidget *b_mvar_label, *b_bh_label, *b_throwto_label, *sleeping_label;
41
42 static guint status_context_id;
43
44 gboolean continue_now = FALSE, stop_now = FALSE, quit = FALSE;
45 UpdateMode update_mode = Continuous;
46
47 static GdkPixmap *map_pixmap = NULL;
48 static GdkPixmap *gen_pixmap = NULL;
49 static GdkPixmap *res_pixmap = NULL;
50
51 #define N_GENS 10
52
53 static GdkColor 
54     bdescr_color = { 0, 0xffff, 0, 0 }, /* red */
55     free_color   = { 0, 0, 0, 0xffff }, /* blue */
56     gen_colors[N_GENS] = {
57         { 0, 0, 0xffff, 0 },
58         { 0, 0, 0xf000, 0 },
59         { 0, 0, 0xe000, 0 },
60         { 0, 0, 0xd000, 0 },
61         { 0, 0, 0xc000, 0 },
62         { 0, 0, 0xb000, 0 },
63         { 0, 0, 0xa000, 0 },
64         { 0, 0, 0x9000, 0 },
65         { 0, 0, 0x8000, 0 },
66         { 0, 0, 0x7000, 0 }
67     };
68
69 GdkGC *my_gc = NULL;
70
71 static void *mem_start = (void *) 0x50000000;
72
73 static void colorBlock( void *addr, GdkColor *color, 
74                         nat block_width, nat block_height, 
75                         nat blocks_per_line );
76
77 static void residencyCensus( void );
78 static void updateResidencyGraph( void );
79 static void updateThreadsPanel( void );
80
81 /* Some code pinched from examples/scribble-simple in the GTK+
82  * distribution.
83  */
84
85 /* Create a new backing pixmap of the appropriate size */
86 static gint 
87 configure_event( GtkWidget *widget, GdkEventConfigure *event STG_UNUSED,
88                  GdkPixmap **pixmap )
89 {
90   if (*pixmap)
91     gdk_pixmap_unref(*pixmap);
92
93   *pixmap = gdk_pixmap_new(widget->window,
94                            widget->allocation.width,
95                            widget->allocation.height,
96                            -1);
97
98   gdk_draw_rectangle (*pixmap,
99                       widget->style->white_gc,
100                       TRUE,
101                       0, 0,
102                       widget->allocation.width,
103                       widget->allocation.height);
104
105   fprintf(stderr, "configure!\n");
106   updateFrontPanel();
107   return TRUE;
108 }
109
110 /* Redraw the screen from the backing pixmap */
111 static gint 
112 expose_event( GtkWidget *widget, GdkEventExpose *event, GdkPixmap **pixmap )
113 {
114   gdk_draw_pixmap(widget->window,
115                   widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
116                   *pixmap,
117                   event->area.x, event->area.y,
118                   event->area.x, event->area.y,
119                   event->area.width, event->area.height);
120
121   return FALSE;
122 }
123
124 void
125 initFrontPanel( void )
126 {
127     GdkColormap *colormap;
128     GtkWidget *gen_hbox;
129
130     gtk_init( &prog_argc, &prog_argv );
131
132     window = create_GHC_Front_Panel();
133     map_drawing_area  = lookup_widget(window, "memmap");
134     gen_drawing_area  = lookup_widget(window, "generations");
135     res_drawing_area  = lookup_widget(window, "res_drawingarea");
136     stop_but          = lookup_widget(window, "stop_but");
137     continue_but      = lookup_widget(window, "continue_but");
138     quit_but          = lookup_widget(window, "quit_but");
139     statusbar         = lookup_widget(window, "statusbar");
140     live_label        = lookup_widget(window, "live_label");
141     footprint_label   = lookup_widget(window, "footprint_label");
142     allocated_label   = lookup_widget(window, "allocated_label");
143     alloc_rate_label  = lookup_widget(window, "alloc_rate_label");
144     gen_hbox          = lookup_widget(window, "gen_hbox");
145     gen_ruler         = lookup_widget(window, "gen_ruler");
146     map_ruler         = lookup_widget(window, "map_ruler");
147     res_vruler        = lookup_widget(window, "res_vruler");
148     res_hruler        = lookup_widget(window, "res_hruler");
149     running_label     = lookup_widget(window, "running_label");
150     b_read_label      = lookup_widget(window, "blockread_label");
151     b_write_label     = lookup_widget(window, "blockwrite_label");
152     b_mvar_label      = lookup_widget(window, "blockmvar_label");
153     b_bh_label        = lookup_widget(window, "blockbh_label");
154     b_throwto_label   = lookup_widget(window, "blockthrowto_label");
155     sleeping_label    = lookup_widget(window, "sleeping_label");
156     total_label       = lookup_widget(window, "total_label");
157     
158     status_context_id = 
159         gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "context" );
160
161     /* hook up some signals for the mem map drawing area */
162     gtk_signal_connect (GTK_OBJECT(map_drawing_area), "expose_event",
163                         (GtkSignalFunc)expose_event, &map_pixmap);
164     gtk_signal_connect (GTK_OBJECT(map_drawing_area), "configure_event",
165                         (GtkSignalFunc)configure_event, &map_pixmap);
166
167     gtk_widget_set_events(map_drawing_area, GDK_EXPOSURE_MASK);
168
169     /* hook up some signals for the gen drawing area */
170     gtk_signal_connect (GTK_OBJECT(gen_drawing_area), "expose_event",
171                         (GtkSignalFunc)expose_event, &gen_pixmap);
172     gtk_signal_connect (GTK_OBJECT(gen_drawing_area), "configure_event",
173                         (GtkSignalFunc)configure_event, &gen_pixmap);
174
175     gtk_widget_set_events(gen_drawing_area, GDK_EXPOSURE_MASK);
176     
177     /* hook up some signals for the res drawing area */
178     gtk_signal_connect (GTK_OBJECT(res_drawing_area), "expose_event",
179                         (GtkSignalFunc)expose_event, &res_pixmap);
180     gtk_signal_connect (GTK_OBJECT(res_drawing_area), "configure_event",
181                         (GtkSignalFunc)configure_event, &res_pixmap);
182
183     gtk_widget_set_events(res_drawing_area, GDK_EXPOSURE_MASK);
184     
185     /* allocate our colors */
186     colormap = gdk_colormap_get_system();
187     gdk_colormap_alloc_color(colormap, &bdescr_color, TRUE, TRUE);
188     gdk_colormap_alloc_color(colormap, &free_color, TRUE, TRUE);
189
190     {
191         gboolean success[N_GENS];
192         gdk_colormap_alloc_colors(colormap, gen_colors, N_GENS, TRUE,
193                                   TRUE, success);
194         if (!success) { barf("can't allocate colors"); }
195     }
196
197     /* set the labels on the generation histogram */
198     {
199         char buf[64];
200         nat g, s;
201         GtkWidget *label;
202
203         for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
204             for(s = 0; s < generations[g].n_steps; s++) {
205                 g_snprintf( buf, 64, "%d.%d", g, s );
206                 label = gtk_label_new( buf );
207                 gtk_box_pack_start( GTK_BOX(gen_hbox), label,
208                                     TRUE, TRUE, 5 );
209                 gtk_widget_show(label);
210             }
211         }
212     }
213
214     gtk_widget_show(window);
215
216     /* wait for the user to press "Continue" before getting going... */
217     gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id, 
218                         "Program start");
219     gtk_widget_set_sensitive( stop_but, FALSE );
220     continue_now = FALSE;
221     while (continue_now == FALSE) {
222         gtk_main_iteration();
223     }
224     gtk_statusbar_pop( GTK_STATUSBAR(statusbar), status_context_id );
225     gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id, 
226                         "Running");
227
228     gtk_widget_set_sensitive( continue_but, FALSE );
229     gtk_widget_set_sensitive( stop_but, TRUE );
230     gtk_widget_set_sensitive( quit_but, FALSE );
231
232     while (gtk_events_pending()) {
233         gtk_main_iteration();
234     }
235 }
236
237 void
238 stopFrontPanel( void )
239 {
240     gtk_widget_set_sensitive( quit_but, TRUE );
241     gtk_widget_set_sensitive( continue_but, FALSE );
242     gtk_widget_set_sensitive( stop_but, FALSE );
243
244     updateFrontPanel();
245
246     gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id, 
247                         "Program finished");
248
249     quit = FALSE;
250     while (quit == FALSE) {
251         gtk_main_iteration();
252     }
253 }
254
255 static void
256 waitForContinue( void )
257 {
258     gtk_widget_set_sensitive( continue_but, TRUE );
259     gtk_widget_set_sensitive( stop_but, FALSE );
260     stop_now = FALSE;
261     continue_now = FALSE;
262     while (continue_now == FALSE) {
263         gtk_main_iteration();
264     }
265     gtk_widget_set_sensitive( continue_but, FALSE );
266     gtk_widget_set_sensitive( stop_but, TRUE );
267 }
268
269 void
270 updateFrontPanelBeforeGC( nat N )
271 {
272     char buf[1000];
273
274     updateFrontPanel();
275
276     if (update_mode == BeforeGC 
277         || update_mode == BeforeAfterGC
278         || stop_now == TRUE) {
279         g_snprintf( buf, 1000, "Stopped (before GC, generation %d)", N );
280         gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id, buf );
281         waitForContinue();
282         gtk_statusbar_pop( GTK_STATUSBAR(statusbar), status_context_id );
283     }
284
285     g_snprintf( buf, 1000, "Garbage collecting (generation %d)", N );
286     gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id, buf);
287
288     while (gtk_events_pending()) {
289         gtk_main_iteration();
290     }
291 }
292
293 static void
294 numLabel( GtkWidget *lbl, nat n )
295 {
296     char buf[64];
297     g_snprintf(buf, 64, "%d", n);
298     gtk_label_set_text( GTK_LABEL(lbl), buf );
299 }
300
301 void
302 updateFrontPanelAfterGC( nat N, lnat live )
303 {
304     char buf[1000];
305
306     gtk_statusbar_pop( GTK_STATUSBAR(statusbar), status_context_id );
307
308     /* is a major GC? */
309     if (N == RtsFlags.GcFlags.generations-1) {
310         residencyCensus();
311     }
312
313     updateFrontPanel();
314
315     if (update_mode == AfterGC 
316         || update_mode == BeforeAfterGC
317         || stop_now == TRUE) {
318         snprintf( buf, 1000, "Stopped (after GC, generation %d)", N );
319         gtk_statusbar_push( GTK_STATUSBAR(statusbar), status_context_id, buf );
320         waitForContinue();
321         gtk_statusbar_pop( GTK_STATUSBAR(statusbar), status_context_id );
322     }
323
324     {
325         double words_to_megs = (1024 * 1024) / sizeof(W_);
326         double time = mut_user_time();
327
328         snprintf( buf, 1000, "%.2f", (double)live / words_to_megs );
329         gtk_label_set_text( GTK_LABEL(live_label), buf );
330
331         snprintf( buf, 1000, "%.2f", (double)total_allocated / words_to_megs );
332         gtk_label_set_text( GTK_LABEL(allocated_label), buf );
333
334         snprintf( buf, 1000, "%.2f",
335                   (double)(mblocks_allocated * MBLOCK_SIZE_W) / words_to_megs );
336         gtk_label_set_text( GTK_LABEL(footprint_label), buf );
337
338         if ( time == 0.0 )
339             snprintf( buf, 1000, "%.2f", time );
340         else
341             snprintf( buf, 1000, "%.2f",
342                       (double)(total_allocated / words_to_megs) / time );
343         gtk_label_set_text( GTK_LABEL(alloc_rate_label), buf );
344     }
345
346     while (gtk_events_pending()) {
347         gtk_main_iteration();
348     }
349 }
350
351 void
352 updateFrontPanel( void )
353 {
354     void *m, *a;
355     bdescr *bd;
356
357     updateThreadsPanel();
358
359     if (my_gc == NULL) {
360         my_gc = gdk_gc_new( window->window );
361     }
362
363     if (map_pixmap != NULL) {
364         nat height, width, blocks_per_line, 
365             block_height, block_width, mblock_height;
366
367         height = map_drawing_area->allocation.height;
368         width  = map_drawing_area->allocation.width;
369
370         mblock_height =  height / mblocks_allocated;
371         blocks_per_line = 16;
372         block_height  = mblock_height / 
373             ((MBLOCK_SIZE/BLOCK_SIZE) / blocks_per_line);
374         while (block_height == 0) {
375             blocks_per_line *= 2;
376             block_height  = mblock_height / 
377                 ((MBLOCK_SIZE/BLOCK_SIZE) / blocks_per_line);
378         }
379         block_width = width / blocks_per_line;
380
381         gdk_draw_rectangle (map_pixmap,
382                             map_drawing_area->style->bg_gc[GTK_STATE_NORMAL],
383                             TRUE,
384                             0, 0,
385                             map_drawing_area->allocation.width,
386                             map_drawing_area->allocation.height);
387         
388         for ( m = mem_start; 
389               (char *)m < (char *)mem_start + 
390                   (mblocks_allocated * MBLOCK_SIZE); 
391               (char *)m += MBLOCK_SIZE ) {
392             
393             /* color the bdescr area first */
394             for (a = m; a < FIRST_BLOCK(m); (char *)a += BLOCK_SIZE) {
395                 colorBlock( a, &bdescr_color, 
396                             block_width, block_height, blocks_per_line );
397             }
398             
399 #if 0 /* Segfaults because bd appears to be bogus but != NULL. stolz, 2003-06-24 */
400             /* color each block */
401             for (; a <= LAST_BLOCK(m); (char *)a += BLOCK_SIZE) {
402                 bd = Bdescr((P_)a);
403                 ASSERT(bd->start == a);
404                 if (bd->free == (void *)-1) {
405                     colorBlock( a, &free_color, 
406                                 block_width, block_height, blocks_per_line );
407                 } else {
408                     colorBlock( a, &gen_colors[bd->gen_no],
409                                 block_width, block_height, blocks_per_line );
410                 }
411             }
412 #endif
413         }
414
415         
416         { 
417             nat height = map_drawing_area->allocation.height,
418                 block_height, mblock_height;
419
420             block_height = (height / mblocks_allocated) / 
421                 ((MBLOCK_SIZE/BLOCK_SIZE) / blocks_per_line);
422             if (block_height < 1) block_height = 1;
423             mblock_height = block_height * 
424                 ((MBLOCK_SIZE/BLOCK_SIZE) / blocks_per_line);
425
426             gtk_ruler_set_range( GTK_RULER(map_ruler), 0, 
427                                  (double)(height * mblocks_allocated) / 
428                                  (double)((mblock_height * mblocks_allocated)),
429                                  0,
430                                  (double)(height * mblocks_allocated) / 
431                                  (double)((mblock_height * mblocks_allocated))
432                 );
433         }
434                                   
435         gtk_widget_draw( map_drawing_area, NULL );
436     }
437
438     if (gen_pixmap != NULL) {
439
440         GdkRectangle rect;
441         nat g, s, columns, column, max_blocks, height_blocks,
442             width, height;
443         
444         gdk_draw_rectangle (gen_pixmap,
445                             gen_drawing_area->style->white_gc,
446                             TRUE,
447                             0, 0,
448                             gen_drawing_area->allocation.width,
449                             gen_drawing_area->allocation.height);
450
451         height = gen_drawing_area->allocation.height;
452         width  = gen_drawing_area->allocation.width;
453
454         columns = 0; max_blocks = 0;
455         for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
456             columns += generations[g].n_steps;
457             for(s = 0; s < generations[g].n_steps; s++) {
458                 if (generations[g].steps[s].n_blocks > max_blocks) {
459                     max_blocks = generations[g].steps[s].n_blocks;
460                 }
461             }
462         }
463
464         /* find a reasonable height value larger than max_blocks */
465         { 
466             nat n = 0;
467             while (max_blocks != 0) {
468                 max_blocks >>= 1; n++;
469             }
470             height_blocks = 1 << n;
471         }
472
473         column = 0;
474         for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
475             for(s = 0; s < generations[g].n_steps; s++, column++) {
476                 gdk_gc_set_foreground(my_gc, &gen_colors[g]);
477
478                 rect.x = column * (width / columns);
479
480                 if (generations[g].steps[s].n_blocks == 0)
481                     rect.y = height;
482                 else
483                     rect.y = height - 
484                         (height * generations[g].steps[s].n_blocks
485                          / height_blocks);
486
487                 rect.width = (width / columns);
488                 rect.height = height - rect.y;
489
490                 gdk_draw_rectangle( gen_pixmap, my_gc, TRUE/*filled*/, 
491                                     rect.x, rect.y, rect.width,
492                                     rect.height );
493             }
494         }
495
496         gtk_ruler_set_range( GTK_RULER(gen_ruler), 
497                              height_blocks * BLOCK_SIZE / (1024 * 1024),
498                              0, 0,
499                              height_blocks * BLOCK_SIZE / (1024 * 1024)
500             );
501
502         gtk_widget_draw( gen_drawing_area, NULL );
503     }
504
505     if (res_pixmap != NULL) {
506         updateResidencyGraph();
507     }
508
509     while (gtk_events_pending()) {
510         gtk_main_iteration_do(FALSE/*don't block*/);
511     }
512 }
513
514 static void
515 colorBlock( void *addr, GdkColor *color, 
516             nat block_width, nat block_height, nat blocks_per_line )
517 {
518     GdkRectangle rect;
519     nat block_no;
520
521     gdk_gc_set_foreground(my_gc, color);
522
523     block_no = ((char *)addr - (char *)mem_start) / BLOCK_SIZE;
524
525     rect.x = (block_no % blocks_per_line) * block_width;
526     rect.y = block_no / blocks_per_line * block_height;
527     rect.width = block_width;
528     rect.height = block_height;
529     gdk_draw_rectangle( map_pixmap, my_gc, TRUE/*filled*/, 
530                         rect.x, rect.y, rect.width, rect.height );
531 }
532
533 static void
534 updateThreadsPanel( void )
535 {
536     nat running = 0,
537         b_read = 0,
538         b_write = 0,
539         b_mvar = 0,
540         b_throwto = 0,
541         b_bh = 0,
542         sleeping = 0,
543         total = 0;
544
545     StgTSO *t;
546
547     for (t = all_threads; t != END_TSO_QUEUE; t = t->global_link) {
548         switch (t->what_next) {
549         case ThreadKilled:          break;
550         case ThreadComplete:        break;
551         default:
552             switch (t->why_blocked) {
553             case BlockedOnRead:       b_read++;    break;
554             case BlockedOnWrite:      b_write++;   break;
555             case BlockedOnDelay:      sleeping++;  break;
556             case BlockedOnMVar:       b_mvar++;    break;
557             case BlockedOnException:  b_throwto++; break;
558             case BlockedOnBlackHole:  b_bh++;      break;
559             case NotBlocked:          running++;   break;
560             }
561         }
562     }
563     total = running + b_read + b_write + b_mvar + b_throwto + b_bh + sleeping;
564     numLabel(running_label,   running);
565     numLabel(b_read_label,    b_read);
566     numLabel(b_write_label,   b_write);
567     numLabel(b_mvar_label,    b_mvar);
568     numLabel(b_bh_label,      b_bh);
569     numLabel(b_throwto_label, b_throwto);
570     numLabel(sleeping_label,  sleeping);
571     numLabel(total_label,     total);
572 }
573
574 typedef enum { Thunk, Fun, Constr, BlackHole,
575                Array, Thread, Other, N_Cats } ClosureCategory;
576
577 #define N_SLICES 100
578
579 static nat *res_prof[N_SLICES];
580 static double res_time[N_SLICES];
581 static nat next_slice = 0;
582
583 static void
584 residencyCensus( void )
585 {
586     nat slice = next_slice++, *prof;
587     bdescr *bd;
588     nat g, s, size, type;
589     StgPtr p;
590     StgInfoTable *info;
591
592     if (slice >= N_SLICES) {
593         barf("too many slices");
594     }
595     res_prof[slice] = stgMallocBytes(N_Cats * sizeof(nat), "residencyCensus");
596     prof = res_prof[slice];
597     memset(prof, 0, N_Cats * sizeof(nat));
598
599     res_time[slice] = mut_user_time();
600     
601     for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
602         for(s = 0; s < generations[g].n_steps; s++) {
603
604             /* skip over g0s0 if multi-generational */
605             if (RtsFlags.GcFlags.generations > 1 &&
606                 g == 0 && s == 0) continue;
607
608             if (RtsFlags.GcFlags.generations == 1) {
609                 bd = generations[g].steps[s].to_blocks;
610             } else {
611                 bd = generations[g].steps[s].blocks;
612             }
613
614             for (; bd != NULL; bd = bd->link) {
615
616                 p = bd->start;
617
618                 while (p < bd->free) {
619                     info = get_itbl((StgClosure *)p);
620                     type = Other;
621                     
622                     switch (info->type) {
623
624                     case CONSTR:
625                     case BCO:
626                         if (((StgClosure *)p)->header.info == &stg_DEAD_WEAK_info) {
627                             size = sizeofW(StgWeak);
628                             type = Other;
629                             break;
630                         }
631                         /* else, fall through... */
632                     case CONSTR_1_0:
633                     case CONSTR_0_1:
634                     case CONSTR_1_1:
635                     case CONSTR_0_2:
636                     case CONSTR_2_0:
637                         size = sizeW_fromITBL(info);
638                         type = Constr;
639                         break;
640                         
641                     case FUN_1_0:
642                     case FUN_0_1:
643                         size = sizeofW(StgHeader) + 1;
644                         goto fun;
645                     case FUN_1_1:
646                     case FUN_0_2:
647                     case FUN_2_0:
648                     case FUN:
649                         size = sizeW_fromITBL(info);
650                     fun:
651                         type = Fun;
652                         break;
653
654                     case THUNK_1_0:
655                     case THUNK_0_1:
656                     case THUNK_SELECTOR:
657                         size = sizeofW(StgHeader) + 2;
658                         goto thunk;
659                     case THUNK_1_1:
660                     case THUNK_0_2:
661                     case THUNK_2_0:
662                     case THUNK:
663                         size = sizeW_fromITBL(info);
664                     thunk:
665                         type = Thunk;
666                         break;
667
668                     case CAF_BLACKHOLE:
669                     case SE_CAF_BLACKHOLE:
670                     case SE_BLACKHOLE:
671                     case BLACKHOLE:
672                     case BLACKHOLE_BQ:
673                         size = sizeW_fromITBL(info);
674                         type = BlackHole;
675                         break;
676
677                     case AP:
678                         size = pap_sizeW((StgPAP *)p);
679                         type = Thunk;
680                         break;
681
682                     case PAP:
683                         size = pap_sizeW((StgPAP *)p);
684                         type = Fun;
685                         break;
686                         
687                     case ARR_WORDS:
688                         size = arr_words_sizeW(stgCast(StgArrWords*,p));
689                         type = Array;
690                         break;
691                         
692                     case MUT_ARR_PTRS:
693                     case MUT_ARR_PTRS_FROZEN:
694                         size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
695                         type = Array;
696                         break;
697                         
698                     case TSO:
699                         size = tso_sizeW((StgTSO *)p);
700                         type = Thread;
701                         break;
702                         
703                     case WEAK:
704                     case FOREIGN:
705                     case STABLE_NAME:
706                     case MVAR:
707                     case MUT_VAR:
708                     case MUT_CONS:
709                     case IND_PERM:
710                     case IND_OLDGEN_PERM:
711                         size = sizeW_fromITBL(info);
712                         type = Other;
713                         break;
714
715                     default:
716                         barf("updateResidencyGraph: strange closure "
717                              "%d", info->type );
718                     }
719
720                     prof[type] += size;
721                     p += size;
722                 }
723             }
724         }
725     }
726
727 }
728             
729 static void
730 updateResidencyGraph( void )
731 {
732     nat total, prev_total, i, max_res;
733     double time;
734     double time_scale = 1;
735     nat last_slice = next_slice-1;
736     double res_scale  = 1; /* in megabytes, doubles */
737     nat *prof;
738     nat width, height;
739     GdkPoint points[4];
740
741     gdk_draw_rectangle (res_pixmap,
742                         res_drawing_area->style->bg_gc[GTK_STATE_NORMAL],
743                         TRUE,
744                         0, 0,
745                         res_drawing_area->allocation.width,
746                         res_drawing_area->allocation.height);
747     
748     if (next_slice == 0) return;
749
750     time = res_time[last_slice];
751     while (time > time_scale) {
752         time_scale *= 2;
753     }
754
755     max_res = 0; 
756     for (i = 0; i < next_slice; i++) {
757         prof = res_prof[i];
758         total = prof[Thunk] + prof[Fun] + prof[Constr] +
759             prof[BlackHole] + prof[Array] + prof[Other];
760         if (total > max_res) {
761             max_res = total;
762         }
763     }
764     while (max_res > res_scale) {
765         res_scale *= 2;
766     }
767
768     height = res_drawing_area->allocation.height;
769     width  = res_drawing_area->allocation.width;
770
771     points[0].x = 0;
772     points[0].y = height;
773     points[1].y = height;
774     points[3].x = 0;
775     points[3].y = height;
776
777     gdk_gc_set_foreground(my_gc, &free_color);
778
779     prev_total = 0;
780     for (i = 0; i < next_slice; i++) {
781         prof = res_prof[i];
782         total = prof[Thunk] + prof[Fun] + prof[Constr] +
783             prof[BlackHole] + prof[Array] + prof[Other];
784         points[1].x = width * res_time[i] / time_scale;
785         points[2].x = points[1].x;
786         points[2].y = height - ((height * total) / res_scale);
787         gdk_draw_polygon(res_pixmap, my_gc, TRUE/*filled*/, points, 4);
788         points[3] = points[2];
789         points[0] = points[1];
790     }
791
792     gtk_ruler_set_range( GTK_RULER(res_vruler), 
793                          res_scale / ((1024*1024)/sizeof(W_)),
794                          0, 0,
795                          res_scale / ((1024*1024)/sizeof(W_)) );
796
797     gtk_ruler_set_range( GTK_RULER(res_hruler), 
798                          0, time_scale, 0, time_scale );
799
800
801     gtk_widget_draw( res_drawing_area, NULL );
802 }
803
804 #endif /* RTS_GTK_FRONTPANEL */