1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <stdio.h>
18#include <unistd.h>
19#include <sys/syscall.h>
20#include <sys/time.h>
21#include <sys/types.h>
22#include <sys/wait.h>
23#include <stdlib.h>
24#include <pthread.h>
25
26#include "utils.h"
27
28
29#define PREEMPT_TIME 20
30
31
32
33
34#define THREAD_FACTOR 8
35
36__thread vector int varray[] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10,11,12},
37 {13,14,15,16},{17,18,19,20},{21,22,23,24},
38 {25,26,27,28},{29,30,31,32},{33,34,35,36},
39 {37,38,39,40},{41,42,43,44},{45,46,47,48}};
40
41int threads_starting;
42int running;
43
44extern void preempt_vmx(vector int *varray, int *threads_starting, int *running);
45
46void *preempt_vmx_c(void *p)
47{
48 int i, j;
49 srand(pthread_self());
50 for (i = 0; i < 12; i++)
51 for (j = 0; j < 4; j++)
52 varray[i][j] = rand();
53
54
55 preempt_vmx(varray, &threads_starting, &running);
56 return p;
57}
58
59int test_preempt_vmx(void)
60{
61 int i, rc, threads;
62 pthread_t *tids;
63
64 threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
65 tids = malloc(threads * sizeof(pthread_t));
66 FAIL_IF(!tids);
67
68 running = true;
69 threads_starting = threads;
70 for (i = 0; i < threads; i++) {
71 rc = pthread_create(&tids[i], NULL, preempt_vmx_c, NULL);
72 FAIL_IF(rc);
73 }
74
75 setbuf(stdout, NULL);
76
77 printf("\tWaiting for all workers to start...");
78 while(threads_starting)
79 asm volatile("": : :"memory");
80 printf("done\n");
81
82 printf("\tWaiting for %d seconds to let some workers get preempted...", PREEMPT_TIME);
83 sleep(PREEMPT_TIME);
84 printf("done\n");
85
86 printf("\tStopping workers...");
87
88
89
90
91 running = 0;
92 for (i = 0; i < threads; i++) {
93 void *rc_p;
94 pthread_join(tids[i], &rc_p);
95
96
97
98
99
100 if ((long) rc_p)
101 printf("oops\n");
102 FAIL_IF((long) rc_p);
103 }
104 printf("done\n");
105
106 return 0;
107}
108
109int main(int argc, char *argv[])
110{
111 return test_harness(test_preempt_vmx, "vmx_preempt");
112}
113