A clone of btpd with my configuration changes.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

182 satır
4.3 KiB

  1. /*
  2. * Copyright 2003 Niels Provos <provos@citi.umich.edu>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. *
  28. * Mon 03/10/2003 - Modified by Davide Libenzi <davidel@xmailserver.org>
  29. *
  30. * Added chain event propagation to improve the sensitivity of
  31. * the measure respect to the event loop efficency.
  32. *
  33. *
  34. */
  35. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <sys/time.h>
  41. #include <sys/socket.h>
  42. #include <sys/signal.h>
  43. #include <sys/resource.h>
  44. #include <fcntl.h>
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <unistd.h>
  49. #include <errno.h>
  50. #include <event.h>
  51. static int count, writes, fired;
  52. static int *pipes;
  53. static int num_pipes, num_active, num_writes;
  54. static struct event *events;
  55. void
  56. read_cb(int fd, short which, void *arg)
  57. {
  58. int idx = (int) arg, widx = idx + 1;
  59. u_char ch;
  60. count += read(fd, &ch, sizeof(ch));
  61. if (writes) {
  62. if (widx >= num_pipes)
  63. widx -= num_pipes;
  64. write(pipes[2 * widx + 1], "e", 1);
  65. writes--;
  66. fired++;
  67. }
  68. }
  69. struct timeval *
  70. run_once(void)
  71. {
  72. int *cp, i, space;
  73. static struct timeval ts, te;
  74. for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
  75. event_del(&events[i]);
  76. event_set(&events[i], cp[0], EV_READ | EV_PERSIST, read_cb, (void *) i);
  77. event_add(&events[i], NULL);
  78. }
  79. event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK);
  80. fired = 0;
  81. space = num_pipes / num_active;
  82. space = space * 2;
  83. for (i = 0; i < num_active; i++, fired++)
  84. write(pipes[i * space + 1], "e", 1);
  85. count = 0;
  86. writes = num_writes;
  87. { int xcount = 0;
  88. gettimeofday(&ts, NULL);
  89. do {
  90. event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK);
  91. xcount++;
  92. } while (count != fired);
  93. gettimeofday(&te, NULL);
  94. if (xcount != count) fprintf(stderr, "Xcount: %d, Rcount: %d\n", xcount, count);
  95. }
  96. timersub(&te, &ts, &te);
  97. return (&te);
  98. }
  99. int
  100. main (int argc, char **argv)
  101. {
  102. struct rlimit rl;
  103. int i, c;
  104. struct timeval *tv;
  105. int *cp;
  106. extern char *optarg;
  107. num_pipes = 100;
  108. num_active = 1;
  109. num_writes = num_pipes;
  110. while ((c = getopt(argc, argv, "n:a:w:")) != -1) {
  111. switch (c) {
  112. case 'n':
  113. num_pipes = atoi(optarg);
  114. break;
  115. case 'a':
  116. num_active = atoi(optarg);
  117. break;
  118. case 'w':
  119. num_writes = atoi(optarg);
  120. break;
  121. default:
  122. fprintf(stderr, "Illegal argument \"%c\"\n", c);
  123. exit(1);
  124. }
  125. }
  126. rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
  127. if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
  128. perror("setrlimit");
  129. exit(1);
  130. }
  131. events = calloc(num_pipes, sizeof(struct event));
  132. pipes = calloc(num_pipes * 2, sizeof(int));
  133. if (events == NULL || pipes == NULL) {
  134. perror("malloc");
  135. exit(1);
  136. }
  137. event_init();
  138. for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
  139. #ifdef USE_PIPES
  140. if (pipe(cp) == -1) {
  141. #else
  142. if (socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
  143. #endif
  144. perror("pipe");
  145. exit(1);
  146. }
  147. }
  148. for (i = 0; i < 25; i++) {
  149. tv = run_once();
  150. if (tv == NULL)
  151. exit(1);
  152. fprintf(stdout, "%ld\n",
  153. tv->tv_sec * 1000000L + tv->tv_usec);
  154. }
  155. exit(0);
  156. }