diff --git a/include/ck_pflock.h b/include/ck_pflock.h new file mode 100644 index 0000000..2c651ea --- /dev/null +++ b/include/ck_pflock.h @@ -0,0 +1,141 @@ +/* + * Copyright 2013 John Wittrock. + * Copyright 2013 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 EXCK_PFLOCK_PRESS 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. + */ + +#ifndef _CK_PFLOCK_H +#define _CK_PFLOCK_H + +/* + * This is a naive implementation of phase-fair locks derived + * from the work described in: + * Brandenburg, B. and Anderson, J. 2010. Spin-Based + * Reader-Writer Synchronization for Multiprocessor Real-Time Systems + */ + +#include +#include + +struct ck_pflock { + uint32_t rin; + uint32_t rout; + uint32_t win; + uint32_t wout; +}; +typedef struct ck_pflock ck_pflock_t; + +#define CK_PFLOCK_LSB 0xFFFFFFF0 +#define CK_PFLOCK_RINC 0x100 /* Reader increment value. */ +#define CK_PFLOCK_WBITS 0x3 /* Writer bits in reader. */ +#define CK_PFLOCK_PRES 0x2 /* Writer present bit. */ +#define CK_PFLOCK_PHID 0x1 /* Phase ID bit. */ + +#define CK_PFLOCK_INITIALIZER {0, 0, 0, 0} + +CK_CC_INLINE static void +ck_pflock_init(struct ck_pflock *pf) +{ + + pf->rin = 0; + pf->rout = 0; + pf->win = 0; + pf->wout = 0; + ck_pr_fence_memory(); + return; +} + +CK_CC_INLINE static void +ck_pflock_write_unlock(ck_pflock_t *pf) +{ + + ck_pr_fence_memory(); + + /* Migrate from write phase to read phase. */ + ck_pr_and_32(&pf->rin, CK_PFLOCK_LSB); + + /* Allow other writers to continue. */ + ck_pr_faa_32(&pf->wout, 1); + return; +} + +CK_CC_INLINE static void +ck_pflock_write_lock(ck_pflock_t *pf) +{ + uint32_t ticket; + + /* Acquire ownership of write-phase. */ + ticket = ck_pr_faa_32(&pf->win, 1); + while (ck_pr_load_32(&pf->wout) != ticket) + ck_pr_stall(); + + /* + * Acquire ticket on read-side in order to allow them + * to flush. Indicates to any incoming reader that a + * write-phase is pending. + */ + ticket = ck_pr_faa_32(&pf->rin, + (ticket & CK_PFLOCK_PHID) | CK_PFLOCK_PRES); + + /* Wait for any pending readers to flush. */ + while (ck_pr_load_32(&pf->rout) != ticket) + ck_pr_stall(); + + ck_pr_fence_memory(); + return; +} + +CK_CC_INLINE static void +ck_pflock_read_unlock(ck_pflock_t *pf) +{ + + ck_pr_fence_memory(); + ck_pr_faa_32(&pf->rout, CK_PFLOCK_RINC); + return; +} + +CK_CC_INLINE static void +ck_pflock_read_lock(ck_pflock_t *pf) +{ + uint32_t w; + + /* + * If no writer is present, then the operation has completed + * successfully. + */ + w = ck_pr_faa_32(&pf->rin, CK_PFLOCK_RINC) & CK_PFLOCK_WBITS; + if (w == 0) + return; + + /* Wait for current write phase to complete. */ + while ((ck_pr_load_32(&pf->rin) & CK_PFLOCK_WBITS) == w) + ck_pr_stall(); + + /* Acquire semantics. */ + ck_pr_fence_load(); + return; +} + +#endif /* _CK_PFLOCK_H */ + diff --git a/regressions/Makefile b/regressions/Makefile index ecd59f9..affeb83 100644 --- a/regressions/Makefile +++ b/regressions/Makefile @@ -47,6 +47,8 @@ all: $(MAKE) -C ./ck_ring/benchmark all $(MAKE) -C ./ck_rwlock/validate all $(MAKE) -C ./ck_rwlock/benchmark all + $(MAKE) -C ./ck_pflock/validate all + $(MAKE) -C ./ck_pflock/benchmark all $(MAKE) -C ./ck_hp/validate all $(MAKE) -C ./ck_hp/benchmark all $(MAKE) -C ./ck_bag/validate all @@ -79,6 +81,8 @@ clean: $(MAKE) -C ./ck_ring/benchmark clean $(MAKE) -C ./ck_rwlock/validate clean $(MAKE) -C ./ck_rwlock/benchmark clean + $(MAKE) -C ./ck_pflock/validate clean + $(MAKE) -C ./ck_pflock/benchmark clean $(MAKE) -C ./ck_hp/validate clean $(MAKE) -C ./ck_hp/benchmark clean $(MAKE) -C ./ck_bag/validate clean diff --git a/regressions/ck_pflock/benchmark/Makefile b/regressions/ck_pflock/benchmark/Makefile new file mode 100644 index 0000000..59c0b53 --- /dev/null +++ b/regressions/ck_pflock/benchmark/Makefile @@ -0,0 +1,17 @@ +.PHONY: clean distribution + +OBJECTS=latency throughput + +all: $(OBJECTS) + +latency: latency.c ../../../include/ck_rwlock.h + $(CC) $(CFLAGS) -o latency latency.c + +throughput: throughput.c ../../../include/ck_rwlock.h + $(CC) $(CFLAGS) -o throughput throughput.c + +clean: + rm -rf *.dSYM *~ *.o $(OBJECTS) + +include ../../../build/regressions.build +CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE diff --git a/regressions/ck_pflock/benchmark/latency.c b/regressions/ck_pflock/benchmark/latency.c new file mode 100644 index 0000000..c1feebd --- /dev/null +++ b/regressions/ck_pflock/benchmark/latency.c @@ -0,0 +1,72 @@ +/* + * Copyright 2011-2013 Samy Al Bahra. + * Copyright 2013 John Wittrock. + * 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 OTHEPFISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include + +#include "../../common.h" + +#ifndef STEPS +#define STEPS 1000000 +#endif + +int +main(void) +{ + uint64_t s_b, e_b, i; + ck_pflock_t pflock = CK_PFLOCK_INITIALIZER; + + for (i = 0; i < STEPS; i++) { + ck_pflock_write_lock(&pflock); + ck_pflock_write_unlock(&pflock); + } + + s_b = rdtsc(); + for (i = 0; i < STEPS; i++) { + ck_pflock_write_lock(&pflock); + ck_pflock_write_unlock(&pflock); + } + e_b = rdtsc(); + printf("WRITE: pflock %15" PRIu64 "\n", (e_b - s_b) / STEPS); + + for (i = 0; i < STEPS; i++) { + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + } + + s_b = rdtsc(); + for (i = 0; i < STEPS; i++) { + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + } + e_b = rdtsc(); + printf("READ: pflock %15" PRIu64 "\n", (e_b - s_b) / STEPS); + + return 0; +} + diff --git a/regressions/ck_pflock/benchmark/throughput.c b/regressions/ck_pflock/benchmark/throughput.c new file mode 100644 index 0000000..0911bbd --- /dev/null +++ b/regressions/ck_pflock/benchmark/throughput.c @@ -0,0 +1,163 @@ +/* + * Copyright 2011-2013 Samy Al Bahra. + * Copyright 2013 John Wittrock. + * 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 OTHEPFISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "../../common.h" + +#ifndef STEPS +#define STEPS 1000000 +#endif + +static int barrier; +static int threads; +static unsigned int flag CK_CC_CACHELINE; +static ck_pflock_t pflock = CK_PFLOCK_INITIALIZER; +static struct affinity affinity; + +static void * +thread_pflock(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_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + ck_pflock_read_lock(&pflock); + ck_pflock_read_unlock(&pflock); + 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; +} + +int +main(int argc, char *argv[]) +{ + int t; + pthread_t *p; + uint64_t *latency; + + if (argc != 3) { + ck_error("Usage: throughput \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"); + } + + affinity.delta = atoi(argv[1]); + affinity.request = 0; + + fprintf(stderr, "Creating threads (pflock)..."); + for (t = 0; t < threads; t++) { + if (pthread_create(&p[t], NULL, thread_pflock, 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]); + + return 0; +} + diff --git a/regressions/ck_pflock/validate/Makefile b/regressions/ck_pflock/validate/Makefile new file mode 100644 index 0000000..fd25fea --- /dev/null +++ b/regressions/ck_pflock/validate/Makefile @@ -0,0 +1,17 @@ +.PHONY: check clean distribution + +OBJECTS=validate + +all: $(OBJECTS) + +validate: validate.c ../../../include/ck_pflock.h + $(CC) $(CFLAGS) -o validate validate.c + +check: all + ./validate $(CORES) 1 + +clean: + rm -rf *.dSYM *~ *.o $(OBJECTS) + +include ../../../build/regressions.build +CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE diff --git a/regressions/ck_pflock/validate/validate.c b/regressions/ck_pflock/validate/validate.c new file mode 100644 index 0000000..d5e14ba --- /dev/null +++ b/regressions/ck_pflock/validate/validate.c @@ -0,0 +1,151 @@ +/* + * Copyright 2011-2013 Samy Al Bahra, John Wittrock. + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "../../common.h" + +#ifndef ITERATE +#define ITERATE 1000000 +#endif + +static struct affinity a; +static unsigned int locked; +static int nthr; +static ck_pflock_t lock = CK_PFLOCK_INITIALIZER; + +static void * +thread(void *null CK_CC_UNUSED) +{ + int i = ITERATE; + unsigned int l; + + if (aff_iterate(&a)) { + perror("ERROR: Could not affine thread"); + exit(EXIT_FAILURE); + } + + while (i--) { + ck_pflock_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_pflock_write_unlock(&lock); + + ck_pflock_read_lock(&lock); + { + l = ck_pr_load_uint(&locked); + if (l != 0) { + ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l); + } + } + ck_pflock_read_unlock(&lock); + } + + return NULL; +} + +int +main(int argc, char *argv[]) +{ + pthread_t *threads; + int i; + + if (argc != 3) { + ck_error("Usage: validate \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]); + + 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 %d\n", i); + } + } + 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; +} +