diff: rewrite of runtime/timing.[hc]

Fergus Henderson fjh at cs.mu.oz.au
Sat Jul 26 22:02:19 AEST 1997


Rewrite runtime/timing.[hc] to improve maintainability and portability.
This should hopefully fix a problem with with the timing results being
wrong on Windows NT, due to the use of `sysconf(3)' instead of
`sysconf(_SC_CLK_TCK)'.

runtime/timing.h:
runtime/timing.c:
	A complete rewrite.  The old version was full of non-portable
	cruft that was #ifdef'd on system type rather than by using feature
	tests.  The new version just uses the POSIX.1 times() function.

runtime/prof.c:
	Changes to use the new timing.h.
	Also a couple of changes to use more meaningful names.

library/time.m:
	Minor changes to use the new timing.h.

cvs diff: Diffing .
Index: prof.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/prof.c,v
retrieving revision 1.30
diff -u -r1.30 prof.c
--- prof.c	1997/07/22 22:06:50	1.30
+++ prof.c	1997/07/25 20:12:00
@@ -21,28 +21,20 @@
 
 #include        "std.h"
 #include	"prof_mem.h"
+#include	"timing.h"
 
 #if defined(PROFILE_TIME)
 
 #include	<signal.h>
 
-#ifdef HAVE_SYS_PARAM
-#include	<sys/param.h>
-#endif
-
 #ifdef HAVE_SYS_TIME
 #include	<sys/time.h>
 #endif
 
-/* 
-** if `HZ' is not defined, we may be able to use `sysconf(_SC_CLK_TCK)' instead
-*/
-#if !defined(HZ) && defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
-#define HZ ((int)sysconf(_SC_CLK_TCK))
-#endif
-
-#if !defined(HZ) || !defined(SIGPROF) || !defined(HAVE_SETITIMER)
-#error "Time profiling not supported on this system"
+#if !defined(MR_CLOCK_TICKS_PER_SECOND) \
+	|| !defined(SIGPROF) \
+	|| !defined(HAVE_SETITIMER)
+  #error "Time profiling not supported on this system"
 #endif
 
 #endif	/* PROFILE_TIME */
@@ -52,9 +44,9 @@
 */
 #define CALL_TABLE_SIZE 4096
 #define TIME_TABLE_SIZE 4096
-#define CLOCK_TICKS     5
+#define MR_CLOCK_TICKS_PER_SIGPROF	5
 
-#define USEC            1000000
+#define MR_USEC_PER_SEC            	1000000
 
 /*
 ** profiling node information 
@@ -245,10 +237,13 @@
 
 /*
 **	prof_init_time_profile:
-**		Writes the value of HZ (no. of ticks per second.) at the start
-**		of the file 'Prof.Counts'.
+**		Writes the value of MR_CLOCK_TICKS_PER_SECOND and
+**		MR_CLOCK_TICKS_PER_SIGPROF at the start of the file
+**		'Prof.Counts'.
 **		Then sets up the profiling timer and starts it up. 
-**		At the moment it is after every X ticks of the clock.
+**		At the moment it is after every MR_CLOCK_TICKS_PER_SIGPROF
+**		ticks of the clock.
+**
 **		SYSTEM SPECIFIC CODE
 */
 
