From: Ingo Molnar <mingo@elte.hu>
To: Andy Lutomirski <luto@MIT.EDU>
Cc: x86@kernel.org, linux-kernel@vger.kernel.org,
John Stultz <johnstul@us.ibm.com>,
Thomas Gleixner <tglx@linutronix.de>
Subject: Re: [PATCH 1/6] x86-64: Optimize vread_tsc's barriers
Date: Tue, 29 Mar 2011 08:18:25 +0200 [thread overview]
Message-ID: <20110329061825.GB27398@elte.hu> (raw)
In-Reply-To: <bd92441ac72b3aa162dcd58422334e2091095c33.1301324270.git.luto@mit.edu>
* Andy Lutomirski <luto@MIT.EDU> wrote:
> @@ -767,18 +767,38 @@ static cycle_t read_tsc(struct clocksource *cs)
> static cycle_t __vsyscall_fn vread_tsc(void)
> {
> cycle_t ret;
> -
> - /*
> - * Surround the RDTSC by barriers, to make sure it's not
> - * speculated to outside the seqlock critical section and
> - * does not cause time warps:
> + u64 zero, last;
> +
> + /* rdtsc is unordered, and we want it to be ordered like
> + * a load with respect to other CPUs (and we don't want
> + * it to execute absurdly early wrt code on this CPU.)
> + * rdtsc_barrier() is a barrier that provides this ordering
> + * with respect to *earlier* loads. (Which barrier to use
> + * depends on the CPU.)
> */
> rdtsc_barrier();
> - ret = (cycle_t)vget_cycles();
> - rdtsc_barrier();
>
> - return ret >= __vsyscall_gtod_data.clock.cycle_last ?
> - ret : __vsyscall_gtod_data.clock.cycle_last;
> + asm volatile ("rdtsc\n\t"
> + "shl $0x20,%%rdx\n\t"
> + "or %%rdx,%%rax\n\t"
> + "shl $0x20,%%rdx"
> + : "=a" (ret), "=d" (zero) : : "cc");
> +
> + /* zero == 0, but as far as the processor is concerned, zero
> + * depends on the output of rdtsc. So we can use it as a
> + * load barrier by loading something that depends on it.
> + * x86-64 keeps all loads in order wrt each other, so this
> + * ensures that rdtsc is ordered wrt all later loads.
> + */
> +
> + /* This doesn't multiply 'zero' by anything, which *should*
> + * generate nicer code, except that gcc cleverly embeds the
> + * dereference into the cmp and the cmovae. Oh, well.
> + */
> + last = *( (cycle_t *)
> + ((char *)&__vsyscall_gtod_data.clock.cycle_last + zero) );
> +
> + return ret >= last ? ret : last;
Looks like GCC hurts performance here more than it helps. Have you considered
putting the whole function into assembly in a .S file? How maintainable does it
look like?
Thanks,
Ingo
next prev parent reply other threads:[~2011-03-29 6:18 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-03-28 15:06 [PATCH 0/6] x86-64: Micro-optimize vclock_gettime Andy Lutomirski
2011-03-28 15:06 ` [PATCH 1/6] x86-64: Optimize vread_tsc's barriers Andy Lutomirski
2011-03-29 6:18 ` Ingo Molnar [this message]
2011-03-28 15:06 ` [PATCH 2/6] x86-64: Don't generate cmov in vread_tsc Andy Lutomirski
2011-03-29 6:15 ` Ingo Molnar
2011-03-29 11:52 ` Andrew Lutomirski
2011-03-28 15:06 ` [PATCH 3/6] x86-64: Put vsyscall_gtod_data at a fixed virtual address Andy Lutomirski
2011-03-28 17:49 ` Thomas Gleixner
2011-03-28 18:09 ` Andrew Lutomirski
2011-03-28 21:35 ` Andrew Lutomirski
2011-03-28 23:13 ` Thomas Gleixner
2011-03-28 15:06 ` [PATCH 4/6] x86-64: vclock_gettime(CLOCK_MONOTONIC) can't ever see nsec < 0 Andy Lutomirski
2011-03-29 6:21 ` Ingo Molnar
2011-03-29 11:54 ` Andrew Lutomirski
2011-03-28 15:06 ` [PATCH 5/6] x86-64: Omit frame pointers on vread_tsc Andy Lutomirski
2011-03-29 6:24 ` Ingo Molnar
2011-03-28 15:06 ` [PATCH 6/6] x86-64: Turn off -pg and turn on -foptimize-sibling-calls for vDSO Andy Lutomirski
2011-03-29 6:27 ` [PATCH 0/6] x86-64: Micro-optimize vclock_gettime Ingo Molnar
2011-04-06 18:20 ` Andi Kleen
2011-04-06 20:10 ` Andrew Lutomirski
2011-04-06 20:14 ` Andi Kleen
2011-04-06 20:49 ` Andrew Lutomirski
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=20110329061825.GB27398@elte.hu \
--to=mingo@elte.hu \
--cc=johnstul@us.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@MIT.EDU \
--cc=tglx@linutronix.de \
--cc=x86@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
all inboxes | Powered by JetHome®