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);
22 /* Easy test: 5 threads sleep once each. */
25 /* Somewhat harder test: 5 threads sleep 7 times each. */
29 /* Information about the test. */
32 int64_t start; /* Current time at start of test. */
33 int iterations; /* Number of iterations per thread. */
36 struct lock output_lock; /* Lock protecting output buffer. */
37 int *output_pos; /* Current position in output buffer. */
40 /* Information about an individual thread in the test. */
43 struct sleep_test *test; /* Info shared between all threads. */
44 int id; /* Sleeper ID. */
45 int duration; /* Number of ticks to sleep. */
46 int iterations; /* Iterations counted so far. */
49 static void sleeper (void *);
51 /* Runs THREAD_CNT threads thread sleep ITERATIONS times each. */
53 test_sleep (int thread_cnt, int iterations)
55 struct sleep_test test;
56 struct sleep_thread *threads;
62 "Creating %d threads to sleep %d times each.\n"
63 "Thread 0 sleeps 10 ticks each time,\n"
64 "thread 1 sleeps 20 ticks each time, and so on.\n"
65 "If successful, product of iteration count and\n"
66 "sleep duration will appear in nondescending order.\n\n"
68 thread_cnt, iterations);
70 /* Allocate memory. */
71 threads = malloc (sizeof *threads * thread_cnt);
72 output = malloc (sizeof *output * iterations * thread_cnt * 2);
73 if (threads == NULL || output == NULL)
74 PANIC ("couldn't allocate memory for test");
76 /* Initialize test. */
77 test.start = timer_ticks () + 100;
78 test.iterations = iterations;
79 lock_init (&test.output_lock, "output");
80 test.output_pos = output;
83 ASSERT (output != NULL);
84 for (i = 0; i < thread_cnt; i++)
86 struct sleep_thread *t = threads + i;
91 t->duration = (i + 1) * 10;
94 snprintf (name, sizeof name, "thread %d", i);
95 thread_create (name, PRI_DEFAULT, sleeper, t);
98 /* Wait long enough for all the threads to finish. */
99 timer_sleep (100 + thread_cnt * iterations * 10 + 100);
102 /* Acquire the output lock in case some rogue thread is still
104 lock_acquire (&test.output_lock);
106 /* Print completion order. */
108 for (op = output; op < test.output_pos; op++)
110 struct sleep_thread *t;
113 ASSERT (*op >= 0 && *op < thread_cnt);
116 new_prod = ++t->iterations * t->duration;
118 printf ("thread %d: duration=%d, iteration=%d, product=%d\n",
119 t->id, t->duration, t->iterations, new_prod);
121 if (new_prod >= product)
124 printf ("FAIL: thread %d woke up out of order (%d > %d)!\n",
125 t->id, product, new_prod);
128 /* Verify that we had the proper number of wakeups. */
129 for (i = 0; i < thread_cnt; i++)
130 if (threads[i].iterations != iterations)
131 printf ("FAIL: thread %d woke up %d times instead of %d\n",
132 i, threads[i].iterations, iterations);
134 printf ("Test complete.\n");
136 lock_release (&test.output_lock);
141 /* Sleeper thread. */
145 struct sleep_thread *t = t_;
146 struct sleep_test *test = t->test;
149 for (i = 1; i <= test->iterations; i++)
151 int64_t sleep_until = test->start + i * t->duration;
152 timer_sleep (sleep_until - timer_ticks ());
154 lock_acquire (&test->output_lock);
155 *test->output_pos++ = t->id;
156 lock_release (&test->output_lock);