summaryrefslogtreecommitdiff
path: root/benchmarks/boehm-gc/patches/05-viengoos-scheduler.patch
blob: a9361d5ac5868186b8823868d5cc27b2e7d01579 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
#! /bin/sh
patch -p1 -f $* < $0
exit $?

Patch to support the Viengoos specific scheduler.  To disable it, set
GC_viengoos_scheduler to 0.


diff -urp -x config.guess -x config.sub -x 'aclocal*' -x configure -x Makefile.in gc.orig/allchblk.c gc/allchblk.c
--- gc.orig/allchblk.c	2007-06-07 02:40:07.000000000 +0200
+++ gc/allchblk.c	2009-02-24 03:07:28.000000000 +0100
@@ -117,7 +117,9 @@ void GC_print_hblkfreelist()
       while (h != 0) {
         hhdr = HDR(h);
         sz = hhdr -> hb_sz;
-    	GC_printf("\t%p size %lu ", h, (unsigned long)sz);
+    	GC_printf("\t%p-%p size %lu %smapped, ",
+		  h, (word) h + sz - 1, (unsigned long)sz,
+		  IS_MAPPED(hhdr) ? "" : "un");
     	total_free += sz;
         if (GC_is_black_listed(h, HBLKSIZE) != 0) {
              GC_printf("start black listed\n");
@@ -381,31 +383,142 @@ void GC_add_to_fl(struct hblk *h, hdr *h
 
 #ifdef USE_MUNMAP
 
+extern signed_word GC_bytes_found;
+extern int GC_viengoos_scheduler;
+
+#ifndef __gnu_hurd_viengoos__
+#define GC_available_bytes ((256 + 128) * 1024 * 1024)
+#endif
+
 /* Unmap blocks that haven't been recently touched.  This is the only way */
 /* way blocks are ever unmapped.					  */
 void GC_unmap_old(void)
 {
     struct hblk * h;
     hdr * hhdr;
-    word sz;
     unsigned short last_rec, threshold;
     int i;
 #   define UNMAP_THRESHOLD 6
-    
-    for (i = 0; i <= N_HBLK_FLS; ++i) {
+
+    uint64_t mapped_start = GC_get_heap_size() - GC_unmapped_bytes;
+
+   start:
+    if (GC_viengoos_scheduler
+	&& (GC_get_heap_size() - GC_unmapped_bytes
+	    < 7 * (GC_available_bytes / 8))) {
+      /* The number of mapped bytes is at most 7/8s the available
+	 memory.  That's good enough for now.  (Recall: the high-water
+	 mark is 15/16s the available memory.)  */
+      uint64_t mapped_now = GC_get_heap_size() - GC_unmapped_bytes;
+
+      extern char *program_name;
+      if (0)
+	printf ("%s("VG_THREAD_ID_FMT"): After unmapping %"PRId64" mb, "
+		"%"PRId64"mb used (%"PRId64"mb available)\n",
+		program_name, hurd_myself (),
+		(mapped_start - mapped_now) / 1024 / 1024,
+		mapped_now / 1024 / 1024,
+		(uint64_t) GC_available_bytes / 1024 / 1024);
+      return;
+    }
+
+    /* Start with the large blocks and work our way down.  */
+    for (i = N_HBLK_FLS; i >= 0 ; --i) {
       for (h = GC_hblkfreelist[i]; 0 != h; h = hhdr -> hb_next) {
         hhdr = HDR(h);
 	if (!IS_MAPPED(hhdr)) continue;
 	threshold = (unsigned short)(GC_gc_no - UNMAP_THRESHOLD);
 	last_rec = hhdr -> hb_last_reclaimed;
-	if ((last_rec > GC_gc_no || last_rec < threshold)
-	    && threshold < GC_gc_no /* not recently wrapped */) {
-          sz = hhdr -> hb_sz;
-	  GC_unmap((ptr_t)h, sz);
-	  hhdr -> hb_flags |= WAS_UNMAPPED;
-    	}
+
+	if (GC_viengoos_scheduler
+	    || ((last_rec > GC_gc_no || last_rec < threshold)
+		&& threshold < GC_gc_no /* not recently wrapped */)) {
+	    GC_unmap((ptr_t)h, hhdr -> hb_sz);
+	    hhdr -> hb_flags |= WAS_UNMAPPED;
+
+	    if (0)
+	      GC_printf ("Unmapped %p-%p(%x)\n",
+			 h, (struct hblk *)((word)h + hhdr -> hb_sz),
+			 hhdr -> hb_sz);
+
+	    if (GC_viengoos_scheduler) {
+	        GC_bool need_add = FALSE;
+
+	        /* Also unmap all adjacent blocks.  */
+		int dir;
+		for (dir = 0; dir < 2; dir ++)
+		  while (1) {
+		      struct hblk *next =
+			dir == 0 ? (struct hblk *)((word)h + hhdr -> hb_sz)
+			: GC_free_block_ending_at (h);
+
+		      if (! next)
+			  break;
+
+		      hdr * nexthdr;
+		      GET_HDR(next, nexthdr);
+
+		      if (0 == nexthdr || ! HBLK_IS_FREE(nexthdr))
+			  break;
+
+
+		      if (0)
+			GC_printf ("Merging %p-%p(%x) with %p-%p(%x)\n",
+				   h, (struct hblk *)((word)h
+						      + hhdr -> hb_sz),
+				   hhdr -> hb_sz,
+				   next,
+				   (struct hblk *)((word)next
+						   + nexthdr -> hb_sz),
+				   nexthdr -> hb_sz);
+
+
+		      /* Remove from free list.  */
+		      GC_remove_from_fl(nexthdr, FL_UNKNOWN);
+
+		      if (! need_add) {
+		          need_add = TRUE;
+			  GC_remove_from_fl(hhdr, i);
+		      }
+
+		      /* Unmap if not already unmapped.  */
+		      if (IS_MAPPED (nexthdr)) {
+			  GC_unmap((ptr_t)next, nexthdr -> hb_sz);
+			  nexthdr -> hb_flags |= WAS_UNMAPPED;
+		      }
+
+		      /* Lexically order.  */
+		      if (dir == 1) {
+		          void* t = hhdr;
+			  hhdr = nexthdr;
+			  nexthdr = t;
+
+			  t = h;
+			  h = next;
+			  next = t;
+		      }
+
+		      GC_remove_header(next);
+
+		      /* Unmap any gap in the middle */
+		      GC_unmap_gap((ptr_t)h, hhdr -> hb_sz,
+				   (ptr_t)next, nexthdr -> hb_sz);
+
+		      hhdr -> hb_sz += nexthdr -> hb_sz; 
+		  }
+
+		if (need_add)
+		  /* We removed H from the free lists because its size
+		     changed.  Add it back.  */
+		    GC_add_to_fl(h, hhdr);
+
+		/* We've munged with the lists.  Start from the
+		   beginning.  */
+		goto start;
+	    }
+	}
       }
-    }  
+    }
 }
 
 /* Merge all unmapped blocks that are adjacent to other free		*/
@@ -413,6 +526,12 @@ void GC_unmap_old(void)
 /* fully mapped or fully unmapped.					*/
 void GC_merge_unmapped(void)
 {
+    if (GC_viengoos_scheduler)
+        /* We did some merging in GC_unmap_old.  This is mostly extra
+	   work for us as it undoes the work we did and changes the
+	   number of unmapped pages!  */
+        return;
+
     struct hblk * h, *next;
     hdr * hhdr, *nexthdr;
     word size, nextsize;
@@ -600,12 +719,26 @@ GC_allochblk_nth(size_t sz, int kind, un
 
     size_needed = HBLKSIZE * OBJ_SZ_TO_BLOCKS(sz);
 
+    int try;
+    GC_bool saw_unmapped = FALSE;
+    for (try = 0; try < 2; try ++) {
+        if (try == 1 && ! saw_unmapped) break;
+
     /* search for a big enough block in free list */
 	hbp = GC_hblkfreelist[n];
 	for(; 0 != hbp; hbp = hhdr -> hb_next) {
 	    GET_HDR(hbp, hhdr);
+
 	    size_avail = hhdr->hb_sz;
 	    if (size_avail < size_needed) continue;
+
+	    /* First time through, ignore unmapped blocks (but note
+	       that we saw one that is large enough).  */
+	    if (try == 0 && ! IS_MAPPED(hhdr)) {
+	        saw_unmapped = TRUE;
+		continue;
+	    }
+
 	    if (size_avail != size_needed
 		&& !GC_use_entire_heap
 		&& !GC_dont_gc
@@ -642,7 +775,7 @@ GC_allochblk_nth(size_t sz, int kind, un
 	        next_size = (signed_word)(thishdr -> hb_sz);
 	        if (next_size < size_avail
 	          && next_size >= size_needed
-	          && !GC_is_black_listed(thishbp, (word)size_needed)) {
+		  && !GC_is_black_listed(thishbp, (word)size_needed)) {
 	          continue;
 	        }
 	      }
@@ -748,10 +881,12 @@ GC_allochblk_nth(size_t sz, int kind, un
 		/* hbp may be on the wrong freelist; the parameter n	*/
 		/* is important.					*/
 		hbp = GC_get_first_part(hbp, hhdr, size_needed, n);
-		break;
+		goto break_out;
 	    }
 	}
+    }
 
+   break_out:
     if (0 == hbp) return 0;
 	
     /* Add it to map of valid blocks */
diff -urp -x config.guess -x config.sub -x 'aclocal*' -x configure -x Makefile.in gc/alloc.c gc.new/alloc.c
--- gc/alloc.c	2007-06-22 04:40:30.000000000 +0200
+++ gc.new/alloc.c	2009-02-24 03:07:16.000000000 +0100
@@ -15,6 +15,13 @@
  *
  */
 
+#ifdef __gnu_hurd_viengoos__
+#include <viengoos/activity.h>
+#include <hurd/as.h>
+#endif
+#include <stdint.h>
+#include <sys/time.h>
+#include <assert.h>
 
 # include "private/gc_priv.h"
 
@@ -24,6 +31,8 @@
 #   include <sys/types.h>
 # endif
 
+#include "profile.h"
+
 /*
  * Separate free lists are maintained for different sized objects
  * up to MAXOBJBYTES.
@@ -223,9 +232,160 @@ void GC_clear_a_few_frames()
 /* limits used by blacklisting.						*/
 static word GC_collect_at_heapsize = (word)(-1);
 
+int GC_viengoos_scheduler = 1;
+uintptr_t GC_available_bytes = (uintptr_t) ((256 + 128) * 1024 * 1024);
+int GC_could_unmap;
+
+#define THRESHOLD ((uintptr_t) (15 * (GC_available_bytes / 16)))
+
+#ifdef __gnu_hurd_viengoos__
+static void *
+gather_stats (void *arg)
+{
+  pthread_detach (pthread_self ());
+
+  int period = 0;
+  for (;;)
+    {
+      struct vg_activity_info info;
+      error_t err = vg_activity_info (VG_ADDR_VOID, ACTIVITY,
+				      vg_activity_info_stats
+				      | vg_activity_info_pressure,
+				      period, &info);
+      assert_perror (err);
+
+      /* What I think I've allocated.  This it the amount of mapped
+	 heap; it does not include pages tables, etc.  */
+      uint64_t my_alloced = GC_get_heap_size () - GC_unmapped_bytes;
+      /* What the kernel thinks I've allocated.  It only includes
+	 physical memory.  */
+      uint64_t real_alloced = (uint64_t) (info.stats.stats[0].dirty
+					  + info.stats.stats[0].clean)
+	* PAGESIZE;
+
+      uint64_t old_avail = GC_available_bytes;
+
+      switch (info.event)
+	{
+	case vg_activity_info_stats:
+	  GC_available_bytes
+	    = (uintptr_t) info.stats.stats[0].available_local * PAGESIZE
+	    - (real_alloced - my_alloced);
+	  period = info.stats.stats[0].period + 1;
+	  break;
+
+	case vg_activity_info_pressure:
+	  /* PRESSURE.AMOUNT is negative.  */
+	  if (-(uintptr_t) info.pressure.amount * PAGESIZE < my_alloced)
+	    GC_available_bytes
+	      = my_alloced + (uintptr_t) info.pressure.amount * PAGESIZE;
+	  else
+	    /* Huh? */
+	    GC_available_bytes = 0;
+	  break;
+
+	default:
+	  panic ("Unknown event and unrequested event: %"PRIdPTR, info.event);
+	}
+
+      extern char *program_name;
+      if (0)
+	printf ("%s("VG_THREAD_ID_FMT"): %s: "
+		"%"PRId64" alloced: %"PRId64"mb, heap: %"PRId64"mb, "
+		"mapped:%"PRId64"mb, unmapped: %"PRId64"mb, "
+		"available: %"PRIdPTR"mb => %"PRIdPTR"mb, "
+		"low-water: %"PRIdPTR"mb\n",
+		program_name, hurd_myself (),
+		info.event == vg_activity_info_stats
+		? "Period" : DEBUG_BOLD ("PRESSURE"),
+		(uint64_t) (info.event == vg_activity_info_stats
+			    ? period : info.pressure.amount),
+		(uint64_t) GC_adj_bytes_allocd() / 1024 / 1024,
+		(uint64_t) GC_get_heap_size () / 1024 / 1024,
+		(uint64_t) (GC_get_heap_size () - GC_unmapped_bytes)
+		/ 1024 / 1024,
+		(uint64_t) GC_unmapped_bytes / 1024 / 1024,
+		(uint64_t) old_avail / 1024 / 1024,
+		(uint64_t) GC_available_bytes / 1024 / 1024,
+		(uint64_t) THRESHOLD / 1024 / 1024);
+      if (info.event == vg_activity_info_pressure)
+	printf ("pages to free: %d mb\n",
+		info.pressure.amount * (PAGESIZE / 1024) / 1024);
+    }
+}
+#endif
+
 /* Have we allocated enough to amortize a collection? */
 GC_bool GC_should_collect(void)
 {
+#ifndef USE_MUNMAP
+# define GC_unmapped_bytes 0
+#endif
+
+  if (GC_viengoos_scheduler)
+    {
+      int alloced = GC_adj_bytes_allocd();
+#ifdef __gnu_hurd_viengoos__
+      static int init;
+      if (! init)
+	{
+	  pthread_t tid;
+	  error_t err = pthread_create (&tid, NULL, gather_stats, NULL);
+	  assert_perror (err);
+
+	  init = 1;
+	}
+#endif
+
+      /* Do some clean up if the mapped memory is more than 15/16s the
+	 available memory.  We choose 15/16s as we need to consider
+	 meta-data overhead and as we really want to avoid the system
+	 pager kicking in.  */
+      GC_bool r = GC_get_heap_size() - GC_unmapped_bytes > THRESHOLD;
+
+      if (r && (GC_get_heap_size () - GC_unmapped_bytes - alloced
+		> GC_available_bytes / 3)){
+	/* The number of unused mapped bytes is greater than a third
+	   of the total available memory.  Before doing a GC, try to
+	   unmap some free mapped pages.  */
+	  GC_unmap_old ();
+
+	  r = GC_get_heap_size() - GC_unmapped_bytes > THRESHOLD;
+      }
+
+      if (r) {
+	  if (alloced < GC_available_bytes / 100)
+	      /* If we have not allocated anything since the last
+		 collection, don't do a collection.  */
+	      return FALSE;
+
+	  static int warning;
+	  /* Only print this once per-gc.  GC_allochblk_nth calls this
+	     function repeatedly without immediately following up with
+	     a GC.  */
+	  if (warning != GC_gc_no) {
+	      warning = GC_gc_no;
+	      extern char *program_name;
+	      if (0)
+		printf ("%s("VG_THREAD_ID_FMT"): Scheduling GC #%u: "
+			"alloced: %"PRId64"mb, heap: %"PRId64"mb, "
+			"mapped: %"PRId64"mb, unmapped: %"PRId64"mb, "
+			"available: %"PRIdPTR"mb, low-water: %"PRIdPTR"mb\n",
+			program_name, hurd_myself (),
+			(uint64_t) GC_gc_no,
+			(uint64_t) alloced / 1024 / 1024,
+			(uint64_t) GC_get_heap_size () / 1024 / 1024,
+			(uint64_t) (GC_get_heap_size () - GC_unmapped_bytes)
+			/ 1024 / 1024,
+			(uint64_t) GC_unmapped_bytes / 1024 / 1024,
+			(uint64_t) GC_available_bytes / 1024 / 1024,
+			(uint64_t) THRESHOLD / 1024 / 1024);
+	  }
+      }
+
+      return r;
+    }
+  else
     return(GC_adj_bytes_allocd() >= min_bytes_allocd()
 	   || GC_heapsize >= GC_collect_at_heapsize);
 }
@@ -256,6 +416,8 @@ void GC_maybe_gc(void)
             n_partial_gcs = 0;
             return;
         } else {
+	  profile_region (GC_TIMER);
+
 #   	  ifdef PARALLEL_MARK
 	    GC_wait_for_reclaim();
 #   	  endif
@@ -266,6 +428,7 @@ void GC_maybe_gc(void)
      		  (unsigned long)GC_gc_no+1,
 		  (long)GC_bytes_allocd);
 	    }
+
 	    GC_promote_black_lists();
 	    (void)GC_reclaim_all((GC_stop_func)0, TRUE);
 	    GC_clear_marks();
@@ -275,7 +438,11 @@ void GC_maybe_gc(void)
           } else {
             n_partial_gcs++;
           }
+
+	  profile_region_end ();
 	}
+
+	profile_region (GC_TIMER);
         /* We try to mark with the world stopped.	*/
         /* If we run out of time, this turns into	*/
         /* incremental marking.			*/
@@ -294,6 +461,8 @@ void GC_maybe_gc(void)
 	        GC_n_attempts++;
 	    }
 	}
