1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
#include <apr_thread_proc.h>
18
#include <apr_errno.h>
19
#include <apr_general.h>
20
#include <apr_getopt.h>
21
#include <apr_strings.h>
29
#include <apr_portable.h>
30
#include "apr_queue.h"
36
"This program won't work on this platform because there is no "
37
"support for threads.\n");
40
#else /* !APR_HAS_THREADS */
43
int consumer_activity=400;
44
int producer_activity=300;
46
static void * APR_THREAD_FUNC consumer(apr_thread_t *thd, void *data);
47
static void * APR_THREAD_FUNC producer(apr_thread_t *thd, void *data);
48
static void usage(void);
50
static void * APR_THREAD_FUNC consumer(apr_thread_t *thd, void *data)
53
apr_queue_t *q = (apr_queue_t*)data;
57
char current_thread_str[30];
58
apr_os_thread_t current_thread = apr_os_thread_current();
60
apr_snprintf(current_thread_str, sizeof current_thread_str,
61
"%pT", ¤t_thread);
63
sleeprate = 1000000/consumer_activity;
64
apr_sleep( (rand() % 4 ) * 1000000 ); /* sleep random seconds */
67
rv = apr_queue_pop(q, &v);
68
if (rv == APR_EINTR) {
69
fprintf(stderr, "%s\tconsumer intr\n", current_thread_str);
72
} while (rv == APR_EINTR) ;
73
if (rv != APR_SUCCESS) {
75
fprintf(stderr, "%s\tconsumer:queue terminated APR_EOF\n", current_thread_str);
79
fprintf(stderr, "%s\tconsumer thread exit rv %d\n", current_thread_str, rv);
80
apr_thread_exit(thd, rv);
85
fprintf(stderr, "%s\tpop %d\n", current_thread_str, val);
86
apr_sleep( sleeprate ); /* sleep this long to acheive our rate */
92
static void * APR_THREAD_FUNC producer(apr_thread_t *thd, void *data)
96
apr_queue_t *q = (apr_queue_t*)data;
99
char current_thread_str[30];
100
apr_os_thread_t current_thread = apr_os_thread_current();
102
apr_snprintf(current_thread_str, sizeof current_thread_str,
103
"%pT", ¤t_thread);
105
sleeprate = 1000000/producer_activity;
106
apr_sleep( (rand() % 4 ) * 1000000 ); /* sleep random seconds */
109
val = apr_palloc(context, sizeof(int));
112
fprintf(stderr, "%s\tpush %d\n", current_thread_str, *val);
114
rv = apr_queue_push(q, val);
116
fprintf(stderr, "%s\tproducer intr\n", current_thread_str);
117
} while (rv == APR_EINTR);
119
if (rv != APR_SUCCESS) {
121
fprintf(stderr, "%s\tproducer: queue terminated APR_EOF\n", current_thread_str);
125
fprintf(stderr, "%s\tproducer thread exit rv %d\n", current_thread_str, rv);
126
apr_thread_exit(thd, rv);
130
apr_sleep( sleeprate ); /* sleep this long to acheive our rate */
136
static void usage(void)
138
fprintf(stderr,"usage: testqueue -p n -P n -c n -C n -q n -s n\n");
139
fprintf(stderr,"-c # of consumer\n");
140
fprintf(stderr,"-C amount they consumer before dying\n");
141
fprintf(stderr,"-p # of producers\n");
142
fprintf(stderr,"-P amount they produce before dying\n");
143
fprintf(stderr,"-q queue size\n");
144
fprintf(stderr,"-s amount of time to sleep before killing it\n");
145
fprintf(stderr,"-v verbose\n");
148
int main(int argc, const char* const argv[])
164
srand((unsigned int)apr_time_now());
165
printf("APR Queue Test\n======================\n\n");
167
printf("%-60s", "Initializing the context");
168
if (apr_pool_create(&context, NULL) != APR_SUCCESS) {
170
fprintf(stderr, "Failed.\nCould not initialize\n");
175
apr_getopt_init(&opt, context, argc, argv);
176
while ((rv = apr_getopt(opt, "p:c:P:C:q:s:v", &c, &optarg))
180
numconsumers = atoi( optarg);
183
numproducers = atoi( optarg);
186
consumer_activity = atoi( optarg);
189
producer_activity = atoi( optarg);
192
sleeptime= atoi(optarg);
195
queuesize = atoi(optarg);
205
/* bad cmdline option? then we die */
206
if (rv != APR_EOF || opt->ind < opt->argc) {
213
printf("test stats %d consumers (rate %d/sec) %d producers (rate %d/sec) queue size %d sleep %d\n",
214
numconsumers,consumer_activity, numproducers, producer_activity, queuesize,sleeptime);
215
printf("%-60s", "Initializing the queue");
216
rv = apr_queue_create(&queue, queuesize, context);
218
if (rv != APR_SUCCESS) {
220
fprintf(stderr, "Failed\nCould not create queue %d\n",rv);
221
apr_strerror(rv, errorbuf,200);
222
fprintf(stderr,"%s\n",errorbuf);
227
t = apr_palloc( context, sizeof(apr_thread_t*) * (numconsumers+numproducers));
228
printf("%-60s", "Starting consumers");
229
for (i=0;i<numconsumers;i++) {
230
rv = apr_thread_create(&t[i], NULL, consumer, queue, context);
231
if (rv != APR_SUCCESS) {
232
apr_strerror(rv, errorbuf,200);
233
fprintf(stderr, "Failed\nError starting consumer thread (%d) rv=%d:%s\n",i, rv,errorbuf);
238
for (i=numconsumers;i<(numconsumers+numproducers);i++) {
239
rv = apr_thread_create(&t[i], NULL, producer, queue, context);
240
if (rv != APR_SUCCESS) {
241
apr_strerror(rv, errorbuf,200);
242
fprintf(stderr, "Failed\nError starting producer thread (%d) rv=%d:%s\n",i, rv,errorbuf);
249
printf("%-60s", "Sleeping\n");
250
apr_sleep( sleeptime * 1000000 ); /* sleep 10 seconds */
253
printf("%-60s", "Terminating queue");
254
rv = apr_queue_term(queue);
255
if (rv != APR_SUCCESS) {
256
apr_strerror(rv, errorbuf,200);
257
fprintf( stderr, "apr_queue_term failed %d:%s\n",rv,errorbuf);
262
printf("%-60s", "Waiting for threads to exit\n");
264
for (i=0;i<numconsumers+numproducers;i++) {
265
apr_thread_join(&rv, t[i]);
267
apr_strerror(rv, errorbuf,200);
269
fprintf( stderr, "consumer thread %d failed rv %d:%s\n",i,rv,errorbuf);
271
fprintf( stderr, "producer thread %d failed rv %d:%s\n",i,rv,errorbuf);
282
#endif /* !APR_HAS_THREADS */