Initialize all vcores in SCP mode
[akaros.git] / user / utest / pvcalarm.c
1 #include <utest/utest.h>
2 #include <pthread.h>
3 #include <benchutil/pvcalarm.h>
4
5 TEST_SUITE("PVCALARMS");
6
7 /* <--- Begin definition of test cases ---> */
8 bool test_pvcalarms(void) {
9         const int INTERVAL = 10000;
10         const int ITERS = 100;
11         int count[max_vcores()];
12         void pvcalarm_callback()
13         {
14                 __sync_fetch_and_add(&count[vcore_id()], 1);
15         }
16
17         parlib_never_yield = TRUE;
18         pthread_mcp_init();
19         vcore_request_total(max_vcores());
20         parlib_never_vc_request = TRUE;
21         for (int i=0; i<max_vcores(); i++)
22                 count[i] = 0;
23         
24         uint64_t now, then;
25         now = tsc2usec(read_tsc());
26         enable_pvcalarms(PVCALARM_PROF, INTERVAL, pvcalarm_callback);
27         for (int i=0; i<max_vcores(); i++)
28                 while(count[i] < ITERS)
29                         cpu_relax();
30         disable_pvcalarms();
31         then = tsc2usec(read_tsc());
32
33         UT_ASSERT_M("Alarms finished too soon", then > (now + INTERVAL*ITERS));
34         UT_ASSERT_M("Alarms finished too late", then < (now + 2*INTERVAL*ITERS));
35         return true;
36 }
37
38 bool test_sigperf(void)
39 {
40         const int INTERVAL = 10000;
41         const int ITERATIONS = 100;
42         const int NUM_PTHREADS = 10;
43         int count[NUM_PTHREADS];
44         pthread_t threads[NUM_PTHREADS];
45         static __thread int *__count;
46
47         void *thread_handler(void *arg)
48         {
49                 __count = (int*)arg;
50                 sigset_t s;
51                 sigemptyset(&s);
52                 sigaddset(&s, SIGPROF);
53                 pthread_sigmask(SIG_UNBLOCK, &s, NULL);
54                 int old_count = 0, new_count = 0;
55                 while(1) {
56                         while((new_count = atomic_read((atomic_t)__count)) <= old_count);
57                         if (new_count >= ITERATIONS) break;
58                         old_count = new_count;
59                         pthread_yield();
60                 }
61         }
62         void signal_handler(int signo)
63         {
64                 assert(signo == SIGPROF);
65                 __sync_fetch_and_add(__count, 1);
66         }
67
68         pthread_lib_init();
69         parlib_never_yield = FALSE;
70         parlib_never_vc_request = FALSE;
71
72         sigset_t s;
73         sigemptyset(&s);
74         sigaddset(&s, SIGPROF);
75         pthread_sigmask(SIG_BLOCK, &s, NULL);
76         struct sigaction sigact = {.sa_handler = signal_handler, 0};
77         sigaction(SIGPROF, &sigact, 0);
78         for (int i=0; i<NUM_PTHREADS; i++)
79                 count[i] = 0;
80
81         enable_profalarm(INTERVAL);
82         for (int i=0; i<NUM_PTHREADS; i++)
83                 pthread_create(&threads[i], NULL, thread_handler, &count[i]);
84
85         for (int i=0; i<NUM_PTHREADS; i++)
86                 while(count[i] < ITERATIONS)
87                         cpu_relax();
88
89         disable_pvcalarms();
90         return true;
91 }
92
93 /* <--- End definition of test cases ---> */
94
95 struct utest utests[] = {
96         UTEST_REG(pvcalarms),
97         UTEST_REG(sigperf),
98 };
99 int num_utests = sizeof(utests) / sizeof(struct utest);
100
101 int main(int argc, char *argv[]) {
102         // Run test suite passing it all the args as whitelist of what tests to run.
103         char **whitelist = &argv[1];
104         int whitelist_len = argc - 1;
105         RUN_TEST_SUITE(utests, num_utests, whitelist, whitelist_len);
106 }