+
+	profile_region_end ();
     }
 }
 
@@ -325,6 +494,10 @@ GC_bool GC_try_to_collect_inner(GC_stop_
 	   "Initiating full world-stop collection %lu after %ld allocd bytes\n",
 	   (unsigned long)GC_gc_no+1, (long)GC_bytes_allocd);
     }
+
+    GC_bool ret = TRUE;
+    profile_region (GC_TIMER);
+
     GC_promote_black_lists();
     /* Make sure all blocks have been reclaimed, so sweep routines	*/
     /* don't see cleared mark bits.					*/
@@ -337,7 +510,8 @@ GC_bool GC_try_to_collect_inner(GC_stop_
  	if ((GC_find_leak || stop_func != GC_never_stop_func)
 	    && !GC_reclaim_all(stop_func, FALSE)) {
 	    /* Aborted.  So far everything is still consistent.	*/
-	    return(FALSE);
+	    ret = FALSE;
+	    goto out;
 	}
     GC_invalidate_mark_state();  /* Flush mark stack.	*/
     GC_clear_marks();
@@ -354,7 +528,8 @@ GC_bool GC_try_to_collect_inner(GC_stop_
 	GC_unpromote_black_lists();
       } /* else we claim the world is already still consistent.  We'll 	*/
         /* finish incrementally.					*/
-      return(FALSE);
+      ret = FALSE;
+      goto out;
     }
     GC_finish_collection();
     if (GC_print_stats) {
@@ -362,7 +537,10 @@ GC_bool GC_try_to_collect_inner(GC_stop_
         GC_log_printf("Complete collection took %lu msecs\n",
                   MS_TIME_DIFF(current_time,start_time));
     }
-    return(TRUE);
+
+ out:
+    profile_region_end ();
+    return ret;
 }
 
 
