1047f369cy/*
2047f369cy * Copyright 2007-2012 Niels Provos and Nick Mathewson
3047f369cy *
4047f369cy * Redistribution and use in source and binary forms, with or without
5047f369cy * modification, are permitted provided that the following conditions
6047f369cy * are met:
7047f369cy * 1. Redistributions of source code must retain the above copyright
8047f369cy *    notice, this list of conditions and the following disclaimer.
9047f369cy * 2. Redistributions in binary form must reproduce the above copyright
10047f369cy *    notice, this list of conditions and the following disclaimer in the
11047f369cy *    documentation and/or other materials provided with the distribution.
12047f369cy * 4. The name of the author may not be used to endorse or promote products
13047f369cy *    derived from this software without specific prior written permission.
14047f369cy *
15047f369cy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16047f369cy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17047f369cy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18047f369cy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19047f369cy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20047f369cy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21047f369cy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22047f369cy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23047f369cy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24047f369cy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25047f369cy *
26047f369cy */
27047f369cy
28047f369cy#include "event2/event-config.h"
29047f369cy
30047f369cy#include <sys/types.h>
31047f369cy#include <sys/stat.h>
32047f369cy#ifdef EVENT__HAVE_SYS_TIME_H
33047f369cy#include <sys/time.h>
34047f369cy#endif
35047f369cy#ifdef _WIN32
36047f369cy#define WIN32_LEAN_AND_MEAN
37047f369cy#include <windows.h>
38047f369cy#else
39047f369cy#include <sys/socket.h>
40047f369cy#include <sys/resource.h>
41047f369cy#endif
42047f369cy#include <signal.h>
43047f369cy#include <fcntl.h>
44047f369cy#include <stdlib.h>
45047f369cy#include <stdio.h>
46047f369cy#include <string.h>
47047f369cy#ifdef EVENT__HAVE_UNISTD_H
48047f369cy#include <unistd.h>
49047f369cy#endif
50047f369cy#include <errno.h>
51047f369cy#include <getopt.h>
52047f369cy#include <event.h>
53047f369cy#include <evutil.h>
54047f369cy
55047f369cy/*
56047f369cy * This benchmark tests how quickly we can propagate a write down a chain
57047f369cy * of socket pairs.  We start by writing to the first socket pair and all
58047f369cy * events will fire subsequently until the last socket pair has been reached
59047f369cy * and the benchmark terminates.
60047f369cy */
61047f369cy
62047f369cystatic int fired;
63047f369cystatic evutil_socket_t *pipes;
64047f369cystatic struct event *events;
65047f369cy
66047f369cystatic void
67047f369cyread_cb(evutil_socket_t fd, short which, void *arg)
68047f369cy{
69047f369cy	char ch;
70047f369cy	evutil_socket_t sock = (evutil_socket_t)(ev_intptr_t)arg;
71047f369cy
72047f369cy	(void) recv(fd, &ch, sizeof(ch), 0);
73047f369cy	if (sock >= 0) {
74047f369cy		if (send(sock, "e", 1, 0) < 0)
75047f369cy			perror("send");
76047f369cy	}
77047f369cy	fired++;
78047f369cy}
79047f369cy
80047f369cystatic struct timeval *
81047f369cyrun_once(int num_pipes)
82047f369cy{
83047f369cy	int i;
84047f369cy	evutil_socket_t *cp;
85047f369cy	static struct timeval ts, te, tv_timeout;
86047f369cy
87047f369cy	events = (struct event *)calloc(num_pipes, sizeof(struct event));
88047f369cy	pipes = (evutil_socket_t *)calloc(num_pipes * 2, sizeof(evutil_socket_t));
89047f369cy
90047f369cy	if (events == NULL || pipes == NULL) {
91047f369cy		perror("malloc");
92047f369cy		exit(1);
93047f369cy	}
94047f369cy
95047f369cy	for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
96047f369cy		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
97047f369cy			perror("socketpair");
98047f369cy			exit(1);
99047f369cy		}
100047f369cy	}
101047f369cy
102047f369cy	/* measurements includes event setup */
103047f369cy	evutil_gettimeofday(&ts, NULL);
104047f369cy
105047f369cy	/* provide a default timeout for events */
106047f369cy	evutil_timerclear(&tv_timeout);
107047f369cy	tv_timeout.tv_sec = 60;
108047f369cy
109047f369cy	for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
110047f369cy		evutil_socket_t fd = i < num_pipes - 1 ? cp[3] : -1;
111047f369cy		event_set(&events[i], cp[0], EV_READ, read_cb,
112047f369cy		    (void *)(ev_intptr_t)fd);
113047f369cy		event_add(&events[i], &tv_timeout);
114047f369cy	}
115047f369cy
116047f369cy	fired = 0;
117047f369cy
118047f369cy	/* kick everything off with a single write */
119047f369cy	if (send(pipes[1], "e", 1, 0) < 0)
120047f369cy		perror("send");
121047f369cy
122047f369cy	event_dispatch();
123047f369cy
124047f369cy	evutil_gettimeofday(&te, NULL);
125047f369cy	evutil_timersub(&te, &ts, &te);
126047f369cy
127047f369cy	for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
128047f369cy		event_del(&events[i]);
129047f369cy		evutil_closesocket(cp[0]);
130047f369cy		evutil_closesocket(cp[1]);
131047f369cy	}
132047f369cy
133047f369cy	free(pipes);
134047f369cy	free(events);
135047f369cy
136047f369cy	return (&te);
137047f369cy}
138047f369cy
139047f369cyint
140047f369cymain(int argc, char **argv)
141047f369cy{
142047f369cy#ifdef HAVE_SETRLIMIT
143047f369cy	struct rlimit rl;
144047f369cy#endif
145047f369cy	int i, c;
146047f369cy	struct timeval *tv;
147047f369cy
148047f369cy	int num_pipes = 100;
149047f369cy#ifdef _WIN32
150047f369cy	WSADATA WSAData;
151047f369cy	WSAStartup(0x101, &WSAData);
152047f369cy#endif
153047f369cy
154047f369cy	while ((c = getopt(argc, argv, "n:")) != -1) {
155047f369cy		switch (c) {
156047f369cy		case 'n':
157047f369cy			num_pipes = atoi(optarg);
158047f369cy			break;
159047f369cy		default:
160047f369cy			fprintf(stderr, "Illegal argument \"%c\"\n", c);
161047f369cy			exit(1);
162047f369cy		}
163047f369cy	}
164047f369cy
165047f369cy#ifdef HAVE_SETRLIMIT
166047f369cy	rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
167047f369cy	if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
168047f369cy		perror("setrlimit");
169047f369cy		exit(1);
170047f369cy	}
171047f369cy#endif
172047f369cy
173047f369cy	event_init();
174047f369cy
175047f369cy	for (i = 0; i < 25; i++) {
176047f369cy		tv = run_once(num_pipes);
177047f369cy		if (tv == NULL)
178047f369cy			exit(1);
179047f369cy		fprintf(stdout, "%ld\n",
180047f369cy			tv->tv_sec * 1000000L + tv->tv_usec);
181047f369cy	}
182047f369cy
183047f369cy#ifdef _WIN32
184047f369cy	WSACleanup();
185047f369cy#endif
186047f369cy
187047f369cy	exit(0);
188047f369cy}
189