public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Tom Marshall <tmarshall@real.com>
To: linux-kernel@vger.kernel.org
Subject: missed itimer signals in 2.6
Date: Mon, 13 Oct 2003 14:46:49 -0700	[thread overview]
Message-ID: <20031013214649.GB13516@real.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 1220 bytes --]

It seems that with the 2.6 kernel, ITIMER_REAL becomes inaccurate when the
interval is set under about 50ms.  Using a 10ms interval, I get 455 signals
in five seconds (about 9% loss).  The test machine has virtually no load and
this is reproducible across machines of varying speeds.  The test
application is attached.

Typical output under 2.4.22:

  i=10, ticks=501 (expected 500), elapsed=5.008537, drift=+0.001468
  i=20, ticks=251 (expected 250), elapsed=5.019925, drift=+0.000076
  i=30, ticks=167 (expected 166), elapsed=5.009974, drift=+0.000027
  i=40, ticks=126 (expected 125), elapsed=5.039981, drift=+0.000020
  i=50, ticks=101 (expected 100), elapsed=5.049980, drift=+0.000021

Typical output under 2.6.0-test7:

  i=10, ticks=455 (expected 500), elapsed=5.003323, drift=-0.453316
  i=20, ticks=239 (expected 250), elapsed=5.018035, drift=-0.238033
  i=30, ticks=162 (expected 166), elapsed=5.021115, drift=-0.161113
  i=40, ticks=122 (expected 125), elapsed=5.001113, drift=-0.121111
  i=50, ticks=99 (expected 100), elapsed=5.048102, drift=-0.098101

-- 
Nothing is illegal if a hundred businessmen decide to do it, and that's true
anywhere in the world.
        -- Andrew Young

[-- Attachment #1.2: itimertest.c --]
[-- Type: text/x-csrc, Size: 2481 bytes --]

/*
 * test itimer
 */

#include <stdlib.h>
#include <stdio.h>

#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>

#define TEST_INTERVAL_SEC 5

static inline void
tv_sub(struct timeval* ptv1, struct timeval* ptv2)
{
    ptv1->tv_sec  -= ptv2->tv_sec;
    ptv1->tv_usec -= ptv2->tv_usec;
    while (ptv1->tv_usec < 0)
    {
        ptv1->tv_sec--;
        ptv1->tv_usec += 1000*1000;
    }
}

static uint             g_ticks;
static uint             g_interval;
static struct timeval   g_now;

void
itimer_handler(int sig)
{
    g_ticks++;
    g_now.tv_usec += g_interval*1000;
    while (g_now.tv_usec > 1000*1000)
    {
        g_now.tv_usec -= 1000*1000;
        g_now.tv_sec++;
    }
}

void
start_itimer(uint ms)
{
    struct timeval tv;
    struct itimerval val;

    tv.tv_sec = ms/1000;
    tv.tv_usec = (ms%1000)*1000;

    val.it_interval = tv;
    val.it_value = tv;

    signal(SIGALRM, itimer_handler);
    g_ticks = 0;
    g_interval = ms;
    gettimeofday(&g_now, NULL);
    setitimer(ITIMER_REAL, &val, NULL);
}

void
stop_itimer(void)
{
    struct timeval tv;
    struct itimerval val;

    tv.tv_sec = tv.tv_usec = 0;
    val.it_interval = tv;
    val.it_value = tv;

    setitimer(ITIMER_REAL, &val, NULL);
    signal(SIGALRM, SIG_DFL);
}

int
main(void)
{
    int i;
    struct timeval tv_start;
    struct timeval tv_now;

    char drift_sign;
    struct timeval tv_drift;
    struct timeval tv_elapsed;

    for (i = 10; i <= 50; i += 10)
    {
        gettimeofday(&tv_start, NULL);
        start_itimer(i);
        do
        {
            usleep(1000*1000);
            gettimeofday(&tv_now, NULL);
            tv_elapsed = tv_now;
            tv_sub(&tv_elapsed, &tv_start);
        } while (tv_elapsed.tv_sec < TEST_INTERVAL_SEC);
        stop_itimer();

        if (g_now.tv_sec > tv_now.tv_sec ||
            (g_now.tv_sec == tv_now.tv_sec &&
             g_now.tv_usec > tv_now.tv_usec))
        {
            drift_sign = '+';
            tv_drift = g_now;
            tv_sub(&tv_drift, &tv_now);
        }
        else
        {
            drift_sign='-';
            tv_drift = tv_now;
            tv_sub(&tv_drift, &g_now);
        }
        printf("i=%d, ticks=%u (expected %u), elapsed=%ld.%06ld, drift=%c%ld.%06ld\n",
                i, g_ticks, (TEST_INTERVAL_SEC*1000/i),
                tv_elapsed.tv_sec, tv_elapsed.tv_usec,
                drift_sign, tv_drift.tv_sec, tv_drift.tv_usec);
    }
    return 0;
}

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

                 reply	other threads:[~2003-10-13 21:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20031013214649.GB13516@real.com \
    --to=tmarshall@real.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox