]> pd.if.org Git - nbds/blobdiff - test/rcu_test.c
add perf test driver
[nbds] / test / rcu_test.c
index 96d196b37a9c71aba3c3d1b5c6b02095c70b0382..b2dedaae418d974406b0697cbdef7457efb107c5 100644 (file)
@@ -1,9 +1,12 @@
+#define _POSIX_C_SOURCE 1 // for rand_r
 #include <stdio.h>
 #include <errno.h>
 #include <pthread.h>
+#include <sys/time.h>
 #include "common.h"
 #include "runtime.h"
 #include "mem.h"
+#include "rcu.h"
 
 #define NUM_ITERATIONS 10000000
 
@@ -27,18 +30,18 @@ static lifo_t *lifo_alloc (void) {
 static void lifo_aba_push (lifo_t *stk, node_t *x) {
     node_t *head;
     do {
-        head = ((volatile lifo_t *)stk)->head;
-        ((volatile node_t *)x)->next = head;
-    } while (__sync_val_compare_and_swap(&stk->head, head, x) != head);
+        head = VOLATILE_DEREF(stk).head;
+        VOLATILE_DEREF(x).next = head;
+    } while (SYNC_CAS(&stk->head, head, x) != head);
 }
 
 node_t *lifo_aba_pop (lifo_t *stk) {
     node_t *head;
     do {
-        head = ((volatile lifo_t *)stk)->head;
+        head = VOLATILE_DEREF(stk).head;
         if (head == NULL)
             return NULL;
-    } while (__sync_val_compare_and_swap(&stk->head, head, head->next) != head);
+    } while (SYNC_CAS(&stk->head, head, head->next) != head);
     head->next = NULL;
     return head;
 }
@@ -54,7 +57,7 @@ void *worker (void *arg) {
     unsigned int rand_seed = (unsigned int)id + 1;
 
     // Wait for all the worker threads to be ready.
-    __sync_fetch_and_add(&wait_, -1);
+    (void)__sync_fetch_and_add(&wait_, -1);
     do {} while (wait_); 
 
     int i;
@@ -65,7 +68,7 @@ void *worker (void *arg) {
         } else {
             node_t *x = lifo_aba_pop(stk_);
             if (x) {
-                nbd_defer_free(x);
+                rcu_defer_free(x);
             }
         }
         rcu_update();
@@ -75,10 +78,9 @@ void *worker (void *arg) {
 }
 
 int main (int argc, char **argv) {
-    nbd_init();
-    //lwt_set_trace_level("m0r0");
+    lwt_set_trace_level("m3r3");
 
-    int num_threads = 2;
+    int num_threads = MAX_NUM_THREADS;
     if (argc == 2)
     {
         errno = 0;
@@ -96,6 +98,10 @@ int main (int argc, char **argv) {
     stk_ = lifo_alloc();
     wait_ = num_threads;
 
+    struct timeval tv1, tv2;
+    gettimeofday(&tv1, NULL);
+    wait_ = num_threads;
+
     pthread_t thread[num_threads];
     for (int i = 0; i < num_threads; ++i) {
         int rc = nbd_thread_create(thread + i, i, worker, (void *)(size_t)i);
@@ -105,5 +111,10 @@ int main (int argc, char **argv) {
         pthread_join(thread[i], NULL);
     }
 
+    gettimeofday(&tv2, NULL);
+    int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
+    printf("Th:%d Time:%dms\n\n", num_threads, ms);
+    fflush(stdout);
+
     return 0;
 }