~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/archive/concurrency_test.c

  • Committer: Monty Taylor
  • Date: 2008-08-16 21:06:22 UTC
  • Revision ID: monty@inaugust.com-20080816210622-zpnn13unyinqzn72
Updated po files.

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