~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2009 Sun Microsystems
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
  Just a test application for threads.
  */

#include "config.h"

#include "azio.h"
#include "drizzled/my_getopt.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/time.h>
#include <pthread.h>
#include <string.h>                             /* Pull in memset() */
#ifndef __WIN__
#include <sys/wait.h>
#endif

#ifdef __WIN__
#define srandom  srand
#define random   rand
#define snprintf _snprintf
#endif

#include "azio.h"

#define DEFAULT_INITIAL_LOAD 10000
#define DEFAULT_EXECUTE_SECONDS 120
#define TEST_FILENAME "concurrency_test.az"

#define HUGE_STRING_LENGTH 8192

/* Global Thread counter */
unsigned int thread_counter;
pthread_mutex_t counter_mutex;
pthread_cond_t count_threshhold;
unsigned int master_wakeup;
pthread_mutex_t sleeper_mutex;
pthread_cond_t sleep_threshhold;
static bool timer_alarm= false;
pthread_mutex_t timer_alarm_mutex;
pthread_cond_t timer_alarm_threshold;

pthread_mutex_t row_lock;

/* Prototypes */
extern "C" {
  void *run_concurrent_task(void *p);
  void *timer_thread(void *p);
}
void scheduler(az_method use_aio);
void create_data_file(azio_stream *write_handler, uint64_t rows);
unsigned int write_row(azio_stream *s);

typedef struct thread_context_st thread_context_st;
struct thread_context_st {
  unsigned int how_often_to_write;
  uint64_t counter;
  az_method use_aio;
  azio_stream *writer;
};

/* Use this for string generation */
static const char ALPHANUMERICS[]=
  "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";

#define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)

static void get_random_string(char *buffer, size_t size)
{
  char *buffer_ptr= buffer;

  while (--size)
    *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
  *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
}

int main(int argc, char *argv[])
{

  unsigned int method;
  drizzled::internal::my_init();

  MY_INIT(argv[0]);

  if (argc > 1)
    exit(1);

  srandom(time(NULL));

  pthread_mutex_init(&counter_mutex, NULL);
  pthread_cond_init(&count_threshhold, NULL);
  pthread_mutex_init(&sleeper_mutex, NULL);
  pthread_cond_init(&sleep_threshhold, NULL);
  pthread_mutex_init(&timer_alarm_mutex, NULL);
  pthread_cond_init(&timer_alarm_threshold, NULL);
  pthread_mutex_init(&row_lock, NULL);

  for (method= AZ_METHOD_BLOCK; method < AZ_METHOD_MAX; method++)
    scheduler((az_method)method);

  (void)pthread_mutex_destroy(&counter_mutex);
  (void)pthread_cond_destroy(&count_threshhold);
  (void)pthread_mutex_destroy(&sleeper_mutex);
  (void)pthread_cond_destroy(&sleep_threshhold);
  pthread_mutex_destroy(&timer_alarm_mutex);
  pthread_cond_destroy(&timer_alarm_threshold);
  pthread_mutex_destroy(&row_lock);

  return 0;
}

