Use runtime options instead of conditional compilation for MLFQS,
[pintos-anon] / src / threads / test.c
1 /* Problem 1-1: Alarm Clock tests.
2
3    Creates N threads, each of which sleeps a different, fixed
4    duration, M times.  Records the wake-up order and verifies
5    that it is valid. */
6
7 #include "threads/test.h"
8 #include <stdio.h>
9 #include "threads/malloc.h"
10 #include "threads/synch.h"
11 #include "threads/thread.h"
12 #include "devices/timer.h"
13
14 static void test_sleep (int thread_cnt, int iterations);
15
16 void
17 test (void) 
18 {
19   /* This test does not work with the MLFQS. */
20   ASSERT (!enable_mlfqs);
21
22   /* Easy test: 5 threads sleep once each. */
23   test_sleep (5, 1);
24
25   /* Somewhat harder test: 5 threads sleep 7 times each. */
26   test_sleep (5, 7);
27 }
28 \f
29 /* Information about the test. */
30 struct sleep_test 
31   {
32     int64_t start;              /* Current time at start of test. */
33     int iterations;             /* Number of iterations per thread. */
34
35     /* Output. */
36     struct lock output_lock;    /* Lock protecting output buffer. */
37     int *output_pos;            /* Current position in output buffer. */
38   };
39
40 /* Information about an individual thread in the test. */
41 struct sleep_thread 
42   {
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. */
47   };
48
49 static void sleeper (void *);
50
51 /* Runs THREAD_CNT threads thread sleep ITERATIONS times each. */
52 static void
53 test_sleep (int thread_cnt, int iterations) 
54 {
55   struct sleep_test test;
56   struct sleep_thread *threads;
57   int *output, *op;
58   int product;
59   int i;
60
61   printf ("\n"
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"
67           "Running test...  ",
68           thread_cnt, iterations);
69
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");
75
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;
81
82   /* Start threads. */
83   ASSERT (output != NULL);
84   for (i = 0; i < thread_cnt; i++)
85     {
86       struct sleep_thread *t = threads + i;
87       char name[16];
88       
89       t->test = &test;
90       t->id = i;
91       t->duration = (i + 1) * 10;
92       t->iterations = 0;
93
94       snprintf (name, sizeof name, "thread %d", i);
95       thread_create (name, PRI_DEFAULT, sleeper, t);
96     }
97   
98   /* Wait long enough for all the threads to finish. */
99   timer_sleep (100 + thread_cnt * iterations * 10 + 100);
100   printf ("done\n\n");
101
102   /* Acquire the output lock in case some rogue thread is still
103      running. */
104   lock_acquire (&test.output_lock);
105
106   /* Print completion order. */
107   product = 0;
108   for (op = output; op < test.output_pos; op++) 
109     {
110       struct sleep_thread *t;
111       int new_prod;
112
113       ASSERT (*op >= 0 && *op < thread_cnt);
114       t = threads + *op;
115
116       new_prod = ++t->iterations * t->duration;
117         
118       printf ("thread %d: duration=%d, iteration=%d, product=%d\n",
119               t->id, t->duration, t->iterations, new_prod);
120       
121       if (new_prod >= product)
122         product = new_prod;
123       else
124         printf ("FAIL: thread %d woke up out of order (%d > %d)!\n",
125                 t->id, product, new_prod);
126     }
127
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);
133   
134   printf ("Test complete.\n");
135
136   lock_release (&test.output_lock);
137   free (output);
138   free (threads);
139 }
140
141 /* Sleeper thread. */
142 static void
143 sleeper (void *t_) 
144 {
145   struct sleep_thread *t = t_;
146   struct sleep_test *test = t->test;
147   int i;
148
149   for (i = 1; i <= test->iterations; i++) 
150     {
151       int64_t sleep_until = test->start + i * t->duration;
152       timer_sleep (sleep_until - timer_ticks ());
153
154       lock_acquire (&test->output_lock);
155       *test->output_pos++ = t->id;
156       lock_release (&test->output_lock);
157     }
158 }