You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by so...@apache.org on 2014/08/01 22:44:16 UTC
[03/20] TS-2950: Initial commit of libck.
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/ck_fas.h
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/ck_fas.h b/lib/ck/regressions/ck_spinlock/ck_fas.h
new file mode 100644
index 0000000..e244746
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/ck_fas.h
@@ -0,0 +1,6 @@
+#define LOCK_NAME "ck_fas"
+#define LOCK_DEFINE static ck_spinlock_fas_t CK_CC_CACHELINE lock = CK_SPINLOCK_FAS_INITIALIZER
+#define LOCK ck_spinlock_fas_lock_eb(&lock)
+#define UNLOCK ck_spinlock_fas_unlock(&lock)
+#define LOCKED ck_spinlock_fas_locked(&lock)
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/ck_hclh.h
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/ck_hclh.h b/lib/ck/regressions/ck_spinlock/ck_hclh.h
new file mode 100644
index 0000000..eb2e6eb
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/ck_hclh.h
@@ -0,0 +1,16 @@
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#define LOCK_NAME "ck_clh"
+#define LOCK_DEFINE static ck_spinlock_hclh_t CK_CC_CACHELINE *glob_lock; \
+ static ck_spinlock_hclh_t CK_CC_CACHELINE *local_lock[CORES / 2]
+#define LOCK_STATE ck_spinlock_hclh_t *na = malloc(MAX(sizeof(ck_spinlock_hclh_t), 64))
+#define LOCK ck_spinlock_hclh_lock(&glob_lock, &local_lock[(core % CORES) / 2], na)
+#define UNLOCK ck_spinlock_hclh_unlock(&na)
+#define LOCK_INIT do { \
+ int _i; \
+ ck_spinlock_hclh_init(&glob_lock, malloc(MAX(sizeof(ck_spinlock_hclh_t), 64)), -1); \
+ for (_i = 0; _i < CORES / 2; _i++) { \
+ ck_spinlock_hclh_init(&local_lock[_i], malloc(MAX(sizeof(ck_spinlock_hclh_t), 64)), _i); } \
+} while (0)
+
+#define LOCKED ck_spinlock_hclh_locked(&glob_lock)
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/ck_mcs.h
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/ck_mcs.h b/lib/ck/regressions/ck_spinlock/ck_mcs.h
new file mode 100644
index 0000000..dd127df
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/ck_mcs.h
@@ -0,0 +1,7 @@
+#define LOCK_NAME "ck_mcs"
+#define LOCK_DEFINE static ck_spinlock_mcs_t CK_CC_CACHELINE lock = NULL
+#define LOCK_STATE ck_spinlock_mcs_context_t node CK_CC_CACHELINE;
+#define LOCK ck_spinlock_mcs_lock(&lock, &node)
+#define UNLOCK ck_spinlock_mcs_unlock(&lock, &node)
+#define LOCKED ck_spinlock_mcs_locked(&lock)
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/ck_spinlock.h
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/ck_spinlock.h b/lib/ck/regressions/ck_spinlock/ck_spinlock.h
new file mode 100644
index 0000000..938e1ce
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/ck_spinlock.h
@@ -0,0 +1,6 @@
+#define LOCK_NAME "ck_spinlock"
+#define LOCK_DEFINE static ck_spinlock_t CK_CC_CACHELINE lock = CK_SPINLOCK_INITIALIZER
+#define LOCK ck_spinlock_lock_eb(&lock)
+#define UNLOCK ck_spinlock_unlock(&lock)
+#define LOCKED ck_spinlock_locked(&lock)
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/ck_ticket.h
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/ck_ticket.h b/lib/ck/regressions/ck_spinlock/ck_ticket.h
new file mode 100644
index 0000000..39054a6
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/ck_ticket.h
@@ -0,0 +1,11 @@
+#include <ck_spinlock.h>
+
+#define LOCK_NAME "ck_ticket"
+#define LOCK_DEFINE static ck_spinlock_ticket_t CK_CC_CACHELINE lock = CK_SPINLOCK_TICKET_INITIALIZER
+#define LOCK ck_spinlock_ticket_lock(&lock)
+#define UNLOCK ck_spinlock_ticket_unlock(&lock)
+#ifdef CK_F_SPINLOCK_TICKET_TRYLOCK
+#define TRYLOCK ck_spinlock_ticket_trylock(&lock)
+#endif
+#define LOCKED ck_spinlock_ticket_locked(&lock)
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/ck_ticket_pb.h
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/ck_ticket_pb.h b/lib/ck/regressions/ck_spinlock/ck_ticket_pb.h
new file mode 100644
index 0000000..b8a7a84
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/ck_ticket_pb.h
@@ -0,0 +1,6 @@
+#define LOCK_NAME "ck_ticket_pb"
+#define LOCK_DEFINE static ck_spinlock_ticket_t CK_CC_CACHELINE lock = CK_SPINLOCK_TICKET_INITIALIZER
+#define LOCK ck_spinlock_ticket_lock_pb(&lock, 0)
+#define UNLOCK ck_spinlock_ticket_unlock(&lock)
+#define LOCKED ck_spinlock_ticket_locked(&lock)
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/linux_spinlock.h
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/linux_spinlock.h b/lib/ck/regressions/ck_spinlock/linux_spinlock.h
new file mode 100644
index 0000000..5fe1f3e
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/linux_spinlock.h
@@ -0,0 +1,39 @@
+#include <ck_cc.h>
+
+CK_CC_INLINE static void
+spin_lock(volatile unsigned int *lock)
+{
+#ifdef __x86_64__
+ __asm__ __volatile__(
+ "\n1:\t"
+ "lock ; decl %0\n\t"
+ "jns 2f\n"
+ "3:\n"
+ "rep;nop\n\t"
+ "cmpl $0,%0\n\t"
+ "jle 3b\n\t"
+ "jmp 1b\n"
+ "2:\t" : "=m" (*lock) : : "memory");
+#else
+ *lock = 1;
+#endif
+
+ return;
+}
+
+CK_CC_INLINE static void
+spin_unlock(volatile unsigned int *lock)
+{
+#ifdef __x86_64__
+ __asm__ __volatile__("movl $1,%0" :"=m" (*lock) :: "memory");
+#else
+ *lock = 0;
+ return;
+#endif
+}
+
+#define LOCK_NAME "linux_spinlock"
+#define LOCK_DEFINE volatile unsigned int lock = 1
+#define LOCK spin_lock(&lock)
+#define UNLOCK spin_unlock(&lock)
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/Makefile
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/Makefile b/lib/ck/regressions/ck_spinlock/validate/Makefile
new file mode 100644
index 0000000..731b68b
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/Makefile
@@ -0,0 +1,56 @@
+.PHONY: check clean
+
+all: ck_ticket ck_mcs ck_dec ck_cas ck_fas ck_clh linux_spinlock \
+ ck_ticket_pb ck_anderson ck_spinlock ck_hclh
+
+check: all
+ ./ck_ticket $(CORES) 1
+ ./ck_mcs $(CORES) 1
+ ./ck_dec $(CORES) 1
+ ./ck_cas $(CORES) 1
+ ./ck_fas $(CORES) 1
+ ./ck_clh $(CORES) 1
+ ./linux_spinlock $(CORES) 1
+ ./ck_ticket_pb $(CORES) 1
+ ./ck_anderson $(CORES) 1
+ ./ck_spinlock $(CORES) 1
+
+linux_spinlock: linux_spinlock.c
+ $(CC) $(CFLAGS) -o linux_spinlock linux_spinlock.c
+
+ck_spinlock: ck_spinlock.c
+ $(CC) $(CFLAGS) -o ck_spinlock ck_spinlock.c
+
+ck_ticket_pb: ck_ticket_pb.c
+ $(CC) $(CFLAGS) -o ck_ticket_pb ck_ticket_pb.c
+
+ck_clh: ck_clh.c
+ $(CC) $(CFLAGS) -o ck_clh ck_clh.c
+
+ck_hclh: ck_hclh.c
+ $(CC) $(CFLAGS) -o ck_hclh ck_hclh.c
+
+ck_anderson: ck_anderson.c
+ $(CC) $(CFLAGS) -o ck_anderson ck_anderson.c
+
+ck_fas: ck_fas.c
+ $(CC) $(CFLAGS) -o ck_fas ck_fas.c
+
+ck_ticket: ck_ticket.c
+ $(CC) $(CFLAGS) -o ck_ticket ck_ticket.c
+
+ck_cas: ck_cas.c
+ $(CC) $(CFLAGS) -o ck_cas ck_cas.c
+
+ck_mcs: ck_mcs.c
+ $(CC) $(CFLAGS) -o ck_mcs ck_mcs.c
+
+ck_dec: ck_dec.c
+ $(CC) $(CFLAGS) -o ck_dec ck_dec.c
+
+clean:
+ rm -rf ck_ticket ck_mcs ck_dec ck_cas ck_fas ck_clh linux_spinlock ck_ticket_pb \
+ ck_anderson ck_spinlock *.dSYM *.exe
+
+include ../../../build/regressions.build
+CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE -lm
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/ck_anderson.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/ck_anderson.c b/lib/ck/regressions/ck_spinlock/validate/ck_anderson.c
new file mode 100644
index 0000000..b10900c
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/ck_anderson.c
@@ -0,0 +1,2 @@
+#include "../ck_anderson.h"
+#include "validate.h"
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/ck_cas.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/ck_cas.c b/lib/ck/regressions/ck_spinlock/validate/ck_cas.c
new file mode 100644
index 0000000..162490a
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/ck_cas.c
@@ -0,0 +1,2 @@
+#include "../ck_cas.h"
+#include "validate.h"
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/ck_clh.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/ck_clh.c b/lib/ck/regressions/ck_spinlock/validate/ck_clh.c
new file mode 100644
index 0000000..19cb512
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/ck_clh.c
@@ -0,0 +1,2 @@
+#include "../ck_clh.h"
+#include "validate.h"
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/ck_dec.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/ck_dec.c b/lib/ck/regressions/ck_spinlock/validate/ck_dec.c
new file mode 100644
index 0000000..fd351de
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/ck_dec.c
@@ -0,0 +1,2 @@
+#include "../ck_dec.h"
+#include "validate.h"
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/ck_fas.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/ck_fas.c b/lib/ck/regressions/ck_spinlock/validate/ck_fas.c
new file mode 100644
index 0000000..5cf4071
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/ck_fas.c
@@ -0,0 +1,2 @@
+#include "../ck_fas.h"
+#include "validate.h"
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/ck_hclh.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/ck_hclh.c b/lib/ck/regressions/ck_spinlock/validate/ck_hclh.c
new file mode 100644
index 0000000..001f57b
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/ck_hclh.c
@@ -0,0 +1,2 @@
+#include "../ck_hclh.h"
+#include "validate.h"
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/ck_mcs.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/ck_mcs.c b/lib/ck/regressions/ck_spinlock/validate/ck_mcs.c
new file mode 100644
index 0000000..7adad43
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/ck_mcs.c
@@ -0,0 +1,2 @@
+#include "../ck_mcs.h"
+#include "validate.h"
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/ck_spinlock.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/ck_spinlock.c b/lib/ck/regressions/ck_spinlock/validate/ck_spinlock.c
new file mode 100644
index 0000000..e682905
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/ck_spinlock.c
@@ -0,0 +1,2 @@
+#include "../ck_spinlock.h"
+#include "validate.h"
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/ck_ticket.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/ck_ticket.c b/lib/ck/regressions/ck_spinlock/validate/ck_ticket.c
new file mode 100644
index 0000000..be67254
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/ck_ticket.c
@@ -0,0 +1,2 @@
+#include "../ck_ticket.h"
+#include "validate.h"
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/ck_ticket_pb.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/ck_ticket_pb.c b/lib/ck/regressions/ck_spinlock/validate/ck_ticket_pb.c
new file mode 100644
index 0000000..e62ee0e
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/ck_ticket_pb.c
@@ -0,0 +1,2 @@
+#include "../ck_ticket_pb.h"
+#include "validate.h"
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/linux_spinlock.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/linux_spinlock.c b/lib/ck/regressions/ck_spinlock/validate/linux_spinlock.c
new file mode 100644
index 0000000..781e419
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/linux_spinlock.c
@@ -0,0 +1,14 @@
+#ifdef __x86_64__
+#include "../linux_spinlock.h"
+#include "validate.h"
+#else
+#include <stdio.h>
+
+int
+main(void)
+{
+
+ fprintf(stderr, "Unsupported.\n");
+ return 0;
+}
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_spinlock/validate/validate.h
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_spinlock/validate/validate.h b/lib/ck/regressions/ck_spinlock/validate/validate.h
new file mode 100644
index 0000000..a0c9612
--- /dev/null
+++ b/lib/ck/regressions/ck_spinlock/validate/validate.h
@@ -0,0 +1,181 @@
+/*
+ * Copyright 2011-2014 Samy Al Bahra.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <errno.h>
+#include <inttypes.h>
+#include <pthread.h>
+#include <math.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <unistd.h>
+#include <sys/time.h>
+
+#include <ck_cc.h>
+#include <ck_pr.h>
+#include <ck_spinlock.h>
+
+#include "../../common.h"
+
+#ifndef ITERATE
+#define ITERATE 1000000
+#endif
+
+struct block {
+ unsigned int tid;
+};
+
+static struct affinity a;
+static unsigned int locked = 0;
+static uint64_t nthr;
+
+LOCK_DEFINE;
+
+static void *
+thread(void *null CK_CC_UNUSED)
+{
+#ifdef LOCK_STATE
+ LOCK_STATE;
+#endif
+ unsigned int i = ITERATE;
+ unsigned int j;
+ unsigned int core;
+
+ if (aff_iterate_core(&a, &core)) {
+ perror("ERROR: Could not affine thread");
+ exit(EXIT_FAILURE);
+ }
+
+ while (i--) {
+#ifdef TRYLOCK
+ if (i & 1) {
+ LOCK;
+ } else {
+ while (TRYLOCK == false)
+ ck_pr_stall();
+ }
+#else
+ LOCK;
+#endif
+
+#ifdef LOCKED
+ if (LOCKED == false)
+ ck_error("is_locked operation failed.");
+#endif
+
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+
+ j = ck_pr_load_uint(&locked);
+
+ if (j != 10) {
+ ck_error("ERROR (WR): Race condition (%u)\n", j);
+ exit(EXIT_FAILURE);
+ }
+
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+
+ UNLOCK;
+
+ LOCK;
+
+ j = ck_pr_load_uint(&locked);
+ if (j != 0) {
+ ck_error("ERROR (RD): Race condition (%u)\n", j);
+ exit(EXIT_FAILURE);
+ }
+
+ UNLOCK;
+ }
+
+ return (NULL);
+}
+
+int
+main(int argc, char *argv[])
+{
+ uint64_t i;
+ pthread_t *threads;
+
+ if (argc != 3) {
+ ck_error("Usage: " LOCK_NAME " <number of threads> <affinity delta>\n");
+ exit(EXIT_FAILURE);
+ }
+
+ nthr = atoi(argv[1]);
+ if (nthr <= 0) {
+ ck_error("ERROR: Number of threads must be greater than 0\n");
+ exit(EXIT_FAILURE);
+ }
+
+#ifdef LOCK_INIT
+ LOCK_INIT;
+#endif
+
+ threads = malloc(sizeof(pthread_t) * nthr);
+ if (threads == NULL) {
+ ck_error("ERROR: Could not allocate thread structures\n");
+ exit(EXIT_FAILURE);
+ }
+
+ a.delta = atoi(argv[2]);
+ a.request = 0;
+
+ fprintf(stderr, "Creating threads (mutual exclusion)...");
+ for (i = 0; i < nthr; i++) {
+ if (pthread_create(&threads[i], NULL, thread, NULL)) {
+ ck_error("ERROR: Could not create thread %" PRIu64 "\n", i);
+ exit(EXIT_FAILURE);
+ }
+ }
+ fprintf(stderr, "done\n");
+
+ fprintf(stderr, "Waiting for threads to finish correctness regression...");
+ for (i = 0; i < nthr; i++)
+ pthread_join(threads[i], NULL);
+ fprintf(stderr, "done (passed)\n");
+
+ return (0);
+}
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_stack/benchmark/Makefile
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_stack/benchmark/Makefile b/lib/ck/regressions/ck_stack/benchmark/Makefile
new file mode 100644
index 0000000..6e2df2a
--- /dev/null
+++ b/lib/ck/regressions/ck_stack/benchmark/Makefile
@@ -0,0 +1,14 @@
+.PHONY: clean distribution
+
+OBJECTS=latency
+
+all: $(OBJECTS)
+
+latency: latency.c
+ $(CC) $(CFLAGS) -o latency latency.c
+
+clean:
+ rm -rf *~ *.o *.dSYM *.exe $(OBJECTS)
+
+include ../../../build/regressions.build
+CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_stack/benchmark/latency.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_stack/benchmark/latency.c b/lib/ck/regressions/ck_stack/benchmark/latency.c
new file mode 100644
index 0000000..1bca94c
--- /dev/null
+++ b/lib/ck/regressions/ck_stack/benchmark/latency.c
@@ -0,0 +1,176 @@
+/*
+ * Copyright 2011-2014 Samy Al Bahra.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <ck_stack.h>
+#include <ck_spinlock.h>
+#include <inttypes.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#include "../../common.h"
+
+#ifndef ENTRIES
+#define ENTRIES 4096
+#endif
+
+#ifndef STEPS
+#define STEPS 40000
+#endif
+
+/*
+ * Note the redundant post-increment of r. This is to silence
+ * some irrelevant GCC warnings.
+ */
+
+static ck_stack_t stack CK_CC_CACHELINE;
+
+int
+main(void)
+{
+ ck_stack_entry_t entry[ENTRIES];
+ ck_spinlock_fas_t mutex = CK_SPINLOCK_FAS_INITIALIZER;
+ volatile ck_stack_entry_t * volatile r;
+ uint64_t s, e, a;
+ unsigned int i;
+ unsigned int j;
+
+ a = 0;
+ for (i = 0; i < STEPS; i++) {
+ ck_stack_init(&stack);
+
+ s = rdtsc();
+ for (j = 0; j < ENTRIES; j++) {
+ ck_spinlock_fas_lock(&mutex);
+ ck_stack_push_spnc(&stack, entry + j);
+ ck_spinlock_fas_unlock(&mutex);
+ }
+ e = rdtsc();
+
+ a += e - s;
+ }
+ printf(" spinlock_push: %16" PRIu64 "\n", a / STEPS / ENTRIES);
+
+ a = 0;
+ for (i = 0; i < STEPS; i++) {
+ ck_stack_init(&stack);
+
+ for (j = 0; j < ENTRIES; j++)
+ ck_stack_push_spnc(&stack, entry + j);
+
+ s = rdtsc();
+ for (j = 0; j < ENTRIES; j++) {
+ ck_spinlock_fas_lock(&mutex);
+ r = ck_stack_pop_npsc(&stack);
+ ck_spinlock_fas_unlock(&mutex);
+ }
+ e = rdtsc();
+ a += e - s;
+ }
+ printf(" spinlock_pop: %16" PRIu64 "\n", a / STEPS / ENTRIES);
+ r++;
+
+#ifdef CK_F_STACK_PUSH_UPMC
+ a = 0;
+ for (i = 0; i < STEPS; i++) {
+ ck_stack_init(&stack);
+
+ s = rdtsc();
+ for (j = 0; j < ENTRIES; j++)
+ ck_stack_push_upmc(&stack, entry + j);
+ e = rdtsc();
+
+ a += e - s;
+ }
+ printf("ck_stack_push_upmc: %16" PRIu64 "\n", a / STEPS / ENTRIES);
+#endif /* CK_F_STACK_PUSH_UPMC */
+
+#ifdef CK_F_STACK_PUSH_MPMC
+ a = 0;
+ for (i = 0; i < STEPS; i++) {
+ ck_stack_init(&stack);
+
+ s = rdtsc();
+ for (j = 0; j < ENTRIES; j++)
+ ck_stack_push_mpmc(&stack, entry + j);
+ e = rdtsc();
+
+ a += e - s;
+ }
+ printf("ck_stack_push_mpmc: %16" PRIu64 "\n", a / STEPS / ENTRIES);
+#endif /* CK_F_STACK_PUSH_MPMC */
+
+#ifdef CK_F_STACK_PUSH_MPNC
+ a = 0;
+ for (i = 0; i < STEPS; i++) {
+ ck_stack_init(&stack);
+
+ s = rdtsc();
+ for (j = 0; j < ENTRIES; j++)
+ ck_stack_push_mpnc(&stack, entry + j);
+ e = rdtsc();
+
+ a += e - s;
+ }
+ printf("ck_stack_push_mpnc: %16" PRIu64 "\n", a / STEPS / ENTRIES);
+#endif /* CK_F_STACK_PUSH_MPNC */
+
+#if defined(CK_F_STACK_PUSH_UPMC) && defined(CK_F_STACK_POP_UPMC)
+ a = 0;
+ for (i = 0; i < STEPS; i++) {
+ ck_stack_init(&stack);
+
+ for (j = 0; j < ENTRIES; j++)
+ ck_stack_push_upmc(&stack, entry + j);
+
+ s = rdtsc();
+ for (j = 0; j < ENTRIES; j++)
+ r = ck_stack_pop_upmc(&stack);
+ e = rdtsc();
+ a += e - s;
+ }
+ printf(" ck_stack_pop_upmc: %16" PRIu64 "\n", a / STEPS / (sizeof(entry) / sizeof(*entry)));
+#endif /* CK_F_STACK_PUSH_UPMC && CK_F_STACK_POP_UPMC */
+
+#if defined(CK_F_STACK_POP_MPMC) && defined(CK_F_STACK_PUSH_MPMC)
+ a = 0;
+ for (i = 0; i < STEPS; i++) {
+ ck_stack_init(&stack);
+
+ for (j = 0; j < ENTRIES; j++)
+ ck_stack_push_mpmc(&stack, entry + j);
+
+ s = rdtsc();
+ for (j = 0; j < ENTRIES; j++)
+ r = ck_stack_pop_mpmc(&stack);
+ e = rdtsc();
+ a += e - s;
+ }
+ printf(" ck_stack_pop_mpmc: %16" PRIu64 "\n", a / STEPS / (sizeof(entry) / sizeof(*entry)));
+ r++;
+#endif
+
+ return 0;
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_stack/validate/Makefile
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_stack/validate/Makefile b/lib/ck/regressions/ck_stack/validate/Makefile
new file mode 100644
index 0000000..519dca1
--- /dev/null
+++ b/lib/ck/regressions/ck_stack/validate/Makefile
@@ -0,0 +1,56 @@
+.PHONY: check clean distribution
+
+OBJECTS=serial mpnc_push mpmc_push upmc_push spinlock_push spinlock_eb_push \
+ mpmc_pop upmc_pop spinlock_pop spinlock_eb_pop \
+ upmc_trypop mpmc_trypop mpmc_trypair \
+ mpmc_pair spinlock_pair spinlock_eb_pair pthreads_pair \
+ mpmc_trypush upmc_trypush
+
+all: $(OBJECTS)
+
+check: all
+ ./serial
+ ./mpmc_pair $(CORES) 1 0
+ ./upmc_trypop $(CORES) 1 0
+ ./mpmc_trypop $(CORES) 1 0
+ ./mpmc_trypair $(CORES) 1 0
+ ./mpmc_pop $(CORES) 1 0
+ ./upmc_pop $(CORES) 1 0
+ ./mpnc_push $(CORES) 1 0
+ ./mpmc_push $(CORES) 1 0
+ ./upmc_push $(CORES) 1 0
+ ./mpmc_trypush $(CORES) 1 0
+ ./upmc_trypush $(CORES) 1 0
+
+serial: serial.c
+ $(CC) $(CFLAGS) -o serial serial.c
+
+mpmc_trypush upmc_trypush mpnc_push mpmc_push upmc_push spinlock_push spinlock_eb_push: push.c
+ $(CC) -DTRYUPMC $(CFLAGS) -o upmc_trypush push.c
+ $(CC) -DTRYMPMC $(CFLAGS) -o mpmc_trypush push.c
+ $(CC) -DMPNC $(CFLAGS) -o mpnc_push push.c
+ $(CC) -DMPMC $(CFLAGS) -o mpmc_push push.c
+ $(CC) -DUPMC $(CFLAGS) -o upmc_push push.c
+ $(CC) -DSPINLOCK $(CFLAGS) -o spinlock_push push.c
+ $(CC) -DSPINLOCK -DEB $(CFLAGS) -o spinlock_eb_push push.c
+
+upmc_trypop mpmc_trypop mpmc_pop tryupmc_pop upmc_pop spinlock_pop spinlock_eb_pop: pop.c
+ $(CC) -DTRYMPMC $(CFLAGS) -o mpmc_trypop pop.c
+ $(CC) -DTRYUPMC $(CFLAGS) -o upmc_trypop pop.c
+ $(CC) -DMPMC $(CFLAGS) -o mpmc_pop pop.c
+ $(CC) -DUPMC $(CFLAGS) -o upmc_pop pop.c
+ $(CC) -DSPINLOCK $(CFLAGS) -o spinlock_pop pop.c
+ $(CC) -DEB -DSPINLOCK $(CFLAGS) -o spinlock_eb_pop pop.c
+
+mpmc_trypair mpmc_pair spinlock_pair spinlock_eb_pair pthreads_pair: pair.c
+ $(CC) -DTRYMPMC $(CFLAGS) -o mpmc_trypair pair.c
+ $(CC) -DMPMC $(CFLAGS) -o mpmc_pair pair.c
+ $(CC) -DSPINLOCK $(CFLAGS) -o spinlock_pair pair.c
+ $(CC) -DEB -DSPINLOCK $(CFLAGS) -o spinlock_eb_pair pair.c
+ $(CC) -DPTHREADS $(CFLAGS) -o pthreads_pair pair.c
+
+clean:
+ rm -rf *~ *.o *.dSYM *.exe $(OBJECTS)
+
+include ../../../build/regressions.build
+CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_stack/validate/pair.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_stack/validate/pair.c b/lib/ck/regressions/ck_stack/validate/pair.c
new file mode 100644
index 0000000..c0f1bb1
--- /dev/null
+++ b/lib/ck/regressions/ck_stack/validate/pair.c
@@ -0,0 +1,249 @@
+/*
+ * Copyright 2009 Samy Al Bahra.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <assert.h>
+#include <ck_cc.h>
+#include <ck_pr.h>
+#ifdef SPINLOCK
+#include <ck_spinlock.h>
+#endif
+#include <ck_stack.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <pthread.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+#include "../../common.h"
+
+#ifndef ITEMS
+#define ITEMS (5765760)
+#endif
+
+#define TVTOD(tv) ((tv).tv_sec+((tv).tv_usec / (double)1000000))
+
+struct entry {
+ int value;
+#if defined(SPINLOCK) || defined(PTHREADS)
+ struct entry *next;
+#else
+ ck_stack_entry_t next;
+#endif
+} CK_CC_CACHELINE;
+
+#ifdef SPINLOCK
+static struct entry *stack CK_CC_CACHELINE;
+ck_spinlock_fas_t stack_spinlock = CK_SPINLOCK_FAS_INITIALIZER;
+#define UNLOCK ck_spinlock_fas_unlock
+#if defined(EB)
+#define LOCK ck_spinlock_fas_lock_eb
+#else
+#define LOCK ck_spinlock_fas_lock
+#endif
+#elif defined(PTHREADS)
+static struct entry *stack CK_CC_CACHELINE;
+pthread_mutex_t stack_spinlock = PTHREAD_MUTEX_INITIALIZER;
+#define LOCK pthread_mutex_lock
+#define UNLOCK pthread_mutex_unlock
+#else
+static ck_stack_t stack CK_CC_CACHELINE;
+CK_STACK_CONTAINER(struct entry, next, getvalue)
+#endif
+
+static struct affinity affinerator;
+static unsigned long long nthr;
+static volatile unsigned int barrier = 0;
+static unsigned int critical;
+
+static void *
+stack_thread(void *buffer)
+{
+#if (defined(MPMC) && defined(CK_F_STACK_POP_MPMC)) || (defined(UPMC) && defined(CK_F_STACK_POP_UPMC)) || (defined(TRYUPMC) && defined(CK_F_STACK_TRYPOP_UPMC)) || (defined(TRYMPMC) && defined(CK_F_STACK_TRYPOP_MPMC))
+ ck_stack_entry_t *ref;
+#endif
+ struct entry *entry = buffer;
+ unsigned long long i, n = ITEMS;
+ unsigned int seed;
+ int j;
+
+ if (aff_iterate(&affinerator)) {
+ perror("ERROR: failed to affine thread");
+ exit(EXIT_FAILURE);
+ }
+
+ while (barrier == 0);
+
+ for (i = 0; i < n; i++) {
+#if defined(MPMC)
+ ck_stack_push_mpmc(&stack, &entry->next);
+#elif defined(TRYMPMC)
+ while (ck_stack_trypush_mpmc(&stack, &entry->next) == false)
+ ck_pr_stall();
+#elif defined(UPMC)
+ ck_stack_push_upmc(&stack, &entry->next);
+#elif defined(TRYUPMC)
+ while (ck_stack_trypush_upmc(&stack, &entry->next) == false)
+ ck_pr_stall();
+#elif defined(SPINLOCK) || defined(PTHREADS)
+ LOCK(&stack_spinlock);
+ ck_pr_store_ptr(&entry->next, stack);
+ ck_pr_store_ptr(&stack, entry);
+ UNLOCK(&stack_spinlock);
+#else
+# error Undefined operation.
+#endif
+
+ if (critical) {
+ j = common_rand_r(&seed) % critical;
+ while (j--)
+ __asm__ __volatile__("" ::: "memory");
+ }
+
+#if defined(MPMC)
+#ifdef CK_F_STACK_POP_MPMC
+ ref = ck_stack_pop_mpmc(&stack);
+ entry = getvalue(ref);
+#endif
+#elif defined(TRYMPMC)
+#ifdef CK_F_STACK_TRYPOP_MPMC
+ while (ck_stack_trypop_mpmc(&stack, &ref) == false)
+ ck_pr_stall();
+ entry = getvalue(ref);
+#endif /* CK_F_STACK_TRYPOP_MPMC */
+#elif defined(UPMC)
+ ref = ck_stack_pop_upmc(&stack);
+ entry = getvalue(ref);
+#elif defined(SPINLOCK) || defined(PTHREADS)
+ LOCK(&stack_spinlock);
+ entry = stack;
+ stack = stack->next;
+ UNLOCK(&stack_spinlock);
+#else
+# error Undefined operation.
+#endif
+ }
+
+ return (NULL);
+}
+
+static void
+stack_assert(void)
+{
+
+#if defined(SPINLOCK) || defined(PTHREADS)
+ assert(stack == NULL);
+#else
+ assert(CK_STACK_ISEMPTY(&stack));
+#endif
+ return;
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct entry *bucket;
+ unsigned long long i, d;
+ pthread_t *thread;
+ struct timeval stv, etv;
+
+#if (defined(TRYMPMC) || defined(MPMC)) && (!defined(CK_F_STACK_PUSH_MPMC) || !defined(CK_F_STACK_POP_MPMC))
+ fprintf(stderr, "Unsupported.\n");
+ return 0;
+#endif
+
+ if (argc != 4) {
+ ck_error("Usage: stack <threads> <delta> <critical>\n");
+ }
+
+ {
+ char *e;
+
+ nthr = strtol(argv[1], &e, 10);
+ if (errno == ERANGE) {
+ perror("ERROR: too many threads");
+ exit(EXIT_FAILURE);
+ } else if (*e != '\0') {
+ ck_error("ERROR: input format is incorrect\n");
+ }
+
+ d = strtol(argv[2], &e, 10);
+ if (errno == ERANGE) {
+ perror("ERROR: delta is too large");
+ exit(EXIT_FAILURE);
+ } else if (*e != '\0') {
+ ck_error("ERROR: input format is incorrect\n");
+ }
+
+ critical = strtoul(argv[3], &e, 10);
+ if (errno == ERANGE) {
+ perror("ERROR: critical section is too large");
+ exit(EXIT_FAILURE);
+ } else if (*e != '\0') {
+ ck_error("ERROR: input format is incorrect\n");
+ }
+ }
+
+ srand(getpid());
+
+ affinerator.request = 0;
+ affinerator.delta = d;
+
+ bucket = malloc(sizeof(struct entry) * nthr);
+ assert(bucket != NULL);
+
+ thread = malloc(sizeof(pthread_t) * nthr);
+ assert(thread != NULL);
+
+ for (i = 0; i < nthr; i++)
+ pthread_create(&thread[i], NULL, stack_thread, bucket + i);
+
+ barrier = 1;
+
+ for (i = 0; i < nthr; i++)
+ pthread_join(thread[i], NULL);
+
+ barrier = 0;
+
+ for (i = 0; i < nthr; i++)
+ pthread_create(&thread[i], NULL, stack_thread, bucket + i);
+
+ common_gettimeofday(&stv, NULL);
+ barrier = 1;
+ for (i = 0; i < nthr; i++)
+ pthread_join(thread[i], NULL);
+ common_gettimeofday(&etv, NULL);
+
+ stack_assert();
+#ifdef _WIN32
+ printf("%3llu %.6f\n", nthr, TVTOD(etv) - TVTOD(stv));
+#else
+ printf("%3llu %.6lf\n", nthr, TVTOD(etv) - TVTOD(stv));
+#endif
+ return 0;
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_stack/validate/pop.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_stack/validate/pop.c b/lib/ck/regressions/ck_stack/validate/pop.c
new file mode 100644
index 0000000..ec31a52
--- /dev/null
+++ b/lib/ck/regressions/ck_stack/validate/pop.c
@@ -0,0 +1,269 @@
+/*
+ * Copyright 2009-2014 Samy Al Bahra.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <assert.h>
+#include <ck_cc.h>
+#include <ck_pr.h>
+#ifdef SPINLOCK
+#include <ck_spinlock.h>
+#endif
+#include <ck_stack.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <pthread.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+#include "../../common.h"
+
+#ifndef ITEMS
+#define ITEMS (5765760 * 2)
+#endif
+
+#define TVTOD(tv) ((tv).tv_sec+((tv).tv_usec / (double)1000000))
+
+struct entry {
+ int value;
+#ifdef SPINLOCK
+ struct entry *next;
+#else
+ ck_stack_entry_t next;
+#endif
+};
+
+#ifdef SPINLOCK
+static struct entry *stack CK_CC_CACHELINE;
+ck_spinlock_fas_t stack_spinlock = CK_SPINLOCK_FAS_INITIALIZER;
+#define UNLOCK ck_spinlock_fas_unlock
+#if defined(EB)
+#define LOCK ck_spinlock_fas_lock_eb
+#else
+#define LOCK ck_spinlock_fas_lock
+#endif
+#else
+static ck_stack_t stack CK_CC_CACHELINE;
+CK_STACK_CONTAINER(struct entry, next, getvalue)
+#endif
+
+static struct affinity affinerator = AFFINITY_INITIALIZER;
+static unsigned long long nthr;
+static volatile unsigned int barrier = 0;
+static unsigned int critical;
+
+static void *
+stack_thread(void *unused CK_CC_UNUSED)
+{
+#if (defined(MPMC) && defined(CK_F_STACK_POP_MPMC)) || (defined(UPMC) && defined(CK_F_STACK_POP_UPMC)) || (defined(TRYMPMC) && defined(CK_F_STACK_TRYPOP_MPMC)) || (defined(TRYUPMC) && defined(CK_F_STACK_TRYPOP_UPMC))
+ ck_stack_entry_t *ref;
+#endif
+ struct entry *entry = NULL;
+ unsigned long long i, n = ITEMS / nthr;
+ unsigned int seed;
+ int j, previous = INT_MAX;
+
+ if (aff_iterate(&affinerator)) {
+ perror("ERROR: failed to affine thread");
+ exit(EXIT_FAILURE);
+ }
+
+ while (barrier == 0);
+
+ for (i = 0; i < n; i++) {
+#ifdef MPMC
+#ifdef CK_F_STACK_POP_MPMC
+ ref = ck_stack_pop_mpmc(&stack);
+ assert(ref);
+ entry = getvalue(ref);
+#endif /* CK_F_STACK_POP_MPMC */
+#elif defined(TRYMPMC)
+#ifdef CK_F_STACK_TRYPOP_MPMC
+ while (ck_stack_trypop_mpmc(&stack, &ref) == false)
+ ck_pr_stall();
+ assert(ref);
+ entry = getvalue(ref);
+#endif /* CK_F_STACK_TRYPOP_MPMC */
+#elif defined(UPMC)
+ ref = ck_stack_pop_upmc(&stack);
+ assert(ref);
+ entry = getvalue(ref);
+#elif defined(TRYUPMC)
+ while (ck_stack_trypop_upmc(&stack, &ref) == false)
+ ck_pr_stall();
+ assert(ref);
+ entry = getvalue(ref);
+#elif defined(SPINLOCK)
+ LOCK(&stack_spinlock);
+ entry = stack;
+ stack = stack->next;
+ UNLOCK(&stack_spinlock);
+#else
+# error Undefined operation.
+#endif
+
+ if (critical) {
+ j = common_rand_r(&seed) % critical;
+ while (j--)
+ __asm__ __volatile__("" ::: "memory");
+ }
+
+ assert (previous >= entry->value);
+ previous = entry->value;
+ }
+
+ return (NULL);
+}
+
+static void
+stack_assert(void)
+{
+
+#ifdef SPINLOCK
+ assert(stack == NULL);
+#else
+ assert(CK_STACK_ISEMPTY(&stack));
+#endif
+ return;
+}
+
+static void
+push_stack(struct entry *bucket)
+{
+ unsigned long long i;
+
+#ifdef SPINLOCK
+ stack = NULL;
+#else
+ ck_stack_init(&stack);
+#endif
+
+ for (i = 0; i < ITEMS; i++) {
+ bucket[i].value = i % INT_MAX;
+#ifdef SPINLOCK
+ bucket[i].next = stack;
+ stack = bucket + i;
+#else
+ ck_stack_push_spnc(&stack, &bucket[i].next);
+#endif
+ }
+
+#ifndef SPINLOCK
+ ck_stack_entry_t *entry;
+ i = 0;
+ CK_STACK_FOREACH(&stack, entry) {
+ i++;
+ }
+ assert(i == ITEMS);
+#endif
+
+ return;
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct entry *bucket;
+ unsigned long long i, d;
+ pthread_t *thread;
+ struct timeval stv, etv;
+
+#if (defined(TRYMPMC) || defined(MPMC)) && (!defined(CK_F_STACK_PUSH_MPMC) || !defined(CK_F_STACK_POP_MPMC))
+ fprintf(stderr, "Unsupported.\n");
+ return 0;
+#endif
+
+ if (argc != 4) {
+ ck_error("Usage: stack <threads> <delta> <critical>\n");
+ }
+
+ {
+ char *e;
+
+ nthr = strtol(argv[1], &e, 10);
+ if (errno == ERANGE) {
+ perror("ERROR: too many threads");
+ exit(EXIT_FAILURE);
+ } else if (*e != '\0') {
+ ck_error("ERROR: input format is incorrect\n");
+ }
+
+ d = strtol(argv[2], &e, 10);
+ if (errno == ERANGE) {
+ perror("ERROR: delta is too large");
+ exit(EXIT_FAILURE);
+ } else if (*e != '\0') {
+ ck_error("ERROR: input format is incorrect\n");
+ }
+
+ critical = strtoul(argv[3], &e, 10);
+ if (errno == ERANGE) {
+ perror("ERROR: critical section is too large");
+ exit(EXIT_FAILURE);
+ } else if (*e != '\0') {
+ ck_error("ERROR: input format is incorrect\n");
+ }
+ }
+
+ srand(getpid());
+
+ affinerator.delta = d;
+ bucket = malloc(sizeof(struct entry) * ITEMS);
+ assert(bucket != NULL);
+
+ thread = malloc(sizeof(pthread_t) * nthr);
+ assert(thread != NULL);
+
+ push_stack(bucket);
+ for (i = 0; i < nthr; i++)
+ pthread_create(&thread[i], NULL, stack_thread, NULL);
+
+ barrier = 1;
+
+ for (i = 0; i < nthr; i++)
+ pthread_join(thread[i], NULL);
+
+ barrier = 0;
+
+ push_stack(bucket);
+ for (i = 0; i < nthr; i++)
+ pthread_create(&thread[i], NULL, stack_thread, NULL);
+
+ common_gettimeofday(&stv, NULL);
+ barrier = 1;
+ for (i = 0; i < nthr; i++)
+ pthread_join(thread[i], NULL);
+ common_gettimeofday(&etv, NULL);
+
+ stack_assert();
+#ifdef _WIN32
+ printf("%3llu %.6f\n", nthr, TVTOD(etv) - TVTOD(stv));
+#else
+ printf("%3llu %.6lf\n", nthr, TVTOD(etv) - TVTOD(stv));
+#endif
+ return 0;
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_stack/validate/push.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_stack/validate/push.c b/lib/ck/regressions/ck_stack/validate/push.c
new file mode 100644
index 0000000..f07b86c
--- /dev/null
+++ b/lib/ck/regressions/ck_stack/validate/push.c
@@ -0,0 +1,248 @@
+/*
+ * Copyright 2009-2014 Samy Al Bahra.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <assert.h>
+#include <ck_pr.h>
+#ifdef SPINLOCK
+#include <ck_spinlock.h>
+#endif
+#include <ck_stack.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <pthread.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+#include "../../common.h"
+
+#ifndef ITEMS
+#define ITEMS (5765760 * 2)
+#endif
+
+#define TVTOD(tv) ((tv).tv_sec+((tv).tv_usec / (double)1000000))
+
+struct entry {
+ int value;
+#ifdef SPINLOCK
+ struct entry *next;
+#else
+ ck_stack_entry_t next;
+#endif
+};
+
+#ifdef SPINLOCK
+static struct entry *stack CK_CC_CACHELINE;
+#else
+static ck_stack_t stack CK_CC_CACHELINE;
+#endif
+
+CK_STACK_CONTAINER(struct entry, next, getvalue)
+
+static struct affinity affinerator = AFFINITY_INITIALIZER;
+static unsigned long long nthr;
+static volatile unsigned int barrier = 0;
+static unsigned int critical;
+
+#if defined(SPINLOCK)
+ck_spinlock_fas_t stack_spinlock = CK_SPINLOCK_FAS_INITIALIZER;
+#define UNLOCK ck_spinlock_fas_unlock
+#if defined(EB)
+#define LOCK ck_spinlock_fas_lock_eb
+#else
+#define LOCK ck_spinlock_fas_lock
+#endif
+#elif defined(PTHREAD)
+pthread_mutex_t stack_spinlock = PTHREAD_MUTEX_INITIALIZER;
+#define LOCK pthread_mutex_lock
+#define UNLOCK pthread_mutex_unlock
+#endif
+
+static void *
+stack_thread(void *buffer)
+{
+ struct entry *bucket = buffer;
+ unsigned long long i, n = ITEMS / nthr;
+ unsigned int seed;
+ int j;
+
+ if (aff_iterate(&affinerator)) {
+ perror("ERROR: failed to affine thread");
+ exit(EXIT_FAILURE);
+ }
+
+ while (barrier == 0);
+
+ for (i = 0; i < n; i++) {
+ bucket[i].value = (i + 1) * 2;
+
+#if defined(MPNC)
+ ck_stack_push_mpnc(&stack, &bucket[i].next);
+#elif defined(MPMC)
+ ck_stack_push_mpmc(&stack, &bucket[i].next);
+#elif defined(TRYMPMC)
+ while (ck_stack_trypush_mpmc(&stack, &bucket[i].next) == false)
+ ck_pr_stall();
+#elif defined(TRYUPMC)
+ while (ck_stack_trypush_upmc(&stack, &bucket[i].next) == false)
+ ck_pr_stall();
+#elif defined(UPMC)
+ ck_stack_push_upmc(&stack, &bucket[i].next);
+#elif defined(SPINLOCK) || defined(PTHREADS)
+ LOCK(&stack_spinlock);
+ bucket[i].next = stack;
+ stack = bucket + i;
+ UNLOCK(&stack_spinlock);
+#else
+# error Undefined operation.
+#endif
+
+ if (critical) {
+ j = common_rand_r(&seed) % critical;
+ while (j--)
+ __asm__ __volatile__("" ::: "memory");
+ }
+ }
+
+ return (NULL);
+}
+
+static void
+stack_assert(void)
+{
+#ifndef SPINLOCK
+ ck_stack_entry_t *n;
+#endif
+ struct entry *p;
+ unsigned long long c = 0;
+
+#ifdef SPINLOCK
+ for (p = stack; p; p = p->next)
+ c++;
+#else
+ CK_STACK_FOREACH(&stack, n) {
+ p = getvalue(n);
+ (void)((volatile struct entry *)p)->value;
+ c++;
+ }
+#endif
+
+ assert(c == ITEMS);
+ return;
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct entry *bucket;
+ unsigned long long i, d, n;
+ pthread_t *thread;
+ struct timeval stv, etv;
+
+ if (argc != 4) {
+ ck_error("Usage: stack <threads> <delta> <critical>\n");
+ }
+
+ {
+ char *e;
+
+ nthr = strtol(argv[1], &e, 10);
+ if (errno == ERANGE) {
+ perror("ERROR: too many threads");
+ exit(EXIT_FAILURE);
+ } else if (*e != '\0') {
+ ck_error("ERROR: input format is incorrect\n");
+ }
+
+ d = strtol(argv[2], &e, 10);
+ if (errno == ERANGE) {
+ perror("ERROR: delta is too large");
+ exit(EXIT_FAILURE);
+ } else if (*e != '\0') {
+ ck_error("ERROR: input format is incorrect\n");
+ }
+
+ critical = strtoul(argv[3], &e, 10);
+ if (errno == ERANGE) {
+ perror("ERROR: critical section is too large");
+ exit(EXIT_FAILURE);
+ } else if (*e != '\0') {
+ ck_error("ERROR: input format is incorrect\n");
+ }
+ }
+
+ srand(getpid());
+
+ affinerator.request = 0;
+ affinerator.delta = d;
+ n = ITEMS / nthr;
+
+#ifndef SPINLOCK
+ ck_stack_init(&stack);
+#else
+ stack = NULL;
+#endif
+
+ bucket = malloc(sizeof(struct entry) * ITEMS);
+ assert(bucket != NULL);
+
+ thread = malloc(sizeof(pthread_t) * nthr);
+ assert(thread != NULL);
+
+ for (i = 0; i < nthr; i++)
+ pthread_create(&thread[i], NULL, stack_thread, bucket + i * n);
+
+ barrier = 1;
+
+ for (i = 0; i < nthr; i++)
+ pthread_join(thread[i], NULL);
+
+ barrier = 0;
+
+#ifndef SPINLOCK
+ ck_stack_init(&stack);
+#else
+ stack = NULL;
+#endif
+
+ for (i = 0; i < nthr; i++)
+ pthread_create(&thread[i], NULL, stack_thread, bucket + i * n);
+
+ common_gettimeofday(&stv, NULL);
+ barrier = 1;
+ for (i = 0; i < nthr; i++)
+ pthread_join(thread[i], NULL);
+ common_gettimeofday(&etv, NULL);
+
+ stack_assert();
+#ifdef _WIN32
+ printf("%3llu %.6f\n", nthr, TVTOD(etv) - TVTOD(stv));
+#else
+ printf("%3llu %.6lf\n", nthr, TVTOD(etv) - TVTOD(stv));
+#endif
+ return 0;
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_stack/validate/serial.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_stack/validate/serial.c b/lib/ck/regressions/ck_stack/validate/serial.c
new file mode 100644
index 0000000..c4bea18
--- /dev/null
+++ b/lib/ck/regressions/ck_stack/validate/serial.c
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2009-2014 Samy Al Bahra.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <ck_stack.h>
+
+#ifndef SIZE
+#define SIZE 1024000
+#endif
+
+struct entry {
+ int value;
+ ck_stack_entry_t next;
+};
+
+CK_STACK_CONTAINER(struct entry, next, get_entry)
+
+#define LOOP(PUSH, POP) \
+ for (i = 0; i < SIZE; i++) { \
+ entries[i].value = i; \
+ PUSH(stack, &entries[i].next); \
+ } \
+ for (i = SIZE - 1; i >= 0; i--) { \
+ entry = POP(stack); \
+ assert(entry); \
+ assert(get_entry(entry)->value == i); \
+ }
+
+static void
+serial(ck_stack_t *stack)
+{
+ struct entry *entries;
+ ck_stack_entry_t *entry;
+ int i;
+
+ ck_stack_init(stack);
+
+ entries = malloc(sizeof(struct entry) * SIZE);
+ assert(entries != NULL);
+
+ LOOP(ck_stack_push_upmc, ck_stack_pop_upmc);
+#ifdef CK_F_STACK_POP_MPMC
+ LOOP(ck_stack_push_mpmc, ck_stack_pop_mpmc);
+#endif
+ LOOP(ck_stack_push_mpnc, ck_stack_pop_upmc);
+ LOOP(ck_stack_push_spnc, ck_stack_pop_npsc);
+
+ return;
+}
+
+int
+main(void)
+{
+ ck_stack_t stack CK_CC_CACHELINE;
+
+ serial(&stack);
+ return (0);
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_swlock/benchmark/Makefile
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_swlock/benchmark/Makefile b/lib/ck/regressions/ck_swlock/benchmark/Makefile
new file mode 100644
index 0000000..4ec728c
--- /dev/null
+++ b/lib/ck/regressions/ck_swlock/benchmark/Makefile
@@ -0,0 +1,17 @@
+.PHONY: clean distribution
+
+OBJECTS=latency throughput
+
+all: $(OBJECTS)
+
+latency: latency.c ../../../include/ck_swlock.h
+ $(CC) $(CFLAGS) -o latency latency.c
+
+throughput: throughput.c ../../../include/ck_swlock.h
+ $(CC) $(CFLAGS) -o throughput throughput.c
+
+clean:
+ rm -rf *.dSYM *.exe *~ *.o $(OBJECTS)
+
+include ../../../build/regressions.build
+CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_swlock/benchmark/latency.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_swlock/benchmark/latency.c b/lib/ck/regressions/ck_swlock/benchmark/latency.c
new file mode 100644
index 0000000..7261546
--- /dev/null
+++ b/lib/ck/regressions/ck_swlock/benchmark/latency.c
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2011-2014 Samy Al Bahra.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <ck_swlock.h>
+#include <inttypes.h>
+#include <stdio.h>
+
+#include "../../common.h"
+
+#define CK_F_PR_RTM
+
+#ifndef STEPS
+#define STEPS 2000000
+#endif
+
+int
+main(void)
+{
+ uint64_t s_b, e_b, i;
+ ck_swlock_t swlock = CK_SWLOCK_INITIALIZER;
+
+ for (i = 0; i < STEPS; i++) {
+ ck_swlock_write_lock(&swlock);
+ ck_swlock_write_unlock(&swlock);
+ }
+
+ s_b = rdtsc();
+ for (i = 0; i < STEPS; i++) {
+ ck_swlock_write_lock(&swlock);
+ ck_swlock_write_unlock(&swlock);
+ }
+ e_b = rdtsc();
+ printf(" WRITE: swlock %15" PRIu64 "\n", (e_b - s_b) / STEPS);
+
+ for (i = 0; i < STEPS; i++) {
+ ck_swlock_read_lock(&swlock);
+ ck_swlock_read_unlock(&swlock);
+ }
+
+ s_b = rdtsc();
+ for (i = 0; i < STEPS; i++) {
+ ck_swlock_read_lock(&swlock);
+ ck_swlock_read_unlock(&swlock);
+ }
+ e_b = rdtsc();
+ printf(" READ: swlock %15" PRIu64 "\n", (e_b - s_b) / STEPS);
+
+ for (i = 0; i < STEPS; i++) {
+ ck_swlock_write_latch(&swlock);
+ ck_swlock_write_unlatch(&swlock);
+ }
+
+ s_b = rdtsc();
+ for (i = 0; i < STEPS; i++) {
+ ck_swlock_write_latch(&swlock);
+ ck_swlock_write_unlatch(&swlock);
+ }
+ e_b = rdtsc();
+ printf(" LATCH: swlock %15" PRIu64 "\n", (e_b - s_b) / STEPS);
+
+ return 0;
+}
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_swlock/benchmark/throughput.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_swlock/benchmark/throughput.c b/lib/ck/regressions/ck_swlock/benchmark/throughput.c
new file mode 100644
index 0000000..fa3cf1c
--- /dev/null
+++ b/lib/ck/regressions/ck_swlock/benchmark/throughput.c
@@ -0,0 +1,183 @@
+/*
+ * Copyright 2011-2014 Samy Al Bahra.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <ck_swlock.h>
+#include <inttypes.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "../../common.h"
+
+#ifndef STEPS
+#define STEPS 1000000
+#endif
+
+static int barrier;
+static int threads;
+static unsigned int flag CK_CC_CACHELINE;
+static struct {
+ ck_swlock_t lock;
+} rw CK_CC_CACHELINE = {
+ .lock = CK_SWLOCK_INITIALIZER
+};
+
+static struct affinity affinity;
+
+static void *
+thread_lock(void *pun)
+{
+ uint64_t s_b, e_b, a, i;
+ uint64_t *value = pun;
+
+ if (aff_iterate(&affinity) != 0) {
+ perror("ERROR: Could not affine thread");
+ exit(EXIT_FAILURE);
+ }
+
+ ck_pr_inc_int(&barrier);
+ while (ck_pr_load_int(&barrier) != threads)
+ ck_pr_stall();
+
+ for (i = 1, a = 0;; i++) {
+ s_b = rdtsc();
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ ck_swlock_read_lock(&rw.lock);
+ ck_swlock_read_unlock(&rw.lock);
+ e_b = rdtsc();
+
+ a += (e_b - s_b) >> 4;
+
+ if (ck_pr_load_uint(&flag) == 1)
+ break;
+ }
+
+ ck_pr_inc_int(&barrier);
+ while (ck_pr_load_int(&barrier) != threads * 2)
+ ck_pr_stall();
+
+ *value = (a / i);
+ return NULL;
+}
+
+static void
+swlock_test(pthread_t *p, int d, uint64_t *latency, void *(*f)(void *), const char *label)
+{
+ int t;
+
+ ck_pr_store_int(&barrier, 0);
+ ck_pr_store_uint(&flag, 0);
+
+ affinity.delta = d;
+ affinity.request = 0;
+
+ fprintf(stderr, "Creating threads (%s)...", label);
+ for (t = 0; t < threads; t++) {
+ if (pthread_create(&p[t], NULL, f, latency + t) != 0) {
+ ck_error("ERROR: Could not create thread %d\n", t);
+ }
+ }
+ fprintf(stderr, "done\n");
+
+ common_sleep(10);
+ ck_pr_store_uint(&flag, 1);
+
+ fprintf(stderr, "Waiting for threads to finish acquisition regression...");
+ for (t = 0; t < threads; t++)
+ pthread_join(p[t], NULL);
+ fprintf(stderr, "done\n\n");
+
+ for (t = 1; t <= threads; t++)
+ printf("%10u %20" PRIu64 "\n", t, latency[t - 1]);
+
+ fprintf(stderr, "\n");
+ return;
+}
+
+
+int
+main(int argc, char *argv[])
+{
+ int d;
+ pthread_t *p;
+ uint64_t *latency;
+
+ if (argc != 3) {
+ ck_error("Usage: throughput <delta> <threads>\n");
+ }
+
+ threads = atoi(argv[2]);
+ if (threads <= 0) {
+ ck_error("ERROR: Threads must be a value > 0.\n");
+ }
+
+ p = malloc(sizeof(pthread_t) * threads);
+ if (p == NULL) {
+ ck_error("ERROR: Failed to initialize thread.\n");
+ }
+
+ latency = malloc(sizeof(uint64_t) * threads);
+ if (latency == NULL) {
+ ck_error("ERROR: Failed to create latency buffer.\n");
+ }
+
+ d = atoi(argv[1]);
+ swlock_test(p, d, latency, thread_lock, "swlock");
+
+ return 0;
+}
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_swlock/validate/Makefile
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_swlock/validate/Makefile b/lib/ck/regressions/ck_swlock/validate/Makefile
new file mode 100644
index 0000000..54d62f2
--- /dev/null
+++ b/lib/ck/regressions/ck_swlock/validate/Makefile
@@ -0,0 +1,17 @@
+.PHONY: check clean distribution
+
+OBJECTS=validate
+
+all: $(OBJECTS)
+
+validate: validate.c ../../../include/ck_swlock.h
+ $(CC) $(CFLAGS) -o validate validate.c
+
+check: all
+ ./validate $(CORES) 1
+
+clean:
+ rm -rf *.dSYM *.exe *~ *.o $(OBJECTS)
+
+include ../../../build/regressions.build
+CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_swlock/validate/validate.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_swlock/validate/validate.c b/lib/ck/regressions/ck_swlock/validate/validate.c
new file mode 100644
index 0000000..11366ce
--- /dev/null
+++ b/lib/ck/regressions/ck_swlock/validate/validate.c
@@ -0,0 +1,455 @@
+/*
+ * Copyright 2014 Jaidev Sridhar.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <errno.h>
+#include <inttypes.h>
+#include <pthread.h>
+#include <math.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <unistd.h>
+#include <sys/time.h>
+
+#include <ck_pr.h>
+#include <ck_swlock.h>
+
+#include "../../common.h"
+
+#ifndef ITERATE
+#define ITERATE 1000000
+#endif
+
+static struct affinity a;
+static unsigned int locked;
+static int nthr;
+static ck_swlock_t lock = CK_SWLOCK_INITIALIZER;
+static ck_swlock_t copy;
+#ifdef CK_F_PR_RTM
+static void *
+thread_rtm_adaptive(void *arg)
+{
+ unsigned int i = ITERATE;
+ unsigned int l;
+ int tid = ck_pr_load_int(arg);
+
+ struct ck_elide_config config = CK_ELIDE_CONFIG_DEFAULT_INITIALIZER;
+ struct ck_elide_stat st = CK_ELIDE_STAT_INITIALIZER;
+
+ if (aff_iterate(&a)) {
+ perror("ERROR: Could not affine thread");
+ exit(EXIT_FAILURE);
+ }
+
+ while (i--) {
+ if (tid == 0) {
+ CK_ELIDE_LOCK_ADAPTIVE(ck_swlock_write, &st, &config, &lock);
+ {
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
+ }
+
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+
+ l = ck_pr_load_uint(&locked);
+ if (l != 8) {
+ ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
+ }
+
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
+ }
+ }
+ CK_ELIDE_UNLOCK_ADAPTIVE(ck_swlock_write, &st, &lock);
+ }
+
+ CK_ELIDE_LOCK(ck_swlock_read, &lock);
+ {
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
+ }
+ }
+ CK_ELIDE_UNLOCK(ck_swlock_read, &lock);
+ }
+
+ return NULL;
+}
+
+static void *
+thread_rtm_mix(void *arg)
+{
+ unsigned int i = ITERATE;
+ unsigned int l;
+ int tid = ck_pr_load_int(arg);
+
+ if (aff_iterate(&a)) {
+ perror("ERROR: Could not affine thread");
+ exit(EXIT_FAILURE);
+ }
+
+ while (i--) {
+ if (tid == 0) {
+ if (i & 1) {
+ CK_ELIDE_LOCK(ck_swlock_write, &lock);
+ } else {
+ ck_swlock_write_lock(&lock);
+ }
+
+ {
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
+ }
+
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+
+ l = ck_pr_load_uint(&locked);
+ if (l != 8) {
+ ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
+ }
+
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
+ }
+ }
+
+ if (i & 1) {
+ CK_ELIDE_UNLOCK(ck_swlock_write, &lock);
+ } else {
+ ck_swlock_write_unlock(&lock);
+ }
+ }
+ if (i & 1) {
+ CK_ELIDE_LOCK(ck_swlock_read, &lock);
+ } else {
+ ck_swlock_read_lock(&lock);
+ }
+
+ {
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
+ }
+ }
+
+ if (i & 1) {
+ CK_ELIDE_UNLOCK(ck_swlock_read, &lock);
+ } else {
+ ck_swlock_read_unlock(&lock);
+ }
+ }
+
+ return (NULL);
+}
+
+static void *
+thread_rtm(void *arg)
+{
+ unsigned int i = ITERATE;
+ unsigned int l;
+ int tid = ck_pr_load_int(arg);
+
+ if (aff_iterate(&a)) {
+ perror("ERROR: Could not affine thread");
+ exit(EXIT_FAILURE);
+ }
+
+ while (i--) {
+ if (tid == 0) {
+ CK_ELIDE_LOCK(ck_swlock_write, &lock);
+ {
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
+ }
+
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+
+ l = ck_pr_load_uint(&locked);
+ if (l != 8) {
+ ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
+ }
+
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
+ }
+ }
+ CK_ELIDE_UNLOCK(ck_swlock_write, &lock);
+ }
+
+ CK_ELIDE_LOCK(ck_swlock_read, &lock);
+ {
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
+ }
+ }
+ CK_ELIDE_UNLOCK(ck_swlock_read, &lock);
+ }
+
+ return (NULL);
+}
+#endif /* CK_F_PR_RTM */
+
+static void *
+thread_latch(void *arg)
+{
+ unsigned int i = ITERATE;
+ unsigned int l;
+ int tid = ck_pr_load_int(arg);
+
+ if (aff_iterate(&a)) {
+ perror("ERROR: Could not affine thread");
+ exit(EXIT_FAILURE);
+ }
+
+ while (i--) {
+ if (tid == 0) {
+ /* Writer */
+ ck_swlock_write_latch(&lock);
+ {
+ memcpy(©, &lock, sizeof(ck_swlock_t));
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
+ }
+
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+
+ l = ck_pr_load_uint(&locked);
+ if (l != 8) {
+ ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
+ }
+
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
+ }
+ memcpy(&lock, ©, sizeof(ck_swlock_t));
+ }
+ ck_swlock_write_unlatch(&lock);
+ }
+
+ ck_swlock_read_lock(&lock);
+ {
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
+ }
+ }
+ ck_swlock_read_unlock(&lock);
+ }
+
+ return (NULL);
+}
+
+static void *
+thread(void *arg)
+{
+ unsigned int i = ITERATE;
+ unsigned int l;
+ int tid = ck_pr_load_int(arg);
+
+ if (aff_iterate(&a)) {
+ perror("ERROR: Could not affine thread");
+ exit(EXIT_FAILURE);
+ }
+
+ while (i--) {
+ if (tid == 0) {
+ /* Writer */
+ ck_swlock_write_lock(&lock);
+ {
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
+ }
+
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+
+ l = ck_pr_load_uint(&locked);
+ if (l != 8) {
+ ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
+ }
+
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
+ }
+ }
+ ck_swlock_write_unlock(&lock);
+ }
+
+ ck_swlock_read_lock(&lock);
+ {
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
+ }
+ }
+ ck_swlock_read_unlock(&lock);
+ }
+
+ return (NULL);
+}
+
+static void
+swlock_test(pthread_t *threads, void *(*f)(void *), const char *test)
+{
+ int i, tid[nthr];
+
+ fprintf(stderr, "Creating threads (%s)...", test);
+ for (i = 0; i < nthr; i++) {
+ ck_pr_store_int(&tid[i], i);
+ if (pthread_create(&threads[i], NULL, f, &tid[i])) {
+ ck_error("ERROR: Could not create thread %d\n", i);
+ }
+ }
+ fprintf(stderr, ".");
+
+ for (i = 0; i < nthr; i++)
+ pthread_join(threads[i], NULL);
+ fprintf(stderr, "done (passed)\n");
+ return;
+}
+
+int
+main(int argc, char *argv[])
+{
+ pthread_t *threads;
+
+ if (argc != 3) {
+ ck_error("Usage: validate <number of threads> <affinity delta>\n");
+ }
+
+ nthr = atoi(argv[1]);
+ if (nthr <= 0) {
+ ck_error("ERROR: Number of threads must be greater than 0\n");
+ }
+
+ threads = malloc(sizeof(pthread_t) * nthr);
+ if (threads == NULL) {
+ ck_error("ERROR: Could not allocate thread structures\n");
+ }
+
+ a.delta = atoi(argv[2]);
+
+ swlock_test(threads, thread, "regular");
+ swlock_test(threads, thread_latch, "latch");
+#ifdef CK_F_PR_RTM
+ swlock_test(threads, thread_rtm, "rtm");
+ swlock_test(threads, thread_rtm_mix, "rtm-mix");
+ swlock_test(threads, thread_rtm_adaptive, "rtm-adaptive");
+#endif
+ return 0;
+}
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_tflock/benchmark/Makefile
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_tflock/benchmark/Makefile b/lib/ck/regressions/ck_tflock/benchmark/Makefile
new file mode 100644
index 0000000..ed63504
--- /dev/null
+++ b/lib/ck/regressions/ck_tflock/benchmark/Makefile
@@ -0,0 +1,17 @@
+.PHONY: clean distribution
+
+OBJECTS=latency throughput
+
+all: $(OBJECTS)
+
+latency: latency.c ../../../include/ck_rwlock.h ../../../include/ck_elide.h
+ $(CC) $(CFLAGS) -o latency latency.c
+
+throughput: throughput.c ../../../include/ck_rwlock.h ../../../include/ck_elide.h
+ $(CC) $(CFLAGS) -o throughput throughput.c
+
+clean:
+ rm -rf *.dSYM *.exe *~ *.o $(OBJECTS)
+
+include ../../../build/regressions.build
+CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_tflock/benchmark/latency.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_tflock/benchmark/latency.c b/lib/ck/regressions/ck_tflock/benchmark/latency.c
new file mode 100644
index 0000000..433a42a
--- /dev/null
+++ b/lib/ck/regressions/ck_tflock/benchmark/latency.c
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2011-2014 Samy Al Bahra.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <ck_tflock.h>
+#include <inttypes.h>
+#include <stdio.h>
+
+#include "../../common.h"
+
+#define CK_F_PR_RTM
+
+#ifndef STEPS
+#define STEPS 2000000
+#endif
+
+int
+main(void)
+{
+ uint64_t s_b, e_b, i;
+ ck_tflock_ticket_t tflock = CK_TFLOCK_TICKET_INITIALIZER;
+
+ for (i = 0; i < STEPS; i++) {
+ ck_tflock_ticket_write_lock(&tflock);
+ ck_tflock_ticket_write_unlock(&tflock);
+ }
+
+ s_b = rdtsc();
+ for (i = 0; i < STEPS; i++) {
+ ck_tflock_ticket_write_lock(&tflock);
+ ck_tflock_ticket_write_unlock(&tflock);
+ }
+ e_b = rdtsc();
+ printf(" WRITE: tflock %15" PRIu64 "\n", (e_b - s_b) / STEPS);
+
+ for (i = 0; i < STEPS; i++) {
+ ck_tflock_ticket_read_lock(&tflock);
+ ck_tflock_ticket_read_unlock(&tflock);
+ }
+
+ s_b = rdtsc();
+ for (i = 0; i < STEPS; i++) {
+ ck_tflock_ticket_read_lock(&tflock);
+ ck_tflock_ticket_read_unlock(&tflock);
+ }
+ e_b = rdtsc();
+ printf(" READ: tflock %15" PRIu64 "\n", (e_b - s_b) / STEPS);
+
+ return 0;
+}
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_tflock/benchmark/throughput.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_tflock/benchmark/throughput.c b/lib/ck/regressions/ck_tflock/benchmark/throughput.c
new file mode 100644
index 0000000..2689842
--- /dev/null
+++ b/lib/ck/regressions/ck_tflock/benchmark/throughput.c
@@ -0,0 +1,182 @@
+/*
+ * Copyright 2011-2014 Samy Al Bahra.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <ck_tflock.h>
+#include <inttypes.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "../../common.h"
+
+#ifndef STEPS
+#define STEPS 1000000
+#endif
+
+static int barrier;
+static int threads;
+static unsigned int flag CK_CC_CACHELINE;
+static struct {
+ ck_tflock_ticket_t lock;
+} rw CK_CC_CACHELINE = {
+ .lock = CK_TFLOCK_TICKET_INITIALIZER
+};
+
+static struct affinity affinity;
+
+static void *
+thread_lock(void *pun)
+{
+ uint64_t s_b, e_b, a, i;
+ uint64_t *value = pun;
+
+ if (aff_iterate(&affinity) != 0) {
+ perror("ERROR: Could not affine thread");
+ exit(EXIT_FAILURE);
+ }
+
+ ck_pr_inc_int(&barrier);
+ while (ck_pr_load_int(&barrier) != threads)
+ ck_pr_stall();
+
+ for (i = 1, a = 0;; i++) {
+ s_b = rdtsc();
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ ck_tflock_ticket_read_lock(&rw.lock);
+ ck_tflock_ticket_read_unlock(&rw.lock);
+ e_b = rdtsc();
+
+ a += (e_b - s_b) >> 4;
+
+ if (ck_pr_load_uint(&flag) == 1)
+ break;
+ }
+
+ ck_pr_inc_int(&barrier);
+ while (ck_pr_load_int(&barrier) != threads * 2)
+ ck_pr_stall();
+
+ *value = (a / i);
+ return NULL;
+}
+
+static void
+tflock_test(pthread_t *p, int d, uint64_t *latency, void *(*f)(void *), const char *label)
+{
+ int t;
+
+ ck_pr_store_int(&barrier, 0);
+ ck_pr_store_uint(&flag, 0);
+
+ affinity.delta = d;
+ affinity.request = 0;
+
+ fprintf(stderr, "Creating threads (%s)...", label);
+ for (t = 0; t < threads; t++) {
+ if (pthread_create(&p[t], NULL, f, latency + t) != 0) {
+ ck_error("ERROR: Could not create thread %d\n", t);
+ }
+ }
+ fprintf(stderr, "done\n");
+
+ common_sleep(10);
+ ck_pr_store_uint(&flag, 1);
+
+ fprintf(stderr, "Waiting for threads to finish acquisition regression...");
+ for (t = 0; t < threads; t++)
+ pthread_join(p[t], NULL);
+ fprintf(stderr, "done\n\n");
+
+ for (t = 1; t <= threads; t++)
+ printf("%10u %20" PRIu64 "\n", t, latency[t - 1]);
+
+ fprintf(stderr, "\n");
+ return;
+}
+
+
+int
+main(int argc, char *argv[])
+{
+ int d;
+ pthread_t *p;
+ uint64_t *latency;
+
+ if (argc != 3) {
+ ck_error("Usage: throughput <delta> <threads>\n");
+ }
+
+ threads = atoi(argv[2]);
+ if (threads <= 0) {
+ ck_error("ERROR: Threads must be a value > 0.\n");
+ }
+
+ p = malloc(sizeof(pthread_t) * threads);
+ if (p == NULL) {
+ ck_error("ERROR: Failed to initialize thread.\n");
+ }
+
+ latency = malloc(sizeof(uint64_t) * threads);
+ if (latency == NULL) {
+ ck_error("ERROR: Failed to create latency buffer.\n");
+ }
+
+ d = atoi(argv[1]);
+ tflock_test(p, d, latency, thread_lock, "tflock");
+ return 0;
+}
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_tflock/validate/Makefile
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_tflock/validate/Makefile b/lib/ck/regressions/ck_tflock/validate/Makefile
new file mode 100644
index 0000000..6ae7c73
--- /dev/null
+++ b/lib/ck/regressions/ck_tflock/validate/Makefile
@@ -0,0 +1,17 @@
+.PHONY: check clean distribution
+
+OBJECTS=validate
+
+all: $(OBJECTS)
+
+validate: validate.c ../../../include/ck_tflock.h ../../../include/ck_elide.h
+ $(CC) $(CFLAGS) -o validate validate.c
+
+check: all
+ ./validate $(CORES) 1
+
+clean:
+ rm -rf *.dSYM *.exe *~ *.o $(OBJECTS)
+
+include ../../../build/regressions.build
+CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f098175e/lib/ck/regressions/ck_tflock/validate/validate.c
----------------------------------------------------------------------
diff --git a/lib/ck/regressions/ck_tflock/validate/validate.c b/lib/ck/regressions/ck_tflock/validate/validate.c
new file mode 100644
index 0000000..1b49e3b
--- /dev/null
+++ b/lib/ck/regressions/ck_tflock/validate/validate.c
@@ -0,0 +1,158 @@
+/*
+ * Copyright 2011-2014 Samy Al Bahra.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <errno.h>
+#include <inttypes.h>
+#include <pthread.h>
+#include <math.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <unistd.h>
+#include <sys/time.h>
+
+#include <ck_pr.h>
+#include <ck_tflock.h>
+
+#include "../../common.h"
+
+#ifndef ITERATE
+#define ITERATE 1000000
+#endif
+
+static struct affinity a;
+static unsigned int locked;
+static int nthr;
+static ck_tflock_ticket_t lock = CK_TFLOCK_TICKET_INITIALIZER;
+
+static void *
+thread(void *null CK_CC_UNUSED)
+{
+ unsigned int i = ITERATE;
+ unsigned int l;
+
+ if (aff_iterate(&a)) {
+ perror("ERROR: Could not affine thread");
+ exit(EXIT_FAILURE);
+ }
+
+ while (i--) {
+ ck_tflock_ticket_write_lock(&lock);
+ {
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
+ }
+
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+ ck_pr_inc_uint(&locked);
+
+ l = ck_pr_load_uint(&locked);
+ if (l != 8) {
+ ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
+ }
+
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+ ck_pr_dec_uint(&locked);
+
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
+ }
+ }
+ ck_tflock_ticket_write_unlock(&lock);
+
+ ck_tflock_ticket_read_lock(&lock);
+ {
+ l = ck_pr_load_uint(&locked);
+ if (l != 0) {
+ ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
+ }
+ }
+ ck_tflock_ticket_read_unlock(&lock);
+ }
+
+ return (NULL);
+}
+
+static void
+tflock_ticket_test(pthread_t *threads, void *(*f)(void *), const char *test)
+{
+ int i;
+
+ fprintf(stderr, "Creating threads (%s)...", test);
+ for (i = 0; i < nthr; i++) {
+ if (pthread_create(&threads[i], NULL, f, NULL)) {
+ ck_error("ERROR: Could not create thread %d\n", i);
+ }
+ }
+ fprintf(stderr, ".");
+
+ for (i = 0; i < nthr; i++)
+ pthread_join(threads[i], NULL);
+ fprintf(stderr, "done (passed)\n");
+ return;
+}
+
+int
+main(int argc, char *argv[])
+{
+ pthread_t *threads;
+
+ if (argc != 3) {
+ ck_error("Usage: validate <number of threads> <affinity delta>\n");
+ }
+
+ nthr = atoi(argv[1]);
+ if (nthr <= 0) {
+ ck_error("ERROR: Number of threads must be greater than 0\n");
+ }
+
+ threads = malloc(sizeof(pthread_t) * nthr);
+ if (threads == NULL) {
+ ck_error("ERROR: Could not allocate thread structures\n");
+ }
+
+ a.delta = atoi(argv[2]);
+
+ tflock_ticket_test(threads, thread, "regular");
+ ck_tflock_ticket_init(&lock);
+ return 0;
+}
+