Add a test for P1 that checks that multiple threads can properly wake
authorBen Pfaff <blp@cs.stanford.edu>
Thu, 18 May 2006 02:55:09 +0000 (02:55 +0000)
committerBen Pfaff <blp@cs.stanford.edu>
Thu, 18 May 2006 02:55:09 +0000 (02:55 +0000)
up at the same clock tick.

src/tests/threads/Make.tests
src/tests/threads/Rubric.alarm
src/tests/threads/alarm-simultaneous.c [new file with mode: 0644]
src/tests/threads/alarm-simultaneous.ck [new file with mode: 0644]
src/tests/threads/alarm-wait.c
src/tests/threads/tests.c
src/tests/threads/tests.h

index 5a7b400493d916b08f0a4538de98cb89d2976242..0b6d532eddc103714a4ddeba437753e5fd296814 100644 (file)
@@ -2,16 +2,18 @@
 
 # Test names.
 tests/threads_TESTS = $(addprefix tests/threads/,alarm-single          \
-alarm-multiple alarm-priority alarm-zero alarm-negative                        \
-priority-change priority-donate-one priority-donate-multiple           \
-priority-donate-multiple2 priority-donate-nest priority-donate-sema    \
-priority-fifo priority-preempt priority-sema priority-condvar          \
-mlfqs-load-1 mlfqs-load-60 mlfqs-load-avg mlfqs-recent-1 mlfqs-fair-2  \
-mlfqs-fair-20 mlfqs-nice-2 mlfqs-nice-10)
+alarm-multiple alarm-simultaneous alarm-priority alarm-zero            \
+alarm-negative priority-change priority-donate-one                     \
+priority-donate-multiple priority-donate-multiple2                     \
+priority-donate-nest priority-donate-sema priority-fifo                        \
+priority-preempt priority-sema priority-condvar mlfqs-load-1           \
+mlfqs-load-60 mlfqs-load-avg mlfqs-recent-1 mlfqs-fair-2 mlfqs-fair-20 \
+mlfqs-nice-2 mlfqs-nice-10)
 
 # Sources for tests.
 tests/threads_SRC  = tests/threads/tests.c
 tests/threads_SRC += tests/threads/alarm-wait.c
+tests/threads_SRC += tests/threads/alarm-simultaneous.c
 tests/threads_SRC += tests/threads/alarm-priority.c
 tests/threads_SRC += tests/threads/alarm-zero.c
 tests/threads_SRC += tests/threads/alarm-negative.c
index b2d44a3ea80cc979ca8ad2a1cdc114d6b7120c85..61abe85e348350b37082760997b8936dce55bc66 100644 (file)
@@ -1,7 +1,8 @@
 Functionality and robustness of alarm clock:
-5      alarm-single
-5      alarm-multiple
-5      alarm-priority
+4      alarm-single
+4      alarm-multiple
+4      alarm-simultaneous
+4      alarm-priority
 
 1      alarm-zero
 1      alarm-negative
