CS318 - Pintos
Pintos source browser for JHU CS318 course
alarm-simultaneous.c
Go to the documentation of this file.
1 /** Creates N threads, each of which sleeps a different, fixed
2  duration, M times. Records the wake-up order and verifies
3  that it is valid. */
4 
5 #include <stdio.h>
6 #include "tests/threads/tests.h"
7 #include "threads/init.h"
8 #include "threads/malloc.h"
9 #include "threads/synch.h"
10 #include "threads/thread.h"
11 #include "devices/timer.h"
12 
13 static void test_sleep (int thread_cnt, int iterations);
14 
15 void
17 {
18  test_sleep (3, 5);
19 }
20 
21 /** Information about the test. */
22 struct sleep_test
23  {
24  int64_t start; /**< Current time at start of test. */
25  int iterations; /**< Number of iterations per thread. */
26  int *output_pos; /**< Current position in output buffer. */
27  };
28 
29 static void sleeper (void *);
30 
31 /** Runs THREAD_CNT threads thread sleep ITERATIONS times each. */
32 static void
33 test_sleep (int thread_cnt, int iterations)
34 {
35  struct sleep_test test;
36  int *output;
37  int i;
38 
39  /* This test does not work with the MLFQS. */
41 
42  msg ("Creating %d threads to sleep %d times each.", thread_cnt, iterations);
43  msg ("Each thread sleeps 10 ticks each time.");
44  msg ("Within an iteration, all threads should wake up on the same tick.");
45 
46  /* Allocate memory. */
47  output = malloc (sizeof *output * iterations * thread_cnt * 2);
48  if (output == NULL)
49  PANIC ("couldn't allocate memory for test");
50 
51  /* Initialize test. */
52  test.start = timer_ticks () + 100;
53  test.iterations = iterations;
54  test.output_pos = output;
55 
56  /* Start threads. */
57  ASSERT (output != NULL);
58  for (i = 0; i < thread_cnt; i++)
59  {
60  char name[16];
61  snprintf (name, sizeof name, "thread %d", i);
63  }
64 
65  /* Wait long enough for all the threads to finish. */
66  timer_sleep (100 + iterations * 10 + 100);
67 
68  /* Print completion order. */
69  msg ("iteration 0, thread 0: woke up after %d ticks", output[0]);
70  for (i = 1; i < test.output_pos - output; i++)
71  msg ("iteration %d, thread %d: woke up %d ticks later",
72  i / thread_cnt, i % thread_cnt, output[i] - output[i - 1]);
73 
74  free (output);
75 }
76 
77 /** Sleeper thread. */
78 static void
79 sleeper (void *test_)
80 {
81  struct sleep_test *test = test_;
82  int i;
83 
84  /* Make sure we're at the beginning of a timer tick. */
85  timer_sleep (1);
86 
87  for (i = 1; i <= test->iterations; i++)
88  {
89  int64_t sleep_until = test->start + i * 10;
90  timer_sleep (sleep_until - timer_ticks ());
91  *test->output_pos++ = timer_ticks () - test->start;
92  thread_yield ();
93  }
94 }
name
char * name[]
Definition: insult.c:47
malloc
void * malloc(size_t size)
Obtains and returns a new block of at least SIZE bytes.
Definition: malloc.c:90
snprintf
int snprintf(char *buffer, size_t buf_size, const char *format,...)
Like printf(), except that output is stored into BUFFER, which must have space for BUF_SIZE character...
Definition: stdio.c:62
test_sleep
static void test_sleep(int thread_cnt, int iterations)
Creates N threads, each of which sleeps a different, fixed duration, M times.
Definition: alarm-simultaneous.c:33
NULL
#define NULL
Definition: stddef.h:4
test
Definition: tests.c:6
free
void free(void *p)
Frees block P, which must have been previously allocated with malloc(), calloc(), or realloc().
Definition: malloc.c:219
PANIC
#define PANIC(...)
Halts the OS, printing the source file name, line number, and function name, plus a user-specific mes...
Definition: debug.h:14
int64_t
signed long long int int64_t
Definition: stdint.h:16
init.h
sleep_test::output_pos
int * output_pos
Current position in output buffer.
Definition: alarm-simultaneous.c:26
timer.h
sleeper
static void sleeper(void *)
Sleeper thread.
Definition: alarm-simultaneous.c:79
timer_sleep
void timer_sleep(int64_t ticks)
Sleeps for approximately TICKS timer ticks.
Definition: timer.c:90
malloc.h
timer_ticks
int64_t timer_ticks(void)
Returns the number of timer ticks since the OS booted.
Definition: timer.c:71
ASSERT
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
Definition: debug.h:31
sleep_test::start
int64_t start
Current time at start of test.
Definition: alarm-simultaneous.c:24
sleep_test::iterations
int iterations
Number of iterations per thread.
Definition: alarm-simultaneous.c:25
msg
void msg(const char *format,...)
Definition: lib.c:28
thread_mlfqs
bool thread_mlfqs
If false (default), use round-robin scheduler.
Definition: thread.c:60
thread_create
tid_t thread_create(const char *name, int priority, thread_func *function, void *aux)
Creates a new kernel thread named NAME with the given initial PRIORITY, which executes FUNCTION passi...
Definition: thread.c:166
tests.h
test_alarm_simultaneous
void test_alarm_simultaneous(void)
Definition: alarm-simultaneous.c:16
thread.h
synch.h
thread_yield
void thread_yield(void)
Yields the CPU.
Definition: thread.c:302
PRI_DEFAULT
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24
sleep_test
Information about the test.
Definition: alarm-simultaneous.c:22