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"
15 #error This test not applicable with MLFQS enabled.
18 static void test_sleep (int thread_cnt, int iterations);
23 /* Easy test: 5 threads sleep once each. */
26 /* Somewhat harder test: 5 threads sleep 7 times each. */
30 /* Information about the test. */
33 int64_t start; /* Current time at start of test. */
34 int iterations; /* Number of iterations per thread. */
37 struct lock output_lock; /* Lock protecting output buffer. */
38 int *output_pos; /* Current position in output buffer. */
41 /* Information about an individual thread in the test. */
44 struct sleep_test *test; /* Info shared between all threads. */
45 int id; /* Sleeper ID. */
46 int duration; /* Number of ticks to sleep. */
47 int iterations; /* Iterations counted so far. */
50 static void sleeper (void *);
52 /* Runs THREAD_CNT threads thread sleep ITERATIONS times each. */
54 test_sleep (int thread_cnt, int iterations)
56 struct sleep_test test;
57 struct sleep_thread *threads;
63 "Creating %d threads to sleep %d times each.\n"
64 "Thread 0 sleeps 10 ticks each time,\n"
65 "thread 1 sleeps 20 ticks each time, and so on.\n"
66 "If successful, product of iteration count and\n"
67 "sleep duration will appear in nondescending order.\n\n"
69 thread_cnt, iterations);
71 /* Allocate memory. */
72 threads = malloc (sizeof *threads * thread_cnt);
73 output = malloc (sizeof *output * iterations * thread_cnt * 2);
74 if (threads == NULL || output == NULL)
75 PANIC ("couldn't allocate memory for test");
77 /* Initialize test. */
78 test.start = timer_ticks () + 100;
79 test.iterations = iterations;
80 lock_init (&test.output_lock, "output");
81 test.output_pos = output;
84 ASSERT (output != NULL);
85 for (i = 0; i < thread_cnt; i++)
87 struct sleep_thread *t = threads + i;
92 t->duration = (i + 1) * 10;
95 snprintf (name, sizeof name, "thread %d", i);
96 thread_create (name, PRI_DEFAULT, sleeper, t);
99 /* Wait long enough for all the threads to finish. */
100 timer_sleep (100 + thread_cnt * iterations * 10 + 100);
103 /* Acquire the output lock in case some rogue thread is still
105 lock_acquire (&test.output_lock);
107 /* Print completion order. */
109 for (op = output; op < test.output_pos; op++)
111 struct sleep_thread *t;
114 ASSERT (*op >= 0 && *op < thread_cnt);
117 new_prod = ++t->iterations * t->duration;
119 printf ("thread %d: duration=%d, iteration=%d, product=%d\n",
120 t->id, t->duration, t->iterations, new_prod);
122 if (new_prod >= product)
125 printf ("FAIL: thread %d woke up out of order (%d > %d)!\n",
126 t->id, product, new_prod);
129 /* Verify that we had the proper number of wakeups. */
130 for (i = 0; i < thread_cnt; i++)
131 if (threads[i].iterations != iterations)
132 printf ("FAIL: thread %d woke up %d times instead of %d\n",
133 i, threads[i].iterations, iterations);
135 printf ("Test complete.\n");
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);