sched-messaging.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. *
  3. * sched-messaging.c
  4. *
  5. * messaging: Benchmark for scheduler and IPC mechanisms
  6. *
  7. * Based on hackbench by Rusty Russell <rusty@rustcorp.com.au>
  8. * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
  9. *
  10. */
  11. #include "../perf.h"
  12. #include "../util/util.h"
  13. #include "../util/parse-options.h"
  14. #include "../builtin.h"
  15. #include "bench.h"
  16. /* Test groups of 20 processes spraying to 20 receivers */
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <sys/wait.h>
  26. #include <sys/time.h>
  27. #include <poll.h>
  28. #include <limits.h>
  29. #include <err.h>
  30. #define DATASIZE 100
  31. static bool use_pipes = false;
  32. static unsigned int nr_loops = 100;
  33. static bool thread_mode = false;
  34. static unsigned int num_groups = 10;
  35. struct sender_context {
  36. unsigned int num_fds;
  37. int ready_out;
  38. int wakefd;
  39. int out_fds[0];
  40. };
  41. struct receiver_context {
  42. unsigned int num_packets;
  43. int in_fds[2];
  44. int ready_out;
  45. int wakefd;
  46. };
  47. static void fdpair(int fds[2])
  48. {
  49. if (use_pipes) {
  50. if (pipe(fds) == 0)
  51. return;
  52. } else {
  53. if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0)
  54. return;
  55. }
  56. err(EXIT_FAILURE, use_pipes ? "pipe()" : "socketpair()");
  57. }
  58. /* Block until we're ready to go */
  59. static void ready(int ready_out, int wakefd)
  60. {
  61. char dummy;
  62. struct pollfd pollfd = { .fd = wakefd, .events = POLLIN };
  63. /* Tell them we're ready. */
  64. if (write(ready_out, &dummy, 1) != 1)
  65. err(EXIT_FAILURE, "CLIENT: ready write");
  66. /* Wait for "GO" signal */
  67. if (poll(&pollfd, 1, -1) != 1)
  68. err(EXIT_FAILURE, "poll");
  69. }
  70. /* Sender sprays nr_loops messages down each file descriptor */
  71. static void *sender(struct sender_context *ctx)
  72. {
  73. char data[DATASIZE];
  74. unsigned int i, j;
  75. ready(ctx->ready_out, ctx->wakefd);
  76. /* Now pump to every receiver. */
  77. for (i = 0; i < nr_loops; i++) {
  78. for (j = 0; j < ctx->num_fds; j++) {
  79. int ret, done = 0;
  80. again:
  81. ret = write(ctx->out_fds[j], data + done,
  82. sizeof(data)-done);
  83. if (ret < 0)
  84. err(EXIT_FAILURE, "SENDER: write");
  85. done += ret;
  86. if (done < DATASIZE)
  87. goto again;
  88. }
  89. }
  90. return NULL;
  91. }
  92. /* One receiver per fd */
  93. static void *receiver(struct receiver_context* ctx)
  94. {
  95. unsigned int i;
  96. if (!thread_mode)
  97. close(ctx->in_fds[1]);
  98. /* Wait for start... */
  99. ready(ctx->ready_out, ctx->wakefd);
  100. /* Receive them all */
  101. for (i = 0; i < ctx->num_packets; i++) {
  102. char data[DATASIZE];
  103. int ret, done = 0;
  104. again:
  105. ret = read(ctx->in_fds[0], data + done, DATASIZE - done);
  106. if (ret < 0)
  107. err(EXIT_FAILURE, "SERVER: read");
  108. done += ret;
  109. if (done < DATASIZE)
  110. goto again;
  111. }
  112. return NULL;
  113. }
  114. static pthread_t create_worker(void *ctx, void *(*func)(void *))
  115. {
  116. pthread_attr_t attr;
  117. pthread_t childid;
  118. int ret;
  119. if (!thread_mode) {
  120. /* process mode */
  121. /* Fork the receiver. */
  122. switch (fork()) {
  123. case -1:
  124. err(EXIT_FAILURE, "fork()");
  125. break;
  126. case 0:
  127. (*func) (ctx);
  128. exit(0);
  129. break;
  130. default:
  131. break;
  132. }
  133. return (pthread_t)0;
  134. }
  135. if (pthread_attr_init(&attr) != 0)
  136. err(EXIT_FAILURE, "pthread_attr_init:");
  137. #ifndef __ia64__
  138. if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0)
  139. err(EXIT_FAILURE, "pthread_attr_setstacksize");
  140. #endif
  141. ret = pthread_create(&childid, &attr, func, ctx);
  142. if (ret != 0)
  143. err(EXIT_FAILURE, "pthread_create failed");
  144. return childid;
  145. }
  146. static void reap_worker(pthread_t id)
  147. {
  148. int proc_status;
  149. void *thread_status;
  150. if (!thread_mode) {
  151. /* process mode */
  152. wait(&proc_status);
  153. if (!WIFEXITED(proc_status))
  154. exit(1);
  155. } else {
  156. pthread_join(id, &thread_status);
  157. }
  158. }
  159. /* One group of senders and receivers */
  160. static unsigned int group(pthread_t *pth,
  161. unsigned int num_fds,
  162. int ready_out,
  163. int wakefd)
  164. {
  165. unsigned int i;
  166. struct sender_context *snd_ctx = malloc(sizeof(struct sender_context)
  167. + num_fds * sizeof(int));
  168. if (!snd_ctx)
  169. err(EXIT_FAILURE, "malloc()");
  170. for (i = 0; i < num_fds; i++) {
  171. int fds[2];
  172. struct receiver_context *ctx = malloc(sizeof(*ctx));
  173. if (!ctx)
  174. err(EXIT_FAILURE, "malloc()");
  175. /* Create the pipe between client and server */
  176. fdpair(fds);
  177. ctx->num_packets = num_fds * nr_loops;
  178. ctx->in_fds[0] = fds[0];
  179. ctx->in_fds[1] = fds[1];
  180. ctx->ready_out = ready_out;
  181. ctx->wakefd = wakefd;
  182. pth[i] = create_worker(ctx, (void *)receiver);
  183. snd_ctx->out_fds[i] = fds[1];
  184. if (!thread_mode)
  185. close(fds[0]);
  186. }
  187. /* Now we have all the fds, fork the senders */
  188. for (i = 0; i < num_fds; i++) {
  189. snd_ctx->ready_out = ready_out;
  190. snd_ctx->wakefd = wakefd;
  191. snd_ctx->num_fds = num_fds;
  192. pth[num_fds+i] = create_worker(snd_ctx, (void *)sender);
  193. }
  194. /* Close the fds we have left */
  195. if (!thread_mode)
  196. for (i = 0; i < num_fds; i++)
  197. close(snd_ctx->out_fds[i]);
  198. /* Return number of children to reap */
  199. return num_fds * 2;
  200. }
  201. static const struct option options[] = {
  202. OPT_BOOLEAN('p', "pipe", &use_pipes,
  203. "Use pipe() instead of socketpair()"),
  204. OPT_BOOLEAN('t', "thread", &thread_mode,
  205. "Be multi thread instead of multi process"),
  206. OPT_UINTEGER('g', "group", &num_groups, "Specify number of groups"),
  207. OPT_UINTEGER('l', "nr_loops", &nr_loops, "Specify the number of loops to run (default: 100)"),
  208. OPT_END()
  209. };
  210. static const char * const bench_sched_message_usage[] = {
  211. "perf bench sched messaging <options>",
  212. NULL
  213. };
  214. int bench_sched_messaging(int argc, const char **argv,
  215. const char *prefix __maybe_unused)
  216. {
  217. unsigned int i, total_children;
  218. struct timeval start, stop, diff;
  219. unsigned int num_fds = 20;
  220. int readyfds[2], wakefds[2];
  221. char dummy;
  222. pthread_t *pth_tab;
  223. argc = parse_options(argc, argv, options,
  224. bench_sched_message_usage, 0);
  225. pth_tab = malloc(num_fds * 2 * num_groups * sizeof(pthread_t));
  226. if (!pth_tab)
  227. err(EXIT_FAILURE, "main:malloc()");
  228. fdpair(readyfds);
  229. fdpair(wakefds);
  230. total_children = 0;
  231. for (i = 0; i < num_groups; i++)
  232. total_children += group(pth_tab+total_children, num_fds,
  233. readyfds[1], wakefds[0]);
  234. /* Wait for everyone to be ready */
  235. for (i = 0; i < total_children; i++)
  236. if (read(readyfds[0], &dummy, 1) != 1)
  237. err(EXIT_FAILURE, "Reading for readyfds");
  238. gettimeofday(&start, NULL);
  239. /* Kick them off */
  240. if (write(wakefds[1], &dummy, 1) != 1)
  241. err(EXIT_FAILURE, "Writing to start them");
  242. /* Reap them all */
  243. for (i = 0; i < total_children; i++)
  244. reap_worker(pth_tab[i]);
  245. gettimeofday(&stop, NULL);
  246. timersub(&stop, &start, &diff);
  247. switch (bench_format) {
  248. case BENCH_FORMAT_DEFAULT:
  249. printf("# %d sender and receiver %s per group\n",
  250. num_fds, thread_mode ? "threads" : "processes");
  251. printf("# %d groups == %d %s run\n\n",
  252. num_groups, num_groups * 2 * num_fds,
  253. thread_mode ? "threads" : "processes");
  254. printf(" %14s: %lu.%03lu [sec]\n", "Total time",
  255. diff.tv_sec,
  256. (unsigned long) (diff.tv_usec/1000));
  257. break;
  258. case BENCH_FORMAT_SIMPLE:
  259. printf("%lu.%03lu\n", diff.tv_sec,
  260. (unsigned long) (diff.tv_usec/1000));
  261. break;
  262. default:
  263. /* reaching here is something disaster */
  264. fprintf(stderr, "Unknown format:%d\n", bench_format);
  265. exit(1);
  266. break;
  267. }
  268. free(pth_tab);
  269. return 0;
  270. }