From: "H. Peter Anvin" <hpa@zytor.com>
To: "Michael S. Tsirkin" <mst@redhat.com>, Ingo Molnar <mingo@kernel.org>
Cc: linux-kernel@vger.kernel.org,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>,
x86@kernel.org, Rusty Russell <rusty@rustcorp.com.au>,
Linus Torvalds <torvalds@linux-foundation.org>,
Uros Bizjak <ubizjak@gmail.com>
Subject: Re: [PATCH 1/2] x86/bitops: implement __test_bit
Date: Tue, 01 Sep 2015 16:48:52 -0700 [thread overview]
Message-ID: <55E63964.6000404@zytor.com> (raw)
In-Reply-To: <20150901155824-mutt-send-email-mst@redhat.com>
On 09/01/2015 08:03 AM, Michael S. Tsirkin wrote:
>>>
>>> Hmm - so do you take back the ack?
>>
>> I have no strong feelings either way, it simply strikes me as misguided to
>> explicitly optimize for something that is listed as a high overhead instruction.
>>
>
> [mst@robin test]$ diff a.c b.c
> 31c31
> < if (__variable_test_bit(1, &addr))
> ---
> > if (__constant_test_bit(1, &addr))
>
> [mst@robin test]$ gcc -Wall -O2 a.c; time ./a.out
>
> real 0m0.532s
> user 0m0.531s
> sys 0m0.000s
> [mst@robin test]$ gcc -Wall -O2 b.c; time ./a.out
>
> real 0m0.517s
> user 0m0.517s
> sys 0m0.000s
>
>
> So __constant_test_bit is faster even though it's using more
> instructions
> $ gcc -Wall -O2 a.c; -objdump -ld ./a.out
>
I think this is well understood. The use of bts/btc in locked
operations is sometimes justified since it reports the bit status back
out, whereas in unlocked operations bts/btc has no benefit except for
code size. bt is a read operation, and is therefore "never/always"
atomic; it cannot be locked because there is no read/write pair to lock.
So it is strictly an issue of code size versus performance.
However, your test is simply faulty:
804843f: 50 push %eax
8048440: 6a 01 push $0x1
8048442: e8 b4 ff ff ff call 80483fb <__variable_test_bit>
You're encapsulating the __variable_test_bit() version into an expensive
function call, whereas the __constant_test_bit() seems to emit code that
is quite frankly completely bonkers insane:
8048444: 8b 45 ec mov -0x14(%ebp),%eax
8048447: 83 e0 1f and $0x1f,%eax
804844a: 89 c1 mov %eax,%ecx
804844c: d3 ea shr %cl,%edx
804844e: 89 d0 mov %edx,%eax
8048450: 83 e0 01 and $0x1,%eax
8048453: 85 c0 test %eax,%eax
8048455: 0f 95 c0 setne %al
8048458: 0f b6 c0 movzbl %al,%eax
804845b: 85 c0 test %eax,%eax
804845d: 74 00 je 804845f <main+0x64>
Observe the sequence and/test/setne/movzbl/test!
-hpa
prev parent reply other threads:[~2015-09-01 23:49 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-30 8:38 [PATCH 1/2] x86/bitops: implement __test_bit Michael S. Tsirkin
2015-08-30 8:38 ` [PATCH 2/2] kvm/x86: use __test_bit Michael S. Tsirkin
2015-08-31 6:05 ` [PATCH 1/2] x86/bitops: implement __test_bit Ingo Molnar
2015-08-31 6:13 ` H. Peter Anvin
2015-08-31 7:56 ` Michael S. Tsirkin
2015-08-31 7:59 ` Ingo Molnar
2015-08-31 8:15 ` yalin wang
2015-08-31 8:19 ` Ingo Molnar
2015-08-31 8:15 ` Ingo Molnar
2015-08-31 11:19 ` Michael S. Tsirkin
2015-09-01 9:24 ` Ingo Molnar
2015-09-01 9:40 ` Michael S. Tsirkin
2015-09-01 11:39 ` Ingo Molnar
2015-09-01 15:03 ` Michael S. Tsirkin
2015-09-01 23:48 ` H. Peter Anvin [this message]
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=55E63964.6000404@zytor.com \
--to=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=mingo@redhat.com \
--cc=mst@redhat.com \
--cc=rusty@rustcorp.com.au \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.org \
--cc=ubizjak@gmail.com \
--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