@@ -257,10 +252,13 @@
 {
 	FILE 	*fptr;
 	struct itimerval itime;
+	const long sigprof_interval_in_usecs = MR_CLOCK_TICKS_PER_SIGPROF *
+		(MR_USEC_PER_SEC / MR_CLOCK_TICKS_PER_SECOND);
 
-	/* output the value of HZ */
+	/* output the value of MR_CLOCK_TICKS_PER_SECOND */
 	fptr = checked_fopen("Prof.Counts", "create", "w");
-	fprintf(fptr, "%d %d\n", HZ, CLOCK_TICKS);
+	fprintf(fptr, "%d %d\n",
+		MR_CLOCK_TICKS_PER_SECOND, MR_CLOCK_TICKS_PER_SIGPROF);
 	checked_fclose(fptr, "Prof.Counts");
 
 	checked_atexit(prof_finish);
@@ -268,9 +266,9 @@
 	profiling_on = TRUE;
 
 	itime.it_value.tv_sec = 0;
-	itime.it_value.tv_usec = (long) (USEC / HZ) * CLOCK_TICKS; 
+	itime.it_value.tv_usec = sigprof_interval_in_usecs;
 	itime.it_interval.tv_sec = 0;
-	itime.it_interval.tv_usec = (long) (USEC / HZ) * CLOCK_TICKS;
+	itime.it_interval.tv_usec = sigprof_interval_in_usecs;
 
 	checked_signal(SIGPROF, prof_time_profile);
 	checked_setitimer(ITIMER_PROF, &itime);
Index: timing.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/timing.c,v
retrieving revision 1.9
diff -u -r1.9 timing.c
--- timing.c	1997/02/12 02:16:41	1.9
+++ timing.c	1997/07/25 20:14:51
@@ -1,101 +1,36 @@
-/*---------------------------------------------------------------------------*/
-/* I hacked this from toplev.c in the gcc source - fjh. */
-/* further hacked for Solaris 2 by zs */
-/*---------------------------------------------------------------------------*/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include "conf.h"
-#include "timing.h"
-
-/* Copyright (C) 1987, 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
+/*
+** Copyright (C) 1997 University of Melbourne.
+** This file may only be copied under the terms of the GNU Library General
+** Public License - see the file COPYING.LIB in the Mercury distribution.
+*/
+
+/*
+** file: timing.c
+** main authors: fjh
+**
+**	Timing routines.
+*/
 
-This file is part of GNU CC.
+#include "imp.h"
 
-GNU CC is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
-
-GNU CC is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
-
-#ifdef HAVE_SYSCONF
-#include <sys/times.h>
-#include <limits.h>
-#else
-#ifdef USG
-#undef FLOAT
-#include <sys/param.h>
-/* This is for hpux.  It is a real screw.  They should change hpux.  */
-#undef FLOAT
-#include <sys/times.h>
-#include <time.h>   /* Correct for hpux at least.  Is it good on other USG?  */
-#undef FFS  /* Some systems define this in param.h.  */
-#else
-#ifndef VMS
+#ifdef HAVE_SYS_TIME
 #include <sys/time.h>
-#include <sys/resource.h>
-#endif
-#endif
 #endif
 
-/* Return time used so far, in milliseconds.  */
+#include "timing.h"
 
-int 
-get_run_time (void)
+int
+MR_get_user_cpu_miliseconds(void)
 {
-#ifdef HAVE_SYSCONF
-  struct tms tms;
+#ifndef MR_CLOCK_TICKS_PER_SECOND
+	return -1;
 #else
-#ifdef USG
-  struct tms tms;
-#else
-#ifndef VMS
-  struct rusage rusage;
-#else /* VMS */
-  struct
-    {
-      int proc_user_time;
-      int proc_system_time;
-      int child_user_time;
-      int child_system_time;
-    } vms_times;
-#endif
-#endif
-#endif
+	const double ticks_per_milisecond = MR_CLOCK_TICKS_PER_SECOND / 1000.0;
+	struct tms t;
 
-#ifdef HAVE_SYSCONF
-  times (&tms);
-  return (tms.tms_utime /* + tms.tms_stime */) * (1000 / sysconf(3));
-#else
-#ifdef USG
-  times (&tms);
-  return (tms.tms_utime /* + tms.tms_stime */) * (1000 / HZ);
-#else
-#ifndef VMS
-  getrusage (0, &rusage);
-  return (rusage.ru_utime.tv_sec * 1000 + rusage.ru_utime.tv_usec / 1000
-	  /* + rusage.ru_stime.tv_sec * 1000 + rusage.ru_stime.tv_usec / 1000 */);
-#else /* VMS */
-  times (&vms_times);
-  return (vms_times.proc_user_time /* + vms_times.proc_system_time */) * 10;
+	if (times(&t) == -1) {
+		return -1;
+	}
+	return (int) (t.tms_utime / ticks_per_milisecond);
 #endif
-#endif
-#endif
-}
-
-void 
-print_time (const char *str, int total)
-{
-  fprintf (stderr,
-	   "%s = %d.%03d\n",
-	   str, total / 1000, total % 1000);
 }
Index: timing.h
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/timing.h,v
retrieving revision 1.7
diff -u -r1.7 timing.h
--- timing.h	1997/02/12 02:16:43	1.7
+++ timing.h	1997/07/25 19:00:08
@@ -1,47 +1,46 @@
 /*
-** Copyright (C) 1993-1995 University of Melbourne.
+** Copyright (C) 1997 University of Melbourne.
 ** This file may only be copied under the terms of the GNU Library General
 ** Public License - see the file COPYING.LIB in the Mercury distribution.
 */
 
-#ifndef	TIMING_H
-#define	TIMING_H
-
-/*---------------------------------------------------------------------------*/
-/* I hacked this from toplev.c in the gcc source - fjh. */
-/*---------------------------------------------------------------------------*/
-
-/* Copyright (C) 1987, 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
-
-This file is part of GNU CC.
-
-GNU CC is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
-
-GNU CC is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
-
-/* Return time used so far, in milliseconds.  */
-
-extern	int	get_run_time (void);
+/*
+** timing.h - interface to timing routines.
+**	Defines `MR_CLOCK_TICKS_PER_SECOND'
+**	and `MR_get_user_cpu_miliseconds()'.
+*/
 
-/* Print a message and a time in milliseconds. */
+#ifndef TIMING_H
+#define TIMING_H
 
-extern	void	print_time(const char *str, int total);
+#include "conf.h"
 
-#define TIMEVAR(VAR, BODY)    					\
-	do {							\
-		int otime = get_run_time ();			\
-		BODY;						\
-		VAR = get_run_time () - otime;			\
-	} while (0)	
+#ifdef HAVE_SYS_PARAM
+#include <sys/param.h>
+#endif
+
+#ifdef HAVE_SYS_TIME
+#include <sys/time.h>
+#endif
+
+/* 
+** `HZ' is the number of clock ticks per second.
+** It is used when converting a clock_t value to a time in seconds.
+** It may be defined by <sys/time.h>, but if it is not defined there,
+** we may be able to use `sysconf(_SC_CLK_TCK)' instead
+*/
+#ifdef HZ
+  #define MR_CLOCK_TICKS_PER_SECOND	HZ
+#elif defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
+  #define MR_CLOCK_TICKS_PER_SECOND	((int) sysconf(_SC_CLK_TCK))
+#else
+  /* just leave it undefined */
+#endif
+
+/*
+** MR_get_user_cpu_miliseconds() returns the CPU time consumed by the
+** process, in miliseconds, from an arbitrary initial time.
+*/
+int MR_get_user_cpu_miliseconds(void);
 
-#endif /* not TIMING_H */
+#endif /* TIMING_H */
Index: wrapper.mod
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/wrapper.mod,v
retrieving revision 1.74
diff -u -r1.74 wrapper.mod
--- wrapper.mod	1997/06/03 08:14:40	1.74
+++ wrapper.mod	1997/07/25 20:15:21
@@ -691,7 +691,7 @@
 	nondetstack_zone->max = nondetstack_zone->min;
 #endif
 
-	time_at_start = get_run_time();
+	time_at_start = MR_get_user_cpu_miliseconds();
 	time_at_last_stat = time_at_start;
 
 	for (repcounter = 0; repcounter < repeats; repcounter++) {
@@ -701,7 +701,7 @@
 	}
 
         if (use_own_timer) {
-		time_at_finish = get_run_time();
+		time_at_finish = MR_get_user_cpu_miliseconds();
 	}
 
 #if defined(USE_GCC_NONLOCAL_GOTOS) && !defined(SPEED)
cvs diff: Diffing machdeps
Index: time.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/time.m,v
retrieving revision 1.10
diff -u -r1.10 time.m
--- time.m	1997/07/25 03:44:43	1.10
+++ time.m	1997/07/26 07:52:07
@@ -60,7 +60,7 @@
 	int	time_at_prev_stat;
 
 	time_at_prev_stat = time_at_last_stat;
-	time_at_last_stat = get_run_time();
+	time_at_last_stat = MR_get_user_cpu_miliseconds();
 
 	fprintf(stderr, 
 		""[Time: +%.3fs, %.3fs, D Stack: %.3fk, ND Stack: %.3fk, "",
@@ -166,7 +166,7 @@
 
 	framevar(3) = 0;
 	mark_hp(framevar(5));
-	framevar(4) = get_run_time();
+	framevar(4) = MR_get_user_cpu_miliseconds();
 
 	/* call the higher-order pred closure that we were passed in r3 */
 	r1 = r3;
@@ -210,7 +210,7 @@
 
 	/* no more iterations */
 	count_output = framevar(3);
-	time_output = get_run_time() - framevar(4);
+	time_output = MR_get_user_cpu_miliseconds() - framevar(4);
 	succeed_discard();
 END_MODULE
 
@@ -250,7 +250,7 @@
 	else
 		detstackvar(3) = rep_count;
 
-	detstackvar(4) = get_run_time();
+	detstackvar(4) = MR_get_user_cpu_miliseconds();
 
 	/* call the higher-order pred closure that we were passed in r3 */
 	r1 = r3;
@@ -286,7 +286,7 @@
 
 	/* no more iterations */
 	soln_output = r1; /* the closure *always* returns its output in r1 */
-	time_output = get_run_time() - detstackvar(4);
+	time_output = MR_get_user_cpu_miliseconds() - detstackvar(4);
 	succip = (Word *) detstackvar(6);
 	decr_sp(6);
 	proceed();

-- 
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.



More information about the developers mailing list