From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 1J3+KJs21WIAghUAWB0awg (envelope-from ) for ; Mon, 18 Jul 2022 06:31:55 -0400 Received: by simark.ca (Postfix, from userid 112) id 974F01E5EA; Mon, 18 Jul 2022 06:31:55 -0400 (EDT) Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=KDdNFoHR; dkim-atps=neutral X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,NICE_REPLY_A,RDNS_DYNAMIC, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 1A8301E21F for ; Mon, 18 Jul 2022 06:31:55 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CDAEC3857B9B for ; Mon, 18 Jul 2022 10:31:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CDAEC3857B9B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1658140313; bh=/epw9HKYTz5/gDL0uCdqeMPx++69qI/D2+LlSSMmQFA=; h=Date:Subject:To:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=KDdNFoHRRB93ZvwPtz7c0cxkEoOt9O3Lad7DZybDYKayiZ1h0Vv8abU/D9wcfjkPH vyN/QHRocE1GxOEVXetHxk28zoK4W4CE+0kaKjteFgPjoJN0/wfyRN8EglMyzhreDA nPNGY9rZBDNU2s6Lt+gPo9ARe6qbrrBEzAoy1el4= Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 6B3C33858424 for ; Mon, 18 Jul 2022 10:31:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6B3C33858424 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id B496D20DFB for ; Mon, 18 Jul 2022 10:31:32 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id A374E13A37 for ; Mon, 18 Jul 2022 10:31:32 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id ZxyYJoQ21WJXTgAAMHmgww (envelope-from ) for ; Mon, 18 Jul 2022 10:31:32 +0000 Message-ID: <07df96c3-74ed-50ca-add8-cfb29b9eb63b@suse.de> Date: Mon, 18 Jul 2022 12:31:32 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCH][gdbsupport] Avoid main thread in parallel for with thread sanitizer Content-Language: en-US To: gdb-patches@sourceware.org References: <20220718100520.GA26230@delia.home> In-Reply-To: <20220718100520.GA26230@delia.home> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Tom de Vries via Gdb-patches Reply-To: Tom de Vries Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" On 7/18/22 12:05, Tom de Vries wrote: > Hi, > > When running tasks in a parallel for, some elements may be handled by a worker > thread and others by the main thread. > > When running with -fsanitize=thread, avoid the main thread in the parallel > for, to prevent multi-threading problems being hidden. > FWIW, in terms of parallel_for_each_debug and the example I've been using, with -fsanitize=thread, we have: ... Parallel for: n_elements: 7271 Parallel for: minimum elements per thread: 10 Parallel for: elts_per_thread: 1817 Parallel for: elements on worker thread 0 : 1818 Parallel for: elements on worker thread 1 : 1818 Parallel for: elements on worker thread 2 : 1818 Parallel for: elements on worker thread 3 : 1817 Parallel for: elements on main thread : 0 ... and without: ... Parallel for: elements on worker thread 0 : 1818 Parallel for: elements on worker thread 1 : 1818 Parallel for: elements on worker thread 2 : 1818 Parallel for: elements on worker thread 3 : 0 Parallel for: elements on main thread : 1817 ... Thanks, - Tom > Tested on x86_64-linux, with -fsanitize=thread. > > Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29359 > > Any comments? > > Thanks, > - Tom > > [gdbsupport] Avoid main thread in parallel for with thread sanitizer > > --- > gdbsupport/parallel-for.h | 26 +++++++++++++++++++++++++- > 1 file changed, 25 insertions(+), 1 deletion(-) > > diff --git a/gdbsupport/parallel-for.h b/gdbsupport/parallel-for.h > index bf40f125f0f..81a2cfa215b 100644 > --- a/gdbsupport/parallel-for.h > +++ b/gdbsupport/parallel-for.h > @@ -24,6 +24,15 @@ > #include > #include "gdbsupport/thread-pool.h" > > +/* GCC does not understand __has_feature. */ > +#if !defined(__has_feature) > +# define __has_feature(x) 0 > +#endif > + > +#if __has_feature(thread_sanitizer) > +#define __SANITIZE_ADDRESS__ 1 > +#endif > + > namespace gdb > { > > @@ -161,7 +170,22 @@ parallel_for_each (unsigned n, RandomIt first, RandomIt last, > /* n_elements == n_threads * elts_per_thread + elts_left_over. */ > } > > - size_t count = n_threads == 0 ? 0 : n_threads - 1; > + /* Number of worker threads we're going to use. */ > + size_t count; > +#ifdef __SANITIZE_THREAD__ > + /* Avoid the main thread. */ > + count = (n_threads == 0 > + ? (/* Only use the first worker thread. */ > + 1) > + : (/* Only use worker threads. */ > + n_threads)); > +#else > + count = (n_threads == 0 > + ? (/* Only use the main thread. */ > + 0) > + : (/* Leave at least one worker thread unused. */ > + n_threads - 1)); > +#endif > gdb::detail::par_for_accumulator results (count); > > if (parallel_for_each_debug)