~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/archive/concurrency_test.c

  • Committer: Stewart Smith
  • Date: 2009-01-12 05:43:13 UTC
  • mto: (784.1.4 for-brian)
  • mto: This revision was merged to the branch mainline in revision 785.
  • Revision ID: stewart@flamingspork.com-20090112054313-edk6kpf4l6kpz4j7
fix archive_basic for drizzle

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/drizzle.h>
 
5
#include <libdrizzle/libdrizzle.h>
6
6
#include <mysys/my_getopt.h>
7
7
#include <stdio.h>
8
8
#include <stdlib.h>
28
28
 
29
29
#define DEFAULT_INITIAL_LOAD 10000
30
30
#define DEFAULT_EXECUTE_SECONDS 120
31
 
#define DEFAULT_CONCURRENCY 8
32
31
#define TEST_FILENAME "concurrency_test.az"
33
32
 
34
33
#define HUGE_STRING_LENGTH 8192
47
46
pthread_mutex_t row_lock;
48
47
 
49
48
/* Prototypes */
50
 
void *run_task(void *p);
 
49
void *run_concurrent_task(void *p);
51
50
void *timer_thread(void *p);
52
51
void scheduler(az_method use_aio);
53
 
void create_data_file(azio_stream *write_handler, unsigned long long rows);
 
52
void create_data_file(azio_stream *write_handler, uint64_t rows);
54
53
unsigned int write_row(azio_stream *s);
55
54
 
56
55
typedef struct thread_context_st thread_context_st;
57
56
struct thread_context_st {
58
57
  unsigned int how_often_to_write;
59
 
  unsigned long long counter;
 
58
  uint64_t counter;
60
59
  az_method use_aio;
61
60
  azio_stream *writer;
62
61
};
87
86
  if (argc > 1)
88
87
    exit(1);
89
88
 
90
 
  if (!(drizzle_thread_safe()))
91
 
      fprintf(stderr, "This application was compiled incorrectly. Please recompile with thread support.\n");
92
 
 
93
89
  srandom(time(NULL));
94
90
 
95
91
  pthread_mutex_init(&counter_mutex, NULL);
96
92
  pthread_cond_init(&count_threshhold, NULL);
97
93
  pthread_mutex_init(&sleeper_mutex, NULL);
98
94
  pthread_cond_init(&sleep_threshhold, NULL);
99
 
  VOID(pthread_mutex_init(&timer_alarm_mutex, NULL));
100
 
  VOID(pthread_cond_init(&timer_alarm_threshold, NULL));
101
 
  VOID(pthread_mutex_init(&row_lock, NULL));
 
95
  pthread_mutex_init(&timer_alarm_mutex, NULL);
 
96
  pthread_cond_init(&timer_alarm_threshold, NULL);
 
97
  pthread_mutex_init(&row_lock, NULL);
102
98
 
103
99
  for (method= AZ_METHOD_BLOCK; method < AZ_METHOD_MAX; method++)
104
100
    scheduler(method);
107
103
  (void)pthread_cond_destroy(&count_threshhold);
108
104
  (void)pthread_mutex_destroy(&sleeper_mutex);
109
105
  (void)pthread_cond_destroy(&sleep_threshhold);
110
 
  VOID(pthread_mutex_destroy(&timer_alarm_mutex));
111
 
  VOID(pthread_cond_destroy(&timer_alarm_threshold));
112
 
  VOID(pthread_mutex_destroy(&row_lock));
 
106
  pthread_mutex_destroy(&timer_alarm_mutex);
 
107
  pthread_cond_destroy(&timer_alarm_threshold);
 
108
  pthread_mutex_destroy(&row_lock);
113
109
 
114
110
  return 0;
