Annotation of Net2/tests/benchmarks/inet/inetdg.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include <strings.h>
                      3: #include <signal.h>
                      4: #include <netdb.h>
                      5: 
                      6: #include <sys/types.h>
                      7: #include <sys/socket.h>
                      8: #include <sys/time.h>
                      9: #include <netinet/in.h>
                     10: #include <sys/resource.h>
                     11: 
                     12: struct sockaddr_in sin;
                     13: char   myname[256];
                     14: 
                     15: #define        MAXMSGS         (5000 - 1)
                     16: #define        MAXMSGLEN       BUFSIZ
                     17: 
                     18: char   *malloc();
                     19: 
                     20: main(argc, argv)
                     21:        char *argv[];
                     22: {
                     23:        int s, sinlen;
                     24:        struct hostent *hp;
                     25:        register char *buf;
                     26:        register int i, kb, msgs = 0;
                     27:        int msglen = 0, ms, lens[MAXMSGS];
                     28:        struct timeval t1, t2;
                     29:        struct rusage r1, r2;
                     30: 
                     31:        if (argc < 2) {
                     32:                printf("usage: %s port [ #msgs ] [ max-msg-length ]\n",
                     33:                   argv[0]);
                     34:                exit(1);
                     35:        }
                     36:        s = socket(AF_INET, SOCK_DGRAM, 0);
                     37:        if (s < 0) {
                     38:                perror("socket");
                     39:                exit(1);
                     40:        }
                     41:        sinlen = sizeof (myname);
                     42:        gethostname(myname, &sinlen);
                     43:        hp = gethostbyname(myname);
                     44:        if (hp == 0) {
                     45:                printf("%s: host unknown\n", myname);
                     46:                exit(1);
                     47:        }
                     48:        sin.sin_family = AF_INET;
                     49:        bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
                     50:        if (bind(s, &sin, sizeof (sin)) < 0) {
                     51:                perror("inetdg: bind");
                     52:                exit(1);
                     53:        }
                     54:        sin.sin_port = htons(atoi(argv[1]));
                     55:        if (argc > 2)
                     56:                msgs = atoi(argv[2]);
                     57:        if (msgs <= 0 || msgs > MAXMSGS)
                     58:                msgs = MAXMSGS;
                     59:        if (argc > 3)
                     60:                msglen = atoi(argv[3]);
                     61:        if (msglen <= 0 || msglen >= MAXMSGLEN)
                     62:                msglen = MAXMSGLEN;
                     63:        buf = malloc(msglen);
                     64:        if (buf == 0) {
                     65:                printf("couldn't allocate data buffer\n");
                     66:                exit(1);
                     67:        }
                     68:        for (i = 0; i < msgs; i++)
                     69:                lens[i] = random() % msglen;
                     70:        for (i = 0; i < msglen; i++)
                     71:                buf[i] = random() & 0xff;
                     72:        printf("%d messages, max message length %d bytes\n",
                     73:           msgs + 1, msglen);
                     74:        (void) sendto(s, "begin", 5, 0, &sin, sizeof (sin));
                     75:        kb = 0;
                     76:        getrusage(RUSAGE_SELF, &r1);
                     77:        gettimeofday(&t1, (struct timezone *)0);
                     78:        for (i = 0; i < msgs; i++) {
                     79:                if (sendto(s, buf, lens[i], 0, &sin, sizeof (sin)) < 0)
                     80:                        perror("inetdg: sendto");
                     81:                kb += lens[i];
                     82:        }
                     83:        (void) sendto(s, "end", 3, 0, &sin, sizeof (sin));
                     84:        gettimeofday(&t2, (struct timezone *)0);
                     85:        getrusage(RUSAGE_SELF, &r2);
                     86:        msgs++, kb += 3;
                     87:        timevalsub(&t2, &t1);
                     88:        ms = t2.tv_usec / 1000;
                     89:        printf("%d msgs (%d bytes) in %d.%d secs", msgs,
                     90:            kb, t2.tv_sec, ms / 100);
                     91:        printf(", %d bytes/msg, %6.2f kb/s, %4.1f ms/msg\n",
                     92:            kb / msgs, (8. * kb) / (t2.tv_sec * 1024.),
                     93:            (1000. * t2.tv_sec + ms) / msgs);
                     94:        timevalsub(&r2.ru_stime, &r1.ru_stime);
                     95:        timevalsub(&r2.ru_utime, &r1.ru_utime);
                     96:        r2.ru_nvcsw -= r1.ru_nvcsw;
                     97:        r2.ru_nivcsw -= r1.ru_nivcsw;
                     98:        printf("System %d.%d, user %d.%d, %d vcsw, %d ivcsw\n",
                     99:            r2.ru_stime.tv_sec, r2.ru_stime.tv_usec / 100000,
                    100:            r2.ru_utime.tv_sec, r2.ru_utime.tv_usec / 100000,
                    101:            r2.ru_nvcsw, r2.ru_nivcsw);
                    102:        pause();
                    103: }
                    104: 
                    105: /*
                    106:  * Add and subtract routines for timevals.
                    107:  * N.B.: subtract routine doesn't deal with
                    108:  * results which are before the beginning,
                    109:  * it just gets very confused in this case.
                    110:  * Caveat emptor.
                    111:  */
                    112: timevaladd(t1, t2)
                    113:        struct timeval *t1, *t2;
                    114: {
                    115: 
                    116:        t1->tv_sec += t2->tv_sec;
                    117:        t1->tv_usec += t2->tv_usec;
                    118:        timevalfix(t1);
                    119: }
                    120: 
                    121: timevalsub(t1, t2)
                    122:        struct timeval *t1, *t2;
                    123: {
                    124: 
                    125:        t1->tv_sec -= t2->tv_sec;
                    126:        t1->tv_usec -= t2->tv_usec;
                    127:        timevalfix(t1);
                    128: }
                    129: 
                    130: timevalfix(t1)
                    131:        struct timeval *t1;
                    132: {
                    133: 
                    134:        if (t1->tv_usec < 0) {
                    135:                t1->tv_sec--;
                    136:                t1->tv_usec += 1000000;
                    137:        }
                    138:        if (t1->tv_usec >= 1000000) {
                    139:                t1->tv_sec++;
                    140:                t1->tv_usec -= 1000000;
                    141:        }
                    142: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.