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