void scheduler(az_method use_aio)
{
  unsigned int x;
  uint64_t total;
  azio_stream writer_handle;
  thread_context_st *context;
  pthread_t mainthread;            /* Thread descriptor */
  pthread_attr_t attr;          /* Thread attributes */

  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr,
                              PTHREAD_CREATE_DETACHED);

  pthread_mutex_lock(&counter_mutex);
  thread_counter= 0;

  create_data_file(&writer_handle, DEFAULT_INITIAL_LOAD);

  pthread_mutex_lock(&sleeper_mutex);
  master_wakeup= 1;
  pthread_mutex_unlock(&sleeper_mutex);

  context= (thread_context_st *)malloc(sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
  memset(context, 0, sizeof(thread_context_st) * DEFAULT_CONCURRENCY);

  if (!context)
  {
    fprintf(stderr, "Could not allocate memory for context\n");
    exit(1);
  }

  for (x= 0; x < DEFAULT_CONCURRENCY; x++)
  {

    context[x].how_often_to_write= random()%1000;
    context[x].writer= &writer_handle;
    context[x].counter= 0;
    context[x].use_aio= use_aio;

    /* now you create the thread */
    if (pthread_create(&mainthread, &attr, run_concurrent_task,
                       (void *)context) != 0)
    {
      fprintf(stderr,"Could not create thread\n");
      exit(1);
    }
    thread_counter++;
  }

  if (DEFAULT_EXECUTE_SECONDS)
  {
    time_t opt_timer_length= DEFAULT_EXECUTE_SECONDS;
    pthread_mutex_lock(&timer_alarm_mutex);
    timer_alarm= true;
    pthread_mutex_unlock(&timer_alarm_mutex);

    if (pthread_create(&mainthread, &attr, timer_thread,
                       (void *)&opt_timer_length) != 0)
    {
      fprintf(stderr,"%s: Could not create timer thread\n", drizzled::internal::my_progname);
      exit(1);
    }
  }

  pthread_mutex_unlock(&counter_mutex);
  pthread_attr_destroy(&attr);

  pthread_mutex_lock(&sleeper_mutex);
  master_wakeup= 0;
  pthread_mutex_unlock(&sleeper_mutex);
  pthread_cond_broadcast(&sleep_threshhold);

  /*
    We loop until we know that all children have cleaned up.
  */
  pthread_mutex_lock(&counter_mutex);
  while (thread_counter)
  {
    struct timespec abstime;

    memset(&abstime, 0, sizeof(struct timespec));
    abstime.tv_sec= 1;

    pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
  }
  pthread_mutex_unlock(&counter_mutex);

  for (total= x= 0; x < DEFAULT_CONCURRENCY; x++)
    total+= context[x].counter;

  free(context);
  azclose(&writer_handle);

  printf("Read %"PRIu64" rows\n", total);
}

void *timer_thread(void *p)
{
  time_t *timer_length= (time_t *)p;
  struct timespec abstime;

  /*
    We lock around the initial call in case were we in a loop. This
    also keeps the value properly syncronized across call threads.
  */
  pthread_mutex_lock(&sleeper_mutex);
  while (master_wakeup)
  {
    pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
  }
  pthread_mutex_unlock(&sleeper_mutex);

  set_timespec(abstime, *timer_length);

  pthread_mutex_lock(&timer_alarm_mutex);
  pthread_cond_timedwait(&timer_alarm_threshold, &timer_alarm_mutex, &abstime);
  pthread_mutex_unlock(&timer_alarm_mutex);

  pthread_mutex_lock(&timer_alarm_mutex);
  timer_alarm= false;
  pthread_mutex_unlock(&timer_alarm_mutex);

  return 0;
}

void *run_concurrent_task(void *p)
{
  thread_context_st *context= (thread_context_st *)p;
  uint64_t count;
  int ret;
  int error;
  azio_stream reader_handle;

  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY,
                    context->use_aio)))
  {
    printf("Could not open test file\n");
    return 0;
  }

  pthread_mutex_lock(&sleeper_mutex);
  while (master_wakeup)
  {
    pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
  }
  pthread_mutex_unlock(&sleeper_mutex);

  /* Do Stuff */
  count= 0;
  while (1)
  {
    azread_init(&reader_handle);
    while ((ret= azread_row(&reader_handle, &error)))
      context->counter++;

    if (count % context->how_often_to_write)
    {
      write_row(context->writer);
    }

    /* If the timer is set, and the alarm is not active then end */
    if (timer_alarm == false)
      break;
  }

  pthread_mutex_lock(&counter_mutex);
  thread_counter--;
  pthread_cond_signal(&count_threshhold);
  pthread_mutex_unlock(&counter_mutex);
  azclose(&reader_handle);

  return NULL;
}

void create_data_file(azio_stream *write_handler, uint64_t rows)
{
  int ret;
  uint64_t x;

  if (!(ret= azopen(write_handler, TEST_FILENAME, O_CREAT|O_RDWR|O_TRUNC,
                    AZ_METHOD_BLOCK)))
  {
    printf("Could not create test file\n");
    exit(1);
  }

  for (x= 0; x < rows; x++)
    write_row(write_handler);

  azflush(write_handler, Z_SYNC_FLUSH);
}

unsigned int write_row(azio_stream *s)
{
  size_t length;
  char buffer[HUGE_STRING_LENGTH];

  length= random() % HUGE_STRING_LENGTH;

  /* Avoid zero length strings */
  length++;

  get_random_string(buffer, length);
  pthread_mutex_lock(&row_lock);
  azwrite_row(s, buffer, length);
  pthread_mutex_unlock(&row_lock);

  return 0;
}