~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/archive/concurrency_test.cc

Reverted 1103

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems, Inc.
5
 
 *
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.
9
 
 *
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.
14
 
 *
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
18
 
 */
19
 
 
20
 
/*
21
 
  Just a test application for threads.
22
 
  */
23
 
 
24
 
#include "config.h"
25
 
 
26
 
#include "azio.h"
27
 
#include <stdio.h>
28
 
#include <stdlib.h>
29
 
#include <sys/types.h>
30
 
#include <sys/stat.h>
31
 
#include <sys/types.h>
32
 
#include <sys/mman.h>
33
 
#include <fcntl.h>
34
 
#include <sys/time.h>
35
 
#include <pthread.h>
36
 
#include <string.h>                             /* Pull in memset() */
37
 
#ifndef __WIN__
38
 
#include <sys/wait.h>
39
 
#endif
40
 
#include <memory>
41
 
 
42
 
#ifdef __WIN__
43
 
#define srandom  srand
44
 
#define random   rand
45
 
#define snprintf _snprintf
46
 
#endif
47
 
 
48
 
#include <boost/scoped_ptr.hpp>
49
 
 
50
 
#include "azio.h"
51
 
 
52
 
#define DEFAULT_INITIAL_LOAD 10000
53
 
#define DEFAULT_EXECUTE_SECONDS 120
54
 
#define TEST_FILENAME "concurrency_test.az"
55
 
 
56
 
#define HUGE_STRING_LENGTH 8192
57
 
 
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;
68
 
 
69
 
pthread_mutex_t row_lock;
70
 
 
71
 
/* Prototypes */
72
 
extern "C" {
73
 
  void *run_concurrent_task(void *p);
74
 
  void *timer_thread(void *p);
75
 
}
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);
79
 
 
80
 
typedef struct thread_context_st thread_context_st;
81
 
struct thread_context_st {
82
 
  unsigned int how_often_to_write;
83
 
  uint64_t counter;
84
 
  az_method use_aio;
85
 
  azio_stream *writer;
86
 
};
87
 
 
88
 
/* Use this for string generation */
89
 
static const char ALPHANUMERICS[]=
90
 
  "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
91
 
 
92
 
#define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
93
 
 
94
 
static void get_random_string(char *buffer, size_t size)
95
 
{
96
 
  char *buffer_ptr= buffer;
97
 
 
98
 
  while (--size)
99
 
    *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
100
 
  *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
101
 
}
102
 
 
103
 
int main(int argc, char *argv[])
104
 
{
105
 
 
106
 
  unsigned int method;
107
 
  drizzled::internal::my_init();
108
 
 
109
 
  MY_INIT(argv[0]);
110
 
 
111
 
  if (argc > 1)
112
 
    exit(1);
113
 
 
114
 
  srandom(time(NULL));
115
 
 
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);
123
 
 
124
 
  for (method= AZ_METHOD_BLOCK; method < AZ_METHOD_MAX; method++)
125
 
    scheduler((az_method)method);
126
 
 
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);
134
 
 
135
 
  return 0;
136
 
}
137
 
 
138
 
void scheduler(az_method use_aio)
139
 
