Debian Codegraph @

tests/isc/mutex_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 #include <isc/atomic.h>
27 #include <isc/file.h>
28 #include <isc/mem.h>
29 #include <isc/mutex.h>
30 #include <isc/os.h>
31 #include <isc/pause.h>
32 #include <isc/result.h>
33 #include <isc/stdio.h>
34 #include <isc/thread.h>
35 #include <isc/time.h>
36 #include <isc/util.h>
37
38 #include <tests/isc.h>
39
40 static unsigned int loops = 100;
41 static unsigned int delay_loop = 1;
42
43 static int
44 setup_env(void **unused __attribute__((__unused__))) {
45 char *env = getenv("ISC_BENCHMARK_LOOPS");
46 if (env != NULL) {
47 loops = atoi(env);
48 }
49 assert_int_not_equal(loops, 0);
50
51 env = getenv("ISC_BENCHMARK_DELAY");
52 if (env != NULL) {
53 delay_loop = atoi(env);
54 }
55 assert_int_not_equal(delay_loop, 0);
56
57 return 0;
58 }
59
60 ISC_RUN_TEST_IMPL(isc_mutex) {
61 isc_mutex_t lock;
62
63 isc_mutex_init(&lock);
64
65 for (size_t i = 0; i < loops; i++) {
66 isc_mutex_lock(&lock);
67 isc_pause_n(delay_loop);
68 isc_mutex_unlock(&lock);
69 }
70
71 isc_mutex_destroy(&lock);
72 }
73
74 #define ITERS 20
75
76 #define DC 200
77 #define CNT_MIN 800
78 #define CNT_MAX 1600
79
80 static size_t shared_counter = 0;
81 static size_t expected_counter = SIZE_MAX;
82 static isc_mutex_t lock;
83 static pthread_mutex_t mutex;
84
85 static void *
86 pthread_mutex_thread(void *arg) {
87 size_t cont = *(size_t *)arg;
88
89 for (size_t i = 0; i < loops; i++) {
90 pthread_mutex_lock(&mutex);
91 size_t v = shared_counter;
92 isc_pause_n(delay_loop);
93 shared_counter = v + 1;
94 pthread_mutex_unlock(&mutex);
95 isc_pause_n(cont);
96 }
97
98 return NULL;
99 }
100
101 static void *
102 isc_mutex_thread(void *arg) {
103 size_t cont = *(size_t *)arg;
104
105 for (size_t i = 0; i < loops; i++) {
106 isc_mutex_lock(&lock);
107 size_t v = shared_counter;
108 isc_pause_n(delay_loop);
109 shared_counter = v + 1;
110 isc_mutex_unlock(&lock);
111 isc_pause_n(cont);
112 }
113
114 return NULL;
115 }
116
117 ISC_RUN_TEST_IMPL(isc_mutex_benchmark) {
118 isc_thread_t *threads = isc_mem_cget(mctx, workers, sizeof(*threads));
119 isc_time_t ts1, ts2;
120 double t;
121 int dc;
122 size_t cont;
123 int r;
124
125 memset(threads, 0, sizeof(*threads) * workers);
126
127 expected_counter = ITERS * workers * loops *
128 ((CNT_MAX - CNT_MIN) / DC + 1);
129
130 /* PTHREAD MUTEX */
131
132 r = pthread_mutex_init(&mutex, NULL);
133 assert_int_not_equal(r, -1);
134
135 ts1 = isc_time_now_hires();
136
137 shared_counter = 0;
138 dc = DC;
139 for (size_t l = 0; l < ITERS; l++) {
140 for (cont = (dc > 0) ? CNT_MIN : CNT_MAX;
141 cont <= CNT_MAX && cont >= CNT_MIN; cont += dc)
142 {
143 for (size_t i = 0; i < workers; i++) {
144 isc_thread_create(pthread_mutex_thread, &cont,
145 &threads[i]);
146 }
147 for (size_t i = 0; i < workers; i++) {
148 isc_thread_join(threads[i], NULL);
149 }
150 }
151 dc = -dc;
152 }
153 assert_int_equal(shared_counter, expected_counter);
154
155 ts2 = isc_time_now_hires();
156
157 t = isc_time_microdiff(&ts2, &ts1);
158
159 printf("[ TIME ] isc_mutex_benchmark: %zu pthread_mutex "
160 "loops in "
161 "%u threads, %2.3f seconds, %2.3f calls/second\n",
162 shared_counter, workers, t / 1000000.0,
163 shared_counter / (t / 1000000.0));
164
165 r = pthread_mutex_destroy(&mutex);
166 assert_int_not_equal(r, -1);
167
168 /* ISC MUTEX */
169
170 isc_mutex_init(&lock);
171
172 ts1 = isc_time_now_hires();
173
174 dc = DC;
175 shared_counter = 0;
176 for (size_t l = 0; l < ITERS; l++) {
177 for (cont = (dc > 0) ? CNT_MIN : CNT_MAX;
178 cont <= CNT_MAX && cont >= CNT_MIN; cont += dc)
179 {
180 for (size_t i = 0; i < workers; i++) {
181 isc_thread_create(isc_mutex_thread, &cont,
182 &threads[i]);
183 }
184 for (size_t i = 0; i < workers; i++) {
185 isc_thread_join(threads[i], NULL);
186 }
187 }
188 dc = -dc;
189 }
190 assert_int_equal(shared_counter, expected_counter);
191
192 ts2 = isc_time_now_hires();
193
194 t = isc_time_microdiff(&ts2, &ts1);
195
196 printf("[ TIME ] isc_mutex_benchmark: %zu isc_mutex loops "
197 "in %u "
198 "threads, %2.3f seconds, %2.3f calls/second\n",
199 shared_counter, workers, t / 1000000.0,
200 shared_counter / (t / 1000000.0));
201
202 isc_mutex_destroy(&lock);
203
204 isc_mem_cput(mctx, threads, workers, sizeof(*threads));
205 }
206
207 ISC_TEST_LIST_START
208
209 ISC_TEST_ENTRY(isc_mutex)
210 #if !defined(__SANITIZE_THREAD__)
211 ISC_TEST_ENTRY(isc_mutex_benchmark)
212 #endif /* __SANITIZE_THREAD__ */
213
214 ISC_TEST_LIST_END
215
216 ISC_TEST_MAIN_CUSTOM(setup_env, NULL)