chiark / gitweb /
nspawn: set up a kdbus namespace when starting a container
[elogind.git] / src / libsystemd-bus / test-bus-kernel-benchmark.c
index 0cf685e6de81dd98c69f49f449adf885883c8508..010a161328f43ca0a7d4684a5964563355d583d6 100644 (file)
 
 #include "util.h"
 #include "log.h"
 
 #include "util.h"
 #include "log.h"
+#include "time-util.h"
 
 #include "sd-bus.h"
 #include "bus-message.h"
 #include "bus-error.h"
 #include "bus-kernel.h"
 #include "bus-internal.h"
 
 #include "sd-bus.h"
 #include "bus-message.h"
 #include "bus-error.h"
 #include "bus-kernel.h"
 #include "bus-internal.h"
+#include "bus-util.h"
 
 
-#define N_TRIES 10000
-#define MAX_SIZE (1*1024*1024)
+#define MAX_SIZE (4*1024*1024)
+
+static usec_t arg_loop_usec = 100 * USEC_PER_MSEC;
 
 static void server(sd_bus *b, size_t *result) {
         int r;
 
 static void server(sd_bus *b, size_t *result) {
         int r;
@@ -49,7 +52,7 @@ static void server(sd_bus *b, size_t *result) {
                         continue;
 
                 if (sd_bus_message_is_method_call(m, "benchmark.server", "Ping"))
                         continue;
 
                 if (sd_bus_message_is_method_call(m, "benchmark.server", "Ping"))
-                        assert_se(sd_bus_reply_method_return(b, m, NULL) >= 0);
+                        assert_se(sd_bus_reply_method_return(m, NULL) >= 0);
                 else if (sd_bus_message_is_method_call(m, "benchmark.server", "Work")) {
                         const void *p;
                         size_t sz;
                 else if (sd_bus_message_is_method_call(m, "benchmark.server", "Work")) {
                         const void *p;
                         size_t sz;
@@ -57,7 +60,7 @@ static void server(sd_bus *b, size_t *result) {
                         /* Make sure the mmap is mapped */
                         assert_se(sd_bus_message_read_array(m, 'y', &p, &sz) > 0);
 
                         /* Make sure the mmap is mapped */
                         assert_se(sd_bus_message_read_array(m, 'y', &p, &sz) > 0);
 
-                        assert_se(sd_bus_reply_method_return(b, m, NULL) >= 0);
+                        assert_se(sd_bus_reply_method_return(m, NULL) >= 0);
                 } else if (sd_bus_message_is_method_call(m, "benchmark.server", "Exit")) {
                         uint64_t res;
                         assert_se(sd_bus_message_read(m, "t", &res) > 0);
                 } else if (sd_bus_message_is_method_call(m, "benchmark.server", "Exit")) {
                         uint64_t res;
                         assert_se(sd_bus_message_read(m, "t", &res) > 0);
@@ -72,21 +75,17 @@ static void server(sd_bus *b, size_t *result) {
 
 static void transaction(sd_bus *b, size_t sz) {
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
 
 static void transaction(sd_bus *b, size_t sz) {
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
-        /* 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);
 
         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);
 
-        /* Touch every page */
-        /* psz = page_size(); */
-        /* for (i = 0; i < sz; i += psz) */
-        /*         p[i] = 'X'; */
+        memset(p, 0x80, sz);
 
 
-        assert_se(sd_bus_send_with_reply_and_block(b, m, 0, NULL, &reply) >= 0);
+        assert_se(sd_bus_call(b, m, 0, NULL, &reply) >= 0);
 }
 
 }
 
-static void client(const char *address) {
+static void client_bisect(const char *address) {
         _cleanup_bus_message_unref_ sd_bus_message *x = NULL;
         size_t lsize, rsize, csize;
         sd_bus *b;
         _cleanup_bus_message_unref_ sd_bus_message *x = NULL;
         size_t lsize, rsize, csize;
         sd_bus *b;
@@ -106,45 +105,108 @@ static void client(const char *address) {
         lsize = 1;
         rsize = MAX_SIZE;
 
         lsize = 1;
         rsize = MAX_SIZE;
 
+        printf("SIZE\tCOPY\tMEMFD\n");
+
         for (;;) {
         for (;;) {
-                usec_t copy, memfd, t;
-                unsigned i;
+                usec_t t;
+                unsigned n_copying, n_memfd;
 
                 csize = (lsize + rsize) / 2;
 
 
                 csize = (lsize + rsize) / 2;
 
-                log_info("Trying size=%zu", csize);
-
                 if (csize <= lsize)
                         break;
 
                 if (csize <= 0)
                         break;
 
                 if (csize <= lsize)
                         break;
 
                 if (csize <= 0)
                         break;
 
-                log_info("copying...");
+                printf("%zu\t", csize);
+
                 b->use_memfd = 0;
                 b->use_memfd = 0;
+
                 t = now(CLOCK_MONOTONIC);
                 t = now(CLOCK_MONOTONIC);
-                for (i = 0; i <  N_TRIES; i++)
+                for (n_copying = 0;; n_copying++) {
                         transaction(b, csize);
                         transaction(b, csize);
-                copy = (now(CLOCK_MONOTONIC) - t);
-                log_info("%llu usec per copy transaction", (unsigned long long) (copy / N_TRIES));
+                        if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
+                                break;
+                }
+                printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec));
 
 
-                log_info("sending memfd...");
                 b->use_memfd = -1;
                 b->use_memfd = -1;
+
                 t = now(CLOCK_MONOTONIC);
                 t = now(CLOCK_MONOTONIC);
-                for (i = 0; i <  N_TRIES; i++)
+                for (n_memfd = 0;; n_memfd++) {
                         transaction(b, csize);
                         transaction(b, csize);
-                memfd = (now(CLOCK_MONOTONIC) - t);
-                log_info("%llu usec per memfd transaction", (unsigned long long) (memfd / N_TRIES));
+                        if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
+                                break;
+                }
+                printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec));
 
 
-                if (copy == memfd)
+                if (n_copying == n_memfd)
                         break;
 
                         break;
 
-                if (copy < memfd)
+                if (n_copying > n_memfd)
                         lsize = csize;
                 else
                         rsize = csize;
         }
 
                         lsize = csize;
                 else
                         rsize = csize;
         }
 
+        b->use_memfd = 1;
+        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);
+
+        sd_bus_unref(b);
+}
+
+static void client_chart(const char *address) {
+        _cleanup_bus_message_unref_ sd_bus_message *x = NULL;
+        size_t 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);
+
+        printf("SIZE\tCOPY\tMEMFD\n");
+
+        for (csize = 1; csize <= MAX_SIZE; csize *= 2) {
+                usec_t t;
+                unsigned n_copying, n_memfd;
+
+                printf("%zu\t", csize);
+
+                b->use_memfd = 0;
+
+                t = now(CLOCK_MONOTONIC);
+                for (n_copying = 0;; n_copying++) {
+                        transaction(b, csize);
+                        if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
+                                break;
+                }
+
+                printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec));
+
+                b->use_memfd = -1;
+
+                t = now(CLOCK_MONOTONIC);
+                for (n_memfd = 0;; n_memfd++) {
+                        transaction(b, csize);
+                        if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
+                                break;
+                }
+
+                printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec));
+        }
+
+        b->use_memfd = 1;
         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", &x) >= 0);
         assert_se(sd_bus_message_append(x, "t", csize) >= 0);
         assert_se(sd_bus_send(b, x, NULL) >= 0);
