[m-rev.] diff: optimize MR_GC_check() and add --heap-margin-size option

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Feb 6 19:27:02 AEDT 2002


Estimated hours taken: 2
Branches: main

Two related changes for accurate GC with the MLDS->C back-end:
- Add a new runtime option --heap-margin-size for the
  threshold of free heap space before a collection is
  invoked, rather than the previous hack of reusing
  --heap-zone-size for that purpose.
- A minor optimization to reduce the cost of MR_gc_check():
  cache the value of MR_zone_end - MR_heap_margin_size.

runtime/mercury_wrapper.h:
runtime/mercury_wrapper.c:
runtime/mercury_memory.c:
	Add new variable `MR_heap_margin_size',
	and add a new runtime option `--heap-margin-size' to set it.

runtime/mercury_memory_zones.h:
	Add new field `gc_threshold' to the MR_MemoryZone struct.

runtime/mercury_memory_zones.c:
	Initialize the new field to point to the zone end minus
	MR_heap_margin_size.

runtime/mercury.h:
	Change MR_GC_check() to use the `gc_threshold' field.

Workspace: /home/earth/fjh/ws-earth4/mercury
Index: runtime/mercury.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury.h,v
retrieving revision 1.48
diff -u -d -r1.48 mercury.h
--- runtime/mercury.h	5 Feb 2002 09:14:55 -0000	1.48
+++ runtime/mercury.h	6 Feb 2002 08:20:16 -0000
@@ -514,8 +514,8 @@
 */
 #define MR_GC_check()							\
 	do {								\
-		if ((char *) MR_hp + MR_heap_zone_size >=		\
-		    (char *) MR_ENGINE(MR_eng_heap_zone)->MR_zone_end)	\
+		if ((char *) MR_hp >=					\
+		    MR_ENGINE(MR_eng_heap_zone)->gc_threshold)		\
 		{							\
 			MR_save_registers();				\
 			MR_garbage_collect();				\
Index: runtime/mercury_memory.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_memory.c,v
retrieving revision 1.24
diff -u -d -r1.24 mercury_memory.c
--- runtime/mercury_memory.c	24 Nov 2000 06:03:37 -0000	1.24
+++ runtime/mercury_memory.c	6 Feb 2002 07:59:15 -0000
@@ -164,6 +164,7 @@
 	MR_global_heap_zone_size = 0;
 	MR_debug_heap_size	 = 0;
 	MR_debug_heap_zone_size	 = 0;
+	MR_heap_margin_size	 = 0;
 #else
 	MR_heap_size		 = MR_round_up(MR_heap_size * 1024,
 					MR_unit);
@@ -182,6 +183,8 @@
 					MR_unit);
 	MR_debug_heap_zone_size	 = MR_round_up(MR_debug_heap_zone_size * 1024,
 					MR_unit);
+	/* Note that there's no need for the heap margin to be rounded up */
+	MR_heap_margin_size	 = MR_heap_margin_size * 1024;
 #endif
 	MR_detstack_size	 = MR_round_up(MR_detstack_size * 1024,
 					MR_unit);
Index: runtime/mercury_memory_zones.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_memory_zones.c,v
retrieving revision 1.16
diff -u -d -r1.16 mercury_memory_zones.c
--- runtime/mercury_memory_zones.c	13 Mar 2001 18:02:26 -0000	1.16
+++ runtime/mercury_memory_zones.c	6 Feb 2002 07:59:15 -0000
@@ -424,6 +424,9 @@
 	}
 #endif	/* MR_PROTECTPAGE */
 
+#if defined(NATIVE_GC) && defined(MR_HIGHLEVEL_CODE)
+	zone->gc_threshold = (char *) zone->MR_zone_end - MR_heap_margin_size;
+#endif
 
 	return zone;
 } /* end MR_construct_zone() */
Index: runtime/mercury_memory_zones.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_memory_zones.h,v
retrieving revision 1.10
diff -u -d -r1.10 mercury_memory_zones.h
--- runtime/mercury_memory_zones.h	26 Apr 2001 07:18:09 -0000	1.10
+++ runtime/mercury_memory_zones.h	6 Feb 2002 07:59:15 -0000
@@ -125,6 +125,15 @@
 #else
 	#define MR_zone_end	top
 #endif
