Merge remote-tracking branch 'xen-tip/linux-next'
[deliverable/linux.git] / tools / perf / bench / sched-pipe.c
CommitLineData
c7d9300f
HM
1/*
2 *
2044279d 3 * sched-pipe.c
c7d9300f
HM
4 *
5 * pipe: Benchmark for pipe()
6 *
7 * Based on pipe-test-1m.c by Ingo Molnar <mingo@redhat.com>
8 * http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c
9 * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
c7d9300f 10 */
c7d9300f
HM
11#include "../perf.h"
12#include "../util/util.h"
4b6ab94e 13#include <subcmd/parse-options.h>
c7d9300f
HM
14#include "../builtin.h"
15#include "bench.h"
16
17#include <unistd.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <signal.h>
21#include <sys/wait.h>
c7d9300f
HM
22#include <string.h>
23#include <errno.h>
24#include <assert.h>
25#include <sys/time.h>
5ff0cfc6 26#include <sys/types.h>
ea1fe3a8 27#include <sys/syscall.h>
16633ccf 28#include <linux/time64.h>
c7d9300f 29
a9faa0ca
IM
30#include <pthread.h>
31
32struct thread_data {
33 int nr;
34 int pipe_read;
35 int pipe_write;
36 pthread_t pthread;
37};
38
c7d9300f 39#define LOOPS_DEFAULT 1000000
a9faa0ca
IM
40static int loops = LOOPS_DEFAULT;
41
42/* Use processes by default: */
43static bool threaded;
c7d9300f
HM
44
45static const struct option options[] = {
a9faa0ca
IM
46 OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
47 OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
c7d9300f
HM
48 OPT_END()
49};
50
51static const char * const bench_sched_pipe_usage[] = {
52 "perf bench sched pipe <options>",
53 NULL
54};
55
a9faa0ca 56static void *worker_thread(void *__tdata)
c7d9300f 57{
a9faa0ca 58 struct thread_data *td = __tdata;
c7d9300f 59 int m = 0, i;
a9faa0ca
IM
60 int ret;
61
62 for (i = 0; i < loops; i++) {
63 if (!td->nr) {
64 ret = read(td->pipe_read, &m, sizeof(int));
65 BUG_ON(ret != sizeof(int));
66 ret = write(td->pipe_write, &m, sizeof(int));
67 BUG_ON(ret != sizeof(int));
68 } else {
69 ret = write(td->pipe_write, &m, sizeof(int));
70 BUG_ON(ret != sizeof(int));
71 ret = read(td->pipe_read, &m, sizeof(int));
72 BUG_ON(ret != sizeof(int));
73 }
74 }
75
76 return NULL;
77}
78
79int bench_sched_pipe(int argc, const char **argv, const char *prefix __maybe_unused)
80{
81 struct thread_data threads[2], *td;
82 int pipe_1[2], pipe_2[2];
c7d9300f
HM
83 struct timeval start, stop, diff;
84 unsigned long long result_usec = 0;
a9faa0ca
IM
85 int nr_threads = 2;
86 int t;
c7d9300f
HM
87
88 /*
89 * why does "ret" exist?
90 * discarding returned value of read(), write()
91 * causes error in building environment for perf
92 */
1d037ca1
IT
93 int __maybe_unused ret, wait_stat;
94 pid_t pid, retpid __maybe_unused;
c7d9300f 95
a9faa0ca 96 argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
c7d9300f 97
8bf98b89
IT
98 BUG_ON(pipe(pipe_1));
99 BUG_ON(pipe(pipe_2));
c7d9300f 100
c7d9300f
HM
101 gettimeofday(&start, NULL);
102
a9faa0ca
IM
103 for (t = 0; t < nr_threads; t++) {
104 td = threads + t;
105
106 td->nr = t;
107
108 if (t == 0) {
109 td->pipe_read = pipe_1[0];
110 td->pipe_write = pipe_2[1];
111 } else {
112 td->pipe_write = pipe_1[1];
113 td->pipe_read = pipe_2[0];
c7d9300f
HM
114 }
115 }
116
c7d9300f 117
a9faa0ca
IM
118 if (threaded) {
119
120 for (t = 0; t < nr_threads; t++) {
121 td = threads + t;
122
123 ret = pthread_create(&td->pthread, NULL, worker_thread, td);
124 BUG_ON(ret);
125 }
126
127 for (t = 0; t < nr_threads; t++) {
128 td = threads + t;
129
130 ret = pthread_join(td->pthread, NULL);
131 BUG_ON(ret);
132 }
133
134 } else {
135 pid = fork();
136 assert(pid >= 0);
137
138 if (!pid) {
139 worker_thread(threads + 0);
140 exit(0);
141 } else {
142 worker_thread(threads + 1);
143 }
144
5ff0cfc6
HM
145 retpid = waitpid(pid, &wait_stat, 0);
146 assert((retpid == pid) && WIFEXITED(wait_stat));
5ff0cfc6 147 }
c7d9300f 148
a9faa0ca
IM
149 gettimeofday(&stop, NULL);
150 timersub(&stop, &start, &diff);
151
158ba827
HM
152 switch (bench_format) {
153 case BENCH_FORMAT_DEFAULT:
a9faa0ca
IM
154 printf("# Executed %d pipe operations between two %s\n\n",
155 loops, threaded ? "threads" : "processes");
c7d9300f 156
16633ccf 157 result_usec = diff.tv_sec * USEC_PER_SEC;
c7d9300f
HM
158 result_usec += diff.tv_usec;
159
ff676b19 160 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
2cd9046c 161 diff.tv_sec,
16633ccf 162 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
ff676b19
HM
163
164 printf(" %14lf usecs/op\n",
c7d9300f 165 (double)result_usec / (double)loops);
ff676b19 166 printf(" %14d ops/sec\n",
c7d9300f 167 (int)((double)loops /
16633ccf 168 ((double)result_usec / (double)USEC_PER_SEC)));
158ba827
HM
169 break;
170
171 case BENCH_FORMAT_SIMPLE:
172 printf("%lu.%03lu\n",
2cd9046c 173 diff.tv_sec,
16633ccf 174 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
158ba827
HM
175 break;
176
177 default:
178 /* reaching here is something disaster */
179 fprintf(stderr, "Unknown format:%d\n", bench_format);
180 exit(1);
181 break;
c7d9300f
HM
182 }
183
184 return 0;
185}
This page took 0.317293 seconds and 5 git commands to generate.