blob: 3c88d1f201f1cf81393edf432c90024f8bbe02d2 [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 */
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060012#include <subcmd/parse-options.h>
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090013#include "bench.h"
14
15#include <unistd.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <signal.h>
19#include <sys/wait.h>
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090020#include <string.h>
21#include <errno.h>
22#include <assert.h>
23#include <sys/time.h>
Hitoshi Mitake5ff0cfc2009-11-09 12:31:05 +090024#include <sys/types.h>
Vineet Guptaea1fe3a2015-01-13 19:13:22 +053025#include <sys/syscall.h>
Arnaldo Carvalho de Melo16633cc2016-08-08 14:51:30 -030026#include <linux/time64.h>
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090027
Ingo Molnara9faa0c2013-09-23 10:17:35 +020028#include <pthread.h>
29
30struct thread_data {
31 int nr;
32 int pipe_read;
33 int pipe_write;
34 pthread_t pthread;
35};
36
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090037#define LOOPS_DEFAULT 1000000
Ingo Molnara9faa0c2013-09-23 10:17:35 +020038static int loops = LOOPS_DEFAULT;
39
40/* Use processes by default: */
41static bool threaded;
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090042
43static const struct option options[] = {
Ingo Molnara9faa0c2013-09-23 10:17:35 +020044 OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
45 OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090046 OPT_END()
47};
48
49static const char * const bench_sched_pipe_usage[] = {
50 "perf bench sched pipe <options>",
51 NULL
52};
53
Ingo Molnara9faa0c2013-09-23 10:17:35 +020054static void *worker_thread(void *__tdata)
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090055{
Ingo Molnara9faa0c2013-09-23 10:17:35 +020056 struct thread_data *td = __tdata;
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090057 int m = 0, i;
Ingo Molnara9faa0c2013-09-23 10:17:35 +020058 int ret;
59
60 for (i = 0; i < loops; i++) {
61 if (!td->nr) {
62 ret = read(td->pipe_read, &m, sizeof(int));
63 BUG_ON(ret != sizeof(int));
64 ret = write(td->pipe_write, &m, sizeof(int));
65 BUG_ON(ret != sizeof(int));
66 } else {
67 ret = write(td->pipe_write, &m, sizeof(int));
68 BUG_ON(ret != sizeof(int));
69 ret = read(td->pipe_read, &m, sizeof(int));
70 BUG_ON(ret != sizeof(int));
71 }
72 }
73
74 return NULL;
75}
76
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -030077int bench_sched_pipe(int argc, const char **argv)
Ingo Molnara9faa0c2013-09-23 10:17:35 +020078{
79 struct thread_data threads[2], *td;
80 int pipe_1[2], pipe_2[2];
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090081 struct timeval start, stop, diff;
82 unsigned long long result_usec = 0;
Ingo Molnara9faa0c2013-09-23 10:17:35 +020083 int nr_threads = 2;
84 int t;
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090085
86 /*
87 * why does "ret" exist?
88 * discarding returned value of read(), write()
89 * causes error in building environment for perf
90 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +030091 int __maybe_unused ret, wait_stat;
92 pid_t pid, retpid __maybe_unused;
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090093
Ingo Molnara9faa0c2013-09-23 10:17:35 +020094 argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090095
Irina Tirdea8bf98b82012-09-08 08:35:51 +030096 BUG_ON(pipe(pipe_1));
97 BUG_ON(pipe(pipe_2));
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090098
Hitoshi Mitakec7d93002009-11-05 09:31:33 +090099 gettimeofday(&start, NULL);
100
Ingo Molnara9faa0c2013-09-23 10:17:35 +0200101 for (t = 0; t < nr_threads; t++) {
102 td = threads + t;
103
104 td->nr = t;
105
106 if (t == 0) {
107 td->pipe_read = pipe_1[0];
108 td->pipe_write = pipe_2[1];
109 } else {
110 td->pipe_write = pipe_1[1];
111 td->pipe_read = pipe_2[0];
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900112 }
Ingo Molnara9faa0c2013-09-23 10:17:35 +0200113 }
114
115
116 if (threaded) {
117
118 for (t = 0; t < nr_threads; t++) {
119 td = threads + t;
120
121 ret = pthread_create(&td->pthread, NULL, worker_thread, td);
122 BUG_ON(ret);
123 }
124
125 for (t = 0; t < nr_threads; t++) {
126 td = threads + t;
127
128 ret = pthread_join(td->pthread, NULL);
129 BUG_ON(ret);
130 }
131
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900132 } else {
Ingo Molnara9faa0c2013-09-23 10:17:35 +0200133 pid = fork();
134 assert(pid >= 0);
135
136 if (!pid) {
137 worker_thread(threads + 0);
138 exit(0);
139 } else {
140 worker_thread(threads + 1);
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900141 }
Ingo Molnara9faa0c2013-09-23 10:17:35 +0200142
143 retpid = waitpid(pid, &wait_stat, 0);
144 assert((retpid == pid) && WIFEXITED(wait_stat));
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900145 }
146
147 gettimeofday(&stop, NULL);
148 timersub(&stop, &start, &diff);
149
Hitoshi Mitake158ba822009-11-10 08:20:02 +0900150 switch (bench_format) {
151 case BENCH_FORMAT_DEFAULT:
Ingo Molnara9faa0c2013-09-23 10:17:35 +0200152 printf("# Executed %d pipe operations between two %s\n\n",
153 loops, threaded ? "threads" : "processes");
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900154
Arnaldo Carvalho de Melo16633cc2016-08-08 14:51:30 -0300155 result_usec = diff.tv_sec * USEC_PER_SEC;
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900156 result_usec += diff.tv_usec;
157
Hitoshi Mitakeff676b12009-11-11 00:04:01 +0900158 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
David Miller2cd90462009-12-13 23:56:22 -0800159 diff.tv_sec,
Arnaldo Carvalho de Melo16633cc2016-08-08 14:51:30 -0300160 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
Hitoshi Mitakeff676b12009-11-11 00:04:01 +0900161
162 printf(" %14lf usecs/op\n",
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900163 (double)result_usec / (double)loops);
Hitoshi Mitakeff676b12009-11-11 00:04:01 +0900164 printf(" %14d ops/sec\n",
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900165 (int)((double)loops /
Arnaldo Carvalho de Melo16633cc2016-08-08 14:51:30 -0300166 ((double)result_usec / (double)USEC_PER_SEC)));
Hitoshi Mitake158ba822009-11-10 08:20:02 +0900167 break;
168
169 case BENCH_FORMAT_SIMPLE:
170 printf("%lu.%03lu\n",
David Miller2cd90462009-12-13 23:56:22 -0800171 diff.tv_sec,
Arnaldo Carvalho de Melo16633cc2016-08-08 14:51:30 -0300172 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
Hitoshi Mitake158ba822009-11-10 08:20:02 +0900173 break;
174
175 default:
176 /* reaching here is something disaster */
177 fprintf(stderr, "Unknown format:%d\n", bench_format);
178 exit(1);
179 break;
Hitoshi Mitakec7d93002009-11-05 09:31:33 +0900180 }
181
182 return 0;
183}