1 /*
2 * Copyright 2011-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 <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_barrier.h>
40
41 #include "../../common.h"
42
43 #ifndef ITERATE
44 #define ITERATE 5000000
45 #endif
46
47 #ifndef ENTRIES
48 #define ENTRIES 512
49 #endif
50
51 static struct affinity a;
52 static int nthr;
53 static int counters[ENTRIES];
54 static ck_barrier_centralized_t barrier = CK_BARRIER_CENTRALIZED_INITIALIZER;
55 static int barrier_wait;
56
57 static void *
thread(void * null CK_CC_UNUSED)58 thread(void *null CK_CC_UNUSED)
59 {
60 ck_barrier_centralized_state_t state = CK_BARRIER_CENTRALIZED_STATE_INITIALIZER;
61 int j, counter;
62 int i = 0;
63
64 aff_iterate(&a);
65
66 ck_pr_inc_int(&barrier_wait);
67 while (ck_pr_load_int(&barrier_wait) != nthr)
68 ck_pr_stall();
69
70 for (j = 0; j < ITERATE; j++) {
71 i = j++ & (ENTRIES - 1);
72 ck_pr_inc_int(&counters[i]);
73 ck_barrier_centralized(&barrier, &state, nthr);
74 counter = ck_pr_load_int(&counters[i]);
75 if (counter != nthr * (j / ENTRIES + 1)) {
76 ck_error("FAILED [%d:%d]: %d != %d\n", i, j - 1, counter, nthr);
77 }
78 }
79
80 return (NULL);
81 }
82
83 int
main(int argc,char * argv[])84 main(int argc, char *argv[])
85 {
86 pthread_t *threads;
87 int i;
88
89 if (argc < 3) {
90 ck_error("Usage: correct <number of threads> <affinity delta>\n");
91 }
92
93 nthr = atoi(argv[1]);
94 if (nthr <= 0) {
95 ck_error("ERROR: Number of threads must be greater than 0\n");
96 }
97
98 threads = malloc(sizeof(pthread_t) * nthr);
99 if (threads == NULL) {
100 ck_error("ERROR: Could not allocate thread structures\n");
101 }
102
103 a.delta = atoi(argv[2]);
104
105 fprintf(stderr, "Creating threads (barrier)...");
106 for (i = 0; i < nthr; i++) {
107 if (pthread_create(&threads[i], NULL, thread, NULL)) {
108 ck_error("ERROR: Could not create thread %d\n", i);
109 }
110 }
111 fprintf(stderr, "done\n");
112
113 fprintf(stderr, "Waiting for threads to finish correctness regression...");
114 for (i = 0; i < nthr; i++)
115 pthread_join(threads[i], NULL);
116 fprintf(stderr, "done (passed)\n");
117
118
119 return (0);
120 }
121
122