Debian Codegraph @

regressions/ck_epoch/validate/ck_epoch_poll.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 100000
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, *n;
91
92 record = malloc(sizeof *record);
93 if (record == NULL)
94 ck_error("record allocation failure");
95
96 ck_epoch_register(&stack_epoch, record, NULL);
97
98 if (aff_iterate(&a)) {
99 perror("ERROR: failed to affine thread");
100 exit(EXIT_FAILURE);
101 }
102
103 ck_pr_inc_uint(&barrier);
104 while (ck_pr_load_uint(&barrier) < n_threads);
105
106 while (CK_STACK_ISEMPTY(&stack) == true) {
107 if (ck_pr_load_uint(&readers) != 0)
108 break;
109
110 ck_pr_stall();
111 }
112
113 j = 0;
114 for (;;) {
115 ck_epoch_begin(record, NULL);
116 CK_STACK_FOREACH(&stack, cursor) {
117 if (cursor == NULL)
118 continue;
119
120 n = CK_STACK_NEXT(cursor);
121 j += ck_pr_load_ptr(&n) != NULL;
122 }
123 ck_epoch_end(record, NULL);
124
125 if (j != 0 && ck_pr_load_uint(&readers) == 0)
126 ck_pr_store_uint(&readers, 1);
127
128 if (CK_STACK_ISEMPTY(&stack) == true &&
129 ck_pr_load_uint(&e_barrier) != 0)
130 break;
131 }
132
133 ck_pr_inc_uint(&e_barrier);
134 while (ck_pr_load_uint(&e_barrier) < n_threads);
135
136 fprintf(stderr, "[R] Observed entries: %u\n", j);
137 return (NULL);
138 }
139
140 static void *
141 write_thread(void *unused CK_CC_UNUSED)
142 {
143 struct node **entry, *e;
144 unsigned int i, j, tid;
145 ck_epoch_record_t *record;
146 ck_stack_entry_t *s;
147
148 record = malloc(sizeof *record);
149 if (record == NULL)
150 ck_error("record allocation failure");
151 ck_epoch_register(&stack_epoch, record, NULL);
152
153 if (aff_iterate(&a)) {
154 perror("ERROR: failed to affine thread");
155 exit(EXIT_FAILURE);
156 }
157
158 tid = ck_pr_faa_uint(&writers, 1);
159 ck_pr_inc_uint(&barrier);
160 while (ck_pr_load_uint(&barrier) < n_threads);
161
162 entry = malloc(sizeof(struct node *) * PAIRS_S);
163 if (entry == NULL) {
164 ck_error("Failed allocation.\n");
165 }
166
167 for (j = 0; j < ITERATE_S; j++) {
168 for (i = 0; i < PAIRS_S; i++) {
169 entry[i] = malloc(sizeof(struct node));
170 if (entry == NULL) {
171 ck_error("Failed individual allocation\n");
172 }
173 }
174
175 for (i = 0; i < PAIRS_S; i++) {
176 ck_stack_push_upmc(&stack, &entry[i]->stack_entry);
177 }
178
179 while (ck_pr_load_uint(&readers) == 0)
180 ck_pr_stall();
181
182 if (tid == 0) {
183 fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b[W] %2.2f: %c",
184 (double)j / ITERATE_S, animate[i % strlen(animate)]);
185 }
186
187 for (i = 0; i < PAIRS_S; i++) {
188 ck_epoch_begin(record, NULL);
189 s = ck_stack_pop_upmc(&stack);
190 e = stack_container(s);
191 ck_epoch_end(record, NULL);
192
193 ck_epoch_call(record, &e->epoch_entry, destructor);
194 ck_epoch_poll(record);
195 }
196 }
197
198 ck_epoch_barrier(record);
199
200 if (tid == 0) {
201 fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b[W] Peak: %u (%2.2f%%)\n Reclamations: %u\n\n",
202 record->n_peak,
203 (double)record->n_peak / ((double)PAIRS_S * ITERATE_S) * 100,
204 record->n_dispatch);
205 }
206
207 ck_pr_inc_uint(&e_barrier);
208 while (ck_pr_load_uint(&e_barrier) < n_threads);
209 return (NULL);
210 }
211
212 int
213 main(int argc, char *argv[])
214 {
215 unsigned int i;
216 pthread_t *threads;
217
218 if (argc != 4) {
219 ck_error("Usage: stack <#readers> <#writers> <affinity delta>\n");
220 }
221
222 n_rd = atoi(argv[1]);
223 n_wr = atoi(argv[2]);
224 n_threads = n_wr + n_rd;
225
226 a.delta = atoi(argv[3]);
227 a.request = 0;
228
229 threads = malloc(sizeof(pthread_t) * n_threads);
230 ck_epoch_init(&stack_epoch);
231
232 for (i = 0; i < n_rd; i++)
233 pthread_create(threads + i, NULL, read_thread, NULL);
234
235 do {
236 pthread_create(threads + i, NULL, write_thread, NULL);
237 } while (++i < n_wr + n_rd);
238
239 for (i = 0; i < n_threads; i++)
240 pthread_join(threads[i], NULL);
241
242 return (0);
243 }