Linux Audio

Check our new training course

Loading...
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *
  4 * sched-messaging.c
  5 *
  6 * messaging: Benchmark for scheduler and IPC mechanisms
  7 *
  8 * Based on hackbench by Rusty Russell <rusty@rustcorp.com.au>
  9 * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
 10 *
 11 */
 12
 13#include "../perf.h"
 14#include "../util/util.h"
 15#include <subcmd/parse-options.h>
 16#include "../builtin.h"
 17#include "bench.h"
 18
 19/* Test groups of 20 processes spraying to 20 receivers */
 20#include <pthread.h>
 21#include <stdio.h>
 22#include <stdlib.h>
 23#include <string.h>
 24#include <errno.h>
 25#include <unistd.h>
 26#include <sys/types.h>
 27#include <sys/socket.h>
 28#include <sys/wait.h>
 29#include <sys/time.h>
 30#include <poll.h>
 31#include <limits.h>
 32#include <err.h>
 
 33#include <linux/time64.h>
 34
 35#define DATASIZE 100
 36
 37static bool use_pipes = false;
 38static unsigned int nr_loops = 100;
 39static bool thread_mode = false;
 40static unsigned int num_groups = 10;
 
 
 
 41
 42struct sender_context {
 
 43	unsigned int num_fds;
 44	int ready_out;
 45	int wakefd;
 46	int out_fds[0];
 47};
 48
 49struct receiver_context {
 
 50	unsigned int num_packets;
 51	int in_fds[2];
 52	int ready_out;
 53	int wakefd;
 54};
 55
 
 
 
 
 
 
 
 56static void fdpair(int fds[2])
 57{
 58	if (use_pipes) {
 59		if (pipe(fds) == 0)
 60			return;
 61	} else {
 62		if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0)
 63			return;
 64	}
 65
 66	err(EXIT_FAILURE, use_pipes ? "pipe()" : "socketpair()");
 67}
 68
 69/* Block until we're ready to go */
 70static void ready(int ready_out, int wakefd)
 71{
 72	char dummy;
 73	struct pollfd pollfd = { .fd = wakefd, .events = POLLIN };
 74
 75	/* Tell them we're ready. */
 76	if (write(ready_out, &dummy, 1) != 1)
 77		err(EXIT_FAILURE, "CLIENT: ready write");
 78
 79	/* Wait for "GO" signal */
 80	if (poll(&pollfd, 1, -1) != 1)
 81		err(EXIT_FAILURE, "poll");
 82}
 83
 84/* Sender sprays nr_loops messages down each file descriptor */
 85static void *sender(struct sender_context *ctx)
 86{
 87	char data[DATASIZE];
 88	unsigned int i, j;
 89
 90	ready(ctx->ready_out, ctx->wakefd);
 
 91
 92	/* Now pump to every receiver. */
 93	for (i = 0; i < nr_loops; i++) {
 94		for (j = 0; j < ctx->num_fds; j++) {
 95			int ret, done = 0;
 96
 97again:
 98			ret = write(ctx->out_fds[j], data + done,
 99				    sizeof(data)-done);
100			if (ret < 0)
101				err(EXIT_FAILURE, "SENDER: write");
102			done += ret;
103			if (done < DATASIZE)
104				goto again;
105		}
106	}
107
108	return NULL;
109}
110
111
112/* One receiver per fd */
113static void *receiver(struct receiver_context* ctx)
114{
115	unsigned int i;
116
117	if (!thread_mode)
118		close(ctx->in_fds[1]);
119
120	/* Wait for start... */
121	ready(ctx->ready_out, ctx->wakefd);
122
123	/* Receive them all */
124	for (i = 0; i < ctx->num_packets; i++) {
125		char data[DATASIZE];
126		int ret, done = 0;
127
128again:
129		ret = read(ctx->in_fds[0], data + done, DATASIZE - done);
130		if (ret < 0)
131			err(EXIT_FAILURE, "SERVER: read");
132		done += ret;
133		if (done < DATASIZE)
134			goto again;
135	}
136
137	return NULL;
138}
139
140static pthread_t create_worker(void *ctx, void *(*func)(void *))
 
