Re: gethrtime() is returning negative value on LINUX



In article <1174112370.366020.64530@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>, "linux_bp" <rishabh.garg@xxxxxxxxx> writes:
hi
we had implemented gethrtime on LINUX machine using the following
code:
long long gethrtime(void)
{

[ ... ]

long long now;
asm volatile ("rdtsc" : "=A" (now) : : "memory");
now = (now*1000)/scaleFactor;
return now;
[ ... ]

Now on our customer setup this function starts returning negative
value after sometime i.e after couple of days.
from the logs we can find the scale factor to be 3060 and the returned
value -2940642805558051. What could be the reason of this negative
value? Though the system was up only for 40 days.
regards,
rishabh

So do the math manually:
1) What would be the value of the TSC after the system has been
up for 40 days.
2) Now multiply that by a thousand.
3) What's wrong with this value relative to your algorithm and
your choice of a 64-bit signed int to hold it?

Is this really code from your deployed system? If so:
1) I suggest that you do your calculations in floating point.
You will avoid problems like this and you won't lose any
precision given that you're getting CPU speed from /proc.
2) You fall off the end of your code with no explicitly returned
value if anything goes wrong with your parsing.
3) You're leaking file handles like a sieve.
4) The rdtsc instruction shouldn't need a "memory" directive
to asm. One might possibly want serialization, but this
routine will operate so slowly that it won't matter in this
case.
5) Given that you're using (relatively) slow functions like
open and reading from /proc, why do you need a high precision
value such as the TSC? Why not just use the current time
(e.g., gettimeofday or clock_gettime)? (Plus current time
doesn't have the problem of potentially unsynchronized TSCs
on a multi-processor system.)

- dmw

--
.. Douglas Wells . Connection Technologies .
.. Internet: -sp9804- -at - contek.com- .
.