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 unsigned 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 size_t idx = (size_t) arg, widx = idx + 1;
74 count += read(fd, &ch, sizeof(ch));
76 if (widx >= num_pipes)
78 write(pipes[2 * widx + 1], "e", 1);
89 static eventxx::time ts, te;
91 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
92 events.push_back(new eventxx::cevent(cp[0],
93 eventxx::READ | eventxx::PERSIST, read_cb, (void *) i));
97 d.dispatch(eventxx::ONCE | eventxx::NONBLOCK);
100 space = num_pipes / num_active;
102 for (i = 0; i < num_active; i++, fired++)
103 write(pipes[i * space + 1], "e", 1);
109 gettimeofday(&ts, NULL);
111 d.dispatch(EVLOOP_ONCE | EVLOOP_NONBLOCK);
113 } while (count != fired);
114 gettimeofday(&te, NULL);
116 if (xcount != count) fprintf(stderr, "Xcount: %d, Rcount: %d\n", xcount, count);
119 timersub(&te, &ts, &te);
121 for (i = 0; i < num_pipes; i++) {
131 main (int argc, char **argv)
141 num_writes = num_pipes;
142 while ((c = getopt(argc, argv, "n:a:w:")) != -1) {
145 num_pipes = atoi(optarg);
148 num_active = atoi(optarg);
151 num_writes = atoi(optarg);
154 fprintf(stderr, "Illegal argument \"%c\"\n", c);
159 rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
160 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
165 pipes = new int[num_pipes * 2];
167 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
169 if (pipe(cp) == -1) {
171 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
178 for (i = 0; i < 25; i++) {
181 fprintf(stdout, "%ld\n",
182 tv->sec() * 1000000L + tv->usec());