1 /*
2 * Copyright 2009 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 <assert.h>
28 #include <ck_cc.h>
29 #include <ck_pr.h>
30 #ifdef SPINLOCK
31 #include <ck_spinlock.h>
32 #endif
33 #include <ck_stack.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <pthread.h>
40 #include <sys/time.h>
41 #include <unistd.h>
42
43 #include "../../common.h"
44
45 #ifndef ITEMS
46 #define ITEMS (5765760)
47 #endif
48
49 #define TVTOD(tv) ((tv).tv_sec+((tv).tv_usec / (double)1000000))
50
51 struct entry {
52 int value;
53 #if defined(SPINLOCK) || defined(PTHREADS)
54 struct entry *next;
55 #else
56 ck_stack_entry_t next;
57 #endif
58 } CK_CC_CACHELINE;
59
60 #ifdef SPINLOCK
61 static struct entry *stack CK_CC_CACHELINE;
62 ck_spinlock_fas_t stack_spinlock = CK_SPINLOCK_FAS_INITIALIZER;
63 #define UNLOCK ck_spinlock_fas_unlock
64 #if defined(EB)
65 #define LOCK ck_spinlock_fas_lock_eb
66 #else
67 #define LOCK ck_spinlock_fas_lock
68 #endif
69 #elif defined(PTHREADS)
70 static struct entry *stack CK_CC_CACHELINE;
71 pthread_mutex_t stack_spinlock = PTHREAD_MUTEX_INITIALIZER;
72 #define LOCK pthread_mutex_lock
73 #define UNLOCK pthread_mutex_unlock
74 #else
75 static ck_stack_t stack CK_CC_CACHELINE;
76 CK_STACK_CONTAINER(struct entry, next, getvalue)
77 #endif
78
79 static struct affinity affinerator;
80 static unsigned long long nthr;
81 static volatile unsigned int barrier = 0;
82 static unsigned int critical;
83
84 static void *
stack_thread(void * buffer)85 stack_thread(void *buffer)
86 {
87 #if (defined(MPMC) && defined(CK_F_STACK_POP_MPMC)) || (defined(UPMC) && defined(CK_F_STACK_POP_UPMC)) || (defined(TRYUPMC) && defined(CK_F_STACK_TRYPOP_UPMC)) || (defined(TRYMPMC) && defined(CK_F_STACK_TRYPOP_MPMC))
88 ck_stack_entry_t *ref;
89 #endif
90 struct entry *entry = buffer;
91 unsigned long long i, n = ITEMS;
92 unsigned int seed;
93 int j;
94
95 if (aff_iterate(&affinerator)) {
96 perror("ERROR: failed to affine thread");
97 exit(EXIT_FAILURE);
98 }
99
100 while (barrier == 0);
101
102 for (i = 0; i < n; i++) {
103 #if defined(MPMC)
104 ck_stack_push_mpmc(&stack, &entry->next);
105 #elif defined(TRYMPMC)
106 while (ck_stack_trypush_mpmc(&stack, &entry->next) == false)
107 ck_pr_stall();
108 #elif defined(UPMC)
109 ck_stack_push_upmc(&stack, &entry->next);
110 #elif defined(TRYUPMC)
111 while (ck_stack_trypush_upmc(&stack, &entry->next) == false)
112 ck_pr_stall();
113 #elif defined(SPINLOCK) || defined(PTHREADS)
114 LOCK(&stack_spinlock);
115 ck_pr_store_ptr(&entry->next, stack);
116 ck_pr_store_ptr(&stack, entry);
117 UNLOCK(&stack_spinlock);
118 #else
119 # error Undefined operation.
120 #endif
121
122 if (critical) {
123 j = common_rand_r(&seed) % critical;
124 while (j--)
125 __asm__ __volatile__("" ::: "memory");
126 }
127
128 #if defined(MPMC)
129 #ifdef CK_F_STACK_POP_MPMC
130 ref = ck_stack_pop_mpmc(&stack);
131 entry = getvalue(ref);
132 #endif
133 #elif defined(TRYMPMC)
134 #ifdef CK_F_STACK_TRYPOP_MPMC
135 while (ck_stack_trypop_mpmc(&stack, &ref) == false)
136 ck_pr_stall();
137 entry = getvalue(ref);
138 #endif /* CK_F_STACK_TRYPOP_MPMC */
139 #elif defined(UPMC)
140 ref = ck_stack_pop_upmc(&stack);
141 entry = getvalue(ref);
142 #elif defined(SPINLOCK) || defined(PTHREADS)
143 LOCK(&stack_spinlock);
144 entry = stack;
145 stack = stack->next;
146 UNLOCK(&stack_spinlock);
147 #else
148 # error Undefined operation.
149 #endif
150 }
151
152 return (NULL);
153 }
154
155 static void
stack_assert(void)156 stack_assert(void)
157 {
158
159 #if defined(SPINLOCK) || defined(PTHREADS)
160 assert(stack == NULL);
161 #else
162 assert(CK_STACK_ISEMPTY(&stack));
163 #endif
164 return;
165 }
166
167 int
main(int argc,char * argv[])168 main(int argc, char *argv[])
169 {
170 struct entry *bucket;
171 unsigned long long i, d;
172 pthread_t *thread;
173 struct timeval stv, etv;
174
175 #if (defined(TRYMPMC) || defined(MPMC)) && (!defined(CK_F_STACK_PUSH_MPMC) || !defined(CK_F_STACK_POP_MPMC))
176 fprintf(stderr, "Unsupported.\n");
177 return 0;
178 #endif
179
180 if (argc != 4) {
181 ck_error("Usage: stack <threads> <delta> <critical>\n");
182 }
183
184 {
185 char *e;
186
187 nthr = strtol(argv[1], &e, 10);
188 if (errno == ERANGE) {
189 perror("ERROR: too many threads");
190 exit(EXIT_FAILURE);
191 } else if (*e != '\0') {
192 ck_error("ERROR: input format is incorrect\n");
193 }
194
195 d = strtol(argv[2], &e, 10);
196 if (errno == ERANGE) {
197 perror("ERROR: delta is too large");
198 exit(EXIT_FAILURE);
199 } else if (*e != '\0') {
200 ck_error("ERROR: input format is incorrect\n");
201 }
202
203 critical = strtoul(argv[3], &e, 10);
204 if (errno == ERANGE) {
205 perror("ERROR: critical section is too large");
206 exit(EXIT_FAILURE);
207 } else if (*e != '\0') {
208 ck_error("ERROR: input format is incorrect\n");
209 }
210 }
211
212 srand(getpid());
213
214 affinerator.request = 0;
215 affinerator.delta = d;
216
217 bucket = malloc(sizeof(struct entry) * nthr);
218 assert(bucket != NULL);
219
220 thread = malloc(sizeof(pthread_t) * nthr);
221 assert(thread != NULL);
222
223 for (i = 0; i < nthr; i++)
224 pthread_create(&thread[i], NULL, stack_thread, bucket + i);
225
226 barrier = 1;
227
228 for (i = 0; i < nthr; i++)
229 pthread_join(thread[i], NULL);
230
231 barrier = 0;
232
233 for (i = 0; i < nthr; i++)
234 pthread_create(&thread[i], NULL, stack_thread, bucket + i);
235
236 common_gettimeofday(&stv, NULL);
237 barrier = 1;
238 for (i = 0; i < nthr; i++)
239 pthread_join(thread[i], NULL);
240 common_gettimeofday(&etv, NULL);
241
242 stack_assert();
243 #ifdef _WIN32
244 printf("%3llu %.6f\n", nthr, TVTOD(etv) - TVTOD(stv));
245 #else
246 printf("%3llu %.6lf\n", nthr, TVTOD(etv) - TVTOD(stv));
247 #endif
248 return 0;
249 }
250