2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 * Test program for the micro event library. Set up a simple TCP echo
35 * cc mevent_test.c mevent.c -lpthread
38 #include <sys/types.h>
39 #include <sys/stdint.h>
40 #include <sys/sysctl.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <machine/cpufunc.h>
52 #define TEST_PORT 4321
54 static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;
55 static pthread_cond_t accept_condvar = PTHREAD_COND_INITIALIZER;
57 static struct mevent *tevp;
59 char *vmname = "test vm";
64 /* Number of timer events to capture */
66 uint64_t tevbuf[TEVSZ];
71 uint64_t min, max, diff, sum, tsc_freq;
79 len = sizeof(tsc_freq);
80 sysctlbyname("machdep.tsc_freq", &tsc_freq, &len, NULL, 0);
82 for (j = 1; j < TEVSZ; j++) {
83 /* Convert a tsc diff into microseconds */
84 diff = (tevbuf[j] - tevbuf[j-1]) * 1000000 / tsc_freq;
92 printf("timers done: usecs, min %ld, max %ld, mean %ld\n", min, max,
97 timer_callback(int fd, enum ev_type type, void *param)
104 tevbuf[i++] = rdtsc();
115 pthread_mutex_t e_mt;
116 pthread_cond_t e_cond;
120 echoer_callback(int fd, enum ev_type type, void *param)
122 struct esync *sync = param;
124 pthread_mutex_lock(&sync->e_mt);
125 pthread_cond_signal(&sync->e_cond);
126 pthread_mutex_unlock(&sync->e_mt);
135 int fd = (int)(uintptr_t) param;
138 pthread_mutex_init(&sync.e_mt, NULL);
139 pthread_cond_init(&sync.e_cond, NULL);
141 pthread_mutex_lock(&sync.e_mt);
143 mev = mevent_add(fd, EVF_READ, echoer_callback, &sync);
145 printf("Could not allocate echoer event\n");
149 while (!pthread_cond_wait(&sync.e_cond, &sync.e_mt)) {
150 len = read(fd, buf, sizeof(buf));
159 mevent_delete_close(mev);
161 pthread_mutex_unlock(&sync.e_mt);
162 pthread_mutex_destroy(&sync.e_mt);
163 pthread_cond_destroy(&sync.e_cond);
174 int fd = (int)(uintptr_t) param;
177 while ((len = read(fd, buf, sizeof(buf))) > 0) {
183 #endif /* MEVENT_ECHO */
186 acceptor_callback(int fd, enum ev_type type, void *param)
188 pthread_mutex_lock(&accept_mutex);
189 pthread_cond_signal(&accept_condvar);
190 pthread_mutex_unlock(&accept_mutex);
194 acceptor(void *param)
196 struct sockaddr_in sin;
202 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
203 perror("cannot create socket");
207 sin.sin_len = sizeof(sin);
208 sin.sin_family = AF_INET;
209 sin.sin_addr.s_addr = htonl(INADDR_ANY);
210 sin.sin_port = htons(TEST_PORT);
212 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
213 perror("cannot bind socket");
217 if (listen(s, 1) < 0) {
218 perror("cannot listen socket");
222 (void) mevent_add(s, EVF_READ, acceptor_callback, NULL);
224 pthread_mutex_lock(&accept_mutex);
226 while (!pthread_cond_wait(&accept_condvar, &accept_mutex)) {
227 news = accept(s, NULL, NULL);
229 perror("accept error");
231 static int first = 1;
238 tevp = mevent_add(1, EVF_TIMER, timer_callback,
242 printf("incoming connection, spawning thread\n");
243 pthread_create(&tid, NULL, echoer,
244 (void *)(uintptr_t)news);
255 pthread_create(&tid, NULL, acceptor, NULL);