Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *
0004  * sched-pipe.c
0005  *
0006  * pipe: Benchmark for pipe()
0007  *
0008  * Based on pipe-test-1m.c by Ingo Molnar <mingo@redhat.com>
0009  *  http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c
0010  * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
0011  */
0012 #include <subcmd/parse-options.h>
0013 #include "bench.h"
0014 
0015 #include <unistd.h>
0016 #include <stdio.h>
0017 #include <stdlib.h>
0018 #include <signal.h>
0019 #include <sys/wait.h>
0020 #include <string.h>
0021 #include <errno.h>
0022 #include <assert.h>
0023 #include <sys/time.h>
0024 #include <sys/types.h>
0025 #include <sys/syscall.h>
0026 #include <linux/time64.h>
0027 
0028 #include <pthread.h>
0029 
0030 struct thread_data {
0031     int         nr;
0032     int         pipe_read;
0033     int         pipe_write;
0034     pthread_t       pthread;
0035 };
0036 
0037 #define LOOPS_DEFAULT 1000000
0038 static  int         loops = LOOPS_DEFAULT;
0039 
0040 /* Use processes by default: */
0041 static bool         threaded;
0042 
0043 static const struct option options[] = {
0044     OPT_INTEGER('l', "loop",    &loops,     "Specify number of loops"),
0045     OPT_BOOLEAN('T', "threaded",    &threaded,  "Specify threads/process based task setup"),
0046     OPT_END()
0047 };
0048 
0049 static const char * const bench_sched_pipe_usage[] = {
0050     "perf bench sched pipe <options>",
0051     NULL
0052 };
0053 
0054 static void *worker_thread(void *__tdata)
0055 {
0056     struct thread_data *td = __tdata;
0057     int m = 0, i;
0058     int ret;
0059 
0060     for (i = 0; i < loops; i++) {
0061         if (!td->nr) {
0062             ret = read(td->pipe_read, &m, sizeof(int));
0063             BUG_ON(ret != sizeof(int));
0064             ret = write(td->pipe_write, &m, sizeof(int));
0065             BUG_ON(ret != sizeof(int));
0066         } else {
0067             ret = write(td->pipe_write, &m, sizeof(int));
0068             BUG_ON(ret != sizeof(int));
0069             ret = read(td->pipe_read, &m, sizeof(int));
0070             BUG_ON(ret != sizeof(int));
0071         }
0072     }
0073 
0074     return NULL;
0075 }
0076 
0077 int bench_sched_pipe(int argc, const char **argv)
0078 {
0079     struct thread_data threads[2], *td;
0080     int pipe_1[2], pipe_2[2];
0081     struct timeval start, stop, diff;
0082     unsigned long long result_usec = 0;
0083     int nr_threads = 2;
0084     int t;
0085 
0086     /*
0087      * why does "ret" exist?
0088      * discarding returned value of read(), write()
0089      * causes error in building environment for perf
0090      */
0091     int __maybe_unused ret, wait_stat;
0092     pid_t pid, retpid __maybe_unused;
0093 
0094     argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
0095 
0096     BUG_ON(pipe(pipe_1));
0097     BUG_ON(pipe(pipe_2));
0098 
0099     gettimeofday(&start, NULL);
0100 
0101     for (t = 0; t < nr_threads; t++) {
0102         td = threads + t;
0103 
0104         td->nr = t;
0105 
0106         if (t == 0) {
0107             td->pipe_read = pipe_1[0];
0108             td->pipe_write = pipe_2[1];
0109         } else {
0110             td->pipe_write = pipe_1[1];
0111             td->pipe_read = pipe_2[0];
0112         }
0113     }
0114 
0115 
0116     if (threaded) {
0117 
0118         for (t = 0; t < nr_threads; t++) {
0119             td = threads + t;
0120 
0121             ret = pthread_create(&td->pthread, NULL, worker_thread, td);
0122             BUG_ON(ret);
0123         }
0124 
0125         for (t = 0; t < nr_threads; t++) {
0126             td = threads + t;
0127 
0128             ret = pthread_join(td->pthread, NULL);
0129             BUG_ON(ret);
0130         }
0131 
0132     } else {
0133         pid = fork();
0134         assert(pid >= 0);
0135 
0136         if (!pid) {
0137             worker_thread(threads + 0);
0138             exit(0);
0139         } else {
0140             worker_thread(threads + 1);
0141         }
0142 
0143         retpid = waitpid(pid, &wait_stat, 0);
0144         assert((retpid == pid) && WIFEXITED(wait_stat));
0145     }
0146 
0147     gettimeofday(&stop, NULL);
0148     timersub(&stop, &start, &diff);
0149 
0150     switch (bench_format) {
0151     case BENCH_FORMAT_DEFAULT:
0152         printf("# Executed %d pipe operations between two %s\n\n",
0153             loops, threaded ? "threads" : "processes");
0154 
0155         result_usec = diff.tv_sec * USEC_PER_SEC;
0156         result_usec += diff.tv_usec;
0157 
0158         printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
0159                (unsigned long) diff.tv_sec,
0160                (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
0161 
0162         printf(" %14lf usecs/op\n",
0163                (double)result_usec / (double)loops);
0164         printf(" %14d ops/sec\n",
0165                (int)((double)loops /
0166                  ((double)result_usec / (double)USEC_PER_SEC)));
0167         break;
0168 
0169     case BENCH_FORMAT_SIMPLE:
0170         printf("%lu.%03lu\n",
0171                (unsigned long) diff.tv_sec,
0172                (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
0173         break;
0174 
0175     default:
0176         /* reaching here is something disaster */
0177         fprintf(stderr, "Unknown format:%d\n", bench_format);
0178         exit(1);
0179         break;
0180     }
0181 
0182     return 0;
0183 }