[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: why is poll+gettimeofday so slow?



Gustavo Vieira Gonçalves Coelho Rios <gustavo.rios@terra.com.br> writes:

> I am trying to discover how much time elapsed between two gettimeofday
> after a sleep of 1 milisecond (10 ^ -3 seconds)
> 
> This is my code:
> 
> 
> 
> #include <sys/types.h>
> #include <sys/time.h>
> 
> #include <poll.h>
> #include <stdio.h>
> #include <unistd.h>
> 
> int
> main(int argc, char **argv)
> {
>         struct timeval  t[3];
>         int             i;
> 
> /*	t[1].tv_sec = 0;
> 	t[1].tv_usec = 1000;
> */ 
>        (void)gettimeofday(t + 2, NULL);
>         poll(NULL, 0, 1); 
> /*      select(0, NULL, NULL, NULL, t + 1); */
>         (void)gettimeofday(t, NULL);
> 
>         t[0].tv_sec -= t[2].tv_sec;
>         if ((t[0].tv_usec -= t[2].tv_usec) < 0) {
>                 t[0].tv_sec--;
>                 t[0].tv_usec += 1000000;
>         }
> 
>         fprintf(stdout, "secs: %u, microsecs: %u\n",
> (unsigned)t[0].tv_sec, (unsigned)t[0].tv_usec);
>         return 0;
> }
> 
> 
> I got surprised when i ran it.
> 
> Altough i asked poll to wait just 1 milisecond, the difference between
> the timestamp where always higher the 10 miliseconds! Of course i tried
> select, but it is even far from delivering microseconds precision.
> 
> How may i get my select/poll/gettimeofday performance improved?

You can't. They are controlled by the internal periodic timeout clock
and on all architechtures except the alpha, that clock is running at
100Hz.

It it possible to increase the resolution of the clock on some
architectures, but that code is not really tested and might skew your
clock badly or even crash the kernel.

Try looking at the HZ define in the kernel a see if you can make it higher.

//art