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
2
Just a test application for threads.
7
#include <drizzle_version.h>
29
10
#include <sys/types.h>
45
25
#define snprintf _snprintf
48
#include <boost/scoped_ptr.hpp>
52
#define DEFAULT_CONCURRENCY 10
53
30
#define DEFAULT_INITIAL_LOAD 10000
54
31
#define DEFAULT_EXECUTE_SECONDS 120
32
#define DEFAULT_CONCURRENCY 8
55
33
#define TEST_FILENAME "concurrency_test.az"
57
35
#define HUGE_STRING_LENGTH 8192
63
41
unsigned int master_wakeup;
64
42
pthread_mutex_t sleeper_mutex;
65
43
pthread_cond_t sleep_threshhold;
66
static bool timer_alarm= false;
44
static my_bool timer_alarm= false;
67
45
pthread_mutex_t timer_alarm_mutex;
68
46
pthread_cond_t timer_alarm_threshold;
70
48
pthread_mutex_t row_lock;
74
void *run_concurrent_task(void *p);
75
void *timer_thread(void *p);
51
void *run_task(void *p);
52
void *timer_thread(void *p);
77
53
void scheduler(az_method use_aio);
78
void create_data_file(azio_stream *write_handler, uint64_t rows);
54
void create_data_file(azio_stream *write_handler, unsigned long long rows);
79
55
unsigned int write_row(azio_stream *s);
81
57
typedef struct thread_context_st thread_context_st;
82
58
struct thread_context_st {
83
59
unsigned int how_often_to_write;
60
unsigned long long counter;
86
62
azio_stream *writer;
104
80
int main(int argc, char *argv[])
108
drizzled::internal::my_init();
91
if (!(mysql_thread_safe()))
92
fprintf(stderr, "This application was compiled incorrectly. Please recompile with thread support.\n");
115
94
srandom(time(NULL));
117
96
pthread_mutex_init(&counter_mutex, NULL);
118
97
pthread_cond_init(&count_threshhold, NULL);
119
98
pthread_mutex_init(&sleeper_mutex, NULL);
120
99
pthread_cond_init(&sleep_threshhold, NULL);
121
pthread_mutex_init(&timer_alarm_mutex, NULL);
122
pthread_cond_init(&timer_alarm_threshold, NULL);
123
pthread_mutex_init(&row_lock, NULL);
100
VOID(pthread_mutex_init(&timer_alarm_mutex, NULL));
101
VOID(pthread_cond_init(&timer_alarm_threshold, NULL));
102
VOID(pthread_mutex_init(&row_lock, NULL));
125
104
for (method= AZ_METHOD_BLOCK; method < AZ_METHOD_MAX; method++)
126
scheduler((az_method)method);
128
107
(void)pthread_mutex_destroy(&counter_mutex);
129
108
(void)pthread_cond_destroy(&count_threshhold);
130
109
(void)pthread_mutex_destroy(&sleeper_mutex);
131
110
(void)pthread_cond_destroy(&sleep_threshhold);
132
pthread_mutex_destroy(&timer_alarm_mutex);
133
pthread_cond_destroy(&timer_alarm_threshold);
134
pthread_mutex_destroy(&row_lock);
111
VOID(pthread_mutex_destroy(&timer_alarm_mutex));
112
VOID(pthread_cond_destroy(&timer_alarm_threshold));
113
VOID(pthread_mutex_destroy(&row_lock));
139
118
void scheduler(az_method use_aio)
143
boost::scoped_ptr<azio_stream> writer_handle_ap(new azio_stream);
144
azio_stream &writer_handle= *writer_handle_ap.get();
121
unsigned long long total;
122
azio_stream writer_handle;
145
123
thread_context_st *context;
146
124
pthread_t mainthread; /* Thread descriptor */
147
125
pthread_attr_t attr; /* Thread attributes */
160
138
pthread_mutex_unlock(&sleeper_mutex);
162
140
context= (thread_context_st *)malloc(sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
163
memset(context, 0, sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
141
bzero(context, sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
177
155
context[x].use_aio= use_aio;
179
157
/* now you create the thread */
180
if (pthread_create(&mainthread, &attr, run_concurrent_task,
158
if (pthread_create(&mainthread, &attr, run_task,
181
159
(void *)context) != 0)
183
161
fprintf(stderr,"Could not create thread\n");
193
171
timer_alarm= true;
194
172
pthread_mutex_unlock(&timer_alarm_mutex);
196
if (pthread_create(&mainthread, &attr, timer_thread,
174
if (pthread_create(&mainthread, &attr, timer_thread,
197
175
(void *)&opt_timer_length) != 0)
199
fprintf(stderr,"%s: Could not create timer thread\n", drizzled::internal::my_progname);
177
fprintf(stderr,"%s: Could not create timer thread\n", my_progname);
218
196
struct timespec abstime;
220
memset(&abstime, 0, sizeof(struct timespec));
198
bzero(&abstime, sizeof(struct timespec));
221
199
abstime.tv_sec= 1;
223
201
pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
238
216
time_t *timer_length= (time_t *)p;
239
217
struct timespec abstime;
242
We lock around the initial call in case were we in a loop. This
219
if (mysql_thread_init())
221
fprintf(stderr,"%s: mysql_thread_init() failed.\n",
227
We lock around the initial call in case were we in a loop. This
243
228
also keeps the value properly syncronized across call threads.
245
230
pthread_mutex_lock(&sleeper_mutex);
259
244
timer_alarm= false;
260
245
pthread_mutex_unlock(&timer_alarm_mutex);
265
void *run_concurrent_task(void *p)
252
void *run_task(void *p)
267
254
thread_context_st *context= (thread_context_st *)p;
255
unsigned long long count;
271
boost::scoped_ptr<azio_stream> reader_handle_ap(new azio_stream);
272
azio_stream &reader_handle= *reader_handle_ap.get();
274
if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY,
258
azio_stream reader_handle;
260
if (mysql_thread_init())
262
fprintf(stderr,"%s: mysql_thread_init() failed.\n", my_progname);
266
if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY,
275
267
context->use_aio)))
277
269
printf("Could not open test file\n");
309
301
pthread_mutex_unlock(&counter_mutex);
310
302
azclose(&reader_handle);
315
void create_data_file(azio_stream *write_handler, uint64_t rows)
309
void create_data_file(azio_stream *write_handler, unsigned long long rows)
312
unsigned long long x;
320
if (!(ret= azopen(write_handler, TEST_FILENAME, O_CREAT|O_RDWR|O_TRUNC,
314
if (!(ret= azopen(write_handler, TEST_FILENAME, O_CREAT|O_RDWR|O_TRUNC|O_BINARY,
321
315
AZ_METHOD_BLOCK)))
323
317
printf("Could not create test file\n");
340
334
/* Avoid zero length strings */
343
get_random_string(buffer, length);
337
get_random_string(buffer, length);
344
338
pthread_mutex_lock(&row_lock);
345
339
azwrite_row(s, buffer, length);
346
340
pthread_mutex_unlock(&row_lock);