@@ -390,6 +568,8 @@ void GC_collect_a_little_inner(int n)
     
     if (GC_dont_gc) return;
     if (GC_incremental && GC_collection_in_progress()) {
+	profile_region (GC_TIMER);
+
     	for (i = GC_deficit; i < GC_RATE*n; i++) {
     	    if (GC_mark_some((ptr_t)0)) {
     	        /* Need to finish a collection */
@@ -415,6 +595,8 @@ void GC_collect_a_little_inner(int n)
     	}
     	if (GC_deficit > 0) GC_deficit -= GC_RATE*n;
 	if (GC_deficit < 0) GC_deficit = 0;
+
+	profile_region_end ();
     } else {
         GC_maybe_gc();
     }
@@ -904,6 +1086,8 @@ GC_bool GC_expand_hp_inner(word n)
     /* Force GC before we are likely to allocate past expansion_slop */
       GC_collect_at_heapsize =
          GC_heapsize + expansion_slop - 2*MAXHINCR*HBLKSIZE;
+      GC_collect_at_heapsize =
+         GC_heapsize + expansion_slop - 2*MAXHINCR*HBLKSIZE;
 #     if defined(LARGE_CONFIG)
         if (GC_collect_at_heapsize < GC_heapsize /* wrapped */)
          GC_collect_at_heapsize = (word)(-1);
@@ -1021,3 +1205,410 @@ ptr_t GC_allocobj(size_t gran, int kind)
     
     return(*flh);
 }
