Subject: Re: CVS commit: basesrc/include
To: Perry E. Metzger <perry@wasabisystems.com>
From: Ben Harris <bjh21@netbsd.org>
List: tech-userlevel
Date: 06/25/2002 01:07:21
On 24 Jun 2002, Perry E. Metzger wrote:

> timed, which uses it to get time (it actually uses the return value
> and ignores the usage information.)

Eww.  That's horrid (especially since POSIX explicitly doesn't define an
epoch for times()).

> Fine. Please version it, then.

OK.  Here's a patch that'll do that.  This is the first time I've done
this, so I may have messed it up.  I used the way glob() works as my
example.

I'll commit this at the weekend unless someone asks me not to.  I believe
it maintains backward compatibility with applications compiled before the
CLK_TCK change (tested) and with those compiled now (untested).  Of
course, applications compiled after this patch work too.

Index: sys/sys/times.h
===================================================================
RCS file: /cvsroot/syssrc/sys/sys/times.h,v
retrieving revision 1.8
diff -u -r1.8 times.h
--- sys/sys/times.h	1995/03/26 20:24:54	1.8
+++ sys/sys/times.h	2002/06/24 23:57:29
@@ -61,7 +61,11 @@
 #include <sys/cdefs.h>

 __BEGIN_DECLS
+#ifdef __LIBC12_SOURCE__
 clock_t	times __P((struct tms *));
+#else
+clock_t times __P((struct tms *)) __RENAME(__times13);
+#endif
 __END_DECLS
 #endif
 #endif /* !_SYS_TIMES_H_ */
Index: sys/sys/unistd.h
===================================================================
RCS file: /cvsroot/syssrc/sys/sys/unistd.h,v
retrieving revision 1.19
diff -u -r1.19 unistd.h
--- sys/sys/unistd.h	2002/01/31 00:32:48	1.19
+++ sys/sys/unistd.h	2002/06/24 23:57:29
@@ -121,7 +121,7 @@
 /* configurable system variables */
 #define	_SC_ARG_MAX		 1
 #define	_SC_CHILD_MAX		 2
-#define	_SC_CLK_TCK		 3
+#define	_O_SC_CLK_TCK		 3 /* Old version, always 100 */
 #define	_SC_NGROUPS_MAX		 4
 #define	_SC_OPEN_MAX		 5
 #define	_SC_JOB_CONTROL		 6
@@ -158,6 +158,7 @@
 #define	_SC_MEMORY_PROTECTION	36
 #define	_SC_LOGIN_NAME_MAX	37
 #define	_SC_MONOTONIC_CLOCK	38
+#define	_SC_CLK_TCK		39 /* New, variable version */

 /* configurable system strings */
 #define	_CS_PATH		 1
Index: include/time.h
===================================================================
RCS file: /cvsroot/basesrc/include/time.h,v
retrieving revision 1.28
diff -u -r1.28 time.h
--- include/time.h	2002/06/23 19:41:04	1.28
+++ include/time.h	2002/06/24 23:58:00
@@ -104,13 +104,19 @@

 #if !defined(_ANSI_SOURCE)

+#ifdef __LIBC12_SOURCE__
+#define CLK_TCK 100
+#else
+
 /*
  * CLK_TCK uses libc's internal __sysconf() to retrieve the machine's
- * HZ. The value of _SC_CLK_TCK is 3 -- we hard code it so we do not
+ * HZ. The value of _SC_CLK_TCK is 39 -- we hard code it so we do not
  * need to include unistd.h
  */
 long __sysconf __P((int));
-#define CLK_TCK		(__sysconf(3))
+#define CLK_TCK		(__sysconf(39))
+
+#endif

 extern __aconst char *tzname[2];
 void tzset __P((void));
Index: lib/libc/gen/Makefile.inc
===================================================================
RCS file: /cvsroot/basesrc/lib/libc/gen/Makefile.inc,v
retrieving revision 1.111
diff -u -r1.111 Makefile.inc
--- lib/libc/gen/Makefile.inc	2002/05/24 04:01:43	1.111
+++ lib/libc/gen/Makefile.inc	2002/06/24 23:59:59
@@ -20,11 +20,11 @@
 	sethostname.c setjmperr.c setmode.c setproctitle.c setprogname.c \
 	shquote.c shquotev.c siginterrupt.c __siglist14.c siglist.c signal.c \
 	__signame14.c signame.c __sigsetops14.c sigsetops.c sleep.c \
-	stringlist.c sysconf.c sysctl.c syslog.c telldir.c time.c times.c \
-	timezone.c toascii.c tolower_.c ttyname.c ttyslot.c toupper_.c \
-	ualarm.c ulimit.c uname.c unvis.c usleep.c utime.c utmpx.c valloc.c \
-	vis.c wait.c wait3.c waitpid.c warn.c warnx.c vwarn.c vwarnx.c verr.c \
-	verrx.c
+	stringlist.c sysconf.c sysctl.c syslog.c telldir.c time.c __times13.c \
+	times.c timezone.c toascii.c tolower_.c ttyname.c ttyslot.c \
+	toupper_.c ualarm.c ulimit.c uname.c unvis.c usleep.c utime.c utmpx.c \
+	valloc.c vis.c wait.c wait3.c waitpid.c warn.c warnx.c vwarn.c \
+	vwarnx.c verr.c verrx.c

 # indirect reference stubs, to be removed soon.
 SRCS+=	_err.c _errx.c _sys_errlist.c _sys_nerr.c _sys_siglist.c \
