kdbus benchmark: measure latency via 2 threads 39/308339/4
authorMichal Bloch <m.bloch@samsung.com>
Tue, 19 Mar 2024 12:41:39 +0000 (13:41 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 2 Apr 2024 16:44:45 +0000 (18:44 +0200)
Similar to other existing benchmarks

Change-Id: I68f374939fcd135b5783ba41cc6b6200cc10fadd
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
tests/kdbus/kdbus-test.c
tests/kdbus/kdbus-test.h
tests/kdbus/test-benchmark.c

index 37875ef50648224a3e9eb3c88df53d4c341141bc..062efc6f1e05b3fcd1f73960b82ca29baf7d4d83 100644 (file)
@@ -367,6 +367,14 @@ static const struct kdbus_test tests[] = {
                .timeout = 10,
                .manual_only = true,
        },
+       {
+               .name   = "benchmark-latency-mt",
+               .desc   = "latency benchmark, multithreaded",
+               .func   = kdbus_test_benchmark_mt,
+               .flags  = TEST_CREATE_BUS,
+               .timeout = 10,
+               .manual_only = true,
+       },
 };
 
 #define N_TESTS ((int) (sizeof(tests) / sizeof(tests[0])))
index 2e6bdc4c75060739b0b8fdc5f3a1046ddc92dbd3..d8d170f4b47a97a62614e161c8e8c9426a9565ec 100644 (file)
@@ -82,6 +82,7 @@ wur int kdbus_test_benchmark(struct kdbus_test_env *env);
 wur int kdbus_test_benchmark_nomemfds(struct kdbus_test_env *env);
 wur int kdbus_test_benchmark_uds(struct kdbus_test_env *env);
 wur int kdbus_test_benchmark_bandwidth(struct kdbus_test_env *env);
+wur int kdbus_test_benchmark_mt(struct kdbus_test_env *env);
 wur int kdbus_test_bus_make(struct kdbus_test_env *env);
 wur int kdbus_test_byebye(struct kdbus_test_env *env);
 wur int kdbus_test_chat(struct kdbus_test_env *env);
index 51fbcab7e239f23f72436202dbc7f801ad74a42e..06814969ff51ef5e4da9d95b36235a801b017783 100644 (file)
@@ -35,6 +35,7 @@ static bool use_memfd = true;         /* transmit memfd? */
 static bool compare_uds = false;               /* unix-socket comparison? */
 static bool attach_none = false;               /* clear attach-flags? */
 static bool is_bandwidth = false;              /* is this a bandwidth test? */
+static bool measure_latency_with_fork = false; /* are we measuring latency with separate or same thread? */
 static char *stress_payload;
 
 struct stats {
@@ -421,14 +422,33 @@ static wur int benchmark(struct kdbus_test_env *env)
 struct bandwidth_worker_data {
        int i;
        struct kdbus_test_env *env;
+       struct kdbus_conn *conn;
 };
 
-void *bandwidth_receiver_thread(void *conn)
+// only used in the latency test -> always just 1 sender/receiver pair
+_Atomic uint64_t start_timestamp_mt;
+_Atomic bool pending;
+
+void *bandwidth_receiver_thread(void *_data)
 {
+       struct bandwidth_worker_data *data = _data;
+
+       uint64_t start = now(CLOCK_REALTIME);
        while (1) {
-               if (handle_echo_reply((struct kdbus_conn *) conn, 0)) {
+               while (measure_latency_with_fork && !pending);
+
+               if (handle_echo_reply(data->conn, start_timestamp_mt)) {
                        // no can do
                }
+               if (measure_latency_with_fork) {
+                       const uint64_t current = now(CLOCK_REALTIME);
+                       if (current - start > 1000000000ULL) {
+                               dump_stats(false);
+                               reset_stats();
+                               start = current;
+                       }
+                       pending = false;
+               }
        }
 
        return NULL;
@@ -454,13 +474,22 @@ int bandwidth_sender_thread(struct bandwidth_worker_data *data)
        ASSERT_ZERO(setup_simple_kdbus_msg(conn_b, conn_a->id, data->env->payload, &kdbus_msg));
        ASSERT_NO_PENDING(conn_a);
 
+       data->conn = conn_a;
+
        pthread_t p;
-       pthread_create(&p, NULL, bandwidth_receiver_thread, conn_a);
+       pthread_create(&p, NULL, bandwidth_receiver_thread, data);
 
        while (1) {
+               if (measure_latency_with_fork) {
+                       while (pending);
+
+                       start_timestamp_mt = now(CLOCK_REALTIME);
+               }
                if (send_echo_request(conn_b, kdbus_msg, 0)) {
                        // no can do
                }
+               if (measure_latency_with_fork)
+                       pending = true;
        }
 
        return TEST_OK;
@@ -510,6 +539,22 @@ static wur int benchmark_bandwidth(struct kdbus_test_env *env)
        return TEST_OK;
 }
 
+
+static wur int benchmark_latency_mt(struct kdbus_test_env *env)
+{
+       setlocale(LC_ALL, "");
+
+       stress_payload = malloc(env->payload);
+       ASSERT_NONZERO(stress_payload);
+       for (int i = 0; i < env->payload; i++)
+               stress_payload[i] = i;
+
+       bandwidth_sender_thread(&(struct bandwidth_worker_data){ .i = 0, .env = env });
+
+       return TEST_OK;
+}
+
+
 wur int kdbus_test_benchmark(struct kdbus_test_env *env)
 {
        use_memfd = true;
@@ -528,6 +573,16 @@ wur int kdbus_test_benchmark_bandwidth(struct kdbus_test_env *env)
        return benchmark_bandwidth(env);
 }
 
+wur int kdbus_test_benchmark_mt(struct kdbus_test_env *env)
+{
+       use_memfd = false;
+       attach_none = false;
+       compare_uds = false;
+       is_bandwidth = false;
+       measure_latency_with_fork = true;
+       return benchmark_latency_mt(env);
+}
+
 wur int kdbus_test_benchmark_nomemfds(struct kdbus_test_env *env)
 {
        use_memfd = false;