chiark / gitweb /
bus: benchmark - adjust printf and MAX_SIZE
[elogind.git] / src / libsystemd-bus / test-bus-kernel-benchmark.c
index 0cf685e6de81dd98c69f49f449adf885883c8508..d9ccdc223947fea8b3bd0df73f19dc37932a3ff8 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "util.h"
 #include "log.h"
+#include "time-util.h"
 
 #include "sd-bus.h"
 #include "bus-message.h"
@@ -31,8 +32,9 @@
 #include "bus-kernel.h"
 #include "bus-internal.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;
@@ -72,21 +74,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;
-        /* 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);
 
-        /* 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);
 }
 
-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;
@@ -106,45 +104,108 @@ static void client(const char *address) {
         lsize = 1;
         rsize = MAX_SIZE;
 
+        printf("SIZE\tCOPY\tMEMFD\n");
+
         for (;;) {
-                usec_t copy, memfd, t;
-                unsigned i;
+                usec_t t;
+                unsigned n_copying, n_memfd;
 
                 csize = (lsize + rsize) / 2;
 
-                log_info("Trying size=%zu", csize);
-
                 if (csize <= lsize)
                         break;
 
                 if (csize <= 0)
                         break;
 
-                log_info("copying...");
+                printf("%zu\t", csize);
+
                 b->use_memfd = 0;
+
                 t = now(CLOCK_MONOTONIC);
-                for (i = 0; i <  N_TRIES; i++)
+                for (n_copying = 0;; n_copying++) {
                         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;
+
                 t = now(CLOCK_MONOTONIC);
-                for (i = 0; i <  N_TRIES; i++)
+                for (n_memfd = 0;; n_memfd++) {
                         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_copying * USEC_PER_SEC) / arg_loop_usec));
 
-                if (copy == memfd)
+                if (n_copying == n_memfd)
                         break;
 
-                if (copy < memfd)
+                if (n_copying > n_memfd)
                         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);
@@ -153,6 +214,11 @@ static void client(const char *address) {
 }
 
 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;
@@ -163,6 +229,17 @@ int main(int argc, char *argv[]) {
 
         log_set_max_level(LOG_DEBUG);
 
+        for (i = 1; i < argc; i++) {
+                if (streq(argv[i], "chart")) {
+                        mode = MODE_CHART;
+                        continue;
+                }
+
+                assert_se(parse_sec(argv[i], &arg_loop_usec) >= 0);
+        }
+
+        assert_se(arg_loop_usec > 0);
+
         bus_ref = bus_kernel_create("deine-mutter", &bus_name);
         if (bus_ref == -ENOENT)
                 exit(EXIT_TEST_SKIP);
@@ -195,7 +272,16 @@ int main(int argc, char *argv[]) {
                 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);
         }
 
@@ -205,7 +291,8 @@ int main(int argc, char *argv[]) {
 
         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);