[MLton-devel] cvs commit: writing profiling data when out of memory

Stephen Weeks sweeks@users.sourceforge.net
Wed, 02 Jul 2003 17:42:33 -0700


sweeks      03/07/02 17:42:33

  Modified:    doc      changelog
               runtime  gc.c
  Log:
  Added fix to profiling so that the mlmon.out file is written even when
  the program terminates due to running out of memory.  The fix is to
  use the C atexit function to write out the profiling data (like we
  used to ages ago) if for some reason the SML code didn't get to.

Revision  Changes    Path
1.48      +2 -0      mlton/doc/changelog

Index: changelog
===================================================================
RCS file: /cvsroot/mlton/mlton/doc/changelog,v
retrieving revision 1.47
retrieving revision 1.48
diff -u -r1.47 -r1.48
--- changelog	2 Jul 2003 23:08:38 -0000	1.47
+++ changelog	3 Jul 2003 00:42:33 -0000	1.48
@@ -7,6 +7,8 @@
     time error message:
     "x86Translate.translateChunk ... strange Offset: base: ..."
   - Changed argument type of MLton.IntInf.Small from word to int.
+  - Added fix to profiling so that the mlmon.out file is written even
+    when the program terminates due to running out of memory.
 
 * 2003-06-25
   - Added {Int{8,16},Word8}{,Array,ArraySlice,Vector,VectorSlice,Array2}



1.145     +39 -13    mlton/runtime/gc.c

Index: 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
--- gc.c	23 Jun 2003 04:59:01 -0000	1.144
+++ 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