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