1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2011 NetApp, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD$
29 */
30
31 /*
32 * Copyright 2018 Joyent, Inc.
33 */
34
35 /*
36 * Test program for the micro event library. Set up a simple TCP echo
37 * service.
38 *
39 * cc mevent_test.c mevent.c -lpthread
40 */
41
42 #include <sys/types.h>
43 #include <sys/stdint.h>
44 #ifdef __FreeBSD__
45 #include <sys/sysctl.h>
46 #endif
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #ifdef __FreeBSD__
50 #include <machine/cpufunc.h>
51 #endif
52
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <pthread.h>
56 #include <unistd.h>
57
58 #include "mevent.h"
59
60 #define TEST_PORT 4321
61
62 static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;
63 static pthread_cond_t accept_condvar = PTHREAD_COND_INITIALIZER;
64
65 static struct mevent *tevp;
66
67 char *vmname = "test vm";
68
69
70 #define MEVENT_ECHO
71
72 /* Number of timer events to capture */
73 #define TEVSZ 4096
74 uint64_t tevbuf[TEVSZ];
75
76 static void
timer_print(void)77 timer_print(void)
78 {
79 uint64_t min, max, diff, sum;
80 #ifdef __FreeBSD__
81 uint64_t tsc_freq;
82 size_t len;
83 #endif
84 int j;
85
86 min = UINT64_MAX;
87 max = 0;
88 sum = 0;
89
90 #ifdef __FreeBSD__
91 len = sizeof(tsc_freq);
92 sysctlbyname("machdep.tsc_freq", &tsc_freq, &len, NULL, 0);
93 #endif
94
95 for (j = 1; j < TEVSZ; j++) {
96 #ifdef __FreeBSD__
97 /* Convert a tsc diff into microseconds */
98 diff = (tevbuf[j] - tevbuf[j-1]) * 1000000 / tsc_freq;
99 #else
100 diff = (tevbuf[j] - tevbuf[j-1]) / 1000;
101 #endif
102 sum += diff;
103 if (min > diff)
104 min = diff;
105 if (max < diff)
106 max = diff;
107 }
108
109 printf("timers done: usecs, min %ld, max %ld, mean %ld\n", min, max,
110 sum/(TEVSZ - 1));
111 }
112
113 static void
timer_callback(int fd,enum ev_type type,void * param)114 timer_callback(int fd, enum ev_type type, void *param)
115 {
116 static int i;
117
118 if (i >= TEVSZ)
119 abort();
120
121 #ifdef __FreeBSD__
122 tevbuf[i++] = rdtsc();
123 #else
124 tevbuf[i++] = gethrtime();
125 #endif
126
127 if (i == TEVSZ) {
128 mevent_delete(tevp);
129 timer_print();
130 }
131 }
132
133
134 #ifdef MEVENT_ECHO
135 struct esync {
136 pthread_mutex_t e_mt;
137 pthread_cond_t e_cond;
138 };
139
140 static void
echoer_callback(int fd,enum ev_type type,void * param)141 echoer_callback(int fd, enum ev_type type, void *param)
142 {
143 struct esync *sync = param;
144
145 pthread_mutex_lock(&sync->e_mt);
146 pthread_cond_signal(&sync->e_cond);
147 pthread_mutex_unlock(&sync->e_mt);
148 }
149
150 static void *
echoer(void * param)151 echoer(void *param)
152 {
153 struct esync sync;
154 struct mevent *mev;
155 char buf[128];
156 int fd = (int)(uintptr_t) param;
157 int len;
158
159 pthread_mutex_init(&sync.e_mt, NULL);
160 pthread_cond_init(&sync.e_cond, NULL);
161
162 pthread_mutex_lock(&sync.e_mt);
163
164 mev = mevent_add(fd, EVF_READ, echoer_callback, &sync);
165 if (mev == NULL) {
166 printf("Could not allocate echoer event\n");
167 exit(4);
168 }
169
170 while (!pthread_cond_wait(&sync.e_cond, &sync.e_mt)) {
171 len = read(fd, buf, sizeof(buf));
172 if (len > 0) {
173 write(fd, buf, len);
174 write(0, buf, len);
175 } else {
176 break;
177 }
178 }
179
180 mevent_delete_close(mev);
181
182 pthread_mutex_unlock(&sync.e_mt);
183 pthread_mutex_destroy(&sync.e_mt);
184 pthread_cond_destroy(&sync.e_cond);
185
186 return (NULL);
187 }
188
189 #else
190
191 static void *
echoer(void * param)192 echoer(void *param)
193 {
194 char buf[128];
195 int fd = (int)(uintptr_t) param;
196 int len;
197
198 while ((len = read(fd, buf, sizeof(buf))) > 0) {
199 write(1, buf, len);
200 }
201
202 return (NULL);
203 }
204 #endif /* MEVENT_ECHO */
205
206 static void
acceptor_callback(int fd,enum ev_type type,void * param)207 acceptor_callback(int fd, enum ev_type type, void *param)
208 {
209 pthread_mutex_lock(&accept_mutex);
210 pthread_cond_signal(&accept_condvar);
211 pthread_mutex_unlock(&accept_mutex);
212 }
213
214 static void *
acceptor(void * param)215 acceptor(void *param)
216 {
217 struct sockaddr_in sin;
218 pthread_t tid;
219 int news;
220 int s;
221
222 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
223 perror("cannot create socket");
224 exit(4);
225 }
226
227 #ifdef __FreeBSD__
228 sin.sin_len = sizeof(sin);
229 #endif
230 sin.sin_family = AF_INET;
231 sin.sin_addr.s_addr = htonl(INADDR_ANY);
232 sin.sin_port = htons(TEST_PORT);
233
234 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
235 perror("cannot bind socket");
236 exit(4);
237 }
238
239 if (listen(s, 1) < 0) {
240 perror("cannot listen socket");
241 exit(4);
242 }
243
244 (void) mevent_add(s, EVF_READ, acceptor_callback, NULL);
245
246 pthread_mutex_lock(&accept_mutex);
247
248 while (!pthread_cond_wait(&accept_condvar, &accept_mutex)) {
249 news = accept(s, NULL, NULL);
250 if (news < 0) {
251 perror("accept error");
252 } else {
253 static int first = 1;
254
255 if (first) {
256 /*
257 * Start a timer
258 */
259 first = 0;
260 tevp = mevent_add(1, EVF_TIMER, timer_callback,
261 NULL);
262 }
263
264 printf("incoming connection, spawning thread\n");
265 pthread_create(&tid, NULL, echoer,
266 (void *)(uintptr_t)news);
267 }
268 }
269
270 return (NULL);
271 }
272
273 int
main()274 main()
275 {
276 pthread_t tid;
277
278 pthread_create(&tid, NULL, acceptor, NULL);
279
280 mevent_dispatch();
281 return (0);
282 }
283