Index: lib/libc/gen/__times13.c
===================================================================
RCS file: __times13.c
diff -N __times13.c
--- /dev/null	Tue Jun 25 02:59:59 2002
+++ __times13.c	Tue Jun 25 02:59:59 2002
@@ -0,0 +1,100 @@
+/*	$NetBSD: times.c,v 1.12 2002/06/23 20:21:08 perry Exp $	*/
+
+/*-
+ * Copyright (c) 1990, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+#if 0
+static char sccsid[] = "@(#)times.c	8.1 (Berkeley) 6/4/93";
+#else
+__RCSID("$NetBSD: times.c,v 1.12 2002/06/23 20:21:08 perry Exp $");
+#endif
+#endif /* LIBC_SCCS and not lint */
+
+#include "namespace.h"
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/times.h>
+#include <sys/resource.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <time.h>
+
+#ifdef __weak_alias
+#ifdef __LIBC12_SOURCE__
+__weak_alias(times,_times)
+#endif
+#endif
+
+#ifdef __LIBC12_SOURCE__
+__warn_references(times,
+    "warning: reference to compatibility times(); include <sys/times.h> for correct reference")
+#endif
+
+/*
+ * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
+ * but this would overflow if we switch to nanosec.
+ */
+#define	CONVTCK(r)	(r.tv_sec * clk_tck + r.tv_usec / (1000000 / clk_tck))
+
+clock_t
+times(tp)
+	struct tms *tp;
+{
+	struct rusage ru;
+	struct timeval t;
+	static long clk_tck;
+
+	_DIAGASSERT(tp != NULL);
+
+	/*
+	 * we use a local copy of CLK_TCK because it expands to a
+	 * moderately expensive function call.
+	 */
+	if (clk_tck == 0)
+		clk_tck = CLK_TCK;
+
+	if (getrusage(RUSAGE_SELF, &ru) < 0)
+		return ((clock_t)-1);
+	tp->tms_utime = CONVTCK(ru.ru_utime);
+	tp->tms_stime = CONVTCK(ru.ru_stime);
+	if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
+		return ((clock_t)-1);
+	tp->tms_cutime = CONVTCK(ru.ru_utime);
+	tp->tms_cstime = CONVTCK(ru.ru_stime);
+	if (gettimeofday(&t, (struct timezone *)0))
+		return ((clock_t)-1);
+	return ((clock_t)(CONVTCK(t)));
+}
Index: lib/libc/gen/sysconf.c
===================================================================
RCS file: /cvsroot/basesrc/lib/libc/gen/sysconf.c,v
retrieving revision 1.15
diff -u -r1.15 sysconf.c
--- lib/libc/gen/sysconf.c	2002/06/23 19:19:01	1.15
+++ lib/libc/gen/sysconf.c	2002/06/25 00:00:00
@@ -91,6 +91,12 @@
 		break;
 	case _SC_CHILD_MAX:
 		return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : (long)rl.rlim_cur);
+	case _O_SC_CLK_TCK:
+		/*
+		 * For applications compiled when CLK_TCK was a compile-time
+		 * constant.
+		 */
+		return 100;
 	case _SC_CLK_TCK:
 		/*
 		 * Has to be handled specially because it returns a
Index: lib/libc/gen/times.c
===================================================================
RCS file: /cvsroot/basesrc/lib/libc/gen/times.c,v
retrieving revision 1.12
diff -u -r1.12 times.c
--- lib/libc/gen/times.c	2002/06/23 20:21:08	1.12
+++ lib/libc/gen/times.c	2002/06/25 00:00:00
@@ -1,93 +1,10 @@
-/*	$NetBSD: times.c,v 1.12 2002/06/23 20:21:08 perry Exp $	*/
+/*	$NetBSD$	*/

-/*-
- * Copyright (c) 1990, 1993
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *	This product includes software developed by the University of
- *	California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#if 0
-static char sccsid[] = "@(#)times.c	8.1 (Berkeley) 6/4/93";
-#else
-__RCSID("$NetBSD: times.c,v 1.12 2002/06/23 20:21:08 perry Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
-
-#include "namespace.h"
-#include <sys/param.h>
-#include <sys/time.h>
-#include <sys/times.h>
-#include <sys/resource.h>
-
-#include <assert.h>
-#include <errno.h>
-#include <time.h>
-
-#ifdef __weak_alias
-__weak_alias(times,_times)
-#endif
-
 /*
- * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
- * but this would overflow if we switch to nanosec.
+ * Ben Harris, 2002.
+ * This file is in the Public Domain.
  */
-#define	CONVTCK(r)	(r.tv_sec * clk_tck + r.tv_usec / (1000000 / clk_tck))
-
-clock_t
-times(tp)
-	struct tms *tp;
-{
-	struct rusage ru;
-	struct timeval t;
-	static long clk_tck;
-
-	_DIAGASSERT(tp != NULL);

-	/*
-	 * we use a local copy of CLK_TCK because it expands to a
-	 * moderately expensive function call.
-	 */
-	if (clk_tck == 0)
-		clk_tck = CLK_TCK;
+#define __LIBC12_SOURCE__

-	if (getrusage(RUSAGE_SELF, &ru) < 0)
-		return ((clock_t)-1);
-	tp->tms_utime = CONVTCK(ru.ru_utime);
-	tp->tms_stime = CONVTCK(ru.ru_stime);
-	if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
-		return ((clock_t)-1);
-	tp->tms_cutime = CONVTCK(ru.ru_utime);
-	tp->tms_cstime = CONVTCK(ru.ru_stime);
-	if (gettimeofday(&t, (struct timezone *)0))
-		return ((clock_t)-1);
-	return ((clock_t)(CONVTCK(t)));
-}
+#include "__times13.c"

-- 
Ben Harris                                                   <bjh21@netbsd.org>
Portmaster, NetBSD/acorn26           <URL:http://www.netbsd.org/Ports/acorn26/>