1 /* Creates N threads, each of which sleeps a different, fixed
2 duration, M times. Records the wake-up order and verifies
6 #include "tests/threads/tests.h"
7 #include "threads/init.h"
8 #include "threads/malloc.h"
9 #include "threads/synch.h"
10 #include "threads/thread.h"
11 #include "devices/timer.h"
13 static void test_sleep (int thread_cnt, int iterations);
16 test_alarm_single (void)
22 test_alarm_multiple (void)
27 /* Information about the test. */
30 int64_t start; /* Current time at start of test. */
31 int iterations; /* Number of iterations per thread. */
34 struct lock output_lock; /* Lock protecting output buffer. */
35 int *output_pos; /* Current position in output buffer. */
38 /* Information about an individual thread in the test. */
41 struct sleep_test *test; /* Info shared between all threads. */
42 int id; /* Sleeper ID. */
43 int duration; /* Number of ticks to sleep. */
44 int iterations; /* Iterations counted so far. */
47 static void sleeper (void *);
49 /* Runs THREAD_CNT threads thread sleep ITERATIONS times each. */
51 test_sleep (int thread_cnt, int iterations)
53 struct sleep_test test;
54 struct sleep_thread *threads;
59 /* This test does not work with the MLFQS. */
60 ASSERT (!enable_mlfqs);
62 msg ("Creating %d threads to sleep %d times each.", thread_cnt, iterations);
63 msg ("Thread 0 sleeps 10 ticks each time,");
64 msg ("thread 1 sleeps 20 ticks each time, and so on.");
65 msg ("If successful, product of iteration count and");
66 msg ("sleep duration will appear in nondescending order.");
68 /* Allocate memory. */
69 threads = malloc (sizeof *threads * thread_cnt);
70 output = malloc (sizeof *output * iterations * thread_cnt * 2);
71 if (threads == NULL || output == NULL)
72 PANIC ("couldn't allocate memory for test");
74 /* Initialize test. */
75 test.start = timer_ticks () + 100;
76 test.iterations = iterations;
77 lock_init (&test.output_lock);
78 test.output_pos = output;
81 ASSERT (output != NULL);
82 for (i = 0; i < thread_cnt; i++)
84 struct sleep_thread *t = threads + i;
89 t->duration = (i + 1) * 10;
92 snprintf (name, sizeof name, "thread %d", i);
93 thread_create (name, PRI_DEFAULT, sleeper, t);
96 /* Wait long enough for all the threads to finish. */
97 timer_sleep (100 + thread_cnt * iterations * 10 + 100);
99 /* Acquire the output lock in case some rogue thread is still
101 lock_acquire (&test.output_lock);
103 /* Print completion order. */
105 for (op = output; op < test.output_pos; op++)
107 struct sleep_thread *t;
110 ASSERT (*op >= 0 && *op < thread_cnt);
113 new_prod = ++t->iterations * t->duration;
115 msg ("thread %d: duration=%d, iteration=%d, product=%d",
116 t->id, t->duration, t->iterations, new_prod);
118 if (new_prod >= product)
121 fail ("thread %d woke up out of order (%d > %d)!",
122 t->id, product, new_prod);
125 /* Verify that we had the proper number of wakeups. */
126 for (i = 0; i < thread_cnt; i++)
127 if (threads[i].iterations != iterations)
128 fail ("thread %d woke up %d times instead of %d",
129 i, threads[i].iterations, iterations);
131 lock_release (&test.output_lock);
136 /* Sleeper thread. */
140 struct sleep_thread *t = t_;
141 struct sleep_test *test = t->test;
144 for (i = 1; i <= test->iterations; i++)
146 int64_t sleep_until = test->start + i * t->duration;
147 timer_sleep (sleep_until - timer_ticks ());
149 lock_acquire (&test->output_lock);
150 *test->output_pos++ = t->id;
151 lock_release (&test->output_lock);