blob: 0591be008f2aeba41ba33dcdb71c75f01e0374e6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Hitoshi Mitakec7d93002009-11-05 09:31:33 +09002/*
3 *
Hitoshi Mitake20442792009-12-13 17:01:59 +09004 * sched-pipe.c
Hitoshi Mitakec7d93002009-11-05 09:31:33 +09005 *
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>
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090011 */
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090012#include "../perf.h"
13#include "../util/util.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060014#include <subcmd/parse-options.h>
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090015#include "../builtin.h"
16#include "bench.h"
17
18#include <unistd.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <signal.h>
22#include <sys/wait.h>
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090023#include <string.h>
24#include <errno.h>
25#include <assert.h>
26#include <sys/time.h>
Hitoshi Mitake5ff0cfc2009-11-09 12:31:05 +090027#include <sys/types.h>
Vineet Guptaea1fe3a2015-01-13 19:13:22 +053028#include <sys/syscall.h>
Arnaldo Carvalho de Melo16633cc2016-08-08 14:51:30 -030029#include <linux/time64.h>
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090030
Ingo Molnara9faa0c2013-09-23 10:17:35 +020031#include <pthread.h>
32
33struct thread_data {
34 int nr;
35 int pipe_read;
36 int pipe_write;
37 pthread_t pthread;
38};
39
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090040#define LOOPS_DEFAULT 1000000
Ingo Molnara9faa0c2013-09-23 10:17:35 +020041static int loops = LOOPS_DEFAULT;
42
43/* Use processes by default: */
44static bool threaded;
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090045
46static const struct option options[] = {
Ingo Molnara9faa0c2013-09-23 10:17:35 +020047 OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
48 OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090049 OPT_END()
50};
51
52static const char * const bench_sched_pipe_usage[] = {
53 "perf bench sched pipe <options>",
54 NULL
55};
56
Ingo Molnara9faa0c2013-09-23 10:17:35 +020057static void *worker_thread(void *__tdata)
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090058{
Ingo Molnara9faa0c2013-09-23 10:17:35 +020059 struct thread_data *td = __tdata;
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090060 int m = 0, i;
Ingo Molnara9faa0c2013-09-23 10:17:35 +020061 int ret;
62
63 for (i = 0; i < loops; i++) {
64 if (!td->nr) {
65 ret = read(td->pipe_read, &m, sizeof(int));
66 BUG_ON(ret != sizeof(int));
67 ret = write(td->pipe_write, &m, sizeof(int));
68 BUG_ON(ret != sizeof(int));
69 } else {
70 ret = write(td->pipe_write, &m, sizeof(int));
71 BUG_ON(ret != sizeof(int));
72 ret = read(td->pipe_read, &m, sizeof(int));
73 BUG_ON(ret != sizeof(int));
74 }
75 }
76
77 return NULL;
78}
79
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -030080int bench_sched_pipe(int argc, const char **argv)
Ingo Molnara9faa0c2013-09-23 10:17:35 +020081{
82 struct thread_data threads[2], *td;
83 int pipe_1[2], pipe_2[2];
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090084 struct timeval start, stop, diff;
85 unsigned long long result_usec = 0;
Ingo Molnara9faa0c2013-09-23 10:17:35 +020086 int nr_threads = 2;
87 int t;
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090088
89 /*
90 * why does "ret" exist?
91 * discarding returned value of read(), write()
92 * causes error in building environment for perf
93 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +030094 int __maybe_unused ret, wait_stat;
95 pid_t pid, retpid __maybe_unused;
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090096
Ingo Molnara9faa0c2013-09-23 10:17:35 +020097 argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090098
Irina Tirdea8bf98b82012-09-08 08:35:51 +030099 BUG_ON(pipe(pipe_1));
100 BUG_ON(pipe(pipe_2));
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900101
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900102 gettimeofday(&start, NULL);
103
Ingo Molnara9faa0c2013-09-23 10:17:35 +0200104 for (t = 0; t < nr_threads; t++) {
105 td = threads + t;
106
107 td->nr = t;
108
109 if (t == 0) {
110 td->pipe_read = pipe_1[0];
111 td->pipe_write = pipe_2[1];
112 } else {
113 td->pipe_write = pipe_1[1];
114 td->pipe_read = pipe_2[0];
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900115 }
Ingo Molnara9faa0c2013-09-23 10:17:35 +0200116 }
117
118
119 if (threaded) {
120
121 for (t = 0; t < nr_threads; t++) {
122 td = threads + t;
123
124 ret = pthread_create(&td->pthread, NULL, worker_thread, td);
125 BUG_ON(ret);
126 }
127
128 for (t = 0; t < nr_threads; t++) {
129 td = threads + t;
130
131 ret = pthread_join(td->pthread, NULL);
132 BUG_ON(ret);
133 }
134
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900135 } else {
Ingo Molnara9faa0c2013-09-23 10:17:35 +0200136 pid = fork();
137 assert(pid >= 0);
138
139 if (!pid) {
140 worker_thread(threads + 0);
141 exit(0);
142 } else {
143 worker_thread(threads + 1);
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900144 }
Ingo Molnara9faa0c2013-09-23 10:17:35 +0200145
146 retpid = waitpid(pid, &wait_stat, 0);
147 assert((retpid == pid) && WIFEXITED(wait_stat));
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900148 }
149
150 gettimeofday(&stop, NULL);
151 timersub(&stop, &start, &diff);
152
Hitoshi Mitake158ba822009-11-10 08:20:02 +0900153 switch (bench_format) {
154 case BENCH_FORMAT_DEFAULT:
Ingo Molnara9faa0c2013-09-23 10:17:35 +0200155 printf("# Executed %d pipe operations between two %s\n\n",
156 loops, threaded ? "threads" : "processes");
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900157
Arnaldo Carvalho de Melo16633cc2016-08-08 14:51:30 -0300158 result_usec = diff.tv_sec * USEC_PER_SEC;
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900159 result_usec += diff.tv_usec;
160
Hitoshi Mitakeff676b12009-11-11 00:04:01 +0900161 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
David Miller2cd90462009-12-13 23:56:22 -0800162 diff.tv_sec,
Arnaldo Carvalho de Melo16633cc2016-08-08 14:51:30 -0300163 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
Hitoshi Mitakeff676b12009-11-11 00:04:01 +0900164
165 printf(" %14lf usecs/op\n",
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900166 (double)result_usec / (double)loops);
Hitoshi Mitakeff676b12009-11-11 00:04:01 +0900167 printf(" %14d ops/sec\n",
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900168 (int)((double)loops /
Arnaldo Carvalho de Melo16633cc2016-08-08 14:51:30 -0300169 ((double)result_usec / (double)USEC_PER_SEC)));
Hitoshi Mitake158ba822009-11-10 08:20:02 +0900170 break;
171
172 case BENCH_FORMAT_SIMPLE:
173 printf("%lu.%03lu\n",
David Miller2cd90462009-12-13 23:56:22 -0800174 diff.tv_sec,
Arnaldo Carvalho de Melo16633cc2016-08-08 14:51:30 -0300175 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
Hitoshi Mitake158ba822009-11-10 08:20:02 +0900176 break;
177
178 default:
179 /* reaching here is something disaster */
180 fprintf(stderr, "Unknown format:%d\n", bench_format);
181 exit(1);
182 break;
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900183 }
184
185 return 0;
186}