From hsu  Sun Jan 28 18:44:20 1996
Received: (from hsu@localhost)
          by freefall.freebsd.org (8.7.3/8.7.3) id SAA20763
          Sun, 28 Jan 1996 18:44:20 -0800 (PST)
Message-Id: <199601290244.SAA20763@freefall.freebsd.org>
Date: Sun, 28 Jan 1996 18:44:20 -0800 (PST)
From: Jeffrey Hsu <hsu>
Reply-To: hsu
To: FreeBSD-gnats-submit@freebsd.org
Subject: getrusage returns negative deltas
X-Send-Pr-Version: 3.2

>Number:         975
>Category:       kern
>Synopsis:       getrusage returns negative deltas
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jan 28 18:50:01 PST 1996
>Closed-Date:    Mon Apr 12 07:15:10 PDT 1999
>Last-Modified:  Mon Apr 12 07:19:11 PDT 1999
>Originator:     Jeffrey &
>Release:        FreeBSD 2.1-STABLE i386 and -current
>Organization:
>Environment:

>Description:

Two calls to getrusage().  The second call indicates that the process has
used less time resource then the first call.

>How-To-Repeat:

Run the following program a bunch of times.

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>

main()
{

	struct rusage r, r2;

	getrusage(RUSAGE_SELF, &r);
	printf("user %d sec %d usec\n", r.ru_utime.tv_sec, r.ru_utime.tv_usec);
	printf("system %d sec %d usec\n", r.ru_stime.tv_sec,
			r.ru_stime.tv_usec);

	getrusage(RUSAGE_SELF, &r2);
	printf("user %d sec %d usec\n", r2.ru_utime.tv_sec,
			r2.ru_utime.tv_usec);
	printf("system %d sec %d usec\n", r2.ru_stime.tv_sec,
			r2.ru_stime.tv_usec);

	if (r2.ru_utime.tv_sec - r.ru_utime.tv_sec < 0)
	  printf("user time negative\n");
	if ((r2.ru_utime.tv_sec == r.ru_utime.tv_sec) && 
		 (r2.ru_utime.tv_usec - r.ru_utime.tv_usec < 0))
	  printf("user time negative\n");
	if (r2.ru_stime.tv_sec - r.ru_stime.tv_sec < 0)
	  printf("system time negative\n");
	if ((r2.ru_stime.tv_sec == r.ru_stime.tv_sec) &&
		 (r2.ru_stime.tv_usec - r.ru_stime.tv_usec < 0))
	  printf("system time negative\n");

	printf("\n");

}

>Fix:
>Release-Note:
>Audit-Trail:

From: Bruce Evans <bde@zeta.org.au>
To: FreeBSD-gnats-submit@FreeBSD.ORG, hsu@freefall.freebsd.org
Cc:  Subject: Re: kern/975: getrusage returns negative deltas
Date: Mon, 29 Jan 1996 15:01:39 +1100

 >Two calls to getrusage().  The second call indicates that the process has
 >used less time resource then the first call.
 
 This is caused by the granularity of the sampling method.  E.g., if a
 process has been running for precisely 30000 usec and has been sampled
 once in user mode and once in system mode, then getrusage() reports
 15000 usec in user mode and 15000 usec in system mode.  30 microseconds
 later, after the process has been sampled once more in system mode,
 getrusage will report 10010 usec in user mode and 20020 usec in system
 mode.  The time in user mode has apparently gone backwards by 4990 usec.
 This affect is only noticable when the sample counts are too small to
 be accurate.
 
 It would be too expensive to use non-statistical sampling.  The clock
 would have to be read for every syscall/interrupt entry and exit.
 
 The times should be fudged so that they never appear to go backwards.
 
 Bruce
Responsible-Changed-From-To: freebsd-bugs->bde 
Responsible-Changed-By: pst 
Responsible-Changed-When: Wed Feb 7 17:58:28 PST 1996 
Responsible-Changed-Why:  
State-Changed-From-To: open->suspended 
State-Changed-By: phk 
State-Changed-When: Mon Apr 13 01:40:01 PDT 1998 
State-Changed-Why:  
->suspended 


Responsible-Changed-From-To: bde->freebsd-bugs 
Responsible-Changed-By: phk 
Responsible-Changed-When: Mon Apr 13 01:40:01 PDT 1998 
Responsible-Changed-Why:  
.. 
State-Changed-From-To: suspended->closed 
State-Changed-By: bde 
State-Changed-When: Mon Apr 12 07:15:10 PDT 1999 
State-Changed-Why:  
Fixed in -current in rev.1.45 of kern_resource.c (etc.). 
Cost/benefit ratio too small to fix it in 3.1-stable or earlier. 
>Unformatted:
