[Olsr-users] Broadcast Packets & Windows Routin

Aaron Kaplan (spam-protected)
Thu Mar 13 19:53:40 CET 2008


On Mar 13, 2008, at 7:02 PM, Juliusz Chroboczek wrote:

>>> According to the man page of select(2):
>
>>> "The select function may update the timeout parameter to indicate
>>> how much time was left."
>
>>> What worries me here is the word "may". It would be quite useful
>>> to know the time left. But that is a worry for the implementor.
>
> No, don't do that, it's not portable and error-prone.  Just call
> gettimeofday before and after select, and subtract the two.
>
> (If you *really* want to be correct, call clock_gettime 
> (CLOCK_MONOTONIC),
> but that's not portable.)

I would throw in the following neat trick which only works on totally  
non-portable pentium 3 minimum systems:
(But you do get true nanosec resolution)

#include <stdio.h>
#include <asm-x86_64/msr.h>


#define CPUFREQ 22113730000   /* 2.2 GHz */

#define kHz 1000ULL
#define MHz kHz*1000ULL
#define GHz MHz*1000ULL


static inline unsigned long long read_tsc(void)
{
         unsigned long high, low;
         __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high));
         return ((unsigned long long)high << 32) | low;
}

int main(int argc, char **argv)
{
         unsigned long long t1,t2;
         unsigned long long t3;
         long double d;



while(1) {
         t1=read_tsc();
         t2=read_tsc();

         t3 = t2-t1;
         printf("%Lu\n", t1);
         printf("%Lu\n", t2);
         d = (long double)t3/(long double)CPUFREQ;
         printf("%2.18Lf\n", d);
         printf("-----------\n");
         printf("%Lf (sec), %Lf (ms), %Lf (usec), %Lf (nsec)\n", d,
         d*kHz, d*MHz, d*GHz);
}
         return 0;
}




Take that swiss watchmakers ;-))))


>
>> BTW the select(2) sys-call is dying out and now sometimes just a  
>> wrapper
>> (e.g. in the GNU-libc) function of poll(2).
>
> I believe you are wrong.  Select is a native system call (it doesn't
> call poll) on both Linux and BSD-derived systems.
>
>







More information about the Olsr-users mailing list