Debian Codegraph @

regressions/ck_epoch/validate/ck_epoch_synchronize.c

raw
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 <string.h>
43 #include <ck_epoch.h>
44 #include <ck_stack.h>
45
46 #include "../../common.h"
47
48 static unsigned int n_rd;
49 static unsigned int n_wr;
50 static unsigned int n_threads;
51 static unsigned int barrier;
52 static unsigned int e_barrier;
53 static unsigned int readers;
54 static unsigned int writers;
55
56 #ifndef PAIRS_S
57 #define PAIRS_S 10000
58 #endif
59
60 #ifndef ITERATE_S
61 #define ITERATE_S 20
62 #endif
63
64 struct node {
65 unsigned int value;
66 ck_stack_entry_t stack_entry;
67 ck_epoch_entry_t epoch_entry;
68 };
69 static ck_stack_t stack = CK_STACK_INITIALIZER;
70 static ck_epoch_t stack_epoch;
71 CK_STACK_CONTAINER(struct node, stack_entry, stack_container)
72 CK_EPOCH_CONTAINER(struct node, epoch_entry, epoch_container)
73 static struct affinity a;
74 static const char animate[] = "-/|\\";
75
76 static void
77 destructor(ck_epoch_entry_t *p)
78 {
79 struct node *e = epoch_container(p);
80
81 free(e);
82 return;
83 }
84
85 static void *
86 read_thread(void *unused CK_CC_UNUSED)
87 {
88 unsigned int j;
89 ck_epoch_record_t *record CK_CC_CACHELINE;
90 ck_stack_entry_t *cursor;
91 ck_stack_entry_t *n;
92 unsigned int i;
93
94 record = malloc(sizeof *record);
95 if (record == NULL)
96 ck_error("record allocation failure");
97 ck_epoch_register(&stack_epoch, record, NULL);
98
99 if (aff_iterate(&a)) {
100 perror("ERROR: failed to affine thread");
101 exit(EXIT_FAILURE);
102 }
103
104 ck_pr_inc_uint(&barrier);
105 while (ck_pr_load_uint(&barrier) < n_threads);
106
107 while (CK_STACK_ISEMPTY(&stack) == true) {
108 if (ck_pr_load_uint(&readers) != 0)
109 break;
110
111 ck_pr_stall();
112 }
113
114 j = 0;
115 for (;;) {
116 i = 0;
117
118 ck_epoch_begin(record, NULL);
119 CK_STACK_FOREACH(&stack, cursor) {
120 if (cursor == NULL)
121 continue;
122
123 n = CK_STACK_NEXT(cursor);
124 j += ck_pr_load_ptr(&n) != NULL;
125
126 if (i++ > 4098)
127 break;
128 }
129 ck_epoch_end(record, NULL);
130
131 if (j != 0 && ck_pr_load_uint(&readers) == 0)
132 ck_pr_store_uint(&readers, 1);
133
134 if (CK_STACK_ISEMPTY(&stack) == true &&
135 ck_pr_load_uint(&e_barrier) != 0)
136 break;
137 }
138
139 ck_pr_inc_uint(&e_barrier);
140 while (ck_pr_load_uint(&e_barrier) < n_threads);
141
142 fprintf(stderr, "[R] Observed entries: %u\n", j);
143 return (NULL);
144 }
145
146 static void *
147 write_thread(void *unused CK_CC_UNUSED)
148 {
149 struct node **entry, *e;
150 unsigned int i, j, tid;
151 ck_epoch_record_t *record;
152 ck_stack_entry_t *s;
153
154 record = malloc(sizeof *record);
155 if (record == NULL)
156 ck_error("record allocation failure");
157 ck_epoch_register(&stack_epoch, record, NULL);
158
159 if (aff_iterate(&a)) {
160 perror("ERROR: failed to affine thread");
161 exit(EXIT_FAILURE);
162 }
163
164 tid = ck_pr_faa_uint(&writers, 1);
165 ck_pr_inc_uint(&barrier);
166 while (ck_pr_load_uint(&barrier) < n_threads);
167
168 entry = malloc(sizeof(struct node *) * PAIRS_S);
169 if (entry == NULL) {
170 ck_error("Failed allocation.\n");
171 }
172
173 for (j = 0; j < ITERATE_S; j++) {
174 for (i = 0; i < PAIRS_S; i++) {
175 entry[i] = malloc(sizeof(struct node));
176 if (entry == NULL) {
177 ck_error("Failed individual allocation\n");
178 }
179 }
180
181 for (i = 0; i < PAIRS_S; i++) {
182 ck_stack_push_upmc(&stack, &entry[i]->stack_entry);
183 }
184
185 while (ck_pr_load_uint(&readers) == 0)
186 ck_pr_stall();
187
188 for (i = 0; i < PAIRS_S; i++) {
189 ck_epoch_begin(record, NULL);
190 s = ck_stack_pop_upmc(&stack);
191 e = stack_container(s);
192 ck_epoch_end(record, NULL);
193
194 if (i & 1) {
195 ck_epoch_synchronize(record);
196 ck_epoch_reclaim(record);
197 ck_epoch_call(record, &e->epoch_entry, destructor);
198 } else {
199 ck_epoch_barrier(record);
200 destructor(&e->epoch_entry);
201 }
202
203 if (tid == 0 && (i % 16384) == 0) {
204 fprintf(stderr, "[W] %2.2f: %c\n",
205 (double)j / ITERATE_S, animate[i % strlen(animate)]);
206 }
207 }
208 }
209
210 ck_epoch_synchronize(record);
211
212 if (tid == 0) {
213 fprintf(stderr, "[W] Peak: %u (%2.2f%%)\n Reclamations: %u\n\n",
214 record->n_peak,
215 (double)record->n_peak / ((double)PAIRS_S * ITERATE_S) * 100,
216 record->n_dispatch);
217 }
218
219 ck_pr_inc_uint(&e_barrier);
220 while (ck_pr_load_uint(&e_barrier) < n_threads);
221 return (NULL);
222 }
223
224 int
225 main(int argc, char *argv[])
226 {
227 unsigned int i;
228 pthread_t *threads;
229
230 if (argc != 4) {
231 ck_error("Usage: stack <#readers> <#writers> <affinity delta>\n");
232 }
233
234 n_rd = atoi(argv[1]);
235 n_wr = atoi(argv[2]);
236 n_threads = n_wr + n_rd;
237
238 a.delta = atoi(argv[3]);
239 a.request = 0;
240
241 threads = malloc(sizeof(pthread_t) * n_threads);
242 ck_epoch_init(&stack_epoch);
243
244 for (i = 0; i < n_rd; i++)
245 pthread_create(threads + i, NULL, read_thread, NULL);
246
247 do {
248 pthread_create(threads + i, NULL, write_thread, NULL);
249 } while (++i < n_wr + n_rd);
250
251 for (i = 0; i < n_threads; i++)
252 pthread_join(threads[i], NULL);
253
254 return (0);
255 }