2 * Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
3 * Original Author: Hans Boehm
5 * This file may be redistributed and/or modified under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2, or (at your option) any later version.
9 * It is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License in the
12 * file doc/COPYING for more details.
15 #if defined(HAVE_CONFIG_H)
22 #include "atomic_ops.h"
23 #include "atomic_ops_stack.h"
26 # define MAX_NTHREADS 100
31 # include <sys/time.h>
32 /* Need 64-bit long long support */
33 long long get_msecs(void)
38 return (long long)tv.tv_sec * 1000 + tv.tv_usec/1000;
41 # define get_msecs() 0
49 AO_stack_t the_list = AO_STACK_INITIALIZER;
51 void add_elements(int n)
56 le = malloc(sizeof(list_element));
59 fprintf(stderr, "Out of memory\n");
63 AO_stack_push(&the_list, (AO_t *)le);
70 for (p = (list_element *)AO_REAL_HEAD_PTR(the_list);
72 p = (list_element *)AO_REAL_NEXT_PTR(p -> next))
73 printf("%d\n", p -> data);
76 static char marks[MAX_NTHREADS * MAX_NTHREADS];
78 void check_list(int n)
83 for (i = 1; i <= n; ++i) marks[i] = 0;
84 for (p = (list_element *)AO_REAL_HEAD_PTR(the_list);
86 p = (list_element *)AO_REAL_NEXT_PTR(p -> next))
88 if (p -> data > n || p -> data <= 0)
89 fprintf(stderr, "Found erroneous list element %d\n", p -> data);
90 if (marks[p -> data] != 0)
91 fprintf(stderr, "Found duplicate list element %d\n", p -> data);
94 for (i = 1; i <= n; ++i)
96 fprintf(stderr, "Missing list element %d\n", i);
99 volatile AO_t ops_performed = 0;
101 #define LIMIT 1000000
102 /* Total number of push/pop ops in all threads per test. */
104 #ifdef AO_HAVE_fetch_and_add
105 # define fetch_and_add(addr, val) AO_fetch_and_add(addr, val)
107 /* Fake it. This is really quite unacceptable for timing */
108 /* purposes. But as a correctness test, it should be OK. */
109 AO_INLINE AO_t fetch_and_add(volatile AO_t * addr, AO_t val)
111 AO_t result = AO_load(addr);
112 AO_store(addr, result + val);
117 void * run_one_test(void * arg)
119 list_element * t[MAX_NTHREADS + 1];
120 long index = (long)arg;
125 printf("starting thread %d\n", index);
127 while (fetch_and_add(&ops_performed, index + 1) + index + 1 < LIMIT)
129 for (i = 0; i < index + 1; ++i)
131 t[i] = (list_element *)AO_stack_pop(&the_list);
134 fprintf(stderr, "FAILED\n");
138 for (i = 0; i < index + 1; ++i)
140 AO_stack_push(&the_list, (AO_t *)t[i]);
145 printf("finished thread %d: %d total ops\n", index, j);
150 #ifndef N_EXPERIMENTS
151 # define N_EXPERIMENTS 1
154 unsigned long times[MAX_NTHREADS + 1][N_EXPERIMENTS];
156 int main(int argc, char **argv)
166 max_nthreads = atoi(argv[1]);
167 if (max_nthreads < 1 || max_nthreads > MAX_NTHREADS)
169 fprintf(stderr, "Invalid max # of threads argument\n");
175 fprintf(stderr, "Usage: %s [max # of threads]\n", argv[0]);
178 for (exper_n = 0; exper_n < N_EXPERIMENTS; ++ exper_n)
179 for (nthreads = 1; nthreads <= max_nthreads; ++nthreads)
182 pthread_t thread[MAX_NTHREADS];
183 int list_length = nthreads*(nthreads+1)/2;
184 long long start_time;
187 add_elements(list_length);
189 printf("Initial list (nthreads = %d):\n", nthreads);
193 start_time = get_msecs();
194 for (i = 1; i < nthreads; ++i) {
197 if ((code = pthread_create(thread+i, 0, run_one_test,
198 (void *)(long)i)) != 0) {
199 fprintf(stderr, "Thread creation failed %u\n", code);
203 /* We use the main thread to run one test. This allows gprof */
204 /* profiling to work, for example. */
206 for (i = 1; i < nthreads; ++i) {
208 if ((code = pthread_join(thread[i], 0)) != 0) {
209 fprintf(stderr, "Thread join failed %u\n", code);
212 times[nthreads][exper_n] = (unsigned long)(get_msecs() - start_time);
214 printf("%d %lu\n", nthreads,
215 (unsigned long)(get_msecs() - start_time));
216 printf("final list (should be reordered initial list):\n");
219 check_list(list_length);
220 while ((le = (list_element *)AO_stack_pop(&the_list)) != 0)
224 for (nthreads = 1; nthreads <= max_nthreads; ++nthreads)
226 unsigned long sum = 0;
228 printf("About %d pushes + %d pops in %d threads:",
229 LIMIT, LIMIT, nthreads);
230 for (exper_n = 0; exper_n < N_EXPERIMENTS; ++exper_n)
232 # if defined(VERBOSE)
233 printf("[%lu] ", times[nthreads][exper_n]);
235 sum += times[nthreads][exper_n];
237 printf(" %lu msecs\n", (sum + N_EXPERIMENTS/2)/N_EXPERIMENTS);
239 # endif /* !NO_TIMES */