From 0def322dc4c8692432b25dd6f3f2d7f493725aa3 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 19 Sep 2024 08:13:05 +0000 Subject: [PATCH] ctx: Move the context declaration into its own source file Signed-off-by: Michael Tremer --- Makefile.am | 2 ++ src/client.c | 1 + src/client.h | 1 + src/ctx.c | 21 ++++++++++++++++++ src/ctx.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/logging.h | 2 +- src/main.c | 3 +++ src/main.h | 26 +---------------------- src/random.c | 2 +- src/random.h | 4 ++-- src/server.h | 2 +- src/worker.h | 2 +- 12 files changed, 94 insertions(+), 31 deletions(-) create mode 100644 src/ctx.c create mode 100644 src/ctx.h diff --git a/Makefile.am b/Makefile.am index 16c30da..f1ed703 100644 --- a/Makefile.am +++ b/Makefile.am @@ -62,6 +62,8 @@ bin_PROGRAMS = \ fireperf_SOURCES = \ src/client.c \ src/client.h \ + src/ctx.c \ + src/ctx.h \ src/logging.c \ src/logging.h \ src/main.c \ diff --git a/src/client.c b/src/client.c index 5d7b6ae..691c661 100644 --- a/src/client.c +++ b/src/client.c @@ -26,6 +26,7 @@ #include #include "client.h" +#include "ctx.h" #include "logging.h" #include "main.h" #include "random.h" diff --git a/src/client.h b/src/client.h index b0a3f21..9ff4b76 100644 --- a/src/client.h +++ b/src/client.h @@ -21,6 +21,7 @@ #ifndef FIREPERF_CLIENT_H #define FIREPERF_CLIENT_H +#include "ctx.h" #include "main.h" int fireperf_client(struct fireperf_ctx* ctx, struct fireperf_stats* stats, diff --git a/src/ctx.c b/src/ctx.c new file mode 100644 index 0000000..ebf0b04 --- /dev/null +++ b/src/ctx.c @@ -0,0 +1,21 @@ +/*############################################################################# +# # +# fireperf - A network benchmarking tool # +# Copyright (C) 2024 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include "ctx.h" diff --git a/src/ctx.h b/src/ctx.h new file mode 100644 index 0000000..e31ddad --- /dev/null +++ b/src/ctx.h @@ -0,0 +1,59 @@ +/*############################################################################# +# # +# fireperf - A network benchmarking tool # +# Copyright (C) 2024 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef FIREPERF_CTX_H +#define FIREPERF_CTX_H + +#include + +#define MAX_WORKERS 128 + +// Forward declarations +struct fireperf_random_pool; +struct fireperf_worker; + +struct fireperf_ctx { + int terminated; + int loglevel; + enum { + FIREPERF_MODE_NONE = 0, + FIREPERF_MODE_CLIENT, + FIREPERF_MODE_SERVER, + } mode; + struct in6_addr address; + int duplex; + int keepalive_only; + int keepalive_count; + int keepalive_interval; + struct fireperf_random_pool* pool; + int port; + unsigned int listening_sockets; + unsigned long parallel; + unsigned int timeout; + int close; + int zero; + + // Workers + struct fireperf_workers* workers[MAX_WORKERS]; + unsigned int num_workers; + unsigned int max_workers; +}; + +#endif /* FIREPERF_CTX_H */ diff --git a/src/logging.h b/src/logging.h index 7e05f7e..f7ee661 100644 --- a/src/logging.h +++ b/src/logging.h @@ -23,7 +23,7 @@ #include -#include "main.h" +#include "ctx.h" static inline void __attribute__((always_inline, format(printf, 2, 3))) fireperf_log_null(struct fireperf_ctx* ctx, const char* format, ...) {} diff --git a/src/main.c b/src/main.c index 356989f..afe2c76 100644 --- a/src/main.c +++ b/src/main.c @@ -278,6 +278,9 @@ int main(int argc, char* argv[]) { // Initialise random number generator srandom(time(NULL)); + // Fetch how many workers we would launch + ctx.max_workers = sysconf(_SC_NPROCESSORS_ONLN); + // Set limits r = set_limits(&ctx); if (r) diff --git a/src/main.h b/src/main.h index 215a58f..ae779f3 100644 --- a/src/main.h +++ b/src/main.h @@ -21,7 +21,6 @@ #ifndef FIREPERF_MAIN_H #define FIREPERF_MAIN_H -#include #include #define DEFAULT_KEEPALIVE_COUNT 3 @@ -41,30 +40,7 @@ #define EPOLL_MAX_EVENTS 128 -// Forward declaration -struct fireperf_random_pool; - -struct fireperf_ctx { - int terminated; - int loglevel; - enum { - FIREPERF_MODE_NONE = 0, - FIREPERF_MODE_CLIENT, - FIREPERF_MODE_SERVER, - } mode; - struct in6_addr address; - int duplex; - int keepalive_only; - int keepalive_count; - int keepalive_interval; - struct fireperf_random_pool* pool; - int port; - unsigned int listening_sockets; - unsigned long parallel; - unsigned int timeout; - int close; - int zero; -}; +#include "ctx.h" // Struct to collect statistics struct fireperf_stats { diff --git a/src/random.c b/src/random.c index b263df9..86a99ab 100644 --- a/src/random.c +++ b/src/random.c @@ -21,8 +21,8 @@ #include #include +#include "ctx.h" #include "logging.h" -#include "main.h" #include "random.h" struct fireperf_random_pool* fireperf_random_pool_create(struct fireperf_ctx* ctx, size_t size) { diff --git a/src/random.h b/src/random.h index 2aaa5a6..1fd8919 100644 --- a/src/random.h +++ b/src/random.h @@ -21,13 +21,13 @@ #ifndef FIREPERF_RANDOM_H #define FIREPERF_RANDOM_H -#include "main.h" - struct fireperf_random_pool { char* data; size_t size; }; +#include "ctx.h" + struct fireperf_random_pool* fireperf_random_pool_create( struct fireperf_ctx* ctx, size_t size); void fireperf_random_pool_free(struct fireperf_random_pool* pool); diff --git a/src/server.h b/src/server.h index 77ab911..fb5f095 100644 --- a/src/server.h +++ b/src/server.h @@ -21,7 +21,7 @@ #ifndef FIREPERF_SERVER_H #define FIREPERF_SERVER_H -#include "main.h" +#include "ctx.h" int fireperf_server(struct fireperf_ctx* ctx, int epollfd, int timerfd); diff --git a/src/worker.h b/src/worker.h index 4f3e74a..7059df5 100644 --- a/src/worker.h +++ b/src/worker.h @@ -23,7 +23,7 @@ struct fireperf_worker; -#include "main.h" +#include "ctx.h" int fireperf_worker_create(struct fireperf_worker** worker, struct fireperf_ctx* ctx); void fireperf_worker_free(struct fireperf_worker* worker); -- 2.47.2