1 /* Problem 1-1: Alarm Clock tests.
3 Creates N threads, each of which sleeps a different, fixed
4 duration, M times. Records the wake-up order and verifies
7 #include "threads/test.h"
9 #include "threads/malloc.h"
10 #include "threads/synch.h"
11 #include "threads/thread.h"
12 #include "devices/timer.h"
14 static void test_sleep (int thread_cnt, int iterations);
19 /* This test does not work with the MLFQS. */
20 ASSERT (!enable_mlfqs);
25 /* Information about the test. */
28 int64_t start; /* Current time at start of test. */
29 int iterations; /* Number of iterations per thread. */
32 struct lock output_lock; /* Lock protecting output buffer. */
33 int *output_pos; /* Current position in output buffer. */
36 /* Information about an individual thread in the test. */
39 struct sleep_test *test; /* Info shared between all threads. */
40 int id; /* Sleeper ID. */
41 int duration; /* Number of ticks to sleep. */
42 int iterations; /* Iterations counted so far. */
45 static void sleeper (void *);
47 /* Runs THREAD_CNT threads thread sleep ITERATIONS times each. */
49 test_sleep (int thread_cnt, int iterations)
51 struct sleep_test test;
52 struct sleep_thread *threads;
58 "Creating %d threads to sleep %d times each.\n"
59 "Thread 0 sleeps 10 ticks each time,\n"
60 "thread 1 sleeps 20 ticks each time, and so on.\n"
61 "If successful, product of iteration count and\n"
62 "sleep duration will appear in nondescending order.\n\n"
64 thread_cnt, iterations);
66 /* Allocate memory. */
67 threads = malloc (sizeof *threads * thread_cnt);
68 output = malloc (sizeof *output * iterations * thread_cnt * 2);
69 if (threads == NULL || output == NULL)
70 PANIC ("couldn't allocate memory for test");
72 /* Initialize test. */
73 test.start = timer_ticks () + 100;
74 test.iterations = iterations;
75 lock_init (&test.output_lock, "output");
76 test.output_pos = output;
79 ASSERT (output != NULL);
80 for (i = 0; i < thread_cnt; i++)
82 struct sleep_thread *t = threads + i;
87 t->duration = (i + 1) * 10;
90 snprintf (name, sizeof name, "thread %d", i);
91 thread_create (name, PRI_DEFAULT, sleeper, t);
94 /* Wait long enough for all the threads to finish. */
95 timer_sleep (100 + thread_cnt * iterations * 10 + 100);
98 /* Acquire the output lock in case some rogue thread is still
100 lock_acquire (&test.output_lock);
102 /* Print completion order. */
104 for (op = output; op < test.output_pos; op++)
106 struct sleep_thread *t;
109 ASSERT (*op >= 0 && *op < thread_cnt);
112 new_prod = ++t->iterations * t->duration;
114 printf ("thread %d: duration=%d, iteration=%d, product=%d\n",
115 t->id, t->duration, t->iterations, new_prod);
117 if (new_prod >= product)
120 printf ("FAIL: thread %d woke up out of order (%d > %d)!\n",
121 t->id, product, new_prod);
124 /* Verify that we had the proper number of wakeups. */
125 for (i = 0; i < thread_cnt; i++)
126 if (threads[i].iterations != iterations)
127 printf ("FAIL: thread %d woke up %d times instead of %d\n",
128 i, threads[i].iterations, iterations);
130 printf ("Test complete.\n");
132 lock_release (&test.output_lock);
137 /* Sleeper thread. */
141 struct sleep_thread *t = t_;
142 struct sleep_test *test = t->test;
145 for (i = 1; i <= test->iterations; i++)
147 int64_t sleep_until = test->start + i * t->duration;
148 timer_sleep (sleep_until - timer_ticks ());
150 lock_acquire (&test->output_lock);
151 *test->output_pos++ = t->id;
152 lock_release (&test->output_lock);