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
8 #include "tests/threads/tests.h"
9 #include "threads/init.h"
10 #include "threads/malloc.h"
11 #include "threads/synch.h"
12 #include "threads/thread.h"
13 #include "devices/timer.h"
15 static void test_sleep (int thread_cnt, int iterations);
18 test_alarm_single (void)
24 test_alarm_multiple (void)
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;
61 /* This test does not work with the MLFQS. */
62 ASSERT (!enable_mlfqs);
64 msg ("Creating %d threads to sleep %d times each.", thread_cnt, iterations);
65 msg ("Thread 0 sleeps 10 ticks each time,");
66 msg ("thread 1 sleeps 20 ticks each time, and so on.");
67 msg ("If successful, product of iteration count and");
68 msg ("sleep duration will appear in nondescending order.");
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);
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);
101 /* Acquire the output lock in case some rogue thread is still
103 lock_acquire (&test.output_lock);
105 /* Print completion order. */
107 for (op = output; op < test.output_pos; op++)
109 struct sleep_thread *t;
112 ASSERT (*op >= 0 && *op < thread_cnt);
115 new_prod = ++t->iterations * t->duration;
117 msg ("thread %d: duration=%d, iteration=%d, product=%d",
118 t->id, t->duration, t->iterations, new_prod);
120 if (new_prod >= product)
123 fail ("thread %d woke up out of order (%d > %d)!",
124 t->id, product, new_prod);
127 /* Verify that we had the proper number of wakeups. */
128 for (i = 0; i < thread_cnt; i++)
129 if (threads[i].iterations != iterations)
130 fail ("thread %d woke up %d times instead of %d",
131 i, threads[i].iterations, iterations);
133 lock_release (&test.output_lock);
138 /* Sleeper thread. */
142 struct sleep_thread *t = t_;
143 struct sleep_test *test = t->test;
146 for (i = 1; i <= test->iterations; i++)
148 int64_t sleep_until = test->start + i * t->duration;
149 timer_sleep (sleep_until - timer_ticks ());
151 lock_acquire (&test->output_lock);
152 *test->output_pos++ = t->id;
153 lock_release (&test->output_lock);