0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <subcmd/parse-options.h>
0014 #include "bench.h"
0015
0016
0017 #include <pthread.h>
0018 #include <stdio.h>
0019 #include <stdlib.h>
0020 #include <string.h>
0021 #include <errno.h>
0022 #include <unistd.h>
0023 #include <sys/types.h>
0024 #include <sys/socket.h>
0025 #include <sys/wait.h>
0026 #include <sys/time.h>
0027 #include <poll.h>
0028 #include <limits.h>
0029 #include <err.h>
0030 #include <linux/time64.h>
0031
0032 #define DATASIZE 100
0033
0034 static bool use_pipes = false;
0035 static unsigned int nr_loops = 100;
0036 static bool thread_mode = false;
0037 static unsigned int num_groups = 10;
0038
0039 struct sender_context {
0040 unsigned int num_fds;
0041 int ready_out;
0042 int wakefd;
0043 int out_fds[];
0044 };
0045
0046 struct receiver_context {
0047 unsigned int num_packets;
0048 int in_fds[2];
0049 int ready_out;
0050 int wakefd;
0051 };
0052
0053 static void fdpair(int fds[2])
0054 {
0055 if (use_pipes) {
0056 if (pipe(fds) == 0)
0057 return;
0058 } else {
0059 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0)
0060 return;
0061 }
0062
0063 err(EXIT_FAILURE, use_pipes ? "pipe()" : "socketpair()");
0064 }
0065
0066
0067 static void ready(int ready_out, int wakefd)
0068 {
0069 struct pollfd pollfd = { .fd = wakefd, .events = POLLIN };
0070
0071
0072 if (write(ready_out, "R", 1) != 1)
0073 err(EXIT_FAILURE, "CLIENT: ready write");
0074
0075
0076 if (poll(&pollfd, 1, -1) != 1)
0077 err(EXIT_FAILURE, "poll");
0078 }
0079
0080
0081 static void *sender(struct sender_context *ctx)
0082 {
0083 char data[DATASIZE];
0084 unsigned int i, j;
0085
0086 ready(ctx->ready_out, ctx->wakefd);
0087 memset(data, 'S', sizeof(data));
0088
0089
0090 for (i = 0; i < nr_loops; i++) {
0091 for (j = 0; j < ctx->num_fds; j++) {
0092 int ret, done = 0;
0093
0094 again:
0095 ret = write(ctx->out_fds[j], data + done,
0096 sizeof(data)-done);
0097 if (ret < 0)
0098 err(EXIT_FAILURE, "SENDER: write");
0099 done += ret;
0100 if (done < DATASIZE)
0101 goto again;
0102 }
0103 }
0104
0105 return NULL;
0106 }
0107
0108
0109
0110 static void *receiver(struct receiver_context* ctx)
0111 {
0112 unsigned int i;
0113
0114 if (!thread_mode)
0115 close(ctx->in_fds[1]);
0116
0117
0118 ready(ctx->ready_out, ctx->wakefd);
0119
0120
0121 for (i = 0; i < ctx->num_packets; i++) {
0122 char data[DATASIZE];
0123 int ret, done = 0;
0124
0125 again:
0126 ret = read(ctx->in_fds[0], data + done, DATASIZE - done);
0127 if (ret < 0)
0128 err(EXIT_FAILURE, "SERVER: read");
0129 done += ret;
0130 if (done < DATASIZE)
0131 goto again;
0132 }
0133
0134 return NULL;
0135 }
0136
0137 static pthread_t create_worker(void *ctx, void *(*func)(void *))
0138 {
0139 pthread_attr_t attr;
0140 pthread_t childid;
0141 int ret;
0142
0143 if (!thread_mode) {
0144
0145
0146 switch (fork()) {
0147 case -1:
0148 err(EXIT_FAILURE, "fork()");
0149 break;
0150 case 0:
0151 (*func) (ctx);
0152 exit(0);
0153 break;
0154 default:
0155 break;
0156 }
0157
0158 return (pthread_t)0;
0159 }
0160
0161 if (pthread_attr_init(&attr) != 0)
0162 err(EXIT_FAILURE, "pthread_attr_init:");
0163
0164 #ifndef __ia64__
0165 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0)
0166 err(EXIT_FAILURE, "pthread_attr_setstacksize");
0167 #endif
0168
0169 ret = pthread_create(&childid, &attr, func, ctx);
0170 if (ret != 0)
0171 err(EXIT_FAILURE, "pthread_create failed");
0172
0173 return childid;
0174 }
0175
0176 static void reap_worker(pthread_t id)
0177 {
0178 int proc_status;
0179 void *thread_status;
0180
0181 if (!thread_mode) {
0182
0183 wait(&proc_status);
0184 if (!WIFEXITED(proc_status))
0185 exit(1);
0186 } else {
0187 pthread_join(id, &thread_status);
0188 }
0189 }
0190
0191
0192 static unsigned int group(pthread_t *pth,
0193 unsigned int num_fds,
0194 int ready_out,
0195 int wakefd)
0196 {
0197 unsigned int i;
0198 struct sender_context *snd_ctx = malloc(sizeof(struct sender_context)
0199 + num_fds * sizeof(int));
0200
0201 if (!snd_ctx)
0202 err(EXIT_FAILURE, "malloc()");
0203
0204 for (i = 0; i < num_fds; i++) {
0205 int fds[2];
0206 struct receiver_context *ctx = malloc(sizeof(*ctx));
0207
0208 if (!ctx)
0209 err(EXIT_FAILURE, "malloc()");
0210
0211
0212
0213 fdpair(fds);
0214
0215 ctx->num_packets = num_fds * nr_loops;
0216 ctx->in_fds[0] = fds[0];
0217 ctx->in_fds[1] = fds[1];
0218 ctx->ready_out = ready_out;
0219 ctx->wakefd = wakefd;
0220
0221 pth[i] = create_worker(ctx, (void *)receiver);
0222
0223 snd_ctx->out_fds[i] = fds[1];
0224 if (!thread_mode)
0225 close(fds[0]);
0226 }
0227
0228
0229 for (i = 0; i < num_fds; i++) {
0230 snd_ctx->ready_out = ready_out;
0231 snd_ctx->wakefd = wakefd;
0232 snd_ctx->num_fds = num_fds;
0233
0234 pth[num_fds+i] = create_worker(snd_ctx, (void *)sender);
0235 }
0236
0237
0238 if (!thread_mode)
0239 for (i = 0; i < num_fds; i++)
0240 close(snd_ctx->out_fds[i]);
0241
0242
0243 return num_fds * 2;
0244 }
0245
0246 static const struct option options[] = {
0247 OPT_BOOLEAN('p', "pipe", &use_pipes,
0248 "Use pipe() instead of socketpair()"),
0249 OPT_BOOLEAN('t', "thread", &thread_mode,
0250 "Be multi thread instead of multi process"),
0251 OPT_UINTEGER('g', "group", &num_groups, "Specify number of groups"),
0252 OPT_UINTEGER('l', "nr_loops", &nr_loops, "Specify the number of loops to run (default: 100)"),
0253 OPT_END()
0254 };
0255
0256 static const char * const bench_sched_message_usage[] = {
0257 "perf bench sched messaging <options>",
0258 NULL
0259 };
0260
0261 int bench_sched_messaging(int argc, const char **argv)
0262 {
0263 unsigned int i, total_children;
0264 struct timeval start, stop, diff;
0265 unsigned int num_fds = 20;
0266 int readyfds[2], wakefds[2];
0267 char dummy;
0268 pthread_t *pth_tab;
0269
0270 argc = parse_options(argc, argv, options,
0271 bench_sched_message_usage, 0);
0272
0273 pth_tab = malloc(num_fds * 2 * num_groups * sizeof(pthread_t));
0274 if (!pth_tab)
0275 err(EXIT_FAILURE, "main:malloc()");
0276
0277 fdpair(readyfds);
0278 fdpair(wakefds);
0279
0280 total_children = 0;
0281 for (i = 0; i < num_groups; i++)
0282 total_children += group(pth_tab+total_children, num_fds,
0283 readyfds[1], wakefds[0]);
0284
0285
0286 for (i = 0; i < total_children; i++)
0287 if (read(readyfds[0], &dummy, 1) != 1)
0288 err(EXIT_FAILURE, "Reading for readyfds");
0289
0290 gettimeofday(&start, NULL);
0291
0292
0293 if (write(wakefds[1], &dummy, 1) != 1)
0294 err(EXIT_FAILURE, "Writing to start them");
0295
0296
0297 for (i = 0; i < total_children; i++)
0298 reap_worker(pth_tab[i]);
0299
0300 gettimeofday(&stop, NULL);
0301
0302 timersub(&stop, &start, &diff);
0303
0304 switch (bench_format) {
0305 case BENCH_FORMAT_DEFAULT:
0306 printf("# %d sender and receiver %s per group\n",
0307 num_fds, thread_mode ? "threads" : "processes");
0308 printf("# %d groups == %d %s run\n\n",
0309 num_groups, num_groups * 2 * num_fds,
0310 thread_mode ? "threads" : "processes");
0311 printf(" %14s: %lu.%03lu [sec]\n", "Total time",
0312 (unsigned long) diff.tv_sec,
0313 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
0314 break;
0315 case BENCH_FORMAT_SIMPLE:
0316 printf("%lu.%03lu\n", (unsigned long) diff.tv_sec,
0317 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
0318 break;
0319 default:
0320
0321 fprintf(stderr, "Unknown format:%d\n", bench_format);
0322 exit(1);
0323 break;
0324 }
0325
0326 free(pth_tab);
0327
0328 return 0;
0329 }