From ee19383f4de346050045907a6a151226657da21c Mon Sep 17 00:00:00 2001 From: Michael Krasnyk Date: Tue, 29 Aug 2017 22:46:06 +0200 Subject: [PATCH] Link TBB task_scheduler lifetime with Engine scope --- CHANGELOG.md | 2 + include/engine/engine.hpp | 6 +- include/engine/engine_config.hpp | 2 + include/nodejs/node_osrm_support.hpp | 8 +++ src/engine/engine_config.cpp | 2 +- src/tools/routed.cpp | 96 +++++++++++++--------------- 6 files changed, 61 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e540ce48f..210667a95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ - New function to support relations: `process_relation`. Read more in profiles documentation. - Infrastructure: - Lua 5.1 support is removed due to lack of support in sol2 https://github.com/ThePhD/sol2/issues/302 + - Node.js Bindings: + - Exposes `use_threads_number=Number` parameter of `EngineConfig` to limit a number of threads in a TBB internal pool # 5.12.0 - Guidance diff --git a/include/engine/engine.hpp b/include/engine/engine.hpp index d21baff5c..ec2fee70d 100644 --- a/include/engine/engine.hpp +++ b/include/engine/engine.hpp @@ -24,6 +24,8 @@ #include "util/fingerprint.hpp" #include "util/json_container.hpp" +#include + #include #include @@ -53,7 +55,8 @@ template class Engine final : public EngineInterface { public: explicit Engine(const EngineConfig &config) - : route_plugin(config.max_locations_viaroute, config.max_alternatives), // + : task_scheduler(config.use_threads_number), + route_plugin(config.max_locations_viaroute, config.max_alternatives), // table_plugin(config.max_locations_distance_table), // nearest_plugin(config.max_results_nearest), // trip_plugin(config.max_locations_trip), // @@ -125,6 +128,7 @@ template class Engine final : public EngineInterface } std::unique_ptr> facade_provider; mutable SearchEngineData heaps; + tbb::task_scheduler_init task_scheduler; const plugins::ViaRoutePlugin route_plugin; const plugins::TablePlugin table_plugin; diff --git a/include/engine/engine_config.hpp b/include/engine/engine_config.hpp index 2459b7ee5..a5795413d 100644 --- a/include/engine/engine_config.hpp +++ b/include/engine/engine_config.hpp @@ -90,6 +90,8 @@ struct EngineConfig final int max_alternatives = 3; // set an arbitrary upper bound; can be adjusted by user bool use_shared_memory = true; Algorithm algorithm = Algorithm::CH; + int use_threads_number = 1; + std::string verbosity; }; } } diff --git a/include/nodejs/node_osrm_support.hpp b/include/nodejs/node_osrm_support.hpp index 26b83472c..e1b002570 100644 --- a/include/nodejs/node_osrm_support.hpp +++ b/include/nodejs/node_osrm_support.hpp @@ -186,6 +186,7 @@ inline engine_config_ptr argumentsToEngineConfig(const Nan::FunctionCallbackInfo params->Get(Nan::New("max_locations_map_matching").ToLocalChecked()); auto max_results_nearest = params->Get(Nan::New("max_results_nearest").ToLocalChecked()); auto max_alternatives = params->Get(Nan::New("max_alternatives").ToLocalChecked()); + auto use_threads_number = params->Get(Nan::New("use_threads_number").ToLocalChecked()); if (!max_locations_trip->IsUndefined() && !max_locations_trip->IsNumber()) { @@ -217,6 +218,11 @@ inline engine_config_ptr argumentsToEngineConfig(const Nan::FunctionCallbackInfo Nan::ThrowError("max_alternatives must be an integral number"); return engine_config_ptr(); } + if (!use_threads_number->IsUndefined() && !use_threads_number->IsNumber()) + { + Nan::ThrowError("use_threads_number must be an integral number"); + return engine_config_ptr(); + } if (max_locations_trip->IsNumber()) engine_config->max_locations_trip = static_cast(max_locations_trip->NumberValue()); @@ -233,6 +239,8 @@ inline engine_config_ptr argumentsToEngineConfig(const Nan::FunctionCallbackInfo engine_config->max_results_nearest = static_cast(max_results_nearest->NumberValue()); if (max_alternatives->IsNumber()) engine_config->max_alternatives = static_cast(max_alternatives->NumberValue()); + if (use_threads_number->IsNumber()) + engine_config->use_threads_number = static_cast(use_threads_number->NumberValue()); return engine_config; } diff --git a/src/engine/engine_config.cpp b/src/engine/engine_config.cpp index d516582b9..fd6ac114a 100644 --- a/src/engine/engine_config.cpp +++ b/src/engine/engine_config.cpp @@ -20,7 +20,7 @@ bool EngineConfig::IsValid() const unlimited_or_more_than(max_locations_trip, 2) && unlimited_or_more_than(max_locations_viaroute, 2) && unlimited_or_more_than(max_results_nearest, 0) && - max_alternatives >= 0; + max_alternatives >= 0 && use_threads_number >= 1; return ((use_shared_memory && all_path_are_empty) || storage_config.IsValid()) && limits_valid; } diff --git a/src/tools/routed.cpp b/src/tools/routed.cpp index 96b7ead2d..c41033611 100644 --- a/src/tools/routed.cpp +++ b/src/tools/routed.cpp @@ -52,36 +52,37 @@ const static unsigned INIT_OK_START_ENGINE = 0; const static unsigned INIT_OK_DO_NOT_START_ENGINE = 1; const static unsigned INIT_FAILED = -1; -static EngineConfig::Algorithm stringToAlgorithm(std::string algorithm) +namespace osrm { - boost::to_lower(algorithm); +namespace engine +{ +std::istream &operator>>(std::istream &in, EngineConfig::Algorithm &algorithm) +{ + std::string token; + in >> token; + boost::to_lower(token); - if (algorithm == "ch") - return EngineConfig::Algorithm::CH; - if (algorithm == "corech") - return EngineConfig::Algorithm::CoreCH; - if (algorithm == "mld") - return EngineConfig::Algorithm::MLD; - throw util::RuntimeError(algorithm, ErrorCode::UnknownAlgorithm, SOURCE_REF); + if (token == "ch") + algorithm = EngineConfig::Algorithm::CH; + else if (token == "corech") + algorithm = EngineConfig::Algorithm::CoreCH; + else if (token == "mld") + algorithm = EngineConfig::Algorithm::MLD; + else + throw util::RuntimeError(token, ErrorCode::UnknownAlgorithm, SOURCE_REF); + return in; +} +} } // generate boost::program_options object for the routing part inline unsigned generateServerProgramOptions(const int argc, const char *argv[], - std::string verbosity, boost::filesystem::path &base_path, std::string &ip_address, int &ip_port, - int &requested_num_threads, - bool &use_shared_memory, - std::string &algorithm, bool &trial, - int &max_locations_trip, - int &max_locations_viaroute, - int &max_locations_distance_table, - int &max_locations_map_matching, - int &max_results_nearest, - int &max_alternatives) + EngineConfig &config) { using boost::program_options::value; using boost::filesystem::path; @@ -91,7 +92,7 @@ inline unsigned generateServerProgramOptions(const int argc, generic_options.add_options() // ("version,v", "Show version")("help,h", "Show this help message")( "verbosity,l", - boost::program_options::value(&verbosity)->default_value("INFO"), + boost::program_options::value(&config.verbosity)->default_value("INFO"), std::string("Log verbosity level: " + util::LogPolicy::GetLevels()).c_str())( "trial", value(&trial)->implicit_value(true), "Quit after initialization"); @@ -105,31 +106,32 @@ inline unsigned generateServerProgramOptions(const int argc, value(&ip_port)->default_value(5000), "TCP/IP port") // ("threads,t", - value(&requested_num_threads)->default_value(8), + value(&config.use_threads_number)->default_value(8), "Number of threads to use") // ("shared-memory,s", - value(&use_shared_memory)->implicit_value(true)->default_value(false), + value(&config.use_shared_memory)->implicit_value(true)->default_value(false), "Load data from shared memory") // ("algorithm,a", - value(&algorithm)->default_value("CH"), + value(&config.algorithm) + ->default_value(EngineConfig::Algorithm::CH, "CH"), "Algorithm to use for the data. Can be CH, CoreCH, MLD.") // ("max-viaroute-size", - value(&max_locations_viaroute)->default_value(500), + value(&config.max_locations_viaroute)->default_value(500), "Max. locations supported in viaroute query") // ("max-trip-size", - value(&max_locations_trip)->default_value(100), + value(&config.max_locations_trip)->default_value(100), "Max. locations supported in trip query") // ("max-table-size", - value(&max_locations_distance_table)->default_value(100), + value(&config.max_locations_distance_table)->default_value(100), "Max. locations supported in distance table query") // ("max-matching-size", - value(&max_locations_map_matching)->default_value(100), + value(&config.max_locations_map_matching)->default_value(100), "Max. locations supported in map matching query") // ("max-nearest-size", - value(&max_results_nearest)->default_value(100), + value(&config.max_results_nearest)->default_value(100), "Max. results supported in nearest query") // ("max-alternatives", - value(&max_alternatives)->default_value(3), + value(&config.max_alternatives)->default_value(3), "Max. number of alternatives supported in the MLD route query"); // hidden options, will be allowed on command line, but will not be shown to the user @@ -180,15 +182,15 @@ inline unsigned generateServerProgramOptions(const int argc, boost::program_options::notify(option_variables); - if (!use_shared_memory && option_variables.count("base")) + if (!config.use_shared_memory && option_variables.count("base")) { return INIT_OK_START_ENGINE; } - else if (use_shared_memory && !option_variables.count("base")) + else if (config.use_shared_memory && !option_variables.count("base")) { return INIT_OK_START_ENGINE; } - else if (use_shared_memory && option_variables.count("base")) + else if (config.use_shared_memory && option_variables.count("base")) { util::Log(logWARNING) << "Shared memory settings conflict with path settings."; } @@ -203,28 +205,13 @@ int main(int argc, const char *argv[]) try bool trial_run = false; std::string ip_address; - int ip_port, requested_thread_num; + int ip_port; EngineConfig config; - std::string verbosity; boost::filesystem::path base_path; - std::string algorithm; - const unsigned init_result = generateServerProgramOptions(argc, - argv, - verbosity, - base_path, - ip_address, - ip_port, - requested_thread_num, - config.use_shared_memory, - algorithm, - trial_run, - config.max_locations_trip, - config.max_locations_viaroute, - config.max_locations_distance_table, - config.max_locations_map_matching, - config.max_results_nearest, - config.max_alternatives); + + const unsigned init_result = + generateServerProgramOptions(argc, argv, base_path, ip_address, ip_port, trial_run, config); if (init_result == INIT_OK_DO_NOT_START_ENGINE) { return EXIT_SUCCESS; @@ -234,7 +221,7 @@ int main(int argc, const char *argv[]) try return EXIT_FAILURE; } - util::LogPolicy::GetInstance().SetLevel(verbosity); + util::LogPolicy::GetInstance().SetLevel(config.verbosity); if (!base_path.empty()) { @@ -253,7 +240,6 @@ int main(int argc, const char *argv[]) try } return EXIT_FAILURE; } - config.algorithm = stringToAlgorithm(algorithm); util::Log() << "starting up engines, " << OSRM_VERSION; @@ -262,6 +248,10 @@ int main(int argc, const char *argv[]) try util::Log() << "Loading from shared memory"; } + // Use the same number of threads for Server and TBB threads pools + // It doubles number of used threads + auto requested_thread_num = config.use_threads_number; + util::Log() << "Threads: " << requested_thread_num; util::Log() << "IP address: " << ip_address; util::Log() << "IP port: " << ip_port;