linuxsetitimer

setitimer on linux rounding up?


When I set a short timeout with setitimer and then query the set value (with getitimer or another setitimer) on a Linux 2.6.26 system (Debian 5.0.5), I get back a value higher than I set:

#include <sys/time.h>
#include <iostream>

int main() {
  struct itimerval wanted, got;

  wanted.it_value.tv_sec     = 0;
  wanted.it_value.tv_usec    = 7000;
  wanted.it_interval.tv_sec  = 0;
  wanted.it_interval.tv_usec = 0;

  setitimer(ITIMER_VIRTUAL, &wanted, NULL);
  getitimer(ITIMER_VIRTUAL, &got);

  std::cerr << "we said: " << wanted.it_value.tv_usec << "\n"
            << "linux set: " << got.it_value.tv_usec << std::endl;

  return 0;
}

returns:

we said: 7000
linux set: 12000

This is problematic, since we use the times reported as remaining after some computations, and they are way too large, too.

Is this a known problem? (googling did not work.) Does anyone have a good workaround?


Solution

  • In the POSIX documentation of the setitimer function there is a note

    Implementations may place limitations on the granularity of timer values. For each interval timer, if the requested timer value requires a finer granularity than the implementation supports, the actual timer value shall be rounded up to the next supported value

    The granularity in your system seems to be higher than 1000 usec (seems to be 6000 usec) and the timer value is rounded up. The timer granularity is the problem if you need such precision.