fill-kfs from XCC_TARGET_LIB, not XCC_TARGET_ROOT
[akaros.git] / user / utest / pvcalarm.c
index 2a5ce89..c562de3 100644 (file)
@@ -1,13 +1,14 @@
 #include <utest.h>
 #include <pthread.h>
 #include <pvcalarm.h>
+#include <profalarm.h>
 
 TEST_SUITE("PVCALARMS");
 
 /* <--- Begin definition of test cases ---> */
 bool test_pvcalarms(void) {
        const int INTERVAL = 10000;
-       const int ITERATIONS = 100;
+       const int ITERS = 100;
        int count[max_vcores()];
        void pvcalarm_callback()
        {
@@ -24,13 +25,67 @@ bool test_pvcalarms(void) {
        now = tsc2usec(read_tsc());
        enable_pvcalarms(PVCALARM_PROF, INTERVAL, pvcalarm_callback);
        for (int i=0; i<max_vcores(); i++)
-               while(count[i] < ITERATIONS)
+               while(count[i] < ITERS)
                        cpu_relax();
-       then = tsc2usec(read_tsc());
        disable_pvcalarms();
+       then = tsc2usec(read_tsc());
+
+       UT_ASSERT_M("Alarms finished too soon", then > (now + INTERVAL*ITERS));
+       UT_ASSERT_M("Alarms finished too late", then < (now + 2*INTERVAL*ITERS));
+       return true;
+}
+
+bool test_sigperf(void)
+{
+       const int INTERVAL = 10000;
+       const int ITERATIONS = 100;
+       const int NUM_PTHREADS = 10;
+       int count[NUM_PTHREADS];
+       pthread_t threads[NUM_PTHREADS];
+       static __thread int *__count;
+
+       void *thread_handler(void *arg)
+       {
+               __count = (int*)arg;
+               sigset_t s;
+               sigemptyset(&s);
+               sigaddset(&s, SIGPROF);
+               pthread_sigmask(SIG_UNBLOCK, &s, NULL);
+               int old_count = 0, new_count = 0;
+               while(1) {
+                       while((new_count = atomic_read((atomic_t)__count)) <= old_count);
+                       if (new_count >= ITERATIONS) break;
+                       old_count = new_count;
+                       pthread_yield();
+               }
+       }
+       void signal_handler(int signo)
+       {
+               assert(signo == SIGPROF);
+               __sync_fetch_and_add(__count, 1);
+       }
+
+       pthread_lib_init();
+       pthread_can_vcore_request(TRUE);
+
+       sigset_t s;
+       sigemptyset(&s);
+       sigaddset(&s, SIGPROF);
+       pthread_sigmask(SIG_BLOCK, &s, NULL);
+       struct sigaction sigact = {.sa_handler = signal_handler, 0};
+       sigaction(SIGPROF, &sigact, 0);
+       for (int i=0; i<NUM_PTHREADS; i++)
+               count[i] = 0;
+
+       enable_profalarm(INTERVAL);
+       for (int i=0; i<NUM_PTHREADS; i++)
+               pthread_create(&threads[i], NULL, thread_handler, &count[i]);
+
+       for (int i=0; i<NUM_PTHREADS; i++)
+               while(count[i] < ITERATIONS)
+                       cpu_relax();
 
-       UT_ASSERT_M("Alarms finished too soon", then > (now + INTERVAL*count[0]));
-       UT_ASSERT_M("Alarms finished too late", then < (now + 2*INTERVAL*count[0]));
+       disable_pvcalarms();
        return true;
 }
 
@@ -38,6 +93,7 @@ bool test_pvcalarms(void) {
 
 struct utest utests[] = {
        UTEST_REG(pvcalarms),
+       UTEST_REG(sigperf),
 };
 int num_utests = sizeof(utests) / sizeof(struct utest);