~drizzle-trunk/drizzle/development

1122.2.2 by Monty Taylor
Added missing copyright headers. Added drizzled/global.h to a few things that
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
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
1 by brian
clean slate
20
/*
21
  Just a test application for threads.
22
  */
1130.3.28 by Monty Taylor
Moved heapdef.h and myisamdef.h to *_priv.h for easier filtering for include guard check.
23
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
24
#include "config.h"
1130.3.28 by Monty Taylor
Moved heapdef.h and myisamdef.h to *_priv.h for easier filtering for include guard check.
25
1 by brian
clean slate
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>
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
36
#include <string.h>                             /* Pull in memset() */
1 by brian
clean slate
37
#ifndef __WIN__
38
#include <sys/wait.h>
39
#endif
40
41
#ifdef __WIN__
42
#define srandom  srand
43
#define random   rand
44
#define snprintf _snprintf
45
#endif
46
47
#include "azio.h"
48
49
#define DEFAULT_INITIAL_LOAD 10000
50
#define DEFAULT_EXECUTE_SECONDS 120
51
#define TEST_FILENAME "concurrency_test.az"
52
53
#define HUGE_STRING_LENGTH 8192
54
55
/* Global Thread counter */
56
unsigned int thread_counter;
57
pthread_mutex_t counter_mutex;
58
pthread_cond_t count_threshhold;
59
unsigned int master_wakeup;
60
pthread_mutex_t sleeper_mutex;
61
pthread_cond_t sleep_threshhold;
282 by Brian Aker
Modified blackhole and archive to remove my_bool.
62
static bool timer_alarm= false;
1 by brian
clean slate
63
pthread_mutex_t timer_alarm_mutex;
64
pthread_cond_t timer_alarm_threshold;
65
66
pthread_mutex_t row_lock;
67
68
/* Prototypes */
968.2.15 by Monty Taylor
Fixes for Solaris.
69
extern "C" {
70
  void *run_concurrent_task(void *p);
71
  void *timer_thread(void *p);
72
}
1 by brian
clean slate
73
void scheduler(az_method use_aio);
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
74
void create_data_file(azio_stream *write_handler, uint64_t rows);
1 by brian
clean slate
75
unsigned int write_row(azio_stream *s);
76
77
typedef struct thread_context_st thread_context_st;
78
struct thread_context_st {
79
  unsigned int how_often_to_write;
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
80
  uint64_t counter;
1 by brian
clean slate
81
  az_method use_aio;
82
  azio_stream *writer;
83
};
84
85
/* Use this for string generation */
86
static const char ALPHANUMERICS[]=
87
  "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
