test-bus-benchmark.c revision a9beb123d19304fa6999f63d755a2bda585c1f5b
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering This file is part of systemd.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Copyright 2013 Lennart Poettering
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 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 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/>.
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversstatic usec_t arg_loop_usec = 100 * USEC_PER_MSEC;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic void server(sd_bus *b, size_t *result) {
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace assert_se(sd_bus_wait(b, USEC_INFINITY) >= 0);
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;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* Make sure the mmap is mapped */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_bus_message_read_array(m, 'y', &p, &sz) > 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else if (sd_bus_message_is_method_call(m, "benchmark.server", "Exit")) {
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering assert_se(sd_bus_message_read(m, "t", &res) > 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering } else if (!sd_bus_message_is_signal(m, NULL, NULL))
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 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 assert_se(sd_bus_call(b, m, 0, NULL, &reply) >= 0);
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 r = sd_bus_call_method(b, server_name, "/", "benchmark.server", "Ping", NULL, NULL, NULL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec));
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 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;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers r = sd_bus_call_method(b, server_name, "/", "benchmark.server", "Ping", NULL, NULL, NULL);
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering for (csize = 1; csize <= MAX_SIZE; csize *= 2) {
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poettering if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
94676f3e9352cbf1f72e0a512ee0d2ed83ff676dLennart Poettering printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec));
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec));
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);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL, *server_name = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(parse_sec(argv[i], &arg_loop_usec) >= 0);
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering bus_ref = bus_kernel_create_bus(name, false, &bus_name);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering address = strappend("kernel:path=", bus_name);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *e;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_bus_set_server(b, true, SD_ID128_NULL);
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek setpriority(PRIO_PROCESS, 0, -19);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen client_chart(type, address, server_name, pair[1]);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering printf("Copying/memfd are equally fast at %zu bytes\n", result);