{
140
 
  unsigned int x;
141
 
  uint64_t total;
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 */
147
 
 
148
 
  pthread_attr_init(&attr);
149
 
  pthread_attr_setdetachstate(&attr,
150
 
                              PTHREAD_CREATE_DETACHED);
151
 
 
152
 
  pthread_mutex_lock(&counter_mutex);
153
 
  thread_counter= 0;
154
 
 
155
 
  create_data_file(&writer_handle, DEFAULT_INITIAL_LOAD);
156
 
 
157
 
  pthread_mutex_lock(&sleeper_mutex);
158
 
  master_wakeup= 1;
159
 
  pthread_mutex_unlock(&sleeper_mutex);
160
 
 
161
 
  context= (thread_context_st *)malloc(sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
162
 
  memset(context, 0, sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
163
 
 
164
 
  if (!context)
165
 
  {
166
 
    fprintf(stderr, "Could not allocate memory for context\n");
167
 
    exit(1);
168
 
  }
169
 
 
170
 
  for (x= 0; x < DEFAULT_CONCURRENCY; x++)
171
 
  {
172
 
 
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;
177
 
 
178
 
    /* now you create the thread */
179
 
    if (pthread_create(&mainthread, &attr, run_concurrent_task,
180
 
                       (void *)context) != 0)
181
 
    {
182
 
      fprintf(stderr,"Could not create thread\n");
183
 
      exit(1);
184
 
    }
185
 
    thread_counter++;
186
 
  }
187
 
 
188
 
  if (DEFAULT_EXECUTE_SECONDS)
189
 
  {
190
 
    time_t opt_timer_length= DEFAULT_EXECUTE_SECONDS;
191
 
    pthread_mutex_lock(&timer_alarm_mutex);
192
 
    timer_alarm= true;
193
 
    pthread_mutex_unlock(&timer_alarm_mutex);
194
 
 
195
 
    if (pthread_create(&mainthread, &attr, timer_thread,
196
 
                       (void *)&opt_timer_length) != 0)
197
 
    {
198
 
      fprintf(stderr,"%s: Could not create timer thread\n", drizzled::internal::my_progname);
199
 
      exit(1);
200
 
    }
201
 
  }
202
 
 
203
 
  pthread_mutex_unlock(&counter_mutex);
204
 
  pthread_attr_destroy(&attr);
205
 
 
206
 
  pthread_mutex_lock(&sleeper_mutex);
207
 
  master_wakeup= 0;
208
 
  pthread_mutex_unlock(&sleeper_mutex);
209
 
  pthread_cond_broadcast(&sleep_threshhold);
210
 
 
211
 
  /*
212
 
    We loop until we know that all children have cleaned up.
213
 
  */
214
 
  pthread_mutex_lock(&counter_mutex);
215
 
  while (thread_counter)
216
 
  {
217
 
    struct timespec abstime;
218
 
 
219
 
    memset(&abstime, 0, sizeof(struct timespec));
220
 
    abstime.tv_sec= 1;
221
 
 
222
 
    pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
223
 
  }
224
 
  pthread_mutex_unlock(&counter_mutex);
225
 
 
226
 
  for (total= x= 0; x < DEFAULT_CONCURRENCY; x++)
227
 
    total+= context[x].counter;
228
 
 
229
 
  free(context);
230
 
  azclose(&writer_handle);
231
 
 
232
 
  printf("Read %"PRIu64" rows\n", total);
233
 
}
234
 
 
235
 
void *timer_thread(void *p)
236
 
{
237
 
  time_t *timer_length= (time_t *)p;
238
 
  struct timespec abstime;
239
 
 
240
 
  /*
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.
243
 
  */
244
 
  pthread_mutex_lock(&sleeper_mutex);
245
 
  while (master_wakeup)
246
 
  {
247
 
    pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
248
 
  }
249
 
  pthread_mutex_unlock(&sleeper_mutex);
250
 
 
251
 
  set_timespec(abstime, *timer_length);
252
 
 
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);
256
 
 
257
 
  pthread_mutex_lock(&timer_alarm_mutex);
258
 
  timer_alarm= false;
259
 
  pthread_mutex_unlock(&timer_alarm_mutex);
260
 
 
261
 
  return 0;
262
 
}
263
 
 
264
 
void *run_concurrent_task(void *p)
265
 
{
266
 
  thread_context_st *context= (thread_context_st *)p;
267
 
  uint64_t count;
268
 
  int ret;
269
 
  int error;
270
 
  boost::scoped_ptr<azio_stream> reader_handle_ap(new azio_stream);
271
 
  azio_stream &reader_handle= *reader_handle_ap.get();
272
 
 
273
 
  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY,
274
 
                    context->use_aio)))
275
 
  {
276
 
    printf("Could not open test file\n");
277
 
    return 0;
278
 
  }
279
 
 
280
 
  pthread_mutex_lock(&sleeper_mutex);
281
 
  while (master_wakeup)
282
 
  {
283
 
    pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
284
 
  }
285
 
  pthread_mutex_unlock(&sleeper_mutex);
286
 
 
287
 
  /* Do Stuff */
288
 
  count= 0;
289
 
  while (1)
290
 
  {
291
 
    azread_init(&reader_handle);
292
 
    while ((ret= azread_row(&reader_handle, &error)))
293
 
      context->counter++;
294
 
 
295
 
    if (count % context->how_often_to_write)
296
 
    {
297
 
      write_row(context->writer);
298
 
    }
299
 
 
300
 
    /* If the timer is set, and the alarm is not active then end */
301
 
    if (timer_alarm == false)
302
 
      break;
303
 
  }
304
 
 
305
 
  pthread_mutex_lock(&counter_mutex);
306
 
  thread_counter--;
307
 
  pthread_cond_signal(&count_threshhold);
308
 
  pthread_mutex_unlock(&counter_mutex);
309
 
  azclose(&reader_handle);
310
 
 
311
 
  return NULL;
312
 
}
313
 
 
314
 
void create_data_file(azio_stream *write_handler, uint64_t rows)
315
 
{
316
 
  int ret;
317
 
  uint64_t x;
318
 
 
319
 
  if (!(ret= azopen(write_handler, TEST_FILENAME, O_CREAT|O_RDWR|O_TRUNC,
320
 
                    AZ_METHOD_BLOCK)))
321
 
  {
322
 
    printf("Could not create test file\n");
323
 
    exit(1);
324
 
  }
325
 
 
326
 
  for (x= 0; x < rows; x++)
327
 
    write_row(write_handler);
328
 
 
329
 
  azflush(write_handler, Z_SYNC_FLUSH);
330
 
}
331
 
 
332
 
unsigned int write_row(azio_stream *s)
333
 
{
334
 
  size_t length;
335
 
  char buffer[HUGE_STRING_LENGTH];
336
 
 
337
 
  length= random() % HUGE_STRING_LENGTH;
338
 
 
339
 
  /* Avoid zero length strings */
340
 
  length++;
341
 
 
342
 
  get_random_string(buffer, length);
343
 
  pthread_mutex_lock(&row_lock);
344
 
  azwrite_row(s, buffer, length);
345
 
  pthread_mutex_unlock(&row_lock);
346
 
 
347
 
  return 0;
348
 
}