From 3866462ff5779ee613ec027331ca92a83b1d4906 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Tue, 11 Aug 2020 01:33:11 +0100 Subject: [PATCH] Add worker_threads setting --- orotool.conf | 1 + src/app_config.cpp | 55 +++++++++++++++++++++++++++++++++++++++------- src/app_config.hpp | 2 ++ src/config.hpp.in | 1 + src/evloop.cpp | 3 +-- src/evloop.hpp | 4 +++- src/main.cpp | 2 +- 7 files changed, 56 insertions(+), 12 deletions(-) diff --git a/orotool.conf b/orotool.conf index 26e166f..1191c73 100644 --- a/orotool.conf +++ b/orotool.conf @@ -1,2 +1,3 @@ [main] db_path=originsro.db3 +worker_threads=3 diff --git a/src/app_config.cpp b/src/app_config.cpp index 4a3850b..0d38a94 100644 --- a/src/app_config.cpp +++ b/src/app_config.cpp @@ -19,6 +19,11 @@ #include "orotool_config.hpp" #include #include +#include +#include +#include +#include +#include namespace duck { @@ -28,6 +33,28 @@ namespace { input >> std::noskipws; return { std::istream_iterator(input), std::istream_iterator() }; } + + std::string_view value_ifp ( + const kamokan::IniFile& ini, + std::string_view section, + std::string_view key, + std::string_view def, + bool allow_empty + ) { + const auto& map = ini.parsed(); + auto it_section = map.find(section); + if (map.end() == it_section) + return def; + + auto it_setting = it_section->second.find(key); + if (it_section->second.end() == it_setting) + return def; + + if (not allow_empty and it_setting->second.empty()) + return def; + else + return it_setting->second; + } } //unnamed namespace AppConfig::AppConfig() : @@ -38,15 +65,27 @@ AppConfig::AppConfig() : AppConfig::~AppConfig() noexcept = default; std::string_view AppConfig::db_path() const { - const auto& map = m_ini.parsed(); - auto it_section = map.find("main"); - if (map.end() == it_section) - return g_def_sqlite_db_name; + return value_ifp(m_ini, "main", "db_path", g_def_sqlite_db_name, false); +} - auto it_setting = it_section->second.find("db_path"); - if (it_section->second.end() == it_setting) - return g_def_sqlite_db_name; - return it_setting->second; +std::size_t AppConfig::worker_threads() const { + std::string_view opt_name("worker_threads"); + + std::string_view val = value_ifp(m_ini, "main", opt_name, g_def_worker_threads, false); + if (val == "max") { + return std::max(3U, std::thread::hardware_concurrency()) - 1; + } + else { + try { + const std::size_t num = std::stoul(std::string(val.data(), val.size())); + const std::size_t hard_max = 4U * std::thread::hardware_concurrency(); + return std::max(2U, std::min(num, hard_max)); + } + catch (const std::logic_error& err) { + std::cerr << "Error reading setting " << opt_name << ": " << err.what() << '\n'; + return std::stoul(g_def_worker_threads); + } + } } } //namespace duck diff --git a/src/app_config.hpp b/src/app_config.hpp index f1a639b..6a235a0 100644 --- a/src/app_config.hpp +++ b/src/app_config.hpp @@ -19,6 +19,7 @@ #include "ini_file.hpp" #include +#include namespace duck { @@ -28,6 +29,7 @@ public: ~AppConfig() noexcept; std::string_view db_path() const; + std::size_t worker_threads() const; private: kamokan::IniFile m_ini; diff --git a/src/config.hpp.in b/src/config.hpp.in index c1b57ba..8a77df4 100644 --- a/src/config.hpp.in +++ b/src/config.hpp.in @@ -22,5 +22,6 @@ namespace duck { constexpr const char g_base_url[] = @BASE_URL@; constexpr const char g_config_file_path[] = @CONFIG_FILE_PATH@; constexpr const char g_def_sqlite_db_name[] = @DEF_SQLITE_DB_NAME@; +constexpr const char g_def_worker_threads[] = "2"; } //namespace duck diff --git a/src/evloop.cpp b/src/evloop.cpp index 0492614..7f6f16d 100644 --- a/src/evloop.cpp +++ b/src/evloop.cpp @@ -43,12 +43,11 @@ void join(roar11::ThreadPool& pool) { } //unnamed namespace -void test(oro::Api* api, oro::OriginsDB* db) { +void test(oro::Api* api, oro::OriginsDB* db, std::size_t thread_count) { typedef TimerOroApi<&oro::Api::market_list> TimerShops; typedef TimerOroApi<&oro::Api::items_list> TimerItems; typedef TimerOroApi<&oro::Api::items_icons> TimerIcons; - const std::size_t thread_count = 2U; //std::min(std::max(3U, std::thread::hardware_concurrency()) - 1, 4U); std::cout << "Running with " << thread_count << " worker threads\n"; roar11::ThreadPool pool(thread_count); eve::Eventia worker; diff --git a/src/evloop.hpp b/src/evloop.hpp index ddbe2d4..4b9d314 100644 --- a/src/evloop.hpp +++ b/src/evloop.hpp @@ -17,6 +17,8 @@ #pragma once +#include + namespace oro { class Api; class OriginsDB; @@ -24,6 +26,6 @@ namespace oro { namespace duck { -void test(oro::Api* api, oro::OriginsDB* db); +void test(oro::Api* api, oro::OriginsDB* db, std::size_t thread_count); } //namespace duck diff --git a/src/main.cpp b/src/main.cpp index b4db63c..6ced761 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,7 +44,7 @@ int main(int argc, char* argv[]) { duck::AppConfig app_conf; oro::OriginsDB db(app_conf.db_path()); - duck::test(&oro_api, &db); + duck::test(&oro_api, &db, app_conf.worker_threads()); /* {