| 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 <stdbool.h> |
| 35 |
#include <stddef.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 |
|
| 48 |
#ifndef PAIRS_S |
| 49 |
#define PAIRS_S 10000 |
| 50 |
#endif |
| 51 |
|
| 52 |
#ifndef CK_EPOCH_T_DEPTH |
| 53 |
#define CK_EPOCH_T_DEPTH 8 |
| 54 |
#endif |
| 55 |
|
| 56 |
static ck_epoch_t epoch; |
| 57 |
static struct affinity a; |
| 58 |
|
| 59 |
static void * |
| 60 |
read_thread(void *unused CK_CC_UNUSED) |
| 61 |
{ |
| 62 |
ck_epoch_record_t *record; |
| 63 |
unsigned long long i = 0; |
| 64 |
|
| 65 |
record = malloc(sizeof *record); |
| 66 |
assert(record != NULL); |
| 67 |
ck_epoch_register(&epoch, record, NULL); |
| 68 |
|
| 69 |
if (aff_iterate(&a)) { |
| 70 |
perror("ERROR: failed to affine thread"); |
| 71 |
exit(EXIT_FAILURE); |
| 72 |
} |
| 73 |
|
| 74 |
ck_pr_inc_uint(&barrier); |
| 75 |
while (ck_pr_load_uint(&barrier) < n_threads); |
| 76 |
|
| 77 |
for (;;) { |
| 78 |
ck_epoch_section_t section[2]; |
| 79 |
ck_epoch_section_t junk[CK_EPOCH_T_DEPTH]; |
| 80 |
unsigned int j; |
| 81 |
|
| 82 |
ck_epoch_begin(record, §ion[0]); |
| 83 |
|
| 84 |
for (j = 0; j < CK_EPOCH_T_DEPTH; j++) |
| 85 |
ck_epoch_begin(record, &junk[j]); |
| 86 |
for (j = 0; j < CK_EPOCH_T_DEPTH; j++) |
| 87 |
ck_epoch_end(record, &junk[j]); |
| 88 |
|
| 89 |
if (i > 0) |
| 90 |
ck_epoch_end(record, §ion[1]); |
| 91 |
|
| 92 |
/* Wait for the next synchronize operation. */ |
| 93 |
while ((ck_pr_load_uint(&epoch.epoch) & 1) == |
| 94 |
section[0].bucket) { |
| 95 |
i++; |
| 96 |
|
| 97 |
if (!(i % 10000000)) { |
| 98 |
fprintf(stderr, "%u %u %u\n", |
| 99 |
ck_pr_load_uint(&epoch.epoch), |
| 100 |
section[0].bucket, record->epoch); |
| 101 |
} |
| 102 |
|
| 103 |
while ((ck_pr_load_uint(&epoch.epoch) & 1) == |
| 104 |
section[0].bucket) { |
| 105 |
if (ck_pr_load_uint(&leave) == 1) |
| 106 |
break; |
| 107 |
|
| 108 |
ck_pr_stall(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
ck_epoch_begin(record, §ion[1]); |
| 113 |
if (section[0].bucket == section[1].bucket) { |
| 114 |
ck_error("%u == %u\n", |
| 115 |
section[0].bucket, section[1].bucket); |
| 116 |
} |
| 117 |
ck_epoch_end(record, §ion[0]); |
| 118 |
|
| 119 |
if (ck_pr_load_uint(&record->active) == 0) |
| 120 |
ck_error("active: %u\n", record->active); |
| 121 |
|
| 122 |
if (ck_pr_load_uint(&leave) == 1) { |
| 123 |
ck_epoch_end(record, §ion[1]); |
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
i++; |
| 128 |
} |
| 129 |
|
| 130 |
return NULL; |
| 131 |
} |
| 132 |
|
| 133 |
static void * |
| 134 |
write_thread(void *unused CK_CC_UNUSED) |
| 135 |
{ |
| 136 |
ck_epoch_record_t *record; |
| 137 |
unsigned long iterations = 0; |
| 138 |
|
| 139 |
record = malloc(sizeof *record); |
| 140 |
if (record == NULL) |
| 141 |
ck_error("record allocation failure"); |
| 142 |
|
| 143 |
ck_epoch_register(&epoch, record, NULL); |
| 144 |
|
| 145 |
if (aff_iterate(&a)) { |
| 146 |
perror("ERROR: failed to affine thread"); |
| 147 |
exit(EXIT_FAILURE); |
| 148 |
} |
| 149 |
|
| 150 |
ck_pr_inc_uint(&barrier); |
| 151 |
while (ck_pr_load_uint(&barrier) < n_threads); |
| 152 |
|
| 153 |
for (;;) { |
| 154 |
if (!(iterations % 1048575)) |
| 155 |
fprintf(stderr, "."); |
| 156 |
|
| 157 |
ck_epoch_synchronize(record); |
| 158 |
iterations++; |
| 159 |
|
| 160 |
if (ck_pr_load_uint(&leave) == 1) |
| 161 |
break; |
| 162 |
} |
| 163 |
|
| 164 |
fprintf(stderr, "%lu iterations\n", iterations); |
| 165 |
return NULL; |
| 166 |
} |
| 167 |
|
| 168 |
int |
| 169 |
main(int argc, char *argv[]) |
| 170 |
{ |
| 171 |
unsigned int i; |
| 172 |
pthread_t *threads; |
| 173 |
|
| 174 |
if (argc != 4) { |
| 175 |
ck_error("Usage: stack <#readers> <#writers> <affinity delta>\n"); |
| 176 |
} |
| 177 |
|
| 178 |
n_rd = atoi(argv[1]); |
| 179 |
n_wr = atoi(argv[2]); |
| 180 |
n_threads = n_wr + n_rd; |
| 181 |
|
| 182 |
a.delta = atoi(argv[3]); |
| 183 |
a.request = 0; |
| 184 |
|
| 185 |
threads = malloc(sizeof(pthread_t) * n_threads); |
| 186 |
ck_epoch_init(&epoch); |
| 187 |
|
| 188 |
for (i = 0; i < n_rd; i++) |
| 189 |
pthread_create(threads + i, NULL, read_thread, NULL); |
| 190 |
|
| 191 |
do { |
| 192 |
pthread_create(threads + i, NULL, write_thread, NULL); |
| 193 |
} while (++i < n_wr + n_rd); |
| 194 |
|
| 195 |
common_sleep(10); |
| 196 |
ck_pr_store_uint(&leave, 1); |
| 197 |
|
| 198 |
for (i = 0; i < n_threads; i++) |
| 199 |
pthread_join(threads[i], NULL); |
| 200 |
|
| 201 |
return (0); |
| 202 |
} |