| 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_hp_fifo.h> |
| 32 |
|
| 33 |
#include "../../common.h" |
| 34 |
|
| 35 |
#ifndef ITERATIONS |
| 36 |
#define ITERATIONS 128 |
| 37 |
#endif |
| 38 |
|
| 39 |
struct context { |
| 40 |
unsigned int tid; |
| 41 |
unsigned int previous; |
| 42 |
unsigned int next; |
| 43 |
}; |
| 44 |
|
| 45 |
struct entry { |
| 46 |
int tid; |
| 47 |
int value; |
| 48 |
}; |
| 49 |
|
| 50 |
static ck_hp_fifo_t fifo; |
| 51 |
static ck_hp_t fifo_hp; |
| 52 |
static int nthr; |
| 53 |
|
| 54 |
static struct affinity a; |
| 55 |
static int size; |
| 56 |
static unsigned int barrier; |
| 57 |
static unsigned int e_barrier; |
| 58 |
static unsigned int s_barrier; |
| 59 |
|
| 60 |
static void * |
| 61 |
test(void *c) |
| 62 |
{ |
| 63 |
struct context *context = c; |
| 64 |
struct entry *entry; |
| 65 |
ck_hp_fifo_entry_t *fifo_entry; |
| 66 |
ck_hp_record_t record; |
| 67 |
int i, j; |
| 68 |
|
| 69 |
if (aff_iterate(&a)) { |
| 70 |
perror("ERROR: Could not affine thread"); |
| 71 |
exit(EXIT_FAILURE); |
| 72 |
} |
| 73 |
|
| 74 |
ck_hp_register(&fifo_hp, &record, malloc(sizeof(void *) * 2)); |
| 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_hp_fifo_entry_t)); |
| 81 |
entry = malloc(sizeof(struct entry)); |
| 82 |
entry->tid = context->tid; |
| 83 |
ck_hp_fifo_enqueue_mpmc(&record, &fifo, fifo_entry, entry); |
| 84 |
|
| 85 |
ck_pr_barrier(); |
| 86 |
|
| 87 |
fifo_entry = ck_hp_fifo_dequeue_mpmc(&record, &fifo, &entry); |
| 88 |
if (fifo_entry == NULL) { |
| 89 |
ck_error("ERROR [%u] Queue should never be empty.\n", context->tid); |
| 90 |
} |
| 91 |
|
| 92 |
ck_pr_barrier(); |
| 93 |
|
| 94 |
if (entry->tid < 0 || entry->tid >= nthr) { |
| 95 |
ck_error("ERROR [%u] Incorrect value in entry.\n", entry->tid); |
| 96 |
} |
| 97 |
|
| 98 |
ck_hp_free(&record, &fifo_entry->hazard, fifo_entry, fifo_entry); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
ck_pr_inc_uint(&s_barrier); |
| 103 |
while (ck_pr_load_uint(&s_barrier) < (unsigned int)nthr); |
| 104 |
|
| 105 |
for (i = 0; i < ITERATIONS; i++) { |
| 106 |
for (j = 0; j < size; j++) { |
| 107 |
fifo_entry = malloc(sizeof(ck_hp_fifo_entry_t)); |
| 108 |
entry = malloc(sizeof(struct entry)); |
| 109 |
entry->tid = context->tid; |
| 110 |
|
| 111 |
while (ck_hp_fifo_tryenqueue_mpmc(&record, &fifo, fifo_entry, entry) == false) |
| 112 |
ck_pr_stall(); |
| 113 |
|
| 114 |
while (fifo_entry = ck_hp_fifo_trydequeue_mpmc(&record, &fifo, &entry), fifo_entry == NULL) |
| 115 |
ck_pr_stall(); |
| 116 |
|
| 117 |
if (entry->tid < 0 || entry->tid >= nthr) { |
| 118 |
ck_error("ERROR [%u] Incorrect value in entry.\n", entry->tid); |
| 119 |
} |
| 120 |
|
| 121 |
ck_hp_free(&record, &fifo_entry->hazard, fifo_entry, fifo_entry); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
ck_pr_inc_uint(&e_barrier); |
| 126 |
while (ck_pr_load_uint(&e_barrier) < (unsigned int)nthr); |
| 127 |
|
| 128 |
return (NULL); |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
destructor(void *p) |
| 133 |
{ |
| 134 |
|
| 135 |
free(p); |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
int |
| 140 |
main(int argc, char *argv[]) |
| 141 |
{ |
| 142 |
int i, r; |
| 143 |
struct context *context; |
| 144 |
pthread_t *thread; |
| 145 |
int threshold; |
| 146 |
|
| 147 |
if (argc != 5) { |
| 148 |
ck_error("Usage: validate <threads> <affinity delta> <size> <threshold>\n"); |
| 149 |
} |
| 150 |
|
| 151 |
a.delta = atoi(argv[2]); |
| 152 |
|
| 153 |
nthr = atoi(argv[1]); |
| 154 |
assert(nthr >= 1); |
| 155 |
|
| 156 |
size = atoi(argv[3]); |
| 157 |
assert(size > 0); |
| 158 |
|
| 159 |
threshold = atoi(argv[4]); |
| 160 |
assert(threshold > 0); |
| 161 |
|
| 162 |
context = malloc(sizeof(*context) * nthr); |
| 163 |
assert(context); |
| 164 |
|
| 165 |
thread = malloc(sizeof(pthread_t) * nthr); |
| 166 |
assert(thread); |
| 167 |
|
| 168 |
ck_hp_init(&fifo_hp, 2, threshold, destructor); |
| 169 |
ck_hp_fifo_init(&fifo, malloc(sizeof(ck_hp_fifo_entry_t))); |
| 170 |
|
| 171 |
ck_hp_fifo_entry_t *entry; |
| 172 |
ck_hp_fifo_deinit(&fifo, &entry); |
| 173 |
|
| 174 |
if (entry == NULL) |
| 175 |
ck_error("ERROR: Expected non-NULL stub node.\n"); |
| 176 |
|
| 177 |
free(entry); |
| 178 |
ck_hp_fifo_init(&fifo, malloc(sizeof(ck_hp_fifo_entry_t))); |
| 179 |
|
| 180 |
for (i = 0; i < nthr; i++) { |
| 181 |
context[i].tid = i; |
| 182 |
r = pthread_create(thread + i, NULL, test, context + i); |
| 183 |
assert(r == 0); |
| 184 |
} |
| 185 |
|
| 186 |
for (i = 0; i < nthr; i++) |
| 187 |
pthread_join(thread[i], NULL); |
| 188 |
|
| 189 |
return (0); |
| 190 |
} |
| 191 |
|