Debian Codegraph @

regressions/ck_swlock/validate/validate.c

raw
1 /*
2 * Copyright 2014 Jaidev Sridhar.
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 <math.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <strings.h>
35 #include <unistd.h>
36 #include <sys/time.h>
37
38 #include <ck_pr.h>
39 #include <ck_swlock.h>
40
41 #include "../../common.h"
42
43 #ifndef ITERATE
44 #define ITERATE 1000000
45 #endif
46
47 static struct affinity a;
48 static unsigned int locked;
49 static int nthr;
50 static ck_swlock_t lock = CK_SWLOCK_INITIALIZER;
51 static ck_swlock_t copy;
52 #ifdef CK_F_PR_RTM
53 static void *
54 thread_rtm_adaptive(void *arg)
55 {
56 unsigned int i = ITERATE;
57 unsigned int l;
58 int tid = ck_pr_load_int(arg);
59
60 struct ck_elide_config config = CK_ELIDE_CONFIG_DEFAULT_INITIALIZER;
61 struct ck_elide_stat st = CK_ELIDE_STAT_INITIALIZER;
62
63 if (aff_iterate(&a)) {
64 perror("ERROR: Could not affine thread");
65 exit(EXIT_FAILURE);
66 }
67
68 while (i--) {
69 if (tid == 0) {
70 CK_ELIDE_LOCK_ADAPTIVE(ck_swlock_write, &st, &config, &lock);
71 {
72 l = ck_pr_load_uint(&locked);
73 if (l != 0) {
74 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
75 }
76
77 ck_pr_inc_uint(&locked);
78 ck_pr_inc_uint(&locked);
79 ck_pr_inc_uint(&locked);
80 ck_pr_inc_uint(&locked);
81 ck_pr_inc_uint(&locked);
82 ck_pr_inc_uint(&locked);
83 ck_pr_inc_uint(&locked);
84 ck_pr_inc_uint(&locked);
85
86 l = ck_pr_load_uint(&locked);
87 if (l != 8) {
88 ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
89 }
90
91 ck_pr_dec_uint(&locked);
92 ck_pr_dec_uint(&locked);
93 ck_pr_dec_uint(&locked);
94 ck_pr_dec_uint(&locked);
95 ck_pr_dec_uint(&locked);
96 ck_pr_dec_uint(&locked);
97 ck_pr_dec_uint(&locked);
98 ck_pr_dec_uint(&locked);
99
100 l = ck_pr_load_uint(&locked);
101 if (l != 0) {
102 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
103 }
104 }
105 CK_ELIDE_UNLOCK_ADAPTIVE(ck_swlock_write, &st, &lock);
106 }
107
108 CK_ELIDE_LOCK(ck_swlock_read, &lock);
109 {
110 l = ck_pr_load_uint(&locked);
111 if (l != 0) {
112 ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
113 }
114 }
115 CK_ELIDE_UNLOCK(ck_swlock_read, &lock);
116 }
117
118 return NULL;
119 }
120
121 static void *
122 thread_rtm_mix(void *arg)
123 {
124 unsigned int i = ITERATE;
125 unsigned int l;
126 int tid = ck_pr_load_int(arg);
127
128 if (aff_iterate(&a)) {
129 perror("ERROR: Could not affine thread");
130 exit(EXIT_FAILURE);
131 }
132
133 while (i--) {
134 if (tid == 0) {
135 if (i & 1) {
136 CK_ELIDE_LOCK(ck_swlock_write, &lock);
137 } else {
138 ck_swlock_write_lock(&lock);
139 }
140
141 {
142 l = ck_pr_load_uint(&locked);
143 if (l != 0) {
144 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
145 }
146
147 ck_pr_inc_uint(&locked);
148 ck_pr_inc_uint(&locked);
149 ck_pr_inc_uint(&locked);
150 ck_pr_inc_uint(&locked);
151 ck_pr_inc_uint(&locked);
152 ck_pr_inc_uint(&locked);
153 ck_pr_inc_uint(&locked);
154 ck_pr_inc_uint(&locked);
155
156 l = ck_pr_load_uint(&locked);
157 if (l != 8) {
158 ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
159 }
160
161 ck_pr_dec_uint(&locked);
162 ck_pr_dec_uint(&locked);
163 ck_pr_dec_uint(&locked);
164 ck_pr_dec_uint(&locked);
165 ck_pr_dec_uint(&locked);
166 ck_pr_dec_uint(&locked);
167 ck_pr_dec_uint(&locked);
168 ck_pr_dec_uint(&locked);
169
170 l = ck_pr_load_uint(&locked);
171 if (l != 0) {
172 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
173 }
174 }
175
176 if (i & 1) {
177 CK_ELIDE_UNLOCK(ck_swlock_write, &lock);
178 } else {
179 ck_swlock_write_unlock(&lock);
180 }
181 }
182 if (i & 1) {
183 CK_ELIDE_LOCK(ck_swlock_read, &lock);
184 } else {
185 ck_swlock_read_lock(&lock);
186 }
187
188 {
189 l = ck_pr_load_uint(&locked);
190 if (l != 0) {
191 ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
192 }
193 }
194
195 if (i & 1) {
196 CK_ELIDE_UNLOCK(ck_swlock_read, &lock);
197 } else {
198 ck_swlock_read_unlock(&lock);
199 }
200 }
201
202 return (NULL);
203 }
204
205 static void *
206 thread_rtm(void *arg)
207 {
208 unsigned int i = ITERATE;
209 unsigned int l;
210 int tid = ck_pr_load_int(arg);
211
212 if (aff_iterate(&a)) {
213 perror("ERROR: Could not affine thread");
214 exit(EXIT_FAILURE);
215 }
216
217 while (i--) {
218 if (tid == 0) {
219 CK_ELIDE_LOCK(ck_swlock_write, &lock);
220 {
221 l = ck_pr_load_uint(&locked);
222 if (l != 0) {
223 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
224 }
225
226 ck_pr_inc_uint(&locked);
227 ck_pr_inc_uint(&locked);
228 ck_pr_inc_uint(&locked);
229 ck_pr_inc_uint(&locked);
230 ck_pr_inc_uint(&locked);
231 ck_pr_inc_uint(&locked);
232 ck_pr_inc_uint(&locked);
233 ck_pr_inc_uint(&locked);
234
235 l = ck_pr_load_uint(&locked);
236 if (l != 8) {
237 ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
238 }
239
240 ck_pr_dec_uint(&locked);
241 ck_pr_dec_uint(&locked);
242 ck_pr_dec_uint(&locked);
243 ck_pr_dec_uint(&locked);
244 ck_pr_dec_uint(&locked);
245 ck_pr_dec_uint(&locked);
246 ck_pr_dec_uint(&locked);
247 ck_pr_dec_uint(&locked);
248
249 l = ck_pr_load_uint(&locked);
250 if (l != 0) {
251 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
252 }
253 }
254 CK_ELIDE_UNLOCK(ck_swlock_write, &lock);
255 }
256
257 CK_ELIDE_LOCK(ck_swlock_read, &lock);
258 {
259 l = ck_pr_load_uint(&locked);
260 if (l != 0) {
261 ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
262 }
263 }
264 CK_ELIDE_UNLOCK(ck_swlock_read, &lock);
265 }
266
267 return (NULL);
268 }
269 #endif /* CK_F_PR_RTM */
270
271 static void *
272 thread_latch(void *arg)
273 {
274 unsigned int i = ITERATE;
275 unsigned int l;
276 int tid = ck_pr_load_int(arg);
277
278 if (aff_iterate(&a)) {
279 perror("ERROR: Could not affine thread");
280 exit(EXIT_FAILURE);
281 }
282
283 while (i--) {
284 if (tid == 0) {
285 /* Writer */
286 ck_swlock_write_latch(&lock);
287 {
288 memcpy(&copy, &lock, sizeof(ck_swlock_t));
289 l = ck_pr_load_uint(&locked);
290 if (l != 0) {
291 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
292 }
293
294 ck_pr_inc_uint(&locked);
295 ck_pr_inc_uint(&locked);
296 ck_pr_inc_uint(&locked);
297 ck_pr_inc_uint(&locked);
298 ck_pr_inc_uint(&locked);
299 ck_pr_inc_uint(&locked);
300 ck_pr_inc_uint(&locked);
301 ck_pr_inc_uint(&locked);
302
303 l = ck_pr_load_uint(&locked);
304 if (l != 8) {
305 ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
306 }
307
308 ck_pr_dec_uint(&locked);
309 ck_pr_dec_uint(&locked);
310 ck_pr_dec_uint(&locked);
311 ck_pr_dec_uint(&locked);
312 ck_pr_dec_uint(&locked);
313 ck_pr_dec_uint(&locked);
314 ck_pr_dec_uint(&locked);
315 ck_pr_dec_uint(&locked);
316
317 l = ck_pr_load_uint(&locked);
318 if (l != 0) {
319 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
320 }
321 memcpy(&lock, &copy, sizeof(ck_swlock_t));
322 }
323 ck_swlock_write_unlatch(&lock);
324 }
325
326 ck_swlock_read_lock(&lock);
327 {
328 l = ck_pr_load_uint(&locked);
329 if (l != 0) {
330 ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
331 }
332 }
333 ck_swlock_read_unlock(&lock);
334 }
335
336 return (NULL);
337 }
338
339 static void *
340 thread(void *arg)
341 {
342 unsigned int i = ITERATE;
343 unsigned int l;
344 int tid = ck_pr_load_int(arg);
345
346 if (aff_iterate(&a)) {
347 perror("ERROR: Could not affine thread");
348 exit(EXIT_FAILURE);
349 }
350
351 while (i--) {
352 if (tid == 0) {
353 /* Writer */
354 ck_swlock_write_lock(&lock);
355 {
356 l = ck_pr_load_uint(&locked);
357 if (l != 0) {
358 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
359 }
360
361 ck_pr_inc_uint(&locked);
362 ck_pr_inc_uint(&locked);
363 ck_pr_inc_uint(&locked);
364 ck_pr_inc_uint(&locked);
365 ck_pr_inc_uint(&locked);
366 ck_pr_inc_uint(&locked);
367 ck_pr_inc_uint(&locked);
368 ck_pr_inc_uint(&locked);
369
370 l = ck_pr_load_uint(&locked);
371 if (l != 8) {
372 ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
373 }
374
375 ck_pr_dec_uint(&locked);
376 ck_pr_dec_uint(&locked);
377 ck_pr_dec_uint(&locked);
378 ck_pr_dec_uint(&locked);
379 ck_pr_dec_uint(&locked);
380 ck_pr_dec_uint(&locked);
381 ck_pr_dec_uint(&locked);
382 ck_pr_dec_uint(&locked);
383
384 l = ck_pr_load_uint(&locked);
385 if (l != 0) {
386 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
387 }
388 }
389 ck_swlock_write_unlock(&lock);
390 }
391
392 ck_swlock_read_lock(&lock);
393 {
394 l = ck_pr_load_uint(&locked);
395 if (l != 0) {
396 ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
397 }
398 }
399 ck_swlock_read_unlock(&lock);
400 }
401
402 return (NULL);
403 }
404
405 static void
406 swlock_test(pthread_t *threads, void *(*f)(void *), const char *test)
407 {
408 int i, tid[nthr];
409
410 fprintf(stderr, "Creating threads (%s)...", test);
411 for (i = 0; i < nthr; i++) {
412 ck_pr_store_int(&tid[i], i);
413 if (pthread_create(&threads[i], NULL, f, &tid[i])) {
414 ck_error("ERROR: Could not create thread %d\n", i);
415 }
416 }
417 fprintf(stderr, ".");
418
419 for (i = 0; i < nthr; i++)
420 pthread_join(threads[i], NULL);
421 fprintf(stderr, "done (passed)\n");
422 return;
423 }
424
425 int
426 main(int argc, char *argv[])
427 {
428 pthread_t *threads;
429
430 if (argc != 3) {
431 ck_error("Usage: validate <number of threads> <affinity delta>\n");
432 }
433
434 nthr = atoi(argv[1]);
435 if (nthr <= 0) {
436 ck_error("ERROR: Number of threads must be greater than 0\n");
437 }
438
439 threads = malloc(sizeof(pthread_t) * nthr);
440 if (threads == NULL) {
441 ck_error("ERROR: Could not allocate thread structures\n");
442 }
443
444 a.delta = atoi(argv[2]);
445
446 swlock_test(threads, thread, "regular");
447 swlock_test(threads, thread_latch, "latch");
448 #ifdef CK_F_PR_RTM
449 swlock_test(threads, thread_rtm, "rtm");
450 swlock_test(threads, thread_rtm_mix, "rtm-mix");
451 swlock_test(threads, thread_rtm_adaptive, "rtm-adaptive");
452 #endif
453 return 0;
454 }
455