| 1 |
/* |
| 2 |
* Copyright 2010-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 <pthread.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <unistd.h> |
| 32 |
#include <ck_cc.h> |
| 33 |
#include <ck_pr.h> |
| 34 |
#include <inttypes.h> |
| 35 |
#include <stdbool.h> |
| 36 |
#include <string.h> |
| 37 |
#include <ck_epoch.h> |
| 38 |
#include <ck_stack.h> |
| 39 |
|
| 40 |
#include "../../common.h" |
| 41 |
|
| 42 |
static unsigned int n_rd; |
| 43 |
static unsigned int n_wr; |
| 44 |
static unsigned int n_threads; |
| 45 |
static unsigned int barrier; |
| 46 |
static unsigned int leave; |
| 47 |
static unsigned int first; |
| 48 |
|
| 49 |
struct { |
| 50 |
unsigned int value; |
| 51 |
} valid CK_CC_CACHELINE = { 1 }; |
| 52 |
|
| 53 |
struct { |
| 54 |
unsigned int value; |
| 55 |
} invalid CK_CC_CACHELINE; |
| 56 |
|
| 57 |
#ifndef PAIRS_S |
| 58 |
#define PAIRS_S 10000 |
| 59 |
#endif |
| 60 |
|
| 61 |
#ifndef CK_EPOCH_T_DEPTH |
| 62 |
#define CK_EPOCH_T_DEPTH 8 |
| 63 |
#endif |
| 64 |
|
| 65 |
static ck_epoch_t epoch; |
| 66 |
static struct affinity a; |
| 67 |
|
| 68 |
static void |
| 69 |
test(struct ck_epoch_record *record) |
| 70 |
{ |
| 71 |
unsigned int j[3]; |
| 72 |
unsigned int b, c; |
| 73 |
const unsigned int r = 100; |
| 74 |
size_t i; |
| 75 |
|
| 76 |
for (i = 0; i < 8; i++) { |
| 77 |
ck_epoch_begin(record, NULL); |
| 78 |
c = ck_pr_load_uint(&invalid.value); |
| 79 |
ck_pr_fence_load(); |
| 80 |
b = ck_pr_load_uint(&valid.value); |
| 81 |
ck_test(c > b, "Invalid value: %u > %u\n", c, b); |
| 82 |
ck_epoch_end(record, NULL); |
| 83 |
} |
| 84 |
|
| 85 |
ck_epoch_begin(record, NULL); |
| 86 |
|
| 87 |
/* This implies no early load of epoch occurs. */ |
| 88 |
j[0] = record->epoch; |
| 89 |
|
| 90 |
|
| 91 |
/* We should observe up to one epoch migration. */ |
| 92 |
do { |
| 93 |
ck_pr_fence_load(); |
| 94 |
j[1] = ck_pr_load_uint(&epoch.epoch); |
| 95 |
|
| 96 |
if (ck_pr_load_uint(&leave) == 1) { |
| 97 |
ck_epoch_end(record, NULL); |
| 98 |
return; |
| 99 |
} |
| 100 |
} while (j[1] == j[0]); |
| 101 |
|
| 102 |
/* No more epoch migrations should occur */ |
| 103 |
for (i = 0; i < r; i++) { |
| 104 |
ck_pr_fence_strict_load(); |
| 105 |
j[2] = ck_pr_load_uint(&epoch.epoch); |
| 106 |
|
| 107 |
ck_test(j[2] != j[1], "Inconsistency detected: %u %u %u\n", |
| 108 |
j[0], j[1], j[2]); |
| 109 |
} |
| 110 |
|
| 111 |
ck_epoch_end(record, NULL); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
static void * |
| 116 |
read_thread(void *unused CK_CC_UNUSED) |
| 117 |
{ |
| 118 |
ck_epoch_record_t *record; |
| 119 |
|
| 120 |
record = malloc(sizeof *record); |
| 121 |
assert(record != NULL); |
| 122 |
ck_epoch_register(&epoch, record, NULL); |
| 123 |
|
| 124 |
if (aff_iterate(&a)) { |
| 125 |
perror("ERROR: failed to affine thread"); |
| 126 |
exit(EXIT_FAILURE); |
| 127 |
} |
| 128 |
|
| 129 |
ck_pr_inc_uint(&barrier); |
| 130 |
while (ck_pr_load_uint(&barrier) < n_threads); |
| 131 |
|
| 132 |
do { |
| 133 |
test(record); |
| 134 |
test(record); |
| 135 |
test(record); |
| 136 |
test(record); |
| 137 |
} while (ck_pr_load_uint(&leave) == 0); |
| 138 |
|
| 139 |
ck_pr_dec_uint(&n_rd); |
| 140 |
|
| 141 |
return NULL; |
| 142 |
} |
| 143 |
|
| 144 |
static void * |
| 145 |
write_thread(void *unused CK_CC_UNUSED) |
| 146 |
{ |
| 147 |
ck_epoch_record_t *record; |
| 148 |
unsigned long iterations = 0; |
| 149 |
bool c = ck_pr_faa_uint(&first, 1); |
| 150 |
uint64_t ac = 0; |
| 151 |
|
| 152 |
record = malloc(sizeof *record); |
| 153 |
assert(record != NULL); |
| 154 |
ck_epoch_register(&epoch, record, NULL); |
| 155 |
|
| 156 |
if (aff_iterate(&a)) { |
| 157 |
perror("ERROR: failed to affine thread"); |
| 158 |
exit(EXIT_FAILURE); |
| 159 |
} |
| 160 |
|
| 161 |
ck_pr_inc_uint(&barrier); |
| 162 |
while (ck_pr_load_uint(&barrier) < n_threads); |
| 163 |
|
| 164 |
#define CK_EPOCH_S do { \ |
| 165 |
uint64_t _s = rdtsc(); \ |
| 166 |
ck_epoch_synchronize(record); \ |
| 167 |
ac += rdtsc() - _s; \ |
| 168 |
} while (0) |
| 169 |
|
| 170 |
do { |
| 171 |
/* |
| 172 |
* A thread should never observe invalid.value > valid.value. |
| 173 |
* inside a protected section. Only |
| 174 |
* invalid.value <= valid.value is valid. |
| 175 |
*/ |
| 176 |
if (!c) ck_pr_store_uint(&valid.value, 1); |
| 177 |
CK_EPOCH_S; |
| 178 |
if (!c) ck_pr_store_uint(&invalid.value, 1); |
| 179 |
|
| 180 |
ck_pr_fence_store(); |
| 181 |
if (!c) ck_pr_store_uint(&valid.value, 2); |
| 182 |
CK_EPOCH_S; |
| 183 |
if (!c) ck_pr_store_uint(&invalid.value, 2); |
| 184 |
|
| 185 |
ck_pr_fence_store(); |
| 186 |
if (!c) ck_pr_store_uint(&valid.value, 3); |
| 187 |
CK_EPOCH_S; |
| 188 |
if (!c) ck_pr_store_uint(&invalid.value, 3); |
| 189 |
|
| 190 |
ck_pr_fence_store(); |
| 191 |
if (!c) ck_pr_store_uint(&valid.value, 4); |
| 192 |
CK_EPOCH_S; |
| 193 |
if (!c) ck_pr_store_uint(&invalid.value, 4); |
| 194 |
|
| 195 |
CK_EPOCH_S; |
| 196 |
if (!c) ck_pr_store_uint(&invalid.value, 0); |
| 197 |
CK_EPOCH_S; |
| 198 |
|
| 199 |
iterations += 6; |
| 200 |
} while (ck_pr_load_uint(&leave) == 0 && |
| 201 |
ck_pr_load_uint(&n_rd) > 0); |
| 202 |
|
| 203 |
fprintf(stderr, "%lu iterations\n", iterations); |
| 204 |
fprintf(stderr, "%" PRIu64 " average latency\n", ac / iterations); |
| 205 |
return NULL; |
| 206 |
} |
| 207 |
|
| 208 |
int |
| 209 |
main(int argc, char *argv[]) |
| 210 |
{ |
| 211 |
unsigned int i; |
| 212 |
pthread_t *threads; |
| 213 |
|
| 214 |
if (argc != 4) { |
| 215 |
ck_error("Usage: stack <#readers> <#writers> <affinity delta>\n"); |
| 216 |
} |
| 217 |
|
| 218 |
n_rd = atoi(argv[1]); |
| 219 |
n_wr = atoi(argv[2]); |
| 220 |
n_threads = n_wr + n_rd; |
| 221 |
|
| 222 |
a.delta = atoi(argv[3]); |
| 223 |
a.request = 0; |
| 224 |
|
| 225 |
threads = malloc(sizeof(pthread_t) * n_threads); |
| 226 |
ck_epoch_init(&epoch); |
| 227 |
|
| 228 |
for (i = 0; i < n_rd; i++) |
| 229 |
pthread_create(threads + i, NULL, read_thread, NULL); |
| 230 |
|
| 231 |
do { |
| 232 |
pthread_create(threads + i, NULL, write_thread, NULL); |
| 233 |
} while (++i < n_wr + n_rd); |
| 234 |
|
| 235 |
common_sleep(30); |
| 236 |
ck_pr_store_uint(&leave, 1); |
| 237 |
|
| 238 |
for (i = 0; i < n_threads; i++) |
| 239 |
pthread_join(threads[i], NULL); |
| 240 |
|
| 241 |
return 0; |
| 242 |
} |