This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
RE: [PATCH] Fix sporadic failure in g++.dg/tsan/aligned_vs_unaligned_race.C
- From: Bernd Edlinger <bernd dot edlinger at hotmail dot de>
- To: Jakub Jelinek <jakub at redhat dot com>
- Cc: Mike Stump <mikestump at comcast dot net>, "H.J. Lu" <hjl dot tools at gmail dot com>, "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>, Dmitry Vyukov <dvyukov at google dot com>
- Date: Tue, 6 Jan 2015 18:45:49 +0100
- Subject: RE: [PATCH] Fix sporadic failure in g++.dg/tsan/aligned_vs_unaligned_race.C
- Authentication-results: sourceware.org; auth=none
- References: <DUB118-W8F5E63DE36A8DB2DA1DF6E45B0 at phx dot gbl>,<623A5348-6FC9-4F7B-A9BC-B2B098AF7D37 at comcast dot net>,<20150104191658 dot GK1667 at tucnak dot redhat dot com>,<DUB118-W450ACE7162969EFB27F817E45B0 at phx dot gbl>,<8E43F8AA-96BA-47A3-A886-C058459B4108 at comcast dot net>,<DUB118-W286F157F7B8B1EE05ED51FE4580 at phx dot gbl>,<E67B07D7-6ABA-48C1-B58B-B804144D91C2 at comcast dot net>,<D86529BC-DB94-481A-AE60-913D7E2B8D7F at comcast dot net>,<DUB118-W1E901D082BACEDE02A5E8E4590 at phx dot gbl>,<DUB118-W468072AA78C7E7CD1DCF6BE4590 at phx dot gbl>,<20150106091633 dot GF1667 at tucnak dot redhat dot com>
----------------------------------------
> Date: Tue, 6 Jan 2015 10:16:33 +0100
> From: jakub@redhat.com
> To: bernd.edlinger@hotmail.de
> CC: mikestump@comcast.net; hjl.tools@gmail.com; gcc-patches@gcc.gnu.org; dvyukov@google.com
> Subject: Re: [PATCH] Fix sporadic failure in g++.dg/tsan/aligned_vs_unaligned_race.C
>
> On Tue, Jan 06, 2015 at 10:08:22AM +0100, Bernd Edlinger wrote:
>> Hi Mike,
>>
>> after some hours of sleep I realized that your step function can do something very interesting,
>> (which you already requested previously):
>>
>> That is: create a race condition that is _always_ at 100% missed by tsan:
>>
>> cat lib.c
>> /* { dg-do compile } */
>> /* { dg-options "-O2 -fno-sanitize=all" } */
>>
>> static volatile int serial = 0;
>>
>> void step (int i)
>> {
>> while (__atomic_load_n (&serial, __ATOMIC_ACQUIRE) != i - 1);
>> __atomic_store_n (&serial, i, __ATOMIC_RELEASE);
>> }
>
> Such busy waiting is not very CPU time friendly in the testsuite, especially
> if you have just a single HW thread.
> If libtsan is not fixed not to be so racy, perhaps instead of all the sleeps
> we could arrange (on x86_64-linux, which is the only target supporting tsan
> right now) to make sure the thread runs on the same core/hw thread as the
> initial thread using pthread_[gs]etaffinity_np/pthread_attr_setaffinity_np ?
> Does tsan then always report the races when the threads can't run
> concurrently?
>
> Jakub
I tried your suggestion now, and it seems to work. (on a 4-way core AMD64 laptop)
Would you prefer this over adding a sleep in Thread1, which I posted previously?
2015-01-06 Bernd Edlinger <bernd.edlinger@hotmail.de>
* g++.dg/tsan/aligned_vs_unaligned_race.C: Fixed sporadic failure.
Index: aligned_vs_unaligned_race.C
===================================================================
--- aligned_vs_unaligned_race.C (revision 219198)
+++ aligned_vs_unaligned_race.C (working copy)
@@ -1,5 +1,7 @@
/* { dg-shouldfail "tsan" } */
+/* { dg-additional-options "-pthread" } */
#include <pthread.h>
+#include <sched.h>
#include <stdio.h>
#include <stdint.h>
@@ -19,6 +21,12 @@ void *Thread2(void *x) {
}
int main() {
+ /* Run this test on a single CPU, to make it somewhat easier for tsan to
+ detect the race condition. */
+ cpu_set_t s;
+ CPU_ZERO(&s);
+ CPU_SET(0, &s);
+ pthread_setaffinity_np(pthread_self(), sizeof(s), &s);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
Thanks,
Bernd.