bw_tcp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * bw_tcp.c - simple TCP bandwidth test
  3. *
  4. * Three programs in one -
  5. * server usage: bw_tcp -s
  6. * client usage: bw_tcp hostname [msgsize]
  7. * shutdown: bw_tcp -hostname
  8. *
  9. * Copyright (c) 1994 Larry McVoy.
  10. * Copyright (c) 2002 Carl Staelin. Distributed under the FSF GPL with
  11. * additional restriction that results may published only if
  12. * (1) the benchmark is unmodified, and
  13. * (2) the version in the sccsid below is included in the report.
  14. * Support for this development by Sun Microsystems is gratefully acknowledged.
  15. */
  16. char *id = "$Id$\n";
  17. #include "bench.h"
  18. int server_main(int ac, char **av);
  19. int client_main(int ac, char **av);
  20. void source(int data);
  21. void
  22. transfer(uint64 msgsize, int server, char *buf)
  23. {
  24. int c;
  25. while ((c = read(server, buf, msgsize)) > 0) {
  26. msgsize -= c;
  27. }
  28. if (c < 0) {
  29. perror("bw_tcp: transfer: read failed");
  30. exit(4);
  31. }
  32. }
  33. /* ARGSUSED */
  34. int
  35. client_main(int ac, char **av)
  36. {
  37. int server;
  38. uint64 msgsize = XFERSIZE;
  39. uint64 usecs;
  40. char t[512];
  41. char* buf;
  42. char* usage = "usage: %s -remotehost OR %s remotehost [msgsize]\n";
  43. if (ac != 2 && ac != 3) {
  44. (void)fprintf(stderr, usage, av[0], av[0]);
  45. exit(0);
  46. }
  47. if (ac == 3) {
  48. msgsize = bytes(av[2]);
  49. }
  50. /*
  51. * Disabler message to other side.
  52. */
  53. if (av[1][0] == '-') {
  54. server = tcp_connect(&av[1][1], TCP_DATA, SOCKOPT_REUSE);
  55. write(server, "0", 1);
  56. exit(0);
  57. }
  58. buf = valloc(msgsize);
  59. touch(buf, msgsize);
  60. if (!buf) {
  61. perror("valloc");
  62. exit(1);
  63. }
  64. server = tcp_connect(av[1], TCP_DATA, SOCKOPT_READ|SOCKOPT_REUSE);
  65. if (server < 0) {
  66. perror("bw_tcp: could not open socket to server");
  67. exit(2);
  68. }
  69. (void)sprintf(t, "%llu", msgsize);
  70. if (write(server, t, strlen(t) + 1) != strlen(t) + 1) {
  71. perror("control write");
  72. exit(3);
  73. }
  74. /*
  75. * Send data over socket for at least 7 seconds.
  76. * This minimizes the effect of connect & opening TCP windows.
  77. */
  78. BENCH1(transfer(msgsize, server, buf), LONGER);
  79. BENCH(transfer(msgsize, server, buf), 0);
  80. out: (void)fprintf(stderr, "Socket bandwidth using %s: ", av[1]);
  81. mb(msgsize * get_n());
  82. close(server);
  83. exit(0);
  84. /*NOTREACHED*/
  85. }
  86. void
  87. child()
  88. {
  89. wait(0);
  90. signal(SIGCHLD, child);
  91. }
  92. /* ARGSUSED */
  93. int
  94. server_main(int ac, char **av)
  95. {
  96. int data, newdata;
  97. GO_AWAY;
  98. signal(SIGCHLD, child);
  99. data = tcp_server(TCP_DATA, SOCKOPT_READ|SOCKOPT_WRITE|SOCKOPT_REUSE);
  100. for ( ;; ) {
  101. newdata = tcp_accept(data, SOCKOPT_WRITE|SOCKOPT_READ);
  102. switch (fork()) {
  103. case -1:
  104. perror("fork");
  105. break;
  106. case 0:
  107. source(newdata);
  108. exit(0);
  109. default:
  110. close(newdata);
  111. break;
  112. }
  113. }
  114. }
  115. /*
  116. * Read the number of bytes to be transfered.
  117. * Write that many bytes on the data socket.
  118. */
  119. void
  120. source(int data)
  121. {
  122. int n;
  123. char t[512];
  124. char* buf;
  125. uint64 msgsize;
  126. bzero((void*)t, 512);
  127. if (read(data, t, 511) <= 0) {
  128. perror("control nbytes");
  129. exit(7);
  130. }
  131. sscanf(t, "%llu", &msgsize);
  132. buf = valloc(msgsize);
  133. touch(buf, msgsize);
  134. if (!buf) {
  135. perror("valloc");
  136. exit(1);
  137. }
  138. /*
  139. * A hack to allow turning off the absorb daemon.
  140. */
  141. if (msgsize == 0) {
  142. tcp_done(TCP_DATA);
  143. kill(getppid(), SIGTERM);
  144. exit(0);
  145. }
  146. /*
  147. fprintf(stderr, "server: msgsize=%llu, t=%s\n", msgsize, t); fflush(stderr);
  148. /* XXX */
  149. while ((n = write(data, buf, msgsize)) > 0) {
  150. #ifdef TOUCH
  151. touch(buf, msgsize);
  152. #endif
  153. ;
  154. }
  155. free(buf);
  156. }
  157. int
  158. main(int ac, char **av)
  159. {
  160. char* usage = "Usage: %s -s OR %s -serverhost OR %s serverhost [msgsize]\n";
  161. if (ac < 2 || 3 < ac) {
  162. fprintf(stderr, usage, av[0], av[0], av[0]);
  163. exit(1);
  164. }
  165. if (ac == 2 && !strcmp(av[1], "-s")) {
  166. if (fork() == 0) {
  167. server_main(ac, av);
  168. }
  169. exit(0);
  170. } else {
  171. client_main(ac, av);
  172. }
  173. return(0);
  174. }