From nobody@FreeBSD.org  Sat Jul 31 02:57:23 2010
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 6E81D106566C
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 31 Jul 2010 02:57:23 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 5D6C68FC14
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 31 Jul 2010 02:57:23 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o6V2vNAT039889
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 31 Jul 2010 02:57:23 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o6V2vNkY039888;
	Sat, 31 Jul 2010 02:57:23 GMT
	(envelope-from nobody)
Message-Id: <201007310257.o6V2vNkY039888@www.freebsd.org>
Date: Sat, 31 Jul 2010 02:57:23 GMT
From: Andy Farkas <chuzzwassa@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: grdc(6) stops after n/2 seconds
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         149130
>Category:       bin
>Synopsis:       [patch] grdc(6) stops after n/2 seconds
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    uqs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Jul 31 05:30:03 UTC 2010
>Closed-Date:    Wed Aug 18 08:29:39 UTC 2010
>Last-Modified:  Wed Aug 18 08:29:39 UTC 2010
>Originator:     Andy Farkas
>Release:        8.1-STABLE
>Organization:
>Environment:
FreeBSD 8.1-STABLE #0: Tue Jul 27 21:37:41 EST 2010
>Description:
man 6 grdc states "With an optional numeric argument n it stops after n
seconds (default never)."

Currently, if n is provided, grdc stops before n/2 seconds.

This patch fixes the problem and also makes sure it stops *after* n seconds
like the man page says.

>How-To-Repeat:
> time grdc 10
..
        4.16 real         0.05 user         0.00 sys
>

>Fix:
--- /usr/src/games/grdc/grdc.c	2010-03-12 10:51:13.000000000 +1000
+++ ./grdc.c	2010-07-31 12:43:57.000000000 +1000
@@ -204,9 +204,9 @@
 				ts.tv_nsec = 0;
 			}
 			nanosleep(&ts, NULL);
-			now = ts.tv_sec + 1;
+			++now;
 		} else
-			now = ts.tv_sec;
+			now = ts.tv_sec + 1;
 		if (sigtermed) {
 			standend();
 			clear();
@@ -214,7 +214,7 @@
 			endwin();
 			errx(1, "terminated by signal %d", (int)sigtermed);
 		}
-	} while(--n);
+	} while(n--);
 	standend();
 	clear();
 	refresh();


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->uqs 
Responsible-Changed-By: uqs 
Responsible-Changed-When: Sat Jul 31 11:07:06 UTC 2010 
Responsible-Changed-Why:  
I'll take it. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=149130 

