[m-rev.] for review: thread-local allocation

Peter Wang wangp at students.cs.mu.OZ.AU
Thu Apr 20 19:58:32 AEST 2006


Estimated hours taken: 4
Branches: main

This patch enables the thread-local memory allocation feature of Boehm GC in
parallel grades on Linux hosts; without it, performance is terrible.  It may
work on other operating systems using pthreads, but I haven't tested.

Mmake.common.in:
configure.in:
boehm_gc/Makefile:
	Build Boehm GC with the THREAD_LOCAL_ALLOC preprocessor symbol in
	parallel grades on Linux.

	Add -DGC_REDIRECT_TO_LOCAL to CFLAGS_FOR_THREADS to redirect
	calls to GC_malloc() to GC_local_malloc().

runtime/mercury_memory.c:
	Work around a bug in the garbage collector when using thread-local
	allocation.  Hans Boehm says, "A size zero GC_local_malloc allocation
	currently just returns a fixed address outside the real heap."
	This leads to a crash when we later try to GC_free() the address.


The size zero allocation is due to calls like
"MR_GC_NEW_ARRAY(MR_TypeInfo, expand_info->arity);" in
mercury_ml_expand_body.h, where the arity is zero.  There may be others.
I've emailed the gclist today about the problem.


Index: Mmake.common.in
===================================================================
RCS file: /home/mercury1/repository/mercury/Mmake.common.in,v
retrieving revision 1.87
diff -u -r1.87 Mmake.common.in
--- Mmake.common.in	24 Feb 2006 07:11:07 -0000	1.87
+++ Mmake.common.in	19 Apr 2006 13:25:35 -0000
@@ -146,6 +146,10 @@
 # Options to pass to the C compiler when building Boehm-GC.
 BOEHM_CFLAGS = @ENABLE_BOEHM_LARGE_CONFIG@
 
+# Additional options to pass to the C compiler when building Boehm-GC for
+# threads.
+BOEHM_CFLAGS_FOR_THREADS = @ENABLE_BOEHM_THREAD_LOCAL_ALLOC@
+
 # Do we want to deal with intermodule information when building the library?
 # By default yes, since this way we note immediately when intermodule
 # optimization breaks, and always at installation time, since we must install
Index: configure.in
===================================================================
RCS file: /home/mercury1/repository/mercury/configure.in,v
retrieving revision 1.451
diff -u -r1.451 configure.in
--- configure.in	13 Apr 2006 03:59:22 -0000	1.451
+++ configure.in	19 Apr 2006 13:27:54 -0000
@@ -2474,6 +2474,7 @@
 # Note that changes here may require changes in scripts/ml.in.
 LDFLAGS_FOR_THREADS=
 LD_LIBFLAGS_FOR_THREADS=
+ENABLE_BOEHM_THREAD_LOCAL_ALLOC=
 case "$host" in
 	*solaris*)
 		CFLAGS_FOR_THREADS="-DMR_THREAD_SAFE -DSOLARIS_THREADS \
@@ -2486,8 +2487,9 @@
 		# you may also need to make sure that you don't
 		# pass -ansi to gcc.
 		CFLAGS_FOR_THREADS="-DMR_THREAD_SAFE -DLINUX_THREADS \
-			-D_THREAD_SAFE -D_REENTRANT"
+			-D_THREAD_SAFE -D_REENTRANT -DGC_REDIRECT_TO_LOCAL"
 		THREAD_LIBS="-lpthread -ldl"
+		ENABLE_BOEHM_THREAD_LOCAL_ALLOC="-DTHREAD_LOCAL_ALLOC"
 		;;
 
 	*cygwin*)
@@ -2562,6 +2564,7 @@
 AC_SUBST(THREAD_LIBS)
 AC_SUBST(LDFLAGS_FOR_THREADS)
 AC_SUBST(LD_LIBFLAGS_FOR_THREADS)
+AC_SUBST(ENABLE_BOEHM_THREAD_LOCAL_ALLOC)
 
 #-----------------------------------------------------------------------------#
 
Index: boehm_gc/Makefile
===================================================================
RCS file: /home/mercury1/repository/mercury/boehm_gc/Makefile,v
retrieving revision 1.61
diff -u -r1.61 Makefile
--- boehm_gc/Makefile	13 Feb 2006 07:14:36 -0000	1.61
+++ boehm_gc/Makefile	20 Apr 2006 05:51:53 -0000
@@ -84,6 +84,11 @@
 CFLAGS= -I$(srcdir)/include \
 	-DSILENT -DNO_DEBUGGING -DNO_EXECUTE_PERMISSION \
 	$(BOEHM_CFLAGS) $(CFLAGS_FOR_PIC) $(DLL_CFLAGS) $(EXTRA_CFLAGS)
+
+ifeq ($(GC_GRADE),par_gc)
+CFLAGS += $(BOEHM_CFLAGS_FOR_THREADS)
+endif
+
 # Note that the `mgnuc' script also passes -DNO_SIGNALS, unless
 # profiling was enabled (see comments in runtime/mercury_prof_mem.h for why).
 # We need $(CFLAGS_FOR_PIC) because we might be building a shared library.
Index: runtime/mercury_memory.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_memory.c,v
retrieving revision 1.35
diff -u -r1.35 mercury_memory.c
--- runtime/mercury_memory.c	20 Jul 2004 04:41:22 -0000	1.35
+++ runtime/mercury_memory.c	20 Apr 2006 08:40:12 -0000
@@ -325,6 +325,15 @@
 	void	*ptr;
 
 #ifdef	MR_CONSERVATIVE_GC
+#ifdef	GC_REDIRECT_TO_LOCAL
+	/*
+	** Work around a bug in Boehm GC <= 6.7.
+	** It appears this problem is not in 7.0alpha5.
+	*/
+	if (num_bytes == 0) {
+		return NULL;
+	}
+#endif
 	ptr = GC_MALLOC(num_bytes);
 #else
 	ptr = malloc(num_bytes);
--------------------------------------------------------------------------
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