+
+
+void
+GC_dump_stats (void)
+{
+  extern uint64_t gc_map_time;
+
+  printf ("GC: "
+#ifdef THREADS
+	  "multi-threaded"
+#else
+	  "single threaded"
+#endif
+	  ", %s, "
+#ifndef PARALLEL_MARK
+	  "non-"
+#endif
+	  "parallel mark, dirty bits %smaintained\n",
+	  TRUE_INCREMENTAL ? "true incremental" :
+	  GC_incremental ? "generational" : "stop the world",
+	  GC_dirty_maintained ? "" : "not ");
+  printf("%d collections\n", (int) GC_gc_no);
+  printf("Heap size: %d (%d kb)\n",
+	 (int) GC_get_heap_size(), (int) (GC_get_heap_size() / 1024));
+  printf("Total bytes allocated: %d (%d kb)\n",
+	 (int) GC_get_total_bytes (), (int) (GC_get_total_bytes () / 1024));
+
+  profile_stats_dump ();
+}
+
+#ifndef __gnu_hurd_viengoos__
+/* profile.c - Profiling support implementation.
+   Copyright (C) 2008 Free Software Foundation, Inc.
+   Written by Neal H. Walfield <neal@gnu.org>.
+
+   This file is part of the GNU Hurd.
+
+   The GNU Hurd is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 3 of the
+   License, or (at your option) any later version.
+
+   The GNU Hurd is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifdef RM_INTERN
+# include "../viengoos/zalloc.h"
+#else
+# include <stdlib.h>
+#endif
+
+#include "profile.h"
+#include <hurd/ihash.h>
+#include <stddef.h>
+#include <string.h>
+
+#ifdef __gnu_hurd_viengoos__
+# include <viengoos/bits/sys.h>
+# include <s-printf.h>
+# include <hurd/thread.h>
+# include <viengoos/misc.h>
+#else
+# include <pthread.h>
+# include <sys/time.h>
+# ifndef s_printf
+#  define s_printf printf
+# endif
+# ifndef PAGESIZE
+/* We only use this to determine a buffer size.  It needs to be a
+   constant.  */
+#  define PAGESIZE 4096
+# endif
+# ifndef panic
+#  define panic(fmt, ...)						\
+  do									\
+    {									\
+      printf ("%s:%d: " fmt "\n", __func__, __LINE__ , ##__VA_ARGS__);	\
+      abort ();								\
+    }									\
+  while (0)
+# endif
+#include <stdio.h>
+# ifndef do_debug
+#  define do_debug(x) if (0)
+# endif
+#endif
+
+static inline uint64_t
+now (void)
+{
+#ifdef __gnu_hurd_viengoos__
+  return VG_TIME->ns_since_boot * 1000;
+#else
+  struct timeval t;
+  struct timezone tz;
+
+  if (gettimeofday (&t, &tz) == -1)
+    return 0;
+  return (t.tv_sec * 1000000ULL + t.tv_usec);
+#endif
+}
+
+#define CALL_SITES 100
+struct call_site
+{
+  const char *name;
+  const char *name2;
+  uint64_t time;
+  uint64_t start;
+  int calls;
+  int nested_calls;
+  int pending;
+};
+
+/* The number of threads we support.  */
+#define THREADS 4
+static int thread_count;
+
+struct profile_block
+{
+#ifdef __gnu_hurd_viengoos__
+# define MYSELF() hurd_myself ()
+  vg_thread_id_t tid;
+#else
+# define MYSELF() pthread_self ()
+  pthread_t tid;
+#endif
+
+  /* When we started profiling.  */
+  uint64_t epoch;
+  uint64_t profiled_time;
+
+  uint64_t calls;
+  /* Number of extant profiling calls.  We only update profiled_time
+     if EXTANT is 0.  The result is that the time spent profiling is
+     correct, and the percent of the time profile that a function has
+     been is more meaningful.  */
+  int extant;
+
+  int init_done;
+
+  struct call_site call_sites[CALL_SITES];
+  int call_sites_used;
+
+  struct hurd_ihash sites_hash;
+#ifndef RM_INTERN
+  char sites_hash_buffer[PAGESIZE];
+#endif
+};
+
+static struct profile_block profile_blocks[THREADS];
+
+/* Return the profile block associated with the calling thread.  If
+   there is none, initialize one.  */
+static struct profile_block *
+profile_block (void)
+{
+  int i;
+  for (i = 0; i < thread_count; i ++)
+    if (profile_blocks[i].tid == MYSELF())
+      return &profile_blocks[i];
+
+  /* New thread.  */
+
+  i = __sync_fetch_and_add (&thread_count, 1);
+  if (i >= THREADS)
+    panic ("More threads than profile space available!");
+
+  struct profile_block *pb = &profile_blocks[i];
+
+  pb->tid = MYSELF ();
+
+  size_t size;
+  void *buffer;
+#ifdef RM_INTERN
+  size = hurd_ihash_buffer_size (CALL_SITES, false, 0);
+  /* Round up to a multiple of the page size.  */
+  size = (size + PAGESIZE - 1) & ~(PAGESIZE - 1);
+
+  buffer = (void *) zalloc (size);
+#else
+  size = sizeof (pb->sites_hash_buffer);
+  buffer = pb->sites_hash_buffer;
+#endif
+  if (! buffer)
+    panic ("Failed to allocate memory for object hash!\n");
+
+  memset (buffer, 0, size);
+
+  hurd_ihash_init_with_buffer (&pb->sites_hash, false,
+			       HURD_IHASH_NO_LOCP,
+			       buffer, size);
+
+  pb->epoch = now ();
+  pb->init_done = true;
+
+  return pb;
+}
+
+/* XXX: This implementation assumes that we are single threaded!  In
+   the case of Viengoos, this is essentially true: all relevant
+   functionality is with the kernel lock held.  We also don't
+   currently support nested calls.  */
+
+#undef profile_stats_dump
+void
+profile_stats_dump (void)
+{
+  uint64_t n = now ();
+
+  char digit_whitespace[11];
+  memset (digit_whitespace, ' ', sizeof (digit_whitespace));
+  digit_whitespace[sizeof (digit_whitespace) - 1] = 0;
+
+  char *dws (uint64_t number)
+  {
+    int w = 0;
+    if (number < 0)
+      w ++;
+
+    while (number != 0)
+      {
+	w ++;
+	number /= 10;
+      }
+    if (w == 0)
+      w = 1;
+
+    if (w > sizeof (digit_whitespace))
+      return "";
+    else
+      return &digit_whitespace[w];
+  }
+
+  int t;
+  for (t = 0; t < thread_count; t ++)
+    {
+      struct profile_block *pb = &profile_blocks[t];
+      if (! pb->init_done)
+	continue;
+
+      int width = 0;
+      int count = 0;
+
+      int i;
+      for (i = 0; i < pb->call_sites_used; i ++)
+	{
+	  if (pb->call_sites[i].calls)
+	    count ++;
+	  if (pb->call_sites[i].calls)
+	    {
+	      int w = 0;
+	      if (pb->call_sites[i].name)
+		w += strlen (pb->call_sites[i].name);
+	      if (pb->call_sites[i].name2)
+		w += strlen (pb->call_sites[i].name2);
+	      if (pb->call_sites[i].name && pb->call_sites[i].name2)
+		w += 2;
+	      if (w > width)
+		width = w;
+	    }
+	}
+
+      char spaces[width + 1];
+      memset (spaces, ' ', sizeof (spaces));
+      spaces[width] = 0;
+
+      /* Do a simple bubble sort.  */
+      int order[count];
+
+      int j = 0;
+      for (i = 0; i < pb->call_sites_used; i ++)
+	if (pb->call_sites[i].calls)
+	  order[j ++] = i;
+
+      for (i = 0; i < count; i ++)
+	for (j = 0; j < count - 1; j ++)
+	  if (pb->call_sites[order[j]].time
+	      < pb->call_sites[order[j + 1]].time)
+	    {
+	      int t = order[j];
+	      order[j] = order[j + 1];
+	      order[j + 1] = t;
+	    }
+
+      s_printf ("Thread: "VG_THREAD_ID_FMT"\n", pb->tid);
+      for (j = 0; j < count; j ++)
+	{
+	  i = order[j];
+	  if (pb->call_sites[i].calls)
+	    s_printf ("%s%s%s%s%s: %s%d calls (%d nested),\t%s%"PRId64" ms,"
+		      "\t%"PRId64".%d us/call,\t%d%% total,\t%d%% profiled\n",
+		      &spaces[strlen (pb->call_sites[i].name ?: "")
+			      + strlen (pb->call_sites[i].name2 ?: "")
+			      + (pb->call_sites[i].name
+				 && pb->call_sites[i].name2 ? 2 : 0)],
+		      pb->call_sites[i].name ?: "",
+		      pb->call_sites[i].name && pb->call_sites[i].name2
+		      ? "(" : "",
+		      pb->call_sites[i].name2 ?: "",
+		      pb->call_sites[i].name && pb->call_sites[i].name2
+		      ? ")" : "",
+		      dws (pb->call_sites[i].calls), pb->call_sites[i].calls,
+		      pb->call_sites[i].nested_calls,
+		      dws (pb->call_sites[i].time / 1000),
+		      pb->call_sites[i].time / 1000,
+		      pb->call_sites[i].time / pb->call_sites[i].calls,
+		      (int) ((10 * pb->call_sites[i].time)
+			     / pb->call_sites[i].calls) % 10,
+		      (int) ((100 * pb->call_sites[i].time) / (n - pb->epoch)),
+		      (int) ((100 * pb->call_sites[i].time)
+			     / pb->profiled_time));
+	}
+
+      s_printf ("profiled time: %"PRId64" ms (%d%%), calls: %"PRId64"\n",
+		pb->profiled_time / 1000,
+		(int) ((100 * pb->profiled_time) / (n - pb->epoch)),
+		pb->calls);
+      s_printf ("total time: %"PRId64" ms\n", (n - pb->epoch) / 1000);
+    }
+}
+
+#undef profile_start
+void
+profile_start (uintptr_t id, const char *name, const char *name2)
+{
+  struct profile_block *pb = profile_block ();
+  if (! pb->init_done)
+    return;
+
+  struct call_site *site = hurd_ihash_find (&pb->sites_hash, id);
+  if (! site)
+    {
+      site = &pb->call_sites[pb->call_sites_used ++];
+      if (pb->call_sites_used == CALL_SITES)
+	panic ("Out of profile space.");
+
+      error_t err = hurd_ihash_add (&pb->sites_hash, id, site);
+      if (err)
+	panic ("Failed to add to hash: %d.", err);
+
+      if (name)
+	site->name = name;
+      else
+	{
+#ifdef __gnu_hurd_viengoos__
+	  site->name = vg_method_id_string (id);
+#else
+	  site->name = "unknown";
+#endif
+	}
+      site->name2 = name2;
+    }
+
+  pb->extant ++;
+
+  site->pending ++;
+  if (site->pending == 1)
+    site->start = now ();
+}
+
+#undef profile_end
+void
+profile_end (uintptr_t id)
+{
+  uint64_t n = now ();
+
+  struct profile_block *pb = profile_block ();
+  if (! pb->init_done)
+    return;
+
+  struct call_site *site = hurd_ihash_find (&pb->sites_hash, id);
+  if (! site)
+    panic ("profile_end called without corresponding profile_begin (%p)!",
+	   (void *) id);
+
+  if (! site->pending)
+    panic ("profile_end called but no extant profile_start!");
+
+  pb->extant --;
+
+  site->pending --;
+  if (site->pending == 0)
+    {
+      site->time += n - site->start;
+
+      if (pb->extant == 0)
+	pb->profiled_time += n - site->start;
+
+      site->calls ++;
+      pb->calls ++;
+
+      do_debug (5)
+	if (pb->calls % 100000 == 0)
+	  profile_stats_dump ();
+    }
+  else
+    site->nested_calls ++;
+}
+
+#endif
diff -x config.guess -x config.sub -x 'aclocal*' -x configure -x Makefile.in -rup gc.orig/os_dep.c gc/os_dep.c
--- gc.orig/os_dep.c	2007-06-29 21:17:44.000000000 +0200
+++ gc/os_dep.c	2009-02-21 02:33:55.000000000 +0100
@@ -2064,6 +2064,17 @@ ptr_t GC_unmap_end(ptr_t start, size_t b
     return end_addr;
 }
 
+#ifdef __gnu_hurd_viengoos__
+#include <viengoos/addr.h>
+#include <hurd/as.h>
+
+extern vg_addr_t gc_activity __attribute__ ((weak));
+#define ACTIVITY (&gc_activity ? gc_activity : VG_ADDR_VOID)
+
+#endif
+
+#include "profile.h"
+
 /* Under Win32/WinCE we commit (map) and decommit (unmap)	*/
 /* memory using	VirtualAlloc and VirtualFree.  These functions	*/
 /* work on individual allocations of virtual memory, made	*/
@@ -2077,10 +2088,12 @@ ptr_t GC_unmap_end(ptr_t start, size_t b
 /* round the endpoints in both places.				*/
 void GC_unmap(ptr_t start, size_t bytes)
 {
+  profile_region (MAP_TIMER);
+
     ptr_t start_addr = GC_unmap_start(start, bytes);
     ptr_t end_addr = GC_unmap_end(start, bytes);
     word len = end_addr - start_addr;
-    if (0 == start_addr) return;
+    if (0 == start_addr) goto out;
 #   if defined(MSWIN32) || defined(MSWINCE)
       while (len != 0) {
           MEMORY_BASIC_INFORMATION mem_info;
@@ -2095,6 +2108,9 @@ void GC_unmap(ptr_t start, size_t bytes)
 	  start_addr += free_len;
 	  len -= free_len;
       }
+#   elif defined(__gnu_hurd_viengoos__)
+      madvise (start, bytes, POSIX_MADV_DONTNEED);
+      GC_unmapped_bytes += len;
 #   else
       /* We immediately remap it to prevent an intervening mmap from	*/
       /* accidentally grabbing the same address space.			*/
@@ -2107,19 +2123,25 @@ void GC_unmap(ptr_t start, size_t bytes)
       }
       GC_unmapped_bytes += len;
 #   endif
+
+ out:
+      profile_region_end ();
 }
 
 
 void GC_remap(ptr_t start, size_t bytes)
 {
+  profile_region (MAP_TIMER);
+
     ptr_t start_addr = GC_unmap_start(start, bytes);
     ptr_t end_addr = GC_unmap_end(start, bytes);
     word len = end_addr - start_addr;
 
+    if (0 == start_addr) goto out;
+
 #   if defined(MSWIN32) || defined(MSWINCE)
       ptr_t result;
 
-      if (0 == start_addr) return;
       while (len != 0) {
           MEMORY_BASIC_INFORMATION mem_info;
 	  GC_word alloc_len;
@@ -2137,11 +2159,14 @@ void GC_remap(ptr_t start, size_t bytes)
 	  start_addr += alloc_len;
 	  len -= alloc_len;
       }
+#   elif defined(__gnu_hurd_viengoos__)
+      /* Nothing to do.  We already discarded it and the next access
+	 will fault it in.  */
+      GC_unmapped_bytes -= len;
 #   else
       /* It was already remapped with PROT_NONE. */
       int result; 
 
-      if (0 == start_addr) return;
       result = mprotect(start_addr, len,
 		        PROT_READ | PROT_WRITE | OPT_PROT_EXEC);
       if (result != 0) {
@@ -2152,6 +2177,9 @@ void GC_remap(ptr_t start, size_t bytes)
       }
       GC_unmapped_bytes -= len;
 #   endif
+
+ out:
+      profile_region_end ();
 }
 
 /* Two adjacent blocks have already been unmapped and are about to	*/
@@ -2160,6 +2188,8 @@ void GC_remap(ptr_t start, size_t bytes)
 /* unmapped due to alignment constraints.				*/
 void GC_unmap_gap(ptr_t start1, size_t bytes1, ptr_t start2, size_t bytes2)
 {
+  profile_region (MAP_TIMER);
+
     ptr_t start1_addr = GC_unmap_start(start1, bytes1);
     ptr_t end1_addr = GC_unmap_end(start1, bytes1);
     ptr_t start2_addr = GC_unmap_start(start2, bytes2);
@@ -2170,7 +2200,7 @@ void GC_unmap_gap(ptr_t start1, size_t b
     GC_ASSERT(start1 + bytes1 == start2);
     if (0 == start1_addr) start_addr = GC_unmap_start(start1, bytes1 + bytes2);
     if (0 == start2_addr) end_addr = GC_unmap_end(start1, bytes1 + bytes2);
-    if (0 == start_addr) return;
+    if (0 == start_addr) goto out;
     len = end_addr - start_addr;
 #   if defined(MSWIN32) || defined(MSWINCE)
       while (len != 0) {
@@ -2190,6 +2220,9 @@ void GC_unmap_gap(ptr_t start1, size_t b
       if (len != 0 && munmap(start_addr, len) != 0) ABORT("munmap failed");
       GC_unmapped_bytes += len;
 #   endif
+
+ out:
+      profile_region_end ();
 }
 
 #endif /* USE_MUNMAP */
--- /dev/null	2009-02-13 16:50:04.502633359 +0100
+++ gc/profile.h	2009-02-22 03:37:53.000000000 +0100
@@ -0,0 +1,53 @@
+/* profile.h - Profiling support interface.
+   Copyright (C) 2008 Free Software Foundation, Inc.
+   Written by Neal H. Walfield <neal@gnu.org>.
+
+   This file is part of the GNU Hurd.
+
+   The GNU Hurd is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 3 of the
+   License, or (at your option) any later version.
+
+   The GNU Hurd is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <stdint.h>
+
+#define GC_TIMER "gc"
+#define MAP_TIMER "map"
+
+#ifndef NPROFILE
+
+/* Start a timer for the profile site ID (this must be unique per
+   site, can be the function's address).  NAME is the symbolic
+   name.  */
+extern void profile_start (uintptr_t id, const char *name, const char *name2);
+
+/* End the timer for the profile site ID.  */
+extern void profile_end (uintptr_t id);
+
+extern void profile_stats_dump (void);
+
+#else
+
+#define profile_start(id, name, name2) do { } while (0)
+#define profile_end(id) do { } while (0)
+#define profile_stats_dump() do { } while (0)
+
+#endif
+
+#define profile_region(__pr_name)					\
+  {									\
+    uintptr_t __pr_id_ = (uintptr_t) __func__ + (uintptr_t) (__pr_name); \
+    profile_start (__pr_id_, __func__, (__pr_name))
+
+#define profile_region_end()			\
+    profile_end (__pr_id_);			\
+  }