static void * test(void *c) { struct context *context = c; struct entry *entry; int i, j; bool r; ck_barrier_centralized_state_t sense = CK_BARRIER_CENTRALIZED_STATE_INITIALIZER; if (aff_iterate(&a)) { perror("ERROR: Could not affine thread"); exit(EXIT_FAILURE); } if (context->tid == 0) { struct entry *entries; entries = malloc(sizeof(struct entry) * size); assert(entries != NULL); if (ck_ring_size(ring) != 0) { ck_error("More entries than expected: %u > 0\n", ck_ring_size(ring)); } for (i = 0; i < size; i++) { entries[i].value = i; entries[i].tid = 0; r = ck_ring_enqueue_spsc(ring, entries + i); assert(r != false); } if (ck_ring_size(ring) != (unsigned int)size) { ck_error("Less entries than expected: %u < %d\n", ck_ring_size(ring), size); } if (ck_ring_capacity(ring) != ck_ring_size(ring) + 1) { ck_error("Capacity less than expected: %u < %u\n", ck_ring_size(ring), ck_ring_capacity(ring)); } } ck_barrier_centralized(&barrier, &sense, nthr); for (i = 0; i < ITERATIONS; i++) { for (j = 0; j < size; j++) { while (ck_ring_dequeue_spsc(ring + context->previous, &entry) == false); if (context->previous != (unsigned int)entry->tid) { ck_error("[%u:%p] %u != %u\n", context->tid, (void *)entry, entry->tid, context->previous); } if (entry->value != j) { ck_error("[%u:%p] %u != %u\n", context->tid, (void *)entry, entry->tid, context->previous); } entry->tid = context->tid; r = ck_ring_enqueue_spsc(ring + context->tid, entry); assert(r == true); } } return NULL; }
int main(int argc, char *argv[]) { int i, r, size; uint64_t s, e, e_a, d_a; struct entry entry = {0, 0}; ck_ring_buffer_t *buf; ck_ring_t ring; if (argc != 2) { ck_error("Usage: latency <size>\n"); } size = atoi(argv[1]); if (size <= 4 || (size & (size - 1))) { ck_error("ERROR: Size must be a power of 2 greater than 4.\n"); } buf = malloc(sizeof(ck_ring_buffer_t) * size); if (buf == NULL) { ck_error("ERROR: Failed to allocate buffer\n"); } ck_ring_init(&ring, size); e_a = d_a = s = e = 0; for (r = 0; r < ITERATIONS; r++) { for (i = 0; i < size / 4; i += 4) { s = rdtsc(); ck_ring_enqueue_spsc(&ring, buf, &entry); ck_ring_enqueue_spsc(&ring, buf, &entry); ck_ring_enqueue_spsc(&ring, buf, &entry); ck_ring_enqueue_spsc(&ring, buf, &entry); e = rdtsc(); } e_a += (e - s) / 4; for (i = 0; i < size / 4; i += 4) { s = rdtsc(); ck_ring_dequeue_spsc(&ring, buf, &entry); ck_ring_dequeue_spsc(&ring, buf, &entry); ck_ring_dequeue_spsc(&ring, buf, &entry); ck_ring_dequeue_spsc(&ring, buf, &entry); e = rdtsc(); } d_a += (e - s) / 4; } printf("spsc %10d %16" PRIu64 " %16" PRIu64 "\n", size, e_a / ITERATIONS, d_a / ITERATIONS); e_a = d_a = s = e = 0; for (r = 0; r < ITERATIONS; r++) { for (i = 0; i < size / 4; i += 4) { s = rdtsc(); ck_ring_enqueue_spmc(&ring, buf, &entry); ck_ring_enqueue_spmc(&ring, buf, &entry); ck_ring_enqueue_spmc(&ring, buf, &entry); ck_ring_enqueue_spmc(&ring, buf, &entry); e = rdtsc(); } e_a += (e - s) / 4; for (i = 0; i < size / 4; i += 4) { s = rdtsc(); ck_ring_dequeue_spmc(&ring, buf, &entry); ck_ring_dequeue_spmc(&ring, buf, &entry); ck_ring_dequeue_spmc(&ring, buf, &entry); ck_ring_dequeue_spmc(&ring, buf, &entry); e = rdtsc(); } d_a += (e - s) / 4; } printf("spmc %10d %16" PRIu64 " %16" PRIu64 "\n", size, e_a / ITERATIONS, d_a / ITERATIONS); ck_ring_init(&ring, size); e_a = d_a = s = e = 0; for (r = 0; r < ITERATIONS; r++) { for (i = 0; i < size / 4; i += 4) { s = rdtsc(); ck_ring_enqueue_mpsc(&ring, buf, &entry); ck_ring_enqueue_mpsc(&ring, buf, &entry); ck_ring_enqueue_mpsc(&ring, buf, &entry); ck_ring_enqueue_mpsc(&ring, buf, &entry); e = rdtsc(); } e_a += (e - s) / 4; for (i = 0; i < size / 4; i += 4) { s = rdtsc(); ck_ring_dequeue_mpsc(&ring, buf, &entry); ck_ring_dequeue_mpsc(&ring, buf, &entry); ck_ring_dequeue_mpsc(&ring, buf, &entry); ck_ring_dequeue_mpsc(&ring, buf, &entry); e = rdtsc(); } d_a += (e - s) / 4; } printf("mpsc %10d %16" PRIu64 " %16" PRIu64 "\n", size, e_a / ITERATIONS, d_a / ITERATIONS); ck_ring_init(&ring, size); e_a = d_a = s = e = 0; for (r = 0; r < ITERATIONS; r++) { for (i = 0; i < size / 4; i += 4) { s = rdtsc(); ck_ring_enqueue_mpmc(&ring, buf, &entry); ck_ring_enqueue_mpmc(&ring, buf, &entry); ck_ring_enqueue_mpmc(&ring, buf, &entry); ck_ring_enqueue_mpmc(&ring, buf, &entry); e = rdtsc(); } e_a += (e - s) / 4; for (i = 0; i < size / 4; i += 4) { s = rdtsc(); ck_ring_dequeue_mpmc(&ring, buf, &entry); ck_ring_dequeue_mpmc(&ring, buf, &entry); ck_ring_dequeue_mpmc(&ring, buf, &entry); ck_ring_dequeue_mpmc(&ring, buf, &entry); e = rdtsc(); } d_a += (e - s) / 4; } printf("mpmc %10d %16" PRIu64 " %16" PRIu64 "\n", size, e_a / ITERATIONS, d_a / ITERATIONS); return (0); }