141{
142	pthread_attr_t attr;
143	pthread_t childid;
144	int ret;
145
146	if (!thread_mode) {
147		/* process mode */
148		/* Fork the receiver. */
149		switch (fork()) {
150		case -1:
151			err(EXIT_FAILURE, "fork()");
152			break;
153		case 0:
154			(*func) (ctx);
155			exit(0);
156			break;
157		default:
158			break;
159		}
160
161		return (pthread_t)0;
162	}
163
164	if (pthread_attr_init(&attr) != 0)
165		err(EXIT_FAILURE, "pthread_attr_init:");
166
167#ifndef __ia64__
168	if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0)
169		err(EXIT_FAILURE, "pthread_attr_setstacksize");
170#endif
171
172	ret = pthread_create(&childid, &attr, func, ctx);
173	if (ret != 0)
174		err(EXIT_FAILURE, "pthread_create failed");
175
176	return childid;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177}
178
179static void reap_worker(pthread_t id)
180{
181	int proc_status;
182	void *thread_status;
183
184	if (!thread_mode) {
185		/* process mode */
186		wait(&proc_status);
187		if (!WIFEXITED(proc_status))
188			exit(1);
189	} else {
190		pthread_join(id, &thread_status);
191	}
192}
193
194/* One group of senders and receivers */
195static unsigned int group(pthread_t *pth,
196		unsigned int num_fds,
197		int ready_out,
198		int wakefd)
199{
200	unsigned int i;
201	struct sender_context *snd_ctx = malloc(sizeof(struct sender_context)
202			+ num_fds * sizeof(int));
203
204	if (!snd_ctx)
205		err(EXIT_FAILURE, "malloc()");
206
 
207	for (i = 0; i < num_fds; i++) {
208		int fds[2];
209		struct receiver_context *ctx = malloc(sizeof(*ctx));
210
211		if (!ctx)
212			err(EXIT_FAILURE, "malloc()");
213
 
214
215		/* Create the pipe between client and server */
216		fdpair(fds);
217
218		ctx->num_packets = num_fds * nr_loops;
219		ctx->in_fds[0] = fds[0];
220		ctx->in_fds[1] = fds[1];
221		ctx->ready_out = ready_out;
222		ctx->wakefd = wakefd;
223
224		pth[i] = create_worker(ctx, (void *)receiver);
225
226		snd_ctx->out_fds[i] = fds[1];
227		if (!thread_mode)
228			close(fds[0]);
229	}
230
231	/* Now we have all the fds, fork the senders */
232	for (i = 0; i < num_fds; i++) {
233		snd_ctx->ready_out = ready_out;
234		snd_ctx->wakefd = wakefd;
235		snd_ctx->num_fds = num_fds;
236
237		pth[num_fds+i] = create_worker(snd_ctx, (void *)sender);
238	}
239
240	/* Close the fds we have left */
241	if (!thread_mode)
242		for (i = 0; i < num_fds; i++)
243			close(snd_ctx->out_fds[i]);
244
245	/* Return number of children to reap */
246	return num_fds * 2;
247}
248
 
 
 
 
 
 
 
 
 
 
 
