Debian Codegraph @

regressions/ck_rwcohort/validate/validate.h

raw
1 /*
2 * Copyright 2013-2015 Samy Al Bahra.
3 * Copything 2013 Brendon Scheinman.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <errno.h>
29 #include <inttypes.h>
30 #include <pthread.h>
31 #include <math.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <strings.h>
36 #include <unistd.h>
37 #include <sys/time.h>
38
39 #include <ck_pr.h>
40 #include <ck_rwcohort.h>
41 #include <ck_spinlock.h>
42
43 #include "../../common.h"
44
45 #ifndef ITERATE
46 #define ITERATE 1000000
47 #endif
48
49
50 static struct affinity a;
51 static unsigned int locked;
52 static int nthr;
53 static ck_spinlock_fas_t global_fas_lock = CK_SPINLOCK_FAS_INITIALIZER;
54
55 static void
56 ck_spinlock_fas_lock_with_context(ck_spinlock_fas_t *lock, void *context)
57 {
58 (void)context;
59 ck_spinlock_fas_lock(lock);
60 }
61
62 static void
63 ck_spinlock_fas_unlock_with_context(ck_spinlock_fas_t *lock, void *context)
64 {
65 (void)context;
66 ck_spinlock_fas_unlock(lock);
67 }
68
69 static bool
70 ck_spinlock_fas_locked_with_context(ck_spinlock_fas_t *lock, void *context)
71 {
72 (void)context;
73 return ck_spinlock_fas_locked(lock);
74 }
75
76 CK_COHORT_PROTOTYPE(fas_fas,
77 ck_spinlock_fas_lock_with_context, ck_spinlock_fas_unlock_with_context, ck_spinlock_fas_locked_with_context,
78 ck_spinlock_fas_lock_with_context, ck_spinlock_fas_unlock_with_context, ck_spinlock_fas_locked_with_context)
79 LOCK_PROTOTYPE(fas_fas)
80
81 static CK_COHORT_INSTANCE(fas_fas) *cohorts;
82 static LOCK_INSTANCE(fas_fas) rw_cohort = LOCK_INITIALIZER;
83 static int n_cohorts;
84
85 static void *
86 thread(void *null CK_CC_UNUSED)
87 {
88 int i = ITERATE;
89 unsigned int l;
90 unsigned int core;
91 CK_COHORT_INSTANCE(fas_fas) *cohort;
92
93 if (aff_iterate_core(&a, &core)) {
94 perror("ERROR: Could not affine thread");
95 exit(EXIT_FAILURE);
96 }
97
98 cohort = cohorts + (core / (int)(a.delta)) % n_cohorts;
99
100 while (i--) {
101 WRITE_LOCK(fas_fas, &rw_cohort, cohort, NULL, NULL);
102 {
103 l = ck_pr_load_uint(&locked);
104 if (l != 0) {
105 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
106 }
107
108 ck_pr_inc_uint(&locked);
109 ck_pr_inc_uint(&locked);
110 ck_pr_inc_uint(&locked);
111 ck_pr_inc_uint(&locked);
112 ck_pr_inc_uint(&locked);
113 ck_pr_inc_uint(&locked);
114 ck_pr_inc_uint(&locked);
115 ck_pr_inc_uint(&locked);
116
117 l = ck_pr_load_uint(&locked);
118 if (l != 8) {
119 ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
120 }
121
122 ck_pr_dec_uint(&locked);
123 ck_pr_dec_uint(&locked);
124 ck_pr_dec_uint(&locked);
125 ck_pr_dec_uint(&locked);
126 ck_pr_dec_uint(&locked);
127 ck_pr_dec_uint(&locked);
128 ck_pr_dec_uint(&locked);
129 ck_pr_dec_uint(&locked);
130
131 l = ck_pr_load_uint(&locked);
132 if (l != 0) {
133 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
134 }
135 }
136 WRITE_UNLOCK(fas_fas, &rw_cohort, cohort, NULL, NULL);
137
138 READ_LOCK(fas_fas, &rw_cohort, cohort, NULL, NULL);
139 {
140 l = ck_pr_load_uint(&locked);
141 if (l != 0) {
142 ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
143 }
144 }
145 READ_UNLOCK(fas_fas, &rw_cohort, cohort, NULL, NULL);
146 }
147
148 return (NULL);
149 }
150
151 int
152 main(int argc, char *argv[])
153 {
154 pthread_t *threads;
155 int threads_per_cohort;
156 ck_spinlock_fas_t *local_lock;
157 int i;
158
159 if (argc != 4) {
160 ck_error("Usage: validate <number of cohorts> <threads per cohort> <affinity delta>\n");
161 }
162
163 n_cohorts = atoi(argv[1]);
164 if (n_cohorts <= 0) {
165 ck_error("ERROR: Number of cohorts must be greater than 0\n");
166 }
167
168 threads_per_cohort = atoi(argv[2]);
169 if (threads_per_cohort <= 0) {
170 ck_error("ERROR: Threads per cohort must be greater than 0\n");
171 }
172
173 nthr = n_cohorts * threads_per_cohort;
174
175 threads = malloc(sizeof(pthread_t) * nthr);
176 if (threads == NULL) {
177 ck_error("ERROR: Could not allocate thread structures\n");
178 }
179
180 a.delta = atoi(argv[3]);
181
182 fprintf(stderr, "Creating cohorts...");
183 cohorts = malloc(sizeof(CK_COHORT_INSTANCE(fas_fas)) * n_cohorts);
184 if (cohorts == NULL) {
185 ck_error("ERROR: Could not allocate base cohort structures\n");
186 }
187 for (i = 0 ; i < n_cohorts ; i++) {
188 local_lock = malloc(sizeof(ck_spinlock_fas_t));
189 CK_COHORT_INIT(fas_fas, cohorts + i, &global_fas_lock, local_lock,
190 CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT);
191 }
192 fprintf(stderr, "done\n");
193
194 fprintf(stderr, "Creating threads...");
195 for (i = 0; i < nthr; i++) {
196 if (pthread_create(&threads[i], NULL, thread, NULL)) {
197 ck_error("ERROR: Could not create thread %d\n", i);
198 }
199 }
200 fprintf(stderr, "done\n");
201
202 fprintf(stderr, "Waiting for threads to finish correctness regression...");
203 for (i = 0; i < nthr; i++)
204 pthread_join(threads[i], NULL);
205 fprintf(stderr, "done (passed)\n");
206
207 return (0);
208 }
209