2003-03-10 04:56:41 +00:00
|
|
|
/*
|
2009-01-27 22:34:36 +00:00
|
|
|
* Copyright 2003-2007 Niels Provos <provos@citi.umich.edu>
|
2012-02-10 17:29:53 -05:00
|
|
|
* Copyright 2007-2012 Niels Provos and Nick Mathewson
|
2003-03-10 04:56:41 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2003-10-04 23:33:04 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Mon 03/10/2003 - Modified by Davide Libenzi <davidel@xmailserver.org>
|
|
|
|
*
|
|
|
|
* Added chain event propagation to improve the sensitivity of
|
|
|
|
* the measure respect to the event loop efficency.
|
|
|
|
*
|
|
|
|
*
|
2003-03-10 04:56:41 +00:00
|
|
|
*/
|
|
|
|
|
2010-07-07 16:45:03 -04:00
|
|
|
#include "event2/event-config.h"
|
2003-03-10 04:56:41 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2012-02-29 15:07:31 -05:00
|
|
|
#ifdef EVENT__HAVE_SYS_TIME_H
|
2003-03-10 04:56:41 +00:00
|
|
|
#include <sys/time.h>
|
2011-05-25 10:58:48 +02:00
|
|
|
#endif
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifdef _WIN32
|
2011-05-25 11:04:43 +02:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2007-09-20 19:08:20 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2003-03-10 04:56:41 +00:00
|
|
|
#include <sys/socket.h>
|
2009-01-22 02:47:35 +00:00
|
|
|
#include <signal.h>
|
2003-03-10 04:56:41 +00:00
|
|
|
#include <sys/resource.h>
|
2007-09-20 19:08:20 +00:00
|
|
|
#endif
|
2003-03-10 04:56:41 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2012-02-29 15:07:31 -05:00
|
|
|
#ifdef EVENT__HAVE_UNISTD_H
|
2003-03-10 04:56:41 +00:00
|
|
|
#include <unistd.h>
|
2011-05-25 10:58:48 +02:00
|
|
|
#endif
|
2003-03-10 04:56:41 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
2014-01-21 11:30:27 +01:00
|
|
|
#ifdef _WIN32
|
2013-12-12 16:33:20 +01:00
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
|
|
|
|
2003-03-10 04:56:41 +00:00
|
|
|
#include <event.h>
|
2007-09-20 19:08:20 +00:00
|
|
|
#include <evutil.h>
|
2003-03-10 04:56:41 +00:00
|
|
|
|
2014-01-08 11:54:56 -05:00
|
|
|
static int count, writes, fired, failures;
|
2012-11-01 17:38:34 -04:00
|
|
|
static evutil_socket_t *pipes;
|
2003-10-04 23:33:04 +00:00
|
|
|
static int num_pipes, num_active, num_writes;
|
|
|
|
static struct event *events;
|
|
|
|
|
|
|
|
|
2007-11-26 19:18:49 +00:00
|
|
|
static void
|
2010-03-05 12:47:46 -05:00
|
|
|
read_cb(evutil_socket_t fd, short which, void *arg)
|
2003-03-10 04:56:41 +00:00
|
|
|
{
|
2012-11-01 17:38:34 -04:00
|
|
|
ev_intptr_t idx = (ev_intptr_t) arg, widx = idx + 1;
|
2015-08-25 15:24:39 +02:00
|
|
|
unsigned char ch;
|
2014-01-08 11:54:56 -05:00
|
|
|
ev_ssize_t n;
|
2003-10-04 23:33:04 +00:00
|
|
|
|
2014-01-08 11:54:56 -05:00
|
|
|
n = recv(fd, (char*)&ch, sizeof(ch), 0);
|
|
|
|
if (n >= 0)
|
|
|
|
count += n;
|
|
|
|
else
|
2014-01-08 11:58:50 -05:00
|
|
|
failures++;
|
2003-10-04 23:33:04 +00:00
|
|
|
if (writes) {
|
|
|
|
if (widx >= num_pipes)
|
|
|
|
widx -= num_pipes;
|
2014-01-08 11:54:56 -05:00
|
|
|
n = send(pipes[2 * widx + 1], "e", 1, 0);
|
|
|
|
if (n != 1)
|
2014-01-08 11:58:50 -05:00
|
|
|
failures++;
|
2003-10-04 23:33:04 +00:00
|
|
|
writes--;
|
|
|
|
fired++;
|
|
|
|
}
|
2003-03-10 04:56:41 +00:00
|
|
|
}
|
|
|
|
|
2007-11-26 19:18:49 +00:00
|
|
|
static struct timeval *
|
2003-10-04 23:33:04 +00:00
|
|
|
run_once(void)
|
2003-03-10 04:56:41 +00:00
|
|
|
{
|
2012-11-01 17:38:34 -04:00
|
|
|
evutil_socket_t *cp, space;
|
2008-04-17 19:25:35 +00:00
|
|
|
long i;
|
2003-03-10 04:56:41 +00:00
|
|
|
static struct timeval ts, te;
|
|
|
|
|
|
|
|
for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
|
2010-09-06 10:10:17 -04:00
|
|
|
if (event_initialized(&events[i]))
|
|
|
|
event_del(&events[i]);
|
2012-11-01 17:38:34 -04:00
|
|
|
event_set(&events[i], cp[0], EV_READ | EV_PERSIST, read_cb, (void *)(ev_intptr_t) i);
|
2003-03-10 04:56:41 +00:00
|
|
|
event_add(&events[i], NULL);
|
|
|
|
}
|
|
|
|
|
2003-10-04 23:33:04 +00:00
|
|
|
event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK);
|
2003-03-10 04:56:41 +00:00
|
|
|
|
2003-10-04 23:33:04 +00:00
|
|
|
fired = 0;
|
|
|
|
space = num_pipes / num_active;
|
2003-03-10 04:56:41 +00:00
|
|
|
space = space * 2;
|
2003-10-04 23:33:04 +00:00
|
|
|
for (i = 0; i < num_active; i++, fired++)
|
2013-08-06 19:28:53 -04:00
|
|
|
(void) send(pipes[i * space + 1], "e", 1, 0);
|
2003-03-10 04:56:41 +00:00
|
|
|
|
|
|
|
count = 0;
|
2003-10-04 23:33:04 +00:00
|
|
|
writes = num_writes;
|
|
|
|
{ int xcount = 0;
|
2011-05-25 15:11:01 +02:00
|
|
|
evutil_gettimeofday(&ts, NULL);
|
2003-10-04 23:33:04 +00:00
|
|
|
do {
|
|
|
|
event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK);
|
|
|
|
xcount++;
|
|
|
|
} while (count != fired);
|
2011-05-25 15:11:01 +02:00
|
|
|
evutil_gettimeofday(&te, NULL);
|
2003-03-10 04:56:41 +00:00
|
|
|
|
2003-10-04 23:33:04 +00:00
|
|
|
if (xcount != count) fprintf(stderr, "Xcount: %d, Rcount: %d\n", xcount, count);
|
2003-03-10 04:56:41 +00:00
|
|
|
}
|
|
|
|
|
2007-11-07 06:01:57 +00:00
|
|
|
evutil_timersub(&te, &ts, &te);
|
2003-10-04 23:33:04 +00:00
|
|
|
|
2003-03-10 04:56:41 +00:00
|
|
|
return (&te);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-02-19 03:39:50 -05:00
|
|
|
main(int argc, char **argv)
|
2003-03-10 04:56:41 +00:00
|
|
|
{
|
2017-01-28 16:28:57 +00:00
|
|
|
#ifdef EVENT__HAVE_SETRLIMIT
|
2003-03-10 04:56:41 +00:00
|
|
|
struct rlimit rl;
|
2007-09-20 19:08:20 +00:00
|
|
|
#endif
|
2003-10-04 23:33:04 +00:00
|
|
|
int i, c;
|
2003-03-10 04:56:41 +00:00
|
|
|
struct timeval *tv;
|
2012-11-01 17:38:34 -04:00
|
|
|
evutil_socket_t *cp;
|
2003-03-10 04:56:41 +00:00
|
|
|
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifdef _WIN32
|
2007-12-06 19:36:49 +00:00
|
|
|
WSADATA WSAData;
|
|
|
|
WSAStartup(0x101, &WSAData);
|
|
|
|
#endif
|
2003-03-10 04:56:41 +00:00
|
|
|
num_pipes = 100;
|
|
|
|
num_active = 1;
|
2003-10-04 23:33:04 +00:00
|
|
|
num_writes = num_pipes;
|
|
|
|
while ((c = getopt(argc, argv, "n:a:w:")) != -1) {
|
2003-03-10 04:56:41 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'n':
|
|
|
|
num_pipes = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
num_active = atoi(optarg);
|
|
|
|
break;
|
2003-10-04 23:33:04 +00:00
|
|
|
case 'w':
|
|
|
|
num_writes = atoi(optarg);
|
|
|
|
break;
|
2003-03-10 04:56:41 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Illegal argument \"%c\"\n", c);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 16:28:57 +00:00
|
|
|
#ifdef EVENT__HAVE_SETRLIMIT
|
2003-03-10 04:56:41 +00:00
|
|
|
rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
|
2003-10-04 23:33:04 +00:00
|
|
|
perror("setrlimit");
|
2003-03-10 04:56:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-09-20 19:08:20 +00:00
|
|
|
#endif
|
2003-03-10 04:56:41 +00:00
|
|
|
|
|
|
|
events = calloc(num_pipes, sizeof(struct event));
|
2012-11-01 17:38:34 -04:00
|
|
|
pipes = calloc(num_pipes * 2, sizeof(evutil_socket_t));
|
2003-10-04 23:33:04 +00:00
|
|
|
if (events == NULL || pipes == NULL) {
|
2003-03-10 04:56:41 +00:00
|
|
|
perror("malloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
event_init();
|
|
|
|
|
|
|
|
for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
|
2003-10-04 23:33:04 +00:00
|
|
|
#ifdef USE_PIPES
|
2003-03-10 04:56:41 +00:00
|
|
|
if (pipe(cp) == -1) {
|
2003-10-04 23:33:04 +00:00
|
|
|
#else
|
2007-09-20 19:08:20 +00:00
|
|
|
if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
|
2003-10-04 23:33:04 +00:00
|
|
|
#endif
|
2003-03-10 04:56:41 +00:00
|
|
|
perror("pipe");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 25; i++) {
|
2003-10-04 23:33:04 +00:00
|
|
|
tv = run_once();
|
2003-03-10 04:56:41 +00:00
|
|
|
if (tv == NULL)
|
|
|
|
exit(1);
|
|
|
|
fprintf(stdout, "%ld\n",
|
2003-10-04 23:33:04 +00:00
|
|
|
tv->tv_sec * 1000000L + tv->tv_usec);
|
2003-03-10 04:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|