From 6be7c193241d3a3dd1e024fab0f0babd1b45d264 Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Thu, 27 Jan 2022 15:50:16 +0100 Subject: [PATCH] libdbus-p2p: add CPU pinning Change-Id: I891b477723eef82db8721b8f9fc8f9910e753c21 Signed-off-by: Michal Bloch --- benchmark/libdbus-p2p-client.cpp | 20 +++++++++++++++++++- benchmark/libdbus-p2p-server.cpp | 19 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/benchmark/libdbus-p2p-client.cpp b/benchmark/libdbus-p2p-client.cpp index 9788265..75ff1cb 100644 --- a/benchmark/libdbus-p2p-client.cpp +++ b/benchmark/libdbus-p2p-client.cpp @@ -26,6 +26,7 @@ #include #include +#include #include using namespace std::chrono_literals; @@ -59,6 +60,12 @@ struct run_opts { * and will just take however many tries it managed to receive. */ size_t try_count = 120'000; + /* Pins the client to a specific CPU. If the server lives on the + * same CPU as the client, it will be able to process the message + * as soon as the client goes to sleep (trading off parallelism + * for better "timings" on the process scheduler). */ + std::optional cpu_pin = std::nullopt; + /* Matches the server's default for convenience. */ std::string sock_path = "/tmp/test"; }; @@ -68,9 +75,12 @@ run_opts get_opt (int argc, char ** argv) run_opts ret; int opt; - while ((opt = getopt(argc, argv, "d:m:t:p:h")) != -1) + while ((opt = getopt(argc, argv, "c:d:m:t:p:h")) != -1) switch (opt) { + case 'c': + ret.cpu_pin = std::stoi(optarg); + break; case 'm': ret.payload = std::stoi(optarg); break; @@ -99,6 +109,7 @@ void show_help (const char * argv0) << " [-t try_count] " << " [-d delay_usec] " << " [-p sock_path] " + << " [-c cpu_pin] " << std::endl ; } @@ -112,6 +123,13 @@ int main (int argc, char ** argv) return EXIT_FAILURE; } + if (opt.cpu_pin) { + cpu_set_t set; + CPU_ZERO (&set); + CPU_SET (opt.cpu_pin.value(), & set); + sched_setaffinity (getpid(), sizeof set, & set); + } + auto sock_path = "unix:path=" + opt.sock_path; auto conn = dbus_connection_open_private (sock_path.c_str(), nullptr); diff --git a/benchmark/libdbus-p2p-server.cpp b/benchmark/libdbus-p2p-server.cpp index 0540dfd..8f151ac 100644 --- a/benchmark/libdbus-p2p-server.cpp +++ b/benchmark/libdbus-p2p-server.cpp @@ -67,6 +67,12 @@ struct run_opts { * Good for %-based comparisons, but avoid for time measurements. */ bool busy_wait = false; + /* Pins the server to a specific CPU. If the server lives on the + * same CPU as the client, it will be able to process the message + * as soon as the client goes to sleep (trading off parallelism + * for better "timings" on the process scheduler). */ + std::optional cpu_pin = std::nullopt; + /* When enabled, the server collects all method call times and * writes them out to a CSV file under the specified path. Useful * for distribution charts. */ @@ -78,9 +84,12 @@ run_opts get_opt (int argc, char ** argv) run_opts ret; int opt; - while ((opt = getopt(argc, argv, ":ihp:r:t:w")) != -1) + while ((opt = getopt(argc, argv, ":c:ihp:r:t:w")) != -1) switch (opt) { + case 'c': + ret.cpu_pin = std::stoi(optarg); + break; case 'i': ret.infinite_listening = true; break; @@ -271,6 +280,7 @@ void show_help (const char * argv0) " [-i (infinite listening)]" " [-w (busy wait)]" " [-r [dump_csv_file_path]]" + " [-c cpu_pin]" << std::endl ; } @@ -338,6 +348,13 @@ int main (int argc, char ** argv) return EXIT_FAILURE; } + if (opt.cpu_pin) { + cpu_set_t set; + CPU_ZERO (&set); + CPU_SET (opt.cpu_pin.value(), & set); + sched_setaffinity (getpid(), sizeof set, & set); + } + DBusError derror; dbus_error_init (& derror); -- 2.34.1