Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | /* * Copyright (c) 2006 Apple Inc. All Rights Reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * Order of Execution * * benchmark_init * * benchmark_optswitch * * benchmark_initrun * * benchmark_initworker * benchmark_initbatch * benchmark * benchmark_finibatch * benchmark_initbatch * benchmark * benchmark_finibatch, etc. * benchmark_finiworker * * benchmark_result * * benchmark_finirun * * benchmark_fini */ #ifdef __sun #pragma ident "@(#)socket.c 1.3 05/08/04 Apple Inc." #endif #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #include <string.h> #include <errno.h> #include "../libmicro.h" /* * lmbench routines, etc. brought over for this benchmark */ int open_file(void* tsd); void server(void* tsd); typedef int (*open_f)(void* tsd); /* * end of lmbench support routines */ /* * Your state variables should live in the tsd_t struct below */ typedef struct { char fname[L_tmpnam]; open_f fid_f; pid_t pid; int sock; int fid; int num; int max; fd_set set; } tsd_t; static int optt = 1; static int optn = -1; static int optp = 1; static int optw = 0; /* * lmbench routines, etc. brought over for this benchmark */ void morefds(void) { #ifdef RLIMIT_NOFILE struct rlimit r; getrlimit(RLIMIT_NOFILE, &r); r.rlim_cur = r.rlim_max; setrlimit(RLIMIT_NOFILE, &r); #endif } int open_file(void* tsd) { tsd_t* ts = (tsd_t*)tsd; //(void) fprintf(stderr, "open_file: ts->fname = %s\n",ts->fname); return (int) open(ts->fname, O_RDONLY); } void server(void* tsd) { int pid; tsd_t* ts = (tsd_t*)tsd; pid = getpid(); ts->pid = 0; //(void) fprintf(stderr, "server: state->fid_f = %i\n",ts->fid_f); if (ts->fid_f == open_file) { /* Create a temporary file for clients to open */ sprintf(ts->fname, "/tmp/lat_selectXXXXXX"); //(void) fprintf(stderr, "server: ts->fname = %s\n",ts->fname); ts->fid = mkstemp(ts->fname); //(void) fprintf(stderr, "server: ts->fname = %s: ts->fid = %d\n",ts->fname, ts->fid); if (ts->fid <= 0) { char buf[L_tmpnam+128]; sprintf(buf, "lat_select: Could not create temp file %s", ts->fname); perror(buf); exit(1); } close(ts->fid); return; } // // this is all for the tcp version of this test only // // /* Create a socket for clients to connect to */ // state->sock = tcp_server(TCP_SELECT, SOCKOPT_REUSE); // if (state->sock <= 0) { // perror("lat_select: Could not open tcp server socket"); // exit(1); // } /* Start a server process to accept client connections */ // switch(state->pid = fork()) { // case 0: // /* child server process */ // while (pid == getppid()) { // int newsock = tcp_accept(state->sock, SOCKOPT_NONE); // read(newsock, &state->fid, 1); // close(newsock); // } // exit(0); // case -1: // /* error */ // perror("lat_select::server(): fork() failed"); // exit(1); // default: // break; // } } /* * end of lmbench support routines */ /*ARGSUSED*/ int benchmark_initbatch(void *tsd) { /* * initialize your state variables here second */ return (0); } int benchmark_finirun() { //(void) fprintf(stderr, "benchmark_finirun\n"); return (0); } int benchmark_init() { //(void) fprintf(stderr, "benchmark_init\n"); /* * the lm_optstr must be defined here or no options for you * * ...and the framework will throw an error * */ (void) sprintf(lm_optstr, "p:w:n:t:"); /* * working hypothesis: * * tsd_t is the struct that we can pass around our * state info in * * lm_tsdsize will allocate the space we need for this * structure throughout the rest of the framework */ lm_tsdsize = sizeof (tsd_t); (void) sprintf(lm_usage, " [-p parallelism (default 1)]\n" " [-w warmup (default 0)]\n" " [-n number of descriptors (default 1)]\n" " [-t int (default 1)]\n" "notes: measures lmbench_select_file\n"); lm_defB = 1; return (0); } int benchmark_fini() { //(void) fprintf(stderr, "benchmark_fini\n"); return (0); } int benchmark_finibatch(void *tsd) { return (0); } char * benchmark_result() { static char result = '\0'; //(void) fprintf(stderr, "benchmark_result\n"); return (&result); } int benchmark_finiworker(void *tsd) { tsd_t *ts = (tsd_t *)tsd; int i; // pulls in the lmbench cleanup code //(void) fprintf(stderr, "benchmark_finiworker\n"); for (i = 0; i <= ts->max; ++i) { if (FD_ISSET(i, &(ts->set))) close(i); } FD_ZERO(&(ts->set)); unlink(ts->fname); return (0); } int benchmark_optswitch(int opt, char *optarg) { //(void) fprintf(stderr, "benchmark_optswitch\n"); switch (opt) { case 't': optt = sizetoint(optarg); break; case 'n': optn = sizetoint(optarg); break; case 'p': optp = sizetoint(optarg); break; case 'w': optw = sizetoint(optarg); break; default: return (-1); } return (0); } int benchmark_initworker(void *tsd) { // pulls in code from lmbench main and initialize int n = 0; /* * initialize your state variables here first */ tsd_t *ts = (tsd_t *)tsd; int N, fid, fd; /* * default number of file descriptors */ //(void) fprintf(stderr, "benchmark_initworker\n"); ts->num = 200; if (optn > 0) { ts->num = optn; } N = ts->num; //(void) fprintf(stderr, "benchmark_initworker ts->num is %i\n",ts->num); /* * grab more file descriptors */ morefds(); ts->fid_f = open_file; server(ts); //(void) fprintf(stderr, "benchmark_initworker: returned from server call\n"); /* * Initialize function from lmbench * for this test */ fid = (*ts->fid_f)(ts); //(void) fprintf(stderr, "initworker: fid is %i\n",fid); if (fid <= 0) { perror("Could not open device"); exit(1); } ts->max = 0; FD_ZERO(&(ts->set)); //(void) fprintf(stderr, "initworker FD_ZERO: ts->set result is %i\n",ts->set); //(void) fprintf(stderr, "initworker: N is %i\n",N); for (n = 0; n < N; n++) { //(void) fprintf(stderr, "benchmark_initworker: in the loop - N is %i: n is %i\n",N, n); fd = dup(fid); //(void) fprintf(stderr, "benchmark_initworker: dup result is %i\n",fd); //(void) fprintf(stderr, "benchmark_initworker: errno result is %d - \"%s\"\n",errno, strerror(errno)); if (fd == -1) break; if (fd > ts->max) ts->max = fd; FD_SET(fd, &(ts->set)); //(void) fprintf(stderr, "initworker FD_SET: ts->set result is %i\n",ts->set); } //(void) fprintf(stderr, "benchmark_initworker: after second macro/loop\n"); ts->max++; close(fid); //(void) fprintf(stderr, "benchmark_initworker: N is %i: n is %i\n",N, n); if (n != N) exit(1); /* end of initialize function */ //(void) fprintf(stderr, "benchmark_initworker: about to exit\n"); return (0); } int benchmark_initrun() { //(void) fprintf(stderr, "benchmark_initrun\n"); return (0); } int benchmark(void *tsd, result_t *res) { /* * initialize your state variables here last * * and realize that you are paying for your initialization here * and it is really a bad idea */ tsd_t *ts = (tsd_t *)tsd; fd_set nosave; static struct timeval tv; //(void) fprintf(stderr, "benchmark\n"); int i; //int sel_res; tv.tv_sec = 0; tv.tv_usec = 0; for (i = 0; i < lm_optB; i++) { nosave = ts->set; //(void) fprintf(stderr, "benchmark: nosave is %i\n", nosave); select(ts->num, 0, &nosave, 0, &tv); //(void) fprintf(stderr, "benchmark: select result is %i\n",sel_res); //(void) fprintf(stderr, "benchmark: errno result is %d - \"%s\"\n",errno, strerror(errno)); } res->re_count = i; return (0); } |