249static const struct option options[] = {
250	OPT_BOOLEAN('p', "pipe", &use_pipes,
251		    "Use pipe() instead of socketpair()"),
252	OPT_BOOLEAN('t', "thread", &thread_mode,
253		    "Be multi thread instead of multi process"),
254	OPT_UINTEGER('g', "group", &num_groups, "Specify number of groups"),
255	OPT_UINTEGER('l', "nr_loops", &nr_loops, "Specify the number of loops to run (default: 100)"),
256	OPT_END()
257};
258
259static const char * const bench_sched_message_usage[] = {
260	"perf bench sched messaging <options>",
261	NULL
262};
263
264int bench_sched_messaging(int argc, const char **argv)
265{
266	unsigned int i, total_children;
267	struct timeval start, stop, diff;
268	unsigned int num_fds = 20;
269	int readyfds[2], wakefds[2];
270	char dummy;
271	pthread_t *pth_tab;
272
273	argc = parse_options(argc, argv, options,
274			     bench_sched_message_usage, 0);
275
276	pth_tab = malloc(num_fds * 2 * num_groups * sizeof(pthread_t));
277	if (!pth_tab)
278		err(EXIT_FAILURE, "main:malloc()");
279
280	fdpair(readyfds);
281	fdpair(wakefds);
282
283	total_children = 0;
 
 
 
 
284	for (i = 0; i < num_groups; i++)
285		total_children += group(pth_tab+total_children, num_fds,
286					readyfds[1], wakefds[0]);
287
288	/* Wait for everyone to be ready */
289	for (i = 0; i < total_children; i++)
290		if (read(readyfds[0], &dummy, 1) != 1)
291			err(EXIT_FAILURE, "Reading for readyfds");
292
293	gettimeofday(&start, NULL);
294
295	/* Kick them off */
296	if (write(wakefds[1], &dummy, 1) != 1)
297		err(EXIT_FAILURE, "Writing to start them");
298
299	/* Reap them all */
300	for (i = 0; i < total_children; i++)
301		reap_worker(pth_tab[i]);
302
303	gettimeofday(&stop, NULL);
304
305	timersub(&stop, &start, &diff);
306
307	switch (bench_format) {
308	case BENCH_FORMAT_DEFAULT:
309		printf("# %d sender and receiver %s per group\n",
310		       num_fds, thread_mode ? "threads" : "processes");
311		printf("# %d groups == %d %s run\n\n",
312		       num_groups, num_groups * 2 * num_fds,
313		       thread_mode ? "threads" : "processes");
314		printf(" %14s: %lu.%03lu [sec]\n", "Total time",
315		       diff.tv_sec,
316		       (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
317		break;
318	case BENCH_FORMAT_SIMPLE:
319		printf("%lu.%03lu\n", diff.tv_sec,
320		       (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
321		break;
322	default:
323		/* reaching here is something disaster */
324		fprintf(stderr, "Unknown format:%d\n", bench_format);
325		exit(1);
326		break;
327	}
328
329	free(pth_tab);
330
 
 
 
 
 
 
 
331	return 0;
332}
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *
  4 * sched-messaging.c
  5 *
  6 * messaging: Benchmark for scheduler and IPC mechanisms
  7 *
  8 * Based on hackbench by Rusty Russell <rusty@rustcorp.com.au>
  9 * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
 10 *
 11 */
 12
 
 
 13#include <subcmd/parse-options.h>
 
 14#include "bench.h"
 15
 16/* Test groups of 20 processes spraying to 20 receivers */
 17#include <pthread.h>
 18#include <stdio.h>
 19#include <stdlib.h>
 20#include <string.h>
 21#include <errno.h>
 22#include <unistd.h>
 23#include <sys/types.h>
 24#include <sys/socket.h>
 25#include <sys/wait.h>
 26#include <sys/time.h>
 27#include <poll.h>
 28#include <limits.h>
 29#include <err.h>
 30#include <linux/list.h>
 31#include <linux/time64.h>
 32
 33#define DATASIZE 100
 34
 35static bool use_pipes = false;
 36static unsigned int nr_loops = 100;
 37static bool thread_mode = false;
 38static unsigned int num_groups = 10;
 39static unsigned int total_children = 0;
 40static struct list_head sender_contexts = LIST_HEAD_INIT(sender_contexts);
 41static struct list_head receiver_contexts = LIST_HEAD_INIT(receiver_contexts);
 42
 43struct sender_context {
 44	struct list_head list;
 45	unsigned int num_fds;
 46	int ready_out;
 47	int wakefd;
 48	int out_fds[];
 49};
 50
 51struct receiver_context {
 52	struct list_head list;
 53	unsigned int num_packets;
 54	int in_fds[2];
 55	int ready_out;
 56	int wakefd;
 57};
 58
 59union messaging_worker {
 60	pthread_t thread;
 61	pid_t pid;
 62};
 63
 64static union messaging_worker *worker_tab;
 65
 66static void fdpair(int fds[2])
 67{
 68	if (use_pipes) {
 69		if (pipe(fds) == 0)
 70			return;
 71	} else {
 72		if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0)
 73			return;
 74	}
 75
 76	err(EXIT_FAILURE, use_pipes ? "pipe()" : "socketpair()");
 77}
 78
 79/* Block until we're ready to go */
 80static void ready(int ready_out, int wakefd)
 81{
 
 82	struct pollfd pollfd = { .fd = wakefd, .events = POLLIN };
 83
 84	/* Tell them we're ready. */
 85	if (write(ready_out, "R", 1) != 1)
 86		err(EXIT_FAILURE, "CLIENT: ready write");
 87
 88	/* Wait for "GO" signal */
 89	if (poll(&pollfd, 1, -1) != 1)
 90		err(EXIT_FAILURE, "poll");
 91}
 92
 93/* Sender sprays nr_loops messages down each file descriptor */
 94static void *sender(struct sender_context *ctx)
 95{
 96	char data[DATASIZE];
 97	unsigned int i, j;
 98
 99	ready(ctx->ready_out, ctx->wakefd);
100	memset(data, 'S', sizeof(data));
101
102	/* Now pump to every receiver. */
103	for (i = 0; i < nr_loops; i++) {
104		for (j = 0; j < ctx->num_fds; j++) {
105			int ret, done = 0;
106
107again:
108			ret = write(ctx->out_fds[j], data + done,
109				    sizeof(data) - done);
110			if (ret < 0)
111				err(EXIT_FAILURE, "SENDER: write");
112			done += ret;
113			if (done < DATASIZE)
114				goto again;
115		}
116	}
117
118	return NULL;
119}
120
121
122/* One receiver per fd */
123static void *receiver(struct receiver_context* ctx)
124{
125	unsigned int i;
126
127	if (!thread_mode)
128		close(ctx->in_fds[1]);
129
130	/* Wait for start... */
131	ready(ctx->ready_out, ctx->wakefd);
132
133	/* Receive them all */
134	for (i = 0; i < ctx->num_packets; i++) {
135		char data[DATASIZE];
136		int ret, done = 0;
137
138again:
139		ret = read(ctx->in_fds[0], data + done, DATASIZE - done);
140		if (ret < 0)
141			err(EXIT_FAILURE, "SERVER: read");
142		done += ret;
143		if (done < DATASIZE)
144			goto again;
145	}
146
147	return NULL;
148}
149
150static void create_thread_worker(union messaging_worker *worker,
151				 void *ctx, void *(*func)(void *))
152{
153	pthread_attr_t attr;
 
154	int ret;
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156	if (pthread_attr_init(&attr) != 0)
157		err(EXIT_FAILURE, "pthread_attr_init:");
158
159#ifndef __ia64__
160	if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0)
161		err(EXIT_FAILURE, "pthread_attr_setstacksize");
162#endif
163
164	ret = pthread_create(&worker->thread, &attr, func, ctx);
165	if (ret != 0)
166		err(EXIT_FAILURE, "pthread_create failed");
167
168	pthread_attr_destroy(&attr);
169}
170
171static void create_process_worker(union messaging_worker *worker,
172				  void *ctx, void *(*func)(void *))
173{
174	/* Fork the receiver. */
175	worker->pid = fork();
176
177	if (worker->pid == -1) {
178		err(EXIT_FAILURE, "fork()");
179	} else if (worker->pid == 0) {
180		(*func) (ctx);
181		exit(0);
182	}
183}
184
185static void create_worker(union messaging_worker *worker,
186			  void *ctx, void *(*func)(void *))
187{
188	if (!thread_mode)
189		return create_process_worker(worker, ctx, func);
190	else
191		return create_thread_worker(worker, ctx, func);
192}
193
194static void reap_worker(union messaging_worker *worker)
195{
196	int proc_status;
197	void *thread_status;
198
199	if (!thread_mode) {
200		/* process mode */
201		wait(&proc_status);
202		if (!WIFEXITED(proc_status))
203			exit(1);
204	} else {
205		pthread_join(worker->thread, &thread_status);
206	}
207}
208
209/* One group of senders and receivers */
210static unsigned int group(union messaging_worker *worker,
211		unsigned int num_fds,
212		int ready_out,
213		int wakefd)
214{
215	unsigned int i;
216	struct sender_context *snd_ctx = malloc(sizeof(struct sender_context) +
217						num_fds * sizeof(int));
218
219	if (!snd_ctx)
220		err(EXIT_FAILURE, "malloc()");
221
222	list_add(&snd_ctx->list, &sender_contexts);
223	for (i = 0; i < num_fds; i++) {
224		int fds[2];
225		struct receiver_context *ctx = malloc(sizeof(*ctx));
226
227		if (!ctx)
228			err(EXIT_FAILURE, "malloc()");
229
230		list_add(&ctx->list, &receiver_contexts);
231
232		/* Create the pipe between client and server */
233		fdpair(fds);
234
235		ctx->num_packets = num_fds * nr_loops;
236		ctx->in_fds[0] = fds[0];
237		ctx->in_fds[1] = fds[1];
238		ctx->ready_out = ready_out;
239		ctx->wakefd = wakefd;
240
241		create_worker(worker + i, ctx, (void *)receiver);
242
243		snd_ctx->out_fds[i] = fds[1];
244		if (!thread_mode)
245			close(fds[0]);
246	}
247
248	/* Now we have all the fds, fork the senders */
249	for (i = 0; i < num_fds; i++) {
250		snd_ctx->ready_out = ready_out;
251		snd_ctx->wakefd = wakefd;
252		snd_ctx->num_fds = num_fds;
253
254		create_worker(worker + num_fds + i, snd_ctx, (void *)sender);
255	}
256
257	/* Close the fds we have left */
258	if (!thread_mode)
259		for (i = 0; i < num_fds; i++)
260			close(snd_ctx->out_fds[i]);
261
262	/* Return number of children to reap */
263	return num_fds * 2;
264}
265
266static void sig_handler(int sig __maybe_unused)
267{
268	unsigned int i;
269
270	/*
271	 * When exit abnormally, kill all forked child processes.
272	 */
273	for (i = 0; i < total_children; i++)
274		kill(worker_tab[i].pid, SIGKILL);
275}
276
277static const struct option options[] = {
278	OPT_BOOLEAN('p', "pipe", &use_pipes,
279		    "Use pipe() instead of socketpair()"),
280	OPT_BOOLEAN('t', "thread", &thread_mode,
281		    "Be multi thread instead of multi process"),
282	OPT_UINTEGER('g', "group", &num_groups, "Specify number of groups"),
283	OPT_UINTEGER('l', "nr_loops", &nr_loops, "Specify the number of loops to run (default: 100)"),
284	OPT_END()
285};
286
287static const char * const bench_sched_message_usage[] = {
288	"perf bench sched messaging <options>",
289	NULL
290};
291
292int bench_sched_messaging(int argc, const char **argv)
293{
294	unsigned int i;
295	struct timeval start, stop, diff;
296	unsigned int num_fds = 20;
297	int readyfds[2], wakefds[2];
298	char dummy;
299	struct sender_context *pos, *n;
300
301	argc = parse_options(argc, argv, options,
302			     bench_sched_message_usage, 0);
303
304	worker_tab = malloc(num_fds * 2 * num_groups * sizeof(union messaging_worker));
305	if (!worker_tab)
306		err(EXIT_FAILURE, "main:malloc()");
307
308	fdpair(readyfds);
309	fdpair(wakefds);
310
311	if (!thread_mode) {
312		signal(SIGINT, sig_handler);
313		signal(SIGTERM, sig_handler);
314	}
315
316	for (i = 0; i < num_groups; i++)
317		total_children += group(worker_tab + total_children, num_fds,
318					readyfds[1], wakefds[0]);
319
320	/* Wait for everyone to be ready */
321	for (i = 0; i < total_children; i++)
322		if (read(readyfds[0], &dummy, 1) != 1)
323			err(EXIT_FAILURE, "Reading for readyfds");
324
325	gettimeofday(&start, NULL);
326
327	/* Kick them off */
328	if (write(wakefds[1], &dummy, 1) != 1)
329		err(EXIT_FAILURE, "Writing to start them");
330
331	/* Reap them all */
332	for (i = 0; i < total_children; i++)
333		reap_worker(worker_tab + i);
334
335	gettimeofday(&stop, NULL);
336
337	timersub(&stop, &start, &diff);
338
339	switch (bench_format) {
340	case BENCH_FORMAT_DEFAULT:
341		printf("# %d sender and receiver %s per group\n",
342		       num_fds, thread_mode ? "threads" : "processes");
343		printf("# %d groups == %d %s run\n\n",
344		       num_groups, num_groups * 2 * num_fds,
345		       thread_mode ? "threads" : "processes");
346		printf(" %14s: %lu.%03lu [sec]\n", "Total time",
347		       (unsigned long) diff.tv_sec,
348		       (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
349		break;
350	case BENCH_FORMAT_SIMPLE:
351		printf("%lu.%03lu\n", (unsigned long) diff.tv_sec,
352		       (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
353		break;
354	default:
355		/* reaching here is something disaster */
356		fprintf(stderr, "Unknown format:%d\n", bench_format);
357		exit(1);
358		break;
359	}
360
361	free(worker_tab);
362	list_for_each_entry_safe(pos, n, &sender_contexts, list) {
363		list_del_init(&pos->list);
364		free(pos);
365	}
366	list_for_each_entry_safe(pos, n, &receiver_contexts, list) {
367		list_del_init(&pos->list);
368		free(pos);
369	}
370	return 0;
371}