115
111
}
117
113
void scheduler(az_method use_aio)
118
114
{
119
115
  unsigned int x;
120
 
  unsigned long long total;
 
116
  uint64_t total;
121
117
  azio_stream writer_handle;
122
118
  thread_context_st *context;
123
119
  pthread_t mainthread;            /* Thread descriptor */
154
150
    context[x].use_aio= use_aio;
155
151
 
156
152
    /* now you create the thread */
157
 
    if (pthread_create(&mainthread, &attr, run_task,
 
153
    if (pthread_create(&mainthread, &attr, run_concurrent_task,
158
154
                       (void *)context) != 0)
159
155
    {
160
156
      fprintf(stderr,"Could not create thread\n");
170
166
    timer_alarm= true;
171
167
    pthread_mutex_unlock(&timer_alarm_mutex);
172
168
 
173
 
    if (pthread_create(&mainthread, &attr, timer_thread, 
 
169
    if (pthread_create(&mainthread, &attr, timer_thread,
174
170
                       (void *)&opt_timer_length) != 0)
175
171
    {
176
172
      fprintf(stderr,"%s: Could not create timer thread\n", my_progname);
207
203
  free(context);
208
204
  azclose(&writer_handle);
209
205
 
210
 
  printf("Read %llu rows\n", total);
 
206
  printf("Read %"PRIu64" rows\n", total);
211
207
}
212
208
 
213
209
void *timer_thread(void *p)
215
211
  time_t *timer_length= (time_t *)p;
216
212
  struct timespec abstime;
217
213
 
218
 
  if (drizzle_thread_init())
219
 
  {
220
 
    fprintf(stderr,"%s: drizzle_thread_init() failed.\n",
221
 
            my_progname);
222
 
    exit(1);
223
 
  }
224
 
 
225
 
  /* 
226
 
    We lock around the initial call in case were we in a loop. This 
 
214
  /*
 
215
    We lock around the initial call in case were we in a loop. This
227
216
    also keeps the value properly syncronized across call threads.
228
217
  */
229
218
  pthread_mutex_lock(&sleeper_mutex);
243
232
  timer_alarm= false;
244
233
  pthread_mutex_unlock(&timer_alarm_mutex);
245
234
 
246
 
  drizzle_thread_end();
247
 
 
248
235
  return 0;
249
236
}
250
237
 
251
 
void *run_task(void *p)
 
238
void *run_concurrent_task(void *p)
252
239
{
253
240
  thread_context_st *context= (thread_context_st *)p;
254
 
  unsigned long long count;
 
241
  uint64_t count;
255
242
  int ret;
256
243
  int error;
257
244
  azio_stream reader_handle;
258
245
 
259
 
  if (drizzle_thread_init())
260
 
  {
261
 
    fprintf(stderr,"%s: drizzle_thread_init() failed.\n", my_progname);
262
 
    exit(1);
263
 
  }
264
 
 
265
 
  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY,
 
246
  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY,
266
247
                    context->use_aio)))
267
248
  {
268
249
    printf("Could not open test file\n");
273
254
  while (master_wakeup)
274
255
  {
275
256
    pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
276
 
  } 
 
257
  }
277
258
  pthread_mutex_unlock(&sleeper_mutex);
278
259
 
279
260
  /* Do Stuff */
300
281
  pthread_mutex_unlock(&counter_mutex);
301
282
  azclose(&reader_handle);
302
283
 
303
 
  drizzle_thread_end();
304
 
 
305
284
  return NULL;
306
285
}
307
286
 
308
 
void create_data_file(azio_stream *write_handler, unsigned long long rows)
 
287
void create_data_file(azio_stream *write_handler, uint64_t rows)
309
288
{
310
289
  int ret;
311
 
  unsigned long long x;
 
290
  uint64_t x;
312
291
 
313
 
  if (!(ret= azopen(write_handler, TEST_FILENAME, O_CREAT|O_RDWR|O_TRUNC|O_BINARY,
 
292
  if (!(ret= azopen(write_handler, TEST_FILENAME, O_CREAT|O_RDWR|O_TRUNC,
314
293
                    AZ_METHOD_BLOCK)))
315
294
  {
316
295
    printf("Could not create test file\n");
333
312
  /* Avoid zero length strings */
334
313
  length++;
335
314
 
336
 
  get_random_string(buffer, length); 
 
315
  get_random_string(buffer, length);
337
316
  pthread_mutex_lock(&row_lock);
338
317
  azwrite_row(s, buffer, length);
339
318
  pthread_mutex_unlock(&row_lock);