ck_pring
David Joseph 12 years ago
commit 60873b9af5

5
.gitignore vendored

@ -13,6 +13,11 @@ build/Makefile
*.a
*.so
*.dSYM
regressions/ck_cohort/benchmark/ck_cohort.LATENCY
regressions/ck_cohort/benchmark/ck_cohort.THROUGHPUT
regressions/ck_pflock/benchmark/latency
regressions/ck_pflock/benchmark/throughput
regressions/ck_pflock/validate/validate
regressions/ck_barrier/benchmark/throughput
regressions/ck_barrier/validate/barrier_centralized
regressions/ck_barrier/validate/barrier_combining

@ -0,0 +1,126 @@
/*
* Copyright 2013 Samy Al Bahra.
* Copyright 2013 Brendon Scheinman.
* 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.
*/
#ifndef _CK_COHORT_H
#define _CK_COHORT_H
/*
* This is an implementation of lock cohorts as described in:
* Dice, D.; Marathe, V.; and Shavit, N. 2012.
* Lock Cohorting: A General Technique for Designing NUMA Locks
*/
#include <ck_cc.h>
#include <ck_pr.h>
#include <stddef.h>
enum ck_cohort_state {
CK_COHORT_STATE_GLOBAL = 0,
CK_COHORT_STATE_LOCAL = 1
};
#define CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT 10
#define CK_COHORT_NAME(N) ck_cohort_##N
#define CK_COHORT_INSTANCE(N) struct CK_COHORT_NAME(N)
#define CK_COHORT_INIT(N, C, GL, LL, P) ck_cohort_##N##_init(C, GL, LL, P)
#define CK_COHORT_LOCK(N, C, GC, LC) ck_cohort_##N##_lock(C, GC, LC)
#define CK_COHORT_UNLOCK(N, C, GC, LC) ck_cohort_##N##_unlock(C, GC, LC)
#define CK_COHORT_PROTOTYPE(N, GT, GL, GU, LT, LL, LU) \
CK_COHORT_INSTANCE(N) { \
GT *global_lock; \
LT *local_lock; \
enum ck_cohort_state release_state; \
unsigned int waiting_threads; \
unsigned int acquire_count; \
unsigned int local_pass_limit; \
}; \
\
CK_CC_INLINE static void \
ck_cohort_##N##_init(struct ck_cohort_##N *cohort, \
GT *global_lock, LT *local_lock, unsigned int pass_limit) \
{ \
cohort->global_lock = global_lock; \
cohort->local_lock = local_lock; \
cohort->release_state = CK_COHORT_STATE_GLOBAL; \
cohort->waiting_threads = 0; \
cohort->acquire_count = 0; \
cohort->local_pass_limit = pass_limit; \
ck_pr_barrier(); \
return; \
} \
\
CK_CC_INLINE static void \
ck_cohort_##N##_lock(CK_COHORT_INSTANCE(N) *cohort, \
void *global_context, void *local_context) \
{ \
\
ck_pr_inc_uint(&cohort->waiting_threads); \
LL(cohort->local_lock, local_context); \
ck_pr_dec_uint(&cohort->waiting_threads); \
\
if (cohort->release_state == CK_COHORT_STATE_GLOBAL) { \
GL(cohort->global_lock, global_context); \
} \
\
++cohort->acquire_count; \
return; \
} \
\
CK_CC_INLINE static void \
ck_cohort_##N##_unlock(CK_COHORT_INSTANCE(N) *cohort, \
void *global_context, void *local_context) \
{ \
\
if (ck_pr_load_uint(&cohort->waiting_threads) > 0 \
&& cohort->acquire_count < cohort->local_pass_limit) { \
cohort->release_state = CK_COHORT_STATE_LOCAL; \
} else { \
GU(cohort->global_lock, global_context); \
cohort->release_state = CK_COHORT_STATE_GLOBAL; \
cohort->acquire_count = 0; \
} \
\
ck_pr_fence_memory(); \
LU(cohort->local_lock, local_context); \
\
return; \
}
#define CK_COHORT_INITIALIZER { \
.global_lock = NULL, \
.local_lock = NULL, \
.release_state = CK_COHORT_STATE_GLOBAL, \
.waiting_threads = 0, \
.acquire_count = 0, \
.local_pass_limit = CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT \
}
#endif /* _CK_COHORT_H */

@ -142,7 +142,6 @@ ck_fifo_spsc_dequeue(struct ck_fifo_spsc *fifo, void *value)
return (false);
/* If entry is visible, guarantee store to value is visible. */
ck_pr_fence_load_depends();
ck_pr_store_ptr(value, entry->value);
ck_pr_fence_store();
ck_pr_store_ptr(&fifo->head, entry);
@ -244,7 +243,6 @@ ck_fifo_mpmc_enqueue(struct ck_fifo_mpmc *fifo,
tail.generation = ck_pr_load_ptr(&fifo->tail.generation);
ck_pr_fence_load();
tail.pointer = ck_pr_load_ptr(&fifo->tail.pointer);
ck_pr_fence_load_depends();
next.generation = ck_pr_load_ptr(&tail.pointer->next.generation);
next.pointer = ck_pr_load_ptr(&tail.pointer->next.pointer);
@ -296,7 +294,6 @@ ck_fifo_mpmc_tryenqueue(struct ck_fifo_mpmc *fifo,
tail.generation = ck_pr_load_ptr(&fifo->tail.generation);
ck_pr_fence_load();
tail.pointer = ck_pr_load_ptr(&fifo->tail.pointer);
ck_pr_fence_load_depends();
next.generation = ck_pr_load_ptr(&tail.pointer->next.generation);
next.pointer = ck_pr_load_ptr(&tail.pointer->next.pointer);

@ -0,0 +1,142 @@
/*
* 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 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.
*/
#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 <ck_cc.h>
#include <ck_pr.h>
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_load();
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)
goto leave;
/* Wait for current write phase to complete. */
while ((ck_pr_load_32(&pf->rin) & CK_PFLOCK_WBITS) == w)
ck_pr_stall();
leave:
/* Acquire semantics. */
ck_pr_fence_load();
return;
}
#endif /* _CK_PFLOCK_H */

@ -405,6 +405,141 @@ ck_spinlock_dec_unlock(struct ck_spinlock_dec *lock)
#ifndef CK_F_SPINLOCK_TICKET
#define CK_F_SPINLOCK_TICKET
/*
* If 16-bit or 32-bit increment is supported, implement support for
* trylock functionality on availability of 32-bit or 64-bit fetch-and-add
* and compare-and-swap.
*/
#if defined(CK_MD_TSO)
#if defined(CK_F_PR_FAA_32) && defined(CK_F_PR_INC_16) && defined(CK_F_PR_CAS_32)
#define CK_SPINLOCK_TICKET_TYPE uint32_t
#define CK_SPINLOCK_TICKET_TYPE_BASE uint16_t
#define CK_SPINLOCK_TICKET_INC(x) ck_pr_inc_16(x)
#define CK_SPINLOCK_TICKET_CAS(x, y, z) ck_pr_cas_32(x, y, z)
#define CK_SPINLOCK_TICKET_FAA(x, y) ck_pr_faa_32(x, y)
#define CK_SPINLOCK_TICKET_LOAD(x) ck_pr_load_32(x)
#define CK_SPINLOCK_TICKET_INCREMENT (0x00010000UL)
#define CK_SPINLOCK_TICKET_MASK (0xFFFFUL)
#define CK_SPINLOCK_TICKET_SHIFT (16)
#elif defined(CK_F_PR_FAA_64) && defined(CK_F_PR_INC_32) && defined(CK_F_PR_CAS_64)
#define CK_SPINLOCK_TICKET_TYPE uint64_t
#define CK_SPINLOCK_TICKET_TYPE_BASE uint32_t
#define CK_SPINLOCK_TICKET_INC(x) ck_pr_inc_32(x)
#define CK_SPINLOCK_TICKET_CAS(x, y, z) ck_pr_cas_64(x, y, z)
#define CK_SPINLOCK_TICKET_FAA(x, y) ck_pr_faa_64(x, y)
#define CK_SPINLOCK_TICKET_LOAD(x) ck_pr_load_64(x)
#define CK_SPINLOCK_TICKET_INCREMENT (0x0000000100000000ULL)
#define CK_SPINLOCK_TICKET_MASK (0xFFFFFFFFULL)
#define CK_SPINLOCK_TICKET_SHIFT (32)
#endif
#endif /* CK_MD_TSO */
#if defined(CK_SPINLOCK_TICKET_TYPE)
#define CK_F_SPINLOCK_TICKET_TRYLOCK
struct ck_spinlock_ticket {
CK_SPINLOCK_TICKET_TYPE value;
};
typedef struct ck_spinlock_ticket ck_spinlock_ticket_t;
#define CK_SPINLOCK_TICKET_INITIALIZER { .value = 0 }
CK_CC_INLINE static void
ck_spinlock_ticket_init(struct ck_spinlock_ticket *ticket)
{
ticket->value = 0;
ck_pr_fence_store();
return;
}
CK_CC_INLINE static void
ck_spinlock_ticket_lock(struct ck_spinlock_ticket *ticket)
{
CK_SPINLOCK_TICKET_TYPE request, position;
/* Get our ticket number and set next ticket number. */
request = CK_SPINLOCK_TICKET_FAA(&ticket->value,
CK_SPINLOCK_TICKET_INCREMENT);
position = request & CK_SPINLOCK_TICKET_MASK;
request >>= CK_SPINLOCK_TICKET_SHIFT;
while (request != position) {
ck_pr_stall();
position = CK_SPINLOCK_TICKET_LOAD(&ticket->value) &
CK_SPINLOCK_TICKET_MASK;
}
ck_pr_fence_memory();
return;
}
CK_CC_INLINE static void
ck_spinlock_ticket_lock_pb(struct ck_spinlock_ticket *ticket, unsigned int c)
{
CK_SPINLOCK_TICKET_TYPE request, position;
ck_backoff_t backoff;
/* Get our ticket number and set next ticket number. */
request = CK_SPINLOCK_TICKET_FAA(&ticket->value,
CK_SPINLOCK_TICKET_INCREMENT);
position = request & CK_SPINLOCK_TICKET_MASK;
request >>= CK_SPINLOCK_TICKET_SHIFT;
while (request != position) {
ck_pr_stall();
position = CK_SPINLOCK_TICKET_LOAD(&ticket->value) &
CK_SPINLOCK_TICKET_MASK;
backoff = request - position;
backoff <<= c;
ck_backoff_eb(&backoff);
}
ck_pr_fence_memory();
return;
}
CK_CC_INLINE static bool
ck_spinlock_ticket_trylock(struct ck_spinlock_ticket *ticket)
{
CK_SPINLOCK_TICKET_TYPE snapshot, request, position;
snapshot = CK_SPINLOCK_TICKET_LOAD(&ticket->value);
position = snapshot & CK_SPINLOCK_TICKET_MASK;
request = snapshot >> CK_SPINLOCK_TICKET_SHIFT;
if (position != request)
return false;
if (CK_SPINLOCK_TICKET_CAS(&ticket->value,
snapshot, snapshot + CK_SPINLOCK_TICKET_INCREMENT) == false) {
return false;
}
ck_pr_fence_memory();
return true;
}
CK_CC_INLINE static void
ck_spinlock_ticket_unlock(struct ck_spinlock_ticket *ticket)
{
ck_pr_fence_memory();
CK_SPINLOCK_TICKET_INC((CK_SPINLOCK_TICKET_TYPE_BASE *)&ticket->value);
return;
}
#undef CK_SPINLOCK_TICKET_TYPE
#undef CK_SPINLOCK_TICKET_TYPE_BASE
#undef CK_SPINLOCK_TICKET_INC
#undef CK_SPINLOCK_TICKET_FAA
#undef CK_SPINLOCK_TICKET_LOAD
#undef CK_SPINLOCK_TICKET_INCREMENT
#undef CK_SPINLOCK_TICKET_MASK
#undef CK_SPINLOCK_TICKET_SHIFT
#else
/*
* MESI benefits from cacheline padding between next and current. This avoids
* invalidation of current from the cache due to incoming lock requests.
@ -449,7 +584,7 @@ ck_spinlock_ticket_lock(struct ck_spinlock_ticket *ticket)
}
CK_CC_INLINE static void
ck_spinlock_ticket_lock_pb(struct ck_spinlock_ticket *ticket)
ck_spinlock_ticket_lock_pb(struct ck_spinlock_ticket *ticket, unsigned int c)
{
ck_backoff_t backoff;
unsigned int request, position;
@ -463,7 +598,7 @@ ck_spinlock_ticket_lock_pb(struct ck_spinlock_ticket *ticket)
/* Overflow is handled fine, assuming 2s complement. */
backoff = (request - position);
backoff *= 64;
backoff <<= c;
/*
* Ideally, back-off from generating cache traffic for at least
@ -494,6 +629,7 @@ ck_spinlock_ticket_unlock(struct ck_spinlock_ticket *ticket)
ck_pr_store_uint(&ticket->position, update + 1);
return;
}
#endif /* !CK_F_SPINLOCK_TICKET_TRYLOCK */
#endif /* CK_F_SPINLOCK_TICKET */
#ifndef CK_F_SPINLOCK_MCS

@ -99,6 +99,8 @@ ck_pr_stall(void)
CK_CC_INLINE static void
ck_pr_fence_load_depends(void)
{
__sync_synchronize();
return;
}

@ -4,11 +4,13 @@ DIR=backoff \
bitmap \
brlock \
bytelock \
cohort \
epoch \
fifo \
hp \
hs \
ht \
pflock \
pr \
queue \
ring \
@ -20,6 +22,8 @@ DIR=backoff \
.PHONY: all clean check
all:
$(MAKE) -C ./ck_cohort/validate all
$(MAKE) -C ./ck_cohort/benchmark all
$(MAKE) -C ./ck_bitmap/validate all
$(MAKE) -C ./ck_backoff/validate all
$(MAKE) -C ./ck_queue/validate all
@ -47,14 +51,20 @@ 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
clean:
$(MAKE) -C ./ck_pflock/validate clean
$(MAKE) -C ./ck_pflock/benchmark clean
$(MAKE) -C ./ck_backoff/validate clean
$(MAKE) -C ./ck_bitmap/validate clean
$(MAKE) -C ./ck_queue/validate clean
$(MAKE) -C ./ck_cohort/validate clean
$(MAKE) -C ./ck_cohort/benchmark clean
$(MAKE) -C ./ck_brlock/validate clean
$(MAKE) -C ./ck_ht/validate clean
$(MAKE) -C ./ck_ht/benchmark clean
@ -79,6 +89,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

@ -0,0 +1,17 @@
.PHONY: all clean
OBJECTS=ck_cohort.THROUGHPUT ck_cohort.LATENCY
all: $(OBJECTS)
ck_cohort.THROUGHPUT: ck_cohort.c
$(CC) $(CFLAGS) -o ck_cohort.THROUGHPUT throughput.c -lm
ck_cohort.LATENCY: ck_cohort.c
$(CC) -DLATENCY $(CFLAGS) -o ck_cohort.LATENCY ck_cohort.c
clean:
rm -rf *.dSYM $(OBJECTS)
include ../../../build/regressions.build
CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE -lm

@ -0,0 +1,8 @@
#include "../ck_cohort.h"
#include <ck_cohort.h>
#ifdef THROUGHPUT
#include "../../ck_spinlock/benchmark/throughput.h"
#elif defined(LATENCY)
#include "../../ck_spinlock/benchmark/latency.h"
#endif

