1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
Just a test application for threads.
29
#include <sys/types.h>
31
#include <sys/types.h>
36
#include <string.h> /* Pull in memset() */
45
#define snprintf _snprintf
48
#include <boost/scoped_ptr.hpp>
52
#define DEFAULT_INITIAL_LOAD 10000
53
#define DEFAULT_EXECUTE_SECONDS 120
54
#define TEST_FILENAME "concurrency_test.az"
56
#define HUGE_STRING_LENGTH 8192
58
/* Global Thread counter */
59
unsigned int thread_counter;
60
pthread_mutex_t counter_mutex;
61
pthread_cond_t count_threshhold;
62
unsigned int master_wakeup;
63
pthread_mutex_t sleeper_mutex;
64
pthread_cond_t sleep_threshhold;
65
static bool timer_alarm= false;
66
pthread_mutex_t timer_alarm_mutex;
67
pthread_cond_t timer_alarm_threshold;
69
pthread_mutex_t row_lock;
73
void *run_concurrent_task(void *p);
74
void *timer_thread(void *p);
76
void scheduler(az_method use_aio);
77
void create_data_file(azio_stream *write_handler, uint64_t rows);
78
unsigned int write_row(azio_stream *s);
80
typedef struct thread_context_st thread_context_st;
81
struct thread_context_st {
82
unsigned int how_often_to_write;
88
/* Use this for string generation */
89
static const char ALPHANUMERICS[]=
90
"0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
92
#define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
94
static void get_random_string(char *buffer, size_t size)
96
char *buffer_ptr= buffer;
99
*buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
100
*buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
103
int main(int argc, char *argv[])
107
drizzled::internal::my_init();
116
pthread_mutex_init(&counter_mutex, NULL);
117
pthread_cond_init(&count_threshhold, NULL);
118
pthread_mutex_init(&sleeper_mutex, NULL);
119
pthread_cond_init(&sleep_threshhold, NULL);
120
pthread_mutex_init(&timer_alarm_mutex, NULL);
121
pthread_cond_init(&timer_alarm_threshold, NULL);
122
pthread_mutex_init(&row_lock, NULL);
124
for (method= AZ_METHOD_BLOCK; method < AZ_METHOD_MAX; method++)
125
scheduler((az_method)method);
127
(void)pthread_mutex_destroy(&counter_mutex);
128
(void)pthread_cond_destroy(&count_threshhold);
129
(void)pthread_mutex_destroy(&sleeper_mutex);
130
(void)pthread_cond_destroy(&sleep_threshhold);
131
pthread_mutex_destroy(&timer_alarm_mutex);
132
pthread_cond_destroy(&timer_alarm_threshold);
133
pthread_mutex_destroy(&row_lock);
138
void scheduler(az_method use_aio)
142
boost::scoped_ptr<azio_stream> writer_handle_ap(new azio_stream);
143
azio_stream &writer_handle= *writer_handle_ap.get();
144
thread_context_st *context;
145
pthread_t mainthread; /* Thread descriptor */
146
pthread_attr_t attr; /* Thread attributes */
148
pthread_attr_init(&attr);
149
pthread_attr_setdetachstate(&attr,
150
PTHREAD_CREATE_DETACHED);
152
pthread_mutex_lock(&counter_mutex);
155
create_data_file(&writer_handle, DEFAULT_INITIAL_LOAD);
157
pthread_mutex_lock(&sleeper_mutex);
159
pthread_mutex_unlock(&sleeper_mutex);
161
context= (thread_context_st *)malloc(sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
162
memset(context, 0, sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
166
fprintf(stderr, "Could not allocate memory for context\n");
170
for (x= 0; x < DEFAULT_CONCURRENCY; x++)
173
context[x].how_often_to_write= random()%1000;
174
context[x].writer= &writer_handle;
175
context[x].counter= 0;
176
context[x].use_aio= use_aio;
178
/* now you create the thread */
179
if (pthread_create(&mainthread, &attr, run_concurrent_task,
180
(void *)context) != 0)
182
fprintf(stderr,"Could not create thread\n");
188
if (DEFAULT_EXECUTE_SECONDS)
190
time_t opt_timer_length= DEFAULT_EXECUTE_SECONDS;
191
pthread_mutex_lock(&timer_alarm_mutex);
193
pthread_mutex_unlock(&timer_alarm_mutex);
195
if (pthread_create(&mainthread, &attr, timer_thread,
196
(void *)&opt_timer_length) != 0)
198
fprintf(stderr,"%s: Could not create timer thread\n", drizzled::internal::my_progname);
203
pthread_mutex_unlock(&counter_mutex);
204
pthread_attr_destroy(&attr);
206
pthread_mutex_lock(&sleeper_mutex);
208
pthread_mutex_unlock(&sleeper_mutex);
209
pthread_cond_broadcast(&sleep_threshhold);
212
We loop until we know that all children have cleaned up.
214
pthread_mutex_lock(&counter_mutex);
215
while (thread_counter)
217
struct timespec abstime;
219
memset(&abstime, 0, sizeof(struct timespec));
222
pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
224
pthread_mutex_unlock(&counter_mutex);
226
for (total= x= 0; x < DEFAULT_CONCURRENCY; x++)
227
total+= context[x].counter;
230
azclose(&writer_handle);
232
printf("Read %"PRIu64" rows\n", total);
235
void *timer_thread(void *p)
237
time_t *timer_length= (time_t *)p;
238
struct timespec abstime;
241
We lock around the initial call in case were we in a loop. This
242
also keeps the value properly syncronized across call threads.
244
pthread_mutex_lock(&sleeper_mutex);
245
while (master_wakeup)
247
pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
249
pthread_mutex_unlock(&sleeper_mutex);
251
set_timespec(abstime, *timer_length);
253
pthread_mutex_lock(&timer_alarm_mutex);
254
pthread_cond_timedwait(&timer_alarm_threshold, &timer_alarm_mutex, &abstime);
255
pthread_mutex_unlock(&timer_alarm_mutex);
257
pthread_mutex_lock(&timer_alarm_mutex);
259
pthread_mutex_unlock(&timer_alarm_mutex);
264
void *run_concurrent_task(void *p)
266
thread_context_st *context= (thread_context_st *)p;
270
boost::scoped_ptr<azio_stream> reader_handle_ap(new azio_stream);
271
azio_stream &reader_handle= *reader_handle_ap.get();
273
if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY,
276
printf("Could not open test file\n");
280
pthread_mutex_lock(&sleeper_mutex);
281
while (master_wakeup)
283
pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
285
pthread_mutex_unlock(&sleeper_mutex);
291
azread_init(&reader_handle);
292
while ((ret= azread_row(&reader_handle, &error)))
295
if (count % context->how_often_to_write)
297
write_row(context->writer);
300
/* If the timer is set, and the alarm is not active then end */
301
if (timer_alarm == false)
305
pthread_mutex_lock(&counter_mutex);
307
pthread_cond_signal(&count_threshhold);
308
pthread_mutex_unlock(&counter_mutex);
309
azclose(&reader_handle);
314
void create_data_file(azio_stream *write_handler, uint64_t rows)
319
if (!(ret= azopen(write_handler, TEST_FILENAME, O_CREAT|O_RDWR|O_TRUNC,
322
printf("Could not create test file\n");
326
for (x= 0; x < rows; x++)
327
write_row(write_handler);
329
azflush(write_handler, Z_SYNC_FLUSH);
332
unsigned int write_row(azio_stream *s)
335
char buffer[HUGE_STRING_LENGTH];
337
length= random() % HUGE_STRING_LENGTH;
339
/* Avoid zero length strings */
342
get_random_string(buffer, length);
343
pthread_mutex_lock(&row_lock);
344
azwrite_row(s, buffer, length);
345
pthread_mutex_unlock(&row_lock);