[En-Nut-Discussion] systime limit reduces valid uptime to 49 days

Michael Müller mmueller at cs.uni-kassel.de
Wed Nov 7 14:35:10 CET 2007


Hi all,

I wrote a little workaround for the discussed overflow problem. It is 
quite special for the default configuration to avoid too many 
calculations with every function call. It also prevents larger variable 
types and the change if system tick.

/* 2^32 ( sizeof(long) ) / 1024 (ticks / s) */
#define TICK_OVERFLOW_FACTOR		0x400000	

u_long NutGetSeconds(void)
{
	static int tickOverflowCounter = 0;
	static u_long lastTickCount = 0;
	u_long currentTickCount;
	
	currentTickCount = NutGetTickCount();
	
	/* overflow occurred? */
	if ( lastTickCount > currentTickCount )
		tickOverflowCounter++;
	
	lastTickCount = currentTickCount;
	
	return ( NutGetTickCount() / NutGetTickClock() )   +
		   ( tickOverflowCounter * TICK_OVERFLOW_FACTOR  );
}

My former described test program survived the overflow this way. I 
assume that NutGetSeconds is called at least once every 49days.

@Harald: yes the calculation of unix time vs sys ticks and everything 
around is quite mind bending ;-) I somehow kept 1970 in mind...

Another affected part (I hope the only one - just trusted in the 
reference search function of Eclipse) is the NutTimerProcessElapsed 
function. I think it is useful to consider the overflow here, too.

void NutTimerProcessElapsed(void)
{
     NUTTIMERINFO *tn;
     u_long ticks;
     u_long ticks_new;

     // calculate ticks since last call
     ticks = NutGetTickCount();

     /* overflow in ticks? */
     if ( nut_ticks_resume > ticks )
     {
     	/* hope it survives the compiler optimization */
     	/* little trick to stay inside 32bit variable space */
     	ticks_new = (0xffffffff - nut_ticks_resume);
     	ticks_new += ticks + 1;
     }
     else
     {
     	ticks_new = ticks - nut_ticks_resume;
     }

     nut_ticks_resume = ticks;
...


Best regards
  Michael




Harald Kipp schrieb:
> Michael Müller schrieb:
>> 07.11.2007|07:00:58 tick count: 4294966901
>> 19.09.2007|18:55:55 tick count: 631
>>
>>
>> I can´t explain the date as I expected something far more in the past 
>> but anyway the result is invalid.
>>   
> Why further in the past? You explained earlier, that the overflow takes 
> place after 49 days. Without re-reading my HP Calculator Manual on how 
> to do calendar calculations, it looks like 49 days to me.
> 
> As we all know, time and date calculations can be quite mind boggling. :-)
> 
> Harald
> 
> _______________________________________________
> http://lists.egnite.de/mailman/listinfo/en-nut-discussion
> 




More information about the En-Nut-Discussion mailing list