sched-pipe.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * sched-pipe.c
  5. *
  6. * pipe: Benchmark for pipe()
  7. *
  8. * Based on pipe-test-1m.c by Ingo Molnar <mingo@redhat.com>
  9. * http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c
  10. * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
  11. */
  12. #include <subcmd/parse-options.h>
  13. #include <api/fs/fs.h>
  14. #include "bench.h"
  15. #include "util/cgroup.h"
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <signal.h>
  20. #include <sys/wait.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <assert.h>
  25. #include <sys/time.h>
  26. #include <sys/types.h>
  27. #include <sys/syscall.h>
  28. #include <linux/time64.h>
  29. #include <pthread.h>
  30. struct thread_data {
  31. int nr;
  32. int pipe_read;
  33. int pipe_write;
  34. bool cgroup_failed;
  35. pthread_t pthread;
  36. };
  37. #define LOOPS_DEFAULT 1000000
  38. static int loops = LOOPS_DEFAULT;
  39. /* Use processes by default: */
  40. static bool threaded;
  41. static char *cgrp_names[2];
  42. static struct cgroup *cgrps[2];
  43. static int parse_two_cgroups(const struct option *opt __maybe_unused,
  44. const char *str, int unset __maybe_unused)
  45. {
  46. char *p = strdup(str);
  47. char *q;
  48. int ret = -1;
  49. if (p == NULL) {
  50. fprintf(stderr, "memory allocation failure\n");
  51. return -1;
  52. }
  53. q = strchr(p, ',');
  54. if (q == NULL) {
  55. fprintf(stderr, "it should have two cgroup names: %s\n", p);
  56. goto out;
  57. }
  58. *q = '\0';
  59. cgrp_names[0] = strdup(p);
  60. cgrp_names[1] = strdup(q + 1);
  61. if (cgrp_names[0] == NULL || cgrp_names[1] == NULL) {
  62. fprintf(stderr, "memory allocation failure\n");
  63. goto out;
  64. }
  65. ret = 0;
  66. out:
  67. free(p);
  68. return ret;
  69. }
  70. static const struct option options[] = {
  71. OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
  72. OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
  73. OPT_CALLBACK('G', "cgroups", NULL, "SEND,RECV",
  74. "Put sender and receivers in given cgroups",
  75. parse_two_cgroups),
  76. OPT_END()
  77. };
  78. static const char * const bench_sched_pipe_usage[] = {
  79. "perf bench sched pipe <options>",
  80. NULL
  81. };
  82. static int enter_cgroup(int nr)
  83. {
  84. char buf[32];
  85. int fd, len, ret;
  86. int saved_errno;
  87. struct cgroup *cgrp;
  88. pid_t pid;
  89. if (cgrp_names[nr] == NULL)
  90. return 0;
  91. if (cgrps[nr] == NULL) {
  92. cgrps[nr] = cgroup__new(cgrp_names[nr], /*do_open=*/true);
  93. if (cgrps[nr] == NULL)
  94. goto err;
  95. }
  96. cgrp = cgrps[nr];
  97. if (threaded)
  98. pid = syscall(__NR_gettid);
  99. else
  100. pid = getpid();
  101. snprintf(buf, sizeof(buf), "%d\n", pid);
  102. len = strlen(buf);
  103. /* try cgroup v2 interface first */
  104. if (threaded)
  105. fd = openat(cgrp->fd, "cgroup.threads", O_WRONLY);
  106. else
  107. fd = openat(cgrp->fd, "cgroup.procs", O_WRONLY);
  108. /* try cgroup v1 if failed */
  109. if (fd < 0 && errno == ENOENT)
  110. fd = openat(cgrp->fd, "tasks", O_WRONLY);
  111. if (fd < 0)
  112. goto err;
  113. ret = write(fd, buf, len);
  114. close(fd);
  115. if (ret != len) {
  116. printf("Cannot enter to cgroup: %s\n", cgrp->name);
  117. return -1;
  118. }
  119. return 0;
  120. err:
  121. saved_errno = errno;
  122. printf("Failed to open cgroup file in %s\n", cgrp_names[nr]);
  123. if (saved_errno == ENOENT) {
  124. char mnt[PATH_MAX];
  125. if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event") == 0)
  126. printf(" Hint: create the cgroup first, like 'mkdir %s/%s'\n",
  127. mnt, cgrp_names[nr]);
  128. } else if (saved_errno == EACCES && geteuid() > 0) {
  129. printf(" Hint: try to run as root\n");
  130. }
  131. return -1;
  132. }
  133. static void exit_cgroup(int nr)
  134. {
  135. cgroup__put(cgrps[nr]);
  136. free(cgrp_names[nr]);
  137. }
  138. static void *worker_thread(void *__tdata)
  139. {
  140. struct thread_data *td = __tdata;
  141. int m = 0, i;
  142. int ret;
  143. ret = enter_cgroup(td->nr);
  144. if (ret < 0) {
  145. td->cgroup_failed = true;
  146. return NULL;
  147. }
  148. for (i = 0; i < loops; i++) {
  149. if (!td->nr) {
  150. ret = read(td->pipe_read, &m, sizeof(int));
  151. BUG_ON(ret != sizeof(int));
  152. ret = write(td->pipe_write, &m, sizeof(int));
  153. BUG_ON(ret != sizeof(int));
  154. } else {
  155. ret = write(td->pipe_write, &m, sizeof(int));
  156. BUG_ON(ret != sizeof(int));
  157. ret = read(td->pipe_read, &m, sizeof(int));
  158. BUG_ON(ret != sizeof(int));
  159. }
  160. }
  161. return NULL;
  162. }
  163. int bench_sched_pipe(int argc, const char **argv)
  164. {
  165. struct thread_data threads[2] = {};
  166. struct thread_data *td;
  167. int pipe_1[2], pipe_2[2];
  168. struct timeval start, stop, diff;
  169. unsigned long long result_usec = 0;
  170. int nr_threads = 2;
  171. int t;
  172. /*
  173. * why does "ret" exist?
  174. * discarding returned value of read(), write()
  175. * causes error in building environment for perf
  176. */
  177. int __maybe_unused ret, wait_stat;
  178. pid_t pid, retpid __maybe_unused;
  179. argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
  180. BUG_ON(pipe(pipe_1));
  181. BUG_ON(pipe(pipe_2));
  182. gettimeofday(&start, NULL);
  183. for (t = 0; t < nr_threads; t++) {
  184. td = threads + t;
  185. td->nr = t;
  186. if (t == 0) {
  187. td->pipe_read = pipe_1[0];
  188. td->pipe_write = pipe_2[1];
  189. } else {
  190. td->pipe_write = pipe_1[1];
  191. td->pipe_read = pipe_2[0];
  192. }
  193. }
  194. if (threaded) {
  195. for (t = 0; t < nr_threads; t++) {
  196. td = threads + t;
  197. ret = pthread_create(&td->pthread, NULL, worker_thread, td);
  198. BUG_ON(ret);
  199. }
  200. for (t = 0; t < nr_threads; t++) {
  201. td = threads + t;
  202. ret = pthread_join(td->pthread, NULL);
  203. BUG_ON(ret);
  204. }
  205. } else {
  206. pid = fork();
  207. assert(pid >= 0);
  208. if (!pid) {
  209. worker_thread(threads + 0);
  210. exit(0);
  211. } else {
  212. worker_thread(threads + 1);
  213. }
  214. retpid = waitpid(pid, &wait_stat, 0);
  215. assert((retpid == pid) && WIFEXITED(wait_stat));
  216. }
  217. gettimeofday(&stop, NULL);
  218. timersub(&stop, &start, &diff);
  219. exit_cgroup(0);
  220. exit_cgroup(1);
  221. if (threads[0].cgroup_failed || threads[1].cgroup_failed)
  222. return 0;
  223. switch (bench_format) {
  224. case BENCH_FORMAT_DEFAULT:
  225. printf("# Executed %d pipe operations between two %s\n\n",
  226. loops, threaded ? "threads" : "processes");
  227. result_usec = diff.tv_sec * USEC_PER_SEC;
  228. result_usec += diff.tv_usec;
  229. printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
  230. (unsigned long) diff.tv_sec,
  231. (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
  232. printf(" %14lf usecs/op\n",
  233. (double)result_usec / (double)loops);
  234. printf(" %14d ops/sec\n",
  235. (int)((double)loops /
  236. ((double)result_usec / (double)USEC_PER_SEC)));
  237. break;
  238. case BENCH_FORMAT_SIMPLE:
  239. printf("%lu.%03lu\n",
  240. (unsigned long) diff.tv_sec,
  241. (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
  242. break;
  243. default:
  244. /* reaching here is something disaster */
  245. fprintf(stderr, "Unknown format:%d\n", bench_format);
  246. exit(1);
  247. break;
  248. }
  249. return 0;
  250. }