NetBSD Problem Report #45592

From woods@once.weird.com  Tue Nov  8 20:12:37 2011
Return-Path: <woods@once.weird.com>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by www.NetBSD.org (Postfix) with ESMTP id 6B69263C39E
	for <gnats-bugs@gnats.NetBSD.org>; Tue,  8 Nov 2011 20:12:37 +0000 (UTC)
Message-Id: <m1RNs1V-001EBsC@once.weird.com>
Date: Tue, 8 Nov 2011 12:11:57 -0800 (PST)
From: "Greg A. Woods" <woods@planix.com>
Sender: "Greg A. Woods" <woods@once.weird.com>
Reply-To: "Greg A. Woods" <woods@planix.com>
To: gnats-bugs@gnats.NetBSD.org
Subject: changes to get time(1) to use clock_gettime(CLOCK_MONOTONIC)
X-Send-Pr-Version: 3.95

>Number:         45592
>Category:       bin
>Synopsis:       changes to get time(1) to use clock_gettime(CLOCK_MONOTONIC)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    christos
>State:          closed
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Tue Nov 08 20:15:00 +0000 2011
>Closed-Date:    Fri Sep 30 08:49:35 +0000 2016
>Last-Modified:  Fri Sep 30 08:49:35 +0000 2016
>Originator:     Greg A. Woods
>Release:        all
>Organization:
Planix, Inc.; Kelowna, BC; Canada
>Environment:
System: NetBSD
Architecture: all
Machine: all
>Description:

	time(1) uses gettimeofday() and so can be subject to time
	adjustments and such

	It could use clock_gettime() with CLOCK_MONOTONIC instead

>How-To-Repeat:


>Fix:

	these changes are incomplete -- they hack some common code
	stolen from csh but don't fix the rest of csh to adapt similarly
	as I don't build, use, or otherwise support or care about csh.  :-)

	these diffs are against the netbsd-5 branch as of 2011/11/08


Index: usr.bin/time/time.c
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/usr.bin/time/time.c,v
retrieving revision 1.19
diff -u -r1.19 time.c
--- usr.bin/time/time.c	21 Jul 2008 14:19:26 -0000	1.19
+++ usr.bin/time/time.c	27 Oct 2011 02:39:41 -0000
@@ -59,6 +59,8 @@
 int		main(int, char **);
 static void	usage(void);
 static void	prl(long, const char *);
+static void	prts(const char *, const char *, const struct timespec *,
+    const char *);
 static void	prtv(const char *, const char *, const struct timeval *,
     const char *);

@@ -72,7 +74,7 @@
 	int volatile cshflag;
 	const char *decpt;
 	const struct lconv *lconv;
-	struct timeval before, after;
+	struct timespec before, after;
 	struct rusage ru;

 	(void)setlocale(LC_ALL, "");
@@ -106,7 +108,7 @@
 	if (argc < 1)
 		usage();

-	gettimeofday(&before, (struct timezone *)NULL);
+	(void)clock_gettime(CLOCK_MONOTONIC, &before);
 	switch(pid = vfork()) {
 	case -1:			/* error */
 		err(EXIT_FAILURE, "Vfork failed");
@@ -123,10 +125,10 @@
 	(void)signal(SIGQUIT, SIG_IGN);
 	if ((pid = wait4(pid, &status, 0, &ru)) == -1)
 		err(EXIT_FAILURE, "wait4 %d failed", pid);
-	(void)gettimeofday(&after, (struct timezone *)NULL);
+	(void)clock_gettime(CLOCK_MONOTONIC, &after);
 	if (!WIFEXITED(status))
 		warnx("Command terminated abnormally.");
-	timersub(&after, &before, &after);
+	timespecsub(&after, &before, &after);

 	if ((lconv = localeconv()) == NULL ||
 	    (decpt = lconv->decimal_point) == NULL)
@@ -135,14 +137,14 @@
 	if (cshflag) {
 		static struct rusage null_ru;
 		before.tv_sec = 0;
-		before.tv_usec = 0;
+		before.tv_nsec = 0;
 		prusage(stderr, &null_ru, &ru, &after, &before);
 	} else if (portableflag) {
-		prtv("real ", decpt, &after, "\n");
+		prts("real ", decpt, &after, "\n");
 		prtv("user ", decpt, &ru.ru_utime, "\n");
 		prtv("sys  ", decpt, &ru.ru_stime, "\n");
 	} else {
-		prtv("", decpt, &after, " real ");
+		prts("", decpt, &after, " real ");
 		prtv("", decpt, &ru.ru_utime, " user ");
 		prtv("", decpt, &ru.ru_stime, " sys\n");
 	}
@@ -190,6 +192,15 @@
 }

 static void
