2 * Copyright 2003-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2007-2012 Niels Provos and Nick Mathewson
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.
13 * 4. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Mon 03/10/2003 - Modified by Davide Libenzi <davidel@xmailserver.org>
30 * Added chain event propagation to improve the sensitivity of
31 * the measure respect to the event loop efficency.
36 #include "event2/event-config.h"
37 #include "../util-internal.h"
39 #include <sys/types.h>
41 #ifdef EVENT__HAVE_SYS_TIME_H
45 #define WIN32_LEAN_AND_MEAN
48 #include <sys/socket.h>
50 #include <sys/resource.h>
56 #ifdef EVENT__HAVE_UNISTD_H
68 static ev_ssize_t count, fired;
69 static int writes, failures;
70 static evutil_socket_t *pipes;
71 static int num_pipes, num_active, num_writes;
72 static struct event *events;
76 read_cb(evutil_socket_t fd, short which, void *arg)
78 ev_intptr_t idx = (ev_intptr_t) arg, widx = idx + 1;
82 n = recv(fd, (char*)&ch, sizeof(ch), 0);
88 if (widx >= num_pipes)
90 n = send(pipes[2 * widx + 1], "e", 1, 0);
98 static struct timeval *
101 evutil_socket_t *cp, space;
103 static struct timeval ts, te;
105 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
106 if (event_initialized(&events[i]))
107 event_del(&events[i]);
108 event_set(&events[i], cp[0], EV_READ | EV_PERSIST, read_cb, (void *)(ev_intptr_t) i);
109 event_add(&events[i], NULL);
112 event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK);
115 space = num_pipes / num_active;
117 for (i = 0; i < num_active; i++, fired++)
118 (void) send(pipes[i * space + 1], "e", 1, 0);
124 evutil_gettimeofday(&ts, NULL);
126 event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK);
128 } while (count != fired);
129 evutil_gettimeofday(&te, NULL);
132 fprintf(stderr, "Xcount: %d, Rcount: " EV_SSIZE_FMT "\n",
136 evutil_timersub(&te, &ts, &te);
142 main(int argc, char **argv)
144 #ifdef EVENT__HAVE_SETRLIMIT
153 WSAStartup(0x101, &WSAData);
157 num_writes = num_pipes;
158 while ((c = getopt(argc, argv, "n:a:w:")) != -1) {
161 num_pipes = atoi(optarg);
164 num_active = atoi(optarg);
167 num_writes = atoi(optarg);
170 fprintf(stderr, "Illegal argument \"%c\"\n", c);
175 #ifdef EVENT__HAVE_SETRLIMIT
176 rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
177 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
183 events = calloc(num_pipes, sizeof(struct event));
184 pipes = calloc(num_pipes * 2, sizeof(evutil_socket_t));
185 if (events == NULL || pipes == NULL) {
192 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
194 if (pipe(cp) == -1) {
196 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
203 for (i = 0; i < 25; i++) {
207 fprintf(stdout, "%ld\n",
208 tv->tv_sec * 1000000L + tv->tv_usec);