|
|
1.1 ! root 1: /* ! 2: * UNIX domain datagram socket random ! 3: * send benchmark. ! 4: */ ! 5: ! 6: #include <stdio.h> ! 7: #include <signal.h> ! 8: ! 9: #include <sys/types.h> ! 10: #include <sys/socket.h> ! 11: #include <sys/time.h> ! 12: #include <sys/un.h> ! 13: #include <sys/resource.h> ! 14: ! 15: struct sockaddr_un sun; ! 16: struct sockaddr_un myname; ! 17: ! 18: int catchsig(); ! 19: #define MAXMSGS 10000 ! 20: int lens[MAXMSGS]; ! 21: char *malloc(); ! 22: ! 23: main(argc, argv) ! 24: char *argv[]; ! 25: { ! 26: register char *buf; ! 27: register int i, kb, msgs = 0; ! 28: int msglen = 0, ms, lens[MAXMSGS]; ! 29: int pid, s, sunlen; ! 30: struct timeval t1, t2; ! 31: struct rusage r1, r2; ! 32: ! 33: if (argc < 3) { ! 34: printf("usage: %s #msgs max-msg-length\n", argv[0]); ! 35: exit(1); ! 36: } ! 37: msgs = atoi(argv[1]); ! 38: msglen = atoi(argv[2]); ! 39: buf = malloc(msglen); ! 40: if (buf == 0) { ! 41: printf("Couldn't allocate data buffer\n"); ! 42: exit(1); ! 43: } ! 44: for (i = 0; i < msgs; i++) ! 45: lens[i] = random() % msglen; ! 46: for (i = 0; i < msglen; i++) ! 47: buf[i] = random() & 0xff; ! 48: myname.sun_family = AF_UNIX; ! 49: printf("%d messages, max message length %d bytes\n", msgs, msglen); ! 50: signal(SIGINT, catchsig); ! 51: pid = fork(); ! 52: if (pid == 0) { ! 53: s = socket(AF_UNIX, SOCK_DGRAM, 0); ! 54: if (s < 0) { ! 55: perror("socket"); ! 56: exit(1); ! 57: } ! 58: sprintf(myname.sun_path, "unixdg%d", getpid()); ! 59: if (bind(s, &myname, strlen(myname.sun_path) + 2) < 0) { ! 60: perror("bind (child)"); ! 61: exit(1); ! 62: } ! 63: for (i = 0; i < msgs; i++) { ! 64: sunlen = sizeof (sun); ! 65: if (recvfrom(s, buf, lens[i], 0, &sun, &sunlen) < 0) ! 66: perror("recvfrom"); ! 67: } ! 68: } else { ! 69: s = socket(AF_UNIX, SOCK_DGRAM, 0); ! 70: if (s < 0) { ! 71: perror("socket"); ! 72: exit(1); ! 73: } ! 74: sprintf(myname.sun_path, "unixdg%d", getpid()); ! 75: if (bind(s, &myname, strlen(myname.sun_path) + 2) < 0) { ! 76: perror("bind (parent)"); ! 77: exit(1); ! 78: } ! 79: sun.sun_family = AF_UNIX; ! 80: sprintf(sun.sun_path, "unixdg%d", pid); ! 81: sunlen = strlen(sun.sun_path) + 2; ! 82: getrusage(RUSAGE_SELF, &r1); ! 83: gettimeofday(&t1, (struct timezone *)0); ! 84: for (kb = 0, i = 0; i < msgs; kb += lens[i], i++) ! 85: if (sendto(s, buf, lens[i], 0, &sun, sunlen) < 0) ! 86: perror("sendto"); ! 87: gettimeofday(&t2, (struct timezone *)0); ! 88: getrusage(RUSAGE_SELF, &r2); ! 89: timevalsub(&t2, &t1); ! 90: ms = t2.tv_usec / 1000; ! 91: printf("%d msgs (%d bytes) in %d.%d secs", msgs, ! 92: kb, t2.tv_sec, ms / 100); ! 93: #define nz(x) (x == 0 ? 1 : x) ! 94: printf(", %d bytes/msg, %6.2f kb/s, %4.1f ms/msg\n", ! 95: kb / nz(msgs), (8. * kb) / (nz(t2.tv_sec) * 1024.), ! 96: (1000. * t2.tv_sec + ms) / nz(msgs)); ! 97: timevalsub(&r2.ru_stime, &r1.ru_stime); ! 98: timevalsub(&r2.ru_utime, &r1.ru_utime); ! 99: r2.ru_nvcsw -= r1.ru_nvcsw; ! 100: r2.ru_nivcsw -= r1.ru_nivcsw; ! 101: printf("System %d.%d, user %d.%d, %d vcsw, %d ivcsw\n", ! 102: r2.ru_stime.tv_sec, r2.ru_stime.tv_usec / 100000, ! 103: r2.ru_utime.tv_sec, r2.ru_utime.tv_usec / 100000, ! 104: r2.ru_nvcsw, r2.ru_nivcsw); ! 105: kill(pid, SIGINT); ! 106: } ! 107: close(s); ! 108: unlink(myname.sun_path); ! 109: } ! 110: ! 111: catchsig(s) ! 112: int s; ! 113: { ! 114: ! 115: unlink(myname.sun_path); ! 116: exit(1); ! 117: } ! 118: ! 119: /* ! 120: * Add and subtract routines for timevals. ! 121: * N.B.: subtract routine doesn't deal with ! 122: * results which are before the beginning, ! 123: * it just gets very confused in this case. ! 124: * Caveat emptor. ! 125: */ ! 126: timevaladd(t1, t2) ! 127: struct timeval *t1, *t2; ! 128: { ! 129: ! 130: t1->tv_sec += t2->tv_sec; ! 131: t1->tv_usec += t2->tv_usec; ! 132: timevalfix(t1); ! 133: } ! 134: ! 135: timevalsub(t1, t2) ! 136: struct timeval *t1, *t2; ! 137: { ! 138: ! 139: t1->tv_sec -= t2->tv_sec; ! 140: t1->tv_usec -= t2->tv_usec; ! 141: timevalfix(t1); ! 142: } ! 143: ! 144: timevalfix(t1) ! 145: struct timeval *t1; ! 146: { ! 147: ! 148: if (t1->tv_usec < 0) { ! 149: t1->tv_sec--; ! 150: t1->tv_usec += 1000000; ! 151: } ! 152: if (t1->tv_usec >= 1000000) { ! 153: t1->tv_sec++; ! 154: t1->tv_usec -= 1000000; ! 155: } ! 156: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.