negative response time with gettimeofday
flag
Messages 1 - 10 of 13 - Collapse all
/groups/adfetch?adid=qncblRAAAADPY6ppPjx6sSfd8SV8wiVV
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
1.  yuanhao1...@gmail.com  
View profile  
 More options Feb 11 2007, 10:06 pm
Newsgroups: comp.os.linux.development.apps
From: yuanhao1...@gmail.com
Date: 11 Feb 2007 19:06:10 -0800
Local: Sun, Feb 11 2007 10:06 pm
Subject: negative response time with gettimeofday
I hope someone can help me with negative time measurements I am
getting.
Surely i'm doing something silly but cannot find it.

code:
                struct timeval tvStart,tvEnd;
                double linStart = 0,linEnd = 0,lTime = 0;

                gettimeofday (&tvStart,NULL);
               //do something
                gettimeofday (&tvEnd,NULL);

                linStart = ((double)tvStart.tv_sec * 1000000 +
(double)tvStart.tv_usec);  //unit S
                linEnd = ((double)tvEnd.tv_sec * 1000000 +
(double)tvEnd.tv_usec);        //unit S
                lTime = linEnd-linStart;
                if(lTime <= 0)
               {
                        .......
               }
But  lTime is negative rarely;
Linux version 2.4.21-37.ELsmp (gcc version 3.2.3 20030502 (Red Hat
Linux 3.2.3-53)) #10 SMP Fri Apr 28 00:06:06 CST 2006
Thanks for any help


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
2.  David Schwartz  
View profile  
 More options Feb 12 2007, 12:33 am
Newsgroups: comp.os.linux.development.apps
From: "David Schwartz" <dav...@webmaster.com>
Date: 11 Feb 2007 21:33:20 -0800
Local: Mon, Feb 12 2007 12:33 am
Subject: Re: negative response time with gettimeofday
On Feb 11, 7:06 pm, yuanhao1...@gmail.com wrote:

> I hope someone can help me with negative time measurements > I am
> getting.
> Surely i'm doing something silly but cannot find it.

> code:
>                 struct timeval tvStart,tvEnd;
>                 double linStart = 0,linEnd = 0,lTime = 0;

>                 gettimeofday (&tvStart,NULL);

