NetBSD Problem Report #10201

Received: (qmail 1719 invoked from network); 25 May 2000 18:54:09 -0000
Message-Id: <200005251854.LAA02353@nbwww.isc.org>
Date: Thu, 25 May 2000 11:54:08 -0700 (PDT)
From: refling@comet.lbl.gov
Reply-To: refling@comet.lbl.gov
To: gnats-bugs@gnats.netbsd.org
Subject: getrusage microseconds can go backwards in time!
X-Send-Pr-Version: www-1.0

>Number:         10201
>Category:       bin
>Synopsis:       getrusage microseconds can go backwards in time!
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    bin-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu May 25 18:55:00 +0000 2000
>Closed-Date:    Sat Nov 28 19:17:28 +0000 2009
>Last-Modified:  Sat Nov 28 19:17:28 +0000 2009
>Originator:     John P. Refling
>Release:        1.4.2
>Organization:
LBNL
>Environment:
i386 and sparc

>Description:
getrusage microseconds can go backwards in time!

s micro delta
~ ~~~~~ ~~~~~
0 24777 24777
0 31365 6588
0 31611 246
0 31839 228
0 32065 226
0 32294 229
0 32520 226
0 32746 226
...
0 44786 255
0 45039 253
0 48338 3299
0 48628 290
0 48902 274
...
0 54697 270
0 54969 272
0 55240 271
0 58332 3092
0 58724 392
0 59011 287
0 59294 283
...
0 132346 355
0 132701 355
0 133054 353
0 133408 354
0 126362 -7046 BAD
0 126725 363
0 131857 335
0 132191 334
0 132527 336
0 132866 339
0 133204 338
0 126546 -6658 BAD
0 126895 349
0 127209 314
0 127526 317
0 127846 320
...
0 131779 320
0 132097 318
0 132414 317
0 132731 317
0 126431 -6300 BAD
0 126758 327
0 127062 304
...
0 131973 303
0 132272 299
0 132569 297
0 126564 -6005 BAD
0 126876 312
0 127164 288
...
0 171575 313
0 171886 311
0 174797 2911
0 175134 337
0 175453 319
0 175771 318
0 176089 318

etc

>How-To-Repeat:
run the following program:

// John P. Refling ... demonstrates problems with getrusage
// on my sparc LX and i386 on NetBSD.  Not sure what other ports may
// have this problem, or what other sparc machines may have it.
//
// Basically, the microseconds part can occasionally vary
// drastically, both forward in time and BACKWARDS!
//

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

// no problems here on LX: #define LOOP 100000L
// problems #define LOOP 10000L
// problems #define LOOP 1000L
#define LOOP 100L

main() {
    long i, sec, psec = -1L, usec, pusec = 0L, diff;
    struct rusage ru;
    for (;;) {
        getrusage( RUSAGE_SELF, &ru );
        usec = ru.ru_utime.tv_usec;
        sec = ru.ru_utime.tv_sec;
        diff = usec - pusec;

        // neg diff ok if seconds changed
        if (diff >= 0 || sec != psec) {} // printf("%ld %ld %ld\n", sec, usec, usec - pusec);
        else printf("%ld %ld %ld BAD\n", sec, usec, diff);

        // store current, soon to be prior, for next iteration and delay
        psec = sec;
        pusec = usec;
        for (i = 0; i < LOOP; i++);
    }
}

>Fix:


>Release-Note:
>Audit-Trail:

From: Greg Oster <oster@cs.usask.ca>
To: refling@comet.lbl.gov
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/10201: getrusage microseconds can go backwards in time! 
Date: Thu, 25 May 2000 14:40:19 -0600

 refling@comet.lbl.gov writes:
 > 
 > >Number:         10201
 > >Category:       bin
 > >Synopsis:       getrusage microseconds can go backwards in time!

 There is a thread on this from back in November 1997, on the port-hp300 list.
 You should be able to find it at: 

   http://mail-index.netbsd.org/port-hp300/1997/11/

 The program I used to duplicate the problem was:

 ----8<----
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <unistd.h>
 #define   RUSAGE_SELF     0
 main()
 {
         struct rusage my_usage;
         int i,j;

         for(i=0;i<10;i++) {
                 if( getrusage( RUSAGE_SELF, &my_usage ) < 0 ) {
                         perror( "show" );
                         exit( 1 );
                 } else {
                         printf( "%ld + %ld\n", my_usage.ru_utime.tv_sec,
                                 my_usage.ru_utime.tv_usec );
                 }
                 sleep( 2 );
         }
 }
 ----8<----

 on my hp300 box:

 NetBSD 1.4Y (GENERIC) #1: Sat May 13 16:31:25 PDT 2000
     oster@zoot:/build/current/src/sys/arch/hp300/compile/GENERIC
 HP 9000/425t (25MHz MC68040 CPU+MMU+FPU, 4k on-chip physical I/D caches)

 with your little program I get:

 0 117667 -4815 BAD
 0 116952 -4595 BAD
 0 116849 -4417 BAD
 0 116506 -4242 BAD
 0 116035 -4073 BAD
 0 115800 -3936 BAD
 0 115472 -3784 BAD
 0 115275 -3655 BAD
 0 124550 -3708 BAD
 0 124378 -3595 BAD
 0 133808 -3651 BAD
 0 153307 -3867 BAD
 0 153067 -3773 BAD
 0 162439 -3803 BAD
 0 181511 -3964 BAD
 0 181321 -3886 BAD
 0 181250 -3790 BAD
 0 181187 -3708 BAD
 ........

 Now 'way back then' I posted an 'analysis' of this problem:

   http://mail-index.netbsd.org/port-hp300/1997/11/20/0005.html

 but I never received any feedback on how far out-to-lunch I was :)
 If someone would care to sanity-check it, that'd be great :)

 Later...

 Greg Oster


State-Changed-From-To: open->closed
State-Changed-By: dsl@NetBSD.org
State-Changed-When: Sat, 28 Nov 2009 19:17:28 +0000
State-Changed-Why:
Duplicated by 30115


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.