Debian Codegraph @

tests/isc/spinlock_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <fcntl.h>
15 #include <inttypes.h>
16 #include <sched.h> /* IWYU pragma: keep */
17 #include <setjmp.h>
18 #include <stdarg.h>
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22
23 #define UNIT_TESTING
24 #include <cmocka.h>
25
26 #ifdef HAVE_PTHREAD_SPIN_INIT
27 #define HAD_PTHREAD_SPIN_INIT 1
28 #undef HAVE_PTHREAD_SPIN_INIT
29 #endif
30
31 #include <isc/atomic.h>
32 #include <isc/file.h>
33 #include <isc/mem.h>
34 #include <isc/os.h>
35 #include <isc/pause.h>
36 #include <isc/result.h>
37 #include <isc/spinlock.h>
38 #include <isc/stdio.h>
39 #include <isc/thread.h>
40 #include <isc/time.h>
41 #include <isc/util.h>
42
43 #include <tests/isc.h>
44
45 static unsigned int loops = 100;
46 static unsigned int delay_loop = 1;
47
48 static int
49 setup_env(void **unused __attribute__((__unused__))) {
50 char *env = getenv("ISC_BENCHMARK_LOOPS");
51 if (env != NULL) {
52 loops = atoi(env);
53 }
54 assert_int_not_equal(loops, 0);
55
56 env = getenv("ISC_BENCHMARK_DELAY");
57 if (env != NULL) {
58 delay_loop = atoi(env);
59 }
60 assert_int_not_equal(delay_loop, 0);
61
62 return 0;
63 }
64
65 ISC_RUN_TEST_IMPL(isc_spinlock) {
66 isc_spinlock_t lock;
67
68 isc_spinlock_init(&lock);
69
70 for (size_t i = 0; i < loops; i++) {
71 isc_spinlock_lock(&lock);
72 isc_pause_n(delay_loop);
73 isc_spinlock_unlock(&lock);
74 }
75
76 isc_spinlock_destroy(&lock);
77 }
78
79 #define ITERS 20
80
81 #define DC 200
82 #define CNT_MIN 800
83 #define CNT_MAX 1600
84
85 static size_t shared_counter = 0;
86 static size_t expected_counter = SIZE_MAX;
87
88 #if HAD_PTHREAD_SPIN_INIT
89 static pthread_spinlock_t spin;
90
91 static void *
92 pthread_spin_thread(void *arg) {
93 size_t cont = *(size_t *)arg;
94
95 for (size_t i = 0; i < loops; i++) {
96 pthread_spin_lock(&spin);
97 size_t v = shared_counter;
98 isc_pause_n(delay_loop);
99 shared_counter = v + 1;
100 pthread_spin_unlock(&spin);
101 isc_pause_n(cont);
102 }
103
104 return NULL;
105 }
106 #endif
107
108 static isc_spinlock_t lock;
109
110 static void *
111 isc_spinlock_thread(void *arg) {
112 size_t cont = *(size_t *)arg;
113
114 for (size_t i = 0; i < loops; i++) {
115 isc_spinlock_lock(&lock);
116 size_t v = shared_counter;
117 isc_pause_n(delay_loop);
118 shared_counter = v + 1;
119 isc_spinlock_unlock(&lock);
120 isc_pause_n(cont);
121 }
122
123 return NULL;
124 }
125
126 ISC_RUN_TEST_IMPL(isc_spinlock_benchmark) {
127 isc_thread_t *threads = isc_mem_cget(mctx, workers, sizeof(*threads));
128 isc_time_t ts1, ts2;
129 double t;
130 int dc;
131 size_t cont;
132
133 memset(threads, 0, sizeof(*threads) * workers);
134
135 expected_counter = ITERS * workers * loops *
136 ((CNT_MAX - CNT_MIN) / DC + 1);
137
138 /* PTHREAD SPINLOCK */
139
140 #if HAD_PTHREAD_SPIN_INIT
141 int r = pthread_spin_init(&spin, PTHREAD_PROCESS_PRIVATE);
142 assert_int_not_equal(r, -1);
143
144 ts1 = isc_time_now_hires();
145
146 shared_counter = 0;
147 dc = DC;
148 for (size_t l = 0; l < ITERS; l++) {
149 for (cont = (dc > 0) ? CNT_MIN : CNT_MAX;
150 cont <= CNT_MAX && cont >= CNT_MIN; cont += dc)
151 {
152 for (size_t i = 0; i < workers; i++) {
153 isc_thread_create(pthread_spin_thread, &cont,
154 &threads[i]);
155 }
156 for (size_t i = 0; i < workers; i++) {
157 isc_thread_join(threads[i], NULL);
158 }
159 }
160 dc = -dc;
161 }
162 assert_int_equal(shared_counter, expected_counter);
163
164 ts2 = isc_time_now_hires();
165
166 t = isc_time_microdiff(&ts2, &ts1);
167
168 printf("[ TIME ] isc_spinlock_benchmark: %zu pthread_spin "
169 "loops in "
170 "%u threads, %2.3f seconds, %2.3f calls/second\n",
171 shared_counter, workers, t / 1000000.0,
172 shared_counter / (t / 1000000.0));
173
174 r = pthread_spin_destroy(&spin);
175 assert_int_not_equal(r, -1);
176 #endif
177
178 /* ISC SPINLOCK */
179
180 isc_spinlock_init(&lock);
181
182 ts1 = isc_time_now_hires();
183
184 dc = DC;
185 shared_counter = 0;
186 for (size_t l = 0; l < ITERS; l++) {
187 for (cont = (dc > 0) ? CNT_MIN : CNT_MAX;
188 cont <= CNT_MAX && cont >= CNT_MIN; cont += dc)
189 {
190 for (size_t i = 0; i < workers; i++) {
191 isc_thread_create(isc_spinlock_thread, &cont,
192 &threads[i]);
193 }
194 for (size_t i = 0; i < workers; i++) {
195 isc_thread_join(threads[i], NULL);
196 }
197 }
198 dc = -dc;
199 }
200 assert_int_equal(shared_counter, expected_counter);
201
202 ts2 = isc_time_now_hires();
203
204 t = isc_time_microdiff(&ts2, &ts1);
205
206 printf("[ TIME ] isc_spinlock_benchmark: %zu isc_spinlock loops "
207 "in %u "
208 "threads, %2.3f seconds, %2.3f calls/second\n",
209 shared_counter, workers, t / 1000000.0,
210 shared_counter / (t / 1000000.0));
211
212 isc_spinlock_destroy(&lock);
213
214 isc_mem_cput(mctx, threads, workers, sizeof(*threads));
215 }
216
217 ISC_TEST_LIST_START
218
219 ISC_TEST_ENTRY(isc_spinlock)
220 #if !defined(__SANITIZE_THREAD__)
221 ISC_TEST_ENTRY(isc_spinlock_benchmark)
222 #endif /* __SANITIZE_THREAD__ */
223
224 ISC_TEST_LIST_END
225
226 ISC_TEST_MAIN_CUSTOM(setup_env, NULL)