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