+
+#if defined(NATIVE_GC) && defined(MR_HIGHLEVEL_CODE)
+	/*
+	** This field, which is only used for heap zones,
+	** points to MR_heap_margin_size bytes before MR_zone_end.
+	** It is used to decide when to do garbage collection.
+	*/
+	char *gc_threshold;	/* == MR_zone_end - MR_heap_margin_size */
+#endif
 };
 
 /*
Index: runtime/mercury_wrapper.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_wrapper.c,v
retrieving revision 1.98
diff -u -d -r1.98 mercury_wrapper.c
--- runtime/mercury_wrapper.c	5 Feb 2002 10:05:24 -0000	1.98
+++ runtime/mercury_wrapper.c	6 Feb 2002 07:59:15 -0000
@@ -82,6 +82,18 @@
 size_t		MR_generatorstack_zone_size =	  16;
 size_t		MR_cutstack_zone_size =		  16;
 
+/*
+** MR_heap_margin_size is used for accurate GC with the MLDS->C back-end.
+** It is used to decide when to actually do a garbage collection.
+** At each call to MR_GC_check(), which is normally done on entry
+** to each C function, we check whether there is less than this
+** amount of heap space still available, and if not, we call
+** MR_garbage_collect().
+** Like the sizes above, it is measured in kilobytes
+** (but we later multiply by 1024 to convert to bytes).
+*/
+size_t		MR_heap_margin_size =		  16;
+
 /* primary cache size to optimize for, in bytes */
 size_t		MR_pcache_size =	        8192;
 
@@ -735,6 +747,7 @@
 	MR_NONDETSTACK_REDZONE_SIZE,
 	MR_SOLUTIONS_HEAP_REDZONE_SIZE,
 	MR_TRAIL_REDZONE_SIZE,
+	MR_HEAP_MARGIN_SIZE,
 	MR_MDB_TTY,
 	MR_MDB_IN,
 	MR_MDB_OUT,
@@ -754,6 +767,7 @@
 	{ "nondetstack-redzone-size", 	1, 0, MR_NONDETSTACK_REDZONE_SIZE },
 	{ "solutions-heap-redzone-size",1, 0, MR_SOLUTIONS_HEAP_REDZONE_SIZE },
 	{ "trail-redzone-size", 	1, 0, MR_TRAIL_REDZONE_SIZE },
+	{ "heap-margin-size", 		1, 0, MR_HEAP_MARGIN_SIZE },
 	{ "mdb-tty", 			1, 0, MR_MDB_TTY },
 	{ "mdb-in", 			1, 0, MR_MDB_IN },
 	{ "mdb-out", 			1, 0, MR_MDB_OUT },
@@ -843,6 +857,13 @@
 				usage();
 
 			MR_trail_zone_size = size;
+			break;
+
+		case MR_HEAP_MARGIN_SIZE:
+			if (sscanf(MR_optarg, "%lu", &size) != 1)
+				usage();
+
+			MR_heap_margin_size = size;
 			break;
 
 		case 'i':
Index: runtime/mercury_wrapper.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_wrapper.h,v
retrieving revision 1.47
diff -u -d -r1.47 mercury_wrapper.h
--- runtime/mercury_wrapper.h	30 Jan 2002 14:51:10 -0000	1.47
+++ runtime/mercury_wrapper.h	6 Feb 2002 07:59:15 -0000
@@ -209,6 +209,9 @@
 extern	size_t		MR_generatorstack_zone_size;
 extern	size_t		MR_cutstack_zone_size;
 
+/* heap margin for MLDS->C accurate GC (documented in mercury_wrapper.c) */
+extern	size_t		MR_heap_margin_size;
+
 /* file names for the mdb debugging streams */
 extern	const char	*MR_mdb_in_filename;
 extern	const char	*MR_mdb_out_filename;
-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list