From 8b88e673604fbaf521d664aa4ff47638286586c4 Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Thu, 24 May 2018 15:38:22 +0200 Subject: [PATCH] Refactor FairMQProgOptions --- fairmq/CMakeLists.txt | 2 - fairmq/FairMQChannel.cxx | 2 - fairmq/FairMQDevice.cxx | 10 +- fairmq/devices/FairMQSink.cxx | 69 ---- fairmq/options/FairMQParser.cxx | 24 +- fairmq/options/FairMQParser.h | 15 +- fairmq/options/FairMQProgOptions.cxx | 366 +++++++++++++----- fairmq/options/FairMQProgOptions.h | 184 +++++---- fairmq/options/FairMQSuboptParser.cxx | 2 +- fairmq/options/FairMQSuboptParser.h | 6 +- fairmq/options/FairProgOptions.cxx | 178 --------- fairmq/options/FairProgOptions.h | 173 +-------- fairmq/options/FairProgOptionsHelper.h | 200 ++++------ fairmq/options/ProgOptionTest/CMakeLists.txt | 98 ----- .../lib/FairMQParserExample.cxx | 74 ---- .../ProgOptionTest/lib/FairMQParserExample.h | 86 ---- .../macro/ConfigFileTest.cfg.in | 38 -- .../macro/start-bsampler-sink.sh.in | 18 - .../ProgOptionTest/macro/testJSON.json | 63 --- .../ProgOptionTest/macro/testMQOptions1.sh.in | 11 - .../ProgOptionTest/macro/testMQOptions2.sh.in | 59 --- .../ProgOptionTest/macro/testMQOptions3.sh.in | 10 - .../ProgOptionTest/macro/testMQOptions4.sh.in | 11 - .../ProgOptionTest/macro/testMQOptions5.sh.in | 77 ---- .../options/ProgOptionTest/macro/testXML.xml | 44 --- .../ProgOptionTest/run/runOptTestSampler.cxx | 83 ---- .../ProgOptionTest/run/runOptTestSink.cxx | 77 ---- .../ProgOptionTest/run/testMQoptions1.cxx | 141 ------- .../ProgOptionTest/run/testMQoptions2.cxx | 69 ---- fairmq/options/runConfigEx.cxx | 35 +- fairmq/options/startConfigExample.sh.in | 12 +- fairmq/shmem/FairMQTransportFactorySHM.cxx | 8 +- fairmq/shmem/Region.cxx | 2 +- 33 files changed, 489 insertions(+), 1758 deletions(-) delete mode 100644 fairmq/devices/FairMQSink.cxx delete mode 100644 fairmq/options/FairProgOptions.cxx delete mode 100644 fairmq/options/ProgOptionTest/CMakeLists.txt delete mode 100644 fairmq/options/ProgOptionTest/lib/FairMQParserExample.cxx delete mode 100644 fairmq/options/ProgOptionTest/lib/FairMQParserExample.h delete mode 100644 fairmq/options/ProgOptionTest/macro/ConfigFileTest.cfg.in delete mode 100644 fairmq/options/ProgOptionTest/macro/start-bsampler-sink.sh.in delete mode 100644 fairmq/options/ProgOptionTest/macro/testJSON.json delete mode 100644 fairmq/options/ProgOptionTest/macro/testMQOptions1.sh.in delete mode 100644 fairmq/options/ProgOptionTest/macro/testMQOptions2.sh.in delete mode 100644 fairmq/options/ProgOptionTest/macro/testMQOptions3.sh.in delete mode 100644 fairmq/options/ProgOptionTest/macro/testMQOptions4.sh.in delete mode 100644 fairmq/options/ProgOptionTest/macro/testMQOptions5.sh.in delete mode 100644 fairmq/options/ProgOptionTest/macro/testXML.xml delete mode 100644 fairmq/options/ProgOptionTest/run/runOptTestSampler.cxx delete mode 100644 fairmq/options/ProgOptionTest/run/runOptTestSink.cxx delete mode 100644 fairmq/options/ProgOptionTest/run/testMQoptions1.cxx delete mode 100644 fairmq/options/ProgOptionTest/run/testMQoptions2.cxx diff --git a/fairmq/CMakeLists.txt b/fairmq/CMakeLists.txt index f45b919f..51779992 100644 --- a/fairmq/CMakeLists.txt +++ b/fairmq/CMakeLists.txt @@ -129,12 +129,10 @@ set(FAIRMQ_SOURCE_FILES devices/FairMQMerger.cxx devices/FairMQMultiplier.cxx devices/FairMQProxy.cxx - # devices/FairMQSink.cxx devices/FairMQSplitter.cxx options/FairMQParser.cxx options/FairMQProgOptions.cxx options/FairMQSuboptParser.cxx - options/FairProgOptions.cxx Plugin.cxx PluginManager.cxx PluginServices.cxx diff --git a/fairmq/FairMQChannel.cxx b/fairmq/FairMQChannel.cxx index f5b955df..6bdc0b9f 100644 --- a/fairmq/FairMQChannel.cxx +++ b/fairmq/FairMQChannel.cxx @@ -327,9 +327,7 @@ void FairMQChannel::UpdateTransport(const string& transport) { unique_lock lock(fChannelMutex); fIsValid = false; - LOG(WARN) << fName << ": " << transport; fTransportType = fair::mq::TransportTypes.at(transport); - LOG(WARN) << fName << ": " << fair::mq::TransportNames.at(fTransportType); fModified = true; } catch (exception& e) diff --git a/fairmq/FairMQDevice.cxx b/fairmq/FairMQDevice.cxx index b9482bd9..eeabca7b 100644 --- a/fairmq/FairMQDevice.cxx +++ b/fairmq/FairMQDevice.cxx @@ -837,17 +837,17 @@ void FairMQDevice::SetConfig(FairMQProgOptions& config) { fExternalConfig = true; fConfig = &config; - for (auto& c : config.GetFairMQMap()) + for (auto& c : fConfig->GetFairMQMap()) { if (!fChannels.insert(c).second) { LOG(warn) << "did not insert channel '" << c.first << "', it is already in the device."; } } - fId = config.GetValue("id"); - fNetworkInterface = config.GetValue("network-interface"); - fNumIoThreads = config.GetValue("io-threads"); - fInitializationTimeoutInS = config.GetValue("initialization-timeout"); + fId = fConfig->GetValue("id"); + fNetworkInterface = fConfig->GetValue("network-interface"); + fNumIoThreads = fConfig->GetValue("io-threads"); + fInitializationTimeoutInS = fConfig->GetValue("initialization-timeout"); fRate = fConfig->GetValue("rate"); try { fDefaultTransportType = fair::mq::TransportTypes.at(fConfig->GetValue("transport")); diff --git a/fairmq/devices/FairMQSink.cxx b/fairmq/devices/FairMQSink.cxx deleted file mode 100644 index b792d6e4..00000000 --- a/fairmq/devices/FairMQSink.cxx +++ /dev/null @@ -1,69 +0,0 @@ -/******************************************************************************** - * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * - * * - * This software is distributed under the terms of the * - * GNU Lesser General Public Licence (LGPL) version 3, * - * copied verbatim in the file "LICENSE" * - ********************************************************************************/ -/** - * FairMQSink.cxx - * - * @since 2013-01-09 - * @author D. Klein, A. Rybalchenko - */ - -#include "FairMQSink.h" - -#include - -#include "../FairMQLogger.h" -#include "../options/FairMQProgOptions.h" - -using namespace std; - -FairMQSink::FairMQSink() - : fNumMsgs(0) - , fInChannelName() -{ -} - -void FairMQSink::InitTask() -{ - fNumMsgs = fConfig->GetValue("num-msgs"); - fInChannelName = fConfig->GetValue("in-channel"); -} - -void FairMQSink::Run() -{ - uint64_t numReceivedMsgs = 0; - // store the channel reference to avoid traversing the map on every loop iteration - FairMQChannel& dataInChannel = fChannels.at(fInChannelName).at(0); - - LOG(info) << "Starting the benchmark and expecting to receive " << fNumMsgs << " messages."; - auto tStart = chrono::high_resolution_clock::now(); - - while (CheckCurrentState(RUNNING)) - { - FairMQMessagePtr msg(dataInChannel.Transport()->CreateMessage()); - - if (dataInChannel.Receive(msg) >= 0) - { - if (fNumMsgs > 0) - { - if (numReceivedMsgs >= fNumMsgs) - { - break; - } - } - numReceivedMsgs++; - } - } - - auto tEnd = chrono::high_resolution_clock::now(); - - LOG(info) << "Leaving RUNNING state. Received " << numReceivedMsgs << " messages in " << chrono::duration(tEnd - tStart).count() << "ms."; -} - -FairMQSink::~FairMQSink() -{ -} diff --git a/fairmq/options/FairMQParser.cxx b/fairmq/options/FairMQParser.cxx index 44d8a867..4c6e932c 100644 --- a/fairmq/options/FairMQParser.cxx +++ b/fairmq/options/FairMQParser.cxx @@ -27,11 +27,16 @@ namespace parser { // TODO : add key-value map parameter for replacing/updating values from keys -// function that convert property tree (given the json structure) to FairMQMap -FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const string& id, const string& rootNode) +// function that convert property tree (given the json structure) to FairMQChannelMap +FairMQChannelMap ptreeToMQMap(const boost::property_tree::ptree& pt, const string& id, const string& rootNode) { + if (id == "") + { + throw ParserError("no device ID provided. Provide with `--id` cmd option"); + } + // Create fair mq map - FairMQMap channelMap; + FairMQChannelMap channelMap; // boost::property_tree::json_parser::write_json(std::cout, pt); // Helper::PrintDeviceList(pt.get_child(rootNode)); // Extract value from boost::property_tree @@ -46,20 +51,13 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const string& id, return channelMap; } -FairMQMap JSON::UserParser(const string& filename, const string& deviceId, const string& rootNode) +FairMQChannelMap JSON::UserParser(const string& filename, const string& deviceId, const string& rootNode) { boost::property_tree::ptree pt; boost::property_tree::read_json(filename, pt); return ptreeToMQMap(pt, deviceId, rootNode); } -FairMQMap JSON::UserParser(stringstream& input, const string& deviceId, const string& rootNode) -{ - boost::property_tree::ptree pt; - boost::property_tree::read_json(input, pt); - return ptreeToMQMap(pt, deviceId, rootNode); -} - namespace Helper { @@ -90,7 +88,7 @@ void PrintDeviceList(const boost::property_tree::ptree& tree) } } -void DeviceParser(const boost::property_tree::ptree& tree, FairMQMap& channelMap, const string& deviceId) +void DeviceParser(const boost::property_tree::ptree& tree, FairMQChannelMap& channelMap, const string& deviceId) { string deviceIdKey; @@ -129,7 +127,7 @@ void DeviceParser(const boost::property_tree::ptree& tree, FairMQMap& channelMap } } -void ChannelParser(const boost::property_tree::ptree& tree, FairMQMap& channelMap) +void ChannelParser(const boost::property_tree::ptree& tree, FairMQChannelMap& channelMap) { string channelKey; diff --git a/fairmq/options/FairMQParser.h b/fairmq/options/FairMQParser.h index d0eeb158..ce1c8c85 100644 --- a/fairmq/options/FairMQParser.h +++ b/fairmq/options/FairMQParser.h @@ -10,8 +10,8 @@ #include #include -#include #include +#include #include @@ -24,22 +24,23 @@ namespace mq namespace parser { -using FairMQMap = std::unordered_map>; +using FairMQChannelMap = std::unordered_map>; -FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string& deviceId, const std::string& rootNode); +struct ParserError : std::runtime_error { using std::runtime_error::runtime_error; }; + +FairMQChannelMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string& deviceId, const std::string& rootNode); struct JSON { - FairMQMap UserParser(const std::string& filename, const std::string& deviceId, const std::string& rootNode = "fairMQOptions"); - FairMQMap UserParser(std::stringstream& input, const std::string& deviceId, const std::string& rootNode = "fairMQOptions"); + FairMQChannelMap UserParser(const std::string& filename, const std::string& deviceId, const std::string& rootNode = "fairMQOptions"); }; namespace Helper { void PrintDeviceList(const boost::property_tree::ptree& tree); -void DeviceParser(const boost::property_tree::ptree& tree, FairMQMap& channelMap, const std::string& deviceId); -void ChannelParser(const boost::property_tree::ptree& tree, FairMQMap& channelMap); +void DeviceParser(const boost::property_tree::ptree& tree, FairMQChannelMap& channelMap, const std::string& deviceId); +void ChannelParser(const boost::property_tree::ptree& tree, FairMQChannelMap& channelMap); void SocketParser(const boost::property_tree::ptree& tree, std::vector& channelList, const std::string& channelName, const FairMQChannel& commonChannel); } // Helper namespace diff --git a/fairmq/options/FairMQProgOptions.cxx b/fairmq/options/FairMQProgOptions.cxx index f68bfb6b..3715ceb8 100644 --- a/fairmq/options/FairMQProgOptions.cxx +++ b/fairmq/options/FairMQProgOptions.cxx @@ -12,25 +12,70 @@ * Created on March 11, 2015, 10:20 PM */ +#include "FairMQLogger.h" #include "FairMQProgOptions.h" +#include "FairProgOptionsHelper.h" + #include "FairMQParser.h" #include "FairMQSuboptParser.h" -#include "FairMQLogger.h" + +#include // join/split #include +#include #include +#include using namespace std; +using namespace fair::mq; + +namespace po = boost::program_options; FairMQProgOptions::FairMQProgOptions() - : FairProgOptions() - , fMQCmdOptions("FairMQ device options") - , fMQParserOptions("FairMQ config parser options") - , fFairMQMap() + : fVarMap() + , fFairMQChannelMap() + , fAllOptions("FairMQ Command Line Options") + , fGeneralOptions("General options") + , fMQOptions("FairMQ device options") + , fParserOptions("FairMQ channel config parser options") + , fConfigMutex() , fChannelInfo() - , fMQKeyMap() + , fChannelKeyMap() + , fUnregisteredOptions() + , fEvents() { - InitOptionDescription(); + fGeneralOptions.add_options() + ("help,h", "Print help") + ("version,v", "Print version") + ("severity", po::value()->default_value("debug"), "Log severity level: trace, debug, info, state, warn, error, fatal, nolog") + ("verbosity", po::value()->default_value("medium"), "Log verbosity level: veryhigh, high, medium, low") + ("color", po::value()->default_value(true), "Log color (true/false)") + ("log-to-file", po::value()->default_value(""), "Log output to a file.") + ("print-options", po::value()->implicit_value(true), "Print options in machine-readable format (