| 1 |
#include <ck_ring.h> |
| 2 |
#include <inttypes.h> |
| 3 |
#include <stdint.h> |
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
|
| 7 |
#include "../../common.h" |
| 8 |
|
| 9 |
#ifndef ITERATIONS |
| 10 |
#define ITERATIONS (128000) |
| 11 |
#endif |
| 12 |
|
| 13 |
struct entry { |
| 14 |
int tid; |
| 15 |
int value; |
| 16 |
}; |
| 17 |
|
| 18 |
int |
| 19 |
main(int argc, char *argv[]) |
| 20 |
{ |
| 21 |
int i, r, size; |
| 22 |
uint64_t s, e, e_a, d_a; |
| 23 |
struct entry entry = {0, 0}; |
| 24 |
ck_ring_buffer_t *buf; |
| 25 |
ck_ring_t ring; |
| 26 |
|
| 27 |
if (argc != 2) { |
| 28 |
ck_error("Usage: latency <size>\n"); |
| 29 |
} |
| 30 |
|
| 31 |
size = atoi(argv[1]); |
| 32 |
if (size <= 4 || (size & (size - 1))) { |
| 33 |
ck_error("ERROR: Size must be a power of 2 greater than 4.\n"); |
| 34 |
} |
| 35 |
|
| 36 |
buf = malloc(sizeof(ck_ring_buffer_t) * size); |
| 37 |
if (buf == NULL) { |
| 38 |
ck_error("ERROR: Failed to allocate buffer\n"); |
| 39 |
} |
| 40 |
|
| 41 |
ck_ring_init(&ring, size); |
| 42 |
|
| 43 |
e_a = d_a = s = e = 0; |
| 44 |
for (r = 0; r < ITERATIONS; r++) { |
| 45 |
for (i = 0; i < size / 4; i += 4) { |
| 46 |
s = rdtsc(); |
| 47 |
ck_ring_enqueue_spsc(&ring, buf, &entry); |
| 48 |
ck_ring_enqueue_spsc(&ring, buf, &entry); |
| 49 |
ck_ring_enqueue_spsc(&ring, buf, &entry); |
| 50 |
ck_ring_enqueue_spsc(&ring, buf, &entry); |
| 51 |
e = rdtsc(); |
| 52 |
} |
| 53 |
e_a += (e - s) / 4; |
| 54 |
|
| 55 |
for (i = 0; i < size / 4; i += 4) { |
| 56 |
s = rdtsc(); |
| 57 |
ck_ring_dequeue_spsc(&ring, buf, &entry); |
| 58 |
ck_ring_dequeue_spsc(&ring, buf, &entry); |
| 59 |
ck_ring_dequeue_spsc(&ring, buf, &entry); |
| 60 |
ck_ring_dequeue_spsc(&ring, buf, &entry); |
| 61 |
e = rdtsc(); |
| 62 |
} |
| 63 |
d_a += (e - s) / 4; |
| 64 |
} |
| 65 |
|
| 66 |
printf("spsc %10d %16" PRIu64 " %16" PRIu64 "\n", size, e_a / ITERATIONS, d_a / ITERATIONS); |
| 67 |
|
| 68 |
e_a = d_a = s = e = 0; |
| 69 |
for (r = 0; r < ITERATIONS; r++) { |
| 70 |
for (i = 0; i < size / 4; i += 4) { |
| 71 |
s = rdtsc(); |
| 72 |
ck_ring_enqueue_spmc(&ring, buf, &entry); |
| 73 |
ck_ring_enqueue_spmc(&ring, buf, &entry); |
| 74 |
ck_ring_enqueue_spmc(&ring, buf, &entry); |
| 75 |
ck_ring_enqueue_spmc(&ring, buf, &entry); |
| 76 |
e = rdtsc(); |
| 77 |
} |
| 78 |
e_a += (e - s) / 4; |
| 79 |
|
| 80 |
for (i = 0; i < size / 4; i += 4) { |
| 81 |
s = rdtsc(); |
| 82 |
ck_ring_dequeue_spmc(&ring, buf, &entry); |
| 83 |
ck_ring_dequeue_spmc(&ring, buf, &entry); |
| 84 |
ck_ring_dequeue_spmc(&ring, buf, &entry); |
| 85 |
ck_ring_dequeue_spmc(&ring, buf, &entry); |
| 86 |
e = rdtsc(); |
| 87 |
} |
| 88 |
d_a += (e - s) / 4; |
| 89 |
} |
| 90 |
|
| 91 |
printf("spmc %10d %16" PRIu64 " %16" PRIu64 "\n", size, e_a / ITERATIONS, d_a / ITERATIONS); |
| 92 |
|
| 93 |
ck_ring_init(&ring, size); |
| 94 |
e_a = d_a = s = e = 0; |
| 95 |
for (r = 0; r < ITERATIONS; r++) { |
| 96 |
for (i = 0; i < size / 4; i += 4) { |
| 97 |
s = rdtsc(); |
| 98 |
ck_ring_enqueue_mpsc(&ring, buf, &entry); |
| 99 |
ck_ring_enqueue_mpsc(&ring, buf, &entry); |
| 100 |
ck_ring_enqueue_mpsc(&ring, buf, &entry); |
| 101 |
ck_ring_enqueue_mpsc(&ring, buf, &entry); |
| 102 |
e = rdtsc(); |
| 103 |
} |
| 104 |
e_a += (e - s) / 4; |
| 105 |
|
| 106 |
for (i = 0; i < size / 4; i += 4) { |
| 107 |
s = rdtsc(); |
| 108 |
ck_ring_dequeue_mpsc(&ring, buf, &entry); |
| 109 |
ck_ring_dequeue_mpsc(&ring, buf, &entry); |
| 110 |
ck_ring_dequeue_mpsc(&ring, buf, &entry); |
| 111 |
ck_ring_dequeue_mpsc(&ring, buf, &entry); |
| 112 |
e = rdtsc(); |
| 113 |
} |
| 114 |
d_a += (e - s) / 4; |
| 115 |
} |
| 116 |
printf("mpsc %10d %16" PRIu64 " %16" PRIu64 "\n", size, e_a / ITERATIONS, d_a / ITERATIONS); |
| 117 |
ck_ring_init(&ring, size); |
| 118 |
e_a = d_a = s = e = 0; |
| 119 |
for (r = 0; r < ITERATIONS; r++) { |
| 120 |
for (i = 0; i < size / 4; i += 4) { |
| 121 |
s = rdtsc(); |
| 122 |
ck_ring_enqueue_mpmc(&ring, buf, &entry); |
| 123 |
ck_ring_enqueue_mpmc(&ring, buf, &entry); |
| 124 |
ck_ring_enqueue_mpmc(&ring, buf, &entry); |
| 125 |
ck_ring_enqueue_mpmc(&ring, buf, &entry); |
| 126 |
e = rdtsc(); |
| 127 |
} |
| 128 |
e_a += (e - s) / 4; |
| 129 |
|
| 130 |
for (i = 0; i < size / 4; i += 4) { |
| 131 |
s = rdtsc(); |
| 132 |
ck_ring_dequeue_mpmc(&ring, buf, &entry); |
| 133 |
ck_ring_dequeue_mpmc(&ring, buf, &entry); |
| 134 |
ck_ring_dequeue_mpmc(&ring, buf, &entry); |
| 135 |
ck_ring_dequeue_mpmc(&ring, buf, &entry); |
| 136 |
e = rdtsc(); |
| 137 |
} |
| 138 |
d_a += (e - s) / 4; |
| 139 |
} |
| 140 |
printf("mpmc %10d %16" PRIu64 " %16" PRIu64 "\n", size, e_a / ITERATIONS, d_a / ITERATIONS); |
| 141 |
return (0); |
| 142 |
} |