88
89
#define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
90
91
static void get_random_string(char *buffer, size_t size)
92
{
93
  char *buffer_ptr= buffer;
94
95
  while (--size)
96
    *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
97
  *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
98
}
99
100
int main(int argc, char *argv[])
101
{
102
968.2.14 by Monty Taylor
Removed references to stdbool, since they aren't valid in c++. Added them in C where we need them. Made archive tests c++.
103
  unsigned int method;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
104
  drizzled::internal::my_init();
1 by brian
clean slate
105
106
  MY_INIT(argv[0]);
107
108
  if (argc > 1)
109
    exit(1);
110
111
  srandom(time(NULL));
112
113
  pthread_mutex_init(&counter_mutex, NULL);
114
  pthread_cond_init(&count_threshhold, NULL);
115
  pthread_mutex_init(&sleeper_mutex, NULL);
116
  pthread_cond_init(&sleep_threshhold, NULL);
398.1.10 by Monty Taylor
Actually removed VOID() this time.
117
  pthread_mutex_init(&timer_alarm_mutex, NULL);
118
  pthread_cond_init(&timer_alarm_threshold, NULL);
119
  pthread_mutex_init(&row_lock, NULL);
1 by brian
clean slate
120
121
  for (method= AZ_METHOD_BLOCK; method < AZ_METHOD_MAX; method++)
968.2.14 by Monty Taylor
Removed references to stdbool, since they aren't valid in c++. Added them in C where we need them. Made archive tests c++.
122
    scheduler((az_method)method);
1 by brian
clean slate
123
124
  (void)pthread_mutex_destroy(&counter_mutex);
125
  (void)pthread_cond_destroy(&count_threshhold);
126
  (void)pthread_mutex_destroy(&sleeper_mutex);
127
  (void)pthread_cond_destroy(&sleep_threshhold);
398.1.10 by Monty Taylor
Actually removed VOID() this time.
128
  pthread_mutex_destroy(&timer_alarm_mutex);
129
  pthread_cond_destroy(&timer_alarm_threshold);
130
  pthread_mutex_destroy(&row_lock);
1 by brian
clean slate
131
132
  return 0;
133
}
134
135
void scheduler(az_method use_aio)
136
{
137
  unsigned int x;
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
138
  uint64_t total;
1 by brian
clean slate
139
  azio_stream writer_handle;
140
  thread_context_st *context;
141
  pthread_t mainthread;            /* Thread descriptor */
142
  pthread_attr_t attr;          /* Thread attributes */
143
144
  pthread_attr_init(&attr);
145
  pthread_attr_setdetachstate(&attr,
146
                              PTHREAD_CREATE_DETACHED);
147
148
  pthread_mutex_lock(&counter_mutex);
149
  thread_counter= 0;
150
151
  create_data_file(&writer_handle, DEFAULT_INITIAL_LOAD);
152
153
  pthread_mutex_lock(&sleeper_mutex);
154
  master_wakeup= 1;
155
  pthread_mutex_unlock(&sleeper_mutex);
156
157
  context= (thread_context_st *)malloc(sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
158
  memset(context, 0, sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
1 by brian
clean slate
159
160
  if (!context)
161
  {
162
    fprintf(stderr, "Could not allocate memory for context\n");
163
    exit(1);
164
  }
165
166
  for (x= 0; x < DEFAULT_CONCURRENCY; x++)
167
  {
168
169
    context[x].how_often_to_write= random()%1000;
170
    context[x].writer= &writer_handle;
171
    context[x].counter= 0;
172
    context[x].use_aio= use_aio;
173
174
    /* now you create the thread */
602 by Brian Aker
Merge in Monty's work for Solaris Sun Studio
175
    if (pthread_create(&mainthread, &attr, run_concurrent_task,
1 by brian
clean slate
176
                       (void *)context) != 0)
177
    {
178
      fprintf(stderr,"Could not create thread\n");
179
      exit(1);
180
    }
181
    thread_counter++;
182
  }
183
184
  if (DEFAULT_EXECUTE_SECONDS)
185
  {
186
    time_t opt_timer_length= DEFAULT_EXECUTE_SECONDS;
187
    pthread_mutex_lock(&timer_alarm_mutex);
163 by Brian Aker
Merge Monty's code.
188
    timer_alarm= true;
1 by brian
clean slate
189
    pthread_mutex_unlock(&timer_alarm_mutex);
190
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
191
    if (pthread_create(&mainthread, &attr, timer_thread,
1 by brian
clean slate
192
                       (void *)&opt_timer_length) != 0)
193
    {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
194
      fprintf(stderr,"%s: Could not create timer thread\n", drizzled::internal::my_progname);
1 by brian
clean slate
195
      exit(1);
196
    }
197
  }
198
199
  pthread_mutex_unlock(&counter_mutex);
200
  pthread_attr_destroy(&attr);
201
202
  pthread_mutex_lock(&sleeper_mutex);
203
  master_wakeup= 0;
204
  pthread_mutex_unlock(&sleeper_mutex);
205
  pthread_cond_broadcast(&sleep_threshhold);
206
207
  /*
208
    We loop until we know that all children have cleaned up.
209
  */
210
  pthread_mutex_lock(&counter_mutex);
211
  while (thread_counter)
212
  {
213
    struct timespec abstime;
214
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
215
    memset(&abstime, 0, sizeof(struct timespec));
1 by brian
clean slate
216
    abstime.tv_sec= 1;
217
218
    pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
219
  }
220
  pthread_mutex_unlock(&counter_mutex);
221
222
  for (total= x= 0; x < DEFAULT_CONCURRENCY; x++)
223
    total+= context[x].counter;
224
225
  free(context);
226
  azclose(&writer_handle);
227
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
228
  printf("Read %"PRIu64" rows\n", total);
1 by brian
clean slate
229
}
230
231
void *timer_thread(void *p)
232
{
233
  time_t *timer_length= (time_t *)p;
234
  struct timespec abstime;
235
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
236
  /*
237
    We lock around the initial call in case were we in a loop. This
1 by brian
clean slate
238
    also keeps the value properly syncronized across call threads.
239
  */
240
  pthread_mutex_lock(&sleeper_mutex);
241
  while (master_wakeup)
242
  {
243
    pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
244
  }
245
  pthread_mutex_unlock(&sleeper_mutex);
246
247
  set_timespec(abstime, *timer_length);
248
249
  pthread_mutex_lock(&timer_alarm_mutex);
250
  pthread_cond_timedwait(&timer_alarm_threshold, &timer_alarm_mutex, &abstime);
251
  pthread_mutex_unlock(&timer_alarm_mutex);
252
253
  pthread_mutex_lock(&timer_alarm_mutex);
163 by Brian Aker
Merge Monty's code.
254
  timer_alarm= false;
1 by brian
clean slate
255
  pthread_mutex_unlock(&timer_alarm_mutex);
256
257
  return 0;
258
}
259
602 by Brian Aker
Merge in Monty's work for Solaris Sun Studio
260
void *run_concurrent_task(void *p)
1 by brian
clean slate
261
{
262
  thread_context_st *context= (thread_context_st *)p;
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
263
  uint64_t count;
1 by brian
clean slate
264
  int ret;
265
  int error;
266
  azio_stream reader_handle;
267
492.1.14 by Monty Taylor
Removed O_BINARY and FILE_BINARY.
268
  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY,
1 by brian
clean slate
269
                    context->use_aio)))
