summaryrefslogtreecommitdiffhomepage
path: root/src/test/nxt_cq_test.c
blob: ae69505a3a47d5e6320b14a119aab6a5ef3d25f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578

/*
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>
#include <math.h>
#include <inttypes.h>

#ifndef NXT_NCQ_TEST
#define NXT_NCQ_TEST          1
#endif

#define NXT_QTEST_USE_THREAD  0

#if NXT_NCQ_TEST
#include <nxt_nncq.h>
#else
#include <nxt_nvbcq.h>
#endif


#define MAX_ITER   20
#define STAT_ITER  5
#define MIN_COV    0.02

extern char  **environ;
static uintptr_t nops = 10000000;

static uintptr_t nprocs_enq     = 0;
static uintptr_t nprocs_deq     = 0;
static uintptr_t nprocs_wenq    = 0;
static uintptr_t nprocs_wdeq    = 0;
static uintptr_t nprocs_enq_deq = 0;
static uintptr_t nprocs_cas     = 0;
static uintptr_t nprocs_faa     = 0;

static uintptr_t nprocs = 1;


static size_t
elapsed_time(size_t us)
{
  struct timeval t;

  gettimeofday(&t, NULL);

  return t.tv_sec * 1000000 + t.tv_usec - us;
}


static double
mean(const double *times, int n)
{
    int     i;
    double  sum;

    sum = 0;

    for (i = 0; i < n; i++) {
        sum += times[i];
    }

    return sum / n;
}


static double
cov(const double *times, double mean, int n)
{
    int     i;
    double  variance;

    variance = 0;

    for (i = 0; i < n; i++) {
        variance += (times[i] - mean) * (times[i] - mean);
    }

    variance /= n;

    return sqrt(variance) / mean;
}

typedef struct {
#if NXT_NCQ_TEST
    nxt_nncq_t   free_queue;
    nxt_nncq_t   active_queue;
#else
    nxt_nvbcq_t  free_queue;
    nxt_nvbcq_t  active_queue;
#endif
    uint32_t     counter;
} nxt_cq_t;


static nxt_cq_t  *pgq;


#if NXT_NCQ_TEST
#define nxt_cq_enqueue  nxt_nncq_enqueue
#define nxt_cq_dequeue  nxt_nncq_dequeue
#define nxt_cq_empty    nxt_nncq_empty
#define nxt_cq_init     nxt_nncq_init
#define NXT_CQ_SIZE     NXT_NNCQ_SIZE
#else
#define nxt_cq_enqueue  nxt_nvbcq_enqueue
#define nxt_cq_dequeue  nxt_nvbcq_dequeue
#define nxt_cq_empty    nxt_nvbcq_empty
#define nxt_cq_init     nxt_nvbcq_init
#define NXT_CQ_SIZE     NXT_NVBCQ_SIZE
#endif

typedef struct {
    int                  id;
    uint64_t             enq;
    uint64_t             deq;
    uint64_t             wait_enq;
    uint64_t             wait_deq;
    uint64_t             own_res;
    uint64_t             cas;
    uint64_t             faa;

#if NXT_QTEST_USE_THREAD
    nxt_thread_handle_t  handle;
#else
    nxt_pid_t            pid;
    int status;
#endif
} nxt_worker_info_t;


static void
cas_worker(void *p)
{
    nxt_cq_t           *q;
    uint32_t           c;
    uintptr_t          i;
    nxt_worker_info_t  *wi;

    q = pgq;
    wi = p;

    for (i = 0; i < nops / nprocs_cas; i++) {
        c = q->counter;

        if (nxt_atomic_cmp_set(&q->counter, c, c + 1)) {
            ++wi->cas;
        }
    }
}


static void
faa_worker(void *p)
{
    nxt_cq_t           *q;
    uintptr_t          i;
    nxt_worker_info_t  *wi;

    q = pgq;
    wi = p;

    for (i = 0; i < nops / nprocs_faa; i++) {
        nxt_atomic_fetch_add(&q->counter, 1);
        wi->faa++;
    }
}


static void
enq_deq_worker(void *p)
{
    nxt_cq_t           *q;
    uintptr_t          i, v;
    nxt_worker_info_t  *wi;

    q = pgq;
    wi = p;

    for (i = 0; i < nops / nprocs_enq_deq; i++) {
        v = nxt_cq_dequeue(&q->free_queue);

        if (v != nxt_cq_empty(&q->free_queue)) {
            nxt_cq_enqueue(&q->active_queue, wi->id);
            wi->enq++;
        }

        v = nxt_cq_dequeue(&q->active_queue);

        if (v != nxt_cq_empty(&q->active_queue)) {
            nxt_cq_enqueue(&q->free_queue, v);
            wi->deq++;

            if ((int) v == wi->id) {
                wi->own_res++;
            }
        }
    }
}


static void
enq_worker(void *p)
{
    nxt_cq_t           *q;
    uintptr_t          i, v;
    nxt_worker_info_t  *wi;

    q = pgq;
    wi = p;

    for (i = 0; i < nops / nprocs_enq; i++) {
        v = nxt_cq_dequeue(&q->free_queue);

        if (v != nxt_cq_empty(&q->free_queue)) {
            nxt_cq_enqueue(&q->active_queue, v);
            wi->enq++;
        }
    }
}


static void
deq_worker(void *p)
{
    nxt_cq_t           *q;
    uintptr_t          i, v;
    nxt_worker_info_t  *wi;

    q = pgq;
    wi = p;

    for (i = 0; i < nops / nprocs_deq; i++) {
        v = nxt_cq_dequeue(&q->active_queue);

        if (v != nxt_cq_empty(&q->active_queue)) {
            nxt_cq_enqueue(&q->free_queue, v);
            ++wi->deq;
        }
    }
}


static void
wenq_worker(void *p)
{
    nxt_cq_t           *q;
    uintptr_t          i, v;
    nxt_worker_info_t  *wi;

    q = pgq;
    wi = p;

    for (i = 0; i < nops / nprocs_wenq; i++) {

        do {
            wi->wait_enq++;
            v = nxt_cq_dequeue(&q->free_queue);
        } while (v == nxt_cq_empty(&q->free_queue));

        nxt_cq_enqueue(&q->active_queue, v);

        wi->enq++;
        wi->wait_enq--;
    }
}


static void
wdeq_worker(void *p)
{
    nxt_cq_t           *q;
    uintptr_t          i, v;
    nxt_worker_info_t  *wi;

    q = pgq;
    wi = p;

    for (i = 0; i < nops / nprocs_wdeq; i++) {

        do {
            wi->wait_deq++;
            v = nxt_cq_dequeue(&q->active_queue);
        } while (v == nxt_cq_empty(&q->active_queue));

        nxt_cq_enqueue(&q->free_queue, v);

        wi->deq++;
        wi->wait_deq--;
    }
}


static nxt_int_t
worker_create(nxt_worker_info_t *wi, int id, nxt_thread_start_t start)
{
    wi->id = id;

#if NXT_QTEST_USE_THREAD
    nxt_thread_link_t  *link;

    link = nxt_zalloc(sizeof(nxt_thread_link_t));

    link->start = start;
    link->work.data = wi;

    return nxt_thread_create(&wi->handle, link);

#else
    pid_t pid = fork();

    if (pid == 0) {
        start(wi);
        exit(0);

    } else {
        wi->pid = pid;
    }

    return NXT_OK;
#endif
}


static void
worker_wait(nxt_worker_info_t *wi)
{
#if NXT_QTEST_USE_THREAD
    pthread_join(wi->handle, NULL);

#else
    waitpid(wi->pid, &wi->status, 0);
#endif
}


int nxt_cdecl
main(int argc, char **argv)
{
    int                i, k, id, verbose, objective, rk;
    char               *a;
    size_t             start, elapsed;
    double             *stats, m, c;
    uint64_t           total_ops;
    uintptr_t          j;
    nxt_task_t         task;
    nxt_thread_t       *thr;
    nxt_worker_info_t  *wi;
    double             times[MAX_ITER], mopsec[MAX_ITER];

    verbose = 0;
    objective = 0;

    for (i = 1; i < argc; i++) {
        a = argv[i];

        if (strcmp(a, "-v") == 0) {
            verbose++;
            continue;
        }

        if (strcmp(a, "-n") == 0 && (i + 1) < argc) {
            nops = atoi(argv[++i]);
            continue;
        }

        if (strcmp(a, "--enq") == 0 && (i + 1) < argc) {
            nprocs_enq = atoi(argv[++i]);
            continue;
        }

        if (strcmp(a, "--deq") == 0 && (i + 1) < argc) {
            nprocs_deq = atoi(argv[++i]);
            continue;
        }

        if (strcmp(a, "--wenq") == 0 && (i + 1) < argc) {
            nprocs_wenq = atoi(argv[++i]);
            continue;
        }

        if (strcmp(a, "--wdeq") == 0 && (i + 1) < argc) {
            nprocs_wdeq = atoi(argv[++i]);
            continue;
        }

        if (strcmp(a, "--ed") == 0 && (i + 1) < argc) {
            nprocs_enq_deq = atoi(argv[++i]);
            continue;
        }

        if (strcmp(a, "--cas") == 0 && (i + 1) < argc) {
            nprocs_cas = atoi(argv[++i]);
            continue;
        }

        if (strcmp(a, "--faa") == 0 && (i + 1) < argc) {
            nprocs_faa = atoi(argv[++i]);
            continue;
        }

        if (strcmp(a, "--obj") == 0 && (i + 1) < argc) {
            objective = atoi(argv[++i]);
            continue;
        }

        printf("unknown option %s", a);

        return 1;
    }

    if (nxt_lib_start("ncq_test", argv, &environ) != NXT_OK) {
        return 1;
    }

    nprocs = nprocs_enq + nprocs_deq + nprocs_wenq + nprocs_wdeq
             + nprocs_enq_deq + nprocs_cas + nprocs_faa;

    if (nprocs == 0) {
        return 0;
    }

    nxt_main_log.level = NXT_LOG_INFO;
    task.log  = &nxt_main_log;

    thr = nxt_thread();
    thr->task = &task;

    pgq = mmap(NULL, sizeof(nxt_cq_t), PROT_READ | PROT_WRITE,
               MAP_ANON | MAP_SHARED, -1, 0);
    if (pgq == MAP_FAILED) {
        return 2;
    }

    nxt_cq_init(&pgq->free_queue);
    nxt_cq_init(&pgq->active_queue);

    for(i = 0; i < NXT_CQ_SIZE; i++) {
        nxt_cq_enqueue(&pgq->free_queue, i);
    }

    if (verbose >= 1) {
        printf("number of workers: %d\n", (int) nprocs);
        printf("number of ops:     %d\n", (int) nops);
    }

    wi = mmap(NULL, nprocs * sizeof(nxt_worker_info_t), PROT_READ | PROT_WRITE,
              MAP_ANON | MAP_SHARED, -1, 0);
    if (wi == MAP_FAILED) {
        return 3;
    }

    for (k = 0; k < MAX_ITER; k++) {
        nxt_memzero(wi, nprocs * sizeof(nxt_worker_info_t));

        nxt_cq_init(&pgq->free_queue);
        nxt_cq_init(&pgq->active_queue);

        for(i = 0; i < NXT_CQ_SIZE; i++) {
            nxt_cq_enqueue(&pgq->free_queue, i);
        }

        start = elapsed_time(0);

        id = 0;

        for (j = 0; j < nprocs_enq; j++, id++) {
            worker_create(wi + id, id, enq_worker);
        }

        for (j = 0; j < nprocs_deq; j++, id++) {
            worker_create(wi + id, id, deq_worker);
        }

        for (j = 0; j < nprocs_wenq; j++, id++) {
            worker_create(wi + id, id, wenq_worker);
        }

        for (j = 0; j < nprocs_wdeq; j++, id++) {
            worker_create(wi + id, id, wdeq_worker);
        }

        for (j = 0; j < nprocs_enq_deq; j++, id++) {
            worker_create(wi + id, id, enq_deq_worker);
        }

        for (j = 0; j < nprocs_cas; j++, id++) {
            worker_create(wi + id, id, cas_worker);
        }

        for (j = 0; j < nprocs_faa; j++, id++) {
            worker_create(wi + id, id, faa_worker);
        }

        for (j = 0; j < nprocs; j++) {
            worker_wait(wi + j);
        }

        elapsed = elapsed_time(start);

        for (j = 1; j < nprocs; j++) {
            wi[0].enq += wi[j].enq;
            wi[0].deq += wi[j].deq;
            wi[0].wait_enq += wi[j].wait_enq;
            wi[0].wait_deq += wi[j].wait_deq;
            wi[0].own_res += wi[j].own_res;
            wi[0].cas += wi[j].cas;
            wi[0].faa += wi[j].faa;
        }

        total_ops = wi[0].enq + wi[0].deq + wi[0].cas + wi[0].faa;

        if (total_ops == 0) {
            total_ops = nops;
        }

        times[k] = elapsed / 1000.0;
        mopsec[k] = (double) total_ops / elapsed;

        if (verbose >= 2) {
            printf("enq        %10"PRIu64"\n", wi[0].enq);
            printf("deq        %10"PRIu64"\n", wi[0].deq);
            printf("wait_enq   %10"PRIu64"\n", wi[0].wait_enq);
            printf("wait_deq   %10"PRIu64"\n", wi[0].wait_deq);
            printf("own_res    %10"PRIu64"\n", wi[0].own_res);
            printf("cas        %10"PRIu64"\n", wi[0].cas);
            printf("faa        %10"PRIu64"\n", wi[0].faa);
            printf("total ops  %10"PRIu64"\n", total_ops);
            printf("Mops/sec   %13.2f\n", mopsec[k]);

            printf("elapsed    %10d us\n", (int) elapsed);
            printf("per op     %10d ns\n", (int) ((1000 * elapsed) / total_ops));
        }

        if (k >= STAT_ITER) {
            stats = (objective == 0) ? times : mopsec;

            m = mean(stats + k - STAT_ITER, STAT_ITER);
            c = cov(stats + k - STAT_ITER, m, STAT_ITER);

            if (verbose >= 1) {
                if (objective == 0) {
                    printf("  #%02d elapsed time: %.2f ms; Mops/sec %.2f; "
                           "mean time %.2f ms; cov %.4f\n",
                           (int) k + 1, times[k], mopsec[k], m, c);

                } else {
                    printf("  #%02d elapsed time: %.2f ms; Mops/sec %.2f; "
                           "mean Mop/sec %.2f; cov %.4f\n",
                           (int) k + 1, times[k], mopsec[k], m, c);
                }
            }

            if (c < MIN_COV) {
                rk = k - STAT_ITER;

                for (i = rk + 1; i <= k; i++) {
                    if (fabs(stats[i] - m) < fabs(stats[rk] - m)) {
                        rk = i;
                    }
                }

                printf("#%d %.2f ms; %.2f\n", rk, times[rk], mopsec[rk]);

                return 0;
            }

        } else {
            if (verbose >= 1) {
                printf("  #%02d elapsed time: %.2f ms; Mops/sec %.2f\n",
                       (int) k + 1, times[k], mopsec[k]);
            }
        }
    }

    return 0;
}