1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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
29 /*
30 * Test program for the micro event library. Set up a simple TCP echo
31 * service.
32 *
33 * cc mevent_test.c mevent.c -lpthread
34 */
35
36 #include <sys/types.h>
37 #include <sys/stdint.h>
38 #include <sys/sysctl.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <machine/cpufunc.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <pthread.h>
46 #include <unistd.h>
47
48 #include "bhyverun.h"
49 #include "mevent.h"
50
51 #define TEST_PORT 4321
52
53 static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;
54 static pthread_cond_t accept_condvar = PTHREAD_COND_INITIALIZER;
55
56 static struct mevent *tevp;
57
58
59 #define MEVENT_ECHO
60
61 /* Number of timer events to capture */
62 #define TEVSZ 4096
63 uint64_t tevbuf[TEVSZ];
64
65 static void
timer_print(void)66 timer_print(void)
67 {
68 uint64_t min, max, diff, sum, tsc_freq;
69 size_t len;
70 int j;
71
72 min = UINT64_MAX;
73 max = 0;
74 sum = 0;
75
76 len = sizeof(tsc_freq);
77 sysctlbyname("machdep.tsc_freq", &tsc_freq, &len, NULL, 0);
78
79 for (j = 1; j < TEVSZ; j++) {
80 /* Convert a tsc diff into microseconds */
81 diff = (tevbuf[j] - tevbuf[j-1]) * 1000000 / tsc_freq;
82 sum += diff;
83 if (min > diff)
84 min = diff;
85 if (max < diff)
86 max = diff;
87 }
88
89 printf("timers done: usecs, min %ld, max %ld, mean %ld\n", min, max,
90 sum/(TEVSZ - 1));
91 }
92
93 static void
timer_callback(int fd,enum ev_type type,void * param)94 timer_callback(int fd, enum ev_type type, void *param)
95 {
96 static int i;
97
98 if (i >= TEVSZ)
99 abort();
100
101 tevbuf[i++] = rdtsc();
102
103 if (i == TEVSZ) {
104 mevent_delete(tevp);
105 timer_print();
106 }
107 }
108
109
110 #ifdef MEVENT_ECHO
111 struct esync {
112 pthread_mutex_t e_mt;
113 pthread_cond_t e_cond;
114 };
115
116 static void
echoer_callback(int fd,enum ev_type type,void * param)117 echoer_callback(int fd, enum ev_type type, void *param)
118 {
119 struct esync *sync = param;
120
121 pthread_mutex_lock(&sync->e_mt);
122 pthread_cond_signal(&sync->e_cond);
123 pthread_mutex_unlock(&sync->e_mt);
124 }
125
126 static void *
echoer(void * param)127 echoer(void *param)
128 {
129 struct esync sync;
130 struct mevent *mev;
131 char buf[128];
132 int fd = (int)(uintptr_t) param;
133 int len;
134
135 pthread_mutex_init(&sync.e_mt, NULL);
136 pthread_cond_init(&sync.e_cond, NULL);
137
138 pthread_mutex_lock(&sync.e_mt);
139
140 mev = mevent_add(fd, EVF_READ, echoer_callback, &sync);
141 if (mev == NULL) {
142 printf("Could not allocate echoer event\n");
143 exit(BHYVE_EXIT_ERROR);
144 }
145
146 while (!pthread_cond_wait(&sync.e_cond, &sync.e_mt)) {
147 len = read(fd, buf, sizeof(buf));
148 if (len > 0) {
149 write(fd, buf, len);
150 write(0, buf, len);
151 } else {
152 break;
153 }
154 }
155
156 mevent_delete_close(mev);
157
158 pthread_mutex_unlock(&sync.e_mt);
159 pthread_mutex_destroy(&sync.e_mt);
160 pthread_cond_destroy(&sync.e_cond);
161
162 return (NULL);
163 }
164
165 #else
166
167 static void *
echoer(void * param)168 echoer(void *param)
169 {
170 char buf[128];
171 int fd = (int)(uintptr_t) param;
172 int len;
173
174 while ((len = read(fd, buf, sizeof(buf))) > 0) {
175 write(1, buf, len);
176 }
177
178 return (NULL);
179 }
180 #endif /* MEVENT_ECHO */
181
182 static void
acceptor_callback(int fd,enum ev_type type,void * param)183 acceptor_callback(int fd, enum ev_type type, void *param)
184 {
185 pthread_mutex_lock(&accept_mutex);
186 pthread_cond_signal(&accept_condvar);
187 pthread_mutex_unlock(&accept_mutex);
188 }
189
190 static void *
acceptor(void * param)191 acceptor(void *param)
192 {
193 struct sockaddr_in sin;
194 pthread_t tid;
195 int news;
196 int s;
197 static int first;
198
199 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
200 perror("cannot create socket");
201 exit(BHYVE_EXIT_ERROR);
202 }
203
204 sin.sin_len = sizeof(sin);
205 sin.sin_family = AF_INET;
206 sin.sin_addr.s_addr = htonl(INADDR_ANY);
207 sin.sin_port = htons(TEST_PORT);
208
209 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
210 perror("cannot bind socket");
211 exit(BHYVE_EXIT_ERROR);
212 }
213
214 if (listen(s, 1) < 0) {
215 perror("cannot listen socket");
216 exit(BHYVE_EXIT_ERROR);
217 }
218
219 (void) mevent_add(s, EVF_READ, acceptor_callback, NULL);
220
221 pthread_mutex_lock(&accept_mutex);
222
223 while (!pthread_cond_wait(&accept_condvar, &accept_mutex)) {
224 news = accept(s, NULL, NULL);
225 if (news < 0) {
226 perror("accept error");
227 } else {
228 static int first = 1;
229
230 if (first) {
231 /*
232 * Start a timer
233 */
234 first = 0;
235 tevp = mevent_add(1, EVF_TIMER, timer_callback,
236 NULL);
237 }
238
239 printf("incoming connection, spawning thread\n");
240 pthread_create(&tid, NULL, echoer,
241 (void *)(uintptr_t)news);
242 }
243 }
244
245 return (NULL);
246 }
247
main()248 main()
249 {
250 pthread_t tid;
251
252 pthread_create(&tid, NULL, acceptor, NULL);
253
254 mevent_dispatch();
255 }
256