Commit 339a55fe8429e60e15562d885116847f28574e4e

Authored by Wiebe Cazemier
1 parent a6455a98

Make amount of threads configurable

configfileparser.cpp
@@ -108,6 +108,7 @@ ConfigFileParser::ConfigFileParser(const std::string &path) : @@ -108,6 +108,7 @@ ConfigFileParser::ConfigFileParser(const std::string &path) :
108 validKeys.insert("allow_anonymous"); 108 validKeys.insert("allow_anonymous");
109 validKeys.insert("rlimit_nofile"); 109 validKeys.insert("rlimit_nofile");
110 validKeys.insert("expire_sessions_after_seconds"); 110 validKeys.insert("expire_sessions_after_seconds");
  111 + validKeys.insert("thread_count");
111 validKeys.insert("storage_dir"); 112 validKeys.insert("storage_dir");
112 113
113 validListenKeys.insert("port"); 114 validListenKeys.insert("port");
@@ -428,6 +429,16 @@ void ConfigFileParser::loadFile(bool test) @@ -428,6 +429,16 @@ void ConfigFileParser::loadFile(bool test)
428 checkWritableDir<ConfigFileException>(newPath); 429 checkWritableDir<ConfigFileException>(newPath);
429 tmpSettings->storageDir = newPath; 430 tmpSettings->storageDir = newPath;
430 } 431 }
  432 +
  433 + if (key == "thread_count")
  434 + {
  435 + int newVal = std::stoi(value);
  436 + if (newVal < 0)
  437 + {
  438 + throw ConfigFileException(formatString("thread_count value '%d' is invalid. Valid values are 0 or higher. 0 means auto.", newVal));
  439 + }
  440 + tmpSettings->threadCount = newVal;
  441 + }
431 } 442 }
432 } 443 }
433 catch (std::invalid_argument &ex) // catch for the stoi() 444 catch (std::invalid_argument &ex) // catch for the stoi()
mainapp.cpp
@@ -36,19 +36,25 @@ MainApp *MainApp::instance = nullptr; @@ -36,19 +36,25 @@ MainApp *MainApp::instance = nullptr;
36 MainApp::MainApp(const std::string &configFilePath) : 36 MainApp::MainApp(const std::string &configFilePath) :
37 subscriptionStore(new SubscriptionStore()) 37 subscriptionStore(new SubscriptionStore())
38 { 38 {
39 - this->num_threads = get_nprocs();  
40 -  
41 - if (num_threads <= 0)  
42 - throw std::runtime_error("Invalid number of CPUs: " + std::to_string(num_threads));  
43 -  
44 epollFdAccept = check<std::runtime_error>(epoll_create(999)); 39 epollFdAccept = check<std::runtime_error>(epoll_create(999));
45 taskEventFd = eventfd(0, EFD_NONBLOCK); 40 taskEventFd = eventfd(0, EFD_NONBLOCK);
46 41
47 confFileParser.reset(new ConfigFileParser(configFilePath)); 42 confFileParser.reset(new ConfigFileParser(configFilePath));
48 loadConfig(); 43 loadConfig();
49 44
50 - // TODO: override in conf possibility.  
51 - logger->logf(LOG_NOTICE, "%d CPUs are detected, making as many threads.", num_threads); 45 + this->num_threads = get_nprocs();
  46 + if (settings->threadCount > 0)
  47 + {
  48 + this->num_threads = settings->threadCount;
  49 + logger->logf(LOG_NOTICE, "%d threads specified by 'thread_count'.", num_threads);
  50 + }
  51 + else
  52 + {
  53 + logger->logf(LOG_NOTICE, "%d CPUs are detected, making as many threads. Use 'thread_count' setting to override.", num_threads);
  54 + }
  55 +
  56 + if (num_threads <= 0)
  57 + throw std::runtime_error("Invalid number of CPUs: " + std::to_string(num_threads));
52 58
53 if (settings->expireSessionsAfterSeconds > 0) 59 if (settings->expireSessionsAfterSeconds > 0)
54 { 60 {
settings.h
@@ -55,6 +55,7 @@ public: @@ -55,6 +55,7 @@ public:
55 uint64_t expireSessionsAfterSeconds = 1209600; 55 uint64_t expireSessionsAfterSeconds = 1209600;
56 int authPluginTimerPeriod = 60; 56 int authPluginTimerPeriod = 60;
57 std::string storageDir; 57 std::string storageDir;
  58 + int threadCount = 0;
58 std::list<std::shared_ptr<Listener>> listeners; // Default one is created later, when none are defined. 59 std::list<std::shared_ptr<Listener>> listeners; // Default one is created later, when none are defined.
59 60
60 AuthOptCompatWrap &getAuthOptsCompat(); 61 AuthOptCompatWrap &getAuthOptsCompat();