From tobez@lion.plab.ku.dk Fri Mar  5 08:49:40 1999
Return-Path: <tobez@lion.plab.ku.dk>
Received: from lion.plab.ku.dk (lion.plab.ku.dk [130.225.105.49])
	by hub.freebsd.org (Postfix) with ESMTP id AAE1C151CA
	for <FreeBSD-gnats-submit@freebsd.org>; Fri,  5 Mar 1999 08:49:03 -0800 (PST)
	(envelope-from tobez@lion.plab.ku.dk)
Received: (from tobez@localhost)
	by lion.plab.ku.dk (8.9.3/8.9.1) id RAA23394;
	Fri, 5 Mar 1999 17:48:28 +0100 (CET)
Message-Id: <199903051648.RAA23394@lion.plab.ku.dk>
Date: Fri, 5 Mar 1999 17:48:28 +0100 (CET)
From: tobez@plab.ku.dk
Sender: tobez@lion.plab.ku.dk
Reply-To: tobez@plab.ku.dk
To: FreeBSD-gnats-submit@freebsd.org
Subject: times(3) non-decreaseness broken in 4.0
X-Send-Pr-Version: 3.2

>Number:         10402
>Category:       kern
>Synopsis:       times(3) non-decreaseness broken in 4.0
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Mar  5 08:50:01 PST 1999
>Closed-Date:    Mon Apr 12 07:25:28 PDT 1999
>Last-Modified:  Mon Apr 12 07:32:55 PDT 1999
>Originator:     Anton Berezin
>Release:        FreeBSD 4.0-CURRENT i386
>Organization:
The Protein Laboratory, University of Copenhagen
>Environment:

FreeBSD lion.plab.ku.dk 4.0-CURRENT FreeBSD 4.0-CURRENT #12: Fri Mar  5 13:02:17 CET 1999     root@lion.plab.ku.dk:/usr/src/sys/compile/LION  i386

Different Perls:
perl5.005_02 - standard perl
perl5.005_03-MAINT_TRIAL_6
perl5.005_56

>Description:

(I think the correct category is kern;  I might be wrong)

When testing recent versions of Perl on also-recent 4.0-current, a
strange test failure consistently occurs.  I dug this a little bit,
and it turned out that sometimes the second (or n-th) call to times(3)
can return a value of tms_stime less than that of the first call.

Perl's times() directly translates into a call to times(3) plus
divisions to get values in seconds.

Running a test script below (in How-To-Repeat section) sufficient
number of times, I've got (among the other values)

0 0.0078125
0 0 (i = 100000, beg = 920651342, time = 920651344)
----------------

and

0 0.0078125
0.0078125 0 (i = 100000, beg = 920651344, time = 920651347)
----------------

which clearly shows that system time can actually decrease.

>How-To-Repeat:

Create a perl script
----------------------8<---------------------8<---------------
#! /usr/bin/perl

($beguser,$begsys) = times;
$beg = time;

for ($i = 0; $i < 100000; $i++) {
    ($nowuser, $nowsys) = times;
    $i = 200000 if $nowuser > $beguser && ( $nowsys > $begsys || 
                                            (!$nowsys && !$begsys));
    last if time - $beg > 20;
}
my $ttt = time;
if ($i < 200000) {
   print "$beguser $begsys\n";
   print "$nowuser $nowsys (i = $i, beg = $beg, time = $ttt)\n";
   print "----------------\n";
}
----------------------8<---------------------8<---------------

and run it several times, better in a shell loop.

>Fix:
	

>Release-Note:
>Audit-Trail:

From: amagai@nue.org
To: freebsd-gnats-submit@freebsd.org, tobez@plab.ku.dk
Cc:  
Subject: Re: kern/10402: times(3) non-decreaseness broken in 4.0
Date: Wed, 10 Mar 1999 17:23:11 +0900 (JST)

 Dear, I have a same problem.  CPU consumption time is decreased occasionally.
 Environment: FreeBSD 2.2.8-RELEASE, FreeBSD 3.1-RELEASE
 
 ================================================================
 How to repeat: compile and run this program.
 
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/resource.h>
 
 struct rusage rusage;
 main ()
 {
   int x = 0;
   int y;
   int us, uu, ss, su;
   for(;;) {
     getrusage (0, &rusage);
     y = rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
       + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec;
     
     if (y < x) {
       fprintf(stderr, "%d, previous %d %d\n", y, x, y-x);
       fprintf(stderr, "\tuser %d %d   sys %d %d\n", us, uu, ss, su);
       fprintf(stderr, "\tuser %ld %ld   sys %ld %ld\n", 
 	      rusage.ru_utime.tv_sec, rusage.ru_utime.tv_usec,
 	      rusage.ru_stime.tv_sec, rusage.ru_stime.tv_usec);
     }
     x = y;
     us = rusage.ru_utime.tv_sec;
     uu = rusage.ru_utime.tv_usec;
     ss = rusage.ru_stime.tv_sec;
     su = rusage.ru_stime.tv_usec;
   }
 }
 ================================================================
 problem: I make a trace of a funcion calcru in /usr/src/sys/kern/kern_resources.c
 
            p->p_sticks  p->p_uticks  p->p_iticks totusec(adjusted)
 a call   :    76            13          37       928620
 next call:    76            13          38       928793
 
 utime and stime (in micro second) are evaluated as follows:
 
 utime = (totusec * p->p_uticks) / (p->p_sticks + p->p_uticks + p->p_iticks);
 stime = (totusec * p->p_sticks) / (p->p_sticks + p->p_uticks + p->p_iticks);
 
            p->p_sticks  p->p_uticks  p->p_iticks totusec  utime+stime
 a call   :    76            13          37       928620    655930
 next call:    76            13          38       928793    650886
                                                  decrease    5044 usec
 
 fix:
   May be difficult....
 
 Thanks
 
 Yoshiji Amagai
 New Unified Environment Research Project
 amagai@nue.org
 
State-Changed-From-To: open->closed 
State-Changed-By: bde 
State-Changed-When: Mon Apr 12 07:25:28 PDT 1999 
State-Changed-Why:  
Fixed in rev.1.44 of kern_resource.c. 
Fixed a related, much older, much smaller bug in rev.1.45 of kern_resource.c 
(etc.).  See PR 975. 
>Unformatted:
