Re: Accurate timers in rumpkernels on Xen / minios

  • From: George Dunlap <dunlapg@xxxxxxxxx>
  • To: pooka@xxxxxx
  • Date: Fri, 3 Jun 2016 16:09:53 +0100

On Sat, May 28, 2016 at 12:19 PM, Antti Kantee <pooka@xxxxxx> wrote:

On 27/05/16 14:04, George Dunlap wrote:

On Fri, May 27, 2016 at 1:17 PM, Antti Kantee <pooka@xxxxxx> wrote:

Well, try this hacky patch.  It's not correct by any stretches of
imagination, nor am I promising to accept something like it into Rumprun,
but if you can repeat my results, at least we've narrowed down the
problem.
In my testing the clock_gettime-to-clock_gettime delay for a 1ns sleep on
KVM is ~8us with this patch.

(btw, the init code might not in correct order as indicated by the
comment,
try to hack around it somehow if that happens)


Hmm, it didn't work as advertised; the code seems to have run, but the
same issue remains:


Then I don't know.  It made a difference for me.  There's something to be
said about the scientific process of posting the actual code to allow others
to repeat the experiment.  Here's mine:

=== snip ===
#include <sys/types.h>

#include <stdio.h>
#include <time.h>
#include <unistd.h>

int
main()
{
        struct timespec ts, ts1, ts2;

        ts.tv_sec = 0;
        ts.tv_nsec = 1000;

        clock_gettime(CLOCK_REALTIME, &ts1);
        nanosleep(&ts, NULL);
        clock_gettime(CLOCK_REALTIME, &ts2);

        timespecsub(&ts2, &ts1, &ts);
        printf("%ld\n", ts.tv_nsec);
}
=== snip ===

Ah -- right; it turns out that I wasn't actually using nanosleep()
inside that "nsleep() call, I was using pselect.  (I think that's
because I had determined that would be the most portable, but I can't
remember why now.)

In any case, with nanosleep() and this patch, then I'm getting nice
small deltas:

{ "Now":1000782714, "Mops":0, "MaxDelta":0 }
{ "Now":2055607406, "Mops":300, "MaxDelta":25834 }
{ "Now":3057985717, "Mops":580, "MaxDelta":8441 }
{ "Now":4060254047, "Mops":860, "MaxDelta":6169 }
{ "Now":5062553556, "Mops":1140, "MaxDelta":10956 }
{ "Now":6068979087, "Mops":1420, "MaxDelta":5955 }

Thanks for your help.

It's a bit strange that NetBSD clips the sleep time to the clock HZ --
that seems like it's probably an artifact of a bygone age.

"patches welcome", and all that.

I'll see what I can do. :-)

 -George

Other related posts: