901 lines
32 KiB
C
901 lines
32 KiB
C
#include "epoch_cache.h"
|
|
#include <errno.h>
|
|
#include <inttypes.h>
|
|
#include <pthread.h>
|
|
#include <sched.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
|
|
static const char *marker;
|
|
static pthread_mutex_t audit_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
static unsigned long read_locks;
|
|
static unsigned long write_locks;
|
|
static unsigned long unlocks;
|
|
static unsigned audit_errors;
|
|
static unsigned next_thread;
|
|
static _Thread_local unsigned local_thread;
|
|
struct observed_lock {
|
|
unsigned thread;
|
|
uintptr_t address;
|
|
unsigned readers;
|
|
unsigned writers;
|
|
};
|
|
static struct observed_lock *lock_scopes;
|
|
static unsigned scope_capacity;
|
|
static unsigned scope_count;
|
|
|
|
static void note_lock(pthread_rwlock_t *lock, int mode)
|
|
{
|
|
pthread_mutex_lock(&audit_mutex);
|
|
if (local_thread == 0) {
|
|
local_thread = ++next_thread;
|
|
}
|
|
unsigned i;
|
|
for (i = 0; i < scope_count; ++i) {
|
|
if (lock_scopes[i].thread == local_thread &&
|
|
lock_scopes[i].address == (uintptr_t)lock) {
|
|
break;
|
|
}
|
|
}
|
|
if (i == scope_count) {
|
|
if (scope_count == scope_capacity) {
|
|
unsigned capacity = scope_capacity ? scope_capacity * 2 : 16;
|
|
struct observed_lock *more = realloc(lock_scopes,
|
|
capacity * sizeof *more);
|
|
if (more == NULL) {
|
|
audit_errors++;
|
|
pthread_mutex_unlock(&audit_mutex);
|
|
return;
|
|
}
|
|
lock_scopes = more;
|
|
scope_capacity = capacity;
|
|
}
|
|
memset(&lock_scopes[i], 0, sizeof lock_scopes[i]);
|
|
lock_scopes[i].thread = local_thread;
|
|
lock_scopes[i].address = (uintptr_t)lock;
|
|
scope_count++;
|
|
}
|
|
if (mode == 1) {
|
|
lock_scopes[i].readers++;
|
|
read_locks++;
|
|
} else if (mode == 2) {
|
|
lock_scopes[i].writers++;
|
|
write_locks++;
|
|
} else {
|
|
unlocks++;
|
|
if (lock_scopes[i].writers != 0) {
|
|
lock_scopes[i].writers--;
|
|
} else if (lock_scopes[i].readers != 0) {
|
|
lock_scopes[i].readers--;
|
|
} else {
|
|
audit_errors++;
|
|
}
|
|
}
|
|
pthread_mutex_unlock(&audit_mutex);
|
|
}
|
|
|
|
int __real_pthread_rwlock_rdlock(pthread_rwlock_t *lock);
|
|
int __wrap_pthread_rwlock_rdlock(pthread_rwlock_t *lock)
|
|
{
|
|
int result = __real_pthread_rwlock_rdlock(lock);
|
|
if (result == 0) {
|
|
note_lock(lock, 1);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int __real_pthread_rwlock_wrlock(pthread_rwlock_t *lock);
|
|
int __wrap_pthread_rwlock_wrlock(pthread_rwlock_t *lock)
|
|
{
|
|
int result = __real_pthread_rwlock_wrlock(lock);
|
|
if (result == 0) {
|
|
note_lock(lock, 2);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int __real_pthread_rwlock_tryrdlock(pthread_rwlock_t *lock);
|
|
int __wrap_pthread_rwlock_tryrdlock(pthread_rwlock_t *lock)
|
|
{
|
|
int result = __real_pthread_rwlock_tryrdlock(lock);
|
|
if (result == 0) {
|
|
note_lock(lock, 1);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int __real_pthread_rwlock_trywrlock(pthread_rwlock_t *lock);
|
|
int __wrap_pthread_rwlock_trywrlock(pthread_rwlock_t *lock)
|
|
{
|
|
int result = __real_pthread_rwlock_trywrlock(lock);
|
|
if (result == 0) {
|
|
note_lock(lock, 2);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int __real_pthread_rwlock_timedrdlock(pthread_rwlock_t *lock, const struct timespec *deadline);
|
|
int __wrap_pthread_rwlock_timedrdlock(pthread_rwlock_t *lock, const struct timespec *deadline)
|
|
{
|
|
int result = __real_pthread_rwlock_timedrdlock(lock, deadline);
|
|
if (result == 0) {
|
|
note_lock(lock, 1);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int __real_pthread_rwlock_timedwrlock(pthread_rwlock_t *lock, const struct timespec *deadline);
|
|
int __wrap_pthread_rwlock_timedwrlock(pthread_rwlock_t *lock, const struct timespec *deadline)
|
|
{
|
|
int result = __real_pthread_rwlock_timedwrlock(lock, deadline);
|
|
if (result == 0) {
|
|
note_lock(lock, 2);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int __real_pthread_rwlock_unlock(pthread_rwlock_t *lock);
|
|
int __wrap_pthread_rwlock_unlock(pthread_rwlock_t *lock)
|
|
{
|
|
int result = __real_pthread_rwlock_unlock(lock);
|
|
if (result == 0) {
|
|
note_lock(lock, 0);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static void counters(void)
|
|
{
|
|
pthread_mutex_lock(&audit_mutex);
|
|
unsigned active = 0;
|
|
for (unsigned i = 0; i < scope_count; ++i) {
|
|
if (lock_scopes[i].readers || lock_scopes[i].writers) {
|
|
active++;
|
|
}
|
|
}
|
|
printf(" @SCOPE %u %u", audit_errors, active);
|
|
for (unsigned i = 0; i < scope_count; ++i) {
|
|
struct observed_lock *scope = &lock_scopes[i];
|
|
if (scope->readers || scope->writers) {
|
|
printf(" %u %" PRIuPTR " %u %u", scope->thread,
|
|
scope->address, scope->readers, scope->writers);
|
|
}
|
|
}
|
|
printf(" @COUNTS %lu %lu %lu\n", read_locks, write_locks, unlocks);
|
|
pthread_mutex_unlock(&audit_mutex);
|
|
fflush(stdout);
|
|
}
|
|
|
|
static void state_fields(struct epoch_state state)
|
|
{
|
|
printf(" %" PRIu64 " %" PRIu64 " %" PRIu64 " %u %u %u %d %d",
|
|
state.epoch, state.next_ticket, state.serving_ticket,
|
|
state.registered, state.active_readers, state.queued_writers,
|
|
state.writer_active, state.closed);
|
|
}
|
|
|
|
static void sample_fields(struct epoch_sample sample)
|
|
{
|
|
printf(" %" PRIu64 " %d %" PRIu64 " %" PRId64 " %" PRIu64,
|
|
sample.snapshot_epoch, sample.record.present,
|
|
sample.record.version, sample.record.value, sample.record.tag);
|
|
}
|
|
|
|
static struct epoch_reader changed_reader(struct epoch_reader reader,
|
|
int field, uint64_t value)
|
|
{
|
|
if (field == 1) {
|
|
reader.owner = value;
|
|
}
|
|
if (field == 2) {
|
|
reader.generation = value;
|
|
}
|
|
if (field == 3) {
|
|
reader.slot = (unsigned)value;
|
|
}
|
|
return reader;
|
|
}
|
|
|
|
static struct epoch_lease changed_lease(struct epoch_lease lease,
|
|
int field, uint64_t value)
|
|
{
|
|
if (field == 1) {
|
|
lease.owner = value;
|
|
}
|
|
if (field == 2) {
|
|
lease.generation = value;
|
|
}
|
|
if (field == 3) {
|
|
lease.serial = value;
|
|
}
|
|
if (field == 4) {
|
|
lease.epoch = value;
|
|
}
|
|
if (field == 5) {
|
|
lease.slot = (unsigned)value;
|
|
}
|
|
return lease;
|
|
}
|
|
|
|
static struct epoch_tx changed_tx(struct epoch_tx tx,
|
|
int field, uint64_t value)
|
|
{
|
|
if (field == 1) {
|
|
tx.owner = value;
|
|
}
|
|
if (field == 2) {
|
|
tx.ticket = value;
|
|
}
|
|
return tx;
|
|
}
|
|
|
|
struct gate {
|
|
pthread_mutex_t mutex;
|
|
pthread_cond_t changed;
|
|
int acquired[10];
|
|
int released[10];
|
|
unsigned order_count;
|
|
int order[32];
|
|
};
|
|
|
|
struct worker {
|
|
struct epoch_cache *cache;
|
|
struct gate *gate;
|
|
struct epoch_reader reader;
|
|
struct epoch_lease foreign_lease;
|
|
struct epoch_tx foreign_tx;
|
|
struct epoch_lease lease;
|
|
struct epoch_tx tx;
|
|
struct epoch_sample samples[128][2];
|
|
struct epoch_reader_info status;
|
|
int begin_rc;
|
|
int first_rc;
|
|
int second_rc;
|
|
int end_rc;
|
|
int role;
|
|
int index;
|
|
int loops;
|
|
int64_t value;
|
|
uint64_t tag;
|
|
uint64_t serials[128];
|
|
int results[128][4];
|
|
};
|
|
|
|
static void gate_init(struct gate *gate)
|
|
{
|
|
memset(gate, 0, sizeof *gate);
|
|
pthread_mutex_init(&gate->mutex, NULL);
|
|
pthread_cond_init(&gate->changed, NULL);
|
|
}
|
|
|
|
static void gate_destroy(struct gate *gate)
|
|
{
|
|
pthread_cond_destroy(&gate->changed);
|
|
pthread_mutex_destroy(&gate->mutex);
|
|
}
|
|
|
|
static void acquired(struct worker *worker)
|
|
{
|
|
struct gate *gate = worker->gate;
|
|
pthread_mutex_lock(&gate->mutex);
|
|
gate->acquired[worker->index] = 1;
|
|
if (gate->order_count < 32) {
|
|
gate->order[gate->order_count++] = worker->index;
|
|
}
|
|
pthread_cond_broadcast(&gate->changed);
|
|
while (!gate->released[worker->index]) {
|
|
pthread_cond_wait(&gate->changed, &gate->mutex);
|
|
}
|
|
pthread_mutex_unlock(&gate->mutex);
|
|
}
|
|
|
|
static int wait_acquired(struct gate *gate, int index)
|
|
{
|
|
struct timespec deadline;
|
|
clock_gettime(CLOCK_REALTIME, &deadline);
|
|
deadline.tv_sec += 5;
|
|
pthread_mutex_lock(&gate->mutex);
|
|
int error = 0;
|
|
while (!gate->acquired[index] && error == 0) {
|
|
error = pthread_cond_timedwait(&gate->changed, &gate->mutex, &deadline);
|
|
}
|
|
pthread_mutex_unlock(&gate->mutex);
|
|
return error;
|
|
}
|
|
|
|
static void release_worker(struct gate *gate, int index)
|
|
{
|
|
pthread_mutex_lock(&gate->mutex);
|
|
gate->released[index] = 1;
|
|
pthread_cond_broadcast(&gate->changed);
|
|
pthread_mutex_unlock(&gate->mutex);
|
|
}
|
|
|
|
static int poll_state(struct epoch_cache *cache, unsigned waiting,
|
|
unsigned readers, int active)
|
|
{
|
|
struct timespec start;
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
for (;;) {
|
|
struct epoch_state state;
|
|
memset(&state, 0, sizeof state);
|
|
int result = epoch_inspect(cache, &state);
|
|
if (result != 0) {
|
|
return result;
|
|
}
|
|
if (state.queued_writers == waiting &&
|
|
state.active_readers == readers && state.writer_active == active) {
|
|
return 0;
|
|
}
|
|
struct timespec now;
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
if (now.tv_sec - start.tv_sec >= 5) {
|
|
return ETIMEDOUT;
|
|
}
|
|
sched_yield();
|
|
}
|
|
}
|
|
|
|
static int poll_busy(struct epoch_cache *cache, struct epoch_reader reader)
|
|
{
|
|
struct timespec start;
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
for (;;) {
|
|
struct epoch_reader_info status;
|
|
memset(&status, 0, sizeof status);
|
|
int result = epoch_reader_status(cache, reader, &status);
|
|
if (result != 0) {
|
|
return result;
|
|
}
|
|
if (status.busy) {
|
|
return 0;
|
|
}
|
|
struct timespec now;
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
if (now.tv_sec - start.tv_sec >= 5) {
|
|
return ETIMEDOUT;
|
|
}
|
|
sched_yield();
|
|
}
|
|
}
|
|
|
|
static void *held_writer(void *argument)
|
|
{
|
|
struct worker *worker = argument;
|
|
worker->begin_rc = epoch_write_begin(worker->cache, &worker->tx);
|
|
acquired(worker);
|
|
worker->first_rc = epoch_write_put(worker->cache, worker->tx, 0,
|
|
worker->value, worker->tag);
|
|
worker->second_rc = epoch_write_put(worker->cache, worker->tx, 1,
|
|
-worker->value, ~worker->tag);
|
|
worker->end_rc = epoch_write_commit(worker->cache, worker->tx);
|
|
return NULL;
|
|
}
|
|
|
|
static void *held_reader(void *argument)
|
|
{
|
|
struct worker *worker = argument;
|
|
worker->begin_rc = epoch_read_begin(worker->cache, worker->reader,
|
|
&worker->lease);
|
|
acquired(worker);
|
|
worker->first_rc = epoch_read_value(worker->cache, worker->lease, 0,
|
|
&worker->samples[0][0]);
|
|
worker->second_rc = epoch_read_value(worker->cache, worker->lease, 1,
|
|
&worker->samples[0][1]);
|
|
worker->end_rc = epoch_read_end(worker->cache, worker->lease);
|
|
epoch_reader_status(worker->cache, worker->reader, &worker->status);
|
|
return NULL;
|
|
}
|
|
|
|
static void print_worker(const char *kind, struct worker *worker)
|
|
{
|
|
printf("%s %s %d %d %d %d %d %" PRIu64 " %" PRIu64,
|
|
marker, kind, worker->index, worker->begin_rc, worker->first_rc,
|
|
worker->second_rc, worker->end_rc, worker->tx.ticket,
|
|
worker->lease.epoch);
|
|
sample_fields(worker->samples[0][0]);
|
|
sample_fields(worker->samples[0][1]);
|
|
printf(" %" PRIu64 " %" PRIu64 " %d", worker->status.seen_max,
|
|
worker->status.completed, worker->status.busy);
|
|
counters();
|
|
}
|
|
|
|
static void print_stage(const char *kind, int index, int gate_rc,
|
|
struct epoch_cache *cache, struct gate *gate)
|
|
{
|
|
struct epoch_state state;
|
|
memset(&state, 0, sizeof state);
|
|
int result = epoch_inspect(cache, &state);
|
|
pthread_mutex_lock(&gate->mutex);
|
|
printf("%s %s %d %d %d", marker, kind, index, gate_rc, result);
|
|
state_fields(state);
|
|
printf(" %u", gate->order_count);
|
|
for (unsigned i = 0; i < gate->order_count; ++i) {
|
|
printf(" %d", gate->order[i]);
|
|
}
|
|
pthread_mutex_unlock(&gate->mutex);
|
|
counters();
|
|
}
|
|
|
|
static void observe_fifo(uint64_t owner, uint64_t epoch, int count,
|
|
int64_t value, uint64_t tag)
|
|
{
|
|
struct epoch_cache *cache = epoch_create(owner, epoch);
|
|
struct epoch_reader first;
|
|
struct epoch_reader late;
|
|
struct epoch_lease lease;
|
|
memset(&first, 0, sizeof first);
|
|
memset(&late, 0, sizeof late);
|
|
memset(&lease, 0, sizeof lease);
|
|
int r0 = epoch_register(cache, &first);
|
|
int r1 = epoch_register(cache, &late);
|
|
int r2 = epoch_read_begin(cache, first, &lease);
|
|
printf("%s FSTART %d %d %d\n", marker, r0, r1, r2);
|
|
struct gate gate;
|
|
gate_init(&gate);
|
|
struct worker workers[5];
|
|
pthread_t threads[5];
|
|
memset(workers, 0, sizeof workers);
|
|
for (int i = 0; i < count; ++i) {
|
|
workers[i].cache = cache;
|
|
workers[i].gate = &gate;
|
|
workers[i].index = i;
|
|
workers[i].value = value + i;
|
|
workers[i].tag = tag + (uint64_t)i;
|
|
int started = pthread_create(&threads[i], NULL, held_writer, &workers[i]);
|
|
int reached = poll_state(cache, (unsigned)i + 1, 1, 0);
|
|
print_stage("FQUEUE", i, started != 0 ? started : reached, cache, &gate);
|
|
}
|
|
struct worker reader;
|
|
memset(&reader, 0, sizeof reader);
|
|
reader.cache = cache;
|
|
reader.gate = &gate;
|
|
reader.index = 8;
|
|
reader.reader = late;
|
|
pthread_t reader_thread;
|
|
int started = pthread_create(&reader_thread, NULL, held_reader, &reader);
|
|
int busy = poll_busy(cache, late);
|
|
printf("%s FBUSY %d %d %d\n", marker, started, busy,
|
|
epoch_unregister(cache, late));
|
|
int ended = epoch_read_end(cache, lease);
|
|
printf("%s FENDREAD %d\n", marker, ended);
|
|
for (int i = 0; i < count; ++i) {
|
|
int admitted = wait_acquired(&gate, i);
|
|
print_stage("FADMIT", i, admitted, cache, &gate);
|
|
release_worker(&gate, i);
|
|
pthread_join(threads[i], NULL);
|
|
print_worker("FWRITER", &workers[i]);
|
|
}
|
|
int admitted = wait_acquired(&gate, 8);
|
|
print_stage("FREADER", 8, admitted, cache, &gate);
|
|
release_worker(&gate, 8);
|
|
pthread_join(reader_thread, NULL);
|
|
print_worker("FREAD", &reader);
|
|
printf("%s FCLEAN %d %d", marker, epoch_unregister(cache, first),
|
|
epoch_unregister(cache, late));
|
|
int closed = epoch_close(cache);
|
|
int destroyed = epoch_destroy(cache);
|
|
printf(" %d %d\n", closed, destroyed);
|
|
gate_destroy(&gate);
|
|
}
|
|
|
|
static void observe_shared(uint64_t owner, uint64_t epoch,
|
|
int64_t value, uint64_t tag)
|
|
{
|
|
struct epoch_cache *cache = epoch_create(owner, epoch);
|
|
struct epoch_reader readers[2];
|
|
memset(readers, 0, sizeof readers);
|
|
int registered0 = epoch_register(cache, &readers[0]);
|
|
int registered1 = epoch_register(cache, &readers[1]);
|
|
printf("%s GSTART %d %d\n", marker, registered0, registered1);
|
|
struct gate gate;
|
|
gate_init(&gate);
|
|
struct worker work[3];
|
|
pthread_t threads[3];
|
|
memset(work, 0, sizeof work);
|
|
for (int i = 0; i < 2; ++i) {
|
|
work[i].cache = cache;
|
|
work[i].gate = &gate;
|
|
work[i].index = i;
|
|
work[i].reader = readers[i];
|
|
int started = pthread_create(&threads[i], NULL, held_reader, &work[i]);
|
|
int reached = wait_acquired(&gate, i);
|
|
print_stage("GREAD", i, started != 0 ? started : reached, cache, &gate);
|
|
}
|
|
work[2].cache = cache;
|
|
work[2].gate = &gate;
|
|
work[2].index = 2;
|
|
work[2].value = value;
|
|
work[2].tag = tag;
|
|
int started = pthread_create(&threads[2], NULL, held_writer, &work[2]);
|
|
int queued = poll_state(cache, 1, 2, 0);
|
|
print_stage("GQUEUE", 2, started != 0 ? started : queued, cache, &gate);
|
|
release_worker(&gate, 0);
|
|
pthread_join(threads[0], NULL);
|
|
int remaining = poll_state(cache, 1, 1, 0);
|
|
print_stage("GONE", 0, remaining, cache, &gate);
|
|
print_worker("GDONE", &work[0]);
|
|
release_worker(&gate, 1);
|
|
pthread_join(threads[1], NULL);
|
|
int admitted = wait_acquired(&gate, 2);
|
|
print_stage("GWRITE", 2, admitted, cache, &gate);
|
|
print_worker("GDONE", &work[1]);
|
|
release_worker(&gate, 2);
|
|
pthread_join(threads[2], NULL);
|
|
print_worker("GWRITER", &work[2]);
|
|
int u0 = epoch_unregister(cache, readers[0]);
|
|
int u1 = epoch_unregister(cache, readers[1]);
|
|
int closed = epoch_close(cache);
|
|
int destroyed = epoch_destroy(cache);
|
|
printf("%s GCLEAN %d %d %d %d\n", marker, u0, u1, closed, destroyed);
|
|
gate_destroy(&gate);
|
|
}
|
|
|
|
static void *wrong_thread(void *argument)
|
|
{
|
|
struct worker *worker = argument;
|
|
if (worker->role == 0) {
|
|
memset(&worker->samples[0][0], 0xa5, sizeof worker->samples[0][0]);
|
|
struct epoch_sample before = worker->samples[0][0];
|
|
worker->first_rc = epoch_read_value(worker->cache, worker->foreign_lease,
|
|
0, &worker->samples[0][0]);
|
|
worker->second_rc = memcmp(&before, &worker->samples[0][0], sizeof before);
|
|
worker->end_rc = epoch_read_end(worker->cache, worker->foreign_lease);
|
|
} else {
|
|
worker->first_rc = epoch_write_put(worker->cache, worker->foreign_tx,
|
|
0, worker->value, worker->tag);
|
|
worker->second_rc = epoch_write_commit(worker->cache, worker->foreign_tx);
|
|
worker->end_rc = epoch_write_abort(worker->cache, worker->foreign_tx);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static void observe_foreign(struct epoch_cache *cache, struct epoch_lease lease,
|
|
struct epoch_tx tx, int role, int64_t value,
|
|
uint64_t tag)
|
|
{
|
|
struct worker worker;
|
|
memset(&worker, 0, sizeof worker);
|
|
worker.cache = cache;
|
|
worker.foreign_lease = lease;
|
|
worker.foreign_tx = tx;
|
|
worker.role = role;
|
|
worker.value = value;
|
|
worker.tag = tag;
|
|
pthread_t thread;
|
|
int started = pthread_create(&thread, NULL, wrong_thread, &worker);
|
|
int joined = pthread_join(thread, NULL);
|
|
printf("%s FOREIGN %d %d %d %d %d %d", marker, role, started, joined,
|
|
worker.first_rc, worker.second_rc, worker.end_rc);
|
|
counters();
|
|
}
|
|
|
|
static void *stress_reader(void *argument)
|
|
{
|
|
struct worker *worker = argument;
|
|
for (int i = 0; i < worker->loops; ++i) {
|
|
struct epoch_lease lease;
|
|
memset(&lease, 0, sizeof lease);
|
|
worker->results[i][0] = epoch_read_begin(worker->cache, worker->reader,
|
|
&lease);
|
|
worker->serials[i] = lease.serial;
|
|
worker->results[i][1] = epoch_read_value(worker->cache, lease, 0,
|
|
&worker->samples[i][0]);
|
|
worker->results[i][2] = epoch_read_value(worker->cache, lease, 1,
|
|
&worker->samples[i][1]);
|
|
worker->results[i][3] = epoch_read_end(worker->cache, lease);
|
|
}
|
|
epoch_reader_status(worker->cache, worker->reader, &worker->status);
|
|
return NULL;
|
|
}
|
|
|
|
static void *stress_writer(void *argument)
|
|
{
|
|
struct worker *worker = argument;
|
|
for (int i = 0; i < worker->loops; ++i) {
|
|
struct epoch_tx tx;
|
|
memset(&tx, 0, sizeof tx);
|
|
worker->results[i][0] = epoch_write_begin(worker->cache, &tx);
|
|
worker->serials[i] = tx.ticket;
|
|
worker->results[i][1] = epoch_write_put(worker->cache, tx, 0,
|
|
worker->value + i,
|
|
worker->tag + (uint64_t)i);
|
|
worker->results[i][2] = epoch_write_put(worker->cache, tx, 1,
|
|
-(worker->value + i),
|
|
~(worker->tag + (uint64_t)i));
|
|
worker->results[i][3] = epoch_write_commit(worker->cache, tx);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static void observe_stress(uint64_t owner, uint64_t epoch, int loops,
|
|
int64_t value, uint64_t tag)
|
|
{
|
|
struct epoch_cache *cache = epoch_create(owner, epoch);
|
|
struct worker work[4];
|
|
pthread_t threads[4];
|
|
memset(work, 0, sizeof work);
|
|
for (int i = 0; i < 3; ++i) {
|
|
work[i].cache = cache;
|
|
work[i].loops = loops;
|
|
int result = epoch_register(cache, &work[i].reader);
|
|
printf("%s MREGISTER %d %d\n", marker, i, result);
|
|
}
|
|
work[3].cache = cache;
|
|
work[3].loops = loops;
|
|
work[3].value = value;
|
|
work[3].tag = tag;
|
|
for (int i = 0; i < 4; ++i) {
|
|
int result = pthread_create(&threads[i], NULL,
|
|
i == 3 ? stress_writer : stress_reader,
|
|
&work[i]);
|
|
printf("%s MTHREAD %d %d\n", marker, i, result);
|
|
}
|
|
for (int i = 0; i < 4; ++i) {
|
|
int result = pthread_join(threads[i], NULL);
|
|
printf("%s MJOIN %d %d\n", marker, i, result);
|
|
for (int j = 0; j < loops; ++j) {
|
|
printf("%s MROW %d %d %" PRIu64 " %d %d %d %d",
|
|
marker, i, j, work[i].serials[j],
|
|
work[i].results[j][0], work[i].results[j][1],
|
|
work[i].results[j][2], work[i].results[j][3]);
|
|
if (i != 3) {
|
|
sample_fields(work[i].samples[j][0]);
|
|
sample_fields(work[i].samples[j][1]);
|
|
}
|
|
counters();
|
|
}
|
|
if (i != 3) {
|
|
printf("%s MSTATUS %d %" PRIu64 " %" PRIu64 " %d\n",
|
|
marker, i, work[i].status.seen_max,
|
|
work[i].status.completed, work[i].status.busy);
|
|
}
|
|
}
|
|
struct epoch_state state;
|
|
memset(&state, 0, sizeof state);
|
|
int inspected = epoch_inspect(cache, &state);
|
|
printf("%s MSTATE %d", marker, inspected);
|
|
state_fields(state);
|
|
counters();
|
|
int u0 = epoch_unregister(cache, work[0].reader);
|
|
int u1 = epoch_unregister(cache, work[1].reader);
|
|
int u2 = epoch_unregister(cache, work[2].reader);
|
|
int closed = epoch_close(cache);
|
|
int destroyed = epoch_destroy(cache);
|
|
printf("%s MCLEAN %d %d %d %d %d\n", marker, u0, u1, u2, closed, destroyed);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
if (argc != 2) {
|
|
return 2;
|
|
}
|
|
marker = argv[1];
|
|
struct epoch_cache *caches[4] = {NULL, NULL, NULL, NULL};
|
|
struct epoch_reader readers[16];
|
|
struct epoch_lease leases[16];
|
|
struct epoch_tx transactions[16];
|
|
memset(readers, 0, sizeof readers);
|
|
memset(leases, 0, sizeof leases);
|
|
memset(transactions, 0, sizeof transactions);
|
|
char operation;
|
|
unsigned sequence = 0;
|
|
while (scanf(" %c", &operation) == 1) {
|
|
unsigned index = 0;
|
|
unsigned handle = 0;
|
|
unsigned destination = 0;
|
|
unsigned key = 0;
|
|
int field = 0;
|
|
int null_out = 0;
|
|
uint64_t changed = 0;
|
|
uint64_t owner = 0;
|
|
uint64_t epoch = 0;
|
|
uint64_t tag = 0;
|
|
int64_t value = 0;
|
|
int result = 0;
|
|
sequence++;
|
|
if (operation == 'c') {
|
|
if (scanf("%u %" SCNu64 " %" SCNu64, &index, &owner, &epoch) != 3) {
|
|
return 2;
|
|
}
|
|
errno = 0;
|
|
caches[index] = epoch_create(owner, epoch);
|
|
printf("%s c %u %d %d", marker, sequence,
|
|
caches[index] != NULL, errno);
|
|
counters();
|
|
} else if (operation == 'r') {
|
|
if (scanf("%u %u %d", &index, &destination, &null_out) != 3) {
|
|
return 2;
|
|
}
|
|
struct epoch_reader output;
|
|
memset(&output, 0xa5, sizeof output);
|
|
struct epoch_reader before = output;
|
|
result = epoch_register(caches[index], null_out ? NULL : &output);
|
|
if (result == 0 && !null_out) {
|
|
readers[destination] = output;
|
|
}
|
|
printf("%s r %u %d %d %" PRIu64 " %" PRIu64 " %u",
|
|
marker, sequence, result,
|
|
memcmp(&before, &output, sizeof output) == 0,
|
|
output.owner, output.generation, output.slot);
|
|
counters();
|
|
} else if (operation == 'u') {
|
|
if (scanf("%u %u %d %" SCNu64,
|
|
&index, &handle, &field, &changed) != 4) {
|
|
return 2;
|
|
}
|
|
struct epoch_reader reader = changed_reader(readers[handle], field, changed);
|
|
result = epoch_unregister(caches[index], reader);
|
|
printf("%s u %u %d", marker, sequence, result);
|
|
counters();
|
|
} else if (operation == 's') {
|
|
if (scanf("%u %u %d %" SCNu64 " %d",
|
|
&index, &handle, &field, &changed, &null_out) != 5) {
|
|
return 2;
|
|
}
|
|
struct epoch_reader reader = changed_reader(readers[handle], field, changed);
|
|
struct epoch_reader_info output;
|
|
memset(&output, 0xa5, sizeof output);
|
|
struct epoch_reader_info before = output;
|
|
result = epoch_reader_status(caches[index], reader,
|
|
null_out ? NULL : &output);
|
|
printf("%s s %u %d %d %" PRIu64 " %" PRIu64 " %d",
|
|
marker, sequence, result,
|
|
memcmp(&before, &output, sizeof output) == 0,
|
|
output.seen_max, output.completed, output.busy);
|
|
counters();
|
|
} else if (operation == 'b') {
|
|
if (scanf("%u %u %u %d %" SCNu64 " %d",
|
|
&index, &handle, &destination, &field, &changed, &null_out) != 6) {
|
|
return 2;
|
|
}
|
|
struct epoch_reader reader = changed_reader(readers[handle], field, changed);
|
|
struct epoch_lease output;
|
|
memset(&output, 0xa5, sizeof output);
|
|
struct epoch_lease before = output;
|
|
result = epoch_read_begin(caches[index], reader,
|
|
null_out ? NULL : &output);
|
|
if (result == 0 && !null_out) {
|
|
leases[destination] = output;
|
|
}
|
|
printf("%s b %u %d %d %" PRIu64 " %" PRIu64 " %" PRIu64
|
|
" %" PRIu64 " %u", marker, sequence, result,
|
|
memcmp(&before, &output, sizeof output) == 0,
|
|
output.owner, output.generation, output.serial,
|
|
output.epoch, output.slot);
|
|
counters();
|
|
} else if (operation == 'v') {
|
|
if (scanf("%u %u %u %d %" SCNu64 " %d",
|
|
&index, &handle, &key, &field, &changed, &null_out) != 6) {
|
|
return 2;
|
|
}
|
|
struct epoch_lease lease = changed_lease(leases[handle], field, changed);
|
|
struct epoch_sample output;
|
|
memset(&output, 0xa5, sizeof output);
|
|
struct epoch_sample before = output;
|
|
result = epoch_read_value(caches[index], lease, key,
|
|
null_out ? NULL : &output);
|
|
printf("%s v %u %d %d", marker, sequence, result,
|
|
memcmp(&before, &output, sizeof output) == 0);
|
|
sample_fields(output);
|
|
counters();
|
|
} else if (operation == 'e') {
|
|
if (scanf("%u %u %d %" SCNu64,
|
|
&index, &handle, &field, &changed) != 4) {
|
|
return 2;
|
|
}
|
|
struct epoch_lease lease = changed_lease(leases[handle], field, changed);
|
|
result = epoch_read_end(caches[index], lease);
|
|
printf("%s e %u %d", marker, sequence, result);
|
|
counters();
|
|
} else if (operation == 'w') {
|
|
if (scanf("%u %u %d", &index, &destination, &null_out) != 3) {
|
|
return 2;
|
|
}
|
|
struct epoch_tx output;
|
|
memset(&output, 0xa5, sizeof output);
|
|
struct epoch_tx before = output;
|
|
result = epoch_write_begin(caches[index], null_out ? NULL : &output);
|
|
if (result == 0 && !null_out) {
|
|
transactions[destination] = output;
|
|
}
|
|
printf("%s w %u %d %d %" PRIu64 " %" PRIu64,
|
|
marker, sequence, result,
|
|
memcmp(&before, &output, sizeof output) == 0,
|
|
output.owner, output.ticket);
|
|
counters();
|
|
} else if (operation == 'p') {
|
|
if (scanf("%u %u %u %" SCNd64 " %" SCNu64 " %d %" SCNu64,
|
|
&index, &handle, &key, &value, &tag, &field, &changed) != 7) {
|
|
return 2;
|
|
}
|
|
struct epoch_tx tx = changed_tx(transactions[handle], field, changed);
|
|
result = epoch_write_put(caches[index], tx, key, value, tag);
|
|
printf("%s p %u %d", marker, sequence, result);
|
|
counters();
|
|
} else if (operation == 'x') {
|
|
if (scanf("%u %u %u %d %" SCNu64,
|
|
&index, &handle, &key, &field, &changed) != 5) {
|
|
return 2;
|
|
}
|
|
struct epoch_tx tx = changed_tx(transactions[handle], field, changed);
|
|
result = epoch_write_erase(caches[index], tx, key);
|
|
printf("%s x %u %d", marker, sequence, result);
|
|
counters();
|
|
} else if (operation == 'k' || operation == 'a') {
|
|
if (scanf("%u %u %d %" SCNu64,
|
|
&index, &handle, &field, &changed) != 4) {
|
|
return 2;
|
|
}
|
|
struct epoch_tx tx = changed_tx(transactions[handle], field, changed);
|
|
result = operation == 'k' ? epoch_write_commit(caches[index], tx) :
|
|
epoch_write_abort(caches[index], tx);
|
|
printf("%s %c %u %d", marker, operation, sequence, result);
|
|
counters();
|
|
} else if (operation == 'i') {
|
|
if (scanf("%u %d", &index, &null_out) != 2) {
|
|
return 2;
|
|
}
|
|
struct epoch_state output;
|
|
memset(&output, 0xa5, sizeof output);
|
|
struct epoch_state before = output;
|
|
result = epoch_inspect(caches[index], null_out ? NULL : &output);
|
|
printf("%s i %u %d %d", marker, sequence, result,
|
|
memcmp(&before, &output, sizeof output) == 0);
|
|
state_fields(output);
|
|
counters();
|
|
} else if (operation == 'z') {
|
|
if (scanf("%u %" SCNu64, &index, &epoch) != 2) {
|
|
return 2;
|
|
}
|
|
result = epoch_reseed(caches[index], epoch);
|
|
printf("%s z %u %d", marker, sequence, result);
|
|
counters();
|
|
} else if (operation == 'q' || operation == 'd') {
|
|
if (scanf("%u", &index) != 1) {
|
|
return 2;
|
|
}
|
|
result = operation == 'q' ? epoch_close(caches[index]) :
|
|
epoch_destroy(caches[index]);
|
|
if (operation == 'd' && result == 0) {
|
|
caches[index] = NULL;
|
|
}
|
|
printf("%s %c %u %d", marker, operation, sequence, result);
|
|
counters();
|
|
} else if (operation == 'T') {
|
|
int role;
|
|
if (scanf("%u %u %u %d %" SCNd64 " %" SCNu64,
|
|
&index, &handle, &destination, &role, &value, &tag) != 6) {
|
|
return 2;
|
|
}
|
|
observe_foreign(caches[index], leases[handle], transactions[destination],
|
|
role, value, tag);
|
|
} else if (operation == 'F') {
|
|
int count;
|
|
if (scanf("%" SCNu64 " %" SCNu64 " %d %" SCNd64 " %" SCNu64,
|
|
&owner, &epoch, &count, &value, &tag) != 5) {
|
|
return 2;
|
|
}
|
|
observe_fifo(owner, epoch, count, value, tag);
|
|
} else if (operation == 'G') {
|
|
if (scanf("%" SCNu64 " %" SCNu64 " %" SCNd64 " %" SCNu64,
|
|
&owner, &epoch, &value, &tag) != 4) {
|
|
return 2;
|
|
}
|
|
observe_shared(owner, epoch, value, tag);
|
|
} else if (operation == 'M') {
|
|
int loops;
|
|
if (scanf("%" SCNu64 " %" SCNu64 " %d %" SCNd64 " %" SCNu64,
|
|
&owner, &epoch, &loops, &value, &tag) != 5) {
|
|
return 2;
|
|
}
|
|
observe_stress(owner, epoch, loops, value, tag);
|
|
} else {
|
|
return 2;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|