12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert * Copyright 2003-2007 Niels Provos <provos@citi.umich.edu>
32b15cb3dSCy Schubert * Copyright 2007-2012 Niels Provos and Nick Mathewson
42b15cb3dSCy Schubert *
52b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without
62b15cb3dSCy Schubert * modification, are permitted provided that the following conditions
72b15cb3dSCy Schubert * are met:
82b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright
92b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer.
102b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
112b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the
122b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution.
132b15cb3dSCy Schubert * 4. The name of the author may not be used to endorse or promote products
142b15cb3dSCy Schubert * derived from this software without specific prior written permission.
152b15cb3dSCy Schubert *
162b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262b15cb3dSCy Schubert *
272b15cb3dSCy Schubert *
282b15cb3dSCy Schubert * Mon 03/10/2003 - Modified by Davide Libenzi <davidel@xmailserver.org>
292b15cb3dSCy Schubert *
302b15cb3dSCy Schubert * Added chain event propagation to improve the sensitivity of
312b15cb3dSCy Schubert * the measure respect to the event loop efficency.
322b15cb3dSCy Schubert *
332b15cb3dSCy Schubert *
342b15cb3dSCy Schubert */
352b15cb3dSCy Schubert
362b15cb3dSCy Schubert #include "event2/event-config.h"
37*a466cc55SCy Schubert #include "../util-internal.h"
382b15cb3dSCy Schubert
392b15cb3dSCy Schubert #include <sys/types.h>
402b15cb3dSCy Schubert #include <sys/stat.h>
412b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
422b15cb3dSCy Schubert #include <sys/time.h>
432b15cb3dSCy Schubert #endif
442b15cb3dSCy Schubert #ifdef _WIN32
452b15cb3dSCy Schubert #define WIN32_LEAN_AND_MEAN
462b15cb3dSCy Schubert #include <windows.h>
472b15cb3dSCy Schubert #else
482b15cb3dSCy Schubert #include <sys/socket.h>
492b15cb3dSCy Schubert #include <signal.h>
502b15cb3dSCy Schubert #include <sys/resource.h>
512b15cb3dSCy Schubert #endif
522b15cb3dSCy Schubert #include <fcntl.h>
532b15cb3dSCy Schubert #include <stdlib.h>
542b15cb3dSCy Schubert #include <stdio.h>
552b15cb3dSCy Schubert #include <string.h>
562b15cb3dSCy Schubert #ifdef EVENT__HAVE_UNISTD_H
572b15cb3dSCy Schubert #include <unistd.h>
582b15cb3dSCy Schubert #endif
592b15cb3dSCy Schubert #include <errno.h>
602b15cb3dSCy Schubert
612b15cb3dSCy Schubert #ifdef _WIN32
622b15cb3dSCy Schubert #include <getopt.h>
632b15cb3dSCy Schubert #endif
642b15cb3dSCy Schubert
652b15cb3dSCy Schubert #include <event.h>
662b15cb3dSCy Schubert #include <evutil.h>
672b15cb3dSCy Schubert
68*a466cc55SCy Schubert static ev_ssize_t count, fired;
69*a466cc55SCy Schubert static int writes, failures;
702b15cb3dSCy Schubert static evutil_socket_t *pipes;
712b15cb3dSCy Schubert static int num_pipes, num_active, num_writes;
722b15cb3dSCy Schubert static struct event *events;
73*a466cc55SCy Schubert static struct event_base *base;
742b15cb3dSCy Schubert
752b15cb3dSCy Schubert
762b15cb3dSCy Schubert static void
read_cb(evutil_socket_t fd,short which,void * arg)772b15cb3dSCy Schubert read_cb(evutil_socket_t fd, short which, void *arg)
782b15cb3dSCy Schubert {
792b15cb3dSCy Schubert ev_intptr_t idx = (ev_intptr_t) arg, widx = idx + 1;
80*a466cc55SCy Schubert unsigned char ch;
812b15cb3dSCy Schubert ev_ssize_t n;
822b15cb3dSCy Schubert
832b15cb3dSCy Schubert n = recv(fd, (char*)&ch, sizeof(ch), 0);
842b15cb3dSCy Schubert if (n >= 0)
852b15cb3dSCy Schubert count += n;
862b15cb3dSCy Schubert else
872b15cb3dSCy Schubert failures++;
882b15cb3dSCy Schubert if (writes) {
892b15cb3dSCy Schubert if (widx >= num_pipes)
902b15cb3dSCy Schubert widx -= num_pipes;
912b15cb3dSCy Schubert n = send(pipes[2 * widx + 1], "e", 1, 0);
922b15cb3dSCy Schubert if (n != 1)
932b15cb3dSCy Schubert failures++;
942b15cb3dSCy Schubert writes--;
952b15cb3dSCy Schubert fired++;
962b15cb3dSCy Schubert }
972b15cb3dSCy Schubert }
982b15cb3dSCy Schubert
992b15cb3dSCy Schubert static struct timeval *
run_once(void)1002b15cb3dSCy Schubert run_once(void)
1012b15cb3dSCy Schubert {
1022b15cb3dSCy Schubert evutil_socket_t *cp, space;
1032b15cb3dSCy Schubert long i;
1042b15cb3dSCy Schubert static struct timeval ts, te;
1052b15cb3dSCy Schubert
1062b15cb3dSCy Schubert for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
1072b15cb3dSCy Schubert if (event_initialized(&events[i]))
1082b15cb3dSCy Schubert event_del(&events[i]);
109*a466cc55SCy Schubert event_assign(&events[i], base, cp[0], EV_READ | EV_PERSIST, read_cb, (void *)(ev_intptr_t) i);
1102b15cb3dSCy Schubert event_add(&events[i], NULL);
1112b15cb3dSCy Schubert }
1122b15cb3dSCy Schubert
113*a466cc55SCy Schubert event_base_loop(base, EVLOOP_ONCE | EVLOOP_NONBLOCK);
1142b15cb3dSCy Schubert
1152b15cb3dSCy Schubert fired = 0;
1162b15cb3dSCy Schubert space = num_pipes / num_active;
1172b15cb3dSCy Schubert space = space * 2;
1182b15cb3dSCy Schubert for (i = 0; i < num_active; i++, fired++)
1192b15cb3dSCy Schubert (void) send(pipes[i * space + 1], "e", 1, 0);
1202b15cb3dSCy Schubert
1212b15cb3dSCy Schubert count = 0;
1222b15cb3dSCy Schubert writes = num_writes;
123*a466cc55SCy Schubert {
124*a466cc55SCy Schubert int xcount = 0;
1252b15cb3dSCy Schubert evutil_gettimeofday(&ts, NULL);
1262b15cb3dSCy Schubert do {
127*a466cc55SCy Schubert event_base_loop(base, EVLOOP_ONCE | EVLOOP_NONBLOCK);
1282b15cb3dSCy Schubert xcount++;
1292b15cb3dSCy Schubert } while (count != fired);
1302b15cb3dSCy Schubert evutil_gettimeofday(&te, NULL);
1312b15cb3dSCy Schubert
132*a466cc55SCy Schubert if (xcount != count)
133*a466cc55SCy Schubert fprintf(stderr, "Xcount: %d, Rcount: " EV_SSIZE_FMT "\n",
134*a466cc55SCy Schubert xcount, count);
1352b15cb3dSCy Schubert }
1362b15cb3dSCy Schubert
1372b15cb3dSCy Schubert evutil_timersub(&te, &ts, &te);
1382b15cb3dSCy Schubert
1392b15cb3dSCy Schubert return (&te);
1402b15cb3dSCy Schubert }
1412b15cb3dSCy Schubert
1422b15cb3dSCy Schubert int
main(int argc,char ** argv)1432b15cb3dSCy Schubert main(int argc, char **argv)
1442b15cb3dSCy Schubert {
145*a466cc55SCy Schubert #ifdef EVENT__HAVE_SETRLIMIT
1462b15cb3dSCy Schubert struct rlimit rl;
1472b15cb3dSCy Schubert #endif
1482b15cb3dSCy Schubert int i, c;
1492b15cb3dSCy Schubert struct timeval *tv;
1502b15cb3dSCy Schubert evutil_socket_t *cp;
151*a466cc55SCy Schubert const char **methods;
152*a466cc55SCy Schubert const char *method = NULL;
153*a466cc55SCy Schubert struct event_config *cfg = NULL;
1542b15cb3dSCy Schubert
1552b15cb3dSCy Schubert #ifdef _WIN32
1562b15cb3dSCy Schubert WSADATA WSAData;
1572b15cb3dSCy Schubert WSAStartup(0x101, &WSAData);
1582b15cb3dSCy Schubert #endif
1592b15cb3dSCy Schubert num_pipes = 100;
1602b15cb3dSCy Schubert num_active = 1;
1612b15cb3dSCy Schubert num_writes = num_pipes;
162*a466cc55SCy Schubert while ((c = getopt(argc, argv, "n:a:w:m:l")) != -1) {
1632b15cb3dSCy Schubert switch (c) {
1642b15cb3dSCy Schubert case 'n':
1652b15cb3dSCy Schubert num_pipes = atoi(optarg);
1662b15cb3dSCy Schubert break;
1672b15cb3dSCy Schubert case 'a':
1682b15cb3dSCy Schubert num_active = atoi(optarg);
1692b15cb3dSCy Schubert break;
1702b15cb3dSCy Schubert case 'w':
1712b15cb3dSCy Schubert num_writes = atoi(optarg);
1722b15cb3dSCy Schubert break;
173*a466cc55SCy Schubert case 'm':
174*a466cc55SCy Schubert method = optarg;
175*a466cc55SCy Schubert break;
176*a466cc55SCy Schubert case 'l':
177*a466cc55SCy Schubert methods = event_get_supported_methods();
178*a466cc55SCy Schubert fprintf(stdout, "Using Libevent %s. Available methods are:\n",
179*a466cc55SCy Schubert event_get_version());
180*a466cc55SCy Schubert for (i = 0; methods[i] != NULL; ++i)
181*a466cc55SCy Schubert printf(" %s\n", methods[i]);
182*a466cc55SCy Schubert exit(0);
1832b15cb3dSCy Schubert default:
1842b15cb3dSCy Schubert fprintf(stderr, "Illegal argument \"%c\"\n", c);
1852b15cb3dSCy Schubert exit(1);
1862b15cb3dSCy Schubert }
1872b15cb3dSCy Schubert }
1882b15cb3dSCy Schubert
189*a466cc55SCy Schubert #ifdef EVENT__HAVE_SETRLIMIT
1902b15cb3dSCy Schubert rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
1912b15cb3dSCy Schubert if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
1922b15cb3dSCy Schubert perror("setrlimit");
1932b15cb3dSCy Schubert exit(1);
1942b15cb3dSCy Schubert }
1952b15cb3dSCy Schubert #endif
1962b15cb3dSCy Schubert
1972b15cb3dSCy Schubert events = calloc(num_pipes, sizeof(struct event));
1982b15cb3dSCy Schubert pipes = calloc(num_pipes * 2, sizeof(evutil_socket_t));
1992b15cb3dSCy Schubert if (events == NULL || pipes == NULL) {
2002b15cb3dSCy Schubert perror("malloc");
2012b15cb3dSCy Schubert exit(1);
2022b15cb3dSCy Schubert }
2032b15cb3dSCy Schubert
204*a466cc55SCy Schubert if (method != NULL) {
205*a466cc55SCy Schubert cfg = event_config_new();
206*a466cc55SCy Schubert methods = event_get_supported_methods();
207*a466cc55SCy Schubert for (i = 0; methods[i] != NULL; ++i)
208*a466cc55SCy Schubert if (strcmp(methods[i], method))
209*a466cc55SCy Schubert event_config_avoid_method(cfg, methods[i]);
210*a466cc55SCy Schubert base = event_base_new_with_config(cfg);
211*a466cc55SCy Schubert event_config_free(cfg);
212*a466cc55SCy Schubert } else
213*a466cc55SCy Schubert base = event_base_new();
2142b15cb3dSCy Schubert
2152b15cb3dSCy Schubert for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
2162b15cb3dSCy Schubert #ifdef USE_PIPES
2172b15cb3dSCy Schubert if (pipe(cp) == -1) {
2182b15cb3dSCy Schubert #else
2192b15cb3dSCy Schubert if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
2202b15cb3dSCy Schubert #endif
2212b15cb3dSCy Schubert perror("pipe");
2222b15cb3dSCy Schubert exit(1);
2232b15cb3dSCy Schubert }
2242b15cb3dSCy Schubert }
2252b15cb3dSCy Schubert
2262b15cb3dSCy Schubert for (i = 0; i < 25; i++) {
2272b15cb3dSCy Schubert tv = run_once();
2282b15cb3dSCy Schubert if (tv == NULL)
2292b15cb3dSCy Schubert exit(1);
2302b15cb3dSCy Schubert fprintf(stdout, "%ld\n",
2312b15cb3dSCy Schubert tv->tv_sec * 1000000L + tv->tv_usec);
2322b15cb3dSCy Schubert }
2332b15cb3dSCy Schubert
2342b15cb3dSCy Schubert exit(0);
2352b15cb3dSCy Schubert }
236