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