2 * Copyright 2003 Niels Provos <provos@citi.umich.edu>
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.
33 * Wed 2006-12-27 - Modified by Leandro Lucarella <llucax+eventxx@gmail.com>
35 * Adapted to test the C++ inteface.
43 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/signal.h>
48 #include <sys/resource.h>
61 static int count, writes, fired;
63 static int num_pipes, num_active, num_writes;
64 static std::vector< eventxx::cevent* > events;
65 static eventxx::dispatcher d;
69 read_cb(int fd, short which, void *arg)
71 int idx = (int) arg, widx = idx + 1;
74 count += read(fd, &ch, sizeof(ch));
76 if (widx >= num_pipes)
78 write(pipes[2 * widx + 1], "e", 1);
88 static eventxx::time ts, te;
90 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
91 events.push_back(new eventxx::cevent(cp[0],
92 eventxx::READ | eventxx::PERSIST, read_cb, (void *) i));
96 d.dispatch(EVLOOP_ONCE | EVLOOP_NONBLOCK);
99 space = num_pipes / num_active;
101 for (i = 0; i < num_active; i++, fired++)
102 write(pipes[i * space + 1], "e", 1);
108 gettimeofday(&ts, NULL);
110 d.dispatch(EVLOOP_ONCE | EVLOOP_NONBLOCK);
112 } while (count != fired);
113 gettimeofday(&te, NULL);
115 if (xcount != count) fprintf(stderr, "Xcount: %d, Rcount: %d\n", xcount, count);
118 timersub(&te, &ts, &te);
120 for (i = 0; i < num_pipes; i++) {
130 main (int argc, char **argv)
140 num_writes = num_pipes;
141 while ((c = getopt(argc, argv, "n:a:w:")) != -1) {
144 num_pipes = atoi(optarg);
147 num_active = atoi(optarg);
150 num_writes = atoi(optarg);
153 fprintf(stderr, "Illegal argument \"%c\"\n", c);
158 rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
159 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
164 pipes = new int[num_pipes * 2];
166 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
168 if (pipe(cp) == -1) {
170 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
177 for (i = 0; i < 25; i++) {
180 fprintf(stdout, "%ld\n",
181 tv->sec() * 1000000L + tv->usec());