Re: [patch] __volatile__ needed in get_cycles()?

Mike Galbraith (mikeg@weiden.de)
Wed, 31 Mar 1999 21:30:40 +0200 (CEST)


On Wed, 31 Mar 1999, Andrea Arcangeli wrote:

> On Tue, 30 Mar 1999, Andrea Arcangeli wrote:
>
> >>Yes, I like the above, I did not know you can do
> >>
> >>__asm__ __volatile__("": :);
> >>
> >>to stop compiler from re-ordering things (because I never looked at wmb()
> >>macro). (and I like the "pseudo-smiley" at the end :).
> >
> >Infact you can't do that (thanks to MikeG for make me noticing this) ;).
> >It's been my fault. While I thought that the C compiler shouldn't look
> >throught the contents of the asm (""), it seems he is optimizing it away
> >(too much smart ;) even if it's an "nop" and not a "".
>
> Well as Horst pointed out to me the C compiler shouldn't really optimize
> it away! It looks like a C compiler bug. (I know it's offtopic but I post
> it here too because maybe somebody read my previous email).

It's not off-topic, compiler technicalities are very important in the
kernel. As to it being optimized away.. it didn't. I think that your
initial look was dead center.. the second rdtsc was combined with the
first, which is a lot different from throwing it away. (I would find
it easy to view combination of two rdtsc insns as a bug tho, given the
purpose of that insn :)

> If somebody need a testcase ask to MikeG ;).

In case anyone is interested, here's the code. Is it a bug? I wonder..
all gcc/egcs versions here do basically the same thing. (combine)

typedef unsigned long cycles_t;

static inline cycles_t get_cycles (void)
{
unsigned long eax, edx;

__asm__ ("rdtsc":"=a" (eax), "=d" (edx));
return eax;
}

static inline cycles_t get_cycles_ordered(void)
{
/* I know I should not use `register' but I can't resist ;). -Andrea */
register cycles_t foo;

__asm__ __volatile__ ("lock; addl $0,0(%%esp)": :);
foo = get_cycles();
__asm__ __volatile__("": :);

return foo;
}

extern cycles_t tsca, tscb, diff;

void darn()
{
tsca = tscb = get_cycles_ordered(); /*just wanted to see:)*/
tscb = get_cycles_ordered();
diff = tscb - tsca;
}

-Mike

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/