public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 8/8] add non-stop test that stresses thread starvation issues
Date: Fri, 26 Dec 2014 20:32:00 -0000	[thread overview]
Message-ID: <1419625871-28848-9-git-send-email-palves@redhat.com> (raw)
In-Reply-To: <1419625871-28848-1-git-send-email-palves@redhat.com>

This commit adds a non-stop mode test originally inspired by
signal-while-stepping-over-bp-other-thread.exp, that exposes the
thread starvation issues fixed by the previous patches.  It sets a set
of threads stepping in parallel, and has one of them get a signal.
Without the previous fixes, this would fail with timeouts.

gdb/testsuite/
2014-12-26  Pedro Alves  <palves@redhat.com>

	* gdb.threads/non-stop-fair-events.c: New file.
	* gdb.threads/non-stop-fair-events.exp: New file.
---
 gdb/testsuite/gdb.threads/non-stop-fair-events.c   |  84 +++++++++++
 gdb/testsuite/gdb.threads/non-stop-fair-events.exp | 161 +++++++++++++++++++++
 2 files changed, 245 insertions(+)
 create mode 100644 gdb/testsuite/gdb.threads/non-stop-fair-events.c
 create mode 100644 gdb/testsuite/gdb.threads/non-stop-fair-events.exp

diff --git a/gdb/testsuite/gdb.threads/non-stop-fair-events.c b/gdb/testsuite/gdb.threads/non-stop-fair-events.c
new file mode 100644
index 0000000..421c766
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/non-stop-fair-events.c
@@ -0,0 +1,84 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <pthread.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <signal.h>
+
+#define NUM_THREADS 10
+const int num_threads = NUM_THREADS;
+
+pthread_t child_thread[NUM_THREADS];
+volatile pthread_t signal_thread;
+volatile int got_sig;
+
+void
+handler (int signo)
+{
+  got_sig = 1;
+}
+
+void
+loop_broke (void)
+{
+}
+
+#define INF_LOOP				\
+  do						\
+    {						\
+      while (!got_sig)				\
+	;					\
+    }						\
+  while (0)
+
+void *
+child_function (void *arg)
+{
+  pthread_t self = pthread_self ();
+
+  while (1)
+    {
+      INF_LOOP; /* set thread breakpoint here */
+      loop_broke ();
+    }
+}
+
+int
+main (void)
+{
+  int res;
+  int i;
+
+  alarm (60);
+
+  signal (SIGUSR1, handler);
+
+  for (i = 0; i < NUM_THREADS; i++)
+    {
+      res = pthread_create (&child_thread[i], NULL, child_function, NULL);
+    }
+
+  while (1)
+    {
+      pthread_kill (signal_thread, SIGUSR1); /* set kill breakpoint here */
+      INF_LOOP;
+      loop_broke ();
+    }
+
+  exit(EXIT_SUCCESS);
+}
diff --git a/gdb/testsuite/gdb.threads/non-stop-fair-events.exp b/gdb/testsuite/gdb.threads/non-stop-fair-events.exp
new file mode 100644
index 0000000..944a2d3
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/non-stop-fair-events.exp
@@ -0,0 +1,161 @@
+# Copyright (C) 2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test that GDB in non-stop mode gives roughly equal priority to
+# events of all threads.
+
+standard_testfile
+set executable ${testfile}
+
+if [target_info exists gdb,nosignals] {
+    verbose "Skipping ${testfile}.exp because of nosignals."
+    return -1
+}
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {
+    return -1
+}
+
+gdb_test_no_output "set non-stop on"
+
+if ![runto_main] {
+    return -1
+}
+
+# We want "handle print", to make sure the target backend reports the
+# signal to the run control core.
+gdb_test "handle SIGUSR1 print nostop pass" ""
+
+# Get current value of VAR from the inferior.  TEST is used as test
+# message.
+
+proc get_value {var test} {
+    global expect_out
+    global gdb_prompt
+    global decimal
+
+    set value -1
+    gdb_test_multiple "print $var" "$test" {
+	-re ".*= ($decimal).*\r\n$gdb_prompt $" {
+	    set value $expect_out(1,string)
+	    pass "$test"
+        }
+    }
+    return ${value}
+}
+
+set NUM_THREADS [get_value "num_threads" "get num_threads"]
+
+# Account for the main thread.
+incr NUM_THREADS
+
+# Run threads to their start positions.  This prepares for a new test
+# sequence.
+
+proc restart {} {
+    global gdb_prompt
+    global NUM_THREADS
+
+    delete_breakpoints
+
+    gdb_test "print got_sig = 0" " = 0"
+
+    gdb_breakpoint [gdb_get_line_number "set thread breakpoint here"]
+    gdb_breakpoint [gdb_get_line_number "set kill breakpoint here"]
+
+    set test "continue -a&"
+    gdb_test_multiple $test $test {
+	-re "Continuing.\r\n$gdb_prompt " {
+	    pass $test
+	}
+    }
+
+    for {set i 1} { $i <= $NUM_THREADS } { incr i } {
+	set test "thread $i restarted"
+	gdb_test_multiple "" $test {
+	    -re "breakpoint here" {
+		# The prompt was already matched in the "continue &"
+		# test above.  We're now consuming asynchronous output
+		# that comes after the prompt.
+		pass $test
+	    }
+	}
+    }
+
+    delete_breakpoints
+}
+
+# The test proper.  SIGNAL_THREAD is the thread that has been elected
+# to receive the SIGUSR1 signal.
+
+proc test {signal_thread} {
+    global gdb_prompt
+    global NUM_THREADS
+
+    with_test_prefix "signal_thread=$signal_thread" {
+	restart
+
+	# Set all threads stepping the infinite loop line in parallel.
+	for {set i 2} { $i <= $NUM_THREADS } { incr i } {
+	    gdb_test "thread $i" \
+		"child_function.*set thread breakpoint here.*" \
+		"switch to thread $i to step it"
+
+	    if {$i == $signal_thread} {
+		gdb_test "print signal_thread = self" " = .*"
+	    }
+
+	    gdb_test "step&" "" "set $i thread stepping"
+	}
+
+	gdb_test "thread 1" "Switching to .*" \
+	    "switch to the main thread to queue signal"
+
+	# Let the main thread queue the signal.
+	gdb_breakpoint "loop_broke"
+	set test "continue &"
+	gdb_test_multiple $test $test {
+	    -re "Continuing.\r\n$gdb_prompt " {
+		pass $test
+	    }
+	}
+
+	# Wait for all threads to finish their steps, and for the main
+	# thread to hit the breakpoint.
+	for {set i 1} { $i <= $NUM_THREADS } { incr i } {
+	    set test "thread $i broke out of loop"
+	    gdb_test_multiple "" $test {
+		-re "loop_broke" {
+		    # The prompt was already matched in the "continue
+		    # &" test above.  We're now consuming asynchronous
+		    # output that comes after the prompt.
+		    pass $test
+		}
+	    }
+	}
+
+	# It's helpful to have this in the log if the test ever
+	# happens to fail.
+	gdb_test "info threads"
+    }
+}
+
+# The kernel/debug API may always walk its thread list looking for the
+# first with an event, resulting in giving priority to e.g. the thread
+# with lowest kernel thread ID.  So test once with the signal pending
+# in each thread, except the main thread.
+for {set i 2} { $i <= $NUM_THREADS } { incr i } {
+    test $i
+}
-- 
1.9.3

  parent reply	other threads:[~2014-12-26 20:32 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-26 20:31 [PATCH 0/8] Linux: starvation avoidance in non-stop mode Pedro Alves
2014-12-26 20:31 ` [PATCH 3/8] cleanup and speed up (software_)breakpoint_inserted_here_p Pedro Alves
2014-12-26 20:31 ` [PATCH 2/8] watch_thread_num.exp and targets with fairer event reporting Pedro Alves
2014-12-26 20:31 ` [PATCH 1/8] gdb.threads/{siginfo-thread.c,watchthreads-reorder.c,ia64-sigill.c} races with GDB Pedro Alves
2015-01-06  6:20   ` Yao Qi
2014-12-26 20:32 ` [PATCH 5/8] linux-nat.c: always mark execing LWP as resumed Pedro Alves
2014-12-26 20:32 ` Pedro Alves [this message]
2015-04-02 14:53   ` [PATCH 8/8] add non-stop test that stresses thread starvation issues Yao Qi
2015-04-06 11:26     ` Pedro Alves
2015-04-07 10:10       ` Yao Qi
2015-04-07 10:22         ` Pedro Alves
2015-04-07 10:31           ` Yao Qi
2014-12-26 20:32 ` [PATCH 6/8] linux-nat.c: better starvation avoidance, handle non-stop mode too Pedro Alves
2015-01-07  7:06   ` Yao Qi
2015-01-07 13:22     ` Pedro Alves
2015-01-07 14:08       ` Yao Qi
2015-01-07 14:36         ` Pedro Alves
2014-12-26 20:32 ` [PATCH 7/8] [gdbserver] linux-low.c: " Pedro Alves
2014-12-26 20:32 ` [PATCH 4/8] linux-nat.c: clean up pending status checking and resuming LWPs Pedro Alves
2015-01-06  8:12   ` Yao Qi
2015-01-07 13:22     ` Pedro Alves
2015-01-07 14:10       ` Yao Qi
2015-01-09 15:07 ` [PATCH 0/8] Linux: starvation avoidance in non-stop mode Pedro Alves

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=1419625871-28848-9-git-send-email-palves@redhat.com \
    --to=palves@redhat.com \
    --cc=gdb-patches@sourceware.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).