From 8f155917bf5c11c8f156d7f25f242257d6086cb9 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 22 May 2013 16:02:21 +0200 Subject: bus: add benchmark tool to determine the right threshold for copying vs. memfd --- .gitignore | 1 + Makefile.am | 12 ++ src/libsystemd-bus/bus-internal.h | 2 + src/libsystemd-bus/bus-kernel.c | 2 + src/libsystemd-bus/bus-message.c | 6 +- src/libsystemd-bus/sd-bus.c | 1 + src/libsystemd-bus/test-bus-kernel-benchmark.c | 225 +++++++++++++++++++++++++ 7 files changed, 246 insertions(+), 3 deletions(-) create mode 100644 src/libsystemd-bus/test-bus-kernel-benchmark.c diff --git a/.gitignore b/.gitignore index c2e745672c..7534ac1c07 100644 --- a/.gitignore +++ b/.gitignore @@ -84,6 +84,7 @@ /test-bus-chat /test-bus-kernel /test-bus-kernel-bloom +/test-bus-kernel-benchmark /test-bus-marshal /test-bus-match /test-bus-memfd diff --git a/Makefile.am b/Makefile.am index 2b2efcc747..b278143af2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1764,6 +1764,7 @@ tests += \ test-bus-match \ test-bus-kernel \ test-bus-kernel-bloom \ + test-bus-kernel-benchmark \ test-bus-memfd \ test-bus-zero-copy @@ -1848,6 +1849,17 @@ test_bus_kernel_bloom_LDADD = \ libsystemd-bus.la \ libsystemd-id128-internal.la +test_bus_kernel_benchmark_SOURCES = \ + src/libsystemd-bus/test-bus-kernel-benchmark.c + +test_bus_kernel_benchmark_CFLAGS = \ + $(AM_CFLAGS) + +test_bus_kernel_benchmark_LDADD = \ + libsystemd-shared.la \ + libsystemd-bus.la \ + libsystemd-id128-internal.la + test_bus_memfd_SOURCES = \ src/libsystemd-bus/test-bus-memfd.c diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h index 147a83c045..30b8d519a0 100644 --- a/src/libsystemd-bus/bus-internal.h +++ b/src/libsystemd-bus/bus-internal.h @@ -111,6 +111,8 @@ struct sd_bus { bool filter_callbacks_modified:1; bool object_callbacks_modified:1; + int use_memfd; + void *rbuffer; size_t rbuffer_size; diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c index 19d274b662..10b7a8aba4 100644 --- a/src/libsystemd-bus/bus-kernel.c +++ b/src/libsystemd-bus/bus-kernel.c @@ -326,6 +326,8 @@ int bus_kernel_take_fd(sd_bus *b) { if (b->is_server) return -EINVAL; + b->use_memfd = 1; + if (!b->kdbus_buffer) { b->kdbus_buffer = mmap(NULL, KDBUS_POOL_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); if (b->kdbus_buffer == MAP_FAILED) { diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c index e531dec5cd..55c2d62885 100644 --- a/src/libsystemd-bus/bus-message.c +++ b/src/libsystemd-bus/bus-message.c @@ -564,7 +564,7 @@ static int message_new_reply( goto fail; if (call->sender) { - r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->sender); + r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->destination); if (r < 0) goto fail; } @@ -3865,9 +3865,9 @@ int bus_message_seal(sd_bus_message *m, uint64_t serial) { /* If this is something we can send as memfd, then let's seal the memfd now. Note that we can send memfds as payload only for directed messages, and not for broadcasts. */ - if (m->destination) { + if (m->destination && m->bus && m->bus->use_memfd) { MESSAGE_FOREACH_PART(part, i, m) - if (part->memfd >= 0 && !part->sealed && part->size > MEMFD_MIN_SIZE) { + if (part->memfd >= 0 && !part->sealed && (part->size > MEMFD_MIN_SIZE || m->bus->use_memfd < 0)) { bus_body_part_unmap(part); if (ioctl(part->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) >= 0) diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 7ae32036bd..3f766fb519 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -2514,6 +2514,7 @@ int sd_bus_call_method( int r; if (!bus) + return -EINVAL; if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; diff --git a/src/libsystemd-bus/test-bus-kernel-benchmark.c b/src/libsystemd-bus/test-bus-kernel-benchmark.c new file mode 100644 index 0000000000..403af885c3 --- /dev/null +++ b/src/libsystemd-bus/test-bus-kernel-benchmark.c @@ -0,0 +1,225 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2013 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include + +#include "util.h" +#include "log.h" + +#include "sd-bus.h" +#include "bus-message.h" +#include "bus-error.h" +#include "bus-kernel.h" +#include "bus-internal.h" + +#define N_TRIES 500 +#define MAX_SIZE (5*1024*1024) + +static void server(sd_bus *b, usec_t *result) { + usec_t x = 0; + int r; + + for (;;) { + _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + + r = sd_bus_process(b, &m); + assert_se(r >= 0); + + if (r == 0) + assert_se(sd_bus_wait(b, (usec_t) -1) >= 0); + + if (!m) + continue; + + /* log_error("huhu %s from %s", sd_bus_message_get_member(m), sd_bus_message_get_sender(m)); */ + + if (sd_bus_message_is_method_call(m, "benchmark.server", "Work")) { + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; + size_t i, sz; + char *q; + const char *p; + + assert_se(sd_bus_message_read_array(m, 'y', (const void**) &p, &sz) > 0); + assert_se(sd_bus_message_new_method_return(b, m, &reply) >= 0); + assert_se(sd_bus_message_append_array_space(reply, 'y', sz, (void**) &q) >= 0); + + x = now(CLOCK_MONOTONIC); + + for (i = 0; i < sz; i++) + q[i] = toupper(p[i]); + + x = now(CLOCK_MONOTONIC) - x; + + assert_se(sd_bus_send(b, reply, NULL) >= 0); + } else if (sd_bus_message_is_method_call(m, "benchmark.server", "Exit")) { + usec_t t; + + assert_se(sd_bus_message_read(m, "t", &t) > 0); + assert_se(t >= x); + *result = t - x; + return; + + } else if (sd_bus_message_is_method_call(m, "benchmark.server", "Ping")) { + assert_se(sd_bus_reply_method_return(b, m, "y", 1) >= 0); + } else + assert_not_reached("Unknown method"); + } +} + +static void client(sd_bus *b, size_t sz) { + _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; + char *p; + const char *q; + usec_t t; + size_t l, i; + + assert_se(sd_bus_call_method(b, ":1.1", "/", "benchmark.server", "Ping", NULL, NULL, NULL) >= 0); + + assert_se(sd_bus_message_new_method_call(b, ":1.1", "/", "benchmark.server", "Work", &m) >= 0); + assert_se(sd_bus_message_append_array_space(m, 'y', sz, (void**) &p) >= 0); + + for (i = 0; i < sz; i++) + p[i] = 'a' + (char) (i % 26); + + t = now(CLOCK_MONOTONIC); + assert_se(sd_bus_send_with_reply_and_block(b, m, 0, NULL, &reply) >= 0); + t = now(CLOCK_MONOTONIC) - t; + + assert_se(sd_bus_message_read_array(reply, 'y', (const void**) &q, &l) > 0); + assert_se(l == sz); + + for (i = 0; i < sz; i++) { + assert_se(q[i] == 'A' + (char) (i % 26)); + } + + sd_bus_message_unref(m); + + assert_se(sd_bus_message_new_method_call(b, ":1.1", "/", "benchmark.server", "Exit", &m) >= 0); + assert_se(sd_bus_message_append(m, "t", t) >= 0); + assert_se(sd_bus_send(b, m, NULL) >= 0); +} + +static void run_benchmark(size_t sz, bool force_copy, usec_t *result) { + + _cleanup_close_ int bus_ref = -1; + _cleanup_free_ char *bus_name = NULL, *address = NULL; + sd_bus *b; + int r; + pid_t pid; + + bus_ref = bus_kernel_create("deine-mutter", &bus_name); + if (bus_ref == -ENOENT) + exit(EXIT_TEST_SKIP); + + assert_se(bus_ref >= 0); + + address = strappend("kernel:path=", bus_name); + assert_se(address); + + r = sd_bus_new(&b); + assert_se(r >= 0); + + b->use_memfd = force_copy ? 0 : -1; + + r = sd_bus_set_address(b, address); + assert_se(r >= 0); + + r = sd_bus_start(b); + assert_se(r >= 0); + + pid = fork(); + assert_se(pid >= 0); + + if (pid == 0) { + close_nointr_nofail(bus_ref); + sd_bus_unref(b); + + r = sd_bus_new(&b); + assert_se(r >= 0); + + b->use_memfd = force_copy ? 0 : -1; + + r = sd_bus_set_address(b, address); + assert_se(r >= 0); + + r = sd_bus_start(b); + assert_se(r >= 0); + + client(b, sz); + _exit(0); + } + + server(b, result); + sd_bus_unref(b); + + assert_se(waitpid(pid, NULL, 0) == pid); +} + +int main(int argc, char *argv[]) { + size_t lsize, rsize, csize; + + log_set_max_level(LOG_DEBUG); + + lsize = 1; + rsize = MAX_SIZE; + + for (;;) { + usec_t copy = 0, memfd = 0; + unsigned i; + + csize = (lsize + rsize) / 2; + + log_info("Trying size=%zu", csize); + + if (csize <= lsize) + break; + + for (i = 0; i < N_TRIES; i++) { + usec_t t; + + run_benchmark(csize, true, &t); + copy += t; + } + + for (i = 0; i < N_TRIES; i++) { + usec_t t; + + run_benchmark(csize, false, &t); + memfd += t; + } + + copy /= N_TRIES; + memfd /= N_TRIES; + + if (copy == memfd) + break; + + if (copy < memfd) + lsize = csize; + else + rsize = csize; + } + + log_info("Copying/memfd are equally fast at %zu", csize); + + return 0; +} -- cgit v1.2.3-54-g00ecf