2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Test program for the micro event library. Set up a simple TCP echo
33 * cc mevent_test.c mevent.c -lpthread
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>
50 #define TEST_PORT 4321
52 static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;
53 static pthread_cond_t accept_condvar = PTHREAD_COND_INITIALIZER;
55 static struct mevent *tevp;
57 char *vmname = "test vm";
62 /* Number of timer events to capture */
64 uint64_t tevbuf[TEVSZ];
69 uint64_t min, max, diff, sum, tsc_freq;
77 len = sizeof(tsc_freq);
78 sysctlbyname("machdep.tsc_freq", &tsc_freq, &len, NULL, 0);
80 for (j = 1; j < TEVSZ; j++) {
81 /* Convert a tsc diff into microseconds */
82 diff = (tevbuf[j] - tevbuf[j-1]) * 1000000 / tsc_freq;
90 printf("timers done: usecs, min %ld, max %ld, mean %ld\n", min, max,
95 timer_callback(int fd, enum ev_type type, void *param)
102 tevbuf[i++] = rdtsc();
113 pthread_mutex_t e_mt;
114 pthread_cond_t e_cond;
118 echoer_callback(int fd, enum ev_type type, void *param)
120 struct esync *sync = param;
122 pthread_mutex_lock(&sync->e_mt);
123 pthread_cond_signal(&sync->e_cond);
124 pthread_mutex_unlock(&sync->e_mt);
133 int fd = (int)(uintptr_t) param;
136 pthread_mutex_init(&sync.e_mt, NULL);
137 pthread_cond_init(&sync.e_cond, NULL);
139 pthread_mutex_lock(&sync.e_mt);
141 mev = mevent_add(fd, EVF_READ, echoer_callback, &sync);
143 printf("Could not allocate echoer event\n");
147 while (!pthread_cond_wait(&sync.e_cond, &sync.e_mt)) {
148 len = read(fd, buf, sizeof(buf));
157 mevent_delete_close(mev);
159 pthread_mutex_unlock(&sync.e_mt);
160 pthread_mutex_destroy(&sync.e_mt);
161 pthread_cond_destroy(&sync.e_cond);
172 int fd = (int)(uintptr_t) param;
175 while ((len = read(fd, buf, sizeof(buf))) > 0) {
181 #endif /* MEVENT_ECHO */
184 acceptor_callback(int fd, enum ev_type type, void *param)
186 pthread_mutex_lock(&accept_mutex);
187 pthread_cond_signal(&accept_condvar);
188 pthread_mutex_unlock(&accept_mutex);
192 acceptor(void *param)
194 struct sockaddr_in sin;
200 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
205 sin.sin_len = sizeof(sin);
206 sin.sin_family = AF_INET;
207 sin.sin_addr.s_addr = htonl(INADDR_ANY);
208 sin.sin_port = htons(TEST_PORT);
210 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
215 if (listen(s, 1) < 0) {
220 (void) mevent_add(s, EVF_READ, acceptor_callback, NULL);
222 pthread_mutex_lock(&accept_mutex);
224 while (!pthread_cond_wait(&accept_condvar, &accept_mutex)) {
225 news = accept(s, NULL, NULL);
227 perror("accept error");
229 static int first = 1;
236 tevp = mevent_add(1, EVF_TIMER, timer_callback,
240 printf("incoming connection, spawning thread\n");
241 pthread_create(&tid, NULL, echoer,
242 (void *)(uintptr_t)news);
253 pthread_create(&tid, NULL, acceptor, NULL);