270
  {
271
    printf("Could not open test file\n");
272
    return 0;
273
  }
274
275
  pthread_mutex_lock(&sleeper_mutex);
276
  while (master_wakeup)
277
  {
278
    pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
279
  }
1 by brian
clean slate
280
  pthread_mutex_unlock(&sleeper_mutex);
281
282
  /* Do Stuff */
283
  count= 0;
284
  while (1)
285
  {
286
    azread_init(&reader_handle);
287
    while ((ret= azread_row(&reader_handle, &error)))
288
      context->counter++;
289
290
    if (count % context->how_often_to_write)
291
    {
292
      write_row(context->writer);
293
    }
294
295
    /* If the timer is set, and the alarm is not active then end */
163 by Brian Aker
Merge Monty's code.
296
    if (timer_alarm == false)
1 by brian
clean slate
297
      break;
298
  }
299
300
  pthread_mutex_lock(&counter_mutex);
301
  thread_counter--;
302
  pthread_cond_signal(&count_threshhold);
303
  pthread_mutex_unlock(&counter_mutex);
304
  azclose(&reader_handle);
305
306
  return NULL;
307
}
308
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
309
void create_data_file(azio_stream *write_handler, uint64_t rows)
1 by brian
clean slate
310
{
311
  int ret;
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
312
  uint64_t x;
1 by brian
clean slate
313
492.1.14 by Monty Taylor
Removed O_BINARY and FILE_BINARY.
314
  if (!(ret= azopen(write_handler, TEST_FILENAME, O_CREAT|O_RDWR|O_TRUNC,
1 by brian
clean slate
315
                    AZ_METHOD_BLOCK)))
316
  {
317
    printf("Could not create test file\n");
318
    exit(1);
319
  }
320
321
  for (x= 0; x < rows; x++)
322
    write_row(write_handler);
323
324
  azflush(write_handler, Z_SYNC_FLUSH);
325
}
326
327
unsigned int write_row(azio_stream *s)
328
{
329
  size_t length;
330
  char buffer[HUGE_STRING_LENGTH];
331
332
  length= random() % HUGE_STRING_LENGTH;
333
334
  /* Avoid zero length strings */
335
  length++;
336
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
337
  get_random_string(buffer, length);
1 by brian
clean slate
338
  pthread_mutex_lock(&row_lock);
339
  azwrite_row(s, buffer, length);
340
  pthread_mutex_unlock(&row_lock);
341
342
  return 0;
343
}