From 0a5a97b70e0a75df96be53b7e9d8740819ef96f8 Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Fri, 21 Jan 2022 16:31:53 +0100 Subject: [PATCH] libdbus-p2p: add a dual forked test Instead of a separate client and server binaries, the common test combines both and forks, similar to how the other tests implement this. Change-Id: I7124861bb21330df4aed6ffd02c5b2492408342e Signed-off-by: Michal Bloch --- Makefile | 6 ++- benchmark/libdbus-p2p-client.cpp | 16 ++++--- benchmark/libdbus-p2p-common.cpp | 72 ++++++++++++++++++++++++++++++++ benchmark/libdbus-p2p-server.cpp | 21 ++++++---- packaging/dbus-tools.spec | 1 + 5 files changed, 100 insertions(+), 16 deletions(-) create mode 100644 benchmark/libdbus-p2p-common.cpp diff --git a/Makefile b/Makefile index 42d09a5..5788af6 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ DFTSRC=benchmark/common.c CXXFLAGS = -std=c++2a -Wall -g `pkg-config --cflags dbus-1` LDLIBS = -pthread `pkg-config --libs dbus-1` -all: libdbus-p2p-client libdbus-p2p-server +all: libdbus-p2p-client libdbus-p2p-server libdbus-p2p-common gcc $(CFLAGS) -pg -o benchmark/pipe $(DFTSRC) benchmark/pipe.c $(LDFLAGS) gcc $(CFLAGS) -o benchmark/socket $(DFTSRC) benchmark/socket.c $(LDFLAGS) gcc $(CFLAGS) -o benchmark/sharedmem $(DFTSRC) benchmark/sharedmem.c $(LDFLAGS) @@ -22,6 +22,9 @@ libdbus-p2p-client: benchmark/libdbus-p2p-client.cpp libdbus-p2p-server: benchmark/libdbus-p2p-server.cpp $(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o benchmark/$@ +libdbus-p2p-common: benchmark/libdbus-p2p-common.cpp benchmark/libdbus-p2p-client.cpp benchmark/libdbus-p2p-server.cpp + $(CXX) $(CXXFLAGS) -DLIBDBUS_P2P_COMMON $^ $(LDLIBS) -o benchmark/$@ + install: make -C policychecker install mkdir -p $(DESTDIR)/usr/bin @@ -32,5 +35,6 @@ install: cp benchmark/p2p-gdbus $(DESTDIR)/usr/bin/p2p-gdbus cp benchmark/libdbus-p2p-server $(DESTDIR)/usr/bin/libdbus-p2p-server cp benchmark/libdbus-p2p-client $(DESTDIR)/usr/bin/libdbus-p2p-client + cp benchmark/libdbus-p2p-common $(DESTDIR)/usr/bin/libdbus-p2p-common cp benchmark/libdbus $(DESTDIR)/usr/bin/libdbus cp benchmark/dbus-tools-batch-tests.sh $(DESTDIR)/usr/bin/dbus-tools-batch-tests.sh diff --git a/benchmark/libdbus-p2p-client.cpp b/benchmark/libdbus-p2p-client.cpp index 9788265..12a7b76 100644 --- a/benchmark/libdbus-p2p-client.cpp +++ b/benchmark/libdbus-p2p-client.cpp @@ -34,7 +34,7 @@ static constexpr const char * TEST_PATH = "/org/test"; static constexpr const char * TEST_NAME = "org.test"; static constexpr const char * TEST_METHOD = "Perf"; -struct run_opts { +struct client_run_opts { bool show_help = false; /* The value is mostly conforming to other test's default, @@ -63,9 +63,9 @@ struct run_opts { std::string sock_path = "/tmp/test"; }; -run_opts get_opt (int argc, char ** argv) +static client_run_opts get_opt (int argc, char ** argv) { - run_opts ret; + client_run_opts ret; int opt; while ((opt = getopt(argc, argv, "d:m:t:p:h")) != -1) @@ -92,7 +92,7 @@ run_opts get_opt (int argc, char ** argv) return ret; } -void show_help (const char * argv0) +static void show_help (const char * argv0) { std::cout << "Usage: " << argv0 << " [-m message_size] " @@ -103,9 +103,9 @@ void show_help (const char * argv0) ; } -int main (int argc, char ** argv) +int main_client (int argc, char ** argv) { - run_opts opt = get_opt (argc, argv); + client_run_opts opt = get_opt (argc, argv); if (opt.show_help) { show_help (argv[0]); @@ -161,3 +161,7 @@ int main (int argc, char ** argv) return EXIT_SUCCESS; } + +#ifndef LIBDBUS_P2P_COMMON +int main(int argc, char ** argv) { return main_client(argc, argv); } +#endif diff --git a/benchmark/libdbus-p2p-common.cpp b/benchmark/libdbus-p2p-common.cpp new file mode 100644 index 0000000..1c01371 --- /dev/null +++ b/benchmark/libdbus-p2p-common.cpp @@ -0,0 +1,72 @@ +/* MIT License + * + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ + +#include + +#include +#include +#include +#include + +using namespace std::chrono_literals; + +extern int main_server(int, char **); +extern int main_client(int, char **); + +int main(int argc, char ** argv) +{ + /* Runs the server and the client from the same binary via fork. + * This is in an attempt to make the test behave more like the + * other tests which work that way. See the server and the client + * for their argument format and other implementation details. */ + + int separator = 0; + while (strcmp(argv[separator], "--") && separator < argc) + ++separator; + if (separator == argc) { + std::cerr << "Usage: " << argv[0] << " serverargs -- clientargs" << std::endl; + return EXIT_FAILURE; + } + + int child_pid = fork (); + if (child_pid == -1) { + std::cerr << "fork failed! " << errno << std::endl; + return EXIT_FAILURE; + } + + if (child_pid == 0) { + argc = separator; + argv[separator] = nullptr; + + return main_server (argc, argv); + } else { + /* Let the server set up, + * even under heavy load. */ + std::this_thread::sleep_for (10s); + + /* NB: argv[0] stays (program name), while + * argv[argc] is still part of the array. */ + for (int i = 1; i <= argc - separator; ++i) + argv[i] = argv[i + separator]; + return main_client (argc - separator, argv); + } +} diff --git a/benchmark/libdbus-p2p-server.cpp b/benchmark/libdbus-p2p-server.cpp index f231e5a..86628d7 100644 --- a/benchmark/libdbus-p2p-server.cpp +++ b/benchmark/libdbus-p2p-server.cpp @@ -40,7 +40,7 @@ static constexpr const char * TEST_PATH = "/org/test"; -struct run_opts { +struct server_run_opts { bool show_help = false; /* With the default client settings, this is 5 seconds' worth of @@ -73,9 +73,9 @@ struct run_opts { std::optional raw_data_path = std::nullopt; }; -run_opts get_opt (int argc, char ** argv) +static server_run_opts get_opt (int argc, char ** argv) { - run_opts ret; + server_run_opts ret; int opt; while ((opt = getopt(argc, argv, ":ihp:r:t:w")) != -1) @@ -137,7 +137,7 @@ struct conn_with_metadata { static DBusObjectPathVTable vtable; - conn_with_metadata (const run_opts & ro, DBusConnection * c) + conn_with_metadata (const server_run_opts & ro, DBusConnection * c) : total_time (0) , total_count (0) , min_time (std::numeric_limits :: max ()) @@ -263,7 +263,7 @@ DBusObjectPathVTable conn_with_metadata::vtable = , nullptr }; -void show_help (const char * argv0) +static void show_help (const char * argv0) { std::cout << "Usage: " << argv0 << " [-t warmup_tries]" @@ -275,7 +275,7 @@ void show_help (const char * argv0) ; } -void dispatch_connection (conn_with_metadata & cwm, bool busy_wait) +static void dispatch_connection (conn_with_metadata & cwm, bool busy_wait) { if (dbus_connection_read_write_dispatch (cwm.conn.get(), busy_wait ? 0 : -1)) return; @@ -305,7 +305,7 @@ struct DBusServer_deleter { } }; -void read_from_watch (DBusWatch * watch, bool wait_infinitely) +static void read_from_watch (DBusWatch * watch, bool wait_infinitely) { struct pollfd pfd; pfd.events = POLLIN; @@ -329,12 +329,12 @@ extern "C" void sig_handler (int sig) std::exit (EXIT_SUCCESS); } -int main (int argc, char ** argv) +int main_server (int argc, char ** argv) { std::signal (SIGINT , sig_handler); std::signal (SIGTERM, sig_handler); - run_opts opt = get_opt (argc, argv); + server_run_opts opt = get_opt (argc, argv); if (opt.show_help) { show_help (argv[0]); @@ -412,3 +412,6 @@ int main (int argc, char ** argv) return EXIT_SUCCESS; } +#ifndef LIBDBUS_P2P_COMMON +int main(int argc, char ** argv) { return main_server(argc, argv); } +#endif diff --git a/packaging/dbus-tools.spec b/packaging/dbus-tools.spec index 1822d11..85265ba 100644 --- a/packaging/dbus-tools.spec +++ b/packaging/dbus-tools.spec @@ -58,5 +58,6 @@ make %{_bindir}/libdbus %{_bindir}/libdbus-p2p-server %{_bindir}/libdbus-p2p-client +%{_bindir}/libdbus-p2p-common %{_bindir}/p2p-gdbus %{_bindir}/dbus-tools-batch-tests.sh -- 2.34.1