mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 09:31:45 +00:00
Refactor the transport interface
- give transport Initialize() method with access to device config. - avoid using global context in the transport. - simplify shutdown procedure (no need for extra thread).
This commit is contained in:
committed by
Mohammad Al-Turany
parent
d7eb692951
commit
5aaf27bf02
@@ -1,71 +0,0 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* FairMQContextZMQ.cxx
|
||||
*
|
||||
* @since 2012-12-05
|
||||
* @author D. Klein, A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include <zmq.h>
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
#include "FairMQContextZMQ.h"
|
||||
|
||||
FairMQContextZMQ::FairMQContextZMQ(int numIoThreads)
|
||||
: fContext()
|
||||
{
|
||||
fContext = zmq_ctx_new();
|
||||
if (fContext == NULL)
|
||||
{
|
||||
LOG(ERROR) << "failed creating context, reason: " << zmq_strerror(errno);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (zmq_ctx_set(fContext, ZMQ_IO_THREADS, numIoThreads) != 0)
|
||||
{
|
||||
LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
|
||||
// Set the maximum number of allowed sockets on the context.
|
||||
if (zmq_ctx_set(fContext, ZMQ_MAX_SOCKETS, 10000) != 0)
|
||||
{
|
||||
LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
}
|
||||
|
||||
FairMQContextZMQ::~FairMQContextZMQ()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
void* FairMQContextZMQ::GetContext()
|
||||
{
|
||||
return fContext;
|
||||
}
|
||||
|
||||
void FairMQContextZMQ::Close()
|
||||
{
|
||||
if (fContext == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (zmq_ctx_destroy(fContext) != 0)
|
||||
{
|
||||
if (errno == EINTR)
|
||||
{
|
||||
LOG(ERROR) << " failed closing context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
else
|
||||
{
|
||||
fContext = NULL;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,34 +0,0 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* FairMQContextZMQ.h
|
||||
*
|
||||
* @since 2012-12-05
|
||||
* @author D. Klein, A. Rybalchenko
|
||||
*/
|
||||
|
||||
#ifndef FAIRMQCONTEXTZMQ_H_
|
||||
#define FAIRMQCONTEXTZMQ_H_
|
||||
|
||||
class FairMQContextZMQ
|
||||
{
|
||||
public:
|
||||
/// Constructor
|
||||
FairMQContextZMQ(int numIoThreads);
|
||||
FairMQContextZMQ(const FairMQContextZMQ&) = delete;
|
||||
FairMQContextZMQ operator=(const FairMQContextZMQ&) = delete;
|
||||
|
||||
virtual ~FairMQContextZMQ();
|
||||
void* GetContext();
|
||||
void Close();
|
||||
|
||||
private:
|
||||
void* fContext;
|
||||
};
|
||||
|
||||
#endif /* FAIRMQCONTEXTZMQ_H_ */
|
@@ -22,11 +22,9 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Context to hold the ZeroMQ sockets
|
||||
unique_ptr<FairMQContextZMQ> FairMQSocketZMQ::fContext = unique_ptr<FairMQContextZMQ>(new FairMQContextZMQ(1));
|
||||
atomic<bool> FairMQSocketZMQ::fInterrupted(false);
|
||||
|
||||
FairMQSocketZMQ::FairMQSocketZMQ(const string& type, const string& name, const int numIoThreads, const string& id /*= ""*/)
|
||||
FairMQSocketZMQ::FairMQSocketZMQ(const string& type, const string& name, const string& id /*= ""*/, void* context)
|
||||
: FairMQSocket(ZMQ_SNDMORE, ZMQ_RCVMORE, ZMQ_DONTWAIT)
|
||||
, fSocket(NULL)
|
||||
, fId()
|
||||
@@ -37,12 +35,8 @@ FairMQSocketZMQ::FairMQSocketZMQ(const string& type, const string& name, const i
|
||||
{
|
||||
fId = id + "." + name + "." + type;
|
||||
|
||||
if (zmq_ctx_set(fContext->GetContext(), ZMQ_IO_THREADS, numIoThreads) != 0)
|
||||
{
|
||||
LOG(ERROR) << "Failed configuring context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
|
||||
fSocket = zmq_socket(fContext->GetContext(), GetConstant(type));
|
||||
assert(context);
|
||||
fSocket = zmq_socket(context, GetConstant(type));
|
||||
|
||||
if (fSocket == NULL)
|
||||
{
|
||||
@@ -346,14 +340,6 @@ void FairMQSocketZMQ::Close()
|
||||
fSocket = NULL;
|
||||
}
|
||||
|
||||
void FairMQSocketZMQ::Terminate()
|
||||
{
|
||||
if (zmq_ctx_destroy(fContext->GetContext()) != 0)
|
||||
{
|
||||
LOG(ERROR) << "Failed terminating context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQSocketZMQ::Interrupt()
|
||||
{
|
||||
fInterrupted = true;
|
||||
|
@@ -21,12 +21,11 @@
|
||||
|
||||
#include "FairMQSocket.h"
|
||||
#include "FairMQMessage.h"
|
||||
#include "FairMQContextZMQ.h"
|
||||
|
||||
class FairMQSocketZMQ : public FairMQSocket
|
||||
{
|
||||
public:
|
||||
FairMQSocketZMQ(const std::string& type, const std::string& name, const int numIoThreads, const std::string& id = "");
|
||||
FairMQSocketZMQ(const std::string& type, const std::string& name, const std::string& id = "", void* context = nullptr);
|
||||
FairMQSocketZMQ(const FairMQSocketZMQ&) = delete;
|
||||
FairMQSocketZMQ operator=(const FairMQSocketZMQ&) = delete;
|
||||
|
||||
@@ -44,7 +43,6 @@ class FairMQSocketZMQ : public FairMQSocket
|
||||
virtual void* GetSocket() const;
|
||||
virtual int GetSocket(int nothing) const;
|
||||
virtual void Close();
|
||||
virtual void Terminate();
|
||||
|
||||
virtual void Interrupt();
|
||||
virtual void Resume();
|
||||
@@ -74,7 +72,6 @@ class FairMQSocketZMQ : public FairMQSocket
|
||||
std::atomic<unsigned long> fMessagesTx;
|
||||
std::atomic<unsigned long> fMessagesRx;
|
||||
|
||||
static std::unique_ptr<FairMQContextZMQ> fContext;
|
||||
static std::atomic<bool> fInterrupted;
|
||||
};
|
||||
|
||||
|
@@ -15,16 +15,48 @@
|
||||
#include "zmq.h"
|
||||
|
||||
#include "FairMQTransportFactoryZMQ.h"
|
||||
#include "../options/FairMQProgOptions.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
FairMQ::Transport FairMQTransportFactoryZMQ::fTransportType = FairMQ::Transport::ZMQ;
|
||||
|
||||
FairMQTransportFactoryZMQ::FairMQTransportFactoryZMQ()
|
||||
: fContext(zmq_ctx_new())
|
||||
{
|
||||
int major, minor, patch;
|
||||
zmq_version(&major, &minor, &patch);
|
||||
LOG(DEBUG) << "Transport: Using ZeroMQ library, version: " << major << "." << minor << "." << patch;
|
||||
|
||||
if (!fContext)
|
||||
{
|
||||
LOG(ERROR) << "failed creating context, reason: " << zmq_strerror(errno);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQTransportFactoryZMQ::Initialize(const FairMQProgOptions* config)
|
||||
{
|
||||
int numIoThreads = 1;
|
||||
if (config)
|
||||
{
|
||||
numIoThreads = config->GetValue<int>("io-threads");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(WARN) << "zeromq: FairMQProgOptions not available! Using defaults.";
|
||||
}
|
||||
|
||||
if (zmq_ctx_set(fContext, ZMQ_IO_THREADS, numIoThreads) != 0)
|
||||
{
|
||||
LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
|
||||
// Set the maximum number of allowed sockets on the context.
|
||||
if (zmq_ctx_set(fContext, ZMQ_MAX_SOCKETS, 10000) != 0)
|
||||
{
|
||||
LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
}
|
||||
|
||||
FairMQMessagePtr FairMQTransportFactoryZMQ::CreateMessage() const
|
||||
@@ -42,9 +74,10 @@ FairMQMessagePtr FairMQTransportFactoryZMQ::CreateMessage(void* data, const size
|
||||
return unique_ptr<FairMQMessage>(new FairMQMessageZMQ(data, size, ffn, hint));
|
||||
}
|
||||
|
||||
FairMQSocketPtr FairMQTransportFactoryZMQ::CreateSocket(const string& type, const string& name, const int numIoThreads, const string& id /*= ""*/) const
|
||||
FairMQSocketPtr FairMQTransportFactoryZMQ::CreateSocket(const string& type, const string& name, const string& id /*= ""*/) const
|
||||
{
|
||||
return unique_ptr<FairMQSocket>(new FairMQSocketZMQ(type, name, numIoThreads, id));
|
||||
assert(fContext);
|
||||
return unique_ptr<FairMQSocket>(new FairMQSocketZMQ(type, name, id, fContext));
|
||||
}
|
||||
|
||||
FairMQPollerPtr FairMQTransportFactoryZMQ::CreatePoller(const vector<FairMQChannel>& channels) const
|
||||
@@ -66,3 +99,34 @@ FairMQ::Transport FairMQTransportFactoryZMQ::GetType() const
|
||||
{
|
||||
return fTransportType;
|
||||
}
|
||||
|
||||
void FairMQTransportFactoryZMQ::Shutdown()
|
||||
{
|
||||
if (zmq_ctx_shutdown(fContext) != 0)
|
||||
{
|
||||
LOG(ERROR) << "zeromq: failed shutting down context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQTransportFactoryZMQ::Terminate()
|
||||
{
|
||||
if (fContext)
|
||||
{
|
||||
if (zmq_ctx_term(fContext) != 0)
|
||||
{
|
||||
if (errno == EINTR)
|
||||
{
|
||||
LOG(ERROR) << " failed closing context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
else
|
||||
{
|
||||
fContext = nullptr;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << "shmem: Terminate(): context now available for shutdown";
|
||||
}
|
||||
}
|
||||
|
@@ -19,7 +19,6 @@
|
||||
#include <string>
|
||||
|
||||
#include "FairMQTransportFactory.h"
|
||||
#include "FairMQContextZMQ.h"
|
||||
#include "FairMQMessageZMQ.h"
|
||||
#include "FairMQSocketZMQ.h"
|
||||
#include "FairMQPollerZMQ.h"
|
||||
@@ -29,11 +28,13 @@ class FairMQTransportFactoryZMQ : public FairMQTransportFactory
|
||||
public:
|
||||
FairMQTransportFactoryZMQ();
|
||||
|
||||
virtual void Initialize(const FairMQProgOptions* config);
|
||||
|
||||
virtual FairMQMessagePtr CreateMessage() const;
|
||||
virtual FairMQMessagePtr CreateMessage(const size_t size) const;
|
||||
virtual FairMQMessagePtr CreateMessage(void* data, const size_t size, fairmq_free_fn* ffn, void* hint = NULL) const;
|
||||
virtual FairMQMessagePtr CreateMessage(void* data, const size_t size, fairmq_free_fn* ffn, void* hint = nullptr) const;
|
||||
|
||||
virtual FairMQSocketPtr CreateSocket(const std::string& type, const std::string& name, const int numIoThreads, const std::string& id = "") const;
|
||||
virtual FairMQSocketPtr CreateSocket(const std::string& type, const std::string& name, const std::string& id = "") const;
|
||||
|
||||
virtual FairMQPollerPtr CreatePoller(const std::vector<FairMQChannel>& channels) const;
|
||||
virtual FairMQPollerPtr CreatePoller(const std::unordered_map<std::string, std::vector<FairMQChannel>>& channelsMap, const std::vector<std::string>& channelList) const;
|
||||
@@ -41,10 +42,14 @@ class FairMQTransportFactoryZMQ : public FairMQTransportFactory
|
||||
|
||||
virtual FairMQ::Transport GetType() const;
|
||||
|
||||
virtual void Shutdown();
|
||||
virtual void Terminate();
|
||||
|
||||
virtual ~FairMQTransportFactoryZMQ() {};
|
||||
|
||||
private:
|
||||
static FairMQ::Transport fTransportType;
|
||||
void* fContext;
|
||||
};
|
||||
|
||||
#endif /* FAIRMQTRANSPORTFACTORYZMQ_H_ */
|
||||
|
Reference in New Issue
Block a user