@@ -153,6 +215,11 @@ static void client(const char *address) {
 }
 
 int main(int argc, char *argv[]) {
 }
 
 int main(int argc, char *argv[]) {
+        enum {
+                MODE_BISECT,
+                MODE_CHART,
+        } mode = MODE_BISECT;
+        int i;
         _cleanup_free_ char *bus_name = NULL, *address = NULL;
         _cleanup_close_ int bus_ref = -1;
         cpu_set_t cpuset;
         _cleanup_free_ char *bus_name = NULL, *address = NULL;
         _cleanup_close_ int bus_ref = -1;
         cpu_set_t cpuset;
@@ -161,9 +228,18 @@ int main(int argc, char *argv[]) {
         pid_t pid;
         int r;
 
         pid_t pid;
         int r;
 
-        log_set_max_level(LOG_DEBUG);
+        for (i = 1; i < argc; i++) {
+                if (streq(argv[i], "chart")) {
+                        mode = MODE_CHART;
+                        continue;
+                }
 
 
-        bus_ref = bus_kernel_create("deine-mutter", &bus_name);
+                assert_se(parse_sec(argv[i], &arg_loop_usec) >= 0);
+        }
+
+        assert_se(arg_loop_usec > 0);
+
+        bus_ref = bus_kernel_create_bus("deine-mutter", &bus_name);
         if (bus_ref == -ENOENT)
                 exit(EXIT_TEST_SKIP);
 
         if (bus_ref == -ENOENT)
                 exit(EXIT_TEST_SKIP);
 
@@ -195,7 +271,16 @@ int main(int argc, char *argv[]) {
                 close_nointr_nofail(bus_ref);
                 sd_bus_unref(b);
 
                 close_nointr_nofail(bus_ref);
                 sd_bus_unref(b);
 
-                client(address);
+                switch (mode) {
+                case MODE_BISECT:
+                        client_bisect(address);
+                        break;
+
+                case MODE_CHART:
+                        client_chart(address);
+                        break;
+                }
+
                 _exit(0);
         }
 
                 _exit(0);
         }
 
@@ -205,7 +290,8 @@ int main(int argc, char *argv[]) {
 
         server(b, &result);
 
 
         server(b, &result);
 
-        log_info("Copying/memfd are equally fast at %zu", result);
+        if (mode == MODE_BISECT)
+                printf("Copying/memfd are equally fast at %zu bytes\n", result);
 
         assert_se(waitpid(pid, NULL, 0) == pid);
 
 
         assert_se(waitpid(pid, NULL, 0) == pid);