diff --git a/configfileparser.cpp b/configfileparser.cpp index a4040c1..c32a1ab 100644 --- a/configfileparser.cpp +++ b/configfileparser.cpp @@ -108,6 +108,7 @@ ConfigFileParser::ConfigFileParser(const std::string &path) : validKeys.insert("allow_anonymous"); validKeys.insert("rlimit_nofile"); validKeys.insert("expire_sessions_after_seconds"); + validKeys.insert("thread_count"); validKeys.insert("storage_dir"); validListenKeys.insert("port"); @@ -428,6 +429,16 @@ void ConfigFileParser::loadFile(bool test) checkWritableDir(newPath); tmpSettings->storageDir = newPath; } + + if (key == "thread_count") + { + int newVal = std::stoi(value); + if (newVal < 0) + { + throw ConfigFileException(formatString("thread_count value '%d' is invalid. Valid values are 0 or higher. 0 means auto.", newVal)); + } + tmpSettings->threadCount = newVal; + } } } catch (std::invalid_argument &ex) // catch for the stoi() diff --git a/mainapp.cpp b/mainapp.cpp index 6fafdcc..7806fa9 100644 --- a/mainapp.cpp +++ b/mainapp.cpp @@ -36,19 +36,25 @@ MainApp *MainApp::instance = nullptr; MainApp::MainApp(const std::string &configFilePath) : subscriptionStore(new SubscriptionStore()) { - this->num_threads = get_nprocs(); - - if (num_threads <= 0) - throw std::runtime_error("Invalid number of CPUs: " + std::to_string(num_threads)); - epollFdAccept = check(epoll_create(999)); taskEventFd = eventfd(0, EFD_NONBLOCK); confFileParser.reset(new ConfigFileParser(configFilePath)); loadConfig(); - // TODO: override in conf possibility. - logger->logf(LOG_NOTICE, "%d CPUs are detected, making as many threads.", num_threads); + this->num_threads = get_nprocs(); + if (settings->threadCount > 0) + { + this->num_threads = settings->threadCount; + logger->logf(LOG_NOTICE, "%d threads specified by 'thread_count'.", num_threads); + } + else + { + logger->logf(LOG_NOTICE, "%d CPUs are detected, making as many threads. Use 'thread_count' setting to override.", num_threads); + } + + if (num_threads <= 0) + throw std::runtime_error("Invalid number of CPUs: " + std::to_string(num_threads)); if (settings->expireSessionsAfterSeconds > 0) { diff --git a/settings.h b/settings.h index 06edf5f..1b323ab 100644 --- a/settings.h +++ b/settings.h @@ -55,6 +55,7 @@ public: uint64_t expireSessionsAfterSeconds = 1209600; int authPluginTimerPeriod = 60; std::string storageDir; + int threadCount = 0; std::list> listeners; // Default one is created later, when none are defined. AuthOptCompatWrap &getAuthOptsCompat();