dmsg - Formalize most of the debugging output, cleanup, fix uninit bug
[dragonfly.git] / usr.sbin / spray / spray.c
blobdbea1287b6c21a254702550a8d65014ff7ba8b6c
1 /*
2 * Copyright (c) 1993 Winning Strategies, Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Winning Strategies, Inc.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/usr.sbin/spray/spray.c,v 1.5.2.2 2001/07/30 10:23:00 dd Exp $
33 #include <rpc/rpc.h>
34 #include <rpcsvc/spray.h>
36 #include <err.h>
37 #include <limits.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
42 #ifndef SPRAYOVERHEAD
43 #define SPRAYOVERHEAD 86
44 #endif
46 static void usage(void);
47 static void print_xferstats(unsigned int, int, double);
48 static int getnum(const char *);
50 /* spray buffer */
51 char spray_buffer[SPRAYMAX];
53 /* RPC timeouts */
54 struct timeval NO_DEFAULT = { -1, -1 };
55 struct timeval ONE_WAY = { 0, 0 };
56 struct timeval TIMEOUT = { 25, 0 };
58 int
59 main(int argc, char *argv[])
61 spraycumul host_stats;
62 sprayarr host_array;
63 CLIENT *cl;
64 int c;
65 u_int i;
66 u_int count = 0;
67 int delay = 0;
68 int length = 0;
69 double xmit_time; /* time to receive data */
71 while ((c = getopt(argc, argv, "c:d:l:")) != -1) {
72 switch (c) {
73 case 'c':
74 count = getnum(optarg);
75 break;
76 case 'd':
77 delay = getnum(optarg);
78 break;
79 case 'l':
80 length = getnum(optarg);
81 break;
82 default:
83 usage();
84 /* NOTREACHED */
87 argc -= optind;
88 argv += optind;
90 if (argc != 1) {
91 usage();
92 /* NOTREACHED */
96 /* Correct packet length. */
97 if (length > SPRAYMAX) {
98 length = SPRAYMAX;
99 } else if (length < SPRAYOVERHEAD) {
100 length = SPRAYOVERHEAD;
101 } else {
102 /* The RPC portion of the packet is a multiple of 32 bits. */
103 length -= SPRAYOVERHEAD - 3;
104 length &= ~3;
105 length += SPRAYOVERHEAD;
110 * The default value of count is the number of packets required
111 * to make the total stream size 100000 bytes.
113 if (!count) {
114 count = 100000 / length;
117 /* Initialize spray argument */
118 host_array.sprayarr_len = length - SPRAYOVERHEAD;
119 host_array.sprayarr_val = spray_buffer;
122 /* create connection with server */
123 if ((cl = clnt_create(*argv, SPRAYPROG, SPRAYVERS, "udp")) == NULL) {
124 clnt_pcreateerror(getprogname());
125 exit(1);
129 * For some strange reason, RPC 4.0 sets the default timeout,
130 * thus timeouts specified in clnt_call() are always ignored.
132 * The following (undocumented) hack resets the internal state
133 * of the client handle.
135 clnt_control(cl, CLSET_TIMEOUT, (caddr_t)&NO_DEFAULT);
138 /* Clear server statistics */
139 if (clnt_call(cl, SPRAYPROC_CLEAR, (xdrproc_t)xdr_void, NULL,
140 (xdrproc_t)xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
141 clnt_perror(cl, getprogname());
142 exit(1);
145 /* Spray server with packets */
146 printf ("sending %u packets of lnth %d to %s ...", count, length,
147 *argv);
148 fflush (stdout);
150 for (i = 0; i < count; i++) {
151 clnt_call(cl, SPRAYPROC_SPRAY, (xdrproc_t)xdr_sprayarr,
152 &host_array, (xdrproc_t)xdr_void, NULL, ONE_WAY);
154 if (delay) {
155 usleep(delay);
160 /* Collect statistics from server */
161 if (clnt_call(cl, SPRAYPROC_GET, (xdrproc_t)xdr_void, NULL,
162 (xdrproc_t)xdr_spraycumul, &host_stats, TIMEOUT) != RPC_SUCCESS) {
163 clnt_perror(cl, getprogname());
164 exit(1);
167 xmit_time = host_stats.clock.sec +
168 (host_stats.clock.usec / 1000000.0);
170 printf ("\n\tin %.2f seconds elapsed time\n", xmit_time);
173 /* report dropped packets */
174 if (host_stats.counter != count) {
175 int packets_dropped = count - host_stats.counter;
177 printf("\t%d packets (%.2f%%) dropped\n",
178 packets_dropped,
179 100.0 * packets_dropped / count );
180 } else {
181 printf("\tno packets dropped\n");
184 printf("Sent:");
185 print_xferstats(count, length, xmit_time);
187 printf("Rcvd:");
188 print_xferstats(host_stats.counter, length, xmit_time);
190 clnt_destroy(cl);
191 exit (0);
195 static void
196 print_xferstats(u_int packets, int packetlen, double xfertime)
198 int datalen;
199 double pps; /* packets per second */
200 double bps; /* bytes per second */
202 datalen = packets * packetlen;
203 pps = packets / xfertime;
204 bps = datalen / xfertime;
206 printf("\t%.0f packets/sec, ", pps);
208 if (bps >= 1024)
209 printf ("%.1fK ", bps / 1024);
210 else
211 printf ("%.0f ", bps);
213 printf("bytes/sec\n");
216 static int
217 getnum(const char *arg)
219 char *ep;
220 long tmp;
222 tmp = strtol(arg, &ep, 10);
223 if (*ep != '\0' || tmp < INT_MIN || tmp > INT_MAX)
224 errx(1, "invalid value: %s", arg);
226 return((int)tmp);
229 static void
230 usage(void)
232 fprintf(stderr,
233 "usage: spray [-c count] [-l length] [-d delay] host\n");
234 exit(1);