14 #include "../util/util.h" 15 #include <subcmd/parse-options.h> 16 #include "../builtin.h" 26 #include <sys/types.h> 27 #include <sys/socket.h> 33 #include <linux/time64.h> 62 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0)
66 err(EXIT_FAILURE,
use_pipes ?
"pipe()" :
"socketpair()");
73 struct pollfd pollfd = { .fd =
wakefd, .events = POLLIN };
76 if (write(ready_out, &dummy, 1) != 1)
77 err(EXIT_FAILURE,
"CLIENT: ready write");
80 if (poll(&pollfd, 1, -1) != 1)
81 err(EXIT_FAILURE,
"poll");
94 for (j = 0; j < ctx->
num_fds; j++) {
98 ret = write(ctx->
out_fds[j], data + done,
101 err(EXIT_FAILURE,
"SENDER: write");
131 err(EXIT_FAILURE,
"SERVER: read");
151 err(EXIT_FAILURE,
"fork()");
164 if (pthread_attr_init(&attr) != 0)
165 err(EXIT_FAILURE,
"pthread_attr_init:");
168 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0)
169 err(EXIT_FAILURE,
"pthread_attr_setstacksize");
172 ret = pthread_create(&childid, &attr,
func, ctx);
174 err(EXIT_FAILURE,
"pthread_create failed");
187 if (!WIFEXITED(proc_status))
190 pthread_join(
id, &thread_status);
195 static unsigned int group(pthread_t *pth,
202 + num_fds *
sizeof(
int));
205 err(EXIT_FAILURE,
"malloc()");
207 for (i = 0; i <
num_fds; i++) {
212 err(EXIT_FAILURE,
"malloc()");
232 for (i = 0; i <
num_fds; i++) {
251 "Use pipe() instead of socketpair()"),
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)"),
260 "perf bench sched messaging <options>",
266 unsigned int i, total_children;
267 struct timeval start, stop, diff;
269 int readyfds[2], wakefds[2];
273 argc = parse_options(argc, argv, options,
274 bench_sched_message_usage, 0);
278 err(EXIT_FAILURE,
"main:malloc()");
285 total_children +=
group(pth_tab+total_children, num_fds,
286 readyfds[1], wakefds[0]);
289 for (i = 0; i < total_children; i++)
290 if (read(readyfds[0], &dummy, 1) != 1)
291 err(EXIT_FAILURE,
"Reading for readyfds");
293 gettimeofday(&start, NULL);
296 if (write(wakefds[1], &dummy, 1) != 1)
297 err(EXIT_FAILURE,
"Writing to start them");
300 for (i = 0; i < total_children; i++)
303 gettimeofday(&stop, NULL);
305 timersub(&stop, &start, &diff);
309 printf(
"# %d sender and receiver %s per group\n",
311 printf(
"# %d groups == %d %s run\n\n",
312 num_groups, num_groups * 2 * num_fds,
314 printf(
" %14s: %lu.%03lu [sec]\n",
"Total time",
316 (
unsigned long) (diff.tv_usec / USEC_PER_MSEC));
319 printf(
"%lu.%03lu\n", diff.tv_sec,
320 (
unsigned long) (diff.tv_usec / USEC_PER_MSEC));
static void * receiver(struct receiver_context *ctx)
static pthread_t create_worker(void *ctx, void *(*func)(void *))
static void ready(int ready_out, int wakefd)
int bench_sched_messaging(int argc, const char **argv)
static const char *const bench_sched_message_usage[]
#define BENCH_FORMAT_DEFAULT
static unsigned int group(pthread_t *pth, unsigned int num_fds, int ready_out, int wakefd)
static void * sender(struct sender_context *ctx)
static void reap_worker(pthread_t id)
static void fdpair(int fds[2])
#define BENCH_FORMAT_SIMPLE
static unsigned int num_groups
static unsigned int nr_loops