test-bus-benchmark.c revision a9beb123d19304fa6999f63d755a2bda585c1f5b
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/***
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering This file is part of systemd.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Copyright 2013 Lennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is free software; you can redistribute it and/or modify it
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering under the terms of the GNU Lesser General Public License as published by
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering (at your option) any later version.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is distributed in the hope that it will be useful, but
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Lesser General Public License for more details.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering You should have received a copy of the GNU Lesser General Public License
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering***/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/wait.h>
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "def.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "util.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "time-util.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "sd-bus.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "bus-kernel.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "bus-internal.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "bus-util.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers#define MAX_SIZE (2*1024*1024)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversstatic usec_t arg_loop_usec = 100 * USEC_PER_MSEC;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringtypedef enum Type {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering TYPE_KDBUS,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering TYPE_LEGACY,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering TYPE_DIRECT,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering} Type;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic void server(sd_bus *b, size_t *result) {
0affed79d2e30013f07cb94e6f07e3fcb81c02faLennart Poettering int r;
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar for (;;) {
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_bus_process(b, &m);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (r == 0)
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace assert_se(sd_bus_wait(b, USEC_INFINITY) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!m)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering continue;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (sd_bus_message_is_method_call(m, "benchmark.server", "Ping"))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_bus_reply_method_return(m, NULL) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else if (sd_bus_message_is_method_call(m, "benchmark.server", "Work")) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const void *p;
1b12a7b5896f94bdf33b3a6661ebabd761ea6adcHarald Hoyer size_t sz;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* Make sure the mmap is mapped */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_bus_message_read_array(m, 'y', &p, &sz) > 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_bus_reply_method_return(m, NULL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else if (sd_bus_message_is_method_call(m, "benchmark.server", "Exit")) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering uint64_t res;
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering assert_se(sd_bus_message_read(m, "t", &res) > 0);
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering *result = res;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else if (!sd_bus_message_is_signal(m, NULL, NULL))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_not_reached("Unknown method");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic void transaction(sd_bus *b, size_t sz, const char *server_name) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering uint8_t *p;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_bus_message_new_method_call(b, &m, server_name, "/", "benchmark.server", "Work") >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_bus_message_append_array_space(m, 'y', sz, (void**) &p) >= 0);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar memset(p, 0x80, sz);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar assert_se(sd_bus_call(b, m, 0, NULL, &reply) >= 0);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar}
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletarstatic void client_bisect(const char *address, const char *server_name) {
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar _cleanup_bus_message_unref_ sd_bus_message *x = NULL;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar size_t lsize, rsize, csize;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar sd_bus *b;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar int r;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar r = sd_bus_new(&b);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar assert_se(r >= 0);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar r = sd_bus_set_address(b, address);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar assert_se(r >= 0);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar r = sd_bus_start(b);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar assert_se(r >= 0);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar r = sd_bus_call_method(b, server_name, "/", "benchmark.server", "Ping", NULL, NULL, NULL);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar assert_se(r >= 0);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar lsize = 1;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar rsize = MAX_SIZE;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar printf("SIZE\tCOPY\tMEMFD\n");
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar for (;;) {
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar usec_t t;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar unsigned n_copying, n_memfd;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar csize = (lsize + rsize) / 2;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar if (csize <= lsize)
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar break;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar if (csize <= 0)
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar break;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar printf("%zu\t", csize);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar b->use_memfd = 0;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar t = now(CLOCK_MONOTONIC);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar for (n_copying = 0;; n_copying++) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering transaction(b, csize, server_name);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering b->use_memfd = -1;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering t = now(CLOCK_MONOTONIC);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (n_memfd = 0;; n_memfd++) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering transaction(b, csize, server_name);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (n_copying == n_memfd)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (n_copying > n_memfd)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering lsize = csize;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering rsize = csize;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering b->use_memfd = 1;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(sd_bus_message_new_method_call(b, &x, server_name, "/", "benchmark.server", "Exit") >= 0);
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek assert_se(sd_bus_message_append(x, "t", csize) >= 0);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers assert_se(sd_bus_send(b, x, NULL) >= 0);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers sd_bus_unref(b);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers}
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversstatic void client_chart(Type type, const char *address, const char *server_name, int fd) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers _cleanup_bus_message_unref_ sd_bus_message *x = NULL;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers size_t csize;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers sd_bus *b;
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers int r;
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers r = sd_bus_new(&b);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (type == TYPE_DIRECT) {
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers r = sd_bus_set_fd(b, fd, fd);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers assert_se(r >= 0);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers } else {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers r = sd_bus_set_address(b, address);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers assert_se(r >= 0);
6046278f405b3ebaf84fcf17f6e834c4c4bd6a42Djalal Harouni
6046278f405b3ebaf84fcf17f6e834c4c4bd6a42Djalal Harouni r = sd_bus_set_bus_client(b, true);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(r >= 0);
6046278f405b3ebaf84fcf17f6e834c4c4bd6a42Djalal Harouni }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar r = sd_bus_start(b);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers r = sd_bus_call_method(b, server_name, "/", "benchmark.server", "Ping", NULL, NULL, NULL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering switch (type) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering case TYPE_KDBUS:
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers printf("SIZE\tCOPY\tMEMFD\n");
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers break;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers case TYPE_LEGACY:
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering printf("SIZE\tLEGACY\n");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering case TYPE_DIRECT:
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering printf("SIZE\tDIRECT\n");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering for (csize = 1; csize <= MAX_SIZE; csize *= 2) {
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering usec_t t;
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering unsigned n_copying, n_memfd;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering printf("%zu\t", csize);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering if (type == TYPE_KDBUS) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers b->use_memfd = 0;
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering t = now(CLOCK_MONOTONIC);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers for (n_copying = 0;; n_copying++) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers transaction(b, csize, server_name);
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poettering if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poettering printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
c49b30a23583ff39daaa26696bcab478d2fee0bbLennart Poettering b->use_memfd = -1;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers }
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers t = now(CLOCK_MONOTONIC);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (n_memfd = 0;; n_memfd++) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering transaction(b, csize, server_name);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec));
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna }
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering b->use_memfd = 1;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna assert_se(sd_bus_message_new_method_call(b, &x, server_name, "/", "benchmark.server", "Exit") >= 0);
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering assert_se(sd_bus_message_append(x, "t", csize) >= 0);
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering assert_se(sd_bus_send(b, x, NULL) >= 0);
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek sd_bus_unref(b);
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint main(int argc, char *argv[]) {
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen enum {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering MODE_BISECT,
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek MODE_CHART,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } mode = MODE_BISECT;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Type type = TYPE_KDBUS;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers int i, pair[2] = { -1, -1 };
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL, *server_name = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering _cleanup_close_ int bus_ref = -1;
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering const char *unique;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering cpu_set_t cpuset;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering size_t result;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sd_bus *b;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering pid_t pid;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering int r;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering for (i = 1; i < argc; i++) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (streq(argv[i], "chart")) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering mode = MODE_CHART;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering continue;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else if (streq(argv[i], "legacy")) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering type = TYPE_LEGACY;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering continue;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else if (streq(argv[i], "direct")) {
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering type = TYPE_DIRECT;
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering continue;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(parse_sec(argv[i], &arg_loop_usec) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(!MODE_BISECT || TYPE_KDBUS);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering assert_se(arg_loop_usec > 0);
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering if (type == TYPE_KDBUS) {
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering bus_ref = bus_kernel_create_bus(name, false, &bus_name);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (bus_ref == -ENOENT)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering exit(EXIT_TEST_SKIP);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(bus_ref >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering address = strappend("kernel:path=", bus_name);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(address);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else if (type == TYPE_LEGACY) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *e;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(e);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering address = strdup(e);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(address);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_bus_new(&b);
2b6bf07dd23bb467099d213c97b3875c5e453491Zbigniew Jędrzejewski-Szmek assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (type == TYPE_DIRECT) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_bus_set_fd(b, pair[0], pair[0]);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_bus_set_server(b, true, SD_ID128_NULL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_bus_set_address(b, address);
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek assert_se(r >= 0);
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_bus_set_bus_client(b, true);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_bus_start(b);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (type != TYPE_DIRECT) {
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer r = sd_bus_get_unique_name(b, &unique);
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt server_name = strdup(unique);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(server_name);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek sync();
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek setpriority(PRIO_PROCESS, 0, -19);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering pid = fork();
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(pid >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (pid == 0) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering CPU_ZERO(&cpuset);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering CPU_SET(0, &cpuset);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering safe_close(bus_ref);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sd_bus_unref(b);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering switch (mode) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering case MODE_BISECT:
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering client_bisect(address, server_name);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering case MODE_CHART:
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen client_chart(type, address, server_name, pair[1]);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering break;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering _exit(0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers }
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering CPU_ZERO(&cpuset);
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering CPU_SET(1, &cpuset);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering server(b, &result);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (mode == MODE_BISECT)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering printf("Copying/memfd are equally fast at %zu bytes\n", result);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(waitpid(pid, NULL, 0) == pid);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering safe_close(pair[1]);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sd_bus_unref(b);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
6b2b6f30e38d67b032d6bdc6b47ae05e143e96c5Michal Schmidt