1 /* $NetBSD: bench.c,v 1.4 2016/01/08 21:35:41 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 #ifdef _WIN32 63 #include <getopt.h> 64 #endif 65 66 #include <event.h> 67 #include <evutil.h> 68 69 static int count, writes, fired, failures; 70 static evutil_socket_t *pipes; 71 static int num_pipes, num_active, num_writes; 72 static struct event *events; 73 74 75 static void 76 read_cb(evutil_socket_t fd, short which, void *arg) 77 { 78 ev_intptr_t idx = (ev_intptr_t) arg, widx = idx + 1; 79 u_char ch; 80 ev_ssize_t n; 81 82 n = recv(fd, (char*)&ch, sizeof(ch), 0); 83 if (n >= 0) 84 count += n; 85 else 86 failures++; 87 if (writes) { 88 if (widx >= num_pipes) 89 widx -= num_pipes; 90 n = send(pipes[2 * widx + 1], "e", 1, 0); 91 if (n != 1) 92 failures++; 93 writes--; 94 fired++; 95 } 96 } 97 98 static struct timeval * 99 run_once(void) 100 { 101 evutil_socket_t *cp, space; 102 long i; 103 static struct timeval ts, te; 104 105 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) { 106 if (event_initialized(&events[i])) 107 event_del(&events[i]); 108 event_set(&events[i], cp[0], EV_READ | EV_PERSIST, read_cb, (void *)(ev_intptr_t) i); 109 event_add(&events[i], NULL); 110 } 111 112 event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK); 113 114 fired = 0; 115 space = num_pipes / num_active; 116 space = space * 2; 117 for (i = 0; i < num_active; i++, fired++) 118 (void) send(pipes[i * space + 1], "e", 1, 0); 119 120 count = 0; 121 writes = num_writes; 122 { int xcount = 0; 123 evutil_gettimeofday(&ts, NULL); 124 do { 125 event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK); 126 xcount++; 127 } while (count != fired); 128 evutil_gettimeofday(&te, NULL); 129 130 if (xcount != count) fprintf(stderr, "Xcount: %d, Rcount: %d\n", xcount, count); 131 } 132 133 evutil_timersub(&te, &ts, &te); 134 135 return (&te); 136 } 137 138 int 139 main(int argc, char **argv) 140 { 141 #ifdef HAVE_SETRLIMIT 142 struct rlimit rl; 143 #endif 144 int i, c; 145 struct timeval *tv; 146 evutil_socket_t *cp; 147 148 #ifdef _WIN32 149 WSADATA WSAData; 150 WSAStartup(0x101, &WSAData); 151 #endif 152 num_pipes = 100; 153 num_active = 1; 154 num_writes = num_pipes; 155 while ((c = getopt(argc, argv, "n:a:w:")) != -1) { 156 switch (c) { 157 case 'n': 158 num_pipes = atoi(optarg); 159 break; 160 case 'a': 161 num_active = atoi(optarg); 162 break; 163 case 'w': 164 num_writes = atoi(optarg); 165 break; 166 default: 167 fprintf(stderr, "Illegal argument \"%c\"\n", c); 168 exit(1); 169 } 170 } 171 172 #ifdef HAVE_SETRLIMIT 173 rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50; 174 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) { 175 perror("setrlimit"); 176 exit(1); 177 } 178 #endif 179 180 events = calloc(num_pipes, sizeof(struct event)); 181 pipes = calloc(num_pipes * 2, sizeof(evutil_socket_t)); 182 if (events == NULL || pipes == NULL) { 183 perror("malloc"); 184 exit(1); 185 } 186 187 event_init(); 188 189 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) { 190 #ifdef USE_PIPES 191 if (pipe(cp) == -1) { 192 #else 193 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) { 194 #endif 195 perror("pipe"); 196 exit(1); 197 } 198 } 199 200 for (i = 0; i < 25; i++) { 201 tv = run_once(); 202 if (tv == NULL) 203 exit(1); 204 fprintf(stdout, "%ld\n", 205 tv->tv_sec * 1000000L + tv->tv_usec); 206 } 207 208 exit(0); 209 } 210