From 1cd3f3281b19e2b739eca02a4b21f756e962f78c Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 18 Aug 2020 19:37:12 +0530 Subject: [PATCH] make thread pool size configurable via command line argument, default to #CPUs available --- src/exchange/taler-exchange-httpd.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/exchange/taler-exchange-httpd.c b/src/exchange/taler-exchange-httpd.c index bca7a6231..73cc3530d 100644 --- a/src/exchange/taler-exchange-httpd.c +++ b/src/exchange/taler-exchange-httpd.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include "taler_mhd_lib.h" @@ -116,6 +117,13 @@ struct TALER_EXCHANGEDB_Plugin *TEH_plugin; */ static unsigned int connection_timeout = 30; +/** + * How many threads to use. + * The default value (0) sets the actual number of threads + * based on the number of available cores. + */ +static unsigned int num_threads = 0; + /** * The HTTP Daemon. */ @@ -1137,6 +1145,8 @@ run_main_loop (int fh, { int ret; + GNUNET_assert (0 < num_threads); + mhd = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_PIPE_FOR_SHUTDOWN | MHD_USE_DEBUG | MHD_USE_DUAL_STACK @@ -1145,7 +1155,7 @@ run_main_loop (int fh, (-1 == fh) ? serve_port : 0, NULL, NULL, &handle_mhd_request, NULL, - MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) 4, + MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) num_threads, MHD_OPTION_LISTEN_BACKLOG_SIZE, (unsigned int) 1024, MHD_OPTION_LISTEN_SOCKET, fh, MHD_OPTION_EXTERNAL_LOGGER, &TALER_MHD_handle_logs, @@ -1267,6 +1277,11 @@ main (int argc, &connection_timeout), GNUNET_GETOPT_option_timetravel ('T', "timetravel"), + GNUNET_GETOPT_option_uint ('n', + "num-threads", + "NUM_THREADS", + "size of the thread pool", + &num_threads), #if HAVE_DEVELOPER GNUNET_GETOPT_option_filename ('f', "file-input", @@ -1292,6 +1307,15 @@ main (int argc, options, argc, argv)) return 1; + if (0 == num_threads) + { + cpu_set_t mask; + GNUNET_assert (0 == + sched_getaffinity (0, + sizeof (cpu_set_t), + &mask)); + num_threads = CPU_COUNT (&mask); + } go = TALER_MHD_GO_NONE; if (connection_close) go |= TALER_MHD_GO_FORCE_CONNECTION_CLOSE;