title: nanoseconds from clock_gettime() on mips router
body: <p>I am trying to get nanosecond resolution from <code>CLOCK_REALTIME</code> on a 1GHz MIPS router. When I compile the below code for x86 and run on a 1GHz vm, I get nanosecond resolution. When I compile for mips and run on a 1GHz router, it appears to round up to the microsecond.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  
  int main(int argc, char **argv, char **arge) {
    struct timespec tps, tpe;
    if ((clock_gettime(CLOCK_REALTIME, &amp;tps) != 0)
        || (clock_gettime(CLOCK_REALTIME, &amp;tpe) != 0)) {
      perror("clock_gettime");
      return -1;
    }
    printf("%lu s, %lu ns\n", tpe.tv_sec-tps.tv_sec,
       tpe.tv_nsec-tps.tv_nsec);
    return 0;
  }
  </code></pre>
  
  <p>Here are sample times:</p>
  
  <pre><code>vagrant@vagrant-ubuntu-trusty-64:~$ gcc clock.c -o clock_x86 -static -lrt
  vagrant@vagrant-ubuntu-trusty-64:~$ ./clock_x86
  0 s, 221 ns
  
  
  vagrant@vagrant-ubuntu-trusty-64:~$ mipsel-openwrt-linux-gcc clock.c -lrt -static -o clock_mips
  &lt;...scp...&gt;
  root@OpenWrt:~# ./clock_mips
  0 s, 3000 ns
  </code></pre>
  
  <p>Is there something I am misunderstanding about clocks, MIPS, processors, etc? I would expect a call to <code>CLOCK_REALTIME</code> to produce nanosecond granularity/resolution, regardless of processor architecture.</p>
  
  <p>Further, if anyone could shed light on how these POSIX timers are implemented and how they are getting their measurements, I would appreciate it.</p>