[m-dev.] for review: enhancement for profiling

Thomas Charles CONWAY conway at cs.mu.oz.au
Wed Jul 23 08:05:40 AEST 1997


Fergus Henderson, you write:
> 
> Could this be a function-local static variable rather than a global
> static variable?
Done.
> > +static	bool		addr_table_written = FALSE;
> 
> Ditto.
Done.

> 
> s/atexit %s/atexit: %s/
> 

Done.

Thomas
-- 
ZZ:wq!
^X^C
Thomas Conway               				      conway at cs.mu.oz.au
AD DEUM ET VINUM	  			      Every sword has two edges.


Enhance the runtime support for profiling so that the profiling data
gets written out if the program terminates through a call to exit
rather than by main terminating. This enables profiling to work with
the Tcl/Tk and OpenGL stuff (for example).

runtime/prof.c
	Register a function for writing out profiling info with atexit.
	Make the functions for writing out the profiling info check to
	see if it has been written out yet, and return straight away if
	it has.

cvs diff: Diffing .
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
cvs diff: Diffing compiler/notes
cvs diff: Diffing doc
cvs diff: Diffing extras
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/Togl-1.2
cvs diff: Diffing extras/graphics/samples
cvs diff: Diffing extras/graphics/samples/calc
cvs diff: Diffing extras/graphics/samples/maze
cvs diff: Diffing library
cvs diff: Diffing lp_solve
cvs diff: Diffing lp_solve/lp_examples
cvs diff: Diffing profiler
cvs diff: Diffing runtime
Index: runtime/prof.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/prof.c,v
retrieving revision 1.29
diff -u -r1.29 prof.c
--- prof.c	1997/02/12 02:16:08	1.29
+++ prof.c	1997/07/22 22:02:33
@@ -101,12 +101,18 @@
 #ifdef PROFILE_CALLS
 static	FILE	 	*declfptr = NULL;
 static	prof_call_node	*addr_pair_table[CALL_TABLE_SIZE] = {NULL};
+static	bool		profiling_on = FALSE;
 #endif
 
 #ifdef PROFILE_TIME
 static	prof_time_node	*addr_table[TIME_TABLE_SIZE] = {NULL};
 #endif
 
+/*
+** Local function declarations
+*/
+static	void prof_finish(void);
+
 /* ======================================================================== */
 
 #ifndef HAVE_STRERROR
@@ -168,6 +174,18 @@
 	}
 }
 
+static void
+checked_atexit(void (*func)(void))
+{
+	errno = 0;
+	if (atexit(func) != 0) {
+		fprintf(stderr,
+			"Mercury runtime: error in call to atexit: %s\n",
+			strerror(errno));
+		exit(1);
+	}
+}
+
 #endif /* defined(PROFILE_TIME) || defined(PROFILE_CALLS) */
 
 #ifdef	PROFILE_TIME
@@ -245,6 +263,10 @@
 	fprintf(fptr, "%d %d\n", HZ, CLOCK_TICKS);
 	checked_fclose(fptr, "Prof.Counts");
 
+	checked_atexit(prof_finish);
+
+	profiling_on = TRUE;
+
 	itime.it_value.tv_sec = 0;
 	itime.it_value.tv_usec = (long) (USEC / HZ) * CLOCK_TICKS; 
 	itime.it_interval.tv_sec = 0;
@@ -252,6 +274,7 @@
 
 	checked_signal(SIGPROF, prof_time_profile);
 	checked_setitimer(ITIMER_PROF, &itime);
+
 }
 
 #endif /* PROFILE_TIME */
@@ -369,6 +392,9 @@
 {
 	struct itimerval itime;
 
+	if (profiling_on == FALSE)
+		return;
+
 	itime.it_value.tv_sec = 0;
 	itime.it_value.tv_usec = 0;
 	itime.it_interval.tv_sec = 0;
@@ -394,8 +420,11 @@
 void
 prof_output_addr_pair_table(void)
 {
+	static	bool	addr_pair_table_written = FALSE;
 	FILE		*fptr;
 	int		i;
+	if (addr_pair_table_written == TRUE)
+		return;
 
 	fptr = checked_fopen("Prof.CallPair", "create", "w");
 	for (i = 0; i < CALL_TABLE_SIZE ; i++) {
@@ -403,6 +432,8 @@
 	}
 
 	checked_fclose(fptr, "Prof.CallPair");
+
+	addr_pair_table_written = TRUE;
 }
 
 static void
@@ -457,15 +488,21 @@
 void
 prof_output_addr_table()
 {
+	static	bool	addr_table_written = FALSE;
 	FILE *fptr;
 	int  i;
 
+	if (addr_table_written == TRUE)
+		return;
+
 	fptr = checked_fopen("Prof.Counts", "append to", "a");
 	for (i = 0; i < TIME_TABLE_SIZE ; i++) {
 		print_time_node(fptr, addr_table[i]);
 	}
 
 	checked_fclose(fptr, "Prof.Counts");
+
+	addr_table_written = TRUE;
 }
 
 static void
@@ -481,3 +518,17 @@
 }
 
 #endif /* PROFILE_TIME */
+
+void prof_finish()
+{
+
+#ifdef PROFILE_TIME
+	prof_turn_off_time_profiling();
+	prof_output_addr_table();
+#endif
+
+#ifdef PROFILE_CALLS
+	prof_output_addr_pair_table();
+#endif
+}
+
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/diff
cvs diff: Diffing scripts
cvs diff: Diffing tools
cvs diff: Diffing trial
cvs diff: Diffing util



More information about the developers mailing list