Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2003-2007 Niels Provos <provos@citi.umich.edu> |
michael@0 | 3 | * Copyright 2007-2012 Niels Provos and Nick Mathewson |
michael@0 | 4 | * |
michael@0 | 5 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | * modification, are permitted provided that the following conditions |
michael@0 | 7 | * are met: |
michael@0 | 8 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 9 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 11 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 12 | * documentation and/or other materials provided with the distribution. |
michael@0 | 13 | * 4. The name of the author may not be used to endorse or promote products |
michael@0 | 14 | * derived from this software without specific prior written permission. |
michael@0 | 15 | * |
michael@0 | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
michael@0 | 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
michael@0 | 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@0 | 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
michael@0 | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
michael@0 | 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
michael@0 | 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | * |
michael@0 | 27 | * |
michael@0 | 28 | * Mon 03/10/2003 - Modified by Davide Libenzi <davidel@xmailserver.org> |
michael@0 | 29 | * |
michael@0 | 30 | * Added chain event propagation to improve the sensitivity of |
michael@0 | 31 | * the measure respect to the event loop efficency. |
michael@0 | 32 | * |
michael@0 | 33 | * |
michael@0 | 34 | */ |
michael@0 | 35 | |
michael@0 | 36 | #include "event2/event-config.h" |
michael@0 | 37 | |
michael@0 | 38 | #include <sys/types.h> |
michael@0 | 39 | #include <sys/stat.h> |
michael@0 | 40 | #ifdef _EVENT_HAVE_SYS_TIME_H |
michael@0 | 41 | #include <sys/time.h> |
michael@0 | 42 | #endif |
michael@0 | 43 | #ifdef WIN32 |
michael@0 | 44 | #define WIN32_LEAN_AND_MEAN |
michael@0 | 45 | #include <windows.h> |
michael@0 | 46 | #else |
michael@0 | 47 | #include <sys/socket.h> |
michael@0 | 48 | #include <signal.h> |
michael@0 | 49 | #include <sys/resource.h> |
michael@0 | 50 | #endif |
michael@0 | 51 | #include <fcntl.h> |
michael@0 | 52 | #include <stdlib.h> |
michael@0 | 53 | #include <stdio.h> |
michael@0 | 54 | #include <string.h> |
michael@0 | 55 | #ifdef _EVENT_HAVE_UNISTD_H |
michael@0 | 56 | #include <unistd.h> |
michael@0 | 57 | #endif |
michael@0 | 58 | #include <errno.h> |
michael@0 | 59 | |
michael@0 | 60 | #include <event.h> |
michael@0 | 61 | #include <evutil.h> |
michael@0 | 62 | |
michael@0 | 63 | static int count, writes, fired; |
michael@0 | 64 | static evutil_socket_t *pipes; |
michael@0 | 65 | static int num_pipes, num_active, num_writes; |
michael@0 | 66 | static struct event *events; |
michael@0 | 67 | |
michael@0 | 68 | |
michael@0 | 69 | static void |
michael@0 | 70 | read_cb(evutil_socket_t fd, short which, void *arg) |
michael@0 | 71 | { |
michael@0 | 72 | ev_intptr_t idx = (ev_intptr_t) arg, widx = idx + 1; |
michael@0 | 73 | u_char ch; |
michael@0 | 74 | |
michael@0 | 75 | count += recv(fd, (char*)&ch, sizeof(ch), 0); |
michael@0 | 76 | if (writes) { |
michael@0 | 77 | if (widx >= num_pipes) |
michael@0 | 78 | widx -= num_pipes; |
michael@0 | 79 | send(pipes[2 * widx + 1], "e", 1, 0); |
michael@0 | 80 | writes--; |
michael@0 | 81 | fired++; |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | static struct timeval * |
michael@0 | 86 | run_once(void) |
michael@0 | 87 | { |
michael@0 | 88 | evutil_socket_t *cp, space; |
michael@0 | 89 | long i; |
michael@0 | 90 | static struct timeval ts, te; |
michael@0 | 91 | |
michael@0 | 92 | for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) { |
michael@0 | 93 | if (event_initialized(&events[i])) |
michael@0 | 94 | event_del(&events[i]); |
michael@0 | 95 | event_set(&events[i], cp[0], EV_READ | EV_PERSIST, read_cb, (void *)(ev_intptr_t) i); |
michael@0 | 96 | event_add(&events[i], NULL); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK); |
michael@0 | 100 | |
michael@0 | 101 | fired = 0; |
michael@0 | 102 | space = num_pipes / num_active; |
michael@0 | 103 | space = space * 2; |
michael@0 | 104 | for (i = 0; i < num_active; i++, fired++) |
michael@0 | 105 | send(pipes[i * space + 1], "e", 1, 0); |
michael@0 | 106 | |
michael@0 | 107 | count = 0; |
michael@0 | 108 | writes = num_writes; |
michael@0 | 109 | { int xcount = 0; |
michael@0 | 110 | evutil_gettimeofday(&ts, NULL); |
michael@0 | 111 | do { |
michael@0 | 112 | event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK); |
michael@0 | 113 | xcount++; |
michael@0 | 114 | } while (count != fired); |
michael@0 | 115 | evutil_gettimeofday(&te, NULL); |
michael@0 | 116 | |
michael@0 | 117 | if (xcount != count) fprintf(stderr, "Xcount: %d, Rcount: %d\n", xcount, count); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | evutil_timersub(&te, &ts, &te); |
michael@0 | 121 | |
michael@0 | 122 | return (&te); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | int |
michael@0 | 126 | main(int argc, char **argv) |
michael@0 | 127 | { |
michael@0 | 128 | #ifndef WIN32 |
michael@0 | 129 | struct rlimit rl; |
michael@0 | 130 | #endif |
michael@0 | 131 | int i, c; |
michael@0 | 132 | struct timeval *tv; |
michael@0 | 133 | evutil_socket_t *cp; |
michael@0 | 134 | |
michael@0 | 135 | #ifdef WIN32 |
michael@0 | 136 | WSADATA WSAData; |
michael@0 | 137 | WSAStartup(0x101, &WSAData); |
michael@0 | 138 | #endif |
michael@0 | 139 | num_pipes = 100; |
michael@0 | 140 | num_active = 1; |
michael@0 | 141 | num_writes = num_pipes; |
michael@0 | 142 | while ((c = getopt(argc, argv, "n:a:w:")) != -1) { |
michael@0 | 143 | switch (c) { |
michael@0 | 144 | case 'n': |
michael@0 | 145 | num_pipes = atoi(optarg); |
michael@0 | 146 | break; |
michael@0 | 147 | case 'a': |
michael@0 | 148 | num_active = atoi(optarg); |
michael@0 | 149 | break; |
michael@0 | 150 | case 'w': |
michael@0 | 151 | num_writes = atoi(optarg); |
michael@0 | 152 | break; |
michael@0 | 153 | default: |
michael@0 | 154 | fprintf(stderr, "Illegal argument \"%c\"\n", c); |
michael@0 | 155 | exit(1); |
michael@0 | 156 | } |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | #ifndef WIN32 |
michael@0 | 160 | rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50; |
michael@0 | 161 | if (setrlimit(RLIMIT_NOFILE, &rl) == -1) { |
michael@0 | 162 | perror("setrlimit"); |
michael@0 | 163 | exit(1); |
michael@0 | 164 | } |
michael@0 | 165 | #endif |
michael@0 | 166 | |
michael@0 | 167 | events = calloc(num_pipes, sizeof(struct event)); |
michael@0 | 168 | pipes = calloc(num_pipes * 2, sizeof(evutil_socket_t)); |
michael@0 | 169 | if (events == NULL || pipes == NULL) { |
michael@0 | 170 | perror("malloc"); |
michael@0 | 171 | exit(1); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | event_init(); |
michael@0 | 175 | |
michael@0 | 176 | for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) { |
michael@0 | 177 | #ifdef USE_PIPES |
michael@0 | 178 | if (pipe(cp) == -1) { |
michael@0 | 179 | #else |
michael@0 | 180 | if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) { |
michael@0 | 181 | #endif |
michael@0 | 182 | perror("pipe"); |
michael@0 | 183 | exit(1); |
michael@0 | 184 | } |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | for (i = 0; i < 25; i++) { |
michael@0 | 188 | tv = run_once(); |
michael@0 | 189 | if (tv == NULL) |
michael@0 | 190 | exit(1); |
michael@0 | 191 | fprintf(stdout, "%ld\n", |
michael@0 | 192 | tv->tv_sec * 1000000L + tv->tv_usec); |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | exit(0); |
michael@0 | 196 | } |