"Fossies" - the Fresh Open Source Software Archive

Member "apr-1.7.0/test/testlockperf.c" (7 Apr 2017, 10191 Bytes) of package /linux/www/apr-1.7.0.tar.bz2:


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. See also the latest Fossies "Diffs" side-by-side code changes report for "testlockperf.c": 1.6.5_vs_1.7.0.

    1 /* Licensed to the Apache Software Foundation (ASF) under one or more
    2  * contributor license agreements.  See the NOTICE file distributed with
    3  * this work for additional information regarding copyright ownership.
    4  * The ASF licenses this file to You under the Apache License, Version 2.0
    5  * (the "License"); you may not use this file except in compliance with
    6  * the License.  You may obtain a copy of the License at
    7  *
    8  *     http://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 
   17 #include "apr_thread_proc.h"
   18 #include "apr_thread_mutex.h"
   19 #include "apr_thread_rwlock.h"
   20 #include "apr_file_io.h"
   21 #include "apr_errno.h"
   22 #include "apr_general.h"
   23 #include "apr_getopt.h"
   24 #include "errno.h"
   25 #include <stdio.h>
   26 #include <stdlib.h>
   27 #include "testutil.h"
   28 
   29 #if !APR_HAS_THREADS
   30 int main(void)
   31 {
   32     printf("This program won't work on this platform because there is no "
   33            "support for threads.\n");
   34     return 0;
   35 }
   36 #else /* !APR_HAS_THREADS */
   37 
   38 #define DEFAULT_MAX_COUNTER 1000000
   39 #define MAX_THREADS 6
   40 
   41 static int verbose = 0;
   42 static long mutex_counter;
   43 static long max_counter = DEFAULT_MAX_COUNTER;
   44 
   45 static apr_thread_mutex_t *thread_lock;
   46 void * APR_THREAD_FUNC thread_mutex_func(apr_thread_t *thd, void *data);
   47 apr_status_t test_thread_mutex(int num_threads); /* apr_thread_mutex_t */
   48 
   49 static apr_thread_rwlock_t *thread_rwlock;
   50 void * APR_THREAD_FUNC thread_rwlock_func(apr_thread_t *thd, void *data);
   51 apr_status_t test_thread_rwlock(int num_threads); /* apr_thread_rwlock_t */
   52 
   53 int test_thread_mutex_nested(int num_threads);
   54 
   55 apr_pool_t *pool;
   56 int i = 0, x = 0;
   57 
   58 void * APR_THREAD_FUNC thread_mutex_func(apr_thread_t *thd, void *data)
   59 {
   60     int i;
   61 
   62     for (i = 0; i < max_counter; i++) {
   63         if (data) {
   64             apr_thread_mutex_timedlock(thread_lock, *(apr_interval_time_t *)data);
   65         }
   66         else {
   67             apr_thread_mutex_lock(thread_lock);
   68         }
   69         mutex_counter++;
   70         apr_thread_mutex_unlock(thread_lock);
   71     }
   72     return NULL;
   73 }
   74 
   75 void * APR_THREAD_FUNC thread_rwlock_func(apr_thread_t *thd, void *data)
   76 {
   77     int i;
   78 
   79     for (i = 0; i < max_counter; i++) {
   80         apr_thread_rwlock_wrlock(thread_rwlock);
   81         mutex_counter++;
   82         apr_thread_rwlock_unlock(thread_rwlock);
   83     }
   84     return NULL;
   85 }
   86 
   87 int test_thread_mutex(int num_threads)
   88 {
   89     apr_thread_t *t[MAX_THREADS];
   90     apr_status_t s[MAX_THREADS];
   91     apr_time_t time_start, time_stop;
   92     int i;
   93 
   94     mutex_counter = 0;
   95 
   96     printf("apr_thread_mutex_t Tests\n");
   97     printf("%-60s", "    Initializing the apr_thread_mutex_t (UNNESTED)");
   98     s[0] = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_UNNESTED, pool);
   99     if (s[0] != APR_SUCCESS) {
  100         printf("Failed!\n");
  101         return s[0];
  102     }
  103     printf("OK\n");
  104 
  105     apr_thread_mutex_lock(thread_lock);
  106     /* set_concurrency(4)? -aaron */
  107     printf("    Starting %d threads    ", num_threads); 
  108     for (i = 0; i < num_threads; ++i) {
  109         s[i] = apr_thread_create(&t[i], NULL, thread_mutex_func, NULL, pool);
  110         if (s[i] != APR_SUCCESS) {
  111             printf("Failed!\n");
  112             return s[i];
  113         }
  114     }
  115     printf("OK\n");
  116 
  117     time_start = apr_time_now();
  118     apr_thread_mutex_unlock(thread_lock);
  119 
  120     /* printf("%-60s", "    Waiting for threads to exit"); */
  121     for (i = 0; i < num_threads; ++i) {
  122         apr_thread_join(&s[i], t[i]);
  123     }
  124     /* printf("OK\n"); */
  125 
  126     time_stop = apr_time_now();
  127     printf("microseconds: %" APR_INT64_T_FMT " usec\n",
  128            (time_stop - time_start));
  129     if (mutex_counter != max_counter * num_threads)
  130         printf("error: counter = %ld\n", mutex_counter);
  131 
  132     return APR_SUCCESS;
  133 }
  134 
  135 int test_thread_mutex_nested(int num_threads)
  136 {
  137     apr_thread_t *t[MAX_THREADS];
  138     apr_status_t s[MAX_THREADS];
  139     apr_time_t time_start, time_stop;
  140     int i;
  141 
  142     mutex_counter = 0;
  143 
  144     printf("apr_thread_mutex_t Tests\n");
  145     printf("%-60s", "    Initializing the apr_thread_mutex_t (NESTED)");
  146     s[0] = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_NESTED, pool);
  147     if (s[0] != APR_SUCCESS) {
  148         printf("Failed!\n");
  149         return s[0];
  150     }
  151     printf("OK\n");
  152 
  153     apr_thread_mutex_lock(thread_lock);
  154     /* set_concurrency(4)? -aaron */
  155     printf("    Starting %d threads    ", num_threads); 
  156     for (i = 0; i < num_threads; ++i) {
  157         s[i] = apr_thread_create(&t[i], NULL, thread_mutex_func, NULL, pool);
  158         if (s[i] != APR_SUCCESS) {
  159             printf("Failed!\n");
  160             return s[i];
  161         }
  162     }
  163     printf("OK\n");
  164 
  165     time_start = apr_time_now();
  166     apr_thread_mutex_unlock(thread_lock);
  167 
  168     /* printf("%-60s", "    Waiting for threads to exit"); */
  169     for (i = 0; i < num_threads; ++i) {
  170         apr_thread_join(&s[i], t[i]);
  171     }
  172     /* printf("OK\n"); */
  173 
  174     time_stop = apr_time_now();
  175     printf("microseconds: %" APR_INT64_T_FMT " usec\n",
  176            (time_stop - time_start));
  177     if (mutex_counter != max_counter * num_threads)
  178         printf("error: counter = %ld\n", mutex_counter);
  179 
  180     return APR_SUCCESS;
  181 }
  182 
  183 static int test_thread_mutex_timed(int num_threads)
  184 {
  185     apr_thread_t *t[MAX_THREADS];
  186     apr_status_t s[MAX_THREADS];
  187     apr_time_t time_start, time_stop;
  188     apr_time_t timeout;
  189     int i;
  190 
  191     mutex_counter = 0;
  192 
  193     timeout = apr_time_from_sec(5);
  194 
  195     printf("apr_thread_mutex_t Tests\n");
  196     printf("%-60s", "    Initializing the apr_thread_mutex_t (TIMED)");
  197     s[0] = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_TIMED, pool);
  198     if (s[0] != APR_SUCCESS) {
  199         printf("Failed!\n");
  200         return s[0];
  201     }
  202     printf("OK\n");
  203 
  204     apr_thread_mutex_lock(thread_lock);
  205     /* set_concurrency(4)? -aaron */
  206     printf("    Starting %d threads    ", num_threads); 
  207     for (i = 0; i < num_threads; ++i) {
  208         s[i] = apr_thread_create(&t[i], NULL, thread_mutex_func, &timeout, pool);
  209         if (s[i] != APR_SUCCESS) {
  210             printf("Failed!\n");
  211             return s[i];
  212         }
  213     }
  214     printf("OK\n");
  215 
  216     time_start = apr_time_now();
  217     apr_thread_mutex_unlock(thread_lock);
  218 
  219     /* printf("%-60s", "    Waiting for threads to exit"); */
  220     for (i = 0; i < num_threads; ++i) {
  221         apr_thread_join(&s[i], t[i]);
  222     }
  223     /* printf("OK\n"); */
  224 
  225     time_stop = apr_time_now();
  226     printf("microseconds: %" APR_INT64_T_FMT " usec\n",
  227            (time_stop - time_start));
  228     if (mutex_counter != max_counter * num_threads)
  229         printf("error: counter = %ld\n", mutex_counter);
  230 
  231     return APR_SUCCESS;
  232 }
  233 
  234 int test_thread_rwlock(int num_threads)
  235 {
  236     apr_thread_t *t[MAX_THREADS];
  237     apr_status_t s[MAX_THREADS];
  238     apr_time_t time_start, time_stop;
  239     int i;
  240 
  241     mutex_counter = 0;
  242 
  243     printf("apr_thread_rwlock_t Tests\n");
  244     printf("%-60s", "    Initializing the apr_thread_rwlock_t");
  245     s[0] = apr_thread_rwlock_create(&thread_rwlock, pool);
  246     if (s[0] != APR_SUCCESS) {
  247         printf("Failed!\n");
  248         return s[0];
  249     }
  250     printf("OK\n");
  251 
  252     apr_thread_rwlock_wrlock(thread_rwlock);
  253     /* set_concurrency(4)? -aaron */
  254     printf("    Starting %d threads    ", num_threads); 
  255     for (i = 0; i < num_threads; ++i) {
  256         s[i] = apr_thread_create(&t[i], NULL, thread_rwlock_func, NULL, pool);
  257         if (s[i] != APR_SUCCESS) {
  258             printf("Failed!\n");
  259             return s[i];
  260         }
  261     }
  262     printf("OK\n");
  263 
  264     time_start = apr_time_now();
  265     apr_thread_rwlock_unlock(thread_rwlock);
  266 
  267     /* printf("%-60s", "    Waiting for threads to exit"); */
  268     for (i = 0; i < num_threads; ++i) {
  269         apr_thread_join(&s[i], t[i]);
  270     }
  271     /* printf("OK\n"); */
  272 
  273     time_stop = apr_time_now();
  274     printf("microseconds: %" APR_INT64_T_FMT " usec\n",
  275            (time_stop - time_start));
  276     if (mutex_counter != max_counter * num_threads)
  277         printf("error: counter = %ld\n", mutex_counter);
  278 
  279     return APR_SUCCESS;
  280 }
  281 
  282 int main(int argc, const char * const *argv)
  283 {
  284     apr_status_t rv;
  285     char errmsg[200];
  286     apr_getopt_t *opt;
  287     char optchar;
  288     const char *optarg;
  289 
  290     printf("APR Lock Performance Test\n==============\n\n");
  291         
  292     apr_initialize();
  293     atexit(apr_terminate);
  294 
  295     if (apr_pool_create(&pool, NULL) != APR_SUCCESS)
  296         exit(-1);
  297 
  298     if ((rv = apr_getopt_init(&opt, pool, argc, argv)) != APR_SUCCESS) {
  299         fprintf(stderr, "Could not set up to parse options: [%d] %s\n",
  300                 rv, apr_strerror(rv, errmsg, sizeof errmsg));
  301         exit(-1);
  302     }
  303         
  304     while ((rv = apr_getopt(opt, "c:v", &optchar, &optarg)) == APR_SUCCESS) {
  305         if (optchar == 'c') {
  306             max_counter = atol(optarg);
  307         }
  308         else if (optchar == 'v') {
  309             verbose = 1;
  310         }
  311     }
  312 
  313     if (rv != APR_SUCCESS && rv != APR_EOF) {
  314         fprintf(stderr, "Could not parse options: [%d] %s\n",
  315                 rv, apr_strerror(rv, errmsg, sizeof errmsg));
  316         exit(-1);
  317     }
  318 
  319     for (i = 1; i <= MAX_THREADS; ++i) {
  320         if ((rv = test_thread_mutex(i)) != APR_SUCCESS) {
  321             fprintf(stderr,"thread_mutex test failed : [%d] %s\n",
  322                     rv, apr_strerror(rv, (char*)errmsg, 200));
  323             exit(-3);
  324         }
  325 
  326         if ((rv = test_thread_mutex_nested(i)) != APR_SUCCESS) {
  327             fprintf(stderr,"thread_mutex (NESTED) test failed : [%d] %s\n",
  328                     rv, apr_strerror(rv, (char*)errmsg, 200));
  329             exit(-4);
  330         }
  331 
  332         if ((rv = test_thread_mutex_timed(i)) != APR_SUCCESS) {
  333             fprintf(stderr,"thread_mutex (TIMED) test failed : [%d] %s\n",
  334                     rv, apr_strerror(rv, (char*)errmsg, 200));
  335             exit(-5);
  336         }
  337 
  338         if ((rv = test_thread_rwlock(i)) != APR_SUCCESS) {
  339             fprintf(stderr,"thread_rwlock test failed : [%d] %s\n",
  340                     rv, apr_strerror(rv, (char*)errmsg, 200));
  341             exit(-6);
  342         }
  343     }
  344 
  345     return 0;
  346 }
  347 
  348 #endif /* !APR_HAS_THREADS */