Debian Codegraph @

regressions/ck_fifo/benchmark/latency.c

raw
1 /*
2 * Copyright 2011-2015 Samy Al Bahra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <ck_fifo.h>
28 #include <ck_spinlock.h>
29 #include <inttypes.h>
30 #include <stdint.h>
31 #include <stdio.h>
32
33 #include "../../common.h"
34
35 #ifndef ENTRIES
36 #define ENTRIES 4096
37 #endif
38
39 #ifndef STEPS
40 #define STEPS 40000
41 #endif
42
43 int
44 main(void)
45 {
46 ck_spinlock_fas_t mutex = CK_SPINLOCK_FAS_INITIALIZER;
47 void *r;
48 uint64_t s, e, a;
49 unsigned int i;
50 unsigned int j;
51
52 #if defined(CK_F_FIFO_SPSC)
53 ck_fifo_spsc_t spsc_fifo;
54 ck_fifo_spsc_entry_t spsc_entry[ENTRIES];
55 ck_fifo_spsc_entry_t spsc_stub;
56 #endif
57
58 #if defined(CK_F_FIFO_MPMC)
59 ck_fifo_mpmc_t mpmc_fifo;
60 ck_fifo_mpmc_entry_t mpmc_entry[ENTRIES];
61 ck_fifo_mpmc_entry_t mpmc_stub;
62 ck_fifo_mpmc_entry_t *garbage;
63 #endif
64
65 #ifdef CK_F_FIFO_SPSC
66 a = 0;
67 for (i = 0; i < STEPS; i++) {
68 ck_fifo_spsc_init(&spsc_fifo, &spsc_stub);
69
70 s = rdtsc();
71 for (j = 0; j < ENTRIES; j++) {
72 ck_spinlock_fas_lock(&mutex);
73 ck_fifo_spsc_enqueue(&spsc_fifo, spsc_entry + j, NULL);
74 ck_spinlock_fas_unlock(&mutex);
75 }
76 e = rdtsc();
77
78 a += e - s;
79 }
80 printf(" spinlock_enqueue: %16" PRIu64 "\n", a / STEPS / (sizeof(spsc_entry) / sizeof(*spsc_entry)));
81
82 a = 0;
83 for (i = 0; i < STEPS; i++) {
84 ck_fifo_spsc_init(&spsc_fifo, &spsc_stub);
85 for (j = 0; j < ENTRIES; j++)
86 ck_fifo_spsc_enqueue(&spsc_fifo, spsc_entry + j, NULL);
87
88 s = rdtsc();
89 for (j = 0; j < ENTRIES; j++) {
90 ck_spinlock_fas_lock(&mutex);
91 ck_fifo_spsc_dequeue(&spsc_fifo, &r);
92 ck_spinlock_fas_unlock(&mutex);
93 }
94 e = rdtsc();
95 a += e - s;
96 }
97 printf(" spinlock_dequeue: %16" PRIu64 "\n", a / STEPS / (sizeof(spsc_entry) / sizeof(*spsc_entry)));
98
99 a = 0;
100 for (i = 0; i < STEPS; i++) {
101 ck_fifo_spsc_init(&spsc_fifo, &spsc_stub);
102
103 s = rdtsc();
104 for (j = 0; j < ENTRIES; j++)
105 ck_fifo_spsc_enqueue(&spsc_fifo, spsc_entry + j, NULL);
106 e = rdtsc();
107
108 a += e - s;
109 }
110 printf("ck_fifo_spsc_enqueue: %16" PRIu64 "\n", a / STEPS / (sizeof(spsc_entry) / sizeof(*spsc_entry)));
111
112 a = 0;
113 for (i = 0; i < STEPS; i++) {
114 ck_fifo_spsc_init(&spsc_fifo, &spsc_stub);
115 for (j = 0; j < ENTRIES; j++)
116 ck_fifo_spsc_enqueue(&spsc_fifo, spsc_entry + j, NULL);
117
118 s = rdtsc();
119 for (j = 0; j < ENTRIES; j++)
120 ck_fifo_spsc_dequeue(&spsc_fifo, &r);
121 e = rdtsc();
122 a += e - s;
123 }
124 printf("ck_fifo_spsc_dequeue: %16" PRIu64 "\n", a / STEPS / (sizeof(spsc_entry) / sizeof(*spsc_entry)));
125 #endif
126
127 #ifdef CK_F_FIFO_MPMC
128 a = 0;
129 for (i = 0; i < STEPS; i++) {
130 ck_fifo_mpmc_init(&mpmc_fifo, &mpmc_stub);
131
132 s = rdtsc();
133 for (j = 0; j < ENTRIES; j++)
134 ck_fifo_mpmc_enqueue(&mpmc_fifo, mpmc_entry + j, NULL);
135 e = rdtsc();
136
137 a += e - s;
138 }
139 printf("ck_fifo_mpmc_enqueue: %16" PRIu64 "\n", a / STEPS / (sizeof(mpmc_entry) / sizeof(*mpmc_entry)));
140
141 a = 0;
142 for (i = 0; i < STEPS; i++) {
143 ck_fifo_mpmc_init(&mpmc_fifo, &mpmc_stub);
144 for (j = 0; j < ENTRIES; j++)
145 ck_fifo_mpmc_enqueue(&mpmc_fifo, mpmc_entry + j, NULL);
146
147 s = rdtsc();
148 for (j = 0; j < ENTRIES; j++)
149 ck_fifo_mpmc_dequeue(&mpmc_fifo, &r, &garbage);
150 e = rdtsc();
151 a += e - s;
152 }
153 printf("ck_fifo_mpmc_dequeue: %16" PRIu64 "\n", a / STEPS / (sizeof(mpmc_entry) / sizeof(*mpmc_entry)));
154 #endif
155
156 return 0;
157 }