libdbus-p2p: add CPU pinning 96/270296/1
authorMichal Bloch <m.bloch@samsung.com>
Thu, 27 Jan 2022 14:50:16 +0000 (15:50 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 27 Jan 2022 17:40:01 +0000 (18:40 +0100)
Change-Id: I891b477723eef82db8721b8f9fc8f9910e753c21
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
benchmark/libdbus-p2p-client.cpp
benchmark/libdbus-p2p-server.cpp

index 97882656ad581a99402a4875976b7221fa3fd04c..75ff1cbb4c2c982240e4bcb0f96a3131f4916a48 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <chrono>
 #include <iostream>
+#include <optional>
 #include <thread>
 
 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 <int> 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);
 
index 0540dfd4f37cf71a1c6cab7572723d0534a0f030..8f151ac02c891fe9e45047493de0f166b26d681d 100644 (file)
@@ -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 <int> 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);