| 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 <errno.h> |
| 28 |
#include <inttypes.h> |
| 29 |
#include <pthread.h> |
| 30 |
#include <stdint.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <strings.h> |
| 34 |
#include <unistd.h> |
| 35 |
#include <sys/time.h> |
| 36 |
|
| 37 |
#include <ck_backoff.h> |
| 38 |
#include <ck_cc.h> |
| 39 |
#include <ck_pr.h> |
| 40 |
#include <stdbool.h> |
| 41 |
#include <stddef.h> |
| 42 |
#include <ck_epoch.h> |
| 43 |
#include <ck_stack.h> |
| 44 |
|
| 45 |
#include "../../common.h" |
| 46 |
|
| 47 |
static unsigned int n_threads; |
| 48 |
static unsigned int barrier; |
| 49 |
static unsigned int e_barrier; |
| 50 |
|
| 51 |
#ifndef PAIRS |
| 52 |
#define PAIRS 5000000 |
| 53 |
#endif |
| 54 |
|
| 55 |
struct node { |
| 56 |
unsigned int value; |
| 57 |
ck_epoch_entry_t epoch_entry; |
| 58 |
ck_stack_entry_t stack_entry; |
| 59 |
}; |
| 60 |
static ck_stack_t stack = {NULL, NULL}; |
| 61 |
static ck_epoch_t stack_epoch; |
| 62 |
CK_STACK_CONTAINER(struct node, stack_entry, stack_container) |
| 63 |
CK_EPOCH_CONTAINER(struct node, epoch_entry, epoch_container) |
| 64 |
static struct affinity a; |
| 65 |
|
| 66 |
static void |
| 67 |
destructor(ck_epoch_entry_t *p) |
| 68 |
{ |
| 69 |
struct node *e = epoch_container(p); |
| 70 |
|
| 71 |
free(e); |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
static void * |
| 76 |
thread(void *unused CK_CC_UNUSED) |
| 77 |
{ |
| 78 |
struct node **entry, *e; |
| 79 |
ck_epoch_record_t record; |
| 80 |
ck_stack_entry_t *s; |
| 81 |
unsigned long smr = 0; |
| 82 |
unsigned int i; |
| 83 |
|
| 84 |
ck_epoch_register(&stack_epoch, &record, NULL); |
| 85 |
|
| 86 |
if (aff_iterate(&a)) { |
| 87 |
perror("ERROR: failed to affine thread"); |
| 88 |
exit(EXIT_FAILURE); |
| 89 |
} |
| 90 |
|
| 91 |
entry = malloc(sizeof(struct node *) * PAIRS); |
| 92 |
if (entry == NULL) { |
| 93 |
ck_error("Failed allocation.\n"); |
| 94 |
} |
| 95 |
|
| 96 |
for (i = 0; i < PAIRS; i++) { |
| 97 |
entry[i] = malloc(sizeof(struct node)); |
| 98 |
if (entry == NULL) { |
| 99 |
ck_error("Failed individual allocation\n"); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
ck_pr_inc_uint(&barrier); |
| 104 |
while (ck_pr_load_uint(&barrier) < n_threads); |
| 105 |
|
| 106 |
for (i = 0; i < PAIRS; i++) { |
| 107 |
ck_epoch_begin(&record, NULL); |
| 108 |
ck_stack_push_upmc(&stack, &entry[i]->stack_entry); |
| 109 |
s = ck_stack_pop_upmc(&stack); |
| 110 |
ck_epoch_end(&record, NULL); |
| 111 |
|
| 112 |
e = stack_container(s); |
| 113 |
ck_epoch_call(&record, &e->epoch_entry, destructor); |
| 114 |
smr += ck_epoch_poll(&record) == false; |
| 115 |
} |
| 116 |
|
| 117 |
ck_pr_inc_uint(&e_barrier); |
| 118 |
while (ck_pr_load_uint(&e_barrier) < n_threads); |
| 119 |
|
| 120 |
fprintf(stderr, "Deferrals: %lu (%2.2f)\n", smr, (double)smr / PAIRS); |
| 121 |
fprintf(stderr, "Peak: %u (%2.2f%%), %u pending\nReclamations: %u\n\n", |
| 122 |
record.n_peak, |
| 123 |
(double)record.n_peak / PAIRS * 100, |
| 124 |
record.n_pending, |
| 125 |
record.n_dispatch); |
| 126 |
|
| 127 |
ck_epoch_barrier(&record); |
| 128 |
ck_pr_inc_uint(&e_barrier); |
| 129 |
while (ck_pr_load_uint(&e_barrier) < (n_threads << 1)); |
| 130 |
|
| 131 |
if (record.n_pending != 0) { |
| 132 |
ck_error("ERROR: %u pending, expecting none.\n", |
| 133 |
record.n_pending); |
| 134 |
} |
| 135 |
|
| 136 |
return (NULL); |
| 137 |
} |
| 138 |
|
| 139 |
int |
| 140 |
main(int argc, char *argv[]) |
| 141 |
{ |
| 142 |
unsigned int i; |
| 143 |
pthread_t *threads; |
| 144 |
|
| 145 |
if (argc != 3) { |
| 146 |
ck_error("Usage: stack <threads> <affinity delta>\n"); |
| 147 |
} |
| 148 |
|
| 149 |
n_threads = atoi(argv[1]); |
| 150 |
a.delta = atoi(argv[2]); |
| 151 |
a.request = 0; |
| 152 |
|
| 153 |
threads = malloc(sizeof(pthread_t) * n_threads); |
| 154 |
|
| 155 |
ck_epoch_init(&stack_epoch); |
| 156 |
|
| 157 |
for (i = 0; i < n_threads; i++) |
| 158 |
pthread_create(threads + i, NULL, thread, NULL); |
| 159 |
|
| 160 |
for (i = 0; i < n_threads; i++) |
| 161 |
pthread_join(threads[i], NULL); |
| 162 |
|
| 163 |
return (0); |
| 164 |
} |