parlib: Move alarm/timer functions from benchutil
[akaros.git] / user / utest / pvcalarm.c
1 #include <utest/utest.h>
2 #include <pthread.h>
3 #include <parlib/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                 return 0;
62         }
63         void signal_handler(int signo)
64         {
65                 assert(signo == SIGPROF);
66                 __sync_fetch_and_add(__count, 1);
67         }
68
69         pthread_lib_init();
70         parlib_never_yield = FALSE;
71         parlib_never_vc_request = FALSE;
72
73         sigset_t s;
74         sigemptyset(&s);
75         sigaddset(&s, SIGPROF);
76         pthread_sigmask(SIG_BLOCK, &s, NULL);
77         struct sigaction sigact = {.sa_handler = signal_handler, 0};
78         sigaction(SIGPROF, &sigact, 0);
79         for (int i=0; i<NUM_PTHREADS; i++)
80                 count[i] = 0;
81
82         enable_profalarm(INTERVAL);
83         for (int i=0; i<NUM_PTHREADS; i++)
84                 pthread_create(&threads[i], NULL, thread_handler, &count[i]);
85
86         for (int i=0; i<NUM_PTHREADS; i++)
87                 while(count[i] < ITERATIONS)
88                         cpu_relax();
89
90         disable_pvcalarms();
91         return true;
92 }
93
94 /* <--- End definition of test cases ---> */
95
96 struct utest utests[] = {
97         UTEST_REG(pvcalarms),
98         UTEST_REG(sigperf),
99 };
100 int num_utests = sizeof(utests) / sizeof(struct utest);
101
102 int main(int argc, char *argv[]) {
103         // Run test suite passing it all the args as whitelist of what tests to run.
104         char **whitelist = &argv[1];
105         int whitelist_len = argc - 1;
106         RUN_TEST_SUITE(utests, num_utests, whitelist, whitelist_len);
107 }