~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/archive/concurrency_test.c

  • Committer: Monty Taylor
  • Date: 2008-07-05 22:08:52 UTC
  • mto: This revision was merged to the branch mainline in revision 77.
  • Revision ID: monty@inaugust.com-20080705220852-cqd9t6tfkhvlcf73
Removed HAVE_LONG_LONG, as this is now assumed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
  Just a test application for threads.
3
3
  */
4
4
#include "azio.h"
5
 
#include <libdrizzle/libdrizzle.h>
6
 
#include <mysys/my_getopt.h>
 
5
#include <mysql.h>
 
6
#include <my_getopt.h>
 
7
#include <mysql_version.h>
7
8
#include <stdio.h>
8
9
#include <stdlib.h>
9
10
#include <sys/types.h>
13
14
#include <fcntl.h>
14
15
#include <sys/time.h>
15
16
#include <pthread.h>
16
 
#include <string.h>                             /* Pull in memset() */
 
17
#include <strings.h>
17
18
#ifndef __WIN__
18
19
#include <sys/wait.h>
19
20
#endif
40
41
unsigned int master_wakeup;
41
42
pthread_mutex_t sleeper_mutex;
42
43
pthread_cond_t sleep_threshhold;
43
 
static bool timer_alarm= false;
 
44
static my_bool timer_alarm= FALSE;
44
45
pthread_mutex_t timer_alarm_mutex;
45
46
pthread_cond_t timer_alarm_threshold;
46
47
 
50
51
void *run_task(void *p);
51
52
void *timer_thread(void *p);
52
53
void scheduler(az_method use_aio);
53
 
void create_data_file(azio_stream *write_handler, uint64_t rows);
 
54
void create_data_file(azio_stream *write_handler, unsigned long long rows);
54
55
unsigned int write_row(azio_stream *s);
55
56
 
56
57
typedef struct thread_context_st thread_context_st;
57
58
struct thread_context_st {
58
59
  unsigned int how_often_to_write;
59
 
  uint64_t counter;
 
60
  unsigned long long counter;
60
61
  az_method use_aio;
61
62
  azio_stream *writer;
62
63
};
87
88
  if (argc > 1)
88
89
    exit(1);
89
90
 
 
91
  if (!(mysql_thread_safe()))
 
92
      fprintf(stderr, "This application was compiled incorrectly. Please recompile with thread support.\n");
 
93
 
90
94
  srandom(time(NULL));
91
95
 
92
96
  pthread_mutex_init(&counter_mutex, NULL);
93
97
  pthread_cond_init(&count_threshhold, NULL);
94
98
  pthread_mutex_init(&sleeper_mutex, NULL);
95
99
  pthread_cond_init(&sleep_threshhold, NULL);
96
 
  pthread_mutex_init(&timer_alarm_mutex, NULL);
97
 
  pthread_cond_init(&timer_alarm_threshold, NULL);
98
 
  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));
99
103
 
100
104
  for (method= AZ_METHOD_BLOCK; method < AZ_METHOD_MAX; method++)
101
105
    scheduler(method);
104
108
  (void)pthread_cond_destroy(&count_threshhold);
105
109
  (void)pthread_mutex_destroy(&sleeper_mutex);
106
110
  (void)pthread_cond_destroy(&sleep_threshhold);
107
 
  pthread_mutex_destroy(&timer_alarm_mutex);
108
 
  pthread_cond_destroy(&timer_alarm_threshold);
109
 
  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));
110
114
 
111
115
  return 0;
112
116
}
114
118
void scheduler(az_method use_aio)
115
119
{
116
120
  unsigned int x;
117
 
  uint64_t total;
 
121
  unsigned long long total;
118
122
  azio_stream writer_handle;
119
123
  thread_context_st *context;
120
124
  pthread_t mainthread;            /* Thread descriptor */
134
138
  pthread_mutex_unlock(&sleeper_mutex);
135
139
 
136
140
  context= (thread_context_st *)malloc(sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
137
 
  memset(context, 0, sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
 
141
  bzero(context, sizeof(thread_context_st) * DEFAULT_CONCURRENCY);
138
142
 
139
143
  if (!context)
140
144
  {
164
168
  {
165
169
    time_t opt_timer_length= DEFAULT_EXECUTE_SECONDS;
166
170
    pthread_mutex_lock(&timer_alarm_mutex);
167
 
    timer_alarm= true;
 
171
    timer_alarm= TRUE;
168
172
    pthread_mutex_unlock(&timer_alarm_mutex);
169
173
 
170
174
    if (pthread_create(&mainthread, &attr, timer_thread, 
191
195
  {
192
196
    struct timespec abstime;
193
197
 
194
 
    memset(&abstime, 0, sizeof(struct timespec));
 
198
    bzero(&abstime, sizeof(struct timespec));
195
199
    abstime.tv_sec= 1;
196
200
 
197
201
    pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
204
208
  free(context);
205
209
  azclose(&writer_handle);
206
210
 
207
 
  printf("Read %"PRIu64" rows\n", total);
 
211
  printf("Read %llu rows\n", total);
208
212
}
209
213
 
210
214
void *timer_thread(void *p)
212
216
  time_t *timer_length= (time_t *)p;
213
217
  struct timespec abstime;
214
218
 
 
219
  if (mysql_thread_init())
 
220
  {
 
221
    fprintf(stderr,"%s: mysql_thread_init() failed.\n",
 
222
            my_progname);
 
223
    exit(1);
 
224
  }
 
225
 
215
226
  /* 
216
227
    We lock around the initial call in case were we in a loop. This 
217
228
    also keeps the value properly syncronized across call threads.
230
241
  pthread_mutex_unlock(&timer_alarm_mutex);
231
242
 
232
243
  pthread_mutex_lock(&timer_alarm_mutex);
233
 
  timer_alarm= false;
 
244
  timer_alarm= FALSE;
234
245
  pthread_mutex_unlock(&timer_alarm_mutex);
235
246
 
 
247
  mysql_thread_end();
 
248
 
236
249
  return 0;
237
250
}
238
251
 
239
252
void *run_task(void *p)
240
253
{
241
254
  thread_context_st *context= (thread_context_st *)p;
242
 
  uint64_t count;
 
255
  unsigned long long count;
243
256
  int ret;
244
257
  int error;
245
258
  azio_stream reader_handle;
246
259
 
247
 
  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY,
 
260
  if (mysql_thread_init())
 
261
  {
 
262
    fprintf(stderr,"%s: mysql_thread_init() failed.\n", my_progname);
 
263
    exit(1);
 
264
  }
 
265
 
 
266
  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY,
248
267
                    context->use_aio)))
249
268
  {
250
269
    printf("Could not open test file\n");
272
291
    }
273
292
 
274
293
    /* If the timer is set, and the alarm is not active then end */
275
 
    if (timer_alarm == false)
 
294
    if (timer_alarm == FALSE)
276
295
      break;
277
296
  }
278
297
 
282
301
  pthread_mutex_unlock(&counter_mutex);
283
302
  azclose(&reader_handle);
284
303
 
 
304
  mysql_thread_end();
 
305
 
285
306
  return NULL;
286
307
}
287
308
 
288
 
void create_data_file(azio_stream *write_handler, uint64_t rows)
 
309
void create_data_file(azio_stream *write_handler, unsigned long long rows)
289
310
{
290
311
  int ret;
291
 
  uint64_t x;
 
312
  unsigned long long x;
292
313
 
293
 
  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,
294
315
                    AZ_METHOD_BLOCK)))
295
316
  {
296
317
    printf("Could not create test file\n");