Austin Schuh | a273376 | 2015-09-06 17:46:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2003-2007 Niels Provos <provos@citi.umich.edu> |
| 3 | * Copyright 2007-2012 Niels Provos and Nick Mathewson |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 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. |
| 15 | * |
| 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. |
| 26 | * |
| 27 | * |
| 28 | * Mon 03/10/2003 - Modified by Davide Libenzi <davidel@xmailserver.org> |
| 29 | * |
| 30 | * Added chain event propagation to improve the sensitivity of |
| 31 | * the measure respect to the event loop efficency. |
| 32 | * |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | #include "event2/event-config.h" |
| 37 | |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/stat.h> |
| 40 | #ifdef _EVENT_HAVE_SYS_TIME_H |
| 41 | #include <sys/time.h> |
| 42 | #endif |
| 43 | #ifdef WIN32 |
| 44 | #define WIN32_LEAN_AND_MEAN |
| 45 | #include <windows.h> |
| 46 | #else |
| 47 | #include <sys/socket.h> |
| 48 | #include <signal.h> |
| 49 | #include <sys/resource.h> |
| 50 | #endif |
| 51 | #include <fcntl.h> |
| 52 | #include <stdlib.h> |
| 53 | #include <stdio.h> |
| 54 | #include <string.h> |
| 55 | #ifdef _EVENT_HAVE_UNISTD_H |
| 56 | #include <unistd.h> |
| 57 | #endif |
| 58 | #include <errno.h> |
| 59 | |
| 60 | #include <event.h> |
| 61 | #include <evutil.h> |
| 62 | |
| 63 | static int count, writes, fired; |
| 64 | static evutil_socket_t *pipes; |
| 65 | static int num_pipes, num_active, num_writes; |
| 66 | static struct event *events; |
| 67 | |
| 68 | |
| 69 | static void |
| 70 | read_cb(evutil_socket_t fd, short which, void *arg) |
| 71 | { |
| 72 | ev_intptr_t idx = (ev_intptr_t) arg, widx = idx + 1; |
| 73 | u_char ch; |
| 74 | |
| 75 | count += recv(fd, (char*)&ch, sizeof(ch), 0); |
| 76 | if (writes) { |
| 77 | if (widx >= num_pipes) |
| 78 | widx -= num_pipes; |
| 79 | send(pipes[2 * widx + 1], "e", 1, 0); |
| 80 | writes--; |
| 81 | fired++; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static struct timeval * |
| 86 | run_once(void) |
| 87 | { |
| 88 | evutil_socket_t *cp, space; |
| 89 | long i; |
| 90 | static struct timeval ts, te; |
| 91 | |
| 92 | for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) { |
| 93 | if (event_initialized(&events[i])) |
| 94 | event_del(&events[i]); |
| 95 | event_set(&events[i], cp[0], EV_READ | EV_PERSIST, read_cb, (void *)(ev_intptr_t) i); |
| 96 | event_add(&events[i], NULL); |
| 97 | } |
| 98 | |
| 99 | event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK); |
| 100 | |
| 101 | fired = 0; |
| 102 | space = num_pipes / num_active; |
| 103 | space = space * 2; |
| 104 | for (i = 0; i < num_active; i++, fired++) |
| 105 | send(pipes[i * space + 1], "e", 1, 0); |
| 106 | |
| 107 | count = 0; |
| 108 | writes = num_writes; |
| 109 | { int xcount = 0; |
| 110 | evutil_gettimeofday(&ts, NULL); |
| 111 | do { |
| 112 | event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK); |
| 113 | xcount++; |
| 114 | } while (count != fired); |
| 115 | evutil_gettimeofday(&te, NULL); |
| 116 | |
| 117 | if (xcount != count) fprintf(stderr, "Xcount: %d, Rcount: %d\n", xcount, count); |
| 118 | } |
| 119 | |
| 120 | evutil_timersub(&te, &ts, &te); |
| 121 | |
| 122 | return (&te); |
| 123 | } |
| 124 | |
| 125 | int |
| 126 | main(int argc, char **argv) |
| 127 | { |
| 128 | #ifndef WIN32 |
| 129 | struct rlimit rl; |
| 130 | #endif |
| 131 | int i, c; |
| 132 | struct timeval *tv; |
| 133 | evutil_socket_t *cp; |
| 134 | |
| 135 | #ifdef WIN32 |
| 136 | WSADATA WSAData; |
| 137 | WSAStartup(0x101, &WSAData); |
| 138 | #endif |
| 139 | num_pipes = 100; |
| 140 | num_active = 1; |
| 141 | num_writes = num_pipes; |
| 142 | while ((c = getopt(argc, argv, "n:a:w:")) != -1) { |
| 143 | switch (c) { |
| 144 | case 'n': |
| 145 | num_pipes = atoi(optarg); |
| 146 | break; |
| 147 | case 'a': |
| 148 | num_active = atoi(optarg); |
| 149 | break; |
| 150 | case 'w': |
| 151 | num_writes = atoi(optarg); |
| 152 | break; |
| 153 | default: |
| 154 | fprintf(stderr, "Illegal argument \"%c\"\n", c); |
| 155 | exit(1); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #ifndef WIN32 |
| 160 | rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50; |
| 161 | if (setrlimit(RLIMIT_NOFILE, &rl) == -1) { |
| 162 | perror("setrlimit"); |
| 163 | exit(1); |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | events = calloc(num_pipes, sizeof(struct event)); |
| 168 | pipes = calloc(num_pipes * 2, sizeof(evutil_socket_t)); |
| 169 | if (events == NULL || pipes == NULL) { |
| 170 | perror("malloc"); |
| 171 | exit(1); |
| 172 | } |
| 173 | |
| 174 | event_init(); |
| 175 | |
| 176 | for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) { |
| 177 | #ifdef USE_PIPES |
| 178 | if (pipe(cp) == -1) { |
| 179 | #else |
| 180 | if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) { |
| 181 | #endif |
| 182 | perror("pipe"); |
| 183 | exit(1); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | for (i = 0; i < 25; i++) { |
| 188 | tv = run_once(); |
| 189 | if (tv == NULL) |
| 190 | exit(1); |
| 191 | fprintf(stdout, "%ld\n", |
| 192 | tv->tv_sec * 1000000L + tv->tv_usec); |
| 193 | } |
| 194 | |
| 195 | exit(0); |
| 196 | } |