[En-Nut-Discussion] NutGetMillis overflow
José Vallet
jose.vallet at hut.fi
Tue Nov 1 17:09:45 CET 2005
Hello. First of all, I am novice NUT/OS user, just in case ;-)
I need to controll a process on real time, so I need to time-stamp some
measurements. A precission of milliseconds seems to be good enough, so
the function NutGetMillis suits me. Checking the code I saw that it
returns a 32 bits value. However the comments of the function suggest
something different. Then, either there are some "mistakes" in the
comments or I am missing somethig...
In the comments it is suggested that the tick counter overflows every
7.9 years, and I think that that is wrong:
the variable nut_ticks is defined as u_long, so 32 bits. Thus it can
count up to 4294967296 ticks. With a default tick rate of 1024 tics/s,
we can count a maximum of 4294967296/1024=4194304 seconds, which is
1165.084 hours, which is about 48.5 days, not 7.9 years!! This value is
important to my application, so it doesn't start to make weird things
after 48 days... (now I start to understand the "terrible year 2k
effect" ;-))
Thus, the upper bound of milliseconds returned by the function is
4194304000 that fits in a 32 bit number. Actually the returned value is
a 32 bit value (u_long), not a 64 bit value as the comments suggests.
So, now I wonder if I am missing something or is it so that the comment
is the result of a copy-paste-remake-and-re-bake and forgot to update?
;-) Look at that scary line with the comment:
// carefully stay within 64 bit values
I enclose the function in the following lines.
Regards.
José
/*!
* \brief Return the milliseconds counter value.
*
* This function returns the value of a counter, which is incremented
* every system timer tick. During system start, the counter is cleared
* to zero and will overflow with the 64 bit tick counter (4294967296).
* With the default 1024 ticks/s this will happen after 7.9 years.
* The resolution is also given by the system ticks.
*
* \note There is intentionally no provision to modify the seconds counter.
* Callers can rely on a continuous update and use this value for
* system tick independend timeout calculations.
* Depending on
*
* \return Value of the seconds counter.
*/
u_long NutGetMillis(void)
{
// carefully stay within 64 bit values
u_long ticks = NutGetTickCount();
u_long seconds = ticks / NutGetTickClock();
ticks -= seconds * NutGetTickClock();
return seconds * 1000 + (ticks * 1000 ) / NutGetTickClock();
}
More information about the En-Nut-Discussion
mailing list