From a552f5d0cc4266c5600211d1628f99d7296ca1e3 Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Tue, 19 Mar 2024 13:41:39 +0100 Subject: [PATCH] kdbus benchmark: measure latency via 2 threads Similar to other existing benchmarks Change-Id: I68f374939fcd135b5783ba41cc6b6200cc10fadd Signed-off-by: Michal Bloch --- tests/kdbus/kdbus-test.c | 8 +++++ tests/kdbus/kdbus-test.h | 1 + tests/kdbus/test-benchmark.c | 61 ++++++++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/tests/kdbus/kdbus-test.c b/tests/kdbus/kdbus-test.c index 37875ef..062efc6 100644 --- a/tests/kdbus/kdbus-test.c +++ b/tests/kdbus/kdbus-test.c @@ -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]))) diff --git a/tests/kdbus/kdbus-test.h b/tests/kdbus/kdbus-test.h index 2e6bdc4..d8d170f 100644 --- a/tests/kdbus/kdbus-test.h +++ b/tests/kdbus/kdbus-test.h @@ -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); diff --git a/tests/kdbus/test-benchmark.c b/tests/kdbus/test-benchmark.c index 51fbcab..0681496 100644 --- a/tests/kdbus/test-benchmark.c +++ b/tests/kdbus/test-benchmark.c @@ -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; -- 2.34.1