chiark / gitweb /
bus: rework benchmark test to actually yield useful results
authorLennart Poettering <lennart@poettering.net>
Wed, 29 May 2013 07:59:10 +0000 (16:59 +0900)
committerLennart Poettering <lennart@poettering.net>
Wed, 29 May 2013 08:16:10 +0000 (17:16 +0900)
src/libsystemd-bus/bus-kernel.h
src/libsystemd-bus/test-bus-kernel-benchmark.c

index dddecfb..9515e8f 100644 (file)
 
 /* When we cache a memfd block for reuse, we will truncate blocks
  * longer than this in order not to keep too much data around. */
-#define MEMFD_CACHE_ITEM_SIZE_MAX (32*1024)
+#define MEMFD_CACHE_ITEM_SIZE_MAX (128*1024)
 
 /* This determines at which minimum size we prefer sending memfds over
  * sending vectors */
-#define MEMFD_MIN_SIZE (32*1024)
+#define MEMFD_MIN_SIZE (96*1024)
 
 /* The size of the per-connection memory pool that we set up and where
  * the kernel places our incoming messages */
index 403af88..0cf685e 100644 (file)
 #include "bus-kernel.h"
 #include "bus-internal.h"
 
-#define N_TRIES 500
-#define MAX_SIZE (5*1024*1024)
+#define N_TRIES 10000
+#define MAX_SIZE (1*1024*1024)
 
-static void server(sd_bus *b, usec_t *result) {
-        usec_t x = 0;
+static void server(sd_bus *b, size_t *result) {
         int r;
 
         for (;;) {
@@ -46,85 +45,123 @@ static void server(sd_bus *b, usec_t *result) {
 
                 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);
+                if (sd_bus_message_is_method_call(m, "benchmark.server", "Ping"))
+                        assert_se(sd_bus_reply_method_return(b, m, NULL) >= 0);
+                else if (sd_bus_message_is_method_call(m, "benchmark.server", "Work")) {
+                        const void *p;
+                        size_t sz;
 
-                        for (i = 0; i < sz; i++)
-                                q[i] = toupper(p[i]);
+                        /* Make sure the mmap is mapped */
+                        assert_se(sd_bus_message_read_array(m, 'y', &p, &sz) > 0);
 
-                        x = now(CLOCK_MONOTONIC) - x;
-
-                        assert_se(sd_bus_send(b, reply, NULL) >= 0);
+                        assert_se(sd_bus_reply_method_return(b, m, NULL) >= 0);
                 } else if (sd_bus_message_is_method_call(m, "benchmark.server", "Exit")) {
-                        usec_t t;
+                        uint64_t res;
+                        assert_se(sd_bus_message_read(m, "t", &res) > 0);
 
-                        assert_se(sd_bus_message_read(m, "t", &t) > 0);
-                        assert_se(t >= x);
-                        *result = t - x;
+                        *result = res;
                         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) {
+static void transaction(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);
+        /* size_t psz, i; */
+        uint8_t *p;
 
         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);
+        /* Touch every page */
+        /* psz = page_size(); */
+        /* for (i = 0; i < sz; i += psz) */
+        /*         p[i] = 'X'; */
 
-        t = now(CLOCK_MONOTONIC);
         assert_se(sd_bus_send_with_reply_and_block(b, m, 0, NULL, &reply) >= 0);
-        t = now(CLOCK_MONOTONIC) - t;
+}
+
+static void client(const char *address) {
+        _cleanup_bus_message_unref_ sd_bus_message *x = NULL;
+        size_t lsize, rsize, csize;
+        sd_bus *b;
+        int r;
+
+        r = sd_bus_new(&b);
+        assert_se(r >= 0);
+
+        r = sd_bus_set_address(b, address);
+        assert_se(r >= 0);
+
+        r = sd_bus_start(b);
+        assert_se(r >= 0);
+
+        assert_se(sd_bus_call_method(b, ":1.1", "/", "benchmark.server", "Ping", NULL, NULL, NULL) >= 0);
+
+        lsize = 1;
+        rsize = MAX_SIZE;
+
+        for (;;) {
+                usec_t copy, memfd, t;
+                unsigned i;
+
+                csize = (lsize + rsize) / 2;
+
+                log_info("Trying size=%zu", csize);
+
+                if (csize <= lsize)
+                        break;
+
+                if (csize <= 0)
+                        break;
 
-        assert_se(sd_bus_message_read_array(reply, 'y', (const void**) &q, &l) > 0);
-        assert_se(l == sz);
+                log_info("copying...");
+                b->use_memfd = 0;
+                t = now(CLOCK_MONOTONIC);
+                for (i = 0; i <  N_TRIES; i++)
+                        transaction(b, csize);
+                copy = (now(CLOCK_MONOTONIC) - t);
+                log_info("%llu usec per copy transaction", (unsigned long long) (copy / N_TRIES));
+
+                log_info("sending memfd...");
+                b->use_memfd = -1;
+                t = now(CLOCK_MONOTONIC);
+                for (i = 0; i <  N_TRIES; i++)
+                        transaction(b, csize);
+                memfd = (now(CLOCK_MONOTONIC) - t);
+                log_info("%llu usec per memfd transaction", (unsigned long long) (memfd / N_TRIES));
 
-        for (i = 0; i < sz; i++) {
-                assert_se(q[i] == 'A' + (char) (i % 26));
+                if (copy == memfd)
+                        break;
+
+                if (copy < memfd)
+                        lsize = csize;
+                else
+                        rsize = csize;
         }
 
-        sd_bus_message_unref(m);
+        assert_se(sd_bus_message_new_method_call(b, ":1.1", "/", "benchmark.server", "Exit", &x) >= 0);
+        assert_se(sd_bus_message_append(x, "t", csize) >= 0);
+        assert_se(sd_bus_send(b, x, NULL) >= 0);
 
-        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);
+        sd_bus_unref(b);
 }
 
-static void run_benchmark(size_t sz, bool force_copy, usec_t *result) {
-
-        _cleanup_close_ int bus_ref = -1;
+int main(int argc, char *argv[]) {
         _cleanup_free_ char *bus_name = NULL, *address = NULL;
+        _cleanup_close_ int bus_ref = -1;
+        cpu_set_t cpuset;
+        size_t result;
         sd_bus *b;
-        int r;
         pid_t pid;
+        int r;
+
+        log_set_max_level(LOG_DEBUG);
 
         bus_ref = bus_kernel_create("deine-mutter", &bus_name);
         if (bus_ref == -ENOENT)
@@ -138,88 +175,41 @@ static void run_benchmark(size_t sz, bool force_copy, usec_t *result) {
         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);
 
+        sync();
+        setpriority(PRIO_PROCESS, 0, -19);
+
         pid = fork();
         assert_se(pid >= 0);
 
         if (pid == 0) {
+                CPU_ZERO(&cpuset);
+                CPU_SET(0, &cpuset);
+                pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
+
                 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);
+                client(address);
                 _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;
+        CPU_ZERO(&cpuset);
+        CPU_SET(1, &cpuset);
+        pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
 
-        log_set_max_level(LOG_DEBUG);
+        server(b, &result);
 
-        lsize = 1;
-        rsize = MAX_SIZE;
+        log_info("Copying/memfd are equally fast at %zu", result);
 
-        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;
-        }
+        assert_se(waitpid(pid, NULL, 0) == pid);
 
-        log_info("Copying/memfd are equally fast at %zu", csize);
+        sd_bus_unref(b);
 
         return 0;
 }