FairMQ: Move static and interactive control modes to plugin

NOT YET FINISHED
This commit is contained in:
Dennis Klein
2017-09-14 02:10:00 +02:00
committed by Mohammad Al-Turany
parent 2af3ae99eb
commit 10f67e4c72
9 changed files with 223 additions and 113 deletions

View File

@@ -8,4 +8,4 @@
// List of all builtin plugin headers (the ones which call REGISTER_FAIRMQ_PLUGIN macro)
#include <fairmq/plugins/ControlStatic.h>
#include <fairmq/plugins/Control.h>

137
fairmq/plugins/Control.cxx Normal file
View File

@@ -0,0 +1,137 @@
/********************************************************************************
* Copyright (C) 2017 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" *
********************************************************************************/
#include "Control.h"
#include <chrono>
#include <thread>
using namespace std;
namespace fair
{
namespace mq
{
namespace plugins
{
Control::Control(
const string name,
const Plugin::Version version,
const string maintainer,
const string homepage,
PluginServices* pluginServices)
: Plugin(name, version, maintainer, homepage, pluginServices)
{
try
{
TakeDeviceControl();
auto control = GetProperty<string>("control");
if(control == "static")
{
LOG(DEBUG) << "Running builtin controller: static";
thread t(&Control::StaticMode, this);
t.detach();
}
else if(control == "interactive")
{
LOG(DEBUG) << "Running builtin controller: interactive";
thread t(&Control::InteractiveMode, this);
t.detach();
}
else
{
LOG(ERROR) << "Unrecognized control mode '" << control << "' requested via command line. "
<< "Ignoring and falling back to interactive control mode.";
thread t(&Control::InteractiveMode, this);
t.detach();
}
}
catch(PluginServices::DeviceControlError& e)
{
LOG(DEBUG) << e.what();
}
}
auto ControlPluginProgramOptions() -> Plugin::ProgOptions
{
auto plugin_options = boost::program_options::options_description{"Control (builtin) Plugin"};
plugin_options.add_options()
("ctrlmode", boost::program_options::value<string>(), "Control mode, 'static' or 'interactive'");
// should rename to --control and remove control from device options ?
return plugin_options;
}
auto Control::InteractiveMode() -> void
{
LOG(ERROR) << "NOT YET IMPLEMENTED";
ReleaseDeviceControl();
}
auto Control::WaitForNextState() -> DeviceState
{
unique_lock<mutex> lock{fEventsMutex};
while(fEvents.empty())
{
fNewEvent.wait(lock);
}
// lock.lock();
auto result = fEvents.front();
fEvents.pop();
return result;
}
auto Control::StaticMode() -> void
{
clock_t cStart = clock();
auto tStart = chrono::high_resolution_clock::now();
SubscribeToDeviceStateChange(
[&](DeviceState newState){
{
lock_guard<mutex> lock{fEventsMutex};
fEvents.push(newState);
}
fNewEvent.notify_one();
}
);
ChangeDeviceState(DeviceStateTransition::InitDevice);
while(WaitForNextState() != DeviceState::DeviceReady) {};
clock_t cEnd = std::clock();
auto tEnd = chrono::high_resolution_clock::now();
LOG(DEBUG) << "Init time (CPU) : " << fixed << setprecision(2) << 1000.0 * (cEnd - cStart) / CLOCKS_PER_SEC << " ms";
LOG(DEBUG) << "Init time (Wall): " << chrono::duration<double, milli>(tEnd - tStart).count() << " ms";
ChangeDeviceState(DeviceStateTransition::InitTask);
while(WaitForNextState() != DeviceState::Ready) {};
ChangeDeviceState(DeviceStateTransition::Run);
// WaitForNextState();
// ChangeDeviceState(DeviceStateTransition::ResetTask);
// WaitForNextState();
// WaitForNextState();
// ChangeDeviceState(DeviceStateTransition::ResetDevice);
// WaitForNextState();
// WaitForNextState();
// ChangeDeviceState(DeviceStateTransition::End);
while(WaitForNextState() != DeviceState::Exiting) {};
LOG(WARN) << "1";
UnsubscribeFromDeviceStateChange();
LOG(WARN) << "2";
ReleaseDeviceControl();
LOG(WARN) << "3";
}
} /* namespace plugins */
} /* namespace mq */
} /* namespace fair */