From: Ulrich =?utf-8?B?U3DDtnJsZWlu?= <uqs@FreeBSD.org>
To: bug-followup@FreeBSD.org, chuzzwassa@gmail.com
Cc:  
Subject: Re: bin/149130: grdc(6) stops after n/2 seconds
Date: Sat, 31 Jul 2010 15:05:24 +0200

 --r5Pyd7+fXNt84Ff3
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 Hello Andy,
 
 your patch has a problem, in that it will break the default case of no
 argument given to grdc(6). Also I think the whole timekeeping can be
 simplified a little.
 
 Please have a look at this patch instead, thanks.
 
 --r5Pyd7+fXNt84Ff3
 Content-Type: text/x-diff; charset=utf-8
 Content-Disposition: attachment; filename="grdc.diff"
 Content-Transfer-Encoding: 8bit
 
 commit 358d6e6387d72ef43afb643ba7ab437cdabb0de0
 Author: Ulrich Spörlein <uqs@spoerlein.net>
 Date:   Sat Jul 31 15:00:57 2010 +0200
 
     grdc(6): fix timekeeping for user-supplied value n
     
     - Keep timespec 'now' and 'delay' separate to avoid confusion
     - Increase user-supplied n to run _at least_ n seconds, not max n
     
     PR:            bin/149130 (based on)
     Submitted by:  Andy Farkas
     MFC after:     2 weeks
 
 diff --git a/games/grdc/grdc.c b/games/grdc/grdc.c
 index 7586be6..99960c8 100644
 --- a/games/grdc/grdc.c
 +++ b/games/grdc/grdc.c
 @@ -29,7 +29,7 @@
  #define YDEPTH  7
  
  /* it won't be */
 -time_t now; /* yeah! */
 +struct timespec now; /* yeah! */
  struct tm *tm;
  
  short disp[11] = {
 @@ -57,7 +57,7 @@ sighndl(int signo)
  int
  main(int argc, char *argv[])
  {
 -	struct timespec ts;
 +	struct timespec delay;
  	long t, a;
  	int i, j, s, k;
  	int n;
 @@ -89,7 +89,7 @@ main(int argc, char *argv[])
  	}
  
  	if (argc > 0)
 -		n = atoi(*argv);
 +		n = atoi(*argv) + 1;
  	else
  		n = 0;
  
 @@ -135,10 +135,10 @@ main(int argc, char *argv[])
  
  		attrset(COLOR_PAIR(2));
  	}
 -	time(&now);
 +	clock_gettime(CLOCK_REALTIME_FAST, &now);
  	do {
  		mask = 0;
 -		tm = localtime(&now);
 +		tm = localtime(&now.tv_sec);
  		set(tm->tm_sec%10, 0);
  		set(tm->tm_sec/10, 4);
  		set(tm->tm_min%10, 10);
 @@ -192,19 +192,16 @@ main(int argc, char *argv[])
  		}
  		movto(6, 0);
  		refresh();
 -		clock_gettime(CLOCK_REALTIME_FAST, &ts);
 -		if (ts.tv_sec == now) {
 -			if (ts.tv_nsec > 0) {
 -				ts.tv_sec = 0;
 -				ts.tv_nsec = 1000000000 - ts.tv_nsec;
 -			} else {
 -				ts.tv_sec = 1;
 -				ts.tv_nsec = 0;
 -			}
 -			nanosleep(&ts, NULL);
 -			now = ts.tv_sec + 1;
 -		} else
 -			now = ts.tv_sec;
 +		clock_gettime(CLOCK_REALTIME_FAST, &now);
 +		if (delay.tv_nsec > 0) {
 +		    delay.tv_sec = 0;
 +		    delay.tv_nsec = 1000000000 - now.tv_nsec;
 +		} else {
 +		    delay.tv_sec = 1;
 +		    delay.tv_nsec = 0;
 +		}
 +		nanosleep(&delay, NULL);
 +		now.tv_sec++;
  		if (sigtermed) {
  			standend();
  			clear();
 
 --r5Pyd7+fXNt84Ff3--

From: Andy Farkas <chuzzwassa@gmail.com>
To: =?ISO-8859-1?Q?Ulrich_Sp=F6rlein?= <uqs@freebsd.org>
Cc: bug-followup@freebsd.org
Subject: Re: bin/149130: grdc(6) stops after n/2 seconds
Date: Sun, 1 Aug 2010 00:25:31 +1000

 On Sat, Jul 31, 2010 at 11:05 PM, Ulrich Sp=F6rlein <uqs@freebsd.org> wrote=
 :
 
 > your patch has a problem, in that it will break the default case of no
 > argument given to grdc(6). Also I think the whole timekeeping can be
 > simplified a little.
 >
 > Please have a look at this patch instead, thanks.
 
 Your patch works better than mine! I was so concerned about the n>0 case I
 forgot to test n=3D0.
 
 But I still think a better patch can be done.
 
 This is how the logic should work:
 
 The do loop gets the current time and displays it, taking some time to
 do that. Then we wish to sleep for 1 - (time taken to display) so we can
 get to the next second at exactly 1 second since the time was displayed.
 
 But displaying the time may take more than 1 second!
 
 So maybe the logic should be:
 
 Get current time and display it. Sleep for 1 - (time taken to display)
 unless (time taken to display) is > 1.
 
 What do you think?
 
 -andyf

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/149130: commit references a PR
Date: Mon,  2 Aug 2010 12:15:35 +0000 (UTC)

 Author: uqs
 Date: Mon Aug  2 12:15:22 2010
 New Revision: 210755
 URL: http://svn.freebsd.org/changeset/base/210755
 
 Log:
   grdc(6): fix timekeeping for user-supplied value n
   
   - Keep timespec 'now' and 'delay' separate to avoid confusion
   - Increase user-supplied n to run _at least_ n seconds, not max n
   
   PR:            bin/149130 (based on)
   Submitted by:  Andy Farkas
   MFC after:     2 weeks
 
 Modified:
   head/games/grdc/grdc.c
 
 Modified: head/games/grdc/grdc.c
 ==============================================================================
 --- head/games/grdc/grdc.c	Mon Aug  2 12:14:17 2010	(r210754)
 +++ head/games/grdc/grdc.c	Mon Aug  2 12:15:22 2010	(r210755)
 @@ -29,7 +29,7 @@
  #define YDEPTH  7
  
  /* it won't be */
 -time_t now; /* yeah! */
 +struct timespec now; /* yeah! */
  struct tm *tm;
  
  short disp[11] = {
 @@ -57,7 +57,7 @@ sighndl(int signo)
  int
  main(int argc, char *argv[])
  {
 -	struct timespec ts;
 +	struct timespec delay;
  	long t, a;
  	int i, j, s, k;
  	int n;
 @@ -89,7 +89,7 @@ main(int argc, char *argv[])
  	}
  
  	if (argc > 0)
 -		n = atoi(*argv);
 +		n = atoi(*argv) + 1;
  	else
  		n = 0;
  
 @@ -135,10 +135,10 @@ main(int argc, char *argv[])
  
  		attrset(COLOR_PAIR(2));
  	}
 -	time(&now);
 +	clock_gettime(CLOCK_REALTIME_FAST, &now);
  	do {
  		mask = 0;
 -		tm = localtime(&now);
 +		tm = localtime(&now.tv_sec);
  		set(tm->tm_sec%10, 0);
  		set(tm->tm_sec/10, 4);
  		set(tm->tm_min%10, 10);
 @@ -192,19 +192,16 @@ main(int argc, char *argv[])
  		}
  		movto(6, 0);
  		refresh();
 -		clock_gettime(CLOCK_REALTIME_FAST, &ts);
 -		if (ts.tv_sec == now) {
 -			if (ts.tv_nsec > 0) {
 -				ts.tv_sec = 0;
 -				ts.tv_nsec = 1000000000 - ts.tv_nsec;
 -			} else {
 -				ts.tv_sec = 1;
 -				ts.tv_nsec = 0;
 -			}
 -			nanosleep(&ts, NULL);
 -			now = ts.tv_sec + 1;
 -		} else
 -			now = ts.tv_sec;
 +		clock_gettime(CLOCK_REALTIME_FAST, &now);
 +		if (delay.tv_nsec > 0) {
 +		    delay.tv_sec = 0;
 +		    delay.tv_nsec = 1000000000 - now.tv_nsec;
 +		} else {
 +		    delay.tv_sec = 1;
 +		    delay.tv_nsec = 0;
 +		}
 +		nanosleep(&delay, NULL);
 +		now.tv_sec++;
  		if (sigtermed) {
  			standend();
  			clear();
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: uqs 
State-Changed-When: Mon Aug 2 12:33:55 UTC 2010 
State-Changed-Why:  
Patched in HEAD, keeping in limbo for MFC reminder ... 

http://www.freebsd.org/cgi/query-pr.cgi?pr=149130 
State-Changed-From-To: patched->closed 
State-Changed-By: uqs 
State-Changed-When: Wed Aug 18 08:28:50 UTC 2010 
State-Changed-Why:  
Patch has been merged to stable/8 after improvements by 
bde have gone in. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=149130 
>Unformatted:
