[m-dev.] diff: fix bugs with profiling and C interface

Fergus Henderson fjh at cs.mu.OZ.AU
Tue Nov 23 05:37:14 AEDT 1999


Estimated hours taken: 2

Fix some bugs with profiling and `pragma export'.

runtime/mercury_engine.c:
	In call_engine(), set MR_prof_current_proc before calling the
	procedure (previously this was being done only for the non-local
	gotos case, not for the ANSI case), and restore MR_prof_current_proc
	to its previous value when the called procedure has returned.
	This fixes some problems where time profiling was crediting
	time spent in C code to the wrong procedure because
	MR_prof_current_proc had not been set correctly.

	Also, if both call profiling and time profiling are enabled,
	and there is a call from Mercury to C to Mercury, then
	record the Mercury caller/callee pair in the call counts
	table.  (If only call profiling is enabled, then we don't
	know who the caller was, so we can't do that; for that case,
	we must make do with some missing information.)

Workspace: /home/mercury0/fjh/mercury
Index: runtime/mercury_engine.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_engine.c,v
retrieving revision 1.20
diff -u -d -r1.20 mercury_engine.c
--- mercury_engine.c	1999/11/13 00:53:39	1.20
+++ mercury_engine.c	1999/11/22 18:09:24
@@ -209,6 +209,9 @@
 
 	jmp_buf		curr_jmp_buf;
 	jmp_buf		* volatile prev_jmp_buf;
+#if defined(PROFILE_TIME)
+	Code		* volatile prev_proc;
+#endif
 
 	/*
 	** Preserve the value of MR_ENGINE(e_jmp_buf) on the C stack.
@@ -240,9 +243,12 @@
 		debugmsg0("...caught longjmp\n");
 		/*
 		** On return,
+		** set MR_prof_current_proc to be the caller proc again
+		** (if time profiling is enabled),
 		** restore the registers (since longjmp may clobber them),
 		** and restore the saved value of MR_ENGINE(e_jmp_buf).
 		*/
+		update_prof_current_proc(prev_proc);
 		restore_registers();
 		MR_ENGINE(e_jmp_buf) = prev_jmp_buf;
 		if (catch_exceptions) {
@@ -273,6 +279,51 @@
 		}
 		return NULL;
 	}
+
+
+  	MR_ENGINE(e_jmp_buf) = &curr_jmp_buf;
+  
+	/*
+	** If call profiling is enabled, and this is a case of
+	** Mercury calling C code which then calls Mercury,
+	** then we record the Mercury caller / Mercury callee pair
+	** in the table of call counts, if possible.
+	*/
+#ifdef PROFILE_CALLS
+  #ifdef PROFILE_TIME
+	if (MR_prof_current_proc != NULL) {
+		PROFILE(entry_point, MR_prof_current_proc);
+	}
+  #else
+	/*
+	** XXX There's not much we can do in this case
+	** to keep the call counts accurate, since
+	** we don't know who the caller is.
+	*/ 
+  #endif
+#endif /* PROFILE_CALLS */
+
+	/*
+	** If time profiling is enabled, then we need to
+	** save MR_prof_current_proc so that we can restore it
+	** when we return.  We must then set MR_prof_current_proc
+	** to the procedure that we are about to call.
+	**
+	** We do this last thing before calling call_engine_inner(),
+	** since we want to credit as much as possible of the time
+	** in C code to the caller, not to the callee.
+	** Note that setting and restoring MR_prof_current_proc
+	** here in call_engine() means that time in call_engine_inner()
+	** unfortunately gets credited to the callee.
+	** That is not ideal, but we can't move this code into
+	** call_engine_inner() since call_engine_inner() can't
+	** have any local variables and this code needs the
+	** `prev_proc' local variable.
+	*/
+#ifdef PROFILE_TIME
+	prev_proc = MR_prof_current_proc;
+	set_prof_current_proc(entry_point);
+#endif
 
 	call_engine_inner(entry_point);
 }

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list