0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms
0N/A * of the Common Development and Distribution License
0N/A * (the "License"). You may not use this file except
0N/A * in compliance with the License.
0N/A *
0N/A * You can obtain a copy of the license at
0N/A * src/OPENSOLARIS.LICENSE
0N/A * or http://www.opensolaris.org/os/licensing.
0N/A * See the License for the specific language governing
0N/A * permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL
0N/A * HEADER in each file and include the License file at
0N/A * usr/src/OPENSOLARIS.LICENSE. If applicable,
0N/A * add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your
0N/A * own identifying information: Portions Copyright [yyyy]
0N/A * [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
0N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/A
0N/A#define MAX(x, y) ((x) > (y) ? (x) : (y))
0N/A#define MIN(x, y) ((x) > (y) ? (y) : (x))
0N/A
0N/A#include <unistd.h>
0N/A#include <stdlib.h>
0N/A#include <stdio.h>
0N/A#include <string.h>
0N/A#include <poll.h>
0N/A#include <sys/socket.h>
0N/A
0N/A#include "libmicro.h"
0N/A
0N/A#define DEFN 256
0N/A
0N/Astatic int optn = DEFN;
0N/Astatic int optr = 0;
0N/Astatic int optw = 0;
0N/Astatic int optx = 0;
0N/Astatic int *fds;
0N/Astatic int target = 0;
0N/A
0N/Atypedef struct pollfd pfd_t;
0N/A
0N/Atypedef struct {
0N/A int ts_once;
0N/A pfd_t *ts_pfds;
0N/A} tsd_t;
0N/A
0N/Aint
0N/Abenchmark_init()
0N/A{
0N/A lm_tsdsize = sizeof (tsd_t);
0N/A
0N/A (void) sprintf(lm_optstr, "n:r:w:x");
0N/A
0N/A (void) sprintf(lm_usage,
0N/A " [-n fds-per-thread (default %d)]\n"
0N/A " [-r readable-fds (default 0)]\n"
0N/A " [-w writeable-fds (default 0)]\n"
0N/A " [-x] (start -r option with highest fd first; "
0N/A "default is lowest first)\n"
0N/A "notes: measures poll()\n",
0N/A DEFN);
0N/A
0N/A (void) sprintf(lm_header, "%8s %5s", "nfds", "flags");
0N/A
0N/A return (0);
0N/A}
0N/A
0N/Aint
0N/Abenchmark_optswitch(int opt, char *optarg)
0N/A{
0N/A switch (opt) {
0N/A case 'n':
0N/A optn = atoi(optarg);
0N/A break;
0N/A case 'r':
0N/A optr = atoi(optarg);
0N/A break;
0N/A case 'w':
0N/A optw = atoi(optarg);
0N/A break;
0N/A case 'x':
0N/A optx = 1;
0N/A break;
0N/A default:
0N/A return (-1);
0N/A }
0N/A return (0);
0N/A}
0N/A
0N/Aint
0N/Abenchmark_initrun()
0N/A{
0N/A int i;
0N/A int j;
0N/A int pair[2];
0N/A
0N/A if (optn % 2 != 0) {
0N/A (void) printf("ERROR: -n value must be even\n");
0N/A optn = optr = optw = 0;
0N/A return (-1);
0N/A }
0N/A
0N/A if (optn < 0 || optr < 0 || optw < 0) {
0N/A (void) printf("ERROR: -n, -r and -w values must be > 0\n");
0N/A optn = optr = optw = 0;
0N/A return (-1);
0N/A }
0N/A
0N/A if (optr > optn || optw > optn) {
0N/A (void) printf("ERROR: -r and -w values must be <= maxfd\n");
0N/A optn = optr = optw = 0;
0N/A return (-1);
0N/A }
0N/A
0N/A fds = (int *)malloc(optn * sizeof (int));
0N/A if (fds == NULL) {
0N/A (void) printf("ERROR: malloc() failed\n");
0N/A optn = optr = optw = 0;
0N/A return (-1);
0N/A }
0N/A
0N/A (void) setfdlimit(optn + 10);
0N/A
0N/A
0N/A for (i = 0; i < optn; i += 2) {
0N/A if (socketpair(PF_UNIX, SOCK_STREAM, 0, pair) == -1) {
0N/A (void) printf("ERROR: socketpair() failed\n");
0N/A return (-1);
0N/A }
0N/A
0N/A fds[i] = MIN(pair[0], pair[1]);
0N/A fds[i+1] = MAX(pair[0], pair[1]);
0N/A }
0N/A
0N/A if (optx) {
0N/A target = MIN(optr + optw, optn);
0N/A for (i = 0, j = optn - 1; i < optr; i++, j--) {
0N/A (void) write(fds[j+1 - (2*(j%2))], "", 1);
0N/A }
0N/A } else {
0N/A target = MAX(optr, optw);
0N/A for (i = 0; i < optr; i++) {
0N/A (void) write(fds[i+1 - (2*(i%2))], "", 1);
0N/A }
0N/A }
0N/A
0N/A return (0);
0N/A}
0N/A
0N/Aint
0N/Abenchmark_initbatch(void *tsd)
0N/A{
0N/A tsd_t *ts = (tsd_t *)tsd;
0N/A int i;
0N/A int errors = 0;
0N/A
0N/A if (ts->ts_once++ == 0) {
0N/A ts->ts_pfds = (pfd_t *)malloc(optn * sizeof (pfd_t));
0N/A if (ts->ts_pfds == NULL) {
0N/A errors++;
0N/A }
0N/A
0N/A for (i = 0; i < optn; i++) {
0N/A ts->ts_pfds[i].fd = fds[i];
0N/A ts->ts_pfds[i].events = POLLIN;
0N/A }
0N/A
0N/A for (i = 0; i < optw; i++) {
0N/A ts->ts_pfds[i].events |= POLLOUT;
0N/A }
0N/A }
0N/A
0N/A return (errors);
0N/A}
0N/A
0N/Aint
0N/Abenchmark(void *tsd, result_t *res)
0N/A{
0N/A tsd_t *ts = (tsd_t *)tsd;
0N/A int i;
0N/A
0N/A for (i = 0; i < lm_optB; i++) {
0N/A if (poll(ts->ts_pfds, optn, 0) != target) {
0N/A res->re_errors++;
0N/A }
0N/A }
0N/A res->re_count = i;
0N/A
0N/A return (0);
0N/A}
0N/A
0N/Achar *
0N/Abenchmark_result()
0N/A{
0N/A static char result[256];
0N/A char flags[4];
0N/A
0N/A flags[0] = optr ? 'r' : '-';
0N/A flags[1] = optw ? 'w' : '-';
0N/A flags[2] = optx ? 'x' : '-';
0N/A flags[3] = 0;
0N/A
0N/A (void) sprintf(result, "%8d %5s", optn, flags);
0N/A
0N/A return (result);
0N/A}