1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
/*
* Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#if defined(HAVE_CONFIG_H)
# include "config.h"
#endif
#include <stdio.h>
#if defined(__vxworks)
int main(void)
{
printf("test skipped\n");
return 0;
}
#else
#if ((defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)) \
|| defined(_MSC_VER) || defined(_WIN32_WINCE)) \
&& !defined(AO_USE_WIN32_PTHREADS)
# define USE_WINTHREADS
#endif
#ifdef USE_WINTHREADS
# include <windows.h>
#else
# include <pthread.h>
#endif
#include <assert.h>
#include <stdlib.h>
#include "atomic_ops_stack.h" /* includes atomic_ops.h as well */
#if (defined(_WIN32_WCE) || defined(__MINGW32CE__)) && !defined(AO_HAVE_abort)
# define abort() _exit(-1) /* there is no abort() in WinCE */
#endif
#ifndef MAX_NTHREADS
# define MAX_NTHREADS 100
#endif
#ifndef DEFAULT_NTHREADS
# define DEFAULT_NTHREADS 16 /* must be <= MAX_NTHREADS */
#endif
#ifdef NO_TIMES
# define get_msecs() 0
#elif (defined(USE_WINTHREADS) || defined(AO_USE_WIN32_PTHREADS)) \
&& !defined(CPPCHECK)
# include <sys/timeb.h>
static unsigned long get_msecs(void)
{
struct timeb tb;
ftime(&tb);
return (unsigned long)tb.time * 1000 + tb.millitm;
}
#else /* Unix */
# include <time.h>
# include <sys/time.h>
static unsigned long get_msecs(void)
{
struct timeval tv;
gettimeofday(&tv, 0);
return (unsigned long)tv.tv_sec * 1000 + tv.tv_usec/1000;
}
#endif /* !NO_TIMES */
struct le {
AO_uintptr_t next; /* must be the first field */
int data;
};
typedef union le_u {
AO_uintptr_t next;
struct le e;
} list_element;
#if defined(CPPCHECK)
static AO_stack_t the_list; /* to test AO_stack_init() */
#else
static AO_stack_t the_list = AO_STACK_INITIALIZER;
#endif
/* Add elements from 1 to n to the list (1 is pushed first). */
/* This is called from a single thread only. */
static void add_elements(int n)
{
list_element * le;
if (n == 0) return;
add_elements(n-1);
le = (list_element *)malloc(sizeof(list_element));
if (NULL == le) {
fprintf(stderr, "Out of memory\n");
exit(2);
}
# if defined(CPPCHECK)
le->e.next = 0; /* mark field as used */
# endif
le->e.data = n;
# if defined(CPPCHECK)
(void)le->next;
AO_stack_push(&the_list, (AO_uintptr_t *)le);
#else
AO_stack_push(&the_list, &le->next);
# endif
}
#ifdef VERBOSE_STACK
static void print_list(void)
{
AO_uintptr_t *p;
for (p = AO_REAL_HEAD_PTR(the_list);
p != NULL; p = AO_REAL_NEXT_PTR(*p))
printf("%d\n", ((list_element *)p)->e.data);
}
#endif /* VERBOSE_STACK */
/* Check that the list contains only values from 1 to n, in any order, */
/* w/o duplications. Executed when the list mutation is finished. */
static void check_list(int n)
{
AO_uintptr_t *p;
int i;
int err_cnt = 0;
char *marks = (char*)calloc(n + 1, 1);
if (NULL == marks) {
fprintf(stderr, "Out of memory (marks)\n");
exit(2);
}
for (p = AO_REAL_HEAD_PTR(the_list);
p != NULL; p = AO_REAL_NEXT_PTR(*p)) {
i = ((list_element *)p)->e.data;
if (i > n || i <= 0) {
fprintf(stderr, "Found erroneous list element %d\n", i);
err_cnt++;
} else if (marks[i] != 0) {
fprintf(stderr, "Found duplicate list element %d\n", i);
abort();
} else {
marks[i] = 1;
}
}
for (i = 1; i <= n; ++i)
if (marks[i] != 1) {
fprintf(stderr, "Missing list element %d\n", i);
err_cnt++;
}
free(marks);
if (err_cnt > 0) abort();
}
static volatile AO_t ops_performed = 0;
#ifndef LIMIT
/* Total number of push/pop ops in all threads per test. */
# ifdef AO_USE_PTHREAD_DEFS
# define LIMIT 20000
# else
# define LIMIT 1000000
# endif
#endif
#ifdef AO_HAVE_fetch_and_add
# define fetch_then_add(addr, val) AO_fetch_and_add(addr, val)
#else
/* OK to perform it in two atomic steps, but really quite */
/* unacceptable for timing purposes. */
AO_INLINE AO_t fetch_then_add(volatile AO_t * addr, AO_t val)
{
AO_t result = AO_load(addr);
AO_store(addr, result + val);
return result;
}
#endif
#ifdef USE_WINTHREADS
static DWORD WINAPI run_one_test(LPVOID arg)
#else
static void * run_one_test(void * arg)
#endif
{
AO_uintptr_t *t[MAX_NTHREADS + 1];
unsigned index = (unsigned)(AO_uintptr_t)arg;
unsigned i;
# ifdef VERBOSE_STACK
unsigned j = 0;
printf("starting thread %u\n", index);
# endif
assert(index <= MAX_NTHREADS);
while (fetch_then_add(&ops_performed, index + 1) + index + 1 < LIMIT) {
/* Pop index+1 elements (where index is the thread's one), then */
/* push them back (in the same order of operations). */
/* Note that this is done in parallel by many threads. */
for (i = 0; i <= index; ++i) {
t[i] = AO_stack_pop(&the_list);
if (NULL == t[i]) {
/* This should not happen as at most n*(n+1)/2 elements */
/* could be popped off at a time. */
fprintf(stderr, "Failed - nothing to pop\n");
abort();
}
}
for (i = 0; i <= index; ++i) {
AO_stack_push(&the_list, t[i]);
}
# ifdef VERBOSE_STACK
j += index + 1;
# endif
}
/* Repeat until LIMIT push/pop operations are performed (by all */
/* the threads simultaneously). */
# ifdef VERBOSE_STACK
printf("finished thread %u: %u total ops\n", index, j);
# endif
return 0;
}
#ifndef N_EXPERIMENTS
# define N_EXPERIMENTS 1
#endif
static unsigned long times[MAX_NTHREADS + 1][N_EXPERIMENTS];
static void run_one_experiment(int max_nthreads, int exper_n)
{
int nthreads;
assert(max_nthreads <= MAX_NTHREADS);
assert(exper_n < N_EXPERIMENTS);
for (nthreads = 1; nthreads <= max_nthreads; ++nthreads) {
unsigned i;
# ifdef USE_WINTHREADS
DWORD thread_id;
HANDLE thread[MAX_NTHREADS];
# else
pthread_t thread[MAX_NTHREADS];
# endif
int list_length = nthreads*(nthreads+1)/2;
unsigned long start_time;
AO_uintptr_t *le;
# ifdef VERBOSE_STACK
printf("Before add_elements: exper_n=%d, nthreads=%d,"
" max_nthreads=%d, list_length=%d\n",
exper_n, nthreads, max_nthreads, list_length);
# endif
/* Create a list with n*(n+1)/2 elements. */
assert(0 == AO_REAL_HEAD_PTR(the_list));
add_elements(list_length);
# ifdef VERBOSE_STACK
printf("Initial list (nthreads = %d):\n", nthreads);
print_list();
# endif
ops_performed = 0;
start_time = get_msecs();
/* Start n-1 threads to run_one_test in parallel. */
for (i = 1; (int)i < nthreads; ++i) {
int code;
# ifdef USE_WINTHREADS
thread[i] = CreateThread(NULL, 0, run_one_test,
(LPVOID)(AO_uintptr_t)i, 0, &thread_id);
code = thread[i] != NULL ? 0 : (int)GetLastError();
# else
code = pthread_create(&thread[i], 0, run_one_test,
(void *)(AO_uintptr_t)i);
# endif
if (code != 0) {
fprintf(stderr, "Thread creation failed %u\n", (unsigned)code);
exit(3);
}
}
/* We use the main thread to run one test. This allows */
/* gprof profiling to work, for example. */
run_one_test(0);
/* Wait for all the threads to complete. */
for (i = 1; (int)i < nthreads; ++i) {
int code;
# ifdef USE_WINTHREADS
code = WaitForSingleObject(thread[i], INFINITE) == WAIT_OBJECT_0 ?
0 : (int)GetLastError();
# else
code = pthread_join(thread[i], 0);
# endif
if (code != 0) {
fprintf(stderr, "Thread join failed %u\n", (unsigned)code);
abort();
}
}
times[nthreads][exper_n] = get_msecs() - start_time;
# ifdef VERBOSE_STACK
printf("nthreads=%d, time_ms=%lu\n",
nthreads, times[nthreads][exper_n]);
printf("final list (should be reordered initial list):\n");
print_list();
# endif
/* Ensure that no element is lost or duplicated. */
check_list(list_length);
/* And, free the entire list. */
while ((le = AO_stack_pop(&the_list)) != NULL)
free(le);
/* Retry with larger n values. */
}
}
static void run_all_experiments(int max_nthreads)
{
int exper_n;
for (exper_n = 0; exper_n < N_EXPERIMENTS; ++exper_n)
run_one_experiment(max_nthreads, exper_n);
}
/* Output the performance statistic. */
static void output_stat(int max_nthreads)
{
int nthreads;
assert(max_nthreads <= MAX_NTHREADS);
for (nthreads = 1; nthreads <= max_nthreads; ++nthreads) {
# ifndef NO_TIMES
int exper_n;
unsigned long sum = 0;
# endif
printf("About %d pushes + %d pops in %d threads:",
LIMIT, LIMIT, nthreads);
# ifndef NO_TIMES
for (exper_n = 0; exper_n < N_EXPERIMENTS; ++exper_n) {
# ifdef VERBOSE_STACK
printf(" [%lums]", times[nthreads][exper_n]);
# endif
sum += times[nthreads][exper_n];
}
printf(" %lu msecs\n", (sum + N_EXPERIMENTS/2)/N_EXPERIMENTS);
# else
printf(" completed\n");
# endif
}
}
int main(int argc, char **argv)
{
int max_nthreads = DEFAULT_NTHREADS;
if (2 == argc) {
max_nthreads = atoi(argv[1]);
if (max_nthreads < 1 || max_nthreads > MAX_NTHREADS) {
fprintf(stderr, "Invalid max # of threads argument\n");
exit(1);
}
} else if (argc > 2) {
fprintf(stderr, "Usage: %s [max # of threads]\n", argv[0]);
exit(1);
}
if (!AO_stack_is_lock_free())
printf("Use almost-lock-free implementation\n");
# if defined(CPPCHECK)
AO_stack_init(&the_list);
(void)AO_stack_next_ptr(0);
# endif
run_all_experiments(max_nthreads);
output_stat(max_nthreads);
return 0;
}
#endif
|