bw_unix.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * bw_unix.c - simple Unix stream socket bandwidth test
  3. *
  4. * Copyright (c) 1994 Larry McVoy. Distributed under the FSF GPL with
  5. * additional restriction that results may published only if
  6. * (1) the benchmark is unmodified, and
  7. * (2) the version in the sccsid below is included in the report.
  8. * Support for this development by Sun Microsystems is gratefully acknowledged.
  9. */
  10. char *id = "$Id$\n";
  11. #include "bench.h"
  12. void reader(int controlfd, int pipefd, size_t bytes);
  13. void writer(int controlfd, int pipefd);
  14. size_t XFER = 10*1024*1024;
  15. int pid;
  16. char *buf;
  17. int
  18. main()
  19. {
  20. int pipes[2];
  21. int control[2];
  22. buf = valloc(XFERSIZE);
  23. touch(buf, XFERSIZE);
  24. if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipes) == -1) {
  25. perror("socketpair");
  26. return (1);
  27. }
  28. if (pipe(control) == -1) {
  29. perror("pipe");
  30. return(1);
  31. }
  32. switch (pid = fork()) {
  33. case 0:
  34. close(control[1]);
  35. close(pipes[0]);
  36. writer(control[0], pipes[1]);
  37. return(0);
  38. /*NOTREACHED*/
  39. case -1:
  40. perror("fork");
  41. return(1);
  42. /*NOTREACHED*/
  43. default:
  44. break;
  45. }
  46. close(control[0]);
  47. close(pipes[1]);
  48. BENCH(reader(control[1], pipes[0], XFER), MEDIUM);
  49. fprintf(stderr, "AF_UNIX sock stream bandwidth: ");
  50. mb(get_n() * XFER);
  51. kill(pid, 15);
  52. return(0);
  53. }
  54. void
  55. writer(int controlfd, int pipefd)
  56. {
  57. size_t todo;
  58. size_t bufsize = XFERSIZE;
  59. ssize_t n;
  60. for ( ;; ) {
  61. bufsize = XFERSIZE;
  62. n = read(controlfd, &todo, sizeof(todo));
  63. if (n < 0) perror("writer::read");
  64. while (todo > 0) {
  65. if (todo < bufsize) bufsize = todo;
  66. #ifdef TOUCH
  67. touch(buf, bufsize);
  68. #endif
  69. n = write(pipefd, buf, bufsize);
  70. if (n <= 0) {
  71. perror("writer::write");
  72. break;
  73. }
  74. todo -= n;
  75. }
  76. }
  77. }
  78. void
  79. reader(int controlfd, int pipefd, size_t bytes)
  80. {
  81. int done = 0;
  82. size_t todo = bytes;
  83. size_t bufsize = XFERSIZE;
  84. ssize_t n;
  85. n = write(controlfd, &bytes, sizeof(bytes));
  86. if (n < 0) perror("reader::write");
  87. while ((done < todo) && ((n = read(pipefd, buf, bufsize)) > 0)) {
  88. done += n;
  89. if (todo - done < bufsize) bufsize = todo - done;
  90. }
  91. if (n < 0) perror("reader::write");
  92. if (done < bytes) {
  93. fprintf(stderr, "reader: bytes=%d, done=%d, todo=%d\n", bytes, done, todo);
  94. }
  95. }