+prts(const char *pre, const char *decpt, const struct timespec *ts,
+    const char *post)
+{
+
+	(void)fprintf(stderr, "%s%9ld%s%02ld%s", pre, (long)ts->tv_sec, decpt,
+	    (long)ts->tv_nsec / 10000000, post);
+}
+
+static void
 prtv(const char *pre, const char *decpt, const struct timeval *tv,
     const char *post)
 {
Index: usr.bin/time/ext.h
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/usr.bin/time/ext.h,v
retrieving revision 1.1
diff -u -r1.1 ext.h
--- usr.bin/time/ext.h	24 Feb 2007 21:30:27 -0000	1.1
+++ usr.bin/time/ext.h	27 Oct 2011 02:49:36 -0000
@@ -1,4 +1,5 @@
 /*	$NetBSD: ext.h,v 1.1 2007/02/24 21:30:27 matt Exp $	*/

-void prusage(FILE *, struct rusage *, struct rusage *, struct timeval *,
-        struct timeval *);
+/* borrowed from ../../bin/csh/extern.h */
+void prusage(FILE *, struct rusage *, struct rusage *, struct timespec *,
+        struct timespec *);
Index: usr.bin/time/time.1
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/usr.bin/time/time.1,v
retrieving revision 1.18.40.1
diff -u -r1.18.40.1 time.1
--- usr.bin/time/time.1	26 Mar 2009 17:07:30 -0000	1.18.40.1
+++ usr.bin/time/time.1	8 Nov 2011 20:11:01 -0000
@@ -51,10 +51,10 @@
 .Ar utility
 finishes,
 .Nm
-writes the total time elapsed,
-the time consumed by system overhead,
-and the time used to execute
-.Ar utility
+writes the total time elapsed (wall clock time),
+the CPU time consumed by system overhead,
+and the CPU time used to execute
+.Ar utility ,
 to the standard error stream.
 Times are reported in seconds.
 .Pp
@@ -80,7 +80,7 @@
 .Xr csh 1
 and
 .Xr ksh 1 ,
-have their own and syntactically different builtin version of
+have their own and syntactically different built-in version of
 .Nm .
 The utility described here
 is available as
@@ -152,6 +152,7 @@
 .Sh SEE ALSO
 .Xr csh 1 ,
 .Xr ksh 1 ,
+.Xr clock_gettime 2
 .Xr getrusage 2
 .Sh STANDARDS
 The
Index: bin/csh/time.c
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/bin/csh/time.c,v
retrieving revision 1.17
diff -u -r1.17 time.c
--- bin/csh/time.c	24 Feb 2008 05:20:17 -0000	1.17
+++ bin/csh/time.c	27 Oct 2011 02:42:19 -0000
@@ -127,8 +127,8 @@
 #endif /* NOT_CSH */

 void
-prusage(FILE *fp, struct rusage *r0, struct rusage *r1, struct timeval *e,
-        struct timeval *b)
+prusage(FILE *fp, struct rusage *r0, struct rusage *r1, struct timespec *e,
+        struct timespec *b)
 {
 #ifndef NOT_CSH
     struct varent *vp;
@@ -139,7 +139,7 @@
     int ms;

     cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
-    ms = (e->tv_sec - b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000;
+    ms = (e->tv_sec - b->tv_sec) * 100 + (e->tv_nsec - b->tv_nsec) / 10000000;
     t = (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
         (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
         (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
Index: bin/csh/extern.h
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/bin/csh/extern.h,v
retrieving revision 1.22
diff -u -r1.22 extern.h
--- bin/csh/extern.h	24 Dec 2007 16:11:50 -0000	1.22
+++ bin/csh/extern.h	27 Oct 2011 02:40:08 -0000
@@ -288,8 +288,8 @@
  */
 void donice(Char **, struct command *);
 void dotime(Char **, struct command *);
-void prusage(FILE *, struct rusage *, struct rusage *, struct timeval *,
-             struct timeval *);
+void prusage(FILE *, struct rusage *, struct rusage *, struct timespec *,
+             struct timespec *);
 void ruadd(struct rusage *, struct rusage *);
 void settimes(void);
 void psecs(long);

>Release-Note:

>Audit-Trail:

Responsible-Changed-From-To: bin-bug-people->dholland
Responsible-Changed-By: dholland@NetBSD.org
Responsible-Changed-When: Wed, 09 Nov 2011 03:07:45 +0000
Responsible-Changed-Why:
take

(note that we do build, support, and care about csh, so someone's going to
have to finish your patches)


From: "Greg A. Woods" <woods@planix.com>
To: NetBSD GNATS <gnats-bugs@NetBSD.org>
Cc: 
Subject: Re: bin/45592 (changes to get time(1) to use clock_gettime(CLOCK_MONOTONIC))
Date: Wed, 09 Nov 2011 10:50:09 -0800

 --pgp-sign-Multipart_Wed_Nov__9_10:50:09_2011-1
 Content-Type: text/plain; charset=US-ASCII
 Content-Transfer-Encoding: quoted-printable

 At Wed,  9 Nov 2011 03:07:45 +0000 (UTC), dholland@NetBSD.org wrote:
 Subject: Re: bin/45592 (changes to get time(1) to use clock_gettime(CLOCK_M=
 ONOTONIC))
 >=20
 > (note that we do build, support, and care about csh, so someone's going to
 > have to finish your patches)

 hopefully someone who cares about csh will do so  :-)

 On the other hand I suppose there would be too many outcries of
 impending doom if it were proposed that csh be moved into pkgsrc.  :-)

 --=20
 						Greg A. Woods

 +1 250 762-7675                                RoboHack <woods@robohack.ca>
 Planix, Inc. <woods@planix.com>      Secrets of the Weird <woods@weird.com>

 --pgp-sign-Multipart_Wed_Nov__9_10:50:09_2011-1
 Content-Type: application/pgp-signature
 Content-Transfer-Encoding: 7bit

 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.9 (NetBSD)

 iD8DBQBOusthZn1xt3i/9H8RAmfMAKCfnOk1S9drpGt171i4ZAWNkw/f1QCfaL9W
 Vgx6WAR9Yurdb/tkcLBR26A=
 =GYuK
 -----END PGP SIGNATURE-----

 --pgp-sign-Multipart_Wed_Nov__9_10:50:09_2011-1--

From: "Christos Zoulas" <christos@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/45592 CVS commit: src/usr.bin/time
Date: Wed, 9 Nov 2011 14:10:11 -0500

 Module Name:	src
 Committed By:	christos
 Date:		Wed Nov  9 19:10:10 UTC 2011

 Modified Files:
 	src/usr.bin/time: ext.h time.1 time.c

 Log Message:
 PR/45592: Greg A. Woods: changes to get time(1) to use CLOCK_MONOTONIC


 To generate a diff of this commit:
 cvs rdiff -u -r1.1 -r1.2 src/usr.bin/time/ext.h
 cvs rdiff -u -r1.23 -r1.24 src/usr.bin/time/time.1
 cvs rdiff -u -r1.21 -r1.22 src/usr.bin/time/time.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

Responsible-Changed-From-To: dholland->christos
Responsible-Changed-By: dholland@NetBSD.org
Responsible-Changed-When: Sun, 22 Jan 2012 16:09:20 +0000
Responsible-Changed-Why:
christos pulled the rug out from under me :-)


State-Changed-From-To: open->feedback
State-Changed-By: dholland@NetBSD.org
State-Changed-When: Sun, 22 Jan 2012 16:09:20 +0000
State-Changed-Why:
christos committed it - does it work as committed?


State-Changed-From-To: feedback->closed
State-Changed-By: dholland@NetBSD.org
State-Changed-When: Fri, 30 Sep 2016 08:49:35 +0000
State-Changed-Why:
christos committed it in 2011


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.