View File

@@ -6,11 +6,14 @@
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#ifndef FAIR_MQ_PLUGINS_CONTROLSTATIC
#define FAIR_MQ_PLUGINS_CONTROLSTATIC
#ifndef FAIR_MQ_PLUGINS_CONTROL
#define FAIR_MQ_PLUGINS_CONTROL
#include <fairmq/Plugin.h>
#include <condition_variable>
#include <mutex>
#include <string>
#include <queue>
namespace fair
{
@@ -19,28 +22,39 @@ namespace mq
namespace plugins
{
class ControlStatic : public Plugin
class Control : public Plugin
{
public:
ControlStatic(
Control(
const std::string name,
const Plugin::Version version,
const std::string maintainer,
const std::string homepage,
PluginServices* pluginServices
);
}; /* class ControlStatic */
auto ControlStaticPluginProgramOptions() -> Plugin::ProgOptions;
private:
auto InteractiveMode() -> void;
auto StaticMode() -> void;
auto WaitForNextState() -> DeviceState;
std::queue<DeviceState> fEvents;
std::mutex fEventsMutex;
std::condition_variable fNewEvent;
}; /* class Control */
auto ControlPluginProgramOptions() -> Plugin::ProgOptions;
REGISTER_FAIRMQ_PLUGIN(
ControlStatic, // Class name
control_static, // Plugin name (string, lower case chars only)
Control, // Class name
control, // Plugin name (string, lower case chars only)
(Plugin::Version{1,0,0}), // Version
"FairRootGroup <fairroot@gsi.de>", // Maintainer
"https://github.com/FairRootGroup/FairRoot", // Homepage
ControlStaticPluginProgramOptions // Free function which declares custom program options for the plugin
ControlPluginProgramOptions // Free function which declares custom program options for the plugin
// signature: () -> boost::optional<boost::program_options::options_description>
)
@@ -48,4 +62,4 @@ REGISTER_FAIRMQ_PLUGIN(
} /* namespace mq */
} /* namespace fair */
#endif /* FAIR_MQ_PLUGINS_CONTROLSTATIC */
#endif /* FAIR_MQ_PLUGINS_CONTROL */

View File

@@ -1,54 +0,0 @@
/********************************************************************************
* Copyright (C) 2017 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" *
********************************************************************************/
#include "ControlStatic.h"
namespace fair
{
namespace mq
{
namespace plugins
{
ControlStatic::ControlStatic(
const std::string name,
const Plugin::Version version,
const std::string maintainer,
const std::string homepage,
PluginServices* pluginServices)
: Plugin(name, version, maintainer, homepage, pluginServices)
{
SubscribeToDeviceStateChange(
[&](DeviceState newState){
LOG(WARN) << newState;
switch (newState)
{
case DeviceState::InitializingDevice:
LOG(WARN) << GetPropertyAsString("custom-example-option");
SetProperty("custom-example-option", std::string{"new value"});
break;
case DeviceState::Exiting:
LOG(WARN) << GetProperty<std::string>("custom-example-option");
UnsubscribeFromDeviceStateChange();
break;
}
}
);
}
auto ControlStaticPluginProgramOptions() -> Plugin::ProgOptions
{
auto plugin_options = boost::program_options::options_description{"Control Static Plugin"};
plugin_options.add_options()
("custom-example-option", boost::program_options::value<std::string>(), "Custom option.");
return plugin_options;
}
} /* namespace plugins */
} /* namespace mq */
} /* namespace fair */