Subject: Re: Timer roll over in microtime()
To: None <tech-kern@NetBSD.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-kern
Date: 07/19/2006 12:40:05
>    struct timeval    now; 
>    microtime(&now);
>    printf ("\n Process: [%d] Time:[%ld]",
>                 p->p_pid,
>                 now.tv_sec*100000 + now.tv_usec);

>  05:47:42: Process:[13] Time:[2145071681]
>  05:48:13  Process:[13] Time:[-2146785080] 
>  05:48:13  Process:[13] Time:[-2146785080]

> how do i handle this?

This is not actually a problem in microtime; it's simple integer
overflow.

First, you probably want 1000000, not 100000.

Two suggestions.  (a), one that applies only to printing:

	printf("Process: [%d] Time: [%lu%06lu]\n",
		p->p_pid,
		(unsigned long int)now.tv_sec,
		(unsigned long int)now.tv_usec);

(I also changed the formatting - most things expect newlines at the
ends of lines, not the beginning; intermixing your style of printf with
the other printfs throughout the kernel will be a mismash of blank
lines and collapsed lines.)

(b), one that applies more generally: use a larger type.  In this
example, I use unsigned long long int:

	printf("Process: [%d] Time: [%llu]\n",
		p->p_pid,
		(now.tv_sec*1000000ULL)+now.tv_usec );

The ULL makes the constant unsigned long long int, and type contagion
extends this to the whole expression.

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B