xref: /illumos-gate/usr/src/cmd/bhyve/mevent_test.c (revision 1a065e93eee983124652c3eb0cfdcb4776cd89ab)
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 
68 #define MEVENT_ECHO
69 
70 /* Number of timer events to capture */
71 #define TEVSZ	4096
72 uint64_t tevbuf[TEVSZ];
73 
74 static void
75 timer_print(void)
76 {
77 	uint64_t min, max, diff, sum;
78 #ifdef __FreeBSD__
79 	uint64_t tsc_freq;
80 	size_t len;
81 #endif
82 	int j;
83 
84 	min = UINT64_MAX;
85 	max = 0;
86 	sum = 0;
87 
88 #ifdef __FreeBSD__
89 	len = sizeof(tsc_freq);
90 	sysctlbyname("machdep.tsc_freq", &tsc_freq, &len, NULL, 0);
91 #endif
92 
93 	for (j = 1; j < TEVSZ; j++) {
94 #ifdef __FreeBSD__
95 		/* Convert a tsc diff into microseconds */
96 		diff = (tevbuf[j] - tevbuf[j-1]) * 1000000 / tsc_freq;
97 #else
98 		diff = (tevbuf[j] - tevbuf[j-1]) / 1000;
99 #endif
100 		sum += diff;
101 		if (min > diff)
102 			min = diff;
103 		if (max < diff)
104 			max = diff;
105 	}
106 
107 	printf("timers done: usecs, min %ld, max %ld, mean %ld\n", min, max,
108 	    sum/(TEVSZ - 1));
109 }
110 
111 static void
112 timer_callback(int fd, enum ev_type type, void *param)
113 {
114 	static int i;
115 
116 	if (i >= TEVSZ)
117 		abort();
118 
119 #ifdef __FreeBSD__
120 	tevbuf[i++] = rdtsc();
121 #else
122 	tevbuf[i++] = gethrtime();
123 #endif
124 
125 	if (i == TEVSZ) {
126 		mevent_delete(tevp);
127 		timer_print();
128 	}
129 }
130 
131 
132 #ifdef MEVENT_ECHO
133 struct esync {
134 	pthread_mutex_t	e_mt;
135 	pthread_cond_t	e_cond;
136 };
137 
138 static void
139 echoer_callback(int fd, enum ev_type type, void *param)
140 {
141 	struct esync *sync = param;
142 
143 	pthread_mutex_lock(&sync->e_mt);
144 	pthread_cond_signal(&sync->e_cond);
145 	pthread_mutex_unlock(&sync->e_mt);
146 }
147 
148 static void *
149 echoer(void *param)
150 {
151 	struct esync sync;
152 	struct mevent *mev;
153 	char buf[128];
154 	int fd = (int)(uintptr_t) param;
155 	int len;
156 
157 	pthread_mutex_init(&sync.e_mt, NULL);
158 	pthread_cond_init(&sync.e_cond, NULL);
159 
160 	pthread_mutex_lock(&sync.e_mt);
161 
162 	mev = mevent_add(fd, EVF_READ, echoer_callback, &sync);
163 	if (mev == NULL) {
164 		printf("Could not allocate echoer event\n");
165 		exit(4);
166 	}
167 
168 	while (!pthread_cond_wait(&sync.e_cond, &sync.e_mt)) {
169 		len = read(fd, buf, sizeof(buf));
170 		if (len > 0) {
171 			write(fd, buf, len);
172 			write(0, buf, len);
173 		} else {
174 			break;
175 		}
176 	}
177 
178 	mevent_delete_close(mev);
179 
180 	pthread_mutex_unlock(&sync.e_mt);
181 	pthread_mutex_destroy(&sync.e_mt);
182 	pthread_cond_destroy(&sync.e_cond);
183 
184 	return (NULL);
185 }
186 
187 #else
188 
189 static void *
190 echoer(void *param)
191 {
192 	char buf[128];
193 	int fd = (int)(uintptr_t) param;
194 	int len;
195 
196 	while ((len = read(fd, buf, sizeof(buf))) > 0) {
197 		write(1, buf, len);
198 	}
199 
200 	return (NULL);
201 }
202 #endif /* MEVENT_ECHO */
203 
204 static void
205 acceptor_callback(int fd, enum ev_type type, void *param)
206 {
207 	pthread_mutex_lock(&accept_mutex);
208 	pthread_cond_signal(&accept_condvar);
209 	pthread_mutex_unlock(&accept_mutex);
210 }
211 
212 static void *
213 acceptor(void *param)
214 {
215 	struct sockaddr_in sin;
216 	pthread_t tid;
217 	int news;
218 	int s;
219 
220 	if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
221 		perror("cannot create socket");
222 		exit(4);
223 	}
224 
225 #ifdef __FreeBSD__
226 	sin.sin_len = sizeof(sin);
227 #endif
228 	sin.sin_family = AF_INET;
229 	sin.sin_addr.s_addr = htonl(INADDR_ANY);
230 	sin.sin_port = htons(TEST_PORT);
231 
232 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
233 		perror("cannot bind socket");
234 		exit(4);
235 	}
236 
237 	if (listen(s, 1) < 0) {
238 		perror("cannot listen socket");
239 		exit(4);
240 	}
241 
242 	(void) mevent_add(s, EVF_READ, acceptor_callback, NULL);
243 
244 	pthread_mutex_lock(&accept_mutex);
245 
246 	while (!pthread_cond_wait(&accept_condvar, &accept_mutex)) {
247 		news = accept(s, NULL, NULL);
248 		if (news < 0) {
249 			perror("accept error");
250 		} else {
251 			static int first = 1;
252 
253 			if (first) {
254 				/*
255 				 * Start a timer
256 				 */
257 				first = 0;
258 				tevp = mevent_add(1, EVF_TIMER, timer_callback,
259 						  NULL);
260 			}
261 
262 			printf("incoming connection, spawning thread\n");
263 			pthread_create(&tid, NULL, echoer,
264 				       (void *)(uintptr_t)news);
265 		}
266 	}
267 
268 	return (NULL);
269 }
270 
271 int
272 main()
273 {
274 	pthread_t tid;
275 
276 	pthread_create(&tid, NULL, acceptor, NULL);
277 
278 	mevent_dispatch();
279 	return (0);
280 }
281