[MLton-devel] very unfortunate behavior when profiling allocations

Stephen Weeks MLton@mlton.org
Wed, 2 Jul 2003 17:44:35 -0700


> The timeout idea is a nice one---I'm afraid we didn't think of it
> amidst the hurly-burly of deadlines.  Thanks for the tip.

Ahh.  Too bad.  This wasn't for the ICFP contest was it?

Anyways, the fix was quite simple.  It'll be in the next release.
Here's the patch to the runtime if you need it sooner.

% cvs diff -r 1.144 -r 1.145 runtime/gc.c
Index: runtime/gc.c
===================================================================
RCS file: /cvsroot/mlton/mlton/runtime/gc.c,v
retrieving revision 1.144
retrieving revision 1.145
diff -u -r1.144 -r1.145
--- runtime/gc.c	23 Jun 2003 04:59:01 -0000	1.144
+++ runtime/gc.c	3 Jul 2003 00:42:33 -0000	1.145
@@ -3702,6 +3702,27 @@
 
 #endif
 
+/* profileEnd is for writing out an mlmon.out file even if the C code terminates
+ * abnormally, e.g. due to running out of memory.  It will only run if the usual
+ * SML profile atExit cleanup code did not manage to run.
+ */
+static GC_state profileEndState;
+
+static void profileEnd () {
+	int fd;
+	GC_state s;
+
+	if (DEBUG_PROFILE)
+		fprintf (stderr, "profileEnd ()\n");
+	s = profileEndState;
+	if (s->profilingIsOn) {
+		fd = creat ("mlmon.out", 0666);
+		if (fd < 0)
+			diee ("Cannot create mlmon.out");
+		GC_profileWrite (s, s->profile, fd);
+	}
+}
+
 /* ---------------------------------------------------------------- */
 /*                          Initialization                          */
 /* ---------------------------------------------------------------- */
@@ -4195,19 +4216,6 @@
 	worldFile = NULL;
 	unless (isAligned (s->pageSize, s->cardSize))
 		die ("page size must be a multiple of card size");
-	/* Initialize profiling. */
-	if (s->sourcesSize > 0) {
-		s->profilingIsOn = TRUE;
-		assert (s->frameSourcesSize == s->frameLayoutsSize);
-		if (s->sourceLabelsSize > 0) {
-			s->profileKind = PROFILE_TIME;
-			profileTimeInit (s);
-		} else {
-			s->profileKind = PROFILE_ALLOC;
-			s->profile = GC_profileNew (s);
-		}
-	} else
-		s->profilingIsOn = FALSE;
 	/* Process command-line arguments. */
 	i = 1;
 	if (argc > 1 and (0 == strcmp (argv [1], "@MLton"))) {
@@ -4323,6 +4331,24 @@
 				uintToCommaString (s->totalRam), 
 				uintToCommaString (s->totalSwap),
 				uintToCommaString (s->ram));
+	/* Initialize profiling.  This must occur after processing command-line 
+         * arguments, because those may just be doing a show prof, in which 
+         * case we don't want to initialize the atExit.
+         */
+	if (s->sourcesSize > 0) {
+		s->profilingIsOn = TRUE;
+		assert (s->frameSourcesSize == s->frameLayoutsSize);
+		if (s->sourceLabelsSize > 0) {
+			s->profileKind = PROFILE_TIME;
+			profileTimeInit (s);
+		} else {
+			s->profileKind = PROFILE_ALLOC;
+			s->profile = GC_profileNew (s);
+		}
+		profileEndState = s;
+		atexit (profileEnd);
+	} else
+		s->profilingIsOn = FALSE;
 	if (s->isOriginal)
 		newWorld (s);
 	else {



-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01
_______________________________________________
MLton-devel mailing list
MLton-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mlton-devel