make thread pool size configurable via command line argument, default to #CPUs available

This commit is contained in:
Florian Dold 2020-08-18 19:37:12 +05:30
parent 5eac9b2b31
commit 1cd3f3281b
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B

View File

@ -24,6 +24,7 @@
#include <gnunet/gnunet_util_lib.h> #include <gnunet/gnunet_util_lib.h>
#include <jansson.h> #include <jansson.h>
#include <microhttpd.h> #include <microhttpd.h>
#include <sched.h>
#include <pthread.h> #include <pthread.h>
#include <sys/resource.h> #include <sys/resource.h>
#include "taler_mhd_lib.h" #include "taler_mhd_lib.h"
@ -116,6 +117,13 @@ struct TALER_EXCHANGEDB_Plugin *TEH_plugin;
*/ */
static unsigned int connection_timeout = 30; 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. * The HTTP Daemon.
*/ */
@ -1137,6 +1145,8 @@ run_main_loop (int fh,
{ {
int ret; int ret;
GNUNET_assert (0 < num_threads);
mhd mhd
= MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_PIPE_FOR_SHUTDOWN = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_PIPE_FOR_SHUTDOWN
| MHD_USE_DEBUG | MHD_USE_DUAL_STACK | MHD_USE_DEBUG | MHD_USE_DUAL_STACK
@ -1145,7 +1155,7 @@ run_main_loop (int fh,
(-1 == fh) ? serve_port : 0, (-1 == fh) ? serve_port : 0,
NULL, NULL, NULL, NULL,
&handle_mhd_request, 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_BACKLOG_SIZE, (unsigned int) 1024,
MHD_OPTION_LISTEN_SOCKET, fh, MHD_OPTION_LISTEN_SOCKET, fh,
MHD_OPTION_EXTERNAL_LOGGER, &TALER_MHD_handle_logs, MHD_OPTION_EXTERNAL_LOGGER, &TALER_MHD_handle_logs,
@ -1267,6 +1277,11 @@ main (int argc,
&connection_timeout), &connection_timeout),
GNUNET_GETOPT_option_timetravel ('T', GNUNET_GETOPT_option_timetravel ('T',
"timetravel"), "timetravel"),
GNUNET_GETOPT_option_uint ('n',
"num-threads",
"NUM_THREADS",
"size of the thread pool",
&num_threads),
#if HAVE_DEVELOPER #if HAVE_DEVELOPER
GNUNET_GETOPT_option_filename ('f', GNUNET_GETOPT_option_filename ('f',
"file-input", "file-input",
@ -1292,6 +1307,15 @@ main (int argc,
options, options,
argc, argv)) argc, argv))
return 1; 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; go = TALER_MHD_GO_NONE;
if (connection_close) if (connection_close)
go |= TALER_MHD_GO_FORCE_CONNECTION_CLOSE; go |= TALER_MHD_GO_FORCE_CONNECTION_CLOSE;