4 * Test TCP connection. Makes a connection on port 5001
5 * and transfers fabricated buffers or data copied from stdin.
7 * Usable on 4.2, 4.3, and 4.1a systems by defining one of
9 * Machines using System V with BSD sockets should define SYSV.
11 * Modified for operation under 4.2BSD, 18 Dec 84
13 * Minor improvements, Mike Muuss and Terry Slattery, 16-Oct-85.
14 * Modified in 1989 at Silicon Graphics, Inc.
15 * catch SIGPIPE to be able to print stats when receiver has died
16 * for tcp, don't look for sentinel during reads to allow small transfers
17 * increased default buffer size to 8K, nbuf to 2K to transfer 16MB
18 * moved default port to 5001, beyond IPPORT_USERRESERVED
19 * make sinkmode default because it is more popular,
20 * -s now means don't sink/source
21 * count number of read/write system calls to see effects of
22 * blocking from full socket buffers
23 * for tcp, -D option turns off buffered writes (sets TCP_NODELAY sockopt)
24 * buffer alignment options, -A and -O
25 * print stats in a format that's a bit easier to use with grep & awk
26 * for SYSV, mimic BSD routines to use most of the existing timing code
27 * Modified by Steve Miller of the University of Maryland, College Park
28 * -b sets the socket buffer size (SO_SNDBUF/SO_RCVBUF)
29 * Modified Sept. 1989 at Silicon Graphics, Inc.
30 * restored -s sense at request of tcs@brl
31 * Modified Oct. 1991 at Silicon Graphics, Inc.
32 * use getopt(3) for option processing, add -f and -T options.
33 * SGI IRIX 3.3 and 4.0 releases don't need #define SYSV.
35 * Modified --> Nov 1996 at CERN (Daniel DAVIDS)
36 * printout of the Socket-Buffer-Sizes
37 * configured for HP-UX 9000 OS
38 * configured for Windows NT OS
39 * Modified Dec 1996 at CERN (Jacques ROCHEZ)
41 * addaptation to the gcc compiler (ANSI)
42 * configured for Lynx OS
43 * automatic format for the rate display (G/M/K/)bytes/sec
44 * added log (L) and more help (h) options.
45 * Modified May 1997 at CERN (Jacques ROCHEZ)
46 * removed the mes() function following err() function.
47 * changed the default port to 5010
48 * Modified jul 1997 at CERN (Jacques ROCHEZ)
49 * adapted the timing calculation in microseconds
50 * addapted the code for Vsisual C++ under NT4.0
51 * Modified aug 1997 at CERN (Jacques ROCHEZ)
52 * initialise to 0 the variables nbytes, numcalls
53 * moved the buffer pre-load outside the measured timed area
54 * Distribution Status -
55 * Public Domain. Distribution Unlimited.
56 * Modified jun 1998 at Epigram
57 * - print hash marks to indicate prograss on transmitting side
58 * - sleep between bursts of buffers
59 * - set number of buffers/burst
60 * Modified jul 1998 at Epigram
61 * - send random size buffers
62 * Modified jan 1999 at Epigram (WES)
63 * - added start and end patterns to UDP start and end packets
64 * - added handling of missed start, end, or data packets for UDP tests
67 static char RCSid[] = "ttcp.c $- CERN Revision: 3.8 (dev level) -$";
70 static char VersDate[] = "Epigram 15-jul-98";
72 /* system dependent setting
73 * ------------------------
74 * uname -a,gcc -v a.c are the tools used
76 * Platform/OS #define MACRO predefined
77 * ----------- ------- ---------------------------------------------------
78 * SunOS OS BSD43 __unix__ __sun__ __sparc__
79 * SUN Solaris SYSV __unix__ __sun__ __sparc__ __svr4__
80 * SGI-IRIX < 3.3 SYSV set as #define sgi
81 * HP-UX 9000 SYSV __unix__ __hpux__ __hp9k8__
82 * OSF/1 V3.2 SYSV __unix__ __osf__ __alpha__
83 * OSF/1 V4.0 SYSV __unix__ __osf__ __alpha__ _CFE
84 * LynxOS SYSV __unix__ __lynx__ __powerpc__
85 * Windows NT SYSV __WINNT__ __i386__ __M_COFF__
86 * AIX SYSV _AIX _AIX32 _POWER _IBMR2 _ARCH_PWR
89 * Micosoft Visual C++ compiler under WindowNT 4.0
90 * Windows NT _WINDOWS WIN32
96 * Machines using System V with BSD sockets should define SYSV.
100 * LynxOS : gcc -c ttcp.c -o ttcp.o | gcc -o ttcp -O ttcp.o -lnetinet -lc_p -lc
103 /* -------------attempt to set an automatic UNIX OS family detection -------*/
105 #if defined(__hp9k8__) || defined(__osf__) || defined(__srv4__)
108 #if defined(__lynx__)
111 /* for solaris (__srv4__) the parameters SYSV is already set */
113 /* version A.09.01 'key words' differs from A.09.05 A */
118 #if defined(__sun__)&&!defined(__srv4__)
119 #define BSD43 /* changed by BSD42 if necessary */
122 #if defined(_WIN32) /* Window NT 4.0 compiled with VisualC++ 4 */
127 #if defined(__FreeBSD__)
131 #if defined(__linux__)
134 /*--------------------------------------------------------------------------*/
136 #if !defined(UNDER_CE)
140 #include <sys/types.h>
146 #include <sys/socket.h>
147 #include <netinet/in.h>
148 #include <netinet/tcp.h>
149 #include <arpa/inet.h>
151 #include <sys/time.h> /* struct timeval */
152 #include <sys/resource.h> /* definition of struct rusage */
154 #else /* else of __osf__ */
155 #if defined(__NTVIS__)
156 #include <winsock2.h>
157 #include <windows.h> /* required for all Windows applications */
158 #include <ws2tcpip.h>
161 #if !defined(UNDER_CE)
162 #include <sys\timeb.h>
167 struct rusage { struct timeval ru_utime, ru_stime; };
168 #define RUSAGE_SELF 0
169 #else /* else of __NTVIS__ */
170 #if defined(__lynx__)
171 #include <socket.h> /* located in /usr/include/..... */
172 #include <netinet/in.h>
173 #include <netinet/tcp.h>
174 #include <arpa/inet.h>
177 #include <resource.h> /* definition of struct rusage */
178 #include <sys/times.h>
179 #define RUSAGE_SELF 0
180 #include <conf.h> /* definition of TICKSPERSEC (HZ) */
181 #include <sys/param.h>
183 #else /* else of __Lynx__ */
184 #if defined(__svr4__)
185 #include <sys/socket.h>
187 #include <netinet/in.h>
188 #include <netinet/tcp.h>
189 #include <arpa/inet.h>
191 #include <sys/time.h> /* struct timeval */
192 #include <sys/resource.h> /* definition of struct rusage */
193 #include <sys/times.h>
194 #define RUSAGE_SELF 0
195 #include <sys/param.h>
197 #else /* else of __svr4__ all SYSV cases except those mentionned before */
198 #include <sys/socket.h>
199 #include <netinet/in.h>
200 #include <netinet/tcp.h>
201 #include <arpa/inet.h>
203 #include <sys/time.h> /* struct timeval */
204 #include <sys/resource.h> /* definition of struct rusage */
205 #include <sys/times.h>
206 #define RUSAGE_SELF 0
207 #include <sys/param.h>
209 #endif /* __svr4__ */
210 #endif /* __lynx__ */
211 #endif /* __NTVIS__ */
214 #else /* else of SYSV it is a BSD OS */
217 #include <sys/socket.h>
218 #include <netinet/in.h>
219 #include <netinet/tcp.h>
220 #include <arpa/inet.h>
222 #if defined(__linux__)
223 #include <time.h> /* struct timeval */
225 #include <sys/time.h> /* struct timeval */
227 #include <sys/resource.h> /* definition of struct rusage */
228 #include <unistd.h> /* for usleep() - henry */
229 #define SOCKET_ERROR -1
233 #if defined(__NTVIS__)
235 #if defined(_DEBUG) /* usual debug symbol for VC++ */
239 void usleep(unsigned int microseconds);
241 #define bcopy(a,b,n) memcpy((b), (a), (n))
242 #define bzero(a,n) memset((a), 0, (n))
246 int getopt(int argc, char **argv, char *optstring);
248 extern char *optarg; // returned arg to go with this option
249 extern int optind; // index to next argv element to process
250 extern int opterr; // should error messages be printed?
251 extern int optopt; //
256 /* get option letter from argument vector */
258 opterr = 1, // should error messages be printed?
259 optind = 1, // index into parent argv vector
260 optopt; // character checked for validity
261 char *optarg; // argument associated with option
264 char *progname; // may also be defined elsewhere
267 /* sockaddr_in == file server address structure
269 * Socket address, internet style. declared in : /netinet/in.h
270 * struct sockaddr_in {short sin_family;
272 * struct in_addr sin_addr;
276 * Structure used by kernel to store most addresses. declared in ./sys/socket.h
277 * struct sockaddr{u_short sa_family; address family
278 * char sa_data[14]; up to 14 bytes of direct address
280 * PS : sin stand for "socket internet number"
287 #if defined(__CONST_SOCKADDR_ARG)
288 #define SOCKADDR_CAST (__CONST_SOCKADDR_ARG)
289 #elif defined(__lynx__) || defined(__sun__) || defined(_AIX) || defined(__FreeBSD__) || defined(__NTVIS__)
290 #define SOCKADDR_CAST (struct sockaddr FAR *)
292 #define SOCKADDR_CAST
296 struct sockaddr_in sockaddr; /* done in ./X11/Xdmcp.h */
299 struct sockaddr_in sinme; /* is the socket struct. in the local host */
300 struct sockaddr_in sinhim; /* is the socket struc. in the remote host */
302 #if defined(__lynx__) || defined(__svr4__) || defined(_AIX)
303 struct sockaddr frominet;
305 struct sockaddr_in frominet;
306 #endif /* __lynx__ */
310 #if !defined(__NTVIS__)
312 #endif /* __NTVIS__ */
313 SOCKET fd; /* fd of network socket */
315 #if !defined(__lynx__)
321 FILE *fplog = NULL; /* file pointer for the log file */
322 char logfile[100]; /* file name for the log */
323 static char logfile_head[] ="ttcp_log"; /* header name for the log */
324 int buflen = 8 * 1024; /* length of buffer */
325 char *buf; /* ptr to dynamic buffer */
326 int nbuf = 2 * 1024; /* number of buffers to send in sinkmode */
328 #define START_PKT_LEN 4
329 #define START_PATTERN_LEN 4
330 #define END_PKT_LEN 8
331 #define END_PATTERN_LEN 4
332 #define MIN_UDP_LEN 5
334 char start_pattern[START_PATTERN_LEN] = {1, 2, 3, 4};
335 char end_pattern[END_PATTERN_LEN] = {2, 2, 2, 2};
337 int bufoffset = 0; /* align buffer to this */
338 int bufalign = 16*1024; /* modulo this */
340 int udp = 0; /* 0 = tcp, !0 = udp */
341 int options = 0; /* socket options */
342 int one = 1; /* for 4.3 BSD style setsockopt() */
343 short port = 5010; /* TCP port number */
344 char *host; /* ptr to name of host */
345 int rndm = 0; /* 0="normal", !0=random data */
346 int trans; /* 0=receive, !0=transmit mode */
347 int timeout; /* read timeout in millisec */
348 int debug = 0; /* 0=No-Debug, 1=Debug-Set-On */
349 int sinkmode = 0; /* 0=normal I/O, !0=sink/source mode */
350 int verbose = 0; /* 0=print basic info, 1=print cpu rate,
351 * proc resource usage. */
352 int nodelay = 0; /* set TCP_NODELAY socket option */
353 int pri = 0; /* link layer priority */
354 int b_flag = 0; /* use mread() */
355 int log_cnt = 0; /* append result to a log */
356 int sockbufsize = 0; /* socket buffer size to use */
357 char fmt = 'A'; /* output format:
358 * b = bits, B = bytes,
359 * k = kilobits, K = kilobytes,
360 * m = megabits, M = megabytes,
361 * g = gigabits, G = gigabytes,
362 * A = automatic Xbytes (default) */
363 int touchdata = 0; /* access data after reading */
364 int seq_info = 0; /* report out of order seq nums */
366 int hash = 0; /* print hash marks for each buffer */
367 int udpsleep = 0; /* sleep in between udp buffers */
368 int burstn = 1; /* number of buffers per burst */
369 int bufmin = -1; /* minimum buffer size to use when
370 sending random-size buffers */
371 unsigned int seed = 1; /* seed for random number generator
372 used for random buffer lengths */
373 int no_start = 0; /* skip the start frames for UDP */
374 int no_data = 0; /* skip all data frames for UDP */
375 int no_end = 0; /* skip the end frames for UDP */
377 double nbytes; /* bytes on net */
378 unsigned long numCalls; /* # of I/O system calls */
380 struct hostent *addr;
384 #if defined(UNDER_CE)
386 static char *weekday[] ={"Sun", "Mon","Tues", "Wed", "Thurs", "Fri", "Sat"};
387 static char *month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
388 #define perror printf
391 /*--------------------------------------------------------------------------*/
392 static struct timeval time0; /* Time at which timing started */
393 static struct timeval time1;
394 static struct rusage ru0; /* Resource utilization at the start */
395 static struct rusage ru1;
396 static struct tm *tms; /* system time structure */
399 /*-----------Prototype functions definitions -------------------------------*/
400 /*--------------------------------------------------------------------------*/
402 /* ANSI input/output functions (stdio.h) */
404 #if defined(__lynx__)
405 int getopt(int, char**, char*);
406 int gettimeofday(struct timeval *tp, struct timezone *tzp);
409 #if defined(__svr4__)
415 #if defined(__hpux__)
417 #if defined(__NTVIS__)
419 #if defined(BSD42) || defined(BSD43)
422 int printf( char*, ...);
423 int fprintf(FILE*,char*, ...);
425 int getopt(int, char**, char*);
426 int gettimeofday(struct timeval *tp, struct timezone *tzp);
427 #endif /* BSD42 || BSD43 */
428 #endif /* __NTVIS__ */
429 #endif /* __hpux__ */
431 #endif /* __svr4__ */
432 #endif /* __lynx__ */
434 int main(int argc, char* argv[]);
435 #if ( (!(defined(BSD42))) && (!(defined(BSD43))) && (!(defined(__NTVIS__))) )
436 int read(int, char*, int);
437 int write(int, char*, int);
439 #endif /* !(BSD42) && !(BSD43) */
440 int fclose(FILE *stream);
442 #if !defined(BSD43) && !defined(__NTVIS__)
443 void bzero(char*,int);
444 void bcopy(char*, char*, int);
445 char strncpy(char *s1,char *s2,size_t n);
451 /* ANSI socket functions prototype /sys/socket.h */
452 #if defined(__lynx__)
453 int select(int, fd_set*, fd_set*, fd_set*, struct timeval*);
456 #if defined(__svr4__) && !defined(sparc)
458 /* informations in : /usr/include/sys/socket.h */
459 int socket(int, int, int);
460 int connect(int, struct sockaddr *, int);
461 int bind(int, struct sockaddr *, int);
462 int listen(int, int);
463 int accept(int, struct sockaddr *, int *);
464 int sendto(int, const char *, int, int, const struct sockaddr *, int);
465 int recvfrom(int, char *, int, int, struct sockaddr *, int *);
466 int getpeername(int, struct sockaddr *, int *);
467 int getsockopt(int, int, int, char *, int *);
468 int select(int, fd_set*, fd_set*, fd_set*, struct timeval*);
472 int select(unsigned long, void *, void *, void *, struct timeval *);
475 #if defined(__hpux__)
476 int getrusage(int who,struct rusage *rusage);
479 #if defined(__NTVIS__)
482 #if defined(BSD42) || defined(BSD43)
486 int socket(int, int, int);
487 int connect(int s,struct sockaddr_in *name, int namelen);
488 int bind(int s,struct sockaddr *name,int namelen);
489 int listen(int, int);
490 int accept(int, struct sockaddr_in *, int *);
491 int sendto(int, char *, int, int, struct sockaddr_in *, int);
492 int recvfrom(int, char *, int, int, struct sockaddr_in *, int *);
493 int getpeername(int, struct sockaddr *, int *);
494 int setsockopt(int, int, int, char *, int);
496 int getsockopt(int, int, int, char*, int*);
497 int select(int, fd_set*, fd_set*, fd_set*, struct timeval*);
499 #endif /* BSD42 || BSD43 */
500 #endif /* __hpux__ */
502 #endif /* __svr4__ */
503 #endif /* __lynx__ */
504 #endif /* __NTVIS__ */
506 /* ttcp prototype functions */
507 void udp_rcv_test(void);
508 double calc_cpu_time(struct rusage *r0, struct rusage *r1);
509 double calc_real_time(struct timeval *t0, struct timeval *t1);
510 int getrusage(int who,struct rusage *rusage);
511 void sockets_err(char*);
514 void pattern(char *, int);
515 int Nread(SOCKET, void *, int);
516 int Nwrite(int, void *, int);
518 int mread(int, char *,unsigned);
519 char *outfmt(char format, double b);
520 void prep_timer(void);
521 void read_timer(void);
522 void result_summary(void);
523 void prusage(struct rusage*, struct rusage*,
524 struct timeval*, struct timeval*);
525 void tvadd(struct timeval *tsum, struct timeval *t0,struct timeval *t1);
526 void tvsub(struct timeval *tdiff, struct timeval *t1, struct timeval *t0);
529 void close_log(void);
533 /*--------------------------------------------------------------------------*/
534 #if !defined(__NTVIS__)
540 /*--------------------------------------------------------------------------*/
541 /*--------------------------------------------------------------------------*/
543 main(int argc, char **argv)
545 unsigned long addr_tmp;
547 int sockbufsndsize,sockbufrcvsize;
549 struct sockaddr_in peer;
550 int peerlen = sizeof(peer);
552 #if defined(__NTVIS__)
555 WSAStartup(MAKEWORD(1,1), &WSAData);
556 #endif /* __NTVIS__ */
558 if (argc < 2) { do_Usage(); exit(1); }
561 while ((c=getopt(argc, argv, "hidrstuvxHVBDTLb:f:l:n:p:A:O:S:N:P:R:I:w:")) != -1) {
564 timeout = atoi(optarg);
570 burstn = atoi(optarg);
573 #if defined(__linux__)
577 fprintf(stderr, "ttcp: -P option not supported on this OS\n");
581 bufmin = atoi(optarg);
584 udpsleep = atoi(optarg);
585 if (udpsleep < 0) udpsleep = 0;
586 /* convert msec arg to usec for usleep, minimum 10ms */
587 udpsleep = udpsleep * 1000;
593 fprintf(stdout,"%s %s\n" , RCSid , VersDate );
622 "ttcp: -D option ignored: TCP_NODELAY socket option not supported\n");
629 buflen = atoi(optarg);
632 sinkmode = !sinkmode;
644 bufalign = atoi(optarg);
647 bufoffset = atoi(optarg);
650 #if defined(SO_SNDBUF) || defined(SO_RCVBUF)
651 sockbufsize = atoi(optarg);
654 "ttcp: -b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported\n");
668 {do_Usage(); exit(1);}
670 }/* while getopt() */
676 /* check for '--' args */
679 break; /* while (1) */
681 } else if (!strcmp(argv[0], "--nostart")) {
684 } else if (!strcmp(argv[0], "--nodata")) {
687 } else if (!strcmp(argv[0], "--noend")) {
690 } else if (!strcmp(argv[0], "--debug")) {
694 } else if (!strncmp(argv[0], "--", 2)) {
695 fprintf(stderr, "ttcp: illegal option: %s\n", argv[0]);
698 /* the arg was not a '--' arg */
699 break; /* while (1) */
704 /* ----------------------- main part ----------------------- */
706 #if defined(__NTVIS__) && !defined(UNDER_CE)
707 /* Set "stdin" to have binary mode: */
708 if (_setmode(_fileno(stdin), _O_BINARY) == -1)
709 perror("%s: Error setting stdin to binary mode");
710 /* Set "stdout" to have binary mode: */
711 if (_setmode(_fileno(stdout), _O_BINARY) == -1)
712 perror("%s: Error setting stdout to binary mode");
713 #endif /* __NTVIS__ */
715 if (log_cnt) open_log();
717 /* input error checking */
719 fprintf(stderr, "ttcp: buffers per burst must be less than or equal to "
720 "total number of buffers\n");
724 fprintf(stderr, "ttcp: minimum buffer size specified with -R option must "
725 "be non-negative\n");
728 if (buflen > 65535) {
729 fprintf(stderr, "ttcp: maximum buffer size specified with -l option must "
733 if (bufmin > buflen) {
734 fprintf(stderr, "ttcp: minimum buffer size specified with -R option must "
735 "be less than or equal to (maximum) buffer size specified with -l\n");
739 /* buffer allocation */
741 if (udp && buflen < MIN_UDP_LEN)
742 buflen = MIN_UDP_LEN; /* send more than the sentinel size */
744 if ( (buf = (char *)malloc(buflen+bufalign)) == (char *)NULL)
747 buf +=(bufalign - ((int)buf % bufalign) + bufoffset) % bufalign;
749 fprintf(stderr,"ttcp%s: buflen=%d, nbuf=%d, align=%d/%d, port=%d\n",
750 trans?"-t":"-r",buflen, nbuf, bufalign, bufoffset, port);
751 if (log_cnt)fprintf(fplog," %6d %6d %6d %6d %4d",
752 buflen, nbuf, bufalign, bufoffset, port);
754 /* preload the buffer for the transmit condition */
755 pattern( buf, buflen );
757 /* seed the random number generator */
758 if ((bufmin != -1) || (rndm != 0)) {
759 #if defined(__NTVIS__)
761 #else /* ! __NTVIS__ */
763 #endif /* __NTVIS__ */
769 fprintf(stderr, "ttcp: missing destination host arg\n");
773 bzero((char *)&sinhim, sizeof(sinhim));
777 sinhim.sin_family = AF_INET;
778 sinhim.sin_addr.s_addr = inet_addr(host);
781 {if ((addr=gethostbyname(host)) == NULL) sockets_err("bad hostname");
782 sinhim.sin_family = addr->h_addrtype;
783 bcopy(addr->h_addr,(char*)&addr_tmp, addr->h_length);
784 sinhim.sin_addr.s_addr = addr_tmp;
786 sinhim.sin_port = htons(port);
787 sinme.sin_port = 0; /* free choice */
791 sinme.sin_port = htons(port);
793 #if defined(__NTVIS__)
794 sinme.sin_family = AF_INET;
797 fd = socket(AF_INET, udp?SOCK_DGRAM:SOCK_STREAM, 0);
799 #if defined(__NTVIS__)
800 if (fd == INVALID_SOCKET) sockets_err("socket");
802 if (fd < 0) sockets_err("socket");
806 char *label = trans?"ttcp-t":"ttcp-r";
807 #if defined(UNDER_CE)
808 SYSTEMTIME SystemTime;
811 GetLocalTime(&SystemTime);
812 sprintf(time_str, "%s %s %d %02d:%02d:%02d %d\n", weekday[SystemTime.wDayOfWeek], month[SystemTime.wMonth - 1],
813 SystemTime.wDay, SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wYear);
819 time_str = ctime(&now);
821 fprintf(stdout,"%s: start time %s", label, time_str);
822 fprintf(stdout,"%s: File-Descriptor 0x%x Opened\n", label, fd );
825 #if defined(__NTVIS__)
826 if (bind(fd, (struct sockaddr FAR *)&sinme, sizeof(sinme)) == SOCKET_ERROR)
829 if (bind(fd, SOCKADDR_CAST &sinme, sizeof(sinme)) < 0)
831 #endif /* __NTVIS__ */
834 #if defined(SO_SNDBUF) || defined(SO_RCVBUF)
837 #if defined(__lynx__) || defined(__sun__) || defined(__NTVIS__)
839 {if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *)&sockbufsize,
840 sizeof sockbufsize) < 0)
841 sockets_err("setsockopt: sndbuf");
844 {if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&sockbufsize,
845 sizeof sockbufsize) < 0)
846 sockets_err("setsockopt: rcvbuf");
852 {if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
853 sizeof sockbufsize) < 0)
854 sockets_err("setsockopt: sndbuf");
857 {if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
858 sizeof sockbufsize) < 0)
859 sockets_err("setsockopt: rcvbuf");
861 #endif /* __lynx__ __sun__ __NTVIS__ */
865 ** Added by Daniel Davids to Know Socket-Buffer-Sizes
867 sockbuflen = sizeof sockbufsndsize;
868 #if defined(__lynx__) || defined(__sun__) || defined(__NTVIS__)
869 getsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *)&sockbufsndsize,
871 sockbuflen = sizeof sockbufrcvsize;
872 getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&sockbufrcvsize,
875 getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sockbufsndsize, &sockbuflen);
876 sockbuflen = sizeof sockbufrcvsize;
877 getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &sockbufrcvsize, &sockbuflen);
878 #endif /* __lynx__ __sun__ __NTVIS__ */
879 sockbufsize = ( sockbufsndsize + sockbufrcvsize ) / 2;
881 if ( sockbufsndsize != sockbufrcvsize )
882 {fprintf(stderr, "sockbufsndsize=%d, ", sockbufsndsize );
883 fprintf(stderr, "sockbufrcvsize=%d, ", sockbufrcvsize );
886 #endif /* defined(SO_SNDBUF) || defined(SO_RCVBUF) */
888 if (sockbufsize) fprintf(stderr, "sockbufsize=%d, \n", sockbufsize);
891 {if (sockbufsize)fprintf(fplog," %6d",sockbufsize);
892 else fprintf(fplog," 0");
895 #if defined(__linux__)
896 if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, (char *)&pri, sizeof pri) < 0)
897 sockets_err("setsockopt: priority");
901 if (trans) fprintf(stderr, "# %s sender -> %s #\n", udp?"udp":"tcp", host);
902 else fprintf(stderr, "# %s receiver #\n", udp?"udp":"tcp");
906 #if !defined(__NTVIS__)
907 signal(SIGPIPE, sigpipe);
908 #endif /* !__NTVIS__ */
911 {/* We are the client if transmitting */
915 if( setsockopt(fd, SOL_SOCKET, options, 0, 0) < 0)
917 if( setsockopt(fd, SOL_SOCKET, options, (char *)&one, sizeof(one)) < 0)
919 sockets_err("setsockopt");
924 p = getprotobyname("tcp");
925 #if defined(__lynx__) || defined(__sun__) || defined(__NTVIS__)
926 if( p && setsockopt(fd, p->p_proto, TCP_NODELAY,
927 (char *) &one, sizeof(one)) < 0)
929 if( p && setsockopt(fd, p->p_proto, TCP_NODELAY,
930 &one, sizeof(one)) < 0)
931 #endif /* __lynx__ __sun__ __NTVIS__ */
932 sockets_err("setsockopt: nodelay");
934 #endif /* TCP_NODELAY */
935 if (connect(fd, SOCKADDR_CAST &sinhim, sizeof(sinhim) ) < 0)
936 sockets_err("connect");
939 {/* otherwise, we are the server and should listen for connections */
943 #if defined(sgi) || ( defined(__osf__) && !defined(_CFE) )
944 if( listen(fd,1) <0 )
946 if( listen(fd,0) <0 ) /* allow a queue of 0 */
948 sockets_err("listen");
953 err = setsockopt(fd, SOL_SOCKET, options, 0, 0);
954 #elif defined(__lynx__) || defined(__sun__) || defined(__NTVIS__)
955 err = setsockopt(fd, SOL_SOCKET, options, (char *) &one, sizeof(one));
957 err = setsockopt(fd, SOL_SOCKET, options, &one, sizeof(one));
960 sockets_err("setsockopt");
962 fromlen = sizeof(frominet);
965 fd_set readfds, exceptfds;
966 struct timeval tv_timeout;
969 tv_timeout.tv_sec = timeout/1000;
970 tv_timeout.tv_usec = (timeout%1000)*1000;
974 FD_SET(fd, &readfds);
975 FD_SET(fd, &exceptfds);
977 n = select( fd+1, &readfds, NULL, &exceptfds, &tv_timeout );
978 if (n == 0 || n == SOCKET_ERROR) {
979 sockets_err("select listen");
983 if((fd=accept(fd, SOCKADDR_CAST &frominet, &fromlen) ) < 0)
984 sockets_err("accept");
986 if (getpeername(fd, SOCKADDR_CAST &peer, &peerlen) < 0)
987 sockets_err("getpeername");
989 fprintf(stderr,"ttcp-r: accept from %s\n",inet_ntoa(peer.sin_addr));
991 } /* otherwise we are ... */
1000 register int cnt,multi;
1003 unsigned long rbuflen = 0;
1010 int nochecksum_opt = 0;
1011 err = setsockopt(fd, IPPROTO_UDP, UDP_NOCHECKSUM,
1012 (char *)&nochecksum_opt, sizeof(nochecksum_opt));
1014 sockets_err("setsockopt");
1018 if(udp && !no_start) {
1019 int start_count = 2;
1020 char start_pkt[START_PKT_LEN];
1021 memset(start_pkt, 0, START_PKT_LEN);
1022 memcpy(start_pkt, start_pattern, START_PATTERN_LEN);
1024 while (start_count-- > 0) {
1025 (void)Nwrite( fd, start_pkt, START_PKT_LEN ); /* rcvr start */
1028 /* clear errno (see man page for errno(3), definition of
1029 EINTR. usleep() uses SIGCONT? ) */
1030 if (errno == EINTR) errno = 0;
1035 /* initial seqence num for UDP */
1036 if (udp) buf[0] = 0;
1040 while (multi-- > 0 && !no_data) {
1045 /* set rbuflen to a random value evenly distributed in
1046 [bufmin, buflen]. As long as buflen is < 2^16, we can
1047 fit the calculation in 32 bits */
1048 #if defined(__NTVIS__)
1049 rbuflen = (( (unsigned long)rand() *
1050 ((unsigned long)(buflen-bufmin+1)) ) >> 15) + bufmin;
1051 #else /* ! __NTVIS__ */
1052 rbuflen = (( ((unsigned long)random() >> 15) *
1053 ((unsigned long)(buflen-bufmin+1)) ) >> 16) + bufmin;
1054 #endif /* __NTVIS__ */
1058 unsigned int i, j, l;
1066 for (i = l; i < rbuflen; /* null */) {
1067 #if defined(__NTVIS__)
1068 data = (unsigned long)rand();
1069 #else /* ! __NTVIS__ */
1070 data = (unsigned long)random();
1071 #endif /* __NTVIS__ */
1072 /*fprintf(stderr, "%08x\n",data);*/ /* DEBUG */
1073 /* The NT rand() function returns only 16 useful bits! */
1074 for (j = 0; (j < 2/*4*/) && (i < rbuflen) ; j++) {
1075 buf[i++] = (unsigned char)(data & 0xff);
1081 if ((cnt=Nwrite(fd,buf,rbuflen)) != (int)rbuflen)
1082 sockets_err("error from the socket write");
1083 /* increment sequence num if UDP */
1084 if (udp) buf[0] = (char)(((unsigned char)(buf[0])+1) & 0xFF);
1087 fprintf(stdout,"ttcp-t: %5d | %d Bytes Written in %d write commands \n",
1094 fprintf(stderr,"#");
1097 /* Check for the end of a burst */
1101 /* sleep to avoid overrunning slower receivers - henry */
1104 /* clear errno (see man page for errno(3), definition of
1105 EINTR. usleep() uses SIGCONT? ) */
1106 if (errno == EINTR) errno = 0;
1111 } /* if (sinkmode) */
1120 /* set rbuflen to a random value evenly distributed in
1121 [bufmin, buflen]. As long as buflen is < 2^16, we can
1122 fit the calculation in 32 bits */
1123 #if defined(__NTVIS__)
1124 rbuflen = (( (unsigned long)rand() *
1125 ((unsigned long)(buflen-bufmin+1)) ) >> 15) + bufmin;
1126 #else /* ! __NTVIS__ */
1127 rbuflen = (( ((unsigned long)random() >> 15) *
1128 ((unsigned long)(buflen-bufmin+1)) ) >> 16) + bufmin;
1129 #endif /* __NTVIS__ */
1137 #if !defined(__NTVIS__) /* else case specific to WINNT */
1138 while((cnt=read(0,pb,rbuflen)) > 0)
1139 #else /* __NTVIS__ */
1140 while((cnt=(int)fread(pb,1,rbuflen,stdin)) > 0)
1141 #endif /* __NTVIS__ */
1145 multi = cnt+1; /* don't include seq. num. in count of data */
1148 if ((cnt=Nwrite(fd,buf,multi)) != (int)multi)
1149 sockets_err("error from the socket write");
1150 /* increment seqence num if UDP */
1151 if (udp) buf[0] = (char)(((unsigned char)(buf[0])+1) & 0xFF);
1155 fprintf(stdout,"ttcp-t: %5d | %d Bytes Written in %d write commands \n",
1162 fprintf(stderr,"#");
1165 /* Check for the end of a burst */
1169 /* sleep to avoid overrunning slower receivers - henry */
1172 /* clear errno (see man page for errno(3), definition of
1173 EINTR. usleep() uses SIGCONT? ) */
1174 if (errno == EINTR) errno = 0;
1181 /* set rbuflen to a random value evenly distributed in
1182 [bufmin, buflen]. As long as buflen is < 2^16, we can
1183 fit the calculation in 32 bits */
1184 #if defined(__NTVIS__)
1185 rbuflen = (( (unsigned long)rand() *
1186 ((unsigned long)(buflen-bufmin+1)) ) >> 15) + bufmin;
1187 #else /* ! __NTVIS__ */
1188 rbuflen = (( ((unsigned long)random() >> 15) *
1189 ((unsigned long)(buflen-bufmin+1)) ) >> 16) + bufmin;
1190 #endif /* __NTVIS__ */
1195 } /* if (sinkmode) */
1198 fprintf(stderr,"\n");
1202 #if defined(UNDER_CE)
1203 errno = WSAGetLastError();
1207 if(udp && !no_end) {
1209 int data_count = nbuf;
1210 char end_pkt[END_PKT_LEN];
1211 unsigned char* net_byte = &end_pkt[END_PATTERN_LEN];
1212 memset(end_pkt, 0, END_PKT_LEN);
1213 memcpy(end_pkt, end_pattern, END_PATTERN_LEN);
1214 net_byte[3] = data_count & 0xFF; data_count >>= 8;
1215 net_byte[2] = data_count & 0xFF; data_count >>= 8;
1216 net_byte[1] = data_count & 0xFF; data_count >>= 8;
1217 net_byte[0] = data_count & 0xFF;
1219 while (end_count-- > 0) {
1220 Nwrite( fd, end_pkt, END_PKT_LEN ); /* rcvr end */
1221 if (udpsleep && end_count>0) usleep(udpsleep);
1233 {/* not udp received transfer */
1234 while ((cnt=Nread(fd,buf,buflen)) > 0)
1235 {if(debug)fprintf(stdout,
1236 "ttcp%s: %5d | %d Bytes Read\n",
1237 trans?"-t":"-r", ++nb, cnt );
1240 #if !defined(__NTVIS__) /* else case specific to WINNT */
1241 if (write(1,buf,cnt) != cnt) err("write");
1242 #else /* __NTVIS__ */
1243 if ((int)fwrite(buf,1,cnt,stdout) != cnt) err("fwrite");
1244 #endif /* __NTVIS__ */
1247 #if defined(UNDER_CE)
1248 errno = WSAGetLastError();
1258 if(errno) err("IO");
1260 #if defined(__NTVIS__)
1264 #endif /* __NTVIS__ */
1266 if (verbose) fprintf(stdout,"ttcp%s: File-Descriptor fd 0x%x Closed\n" ,
1267 trans?"-t":"-r", fd );
1269 if (log_cnt) close_log();
1271 fprintf(stderr,"ttcp done.\n");
1276 /*--------------------------------------------------------------------------*/
1280 enum {START_WAIT, DATA_WAIT, DATA_RCV, END_TEST} rcv_state;
1281 enum {START_PKT_TYPE, END_PKT_TYPE, DATA_PKT_TYPE} pkt_type;
1284 unsigned int start_pkt_cnt = 0;
1285 unsigned int end_pkt_cnt = 0;
1286 unsigned int data_pkt_cnt = 0;
1287 unsigned int expected_pkt_cnt = 0;
1288 unsigned char seq = 0;
1289 unsigned char buf_seq;
1291 rcv_state = START_WAIT;
1292 while (rcv_state != END_TEST && (cnt=Nread(fd,buf,buflen)) > 0) {
1293 /* Determine the type of packet received */
1294 if (!memcmp(buf, start_pattern, START_PATTERN_LEN)) {
1295 pkt_type = START_PKT_TYPE;
1297 } else if (!memcmp(buf, end_pattern, END_PATTERN_LEN)) {
1298 /* read the expected data packet count from the end packet */
1299 unsigned char* net_byte = buf;
1300 net_byte += END_PATTERN_LEN;
1301 expected_pkt_cnt = ((net_byte[0] << 24) + (net_byte[1] << 16) +
1302 (net_byte[2] << 8) + net_byte[3]);
1303 pkt_type = END_PKT_TYPE;
1307 pkt_type = DATA_PKT_TYPE;
1310 if (rcv_state == START_WAIT) {
1311 /* Wait until we see a vaild start packet */
1312 if (pkt_type == START_PKT_TYPE) {
1314 rcv_state = DATA_WAIT;
1315 } else if (pkt_type == DATA_PKT_TYPE) {
1316 fprintf(stderr, "ERROR: Missed UDP start packet.\n");
1318 rcv_state = DATA_RCV;
1319 } else if (pkt_type == END_PKT_TYPE) {
1320 fprintf(stderr, "ERROR: Found UDP end packet before start packet or data.\n");
1321 rcv_state = END_TEST;
1323 } else if (rcv_state == DATA_WAIT) {
1324 /* Skip any extra start packets until we see data */
1325 if (pkt_type == START_PKT_TYPE) {
1327 } else if (pkt_type == DATA_PKT_TYPE) {
1328 rcv_state = DATA_RCV;
1329 } else if (pkt_type == END_PKT_TYPE) {
1330 fprintf(stderr, "ERROR: Found UDP end packet without receiving "
1331 "any data packets.\n");
1332 rcv_state = END_TEST;
1334 } else { /* DATA_RCV */
1335 /* Collect data packets until we see a vaild end packet */
1336 if (pkt_type == START_PKT_TYPE) {
1337 /* We missed the end packets and now a new test is
1338 * starting. Report the results of this test then start
1342 fprintf(stderr, "ERROR: Found UDP start packet while receiving data.\n"
1343 "ERROR: Expected more data packets or an end packet.\n");
1348 rcv_state = DATA_WAIT;
1349 } else if (pkt_type == DATA_PKT_TYPE) {
1350 /* loop in this state */
1351 } else if (pkt_type == END_PKT_TYPE) {
1352 /* we used to print results here but now we do when the loop ends */
1353 rcv_state = END_TEST;
1357 /* tally data packets the same way from whatever state we are in */
1358 if (pkt_type == DATA_PKT_TYPE) {
1360 fprintf(stderr, "ttcp-r: %5d | %d Bytes Read\n", ++nbuffers, cnt);
1363 /* check seqence num */
1364 buf_seq = (unsigned char)(buf[0]);
1365 if (buf_seq != seq) {
1366 fprintf(stderr, "ERROR: Out of sequence. "
1367 "Buffer %u, seq %u, expected %u\n",
1368 data_pkt_cnt, buf_seq, seq);
1374 #if !defined(__NTVIS__) /* else case specific to WINNT */
1375 if (write(1,&(buf[1]),cnt-1) != cnt-1) err("write");
1376 #else /* __NTVIS__ */
1377 if ((int)fwrite(&(buf[1]),1,cnt-1,stdout) != cnt-1) err("fwrite");
1378 #endif /* __NTVIS__ */
1383 /* normal end of test */
1386 fprintf(stderr, "ttcp-r: packets %u data, %u start, %u end\n",
1387 data_pkt_cnt, start_pkt_cnt, end_pkt_cnt);
1388 if (expected_pkt_cnt != 0) {
1389 int lost_cnt = expected_pkt_cnt - data_pkt_cnt;
1391 fprintf(stderr, "ttcp-r: %g%% (%u/%u) packet loss\n",
1392 100.0 * (double)lost_cnt/(double)expected_pkt_cnt,
1393 lost_cnt, expected_pkt_cnt);
1397 /*--------------------------------------------------------------------------*/
1398 /* calc_cpu_time(): Time difference, in usecs, of the combined user and
1399 * sys times of the given start and end usage info */
1408 struct timeval tdiff, tend, tstart;
1410 /* total user delta time + total system delta time */
1411 tvadd( &tend, &r1->ru_utime, &r1->ru_stime ); /* user + sys time @ end */
1412 tvadd( &tstart, &r0->ru_utime, &r0->ru_stime ); /* user + sys time @ start*/
1413 tvsub( &tdiff, &tend, &tstart );
1416 time = ((double)tdiff.tv_sec)*1e6 + (double)tdiff.tv_usec;
1418 /* debug mseconds */
1419 ms = tdiff.tv_sec*(time_t)1000 + tdiff.tv_usec/(time_t)1000;
1420 if (ms != (time_t)(time/1000.0)) {
1421 fprintf(stderr, "ERROR: time calc mismatch of msec (%d) to usec (%f)\n",
1422 (int)ms, time/1000.0);
1428 /*--------------------------------------------------------------------------*/
1429 /* calc_real_time(): Time difference in usecs of the given times */
1438 struct timeval tdiff;
1440 tvsub(&tdiff, t1 ,t0);
1443 time = ((double)tdiff.tv_sec)*1e6 + (double)tdiff.tv_usec;
1445 /* debug mseconds */
1446 ms = tdiff.tv_sec*(time_t)1000 + tdiff.tv_usec/(time_t)1000;
1447 if (ms != (time_t)(time/1000.0)) {
1448 fprintf(stderr, "ERROR: time calc mismatch of msec (%d) to usec (%f)\n",
1449 (int)ms, time/1000.0);
1454 /*--------------------------------------------------------------------------*/
1456 result_summary(void)
1458 char* label = trans ? "ttcp-t" : "ttcp-r";
1459 double cput = calc_cpu_time(&ru0, &ru1);
1460 double realt = calc_real_time(&time0, &time1);
1462 double bytes_per_sec;
1464 /* lower end boundary conditions */
1465 t_min = 10.0; /* 10 usec */
1466 #if defined(__NTVIS__)
1467 t_min = 1000.0; /* 1 msec */
1469 if (cput <= t_min) { /* value in usec */
1471 fprintf(stderr,"%s: cpu time too short set at %.0f usec, NOT accurate result.\n",
1474 if (realt <= t_min) { /* value in usec */
1476 fprintf(stderr,"%s: real time too short, set at %.0f usec, NOT accurate result.\n",
1480 bytes_per_sec = (nbytes/realt)*1e6;
1482 fprintf(stderr,"%s: %.0f bytes in %.06f real seconds = %s/sec +++\n",
1483 label, nbytes, realt/1e6, outfmt(fmt, bytes_per_sec));
1486 fprintf(stderr,"%s: %.0f bytes in %.06f cpu seconds = %s/cpu sec\n",
1487 label, nbytes,cput/1e6, outfmt(fmt, (nbytes/cput)*1e6));
1489 fprintf(stderr,"%s: %ld I/O calls, %.3f msec(real)/call, %.3f msec(cpu)/call\n",
1491 (numCalls>0)?(realt/(double)numCalls)/1000.0:0.0,
1492 (numCalls>0)?(cput /(double)numCalls)/1000.0:0.0);
1494 fprintf(stderr,"%s: ", label);
1495 prusage(&ru0, &ru1, &time0, &time1);
1496 fprintf(stderr,"\n");
1499 printf("%s: buffer address %#x\n", label, (unsigned int)buf);
1502 struct timeval tdiff;
1504 tvsub(&tdiff, &ru1.ru_utime, &ru0.ru_utime);
1505 fprintf(fplog," %ld.%06ld", (long)tdiff.tv_sec, (long)tdiff.tv_usec);
1507 tvsub(&tdiff, &ru1.ru_stime, &ru0.ru_stime);
1508 fprintf(fplog," %ld.%06ld", (long)tdiff.tv_sec, (long)tdiff.tv_usec);
1509 /* Percent of cput usage */
1510 fprintf(fplog," %.1f", 100.0 * (cput/realt));
1511 /* validity of real time mesurment */
1512 fprintf(fplog, (realt == t_min)?" 0":" 1");
1513 /* bytes, seconds, MBytes/sec, IO calls */
1514 fprintf(fplog," %10.0f %4.06f %4.3f %6ld",
1515 nbytes, realt/1e6, bytes_per_sec/(1024.0*1024.0), numCalls);
1518 /*--------------------------------------------------------------------------*/
1520 sockets_err(char* s)
1522 #if defined(__NTVIS__)
1523 int err = WSAGetLastError();
1524 char* prefix = trans?"ttcp-t":"ttcp-r";
1525 fprintf(stderr,"%s: %s\n", prefix, s);
1526 fprintf(stderr,"%s: errno=%d\n", prefix, err);
1530 #endif /* __NTVIS__ */
1533 /*--------------------------------------------------------------------------*/
1537 char* prefix = trans?"ttcp-t":"ttcp-r";
1538 fprintf(stderr,"%s: ", prefix);
1540 fprintf(stderr,"%s: errno=%d\n", prefix, errno);
1543 /*--------------------------------------------------------------------------*/
1547 fprintf(stderr,"ttcp%s: %s\n", trans?"-t":"-r", s);
1549 /*--------------------------------------------------------------------------*/
1551 pattern( char *cp, int cnt )
1557 while( cnt1-- > 0 ) {
1558 while( !isprint((c&0x7F)) ) c++;
1562 /*--------------------------------------------------------------------------*/
1564 outfmt(char format, double b)
1566 static char obuf[50];
1567 double giga = 1024.0 * 1024.0 * 1024.0;
1568 double mega = 1024.0 * 1024.0;
1569 double kilo = 1024.0;
1571 if (format == 'A') {
1584 sprintf(obuf, "%.3f GB", b / giga);
1587 sprintf(obuf, "%.3f MB", b / mega);
1590 sprintf(obuf, "%.3f KB", b / kilo);
1593 sprintf(obuf, "%4f B", b);
1596 sprintf(obuf, "%.3f Gbit", b * 8.0 / giga);
1599 sprintf(obuf, "%.3f Mbit", b * 8.0 / mega);
1602 sprintf(obuf, "%.3f Kbit", b * 8.0 / kilo);
1605 sprintf(obuf, "%4f b", b * 8.0);
1608 sprintf(obuf, "default..........");
1612 /*--------------------------------------------------------------------------*/
1617 #if defined(__osf__)
1618 /* getrusage defined in the system lib */
1620 #if defined(__lynx__)
1621 /* getrusage defined in the system lib */
1623 #if defined(__sun__)
1624 /* getrusage defined in the system lib */
1628 getrusage(ignored, ru)
1630 register struct rusage *ru;
1634 #if defined(__NTVIS__)
1636 FILETIME CreateTime, ExitTime, KernelTime, UserTime;
1638 #if defined(UNDER_CE)
1639 phd = GetCurrentThread();
1640 if( GetThreadTimes(phd, &CreateTime, &ExitTime, &KernelTime, &UserTime)
1643 phd = GetCurrentProcess();
1644 if( GetProcessTimes(phd, &CreateTime, &ExitTime, &KernelTime, &UserTime)
1647 {ru->ru_stime.tv_sec = 0;
1648 ru->ru_stime.tv_usec = 0;
1649 ru->ru_utime.tv_sec = 0;
1650 ru->ru_utime.tv_usec = 0;
1654 (void) FileTimeToSystemTime(&KernelTime, &SysTime);
1657 * "System sec=%d, msec=%d\n", SysTime.wSecond, SysTime.wMilliseconds);
1659 ru->ru_stime.tv_sec = SysTime.wSecond;
1660 ru->ru_stime.tv_usec = SysTime.wMilliseconds * 1000;
1661 (void) FileTimeToSystemTime(&UserTime, &SysTime);
1664 * " User sec=%d, msec=%d\n", SysTime.wSecond, SysTime.wMilliseconds);
1666 ru->ru_utime.tv_sec = SysTime.wSecond;
1667 ru->ru_utime.tv_usec = SysTime.wMilliseconds * 1000;
1670 #else /* __NTVIS__ */
1674 /* Assumption: HZ <= 2147 (LONG_MAX/1000000) */
1675 /* info : in lynxOS HZ is called TICKSPERSEC (<conf.h>) */
1677 ru->ru_stime.tv_sec = buftime.tms_stime / HZ;
1678 ru->ru_stime.tv_usec = ((buftime.tms_stime % HZ) * 1000000) / HZ;
1679 ru->ru_utime.tv_sec = buftime.tms_utime / HZ;
1680 ru->ru_utime.tv_usec = ((buftime.tms_utime % HZ) * 1000000) / HZ;
1681 #endif /* __NTVIS__ */
1684 } /* static getrusage */
1686 #endif /* __sun__ */
1687 #endif /* __lynx__ */
1688 #endif /* __osf__ */
1690 /*--------------------------------------------------------------------------*/
1692 #if defined(__hpux__) || defined(_AIX) || defined(__sun__)
1693 /* gettimeofday defined in the system lib */
1695 # if defined (__osf__) ||defined (__lynx__)
1696 /* gettimeofday defined in the system lib */
1700 gettimeofday(tp, zp)
1702 struct timezone *zp;
1704 #if defined(__NTVIS__)
1705 #if defined(UNDER_CE)
1706 SYSTEMTIME SystemTime;
1708 GetLocalTime(&SystemTime);
1709 tp->tv_sec = SystemTime.wSecond;
1710 tp->tv_usec = SystemTime.wMilliseconds * 1000;
1712 struct _timeb timeptr;
1715 tp->tv_sec = timeptr.time;
1716 tp->tv_usec = timeptr.millitm * 1000;
1718 #else /* all cases */
1719 tp->tv_sec = time(0);
1721 #endif /* __NTVIS__ */
1723 } /* static gettimeofday */
1725 #endif /*__osf__ || __lynx__ */
1726 #endif /* __hpux__ || _AIX || __sun__ || __osf__*/
1728 /*--------------------------------------------------------------------------*/
1730 * P R E P _ T I M E R
1735 gettimeofday(&time0, (struct timezone *)0);
1736 getrusage(RUSAGE_SELF, &ru0);
1738 /*--------------------------------------------------------------------------*/
1740 * R E A D _ T I M E R
1746 getrusage(RUSAGE_SELF, &ru1);
1747 gettimeofday(&time1, (struct timezone *)0);
1749 /*--------------------------------------------------------------------------*/
1750 /* Print the process usage calculated from timers values extracted
1751 * before and after the transfer execution.
1755 struct rusage *r0, struct rusage *r1,
1756 struct timeval *t0, struct timeval *t1
1759 struct timeval tdiff;
1762 double cput = calc_cpu_time(r0, r1);
1763 double realt = calc_real_time(t0, t1);
1765 /* t == total user delta time + total system delta time */
1768 printf("timers : end startup\n");
1769 printf("user (sec) : %9ld %9ld\n",r1->ru_utime.tv_sec,
1770 r0->ru_utime.tv_sec);
1771 printf("user (usec): %9ld %9ld\n",r1->ru_utime.tv_usec,
1772 r0->ru_utime.tv_usec);
1773 printf("sys (sec) : %9ld %9ld\n",r1->ru_stime.tv_sec,
1774 r0->ru_stime.tv_sec);
1775 printf("sys (usec): %9ld %9ld\n",r1->ru_stime.tv_usec,
1776 r0->ru_stime.tv_usec);
1777 printf("time (sec) : %9ld %9ld\n",t1->tv_sec,t0->tv_sec);
1778 printf("time (usec): %9ld %9ld\n",t1->tv_usec,t0->tv_usec);
1780 /* for the AIX debug, most counters are outside a good range
1782 printf("ru_ixrss %20ld %20ld \n", r0->ru_ixrss ,r1->ru_ixrss );
1783 printf("ru_idrss %20ld %20ld \n", r0->ru_idrss ,r1->ru_idrss );
1784 printf("ru_isrss %20ld %20ld \n", r0->ru_isrss ,r1->ru_isrss );
1785 printf("ru_minflt %20ld %20ld \n", r0->ru_minflt ,r1->ru_minflt );
1786 printf("ru_majflt %20ld %20ld \n", r0->ru_majflt ,r1->ru_majflt );
1787 printf("ru_nswap %20ld %20ld \n", r0->ru_nswap ,r1->ru_nswap );
1788 printf("ru_inblock %20ld %20ld \n", r0->ru_inblock ,r1->ru_inblock );
1789 printf("ru_oublock %20ld %20ld \n", r0->ru_oublock ,r1->ru_oublock );
1790 printf("ru_msgsnd %20ld %20ld \n", r0->ru_msgsnd ,r1->ru_msgsnd );
1791 printf("ru_msgrcv %20ld %20ld \n", r0->ru_msgrcv ,r1->ru_msgrcv );
1792 printf("ru_nsignals %20ld %20ld \n", r0->ru_nsignals ,r1->ru_nsignals);
1793 printf("ru_nvcsw %20ld %20ld \n", r0->ru_nvcsw ,r1->ru_nvcsw );
1794 printf("ru_nivcsw %20ld %20ld \n", r0->ru_nivcsw ,r1->ru_nivcsw );
1797 /* cpu time in mseconds */
1798 t = (int)(cput / 1000.0);
1800 /* ms == value of the internal clock at the end of the xfer */
1801 /* also called real time. */
1802 /* real time in mseconds */
1803 ms = (int)(realt / 1000.0);
1805 /* The display is based on variables provided by the function getrusage
1806 Info located in : /usr/include/sys/resource.h
1811 /* with AIX cernsp most counters are wrong
1812 * cp = "%Uuser %Ssys %Ereal %P %Xi+%Dd %Mmaxrss %F+%Rpf %Ccsw\0";
1814 cp = "%Uuser %Ssys %Ereal %P\0";
1817 #if defined(__osf__)
1818 cp = "%Uuser %Ssys %Ereal %P %Xi+%Dd %Mmaxrss %F+%Rpf %Ccsw\0";
1820 #if defined(sgi) /* IRIX 3.3 will show 0 for %M,%F,%R,%C */
1821 cp = "%Uuser %Ssys %Ereal %P %Mmaxrss %F+%Rpf %Ccsw\0";
1823 #if defined(__Lynx__)
1824 cp = "%Uuser %Ssys %Ereal %P\0";
1826 cp = "%Uuser %Ssys %Ereal %P\0"; /* all SYSV except those mentionned */
1827 #endif /*__lynx__ */
1832 #else /* BSD system */
1833 cp = "%Uuser %Ssys %Ereal %P %Xi+%Dd %Mmaxrss %F+%Rpf %Ccsw\0";
1840 /* *outp++ = *cp; */
1844 tvsub(&tdiff, &r1->ru_utime, &r0->ru_utime);
1845 fprintf(stderr,"%ld.%06ld", (long)tdiff.tv_sec, (long)tdiff.tv_usec);
1849 tvsub(&tdiff, &r1->ru_stime, &r0->ru_stime);
1850 fprintf(stderr,"%ld.%06ld", (long)tdiff.tv_sec, (long)tdiff.tv_usec);
1858 fprintf(stderr,"%.1f%%", (cput*100.0 / (realt ? realt : 1.0)) );
1861 #if !defined(SYSV) || defined(__osf__) || defined(_AIX)
1864 int i = r1->ru_nswap - r0->ru_nswap;
1865 fprintf(stderr,"%d", i);
1870 fprintf(stderr,"%ld", t == 0 ? 0 : (r1->ru_ixrss-r0->ru_ixrss)/t);
1874 fprintf(stderr,"%ld", t == 0 ? 0 :
1875 (r1->ru_idrss+r1->ru_isrss - (r0->ru_idrss+r0->ru_isrss))/t);
1879 fprintf(stderr,"%ld", t == 0 ? 0 :
1880 ((r1->ru_ixrss+r1->ru_isrss+r1->ru_idrss) -
1881 (r0->ru_ixrss+r0->ru_idrss+r0->ru_isrss))/t);
1885 fprintf(stderr,"%ld", r1->ru_maxrss/2);
1889 fprintf(stderr,"%ld", r1->ru_majflt-r0->ru_majflt);
1893 fprintf(stderr,"%ld", r1->ru_minflt-r0->ru_minflt);
1897 fprintf(stderr,"%ld", r1->ru_inblock-r0->ru_inblock);
1901 fprintf(stderr,"%ld", r1->ru_oublock-r0->ru_oublock);
1905 fprintf(stderr,"%ld+%ld",
1906 r1->ru_nvcsw-r0->ru_nvcsw, r1->ru_nivcsw-r0->ru_nivcsw);
1908 #endif /* !SYSV || __osf__ */
1915 /*--------------------------------------------------------------------------*/
1916 /* add 2 times structure and move usec bigger than 1000000 to sec */
1919 struct timeval *tsum, *t0, *t1;
1921 tsum->tv_sec = t0->tv_sec + t1->tv_sec;
1922 tsum->tv_usec = t0->tv_usec + t1->tv_usec;
1923 if (tsum->tv_usec > 1000000) tsum->tv_sec++, tsum->tv_usec -= 1000000;
1925 /*--------------------------------------------------------------------------*/
1926 /* substract 2 time structure (t1 > t0) */
1928 tvsub(tdiff, t1, t0)
1929 struct timeval *tdiff, *t1, *t0;
1931 tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
1932 tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
1933 if (tdiff->tv_usec < 0) tdiff->tv_sec--, tdiff->tv_usec += 1000000;
1936 /*--------------------------------------------------------------------------*/
1940 int i = (int)l/3600;
1944 fprintf(stderr,"%d:", i);
1946 fprintf(stderr,"%.2d:%.2d", i/60, i%60);
1950 fprintf(stderr,"%d:%.2d", i/60, i%60);
1953 /*--------------------------------------------------------------------------*/
1957 Nread(SOCKET s, void* bufp, int count )
1959 #if defined(__lynx__) || defined(__svr4__) || defined(_AIX) || defined(__NTVIS__) || defined(__FreeBSD__)
1960 struct sockaddr from;
1962 struct sockaddr_in from;
1964 int len = sizeof(from);
1968 fd_set readfds, exceptfds;
1969 struct timeval tv_timeout;
1972 tv_timeout.tv_sec = timeout/1000;
1973 tv_timeout.tv_usec = (timeout%1000)*1000;
1976 FD_ZERO(&exceptfds);
1977 FD_SET(s, &readfds);
1978 FD_SET(s, &exceptfds);
1980 n = select( s+1, &readfds, NULL, &exceptfds, &tv_timeout );
1981 if (n == SOCKET_ERROR) {
1982 sockets_err("select read");
1983 } else if (n == 0) {
1990 {cnt = recvfrom( s, bufp, count, 0, SOCKADDR_CAST &from, &len );
1994 {if( b_flag ) cnt = mread( s, bufp, count ); /* fill bufp */
1997 #if defined(__NTVIS__)
1998 cnt = recv( s, bufp, count, 0 );
2000 cnt = read( s, bufp, count );
2001 #endif /* __NTVIS__ */
2004 if (touchdata && cnt > 0)
2005 {register int c = cnt, sum = 0;
2006 register char *b = bufp;
2007 while (c--) sum += *b++;
2010 /* rchrch printf (" numcall %d read buffer %d bytes \n",numCalls,cnt); */
2013 /*--------------------------------------------------------------------------*/
2017 Nwrite( int s, void* bufp, int count )
2023 cnt = sendto( s, bufp, count, 0, SOCKADDR_CAST &sinhim,
2028 #if defined(__NTVIS__)
2029 if( cnt<0 && WSAENOBUFS == WSAGetLastError())
2031 if( cnt<0 && errno == ENOBUFS )
2032 #endif /* __NTVIS__ */
2034 { delay(18000); errno = 0; goto again; }
2035 } else /* if (udp) */
2038 #if defined(__NTVIS__)
2039 cnt = send( s, bufp, count, 0 );
2042 cnt = write( s, bufp, count );
2044 #endif /* __NTVIS__ */
2048 /*--------------------------------------------------------------------------*/
2056 tv.tv_usec = (time_t)us;
2058 #if defined(__hpux__)
2059 select(1, 0, 0, 0, &tv);
2061 select(1, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv);
2064 /*--------------------------------------------------------------------------*/
2067 * This function performs the function of a read(II) but will
2068 * call read(II) multiple times in order to get the requested
2069 * number of characters. This can be necessary because
2070 * network connections don't deliver data with the same
2071 * grouping as it is written with. Written by Robert S. Miles, BRL.
2074 mread(int s, char* bufp, unsigned n)
2076 register unsigned count = 0;
2081 #if defined(__NTVIS__)
2082 nread = recv(s, bufp, n-count, 0);
2084 nread = read(s, bufp, n-count);
2085 #endif /* __NTVIS__ */
2087 if(nread < 0) {perror("ttcp_mread"); return(-1); }
2088 if(nread == 0) return((int)count);
2089 count += (unsigned)nread;
2095 /*--------------------------------------------------------------------------*/
2098 {static long sysTicks;
2099 #if defined(UNDER_CE)
2100 SYSTEMTIME SystemTime;
2102 sprintf(logfile,"%s_%s",logfile_head,trans?"t":"r");
2104 fprintf(stderr,"open the log file >%s<\n",logfile);
2105 if ((fplog = fopen(logfile,"r")) == NULL)
2107 {if ((fplog = fopen(logfile,"a+")) == NULL)
2108 {fprintf(stderr,"Failure : creation of the file >%s< \n",logfile );
2112 {fprintf(fplog," creation date : ");
2114 #if defined(UNDER_CE)
2115 GetLocalTime(&SystemTime);
2116 sprintf(fplog,"%02d-%02d-%02d %02d:%02d\n", SystemTime.wDay, month[SystemTime.wMonth - 1],
2117 SystemTime.wYear, SystemTime.wHour, SystemTime.wMinute);
2120 tms = localtime(&sysTicks);
2121 fprintf(fplog,"%02d-%02d-%02d %02d:%02d\n",
2122 tms->tm_mday, tms->tm_mon, tms->tm_year,tms->tm_hour, tms->tm_min);
2125 /* An other version will produce : Mon Aug 4 16:32:16 1997
2126 * long lDxcomsTicks; char *pDateTime;
2127 * time(&lDxcomsTicks);
2128 * pDateTime = ctime(&lDxcomsTicks); *(pDateTime+24) = '\0';
2129 * fprintf(fplog," ttcp called : %s", pDateTime);
2131 fprintf(fplog,"format\n");
2132 fprintf(fplog,",buflen, nbuf(byte), bufalign(byte), bufoffset(byte)");
2133 fprintf(fplog,", port, sockbufsize(byte), UserTime(sec), SysTime(sec)\n");
2134 fprintf(fplog,", CPUusage(%%), Validity, nbytes(byte), realt(sec)");
2135 fprintf(fplog,", rate(MB/sec), I/O call, hours*3600+min*60+sec\n\n");
2136 /* day-month-year, hour:minute\n\n"); */
2138 } /* file already exist */
2141 if ((fplog = fopen(logfile,"a+")) == NULL)
2142 {fprintf(stderr,"Failure : access of the file >%s< \n",logfile );
2147 /*--------------------------------------------------------------------------*/
2151 #if defined(UNDER_CE)
2152 SYSTEMTIME SystemTime;
2154 GetLocalTime(&SystemTime);
2155 fprintf(fplog," %d\n", SystemTime.wHour * 3600 + SystemTime.wMinute * 60 + SystemTime.wSecond);
2157 static long sysTicks;
2159 tms = localtime(&sysTicks);
2160 fprintf(fplog," %d\n",((tms->tm_hour)*3600 + (tms->tm_min)*60 + tms->tm_sec));
2165 /*--------------------------------------------------------------------------*/
2166 /* routine emulating UNIX function under NT */
2167 #if defined(__NTVIS__)
2169 /*---------------------------------------------------------------------------*/
2174 return; // without printing
2176 fprintf(stderr, "%s: %s: %c\n",
2177 (NULL != progname) ? progname : "getopt", pch, optopt);
2179 /*---------------------------------------------------------------------------*/
2181 getopt(int argc, char **argv, char *ostr)
2183 static char *place = EMSG; /* option letter processing */
2184 register char *oli; /* option letter list index */
2187 // update scanning pointer
2188 if (optind >= argc || *(place = argv[optind]) != '-' || !*++place) {
2192 // place now points to the first char past the initial '-'
2193 if (place[0] == '-') {
2195 // Was the word just a '--'?
2196 if (place[1] == '\0')
2197 ++optind; // yes, so consume the word
2198 // otherwise, the '--' was the start of a longer word,
2199 // so do not consume it.
2205 /* option letter okay? */
2206 if ((optopt = (int)*place++) == (int)':'
2207 || !(oli = strchr(ostr, optopt))) {
2211 error("illegal option");
2214 if (*++oli != ':') {
2215 /* don't need argument */
2220 /* need an argument */
2222 optarg = place; /* no white space */
2223 } else if (argc <= ++optind) {
2226 error("option requires an argument");
2229 optarg = argv[optind]; /* white space */
2234 return optopt; // return option letter
2238 usleep(unsigned int microseconds)
2240 Sleep(microseconds/1000);
2242 #endif /* __NTVIS__ */
2243 /*--------------------------------------------------------------------------*/
2244 /*--------------------------------------------------------------------------*/
2249 " Details about the reply: \n"
2251 " ttcp-t: buflen=8192, nbuf=100, align=16384/0, port=5010\n"
2252 " ttcp-t: File-Descriptor 0x4 Opened\n"
2253 " # tcp sender -> <host> #\n"
2254 " ttcp-t: 819200 bytes in 1.152557 real seconds = 694.109 KB/sec +++\n"
2255 " ttcp-t: 100 I/O calls, 11.526 msec(real)/call, 0.213 msec(cpu)/call\n"
2256 " ttcp-t: 0.001914user 0.019388sys 0:01real 1% 9i+58d 190maxrss 1+2pf 177+180csw\n"
2257 " ttcp-t: buffer address 0x28000\n"
2258 " ttcp-t: File-Descriptor fd 0x4 Closed\n"
2260 "cpu seconds == (sec) elapse ru_utime + elapse ru_stime.\n"
2261 " ru_utime == The total amount of time running in user mode.\n"
2262 " ru_stime == The total amount of time spent in the system.\n"
2263 " executing on behalf of the process.\n"
2264 "real seconds == elapse time calculated by the system timer (date).\n"
2265 "I/O calls == I/O call to the driver.\n"
2266 "msec/call == average elapse time (Real seconds) between each I/O.\n"
2267 "calls/sec == invert of msec/call.\n"
2268 "user == (sec.msec) elaspe ru_utime.\n"
2269 "sys == (sec.msec) elapse ru_stime.\n"
2270 "real == (min:sec) CPU seconds.\n"
2271 "%% == Real seconds / CPU seconds.\n"
2272 "(ru_ixrss)i+(ru_idrss)d\n"
2273 " ru_ixrss == An integral value indicating the amount of memory \n"
2274 " used by the text segment that was also shared among\n"
2275 " other processes. This value is expressed in units of\n"
2276 " kilobytes * seconds-of-execution and is calculated \n"
2277 " by adding the number of shared memory pages in use \n"
2278 " each time the internal system clock ticks, and then\n"
2279 " averaging over one-second intervals.\n"
2280 " ru_idrss == An integral value of the amount of unshared memory \n"
2281 " in the data segment of a process (expressed in \n"
2282 " units of kilobytes * seconds-of-execution).\n";
2285 " (ru_maxrss/2)maxrss.\n"
2286 " ru_maxrss == The maximum size, in kilobytes, of the used\n"
2287 " resident set size. \n"
2288 " (ru_majflt)+(ru_minflt)pf : Page fault\n"
2289 " ru_majflt == The number of page faults serviced that required\n"
2291 " ru_minflt == The number of page faults serviced without any\n"
2292 " I/O activity. In this case, I/O activity is \n"
2293 " avoided by reclaiming a page frame from the list \n"
2294 " of pages awaiting reallocation. \n"
2295 "(ru_nvcsw)+(ru_nivcsw)csw : context switch\n"
2296 " ru_nvcsw == The number of times a context switch resulted \n"
2297 " because a process voluntarily gave up the \n"
2298 " processor before its time slice was completed. \n"
2299 " This usually occurs while the process waits \n"
2300 " for availability of a resource.\n"
2301 " ru_nivcsw == The number of times a context switch resulted \n"
2302 " because a higher priority process ran or because\n"
2303 " the current process exceeded its time slice.\n\n";
2306 "log file format :\n"
2307 " buflen, nbuf(byte), bufalign(byte), bufoffset(byte)\n"
2308 " port, sockbufsize(byte), UserTime(sec), SysTime(sec), CPUusage(%)\n"
2309 " nbytes(byte), realt(sec), rate(MB/sec), I/O call,\n"
2310 " hours*3600+min*60+sec\n\n";
2312 fprintf(stderr,More_help);
2313 fprintf(stderr,More_help1);
2314 fprintf(stderr,More_help2);
2316 /*---------------------------------------------------------------------------*/
2321 " Usage: ttcp -t [-options] host [ < in ] ttcp -r [-options > out]\n"
2322 "Example: ttcp -t -s -v -n100 host ttcp -r -s -v -n100\n"
2324 " -V prints version number and date of last modification\n"
2325 " -L create and append all results to a file named ttcp_log\n"
2327 " -l ## length of bufs read from or written to network (default 8192,\n"
2329 " -u use UDP instead of TCP\n"
2330 " -p ## port number to send to or listen at (default 5001)\n"
2331 #if defined(__linux__)
2332 " -P ## link-layer priority (default 0)\n"
2334 " -s (ttcp -t) : source a pattern to network\n"
2335 " (ttcp -r) : sink (discard) all data from network\n"
2336 " -A ## align the start of buffers to this modulus (default 16384)\n"
2337 " -O ## start buffers at this offset from the modulus (default 0)\n"
2338 " -v verbose: print more statistics\n"
2339 " -d set SO_DEBUG socket option\n"
2340 " -b ## set socket buffer size (if supported)\n"
2341 " -f X format for rate: b,B = bits, bytes k,K = kilo{bits,bytes};\n"
2342 " m,M = mega{bits,bytes}; g,G = giga{bits,bytes}\n"
2343 " -w ## set timeout value (in milliseconds) to exit if no receive data or tcp connect\n"
2344 "Options specific to (ttcp -t) :\n"
2345 " -n ## number of source bufs written to network (default 2048)\n"
2346 " -x use random data in tcp/udp frames (-I provides seed)\n"
2347 " -D don't buffer TCP writes (sets TCP_NODELAY socket option)\n"
2348 " -H print hash marks to indicate progress, one per buffer\n"
2349 " -I init/seed value for RNG when sending random size bufs (default 1)\n"
2350 " -N ## number of source bufs per burst, i.e between sleeps (default 1)\n"
2351 " -R ## send random size buffers with minimum size specified, max size\n"
2352 " is value of -l option\n"
2353 " -S ## millisecs between bursts (only used for udp, 10ms resolution)\n"
2354 "Options specific to (ttcp -r) :\n"
2355 " -B for -s, only output full blocks as specified by -l (for TAR)\n"
2356 " -T \"touch\": access each byte as it's read\n"
2357 " -i report information on out of order sequence numbers\n"
2359 "Options for debug:\n"
2360 " --nostart do not send UDP start packets\n"
2361 " --noend do not send UDP end packets\n"
2362 " --nodata do not send UDP data packets\n"
2363 " --debug print extra debug outputs\n"
2367 fprintf(stderr,Usage);
2370 /* Define automatic Emacs variables for consistent code formatting */
2371 /* Local Variables: */
2372 /* c-basic-offset:2 */
2373 /* indent-tabs-mode:nil */