| 1 |
/* |
| 2 |
* Copyright 2011-2015 Samy Al Bahra. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 15 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 18 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 20 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 21 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 22 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 23 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 24 |
* SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <assert.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <pthread.h> |
| 31 |
|
| 32 |
#include <ck_fifo.h> |
| 33 |
|
| 34 |
#include "../../common.h" |
| 35 |
|
| 36 |
#ifndef ITERATIONS |
| 37 |
#define ITERATIONS 128 |
| 38 |
#endif |
| 39 |
|
| 40 |
struct context { |
| 41 |
unsigned int tid; |
| 42 |
unsigned int previous; |
| 43 |
unsigned int next; |
| 44 |
}; |
| 45 |
|
| 46 |
struct entry { |
| 47 |
int tid; |
| 48 |
int value; |
| 49 |
}; |
| 50 |
|
| 51 |
static int nthr; |
| 52 |
static ck_fifo_spsc_t *fifo; |
| 53 |
static struct affinity a; |
| 54 |
static int size; |
| 55 |
static unsigned int barrier; |
| 56 |
|
| 57 |
static void * |
| 58 |
test(void *c) |
| 59 |
{ |
| 60 |
struct context *context = c; |
| 61 |
struct entry *entry; |
| 62 |
ck_fifo_spsc_entry_t *fifo_entry; |
| 63 |
int i, j; |
| 64 |
|
| 65 |
if (aff_iterate(&a)) { |
| 66 |
perror("ERROR: Could not affine thread"); |
| 67 |
exit(EXIT_FAILURE); |
| 68 |
} |
| 69 |
|
| 70 |
#ifdef DEBUG |
| 71 |
fprintf(stderr, "%p %u: %u -> %u\n", fifo+context->tid, context->tid, context->previous, context->tid); |
| 72 |
#endif |
| 73 |
|
| 74 |
if (context->tid == 0) { |
| 75 |
struct entry *entries; |
| 76 |
|
| 77 |
entries = malloc(sizeof(struct entry) * size); |
| 78 |
assert(entries != NULL); |
| 79 |
|
| 80 |
for (i = 0; i < size; i++) { |
| 81 |
entries[i].value = i; |
| 82 |
entries[i].tid = 0; |
| 83 |
|
| 84 |
fifo_entry = malloc(sizeof(ck_fifo_spsc_entry_t)); |
| 85 |
ck_fifo_spsc_enqueue(fifo + context->tid, fifo_entry, entries + i); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
ck_pr_inc_uint(&barrier); |
| 90 |
while (ck_pr_load_uint(&barrier) < (unsigned int)nthr); |
| 91 |
|
| 92 |
for (i = 0; i < ITERATIONS; i++) { |
| 93 |
for (j = 0; j < size; j++) { |
| 94 |
while (ck_fifo_spsc_dequeue(fifo + context->previous, &entry) == false); |
| 95 |
if (context->previous != (unsigned int)entry->tid) { |
| 96 |
ck_error("T [%u:%p] %u != %u\n", |
| 97 |
context->tid, (void *)entry, entry->tid, context->previous); |
| 98 |
} |
| 99 |
|
| 100 |
if (entry->value != j) { |
| 101 |
ck_error("V [%u:%p] %u != %u\n", |
| 102 |
context->tid, (void *)entry, entry->value, j); |
| 103 |
} |
| 104 |
|
| 105 |
entry->tid = context->tid; |
| 106 |
fifo_entry = ck_fifo_spsc_recycle(fifo + context->tid); |
| 107 |
if (fifo_entry == NULL) |
| 108 |
fifo_entry = malloc(sizeof(ck_fifo_spsc_entry_t)); |
| 109 |
|
| 110 |
ck_fifo_spsc_enqueue(fifo + context->tid, fifo_entry, entry); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return (NULL); |
| 115 |
} |
| 116 |
|
| 117 |
int |
| 118 |
main(int argc, char *argv[]) |
| 119 |
{ |
| 120 |
int i, r; |
| 121 |
struct context *context; |
| 122 |
pthread_t *thread; |
| 123 |
|
| 124 |
if (argc != 4) { |
| 125 |
ck_error("Usage: validate <threads> <affinity delta> <size>\n"); |
| 126 |
} |
| 127 |
|
| 128 |
a.request = 0; |
| 129 |
a.delta = atoi(argv[2]); |
| 130 |
|
| 131 |
nthr = atoi(argv[1]); |
| 132 |
assert(nthr >= 1); |
| 133 |
|
| 134 |
size = atoi(argv[3]); |
| 135 |
assert(size > 0); |
| 136 |
|
| 137 |
fifo = malloc(sizeof(ck_fifo_spsc_t) * nthr); |
| 138 |
assert(fifo); |
| 139 |
|
| 140 |
context = malloc(sizeof(*context) * nthr); |
| 141 |
assert(context); |
| 142 |
|
| 143 |
thread = malloc(sizeof(pthread_t) * nthr); |
| 144 |
assert(thread); |
| 145 |
|
| 146 |
for (i = 0; i < nthr; i++) { |
| 147 |
ck_fifo_spsc_entry_t *garbage; |
| 148 |
|
| 149 |
context[i].tid = i; |
| 150 |
if (i == 0) { |
| 151 |
context[i].previous = nthr - 1; |
| 152 |
context[i].next = i + 1; |
| 153 |
} else if (i == nthr - 1) { |
| 154 |
context[i].next = 0; |
| 155 |
context[i].previous = i - 1; |
| 156 |
} else { |
| 157 |
context[i].next = i + 1; |
| 158 |
context[i].previous = i - 1; |
| 159 |
} |
| 160 |
|
| 161 |
ck_fifo_spsc_init(fifo + i, malloc(sizeof(ck_fifo_spsc_entry_t))); |
| 162 |
ck_fifo_spsc_deinit(fifo + i, &garbage); |
| 163 |
if (garbage == NULL) |
| 164 |
ck_error("ERROR: Expected non-NULL stub node on deinit.\n"); |
| 165 |
|
| 166 |
free(garbage); |
| 167 |
ck_fifo_spsc_init(fifo + i, malloc(sizeof(ck_fifo_spsc_entry_t))); |
| 168 |
r = pthread_create(thread + i, NULL, test, context + i); |
| 169 |
assert(r == 0); |
| 170 |
} |
| 171 |
|
| 172 |
for (i = 0; i < nthr; i++) |
| 173 |
pthread_join(thread[i], NULL); |
| 174 |
|
| 175 |
return (0); |
| 176 |
} |
| 177 |
|