aff206467e771242c1becf9ebe67f376946c5faf
[pintos-anon] / grading / threads / alarm-single.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 #ifdef MLFQS
15 #error This test not applicable with MLFQS enabled.
16 #endif
17
18 static void test_sleep (int thread_cnt, int iterations);
19
20 void
21 test (void) 
22 {
23   test_sleep (5, 1);
24 }
25 \f
26 /* Information about the test. */
27 struct sleep_test 
28   {
29     int64_t start;              /* Current time at start of test. */
30     int iterations;             /* Number of iterations per thread. */
31
32     /* Output. */
33     struct lock output_lock;    /* Lock protecting output buffer. */
34     int *output_pos;            /* Current position in output buffer. */
35   };
36
37 /* Information about an individual thread in the test. */
38 struct sleep_thread 
39   {
40     struct sleep_test *test;     /* Info shared between all threads. */
41     int id;                     /* Sleeper ID. */
42     int duration;               /* Number of ticks to sleep. */
43     int iterations;             /* Iterations counted so far. */
44   };
45
46 static void sleeper (void *);
47
48 /* Runs THREAD_CNT threads thread sleep ITERATIONS times each. */
49 static void
50 test_sleep (int thread_cnt, int iterations) 
51 {
52   struct sleep_test test;
53   struct sleep_thread *threads;
54   int *output, *op;
55   int product;
56   int i;
57
58   printf ("\n"
59           "Creating %d threads to sleep %d times each.\n"
60           "Thread 0 sleeps 10 ticks each time,\n"
61           "thread 1 sleeps 20 ticks each time, and so on.\n"
62           "If successful, product of iteration count and\n"
63           "sleep duration will appear in nondescending order.\n\n"
64           "Running test...  ",
65           thread_cnt, iterations);
66
67   /* Allocate memory. */
68   threads = malloc (sizeof *threads * thread_cnt);
69   output = malloc (sizeof *output * iterations * thread_cnt * 2);
70   if (threads == NULL || output == NULL)
71     PANIC ("couldn't allocate memory for test");
72
73   /* Initialize test. */
74   test.start = timer_ticks () + 100;
75   test.iterations = iterations;
76   lock_init (&test.output_lock, "output");
77   test.output_pos = output;
78
79   /* Start threads. */
80   ASSERT (output != NULL);
81   for (i = 0; i < thread_cnt; i++)
82     {
83       struct sleep_thread *t = threads + i;
84       char name[16];
85       
86       t->test = &test;
87       t->id = i;
88       t->duration = (i + 1) * 10;
89       t->iterations = 0;
90
91       snprintf (name, sizeof name, "thread %d", i);
92       thread_create (name, PRI_DEFAULT, sleeper, t);
93     }
94   
95   /* Wait long enough for all the threads to finish. */
96   timer_sleep (100 + thread_cnt * iterations * 10 + 100);
97   printf ("done\n\n");
98
99   /* Acquire the output lock in case some rogue thread is still
100      running. */
101   lock_acquire (&test.output_lock);
102
103   /* Print completion order. */
104   product = 0;
105   for (op = output; op < test.output_pos; op++) 
106     {
107       struct sleep_thread *t;
108       int new_prod;
109
110       ASSERT (*op >= 0 && *op < thread_cnt);
111       t = threads + *op;
112
113       new_prod = ++t->iterations * t->duration;
114         
115       printf ("thread %d: duration=%d, iteration=%d, product=%d\n",
116               t->id, t->duration, t->iterations, new_prod);
117       
118       if (new_prod >= product)
119         product = new_prod;
120       else
121         printf ("FAIL: thread %d woke up out of order (%d > %d)!\n",
122                 t->id, product, new_prod);
123     }
124
125   /* Verify that we had the proper number of wakeups. */
126   for (i = 0; i < thread_cnt; i++)
127     if (threads[i].iterations != iterations)
128       printf ("FAIL: thread %d woke up %d times instead of %d\n",
129               i, threads[i].iterations, iterations);
130   
131   printf ("Test complete.\n");
132
133   lock_release (&test.output_lock);
134   free (output);
135   free (threads);
136 }
137
138 /* Sleeper thread. */
139 static void
140 sleeper (void *t_) 
141 {
142   struct sleep_thread *t = t_;
143   struct sleep_test *test = t->test;
144   int i;
145
146   for (i = 1; i <= test->iterations; i++) 
147     {
148       int64_t sleep_until = test->start + i * t->duration;
149       timer_sleep (sleep_until - timer_ticks ());
150
151       lock_acquire (&test->output_lock);
152       *test->output_pos++ = t->id;
153       lock_release (&test->output_lock);
154     }
155 }