All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeremy Fitzhardinge <jeremy@goop.org>
To: Dan Magenheimer <dan.magenheimer@oracle.com>
Cc: kurt.hackel@oracle.com, Avi Kivity <avi@redhat.com>,
	"Xen-Devel (E-mail)" <xen-devel@lists.xensource.com>,
	Keir Fraser <keir.fraser@eu.citrix.com>,
	Tim Deegan <Tim.Deegan@citrix.com>
Subject: Re: rdtsc in userspace
Date: Fri, 23 Oct 2009 17:59:17 -0700	[thread overview]
Message-ID: <4AE25165.9030904@goop.org> (raw)
In-Reply-To: <df154acb-234d-45f7-9617-a290e1297b1b@default>

On 10/23/09 15:51, Dan Magenheimer wrote:
> In measuring rdtsc usage in the kernel, both Jeremy
> and I noticed that compiling the kernel causes a
> large number of userland rdtsc's.  At first I thought
> that this meant that gcc was using rdtsc, but gcc
> sources do not show any use of rdtsc.  Next I suspected
> bash, but it doesn't either.

I think the dynamic linker may use rdtsc as a piece of randomness for
randomizing the addresses of libraries and other mappings.

>   Finally, it appears that
> the calls are occurring from glibc, and searching for
> uses of rdtsc, I found that glibc has its
> own implementation of clock_gettime that uses rdtsc
> directly!
>   

When I run clock_gettime on my system it uses the vsyscall version
(which is either a syscall or, with my recent patches, all in usermode).

Using strace on your test program should show whether its really
bypassing the syscall or not.

> The manpage for clock_gettime ("man 3 clock_gettime")
> has a lengthy caveat about using clock_gettime on
> an SMP system BUT provides a mechanism to test to
> see if your SMP system is safe, clock_getcpuclockid(0).
>   

My manpages don't have anything like that for clock_gettime().

> See for example (near the end):
>
> http://linux.die.net/man/3/clock_gettime
>   

That caveat is for CLOCK_PROCESS_CPUTIME_ID which doesn't seem
reasonable to count on for monotonicity (my manpages
CLOCK_PROCESS_CPUTIME_ID just refer to as "High resolution per-process
timer.").   CLOCK_REALTIME or CLOCK_MONOTONIC is a different matter.

> But testing this on a system I know is unsafe, the
> test says my system is safe!  Looking at the source
> code, I can see why... the test is a compile-time
> test, not a run-time test, and on x86 it appears that
> it will always pass.
>
> So I wrote a simple test program that uses clock_gettime().
> It does indeed do userland rdtsc's.
>   

Using what CLOCK_?

> So... any program that uses clock_gettime to approximate
> the passage of time is subject to break on Xen across
> a migration.  Even if the programmer uses the prescribed
> method for testing "safeness", it is subject to break.
>   

I always see clock_gettime(CLOCK_MONOTONIC or CLOCK_REALTIME, ...) make
a syscall or vsyscall.  If you're testing any of the thread-local times
then I think its less interesting.

    J

  reply	other threads:[~2009-10-24  0:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-23 22:51 rdtsc in userspace Dan Magenheimer
2009-10-24  0:59 ` Jeremy Fitzhardinge [this message]
2009-10-25  1:21   ` Dan Magenheimer
2009-11-02 18:44     ` Dan Magenheimer

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=4AE25165.9030904@goop.org \
    --to=jeremy@goop.org \
    --cc=Tim.Deegan@citrix.com \
    --cc=avi@redhat.com \
    --cc=dan.magenheimer@oracle.com \
    --cc=keir.fraser@eu.citrix.com \
    --cc=kurt.hackel@oracle.com \
    --cc=xen-devel@lists.xensource.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.