diff --git a/src/tests/threads/alarm-simultaneous.c b/src/tests/threads/alarm-simultaneous.c
new file mode 100644 (file)
index 0000000..7db5418
--- /dev/null
@@ -0,0 +1,94 @@
+/* Creates N threads, each of which sleeps a different, fixed
+   duration, M times.  Records the wake-up order and verifies
+   that it is valid. */
+
+#include <stdio.h>
+#include "tests/threads/tests.h"
+#include "threads/init.h"
+#include "threads/malloc.h"
+#include "threads/synch.h"
+#include "threads/thread.h"
+#include "devices/timer.h"
+
+static void test_sleep (int thread_cnt, int iterations);
+
+void
+test_alarm_simultaneous (void) 
+{
+  test_sleep (5, 5);
+}
+
+/* Information about the test. */
+struct sleep_test 
+  {
+    int64_t start;              /* Current time at start of test. */
+    int iterations;             /* Number of iterations per thread. */
+    int *output_pos;            /* Current position in output buffer. */
+  };
+
+static void sleeper (void *);
+
+/* Runs THREAD_CNT threads thread sleep ITERATIONS times each. */
+static void
+test_sleep (int thread_cnt, int iterations) 
+{
+  struct sleep_test test;
+  int *output;
+  int i;
+
+  /* This test does not work with the MLFQS. */
+  ASSERT (!enable_mlfqs);
+
+  msg ("Creating %d threads to sleep %d times each.", thread_cnt, iterations);
+  msg ("Each thread sleeps 10 ticks each time.");
+  msg ("Within an iteration, all threads should wake up on the same tick.");
+
+  /* Allocate memory. */
+  output = malloc (sizeof *output * iterations * thread_cnt * 2);
+  if (output == NULL)
+    PANIC ("couldn't allocate memory for test");
+
+  /* Initialize test. */
+  test.start = timer_ticks () + 100;
+  test.iterations = iterations;
+  test.output_pos = output;
+
+  /* Start threads. */
+  ASSERT (output != NULL);
+  for (i = 0; i < thread_cnt; i++)
+    {
+      char name[16];
+      snprintf (name, sizeof name, "thread %d", i);
+      thread_create (name, PRI_DEFAULT, sleeper, &test);
+    }
+  
+  /* Wait long enough for all the threads to finish. */
+  timer_sleep (100 + iterations * 10 + 100);
+
+  /* Print completion order. */
+  msg ("iteration 0, thread 0: woke up after %d ticks", output[0]);
+  for (i = 1; i < test.output_pos - output; i++) 
+    msg ("iteration %d, thread %d: woke up %d ticks later",
+         i / thread_cnt, i % thread_cnt, output[i] - output[i - 1]);
+  
+  free (output);
+}
+
+/* Sleeper thread. */
+static void
+sleeper (void *test_) 
+{
+  struct sleep_test *test = test_;
+  int i;
+
+  /* Make sure we're at the beginning of a timer tick. */
+  timer_sleep (1);
+
+  for (i = 1; i <= test->iterations; i++) 
+    {
+      int64_t sleep_until = test->start + i * 10;
+      timer_sleep (sleep_until - timer_ticks ());
+      *test->output_pos++ = timer_ticks () - test->start;
+      thread_yield ();
+    }
+}
diff --git a/src/tests/threads/alarm-simultaneous.ck b/src/tests/threads/alarm-simultaneous.ck
new file mode 100644 (file)
index 0000000..510c07d
--- /dev/null
@@ -0,0 +1,36 @@
+# -*- perl -*-
+use strict;
+use warnings;
+use tests::tests;
+check_expected ([<<'EOF']);
+(alarm-simultaneous) begin
+(alarm-simultaneous) Creating 5 threads to sleep 5 times each.
+(alarm-simultaneous) Each thread sleeps 10 ticks each time.
+(alarm-simultaneous) Within an iteration, all threads should wake up on the same tick.
+(alarm-simultaneous) iteration 0, thread 0: woke up after 10 ticks
+(alarm-simultaneous) iteration 0, thread 1: woke up 0 ticks later
+(alarm-simultaneous) iteration 0, thread 2: woke up 0 ticks later
+(alarm-simultaneous) iteration 0, thread 3: woke up 0 ticks later
+(alarm-simultaneous) iteration 0, thread 4: woke up 0 ticks later
+(alarm-simultaneous) iteration 1, thread 0: woke up 10 ticks later
+(alarm-simultaneous) iteration 1, thread 1: woke up 0 ticks later
+(alarm-simultaneous) iteration 1, thread 2: woke up 0 ticks later
+(alarm-simultaneous) iteration 1, thread 3: woke up 0 ticks later
+(alarm-simultaneous) iteration 1, thread 4: woke up 0 ticks later
+(alarm-simultaneous) iteration 2, thread 0: woke up 10 ticks later
+(alarm-simultaneous) iteration 2, thread 1: woke up 0 ticks later
+(alarm-simultaneous) iteration 2, thread 2: woke up 0 ticks later
+(alarm-simultaneous) iteration 2, thread 3: woke up 0 ticks later
+(alarm-simultaneous) iteration 2, thread 4: woke up 0 ticks later
+(alarm-simultaneous) iteration 3, thread 0: woke up 10 ticks later
+(alarm-simultaneous) iteration 3, thread 1: woke up 0 ticks later
+(alarm-simultaneous) iteration 3, thread 2: woke up 0 ticks later
+(alarm-simultaneous) iteration 3, thread 3: woke up 0 ticks later
+(alarm-simultaneous) iteration 3, thread 4: woke up 0 ticks later
+(alarm-simultaneous) iteration 4, thread 0: woke up 10 ticks later
+(alarm-simultaneous) iteration 4, thread 1: woke up 0 ticks later
+(alarm-simultaneous) iteration 4, thread 2: woke up 0 ticks later
+(alarm-simultaneous) iteration 4, thread 3: woke up 0 ticks later
+(alarm-simultaneous) iteration 4, thread 4: woke up 0 ticks later
+(alarm-simultaneous) end
+EOF
index 8030d20155d416e2cbb06bb95c8427e56a69cc43..458e987affa670f1088451dc4b0386f5145671f9 100644 (file)
@@ -145,7 +145,6 @@ sleeper (void *t_)
     {
       int64_t sleep_until = test->start + i * t->duration;
       timer_sleep (sleep_until - timer_ticks ());
-
       lock_acquire (&test->output_lock);
       *test->output_pos++ = t->id;
       lock_release (&test->output_lock);
index b4b12cdc0ac311e5ee9e70e74dd85c4486b1c025..ca99eb6624bde55cf2da798890ceaac9aaf71814 100644 (file)
@@ -13,6 +13,7 @@ static const struct test tests[] =
   {
     {"alarm-single", test_alarm_single},
     {"alarm-multiple", test_alarm_multiple},
+    {"alarm-simultaneous", test_alarm_simultaneous},
     {"alarm-priority", test_alarm_priority},
     {"alarm-zero", test_alarm_zero},
     {"alarm-negative", test_alarm_negative},
index 46d99f8828d27f92f622816c70819844d528f54c..ed9fbac28b0d69826ac9d2cdf4162eb5bb6ffd16 100644 (file)
@@ -7,6 +7,7 @@ typedef void test_func (void);
 
 extern test_func test_alarm_single;
 extern test_func test_alarm_multiple;
+extern test_func test_alarm_simultaneous;
 extern test_func test_alarm_priority;
 extern test_func test_alarm_zero;
 extern test_func test_alarm_negative;