sched-messaging.c 8.3 KB

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