"Fossies" - the Fresh Open Source Software Archive

Member "openpa-1.0.4/test/opa_test.h" (5 Dec 2012, 6356 Bytes) of package /linux/misc/openpa-1.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
    2 /*
    3  *  (C) 2008 by Argonne National Laboratory.
    4  *      See COPYRIGHT in top-level directory.
    5  */
    6 
    7 #ifndef OPA_TEST_NAIVE
    8 #  include "opa_primitives.h"
    9 #else /* OPA_TEST_NAIVE */
   10 #  define OPA_PRIMITIVES_H_INCLUDED
   11 #  include "opa_config.h"
   12 #  include "opa_util.h"
   13 #  ifndef _opa_inline
   14 #    define _opa_inline inline
   15 #  endif
   16 #endif /* OPA_TEST_NAIVE */
   17 #include <assert.h>
   18 #include <stdio.h>
   19 #include <stdlib.h>
   20 #include <limits.h>
   21 #include <time.h>
   22 #if defined(OPA_HAVE_PTHREAD_H)
   23 #  include <pthread.h>
   24 #endif /* HAVE_PTHREAD_H */
   25 
   26 /* Define the macro to use for yielding the current thread (to others) */
   27 #if defined(OPA_HAVE_PTHREAD_YIELD)
   28 #  define OPA_TEST_YIELD() pthread_yield()
   29 #elif defined(OPA_HAVE_SCHED_YIELD)
   30 #  include <sched.h>
   31 #  define OPA_TEST_YIELD() (void) sched_yield()
   32 #else
   33 #  define OPA_TEST_YIELD() (void) 0
   34 #endif
   35 
   36 /*
   37  * Naive redefinition of OPA functions as simple C operations.  These should
   38  * pass all the "simple" tests, but fail on the threaded tests (with >1 thread),
   39  * unless the tested operation is natively atomic.
   40  */
   41 #ifdef OPA_TEST_NAIVE
   42 
   43 #define OPA_UNIVERSAL_PRIMITIVE OPA_CAS
   44 #define OPA_LL_SC_SUPPORTED 1
   45 
   46 typedef volatile int OPA_int_t;
   47 typedef void * volatile OPA_ptr_t;
   48 
   49 #define OPA_load_int(A) (*(A))
   50 #define OPA_store_int(A, B) ((void) (*(A) = (B)))
   51 #define OPA_load_ptr(A) (*(A))
   52 #define OPA_store_ptr(A, B) ((void) (*(A) = (B)))
   53 
   54 #define OPA_add_int(A, B) ((void) (*(A) += (B)))
   55 #define OPA_incr_int(A) ((void) (++(*(A))))
   56 #define OPA_decr_int(A) ((void) (--(*(A))))
   57 
   58 #define OPA_decr_and_test_int(A) (0 == --(*(A)))
   59 static _opa_inline int OPA_fetch_and_add_int(OPA_int_t *ptr, int val)
   60 {
   61     int prev = *ptr;
   62     *ptr += val;
   63     return prev;
   64 }
   65 #define OPA_fetch_and_incr_int(A) ((*(A))++)
   66 #define OPA_fetch_and_decr_int(A) ((*(A))--)
   67 
   68 #define OPA_cas_ptr(A, B, C) (*(A) == (B) ? (*(A) = (C), (B)) : *(A))
   69 #define OPA_cas_int(A, B, C) (*(A) == (B) ? (*(A) = (C), (B)) : *(A))
   70 
   71 static _opa_inline void *OPA_swap_ptr(OPA_ptr_t *ptr, void *val)
   72 {
   73     void *prev;
   74     prev = *ptr;
   75     *ptr = val;
   76     return prev;
   77 }
   78 
   79 static _opa_inline int OPA_swap_int(OPA_int_t *ptr, int val)
   80 {
   81     int prev;
   82     prev = *ptr;
   83     *ptr = val;
   84     return prev;
   85 }
   86 
   87 #define OPA_write_barrier() ((void) 0)
   88 #define OPA_read_barrier() ((void) 0)
   89 #define OPA_read_write_barrier() ((void) 0)
   90 
   91 /* For LL/SC only use load/store.  This is more naive than the above
   92  * implementation for CAS */
   93 #define OPA_LL_int OPA_load_int
   94 #define OPA_SC_int(A, B) (OPA_store_int(A, B), 1)
   95 #define OPA_LL_ptr OPA_load_ptr
   96 #define OPA_SC_ptr(A, B) (OPA_store_ptr(A, B), 1)
   97 
   98 #if defined(OPA_HAVE_SCHED_YIELD)
   99 #  include <sched.h>
  100 #  define OPA_busy_wait() sched_yield()
  101 #else
  102 #  define OPA_busy_wait() do { } while (0)
  103 #endif
  104 
  105 #endif /* OPA_TEST_NAIVE */
  106 
  107 /*
  108  * Cache line padding.  See note in opa_queue.h.
  109  */
  110 #define OPA_TEST_CACHELINE_PADDING 128
  111 
  112 /*
  113  * Print the current location on the standard output stream.
  114  */
  115 #define AT()            printf ("        at %s:%d in %s()...\n",              \
  116                                 __FILE__, __LINE__, __FUNCTION__)
  117 
  118 /*
  119  * The name of the test is printed by saying TESTING("something") which will
  120  * result in the string `Testing something' being flushed to standard output.
  121  * If a test passes, fails, or is skipped then the PASSED(), FAILED(), or
  122  * SKIPPED() macro should be called.  After HFAILED() or SKIPPED() the caller
  123  * should print additional information to stdout indented by at least four
  124  * spaces.
  125  */
  126 #define TESTING(WHAT, NTHREADS)                                                \
  127 do {                                                                           \
  128     if(NTHREADS) {                                                             \
  129         int nwritten_chars;                                                    \
  130         printf("Testing %s with %d thread%s %n",                               \
  131                 WHAT,                                                          \
  132                 (int) NTHREADS,                                                \
  133                 (NTHREADS) > 1 ? "s" : "",                                     \
  134                 &nwritten_chars);                                              \
  135         printf("%*s", nwritten_chars > 70 ? 0 : 70 - nwritten_chars, "");      \
  136     } /* end if */                                                             \
  137     else                                                                       \
  138         printf("Testing %-62s", WHAT);                                         \
  139     fflush(stdout);                                                            \
  140 } while(0)
  141 #define PASSED()        do {puts(" PASSED");fflush(stdout);} while(0)
  142 #define FAILED()        do {puts("*FAILED*");fflush(stdout);} while(0)
  143 #define WARNING()       do {puts("*WARNING*");fflush(stdout);} while(0)
  144 #define SKIPPED()       do {puts(" -SKIP-");fflush(stdout);} while(0)
  145 #define TEST_ERROR      do {FAILED(); AT(); goto error;} while(0)
  146 #define FAIL_OP_ERROR(OP) do {FAILED(); AT(); OP; goto error;} while(0)
  147 #define OP_SUPPRESS(OP, COUNTER, LIMIT) do {                                   \
  148     if((COUNTER) <= (LIMIT)) {                                                 \
  149         OP;                                                                    \
  150         if((COUNTER) == (LIMIT))                                               \
  151             puts("    Suppressing further output...");                         \
  152         fflush(stdout);                                                        \
  153     } /* end if */                                                             \
  154 } while(0)
  155 
  156 /*
  157  * Array of number of threads.  Each threaded test is run once for each entry in
  158  * this array.  Remove the last test if OPA_LIMIT_THREADS is defined.
  159  */
  160 static const unsigned num_threads[] = {1, 2, 4, 10, 100};
  161 static const unsigned num_thread_tests = sizeof(num_threads) / sizeof(num_threads[0])
  162 #if OPA_MAX_NTHREADS < 10
  163 - 2
  164 #elif OPA_MAX_NTHREADS < 100
  165 - 1
  166 #endif /* OPA_LIMIT_THREADS */
  167 ;
  168 
  169 /*
  170  * Factor to reduce the number of iterations by for each test.  Must be the same
  171  * size as num_threads.
  172  */
  173 static const unsigned iter_reduction[] = {1, 1, 1, 1, 10};
  174 
  175 /*
  176  * Other global variables.
  177  */
  178 static unsigned curr_test;   /* Current test number bein run */
  179