From patchwork Tue Apr 9 16:46:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 32224 Received: (qmail 19204 invoked by alias); 9 Apr 2019 16:46:04 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 19059 invoked by uid 89); 9 Apr 2019 16:46:04 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-23.2 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=randomly, sampling, sk:status_, noticed X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 09 Apr 2019 16:46:03 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id CF9995609E; Tue, 9 Apr 2019 12:46:01 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id a6K4CmZ-w6Hh; Tue, 9 Apr 2019 12:46:01 -0400 (EDT) Received: from murgatroyd.Home (174-29-37-56.hlrn.qwest.net [174.29.37.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPSA id 787B55609D; Tue, 9 Apr 2019 12:46:01 -0400 (EDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Use find_thread_in_random in select_event_lwp Date: Tue, 9 Apr 2019 10:46:00 -0600 Message-Id: <20190409164600.29369-1-tromey@adacore.com> MIME-Version: 1.0 I noticed that find_thread_in_random duplicates the code in find_thread_in_random, so this patch changes the latter to use the former. There are two other spots in gdb that do this, but to unify all of them would require switching some code from using the "iterate over" idiom to using iterators. Another possible improvement is that find_thread_in_random could be made single-pass using reservoir sampling. Tested by the buildbot. gdb/gdbserver/ChangeLog 2019-04-09 Tom Tromey * linux-low.c (select_event_lwp): Use find_thread_in_random. --- gdb/gdbserver/ChangeLog | 4 ++++ gdb/gdbserver/linux-low.c | 35 ++++------------------------------- 2 files changed, 8 insertions(+), 31 deletions(-) diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c index 94af240a5c2..168f4b2abc2 100644 --- a/gdb/gdbserver/linux-low.c +++ b/gdb/gdbserver/linux-low.c @@ -2828,7 +2828,6 @@ linux_wait_for_event (ptid_t ptid, int *wstatp, int options) static void select_event_lwp (struct lwp_info **orig_lp) { - int random_selector; struct thread_info *event_thread = NULL; /* In all-stop, give preference to the LWP that is being @@ -2862,39 +2861,13 @@ select_event_lwp (struct lwp_info **orig_lp) /* No single-stepping LWP. Select one at random, out of those which have had events. */ - /* First see how many events we have. */ - int num_events = 0; - for_each_thread ([&] (thread_info *thread) - { - lwp_info *lp = get_thread_lwp (thread); - - /* Count only resumed LWPs that have an event pending. */ - if (thread->last_status.kind == TARGET_WAITKIND_IGNORE - && lp->status_pending_p) - num_events++; - }); - gdb_assert (num_events > 0); - - /* Now randomly pick a LWP out of those that have had - events. */ - random_selector = (int) - ((num_events * (double) rand ()) / (RAND_MAX + 1.0)); - - if (debug_threads && num_events > 1) - debug_printf ("SEL: Found %d SIGTRAP events, selecting #%d\n", - num_events, random_selector); - - event_thread = find_thread ([&] (thread_info *thread) + event_thread = find_thread_in_random ([&] (thread_info *thread) { lwp_info *lp = get_thread_lwp (thread); - /* Select only resumed LWPs that have an event pending. */ - if (thread->last_status.kind == TARGET_WAITKIND_IGNORE - && lp->status_pending_p) - if (random_selector-- == 0) - return true; - - return false; + /* Only resumed LWPs that have an event pending. */ + return (thread->last_status.kind == TARGET_WAITKIND_IGNORE + && lp->status_pending_p); }); }