00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
| /* q_selftest.c - unit test for q module. See https://github.com/fordsfords/q/tree/gh-pages */
/* This file is included by q.c for unit testing when it is built with "-DSELFTEST".
* It is structured this way so that whitebox testing can be performed (i.e. selftest code has
* visibility into q.c's private structures). */
/*
# This code and its documentation is Copyright 2014, 2015 Steven Ford, http://geeky-boy.com
# and licensed "public domain" style under Creative Commons "CC0": http://creativecommons.org/publicdomain/zero/1.0/
# To the extent possible under law, the contributors to this project have
# waived all copyright and related or neighboring rights to this work.
# In other words, you can use this code for any purpose without any
# restrictions. This work is published from: United States. The project home
# is https://github.com/fordsfords/q/tree/gh-pages
*/
#define UINT unsigned int
#define V2I (unsigned long long)(void *)
#define I2V (void *)(unsigned long long)
#define CHK_QERR_E(s,e) do { if (s != e) { printf("ERROR %s[%d]: qerr=%u\n", __FILE__, __LINE__, s); exit(1); } } while (0)
#define CHK_QERR(s) do { if (s != QERR_OK) { printf("ERROR %s[%d]: %d (%s)\n", __FILE__, __LINE__, s, q_qerr_str(s)); exit(1); } } while (0)
#define CHK_PTHREAD_ERR(s) do { if (s != 0) { printf("ERROR %s[%d]: %d (%s)\n", __FILE__, __LINE__, s, strerror(s)); exit(1); } } while (0)
#define ERR(s) do { printf("ERROR %s[%d]: %s\n", __FILE__, __LINE__, s); exit(1); } while (0)
struct tst_s {
UINT enq_cnt;
UINT full_cnt;
UINT full_spans;
UINT max_full_span;
char padding1[CACHE_LINE_SIZE - 4*sizeof(UINT)];
UINT deq_cnt;
UINT empty_cnt;
UINT empty_spans;
UINT max_empty_span;
char padding2[CACHE_LINE_SIZE - 4*sizeof(UINT)];
q_t *q1;
q_t *q2;
unsigned int loops;
};
typedef struct tst_s tst_t;
/* globals for testing */
struct timeval tmp_tv;
UINT phase1_start;
UINT phase1_end;
UINT phase2_start;
UINT phase2_end;
void *enq_thread(void *in_arg)
{
tst_t *test_data = (tst_t *)in_arg;
q_t *q = test_data->q1;
UINT full_span = 0;
UINT enq_cnt = 0;
UINT full_cnt = 0;
UINT full_spans = 0;
UINT max_full_span = 0;
unsigned int msg_num = test_data->loops;
while (msg_num > 0) {
qerr_t qerr = q_enq(q, I2V msg_num);
if (qerr == QERR_OK) {
/* prepare next message contents */
msg_num--;
/* collect stats */
enq_cnt++;
if (full_span > max_full_span) {
max_full_span = full_span; }
full_span = 0;
} else if (qerr == QERR_FULL) {
/* queue empty, collect stats */
full_cnt++;
if (full_span == 0) {
full_spans++; }
full_span++;
} else CHK_QERR(qerr);
} /* while */
gettimeofday(&tmp_tv, NULL); phase1_end = 1000000llu * (UINT)tmp_tv.tv_sec + (UINT)tmp_tv.tv_usec;
test_data->enq_cnt = enq_cnt;
test_data->full_cnt = full_cnt;
test_data->full_spans = full_spans;
test_data->max_full_span = max_full_span;
pthread_exit(NULL);
return NULL; /* don't need this, but keep the compiler happy */
} /* enq_thread */
void *deq_thread(void *in_arg)
{
tst_t *test_data = (tst_t *)in_arg;
q_t *q = test_data->q1;
unsigned int loops = test_data->loops;
UINT deq_cnt = 0;
UINT empty_cnt = 0;
UINT empty_spans = 0;
UINT max_empty_span = 0;
UINT cur_span = 0;
unsigned int msg_num = loops;
while (msg_num > 0) {
void *rtn_m;
qerr_t qerr = q_deq(q, &rtn_m);
if (qerr == QERR_OK) {
/* Make sure we've received the right message in sequence */
if (msg_num != (unsigned int)rtn_m) {
printf("ERROR %s[%d]: msg_num=%u, rtn_m=%u\n", __FILE__, __LINE__, msg_num, (unsigned int)rtn_m); exit(1); }
/* prepare for next message */
msg_num--;
/* collect stats */
if ((unsigned int)rtn_m == loops) { /* first message, ignore initial spins */
/* first message, start timing */
gettimeofday(&tmp_tv, NULL); phase1_start = 1000000llu * (UINT)tmp_tv.tv_sec + (UINT)tmp_tv.tv_usec;
empty_cnt = 0; cur_span = 0; empty_spans = 0;
}
deq_cnt++;
if (cur_span > max_empty_span) {
max_empty_span = cur_span; }
cur_span = 0;
} else if (qerr == QERR_EMPTY) {
/* queue empty, collect stats */
empty_cnt++;
if (cur_span == 0) {
empty_spans++; }
cur_span++;
} else CHK_QERR(qerr);
} /* while */
test_data->deq_cnt = deq_cnt;
test_data->empty_cnt = empty_cnt;
test_data->empty_spans = empty_spans;
test_data->max_empty_span = max_empty_span;
pthread_exit(NULL);
return NULL; /* don't need this, but keep the compiler happy */
} /* deq_thread */
void *ping_thread(void *in_arg)
{
tst_t *test_data = (tst_t *)in_arg;
q_t *q1 = test_data->q1;
q_t *q2 = test_data->q2;
unsigned int loops = test_data->loops;
unsigned int msg_num = loops;
while (msg_num > 0) {
void *rtn_m;
qerr_t qerr;
do {
qerr = q_deq(q1, &rtn_m);
} while (qerr == QERR_EMPTY);
CHK_QERR(qerr);
/* Make sure we've received the right message in sequence */
if (msg_num != (unsigned int)rtn_m) { printf("ERROR %s[%d]: msg_num=%u, rtn_m=%u\n", __FILE__, __LINE__, msg_num, (unsigned int)rtn_m); exit(1); }
/* prepare next message contents */
msg_num--;
/* bounce the message back */
if (msg_num > 0) {
qerr_t qerr = q_enq(q2, I2V msg_num); CHK_QERR(qerr);
/* prepare for next message */
msg_num--;
}
} /* while */
gettimeofday(&tmp_tv, NULL); phase2_end = 1000000llu * (UINT)tmp_tv.tv_sec + (UINT)tmp_tv.tv_usec;
pthread_exit(NULL);
return NULL; /* don't need this, but keep the compiler happy */
} /* ping_thead */
void *pong_thread(void *in_arg)
{
tst_t *test_data = (tst_t *)in_arg;
q_t *q1 = test_data->q1;
q_t *q2 = test_data->q2;
unsigned int loops = test_data->loops;
gettimeofday(&tmp_tv, NULL); phase2_start = 1000000llu * (UINT)tmp_tv.tv_sec + (UINT)tmp_tv.tv_usec;
unsigned int msg_num = loops;
msg_num--; /* pong gets second message */
while (msg_num > 0) {
void *rtn_m;
qerr_t qerr;
do {
qerr = q_deq(q2, &rtn_m);
} while (qerr == QERR_EMPTY);
CHK_QERR(qerr);
/* Make sure we've received the right message in sequence */
if (msg_num != (unsigned int)rtn_m) { printf("ERROR %s[%d]: msg_num=%u, rtn_m=%u\n", __FILE__, __LINE__, msg_num, (unsigned int)rtn_m); exit(1); }
/* prepare next message contents */
msg_num--;
/* bounce the message back */
if (msg_num > 0) {
qerr_t qerr = q_enq(q1, I2V msg_num); CHK_QERR(qerr);
/* prepare for next message */
msg_num--;
}
} /* while */
pthread_exit(NULL);
return NULL; /* don't need this, but keep the compiler happy */
} /* pong_thead */
void print_q(q_t *q)
{
printf("size_mask=%u\n", q->size_mask);
printf("enq_cnt=%u\n", q->enq_cnt);
printf("deq_cnt=%u\n", q->deq_cnt);
printf("msgs==%p\n", q->msgs);
} /* print_q */
/* Handle options */
struct opt_s {
long loops; /* number of loops for streaming test */
long depth; /* queue depth for streaming test */
long LOOPS; /* number of loops for ping-pong test */
long DEPTH; /* queue depth for ping-pong test */
};
typedef struct opt_s opt_t;
void opts_set_def(opt_t *opts)
{
opts->loops = 500000000;
opts->depth = 4096;
opts->LOOPS = 50000000;
opts->DEPTH = 2;
} /* opts_set_def */
char usage_str[] = "Usage: q_selftest [-l loops] [-d depth] [-L LOOPS] [-D DEPTH]\n"
"Where:\n"
" -l loops - number of times to loop the streaming test [%ld]\n"
" -d depth - queue depth to use for streaming test [%ld]\n"
" -L LOOPS - number of times to loop the ping-pong test [%ld]\n"
" -D DEPTH - queue depth to use for ping-pong test [%ld]\n";
void usage(opt_t *opts, char *m)
{
opts_set_def(opts); /* (re)set options to defaults so that we print the right values in the help */
if (m == NULL || m[0] == '\0') {
fprintf(stderr, usage_str, opts->loops, opts->depth, opts->LOOPS, opts->DEPTH);
exit(0);
} else {
fprintf(stderr, "%s\n", m);
fprintf(stderr, usage_str, opts->loops, opts->depth, opts->LOOPS, opts->DEPTH);
exit(1);
}
} /* usage */
void get_options(opt_t *opts, int argc, char **argv)
{
opts_set_def(opts); /* set options to defaults */
int o; char *p;
while ((o = getopt(argc, argv, "l:d:L:D:")) > 0) {
switch (o) {
case 'l':
p = NULL; errno = 0;
opts->loops = strtol(optarg, &p, 10);
if (errno != 0 || p == optarg || p == NULL || *p != '\0')
usage(opts, "Invalid numeric value for -l option");
break;
case 'd':
p = NULL; errno = 0;
opts->depth = strtol(optarg, &p, 10);
if (errno != 0 || p == optarg || p == NULL || *p != '\0')
usage(opts, "Invalid numeric value for -d option");
break;
case 'L':
p = NULL; errno = 0;
opts->LOOPS = strtol(optarg, &p, 10);
if (errno != 0 || p == optarg || p == NULL || *p != '\0')
usage(opts, "Invalid numeric value for -L option");
break;
case 'D':
p = NULL; errno = 0;
opts->DEPTH = strtol(optarg, &p, 10);
if (errno != 0 || p == optarg || p == NULL || *p != '\0')
usage(opts, "Invalid numeric value for -D option");
break;
default:
usage(opts, NULL);
} /* switch o */
} /* while getopt */
} /* get_options */
int main(int argc, char **argv)
{
opt_t opts;
qerr_t qerr;
get_options(&opts, argc, argv);
if (strcmp(q_qerr_str(QERR_OK), "QERR_OK") != 0) { ERR("q_qerr_str OK"); }
if (strcmp(q_qerr_str(QERR_BUG1), "QERR_BUG1") != 0) { ERR("q_qerr_str BUG1"); }
if (strcmp(q_qerr_str(QERR_BUG2), "QERR_BUG2") != 0) { ERR("q_qerr_str BUG2"); }
if (strcmp(q_qerr_str(QERR_BADSIZE), "QERR_BADSIZE") != 0) { ERR("q_qerr_str BADSIZE"); }
if (strcmp(q_qerr_str(QERR_MALLOCERR), "QERR_MALLOCERR") != 0) { ERR("q_qerr_str MALLOCERR"); }
if (strcmp(q_qerr_str(QERR_FULL), "QERR_FULL") != 0) { ERR("q_qerr_str FULL"); }
if (strcmp(q_qerr_str(QERR_EMPTY), "QERR_EMPTY") != 0) { ERR("q_qerr_str EMPTY"); }
if (strcmp(q_qerr_str(LAST_QERR), "QERR_EMPTY") != 0) { ERR("q_qerr_str LAST_QERR"); }
if (strcmp(q_qerr_str(LAST_QERR+1), "BAD_QERR") != 0) { ERR("q_qerr_str BAD_QERR"); }
if (strcmp(q_qerr_str(-1), "BAD_QERR") != 0) { ERR("q_qerr_str -1"); }
printf("FYI %s[%d]: q_qerr_str OK\n", __FILE__, __LINE__);
q_t *q = (q_t *)555;
qerr = q_create(&q, 0); CHK_QERR_E(qerr, QERR_BADSIZE);
qerr = q_create(&q, 1); CHK_QERR_E(qerr, QERR_BADSIZE);
qerr = q_create(&q, 3); CHK_QERR_E(qerr, QERR_BADSIZE);
qerr = q_create(&q, 5); CHK_QERR_E(qerr, QERR_BADSIZE);
qerr = q_create(&q, 6); CHK_QERR_E(qerr, QERR_BADSIZE);
qerr = q_create(&q, 7); CHK_QERR_E(qerr, QERR_BADSIZE);
if (q != (q_t *)555) { printf("ERROR %s[%d]: q=%p\n", __FILE__, __LINE__, q); exit(1); }
printf("FYI %s[%d]: BADSIZE OK\n", __FILE__, __LINE__);
qerr = q_create(&q, 2); CHK_QERR(qerr);
if (q->size_mask != 1) { printf("ERROR %s[%d]: size_mask=%u\n", __FILE__, __LINE__, q->size_mask); exit(1); }
if (q->enq_cnt != 0) { printf("ERROR %s[%d]: enq_cnt=%u\n", __FILE__, __LINE__, q->enq_cnt); exit(1); }
if (q->deq_cnt != 0) { printf("ERROR %s[%d]: deq_cnt=%u\n", __FILE__, __LINE__, q->deq_cnt); exit(1); }
if (q->msgs[0].in_use) { printf("ERROR %s[%d]: msgs[0].in_use=%u\n", __FILE__, __LINE__, (UINT)q->msgs[0].in_use); exit(1); }
if (q->msgs[1].in_use) { printf("ERROR %s[%d]: msgs[1].in_use=%u\n", __FILE__, __LINE__, (UINT)q->msgs[1].in_use); exit(1); }
if (q->msgs[2].d != I2V Q_FENCE) { printf("ERROR %s[%d]: msgs[2].d=%u\n", __FILE__, __LINE__, (UINT)q->msgs[2].d); exit(1); }
printf("FYI %s[%d]: q_create OK\n", __FILE__, __LINE__);
q_t save_q = *q;
if (!q_is_empty(q)) { ERR("not empty"); } if (q_is_full(q)) { ERR("full"); }
void *v = I2V 999;
qerr = q_deq(q, &v); CHK_QERR_E(qerr, QERR_EMPTY);
if (memcmp(q, &save_q, sizeof(save_q)) != 0) { printf("ERROR %s[%d]: q mismatch\n", __FILE__, __LINE__); exit(1); }
if (v != I2V 999) { printf("ERROR %s[%d]: v=%u\n", __FILE__, __LINE__, (unsigned int)v); exit(1); }
printf("FYI %s[%d]: EMPTY test OK\n", __FILE__, __LINE__);
if (!q_is_empty(q)) { ERR("not empty"); } if (q_is_full(q)) { ERR("full"); }
qerr = q_enq(q, I2V 111); CHK_QERR(qerr);
save_q.enq_cnt = 1;
if (memcmp(q, &save_q, sizeof(save_q)) != 0) { ERR("q mismatch"); }
if (q->msgs[2].d != I2V Q_FENCE) { printf("ERROR %s[%d]: msgs[2].d=%u\n", __FILE__, __LINE__, (UINT)q->msgs[2].d); exit(1); }
printf("FYI %s[%d]: q_enq OK\n", __FILE__, __LINE__);
if (q_is_empty(q)) { ERR("empty"); } if (!q_is_full(q)) { ERR("not full"); }
qerr = q_enq(q, I2V 222); CHK_QERR_E(qerr, QERR_FULL);
if (memcmp(q, &save_q, sizeof(save_q)) != 0) { printf("ERROR %s[%d]: q mismatch\n", __FILE__, __LINE__); exit(1); }
if (q->msgs[2].d != I2V Q_FENCE) { printf("ERROR %s[%d]: msgs[2].d=%u\n", __FILE__, __LINE__, (UINT)q->msgs[2].d); exit(1); }
printf("FYI %s[%d]: FULL test OK\n", __FILE__, __LINE__);
if (q_is_empty(q)) { ERR("empty"); } if (!q_is_full(q)) { ERR("not full"); }
qerr = q_deq(q, &v); CHK_QERR(qerr);
if (v != I2V 111) { printf("ERROR %s[%d]: v=%u\n", __FILE__, __LINE__, (unsigned int)v); exit(1); }
save_q.deq_cnt = 1;
if (memcmp(q, &save_q, sizeof(save_q)) != 0) { printf("ERROR %s[%d]: q mismatch\n", __FILE__, __LINE__); exit(1); }
if (q->msgs[2].d != I2V Q_FENCE) { printf("ERROR %s[%d]: msgs[2].d=%u\n", __FILE__, __LINE__, (UINT)q->msgs[2].d); exit(1); }
printf("FYI %s[%d]: q_deq OK\n", __FILE__, __LINE__);
if (!q_is_empty(q)) { ERR("not empty"); } if (q_is_full(q)) { ERR("full"); }
qerr = q_deq(q, &v); CHK_QERR_E(qerr, QERR_EMPTY);
if (memcmp(q, &save_q, sizeof(save_q)) != 0) { printf("ERROR %s[%d]: q mismatch\n", __FILE__, __LINE__); exit(1); }
printf("FYI %s[%d]: EMPTY test OK\n", __FILE__, __LINE__);
if (!q_is_empty(q)) { ERR("not empty"); } if (q_is_full(q)) { ERR("full"); }
qerr = q_delete(q); CHK_QERR(qerr);
printf("FYI %s[%d]: q_delete OK\n", __FILE__, __LINE__);
qerr = q_delete(q); CHK_QERR_E(qerr, QERR_BUG1);
/* Prepare for threading tests */
tst_t test_data; /* shared with test threads */
memset(&test_data, 0, sizeof(test_data));
qerr = q_create(&test_data.q1, opts.depth); CHK_QERR(qerr);
if (test_data.q1->msgs[opts.depth].d != I2V Q_FENCE) { printf("ERROR %s[%d]: msgs[opts.depth].d=%u\n", __FILE__, __LINE__, (UINT)test_data.q1->msgs[opts.depth].d); exit(1); }
test_data.loops = opts.loops;
int perr;
pthread_attr_t deq_attr; pthread_attr_init(&deq_attr); pthread_attr_setdetachstate(&deq_attr, PTHREAD_CREATE_JOINABLE);
pthread_t deq_t_handle;
perr = pthread_create(&deq_t_handle, &deq_attr, deq_thread, &test_data); CHK_PTHREAD_ERR(perr);
pthread_attr_destroy(&deq_attr);
pthread_attr_t enq_attr; pthread_attr_init(&enq_attr); pthread_attr_setdetachstate(&enq_attr, PTHREAD_CREATE_JOINABLE);
pthread_t enq_t_handle;
perr = pthread_create(&enq_t_handle, &enq_attr, enq_thread, &test_data); CHK_PTHREAD_ERR(perr);
pthread_attr_destroy(&enq_attr);
/* wait for threads to complete. */
perr = pthread_join(deq_t_handle, NULL); CHK_PTHREAD_ERR(perr); /* wait for deq first */
perr = pthread_join(enq_t_handle, NULL); CHK_PTHREAD_ERR(perr);
if (test_data.q1->msgs[opts.depth].d != I2V Q_FENCE) { printf("ERROR %s[%d]: msgs[opts.depth].d=%u\n", __FILE__, __LINE__, (UINT)test_data.q1->msgs[opts.depth].d); exit(1); }
if (test_data.q1->size_mask != opts.depth-1) { printf("ERROR %s[%d]: size_mask=%u\n", __FILE__, __LINE__, test_data.q1->size_mask); exit(1); }
if (test_data.q1->enq_cnt != test_data.loops) { printf("ERROR %s[%d]: enq_cnt=%u\n", __FILE__, __LINE__, test_data.q1->enq_cnt); exit(1); }
if (test_data.q1->deq_cnt != test_data.loops) { printf("ERROR %s[%d]: deq_cnt=%u\n", __FILE__, __LINE__, test_data.q1->deq_cnt); exit(1); }
qerr = q_delete(test_data.q1); CHK_QERR(qerr);
if (test_data.enq_cnt != test_data.loops) {
printf("ERROR %s[%d]: enq_cnt=%u, loops=%u\n", __FILE__, __LINE__, test_data.enq_cnt, test_data.loops); exit(1); }
if (test_data.deq_cnt != test_data.loops) {
printf("ERROR %s[%d]: deq_cnt=%u, loops=%u\n", __FILE__, __LINE__, test_data.deq_cnt, test_data.loops); exit(1); }
printf("FYI loops=%u, full_cnt=%u, empty_cnt=%u\n", test_data.loops, test_data.full_cnt, test_data.empty_cnt);
printf("FYI full_spans=%u, empty_spans=%u\n", test_data.full_spans, test_data.empty_spans);
printf("FYI max_full_span=%u, max_empty_span=%u\n", test_data.max_full_span, test_data.max_empty_span);
double duration = (double)(phase1_end - phase1_start);
double usec_per = duration / (double)test_data.loops;
printf("FYI duration=%f sec (%f ns/msg)\n", duration/1000000.0, usec_per*1000.0);
fflush(stdout);
/* Phase 2: ping-pong test */
memset(&test_data, 0, sizeof(test_data));
qerr = q_create(&test_data.q1, opts.DEPTH); CHK_QERR(qerr);
qerr = q_create(&test_data.q2, opts.DEPTH); CHK_QERR(qerr);
test_data.loops = opts.LOOPS;
/* put a message in the queue to be bounced back and forth */
qerr = q_enq(test_data.q1, I2V test_data.loops); CHK_QERR(qerr);
pthread_attr_t ping_attr; pthread_attr_init(&ping_attr); pthread_attr_setdetachstate(&ping_attr, PTHREAD_CREATE_JOINABLE);
pthread_t ping_t_handle;
perr = pthread_create(&ping_t_handle, &ping_attr, ping_thread, &test_data); CHK_PTHREAD_ERR(perr);
pthread_attr_destroy(&ping_attr);
pthread_attr_t pong_attr; pthread_attr_init(&pong_attr); pthread_attr_setdetachstate(&pong_attr, PTHREAD_CREATE_JOINABLE);
pthread_t pong_t_handle;
perr = pthread_create(&pong_t_handle, &pong_attr, pong_thread, &test_data); CHK_PTHREAD_ERR(perr);
pthread_attr_destroy(&pong_attr);
/* wait for threads to complete. */
perr = pthread_join(pong_t_handle, NULL); CHK_PTHREAD_ERR(perr); /* wait for pong first */
perr = pthread_join(ping_t_handle, NULL); CHK_PTHREAD_ERR(perr);
duration = phase2_end - phase2_start;
usec_per = duration / (double)test_data.loops;
printf("FYI ping/pong: msgs=%u, duration=%f sec (%f ns/msg one-way)\n", test_data.loops, duration/1000000.0, usec_per*1000.0);
fflush(stdout);
qerr = q_delete(test_data.q1); CHK_QERR(qerr);
qerr = q_delete(test_data.q2); CHK_QERR(qerr);
} /* main */
|