| 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 |
#include <ck_fifo.h> |
| 32 |
|
| 33 |
#include "../../common.h" |
| 34 |
|
| 35 |
#ifdef CK_F_FIFO_MPMC |
| 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 |
|
| 53 |
#ifdef CK_F_FIFO_MPMC |
| 54 |
static ck_fifo_mpmc_t fifo CK_CC_CACHELINE; |
| 55 |
#endif |
| 56 |
|
| 57 |
static struct affinity a; |
| 58 |
static int size; |
| 59 |
static unsigned int barrier; |
| 60 |
|
| 61 |
static void * |
| 62 |
test(void *c) |
| 63 |
{ |
| 64 |
#ifdef CK_F_FIFO_MPMC |
| 65 |
struct context *context = c; |
| 66 |
struct entry *entry; |
| 67 |
ck_fifo_mpmc_entry_t *fifo_entry, *garbage; |
| 68 |
int i, j; |
| 69 |
|
| 70 |
if (aff_iterate(&a)) { |
| 71 |
perror("ERROR: Could not affine thread"); |
| 72 |
exit(EXIT_FAILURE); |
| 73 |
} |
| 74 |
|
| 75 |
ck_pr_inc_uint(&barrier); |
| 76 |
while (ck_pr_load_uint(&barrier) < (unsigned int)nthr); |
| 77 |
|
| 78 |
for (i = 0; i < ITERATIONS; i++) { |
| 79 |
for (j = 0; j < size; j++) { |
| 80 |
fifo_entry = malloc(sizeof(ck_fifo_mpmc_entry_t)); |
| 81 |
entry = malloc(sizeof(struct entry)); |
| 82 |
entry->tid = context->tid; |
| 83 |
ck_fifo_mpmc_enqueue(&fifo, fifo_entry, entry); |
| 84 |
if (ck_fifo_mpmc_dequeue(&fifo, &entry, &garbage) == false) { |
| 85 |
ck_error("ERROR [%u] Queue should never be empty.\n", context->tid); |
| 86 |
} |
| 87 |
|
| 88 |
if (entry->tid < 0 || entry->tid >= nthr) { |
| 89 |
ck_error("ERROR [%u] Incorrect value in entry.\n", entry->tid); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
for (i = 0; i < ITERATIONS; i++) { |
| 95 |
for (j = 0; j < size; j++) { |
| 96 |
fifo_entry = malloc(sizeof(ck_fifo_mpmc_entry_t)); |
| 97 |
entry = malloc(sizeof(struct entry)); |
| 98 |
entry->tid = context->tid; |
| 99 |
while (ck_fifo_mpmc_tryenqueue(&fifo, fifo_entry, entry) == false) |
| 100 |
ck_pr_stall(); |
| 101 |
|
| 102 |
while (ck_fifo_mpmc_trydequeue(&fifo, &entry, &garbage) == false) |
| 103 |
ck_pr_stall(); |
| 104 |
|
| 105 |
if (entry->tid < 0 || entry->tid >= nthr) { |
| 106 |
ck_error("ERROR [%u] Incorrect value in entry when using try interface.\n", entry->tid); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
#endif |
| 111 |
|
| 112 |
return (NULL); |
| 113 |
} |
| 114 |
|
| 115 |
int |
| 116 |
main(int argc, char *argv[]) |
| 117 |
{ |
| 118 |
int i, r; |
| 119 |
struct context *context; |
| 120 |
ck_fifo_mpmc_entry_t *garbage; |
| 121 |
pthread_t *thread; |
| 122 |
|
| 123 |
if (argc != 4) { |
| 124 |
ck_error("Usage: validate <threads> <affinity delta> <size>\n"); |
| 125 |
} |
| 126 |
|
| 127 |
a.request = 0; |
| 128 |
a.delta = atoi(argv[2]); |
| 129 |
|
| 130 |
nthr = atoi(argv[1]); |
| 131 |
assert(nthr >= 1); |
| 132 |
|
| 133 |
size = atoi(argv[3]); |
| 134 |
assert(size > 0); |
| 135 |
|
| 136 |
context = malloc(sizeof(*context) * nthr); |
| 137 |
assert(context); |
| 138 |
|
| 139 |
thread = malloc(sizeof(pthread_t) * nthr); |
| 140 |
assert(thread); |
| 141 |
|
| 142 |
ck_fifo_mpmc_init(&fifo, malloc(sizeof(ck_fifo_mpmc_entry_t))); |
| 143 |
ck_fifo_mpmc_deinit(&fifo, &garbage); |
| 144 |
if (garbage == NULL) |
| 145 |
ck_error("ERROR: Expected non-NULL stub node on deinit.\n"); |
| 146 |
free(garbage); |
| 147 |
ck_fifo_mpmc_init(&fifo, malloc(sizeof(ck_fifo_mpmc_entry_t))); |
| 148 |
|
| 149 |
for (i = 0; i < nthr; i++) { |
| 150 |
context[i].tid = i; |
| 151 |
r = pthread_create(thread + i, NULL, test, context + i); |
| 152 |
assert(r == 0); |
| 153 |
} |
| 154 |
|
| 155 |
for (i = 0; i < nthr; i++) |
| 156 |
pthread_join(thread[i], NULL); |
| 157 |
|
| 158 |
return (0); |
| 159 |
} |
| 160 |
#else |
| 161 |
int |
| 162 |
main(void) |
| 163 |
{ |
| 164 |
fprintf(stderr, "Unsupported.\n"); |
| 165 |
return 0; |
| 166 |
} |
| 167 |
#endif |
| 168 |
|