1*9034ec65Schristos /*	$NetBSD: bench_cascade.c,v 1.5 2020/05/25 20:47:34 christos Exp $	*/
22b3787f6Schristos 
32b3787f6Schristos /*
42b3787f6Schristos  * Copyright 2007-2012 Niels Provos and Nick Mathewson
52b3787f6Schristos  *
62b3787f6Schristos  * Redistribution and use in source and binary forms, with or without
72b3787f6Schristos  * modification, are permitted provided that the following conditions
82b3787f6Schristos  * are met:
92b3787f6Schristos  * 1. Redistributions of source code must retain the above copyright
102b3787f6Schristos  *    notice, this list of conditions and the following disclaimer.
112b3787f6Schristos  * 2. Redistributions in binary form must reproduce the above copyright
122b3787f6Schristos  *    notice, this list of conditions and the following disclaimer in the
132b3787f6Schristos  *    documentation and/or other materials provided with the distribution.
142b3787f6Schristos  * 4. The name of the author may not be used to endorse or promote products
152b3787f6Schristos  *    derived from this software without specific prior written permission.
162b3787f6Schristos  *
172b3787f6Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
182b3787f6Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
192b3787f6Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
202b3787f6Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
212b3787f6Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
222b3787f6Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232b3787f6Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242b3787f6Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252b3787f6Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
262b3787f6Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272b3787f6Schristos  *
282b3787f6Schristos  */
292b3787f6Schristos 
302b3787f6Schristos #include "event2/event-config.h"
312b3787f6Schristos 
322b3787f6Schristos #include <sys/types.h>
332b3787f6Schristos #include <sys/stat.h>
342b3787f6Schristos #ifdef EVENT__HAVE_SYS_TIME_H
352b3787f6Schristos #include <sys/time.h>
362b3787f6Schristos #endif
372b3787f6Schristos #ifdef _WIN32
382b3787f6Schristos #define WIN32_LEAN_AND_MEAN
392b3787f6Schristos #include <windows.h>
402b3787f6Schristos #else
412b3787f6Schristos #include <sys/socket.h>
422b3787f6Schristos #include <sys/resource.h>
432b3787f6Schristos #endif
442b3787f6Schristos #include <signal.h>
452b3787f6Schristos #include <fcntl.h>
462b3787f6Schristos #include <stdlib.h>
472b3787f6Schristos #include <stdio.h>
482b3787f6Schristos #include <string.h>
492b3787f6Schristos #ifdef EVENT__HAVE_UNISTD_H
502b3787f6Schristos #include <unistd.h>
512b3787f6Schristos #endif
522b3787f6Schristos #include <errno.h>
531b6f2cd4Schristos #include <getopt.h>
542b3787f6Schristos #include <event.h>
552b3787f6Schristos #include <evutil.h>
562b3787f6Schristos 
572b3787f6Schristos /*
582b3787f6Schristos  * This benchmark tests how quickly we can propagate a write down a chain
592b3787f6Schristos  * of socket pairs.  We start by writing to the first socket pair and all
602b3787f6Schristos  * events will fire subsequently until the last socket pair has been reached
612b3787f6Schristos  * and the benchmark terminates.
622b3787f6Schristos  */
632b3787f6Schristos 
642b3787f6Schristos static int fired;
652b3787f6Schristos static evutil_socket_t *pipes;
662b3787f6Schristos static struct event *events;
672b3787f6Schristos 
682b3787f6Schristos static void
read_cb(evutil_socket_t fd,short which,void * arg)692b3787f6Schristos read_cb(evutil_socket_t fd, short which, void *arg)
702b3787f6Schristos {
712b3787f6Schristos 	char ch;
722b3787f6Schristos 	evutil_socket_t sock = (evutil_socket_t)(ev_intptr_t)arg;
732b3787f6Schristos 
741b6f2cd4Schristos 	(void) recv(fd, &ch, sizeof(ch), 0);
752b3787f6Schristos 	if (sock >= 0) {
762b3787f6Schristos 		if (send(sock, "e", 1, 0) < 0)
772b3787f6Schristos 			perror("send");
782b3787f6Schristos 	}
792b3787f6Schristos 	fired++;
802b3787f6Schristos }
812b3787f6Schristos 
822b3787f6Schristos static struct timeval *
run_once(int num_pipes)832b3787f6Schristos run_once(int num_pipes)
842b3787f6Schristos {
852b3787f6Schristos 	int i;
862b3787f6Schristos 	evutil_socket_t *cp;
872b3787f6Schristos 	static struct timeval ts, te, tv_timeout;
882b3787f6Schristos 
891b6f2cd4Schristos 	events = (struct event *)calloc(num_pipes, sizeof(struct event));
901b6f2cd4Schristos 	pipes = (evutil_socket_t *)calloc(num_pipes * 2, sizeof(evutil_socket_t));
912b3787f6Schristos 
922b3787f6Schristos 	if (events == NULL || pipes == NULL) {
932b3787f6Schristos 		perror("malloc");
942b3787f6Schristos 		exit(1);
952b3787f6Schristos 	}
962b3787f6Schristos 
972b3787f6Schristos 	for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
982b3787f6Schristos 		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
992b3787f6Schristos 			perror("socketpair");
1002b3787f6Schristos 			exit(1);
1012b3787f6Schristos 		}
1022b3787f6Schristos 	}
1032b3787f6Schristos 
1042b3787f6Schristos 	/* measurements includes event setup */
1052b3787f6Schristos 	evutil_gettimeofday(&ts, NULL);
1062b3787f6Schristos 
1072b3787f6Schristos 	/* provide a default timeout for events */
1082b3787f6Schristos 	evutil_timerclear(&tv_timeout);
1092b3787f6Schristos 	tv_timeout.tv_sec = 60;
1102b3787f6Schristos 
1112b3787f6Schristos 	for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
1122b3787f6Schristos 		evutil_socket_t fd = i < num_pipes - 1 ? cp[3] : -1;
1132b3787f6Schristos 		event_set(&events[i], cp[0], EV_READ, read_cb,
1142b3787f6Schristos 		    (void *)(ev_intptr_t)fd);
1152b3787f6Schristos 		event_add(&events[i], &tv_timeout);
1162b3787f6Schristos 	}
1172b3787f6Schristos 
1182b3787f6Schristos 	fired = 0;
1192b3787f6Schristos 
1202b3787f6Schristos 	/* kick everything off with a single write */
1212b3787f6Schristos 	if (send(pipes[1], "e", 1, 0) < 0)
1222b3787f6Schristos 		perror("send");
1232b3787f6Schristos 
1242b3787f6Schristos 	event_dispatch();
1252b3787f6Schristos 
1262b3787f6Schristos 	evutil_gettimeofday(&te, NULL);
1272b3787f6Schristos 	evutil_timersub(&te, &ts, &te);
1282b3787f6Schristos 
1292b3787f6Schristos 	for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
1302b3787f6Schristos 		event_del(&events[i]);
1311b6f2cd4Schristos 		evutil_closesocket(cp[0]);
1321b6f2cd4Schristos 		evutil_closesocket(cp[1]);
1332b3787f6Schristos 	}
1342b3787f6Schristos 
1352b3787f6Schristos 	free(pipes);
1362b3787f6Schristos 	free(events);
1372b3787f6Schristos 
1382b3787f6Schristos 	return (&te);
1392b3787f6Schristos }
1402b3787f6Schristos 
1412b3787f6Schristos int
main(int argc,char ** argv)1422b3787f6Schristos main(int argc, char **argv)
1432b3787f6Schristos {
1442b3787f6Schristos #ifdef HAVE_SETRLIMIT
1452b3787f6Schristos 	struct rlimit rl;
1462b3787f6Schristos #endif
1472b3787f6Schristos 	int i, c;
1482b3787f6Schristos 	struct timeval *tv;
1492b3787f6Schristos 
1502b3787f6Schristos 	int num_pipes = 100;
1511b6f2cd4Schristos #ifdef _WIN32
1521b6f2cd4Schristos 	WSADATA WSAData;
1531b6f2cd4Schristos 	WSAStartup(0x101, &WSAData);
1541b6f2cd4Schristos #endif
1551b6f2cd4Schristos 
1562b3787f6Schristos 	while ((c = getopt(argc, argv, "n:")) != -1) {
1572b3787f6Schristos 		switch (c) {
1582b3787f6Schristos 		case 'n':
1592b3787f6Schristos 			num_pipes = atoi(optarg);
1602b3787f6Schristos 			break;
1612b3787f6Schristos 		default:
1622b3787f6Schristos 			fprintf(stderr, "Illegal argument \"%c\"\n", c);
1632b3787f6Schristos 			exit(1);
1642b3787f6Schristos 		}
1652b3787f6Schristos 	}
1662b3787f6Schristos 
1672b3787f6Schristos #ifdef HAVE_SETRLIMIT
1682b3787f6Schristos 	rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
1692b3787f6Schristos 	if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
1702b3787f6Schristos 		perror("setrlimit");
1712b3787f6Schristos 		exit(1);
1722b3787f6Schristos 	}
1732b3787f6Schristos #endif
1742b3787f6Schristos 
1752b3787f6Schristos 	event_init();
1762b3787f6Schristos 
1772b3787f6Schristos 	for (i = 0; i < 25; i++) {
1782b3787f6Schristos 		tv = run_once(num_pipes);
1792b3787f6Schristos 		if (tv == NULL)
1802b3787f6Schristos 			exit(1);
1812b3787f6Schristos 		fprintf(stdout, "%ld\n",
1822b3787f6Schristos 			tv->tv_sec * 1000000L + tv->tv_usec);
1832b3787f6Schristos 	}
1842b3787f6Schristos 
1851b6f2cd4Schristos #ifdef _WIN32
1861b6f2cd4Schristos 	WSACleanup();
1871b6f2cd4Schristos #endif
1881b6f2cd4Schristos 
1892b3787f6Schristos 	exit(0);
1902b3787f6Schristos }
191