@ -0,0 +1,231 @@
/*
* Copyright 2013 Samy Al Bahra.
* Copyright 2013 Brendon Scheinman.
* 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 <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/time.h>
#include <ck_pr.h>
#include <ck_cohort.h>
#include <ck_md.h>
#include <ck_spinlock.h>
#include "../../common.h"
#define max(x, y) (((x) > (y)) ? (x) : (y))
static struct affinity a;
static unsigned int ready;
struct counters {
uint64_t value;
} CK_CC_CACHELINE;
static struct counters *count;
static uint64_t nthr;
static unsigned int n_cohorts;
static unsigned int barrier;
static int critical CK_CC_CACHELINE;
static void
ck_spinlock_lock_with_context(ck_spinlock_t *lock, void *context)
{
(void)context;
ck_spinlock_lock(lock);
return;
}
static void
ck_spinlock_unlock_with_context(ck_spinlock_t *lock, void *context)
{
(void)context;
ck_spinlock_unlock(lock);
return;
}
CK_COHORT_PROTOTYPE(basic,
ck_spinlock_t, ck_spinlock_lock_with_context, ck_spinlock_unlock_with_context,
ck_spinlock_t, ck_spinlock_lock_with_context, ck_spinlock_unlock_with_context)
struct cohort_record {
CK_COHORT_INSTANCE(basic) cohort;
} CK_CC_CACHELINE;
static struct cohort_record *cohorts;
static ck_spinlock_t global_lock = CK_SPINLOCK_INITIALIZER;
struct block {
unsigned int tid;
};
static void *
fairness(void *null)
{
struct block *context = null;
unsigned int i = context->tid;
volatile int j;
long int base;
unsigned int core;
CK_COHORT_INSTANCE(basic) *cohort;
if (aff_iterate_core(&a, &core)) {
perror("ERROR: Could not affine thread");
exit(EXIT_FAILURE);
}
cohort = &((cohorts + (core / (int)(a.delta)) % n_cohorts)->cohort);
while (ck_pr_load_uint(&ready) == 0);
ck_pr_inc_uint(&barrier);
while (ck_pr_load_uint(&barrier) != nthr);
while (ready) {
CK_COHORT_LOCK(basic, cohort, NULL, NULL);
count[i].value++;
if (critical) {
base = common_lrand48() % critical;
for (j = 0; j < base; j++);
}
CK_COHORT_UNLOCK(basic, cohort, NULL, NULL);
}
return NULL;
}
int
main(int argc, char *argv[])
{
uint64_t v, d;
unsigned int i;
pthread_t *threads;
struct block *context;
ck_spinlock_t *local_lock;
if (argc != 5) {
ck_error("Usage: ck_cohort <number of cohorts> <threads per cohort> "
"<affinity delta> <critical section>\n");
}
n_cohorts = atoi(argv[1]);
if (n_cohorts <= 0) {
ck_error("ERROR: Number of cohorts must be greater than 0\n");
}
nthr = n_cohorts * atoi(argv[2]);
if (nthr <= 0) {
ck_error("ERROR: Number of threads must be greater than 0\n");
}
critical = atoi(argv[4]);
if (critical < 0) {
ck_error("ERROR: critical section cannot be negative\n");
}
threads = malloc(sizeof(pthread_t) * nthr);
if (threads == NULL) {
ck_error("ERROR: Could not allocate thread structures\n");
}
cohorts = malloc(sizeof(struct cohort_record) * n_cohorts);
if (cohorts == NULL) {
ck_error("ERROR: Could not allocate cohort structures\n");
}
context = malloc(sizeof(struct block) * nthr);
if (context == NULL) {
ck_error("ERROR: Could not allocate thread contexts\n");
}
a.delta = atoi(argv[2]);
a.request = 0;
count = malloc(sizeof(*count) * nthr);
if (count == NULL) {
ck_error("ERROR: Could not create acquisition buffer\n");
}
memset(count, 0, sizeof(*count) * nthr);
fprintf(stderr, "Creating cohorts...");
for (i = 0 ; i < n_cohorts ; i++) {
local_lock = malloc(max(CK_MD_CACHELINE, sizeof(ck_spinlock_t)));
if (local_lock == NULL) {
ck_error("ERROR: Could not allocate local lock\n");
}
CK_COHORT_INIT(basic, &((cohorts + i)->cohort), &global_lock, local_lock,
CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT);
local_lock = NULL;
}
fprintf(stderr, "done\n");
fprintf(stderr, "Creating threads (fairness)...");
for (i = 0; i < nthr; i++) {
context[i].tid = i;
if (pthread_create(&threads[i], NULL, fairness, context + i)) {
ck_error("ERROR: Could not create thread %d\n", i);
}
}
fprintf(stderr, "done\n");
ck_pr_store_uint(&ready, 1);
common_sleep(10);
ck_pr_store_uint(&ready, 0);
fprintf(stderr, "Waiting for threads to finish acquisition regression...");
for (i = 0; i < nthr; i++)
pthread_join(threads[i], NULL);
fprintf(stderr, "done\n\n");
for (i = 0, v = 0; i < nthr; i++) {
printf("%d %15" PRIu64 "\n", i, count[i].value);
v += count[i].value;
}
printf("\n# total : %15" PRIu64 "\n", v);
printf("# throughput : %15" PRIu64 " a/s\n", (v /= nthr) / 10);
for (i = 0, d = 0; i < nthr; i++)
d += (count[i].value - v) * (count[i].value - v);
printf("# average : %15" PRIu64 "\n", v);
printf("# deviation : %.2f (%.2f%%)\n\n", sqrt(d / nthr), (sqrt(d / nthr) / v) * 100.00);
return 0;
}

@ -0,0 +1,25 @@
#define LOCK_NAME "ck_cohort"
#define LOCK_DEFINE\
static ck_spinlock_fas_t global_fas_lock = CK_SPINLOCK_FAS_INITIALIZER;\
static ck_spinlock_fas_t local_fas_lock = CK_SPINLOCK_FAS_INITIALIZER;\
static void\
ck_spinlock_fas_lock_with_context(ck_spinlock_fas_t *lock, void *context)\
{\
(void)context;\
ck_spinlock_fas_lock(lock);\
}\
\
static void\
ck_spinlock_fas_unlock_with_context(ck_spinlock_fas_t *lock, void *context)\
{\
(void)context;\
ck_spinlock_fas_unlock(lock);\
}\
CK_COHORT_PROTOTYPE(fas_fas,\
ck_spinlock_fas_t, ck_spinlock_fas_lock_with_context, ck_spinlock_fas_unlock_with_context,\
ck_spinlock_fas_t, ck_spinlock_fas_lock_with_context, ck_spinlock_fas_unlock_with_context)\
static CK_COHORT_INSTANCE(fas_fas) CK_CC_CACHELINE cohort = CK_COHORT_INITIALIZER
#define LOCK_INIT CK_COHORT_INIT(fas_fas, &cohort, &global_fas_lock, &local_fas_lock,\
CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT)
#define LOCK CK_COHORT_LOCK(fas_fas, &cohort, NULL, NULL)
#define UNLOCK CK_COHORT_UNLOCK(fas_fas, &cohort, NULL, NULL)

@ -0,0 +1,17 @@
.PHONY: check clean distribution
OBJECTS=validate
all: $(OBJECTS)
validate: validate.c ../../../include/ck_cohort.h
$(CC) $(CFLAGS) -o validate validate.c
check: all
./validate `expr $(CORES) / 2` 2 1
clean:
rm -rf *.dSYM *~ *.o $(OBJECTS)
include ../../../build/regressions.build
CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE

@ -0,0 +1,180 @@
/*
* Copyright 2013 Samy Al Bahra.
* Copyright 2013 Brendon Scheinman.
* 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 <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <ck_pr.h>
#include <ck_cohort.h>
#include <ck_spinlock.h>
#include "../../common.h"
#ifndef ITERATE
#define ITERATE 1000000
#endif
static struct affinity a;
static unsigned int locked;
static int nthr;
static ck_spinlock_fas_t global_fas_lock = CK_SPINLOCK_FAS_INITIALIZER;
static void
ck_spinlock_fas_lock_with_context(ck_spinlock_fas_t *lock, void *context)
{
(void)context;
ck_spinlock_fas_lock(lock);
}
static void
ck_spinlock_fas_unlock_with_context(ck_spinlock_fas_t *lock, void *context)
{
(void)context;
ck_spinlock_fas_unlock(lock);
}
CK_COHORT_PROTOTYPE(fas_fas,
ck_spinlock_fas_t, ck_spinlock_fas_lock_with_context, ck_spinlock_fas_unlock_with_context,
ck_spinlock_fas_t, ck_spinlock_fas_lock_with_context, ck_spinlock_fas_unlock_with_context)
static CK_COHORT_INSTANCE(fas_fas) *cohorts;
static int n_cohorts;
static void *
thread(void *null CK_CC_UNUSED)
{
int i = ITERATE;
unsigned int l;
unsigned int core;
CK_COHORT_INSTANCE(fas_fas) *cohort;
if (aff_iterate_core(&a, &core)) {
perror("ERROR: Could not affine thread");
exit(EXIT_FAILURE);
}
cohort = cohorts + (core / (int)(a.delta)) % n_cohorts;
while (i--) {
CK_COHORT_LOCK(fas_fas, cohort, NULL, NULL);
{
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_COHORT_UNLOCK(fas_fas, cohort, NULL, NULL);
}
return (NULL);
}
int
main(int argc, char *argv[])
{
pthread_t *threads;
int threads_per_cohort;
ck_spinlock_fas_t *local_lock;
int i;
if (argc != 4) {
ck_error("Usage: validate <number of cohorts> <threads per cohort> <affinity delta>\n");
}
n_cohorts = atoi(argv[1]);
if (n_cohorts <= 0) {
ck_error("ERROR: Number of cohorts must be greater than 0\n");
}
threads_per_cohort = atoi(argv[2]);
if (threads_per_cohort <= 0) {
ck_error("ERROR: Threads per cohort must be greater than 0\n");
}
nthr = n_cohorts * threads_per_cohort;
threads = malloc(sizeof(pthread_t) * nthr);
if (threads == NULL) {
ck_error("ERROR: Could not allocate thread structures\n");
}
a.delta = atoi(argv[3]);
fprintf(stderr, "Creating cohorts...");
cohorts = malloc(sizeof(CK_COHORT_INSTANCE(fas_fas)) * n_cohorts);
for (i = 0 ; i < n_cohorts ; i++) {
local_lock = malloc(sizeof(ck_spinlock_fas_t));
CK_COHORT_INIT(fas_fas, cohorts + i, &global_fas_lock, local_lock,
CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT);
}
fprintf(stderr, "done\n");
fprintf(stderr, "Creating threads...");
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);
}

@ -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

@ -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 <ck_pflock.h>
#include <inttypes.h>
#include <stdio.h>
#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;
}

@ -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 <ck_pflock.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 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 <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");
}
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;
}

@ -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

@ -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 <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_pflock.h>
#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 <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]);
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;
}

@ -1,5 +1,10 @@
#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

@ -1,5 +1,5 @@
#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)
#define LOCK ck_spinlock_ticket_lock_pb(&lock, 5)
#define UNLOCK ck_spinlock_ticket_unlock(&lock)

@ -70,21 +70,40 @@ thread(void *null CK_CC_UNUSED)
}
while (i--) {
#ifdef TRYLOCK
if (i & 1) {
LOCK;
} else {
while (TRYLOCK == false)
ck_pr_stall();
}
#else
LOCK;
#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 != 5) {
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);
@ -92,6 +111,7 @@ thread(void *null CK_CC_UNUSED)
ck_pr_dec_uint(&locked);
UNLOCK;
LOCK;
j = ck_pr_load_uint(&locked);
@ -99,6 +119,7 @@ thread(void *null CK_CC_UNUSED)
ck_error("ERROR (RD): Race condition (%u)\n", j);
exit(EXIT_FAILURE);
}
UNLOCK;
}

@ -236,6 +236,18 @@ aff_iterate(struct affinity *acb)
return sched_setaffinity(gettid(), sizeof(s), &s);
}
CK_CC_UNUSED static int
aff_iterate_core(struct affinity *acb, unsigned int *core)
{
cpu_set_t s;
*core = ck_pr_faa_uint(&acb->request, acb->delta);
CPU_ZERO(&s);
CPU_SET((*core) % CORES, &s);
return sched_setaffinity(gettid(), sizeof(s), &s);
}
#elif defined(__MACH__)
CK_CC_UNUSED static int
aff_iterate(struct affinity *acb)
@ -250,6 +262,19 @@ aff_iterate(struct affinity *acb)
(thread_policy_t)&policy,
THREAD_AFFINITY_POLICY_COUNT);
}
CK_CC_UNUSED static int
aff_iterate_core(struct affinity *acb, unsigned int *core)
{
thread_affinity_policy_data_t policy;
*core = ck_pr_faa_uint(&acb->request, acb->delta) % CORES;
policy.affinity_tag = *core;
return thread_policy_set(mach_thread_self(),
THREAD_AFFINITY_POLICY,
(thread_policy_t)&policy,
THREAD_AFFINITY_POLICY_COUNT);
}
#else
CK_CC_UNUSED static int
aff_iterate(struct affinity *acb CK_CC_UNUSED)
@ -257,6 +282,13 @@ aff_iterate(struct affinity *acb CK_CC_UNUSED)
return (0);
}
CK_CC_UNUSED static int
aff_iterate_core(struct affinity *acb CK_CC_UNUSED, unsigned int *core)
{
*core = 0;
return (0);
}
#endif
CK_CC_INLINE static uint64_t

Loading…
Cancel
Save