You are using the wrong function. The 'gettimeofday' function gets the
system's best guess at wall time. This *can* go backwards. What you
want is 'clock_gettime(CLOCK_MONOTONIC)' which measures the elaspsed
time from an arbitrary reference time. (Usually the system uptime, but
that's not guaranteed.)

DS


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
3.  Bin Chen  
View profile  
 More options Feb 12 2007, 4:35 am
Newsgroups: comp.os.linux.development.apps
From: "Bin Chen" <binary.c...@gmail.com>
Date: 12 Feb 2007 01:35:42 -0800
Local: Mon, Feb 12 2007 4:35 am
Subject: Re: negative response time with gettimeofday
On 2月12日, 下午1时33分, "David Schwartz" <dav...@webmaster.com> wrote:

As I know, the Linux kernel read the system RTC(wall time) and then
maintain the Wall time through OS timer, but why the gettimeofday not
accurate?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
4.  David Schwartz  
View profile  
 More options Feb 12 2007, 4:41 am
Newsgroups: comp.os.linux.development.apps
From: "David Schwartz" <dav...@webmaster.com>
Date: 12 Feb 2007 01:41:16 -0800
Local: Mon, Feb 12 2007 4:41 am
Subject: Re: negative response time with gettimeofday
On Feb 12, 1:35 am, "Bin Chen" <binary.c...@gmail.com> wrote:

> As I know, the Linux kernel read the system RTC(wall time) and then
> maintain the Wall time through OS timer, but why the gettimeofday not
> accurate?

What makes you think it's not accurate?

DS


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
5.  Bin Chen  
View profile  
 More options Feb 12 2007, 5:02 am
Newsgroups: comp.os.linux.development.apps
From: "Bin Chen" <binary.c...@gmail.com>
Date: 12 Feb 2007 02:02:08 -0800
Local: Mon, Feb 12 2007 5:02 am
Subject: Re: negative response time with gettimeofday
On 2月12日, 下午5时41分, "David Schwartz" <dav...@webmaster.com> wrote:
> On Feb 12, 1:35 am, "Bin Chen" <binary.c...@gmail.com> wrote:

> > As I know, the Linux kernel read the system RTC(wall time) and then
> > maintain the Wall time through OS timer, but why the gettimeofday not
> > accurate?

> What makes you think it's not accurate?

You said that it can go backwards. If it will go back of the time, is
it accurate. Oh, yes, it is wrong.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
6.  David Schwartz  
View profile  
 More options Feb 12 2007, 6:43 am
Newsgroups: comp.os.linux.development.apps
From: "David Schwartz" <dav...@webmaster.com>
Date: 12 Feb 2007 03:43:47 -0800
Local: Mon, Feb 12 2007 6:43 am
Subject: Re: negative response time with gettimeofday
On Feb 12, 2:02 am, "Bin Chen" <binary.c...@gmail.com> wrote:

> > What makes you think it's not accurate?
> You said that it can go backwards. If it will go back of the
> time, is
> it accurate. Oh, yes, it is wrong.

The purpose of 'gettimeofday' is to get the system's best guess at
actual wall clock time. This can go backwards.

For example, suppose you check the time on a very accurate clock. It
reads "10:03". About 40 minutes pass and I ask you what time it is.
You say, "my best guess is 10:43". Then you look at the clock again
and see it is 10:31. I ask you what time it is about a minute later
and you say, "my best guess is 10:32".

In both cases you accurately gave me your best estimate of wall clock
time. Nevertheless, the time went backwards.

You can ask for a time that is guaranteed monotonic or you can ask for
the best guess of wall time, but there is no way to get both at the
same time. The function you are using requests the system's best guess
at actual wall clock time, and it can be working perfectly and still
go backwards.

DS


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7.  Bin Chen  
View profile  
 More options Feb 12 2007, 7:35 am
Newsgroups: comp.os.linux.development.apps
From: "Bin Chen" <binary.c...@gmail.com>
Date: 12 Feb 2007 04:35:48 -0800
Local: Mon, Feb 12 2007 7:35 am
Subject: Re: negative response time with gettimeofday
On 2月12日, 下午7时43分, "David Schwartz" <dav...@webmaster.com> wrote:

Thanks for your explanation.

> In both cases you accurately gave me your best estimate of wall clock
> time. Nevertheless, the time went backwards.

> You can ask for a time that is guaranteed monotonic or you can ask for
> the best guess of wall time, but there is no way to get both at the
> same time. The function you are using requests the system's best guess
> at actual wall clock time, and it can be working perfectly and still
> go backwards.

> DS

I have some questions after I dive into the kernel source, as you
said, the kernel just estimate the time when you call the
gettimeofday(). I remember this *guess* is done by accumulating each
jiffy that created by OS timer.

But I don't find the code that kernel read the RTC(wall time) to
calibrate the guessed value. If all the time the kernel is guessing,
there is no reason that it guess backwards.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
8.  David Schwartz  
View profile  
 More options Feb 12 2007, 8:00 am
Newsgroups: comp.os.linux.development.apps
From: "David Schwartz" <dav...@webmaster.com>
Date: 12 Feb 2007 05:00:51 -0800
Local: Mon, Feb 12 2007 8:00 am
Subject: Re: negative response time with gettimeofday
On Feb 12, 4:35 am, "Bin Chen" <binary.c...@gmail.com> wrote:

> I have some questions after I dive into the kernel source, as you
> said, the kernel just estimate the time when you call the
> gettimeofday(). I remember this *guess* is done by accumulating each
> jiffy that created by OS timer.

Correct.

> But I don't find the code that kernel read the RTC(wall time) to
> calibrate the guessed value.

It tracks wall time itself in most hardware configurations you are
likely to encounter. If the TSC is usable, it will periodically work
out calibration values to convert the TSC to wall time.

> If all the time the kernel is guessing,
> there is no reason that it guess backwards.

If you need/want monotonic time, there are functions for that. But
'gettimeofday' *specifically* gives up monotonicity in exchange for a
better estimate of wall time.

There are plenty of reasons it guesses backwards. The kernel usually
has at least two time sources, one that indicates time boundaries and
one that indicates time passage. If the source of time passage speeds
up a bit, the next boundary may result in the time going a bit
backwards.

For example, suppose someone tells you the time periodically (this is
like the timer interrupt). You also calibrate your own clock (this is
like the TSC). If the TSC speeds up a bit, you might think it's 10:01
when you're told it's 10:00. You'll immediately slow your own clock
down to compensate, but at that instant, your best guess of wall time
will go backwards.

Simply put, having monotonicity (never going backwards) and making the
best possible guess of wall clock time are *conflicting* requirements.
You specifically used the function that trades one for the other.

DS


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
9.  Bin Chen  
View profile  
 More options Feb 12 2007, 8:52 am
Newsgroups: comp.os.linux.development.apps
From: "Bin Chen" <binary.c...@gmail.com>
Date: 12 Feb 2007 05:52:23 -0800
Local: Mon, Feb 12 2007 8:52 am
Subject: Re: negative response time with gettimeofday
On 2月12日, 下午9时00分, "David Schwartz" <dav...@webmaster.com> wrote:

I have no idea how it works. It will calibrate the OS timer hardware
so that the HZ to be generated a little faster or slower? Or it just
generate a periodic timer interrupt that let kernel know a given time
has passed? If the latter, which interrupt will service this?
> > If all the time the kernel is guessing,
> > there is no reason that it guess backwards.

> If you need/want monotonic time, there are functions for that. But
> 'gettimeofday' *specifically* gives up monotonicity in exchange for a
> better estimate of wall time.

I don't know why it is a *good* estimation compare to read the RTC
directly each time the gettimeofday is called.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
10.  David Schwartz  
View profile  
 More options Feb 12 2007, 9:32 am
Newsgroups: comp.os.linux.development.apps
From: "David Schwartz" <dav...@webmaster.com>
Date: 12 Feb 2007 06:32:37 -0800
Local: Mon, Feb 12 2007 9:32 am
Subject: Re: negative response time with gettimeofday
On Feb 12, 5:52 am, "Bin Chen" <binary.c...@gmail.com> wrote:

> I don't know why it is a *good* estimation compare to read the RTC
> directly each time the gettimeofday is called.

There are at least three reasons the RTC is not used:

The RTC resolution varies but is seldom better than 1/18th of a
second. In contrast, the TSC or HPET can have nanosecond resolution.
(You are asking for the system's best guess of wall clock time, why
should it return something that is *not* that?)

Second, the RTC is very expensive to read. It takes quite a bit of I/O
to read the RTC and 'gettimeofday' can be very frequently called in
some applications (for example consider a server streaming multimedia
over UDP). In comparison, reading the TSC takes one instruction.

Third, the RTC is notoriously inaccurate and cannot really be
disciplined, largely because of its low resolution. In comparison, the
other time sources are much more stable and have enough resolution to
be disciplined.

I believe most modern computers read the RTC only on startup.

DS


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google