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.
40 #include <sys/types.h>
46 #include <sys/socket.h>
48 #include <sys/resource.h>
61 static int count
, writes
, fired
;
63 static int num_pipes
, num_active
, num_writes
;
64 static struct event
*events
;
67 read_cb(int fd
, short which
, void *arg
)
69 long idx
= (long) arg
, widx
= idx
+ 1;
72 count
+= read(fd
, &ch
, sizeof(ch
));
74 if (widx
>= num_pipes
)
76 write(pipes
[2 * widx
+ 1], "e", 1);
82 static struct timeval
*
87 static struct timeval ts
, te
;
89 for (cp
= pipes
, i
= 0; i
< num_pipes
; i
++, cp
+= 2) {
90 event_del(&events
[i
]);
91 event_set(&events
[i
], cp
[0], EV_READ
| EV_PERSIST
, read_cb
, (void *) i
);
92 event_add(&events
[i
], NULL
);
95 event_loop(EVLOOP_ONCE
| EVLOOP_NONBLOCK
);
98 space
= num_pipes
/ num_active
;
100 for (i
= 0; i
< num_active
; i
++, fired
++)
101 write(pipes
[i
* space
+ 1], "e", 1);
106 gettimeofday(&ts
, NULL
);
108 event_loop(EVLOOP_ONCE
| EVLOOP_NONBLOCK
);
110 } while (count
!= fired
);
111 gettimeofday(&te
, NULL
);
113 if (xcount
!= count
) fprintf(stderr
, "Xcount: %d, Rcount: %d\n", xcount
, count
);
116 evutil_timersub(&te
, &ts
, &te
);
122 main (int argc
, char **argv
)
133 num_writes
= num_pipes
;
134 while ((c
= getopt(argc
, argv
, "n:a:w:")) != -1) {
137 num_pipes
= atoi(optarg
);
140 num_active
= atoi(optarg
);
143 num_writes
= atoi(optarg
);
146 fprintf(stderr
, "Illegal argument \"%c\"\n", c
);
152 rl
.rlim_cur
= rl
.rlim_max
= num_pipes
* 2 + 50;
153 if (setrlimit(RLIMIT_NOFILE
, &rl
) == -1) {
159 events
= calloc(num_pipes
, sizeof(struct event
));
160 pipes
= calloc(num_pipes
* 2, sizeof(int));
161 if (events
== NULL
|| pipes
== NULL
) {
168 for (cp
= pipes
, i
= 0; i
< num_pipes
; i
++, cp
+= 2) {
170 if (pipe(cp
) == -1) {
172 if (evutil_socketpair(AF_UNIX
, SOCK_STREAM
, 0, cp
) == -1) {
179 for (i
= 0; i
< 25; i
++) {
183 fprintf(stdout
, "%ld\n",
184 tv
->tv_sec
* 1000000L + tv
->tv_usec
);