From 32d51a482dbca5504d95ef9afe0dace01980fd95 Mon Sep 17 00:00:00 2001 From: Matt Millett Date: Thu, 5 Sep 2024 05:27:24 -0400 Subject: [PATCH] Configure the default scheduler with a maximum number of threads equal to the number of cores on the machine --- groups/ntc/ntcf/ntcf_system.cpp | 2 +- groups/ntc/ntci/ntci_log.cpp | 2 +- groups/ntc/ntcp/ntcp_interface.cpp | 12 ++++++------ groups/ntc/ntcr/ntcr_interface.cpp | 12 ++++++------ groups/nts/ntscfg/ntscfg_platform.cpp | 27 +++++++++++++++++++++++++++ groups/nts/ntscfg/ntscfg_platform.h | 3 +++ 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/groups/ntc/ntcf/ntcf_system.cpp b/groups/ntc/ntcf/ntcf_system.cpp index 07e1e996..b4545af5 100644 --- a/groups/ntc/ntcf/ntcf_system.cpp +++ b/groups/ntc/ntcf/ntcf_system.cpp @@ -222,7 +222,7 @@ void createDefaultInterface(bsl::shared_ptr* result, ntca::InterfaceConfig interfaceConfig; interfaceConfig.setThreadName("default"); interfaceConfig.setMinThreads(1); - interfaceConfig.setMaxThreads(64); + interfaceConfig.setMaxThreads(ntscfg::Platform::numCores()); bsl::shared_ptr interface = ntcf::System::createInterface(interfaceConfig, allocator); diff --git a/groups/ntc/ntci/ntci_log.cpp b/groups/ntc/ntci/ntci_log.cpp index 06caa9c4..b8dcaef6 100644 --- a/groups/ntc/ntci/ntci_log.cpp +++ b/groups/ntc/ntci/ntci_log.cpp @@ -3351,7 +3351,7 @@ bsl::size_t LogUtil::formatContext(char* destination, if (!empty) { os << ' '; } - os << "transport '" << logContext.d_owner << "'"; + os << "scheduler '" << logContext.d_owner << "'"; empty = false; } diff --git a/groups/ntc/ntcp/ntcp_interface.cpp b/groups/ntc/ntcp/ntcp_interface.cpp index c18b4beb..31ec27e3 100644 --- a/groups/ntc/ntcp/ntcp_interface.cpp +++ b/groups/ntc/ntcp/ntcp_interface.cpp @@ -40,7 +40,7 @@ BSLS_IDENT_RCSID(ntcp_interface_cpp, "$Id$ $CSID$") #define NTCP_INTERFACE_LOG_STARTING(config, numThreads) \ NTCI_LOG_DEBUG( \ - "Interface '%s' is starting %d/%d thread(s) " \ + "Scheduler '%s' is starting %d/%d thread(s) " \ "with %s load balancing", \ config.metricName().c_str(), \ (int)(numThreads), \ @@ -48,23 +48,23 @@ BSLS_IDENT_RCSID(ntcp_interface_cpp, "$Id$ $CSID$") (config.dynamicLoadBalancing().value() ? "dynamic" : "static")) #define NTCP_INTERFACE_LOG_STARTED(config) \ - NTCI_LOG_DEBUG("Interface '%s' has started", config.metricName().c_str()) + NTCI_LOG_DEBUG("Scheduler '%s' has started", config.metricName().c_str()) #define NTCP_INTERFACE_LOG_STOPPING(config) \ - NTCI_LOG_DEBUG("Interface '%s' is stopping", config.metricName().c_str()) + NTCI_LOG_DEBUG("Scheduler '%s' is stopping", config.metricName().c_str()) #define NTCP_INTERFACE_LOG_STOPPED(config) \ - NTCI_LOG_DEBUG("Interface '%s' has stopped", config.metricName().c_str()) + NTCI_LOG_DEBUG("Scheduler '%s' has stopped", config.metricName().c_str()) #define NTCP_INTERFACE_LOG_LOAD_FACTOR_EXCEEDED(config, load) \ - NTCI_LOG_DEBUG("Interface '%s' least used thread has a load of %d, " \ + NTCI_LOG_DEBUG("Scheduler '%s' least used thread has a load of %d, " \ "greater than the maximum desired load of %d", \ config.metricName().c_str(), \ (int)(load), \ (int)(config.threadLoadFactor())) #define NTCP_INTERFACE_LOG_EXPANDING(config, numThreads) \ - NTCI_LOG_DEBUG("Interface '%s' is expanding to %d/%d threads", \ + NTCI_LOG_DEBUG("Scheduler '%s' is expanding to %d/%d threads", \ config.metricName().c_str(), \ (int)(numThreads), \ (int)(config.maxThreads())) diff --git a/groups/ntc/ntcr/ntcr_interface.cpp b/groups/ntc/ntcr/ntcr_interface.cpp index b9274bae..6dc72c49 100644 --- a/groups/ntc/ntcr/ntcr_interface.cpp +++ b/groups/ntc/ntcr/ntcr_interface.cpp @@ -41,7 +41,7 @@ BSLS_IDENT_RCSID(ntcr_interface_cpp, "$Id$ $CSID$") #define NTCR_INTERFACE_LOG_STARTING(config, numThreads) \ NTCI_LOG_DEBUG( \ - "Interface '%s' is starting %d/%d thread(s) " \ + "Scheduler '%s' is starting %d/%d thread(s) " \ "with %s load balancing", \ config.metricName().c_str(), \ (int)(numThreads), \ @@ -49,23 +49,23 @@ BSLS_IDENT_RCSID(ntcr_interface_cpp, "$Id$ $CSID$") (config.dynamicLoadBalancing().value() ? "dynamic" : "static")) #define NTCR_INTERFACE_LOG_STARTED(config) \ - NTCI_LOG_DEBUG("Interface '%s' has started", config.metricName().c_str()) + NTCI_LOG_DEBUG("Scheduler '%s' has started", config.metricName().c_str()) #define NTCR_INTERFACE_LOG_STOPPING(config) \ - NTCI_LOG_DEBUG("Interface '%s' is stopping", config.metricName().c_str()) + NTCI_LOG_DEBUG("Scheduler '%s' is stopping", config.metricName().c_str()) #define NTCR_INTERFACE_LOG_STOPPED(config) \ - NTCI_LOG_DEBUG("Interface '%s' has stopped", config.metricName().c_str()) + NTCI_LOG_DEBUG("Scheduler '%s' has stopped", config.metricName().c_str()) #define NTCR_INTERFACE_LOG_LOAD_FACTOR_EXCEEDED(config, load) \ - NTCI_LOG_DEBUG("Interface '%s' least used thread has a load of %d, " \ + NTCI_LOG_DEBUG("Scheduler '%s' least used thread has a load of %d, " \ "greater than the maximum desired load of %d", \ config.metricName().c_str(), \ (int)(load), \ (int)(config.threadLoadFactor())) #define NTCR_INTERFACE_LOG_EXPANDING(config, numThreads) \ - NTCI_LOG_DEBUG("Interface '%s' is expanding to %d/%d threads", \ + NTCI_LOG_DEBUG("Scheduler '%s' is expanding to %d/%d threads", \ config.metricName().c_str(), \ (int)(numThreads), \ (int)(config.maxThreads())) diff --git a/groups/nts/ntscfg/ntscfg_platform.cpp b/groups/nts/ntscfg/ntscfg_platform.cpp index 61e30b8c..9e3b9205 100644 --- a/groups/nts/ntscfg/ntscfg_platform.cpp +++ b/groups/nts/ntscfg/ntscfg_platform.cpp @@ -30,6 +30,7 @@ BSLS_IDENT_RCSID(ntscfg_platform_cpp, "$Id$ $CSID$") #include #include #include +#include #if defined(BSLS_PLATFORM_OS_LINUX) #include @@ -40,6 +41,7 @@ BSLS_IDENT_RCSID(ntscfg_platform_cpp, "$Id$ $CSID$") #if defined(BSLS_PLATFORM_OS_WINDOWS) #include #include +#include #pragma comment(lib, "ws2_32.lib") #endif @@ -163,6 +165,31 @@ int Platform::exit() #endif } + +bsl::size_t Platform::numCores() +{ +#if defined(BSLS_PLATFORM_OS_UNIX) + + errno = 0; + const int cpuCount = sysconf(_SC_NPROCESSORS_ONLN); + if (cpuCount < 0) { + return 1; + } + + return static_cast(cpuCount); + +#elif defined(BSLS_PLATFORM_OS_WINDOWS) + + SYSTEM_INFO systemInfo; + GetSystemInfo(&systemInfo); + + return static_cast(systemInfo.dwNumberOfProcessors); + +#else +#error Not implemented +#endif +} + bool Platform::supportsNotifications() { #if defined(BSLS_PLATFORM_OS_LINUX) diff --git a/groups/nts/ntscfg/ntscfg_platform.h b/groups/nts/ntscfg/ntscfg_platform.h index 5a64d394..e13fc641 100644 --- a/groups/nts/ntscfg/ntscfg_platform.h +++ b/groups/nts/ntscfg/ntscfg_platform.h @@ -414,6 +414,9 @@ struct Platform { /// error otherwise. static int exit(); + /// Return the number of CPU cores on the current machine. + static bsl::size_t numCores(); + /// Return true if the version of the operating system running the current /// process supports asynchronous socket notifications via some mechanism /// such as the Linux error queue.