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,83 +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" *
|
||||
********************************************************************************/
|
||||
#include <zmq.h>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include <boost/interprocess/managed_shared_memory.hpp>
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
#include "FairMQContextSHM.h"
|
||||
#include "FairMQShmManager.h"
|
||||
|
||||
using namespace FairMQ::shmem;
|
||||
|
||||
FairMQContextSHM::FairMQContextSHM(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);
|
||||
}
|
||||
|
||||
Manager::Instance().InitializeSegment("open_or_create", "FairMQSharedMemory", 2000000000);
|
||||
LOG(INFO) << "Created/Opened shared memory segment of 2,000,000,000 bytes. Available are " << Manager::Instance().Segment()->get_free_memory() << " bytes.";
|
||||
}
|
||||
|
||||
FairMQContextSHM::~FairMQContextSHM()
|
||||
{
|
||||
Close();
|
||||
|
||||
if (boost::interprocess::shared_memory_object::remove("FairMQSharedMemory"))
|
||||
{
|
||||
fprintf(stderr, "Successfully removed shared memory after the device has stopped.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Did not remove shared memory after the device stopped. Already removed?\n");
|
||||
}
|
||||
}
|
||||
|
||||
void* FairMQContextSHM::GetContext()
|
||||
{
|
||||
return fContext;
|
||||
}
|
||||
|
||||
void FairMQContextSHM::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,27 +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" *
|
||||
********************************************************************************/
|
||||
#ifndef FAIRMQCONTEXTSHM_H_
|
||||
#define FAIRMQCONTEXTSHM_H_
|
||||
|
||||
class FairMQContextSHM
|
||||
{
|
||||
public:
|
||||
/// Constructor
|
||||
FairMQContextSHM(int numIoThreads);
|
||||
FairMQContextSHM(const FairMQContextSHM&) = delete;
|
||||
FairMQContextSHM operator=(const FairMQContextSHM&) = delete;
|
||||
|
||||
virtual ~FairMQContextSHM();
|
||||
void* GetContext();
|
||||
void Close();
|
||||
|
||||
private:
|
||||
void* fContext;
|
||||
};
|
||||
|
||||
#endif /* FAIRMQCONTEXTSHM_H_ */
|
@@ -16,12 +16,9 @@
|
||||
using namespace std;
|
||||
using namespace FairMQ::shmem;
|
||||
|
||||
// Context to hold the ZeroMQ sockets
|
||||
unique_ptr<FairMQContextSHM> FairMQSocketSHM::fContext; // = unique_ptr<FairMQContextSHM>(new FairMQContextSHM(1));
|
||||
bool FairMQSocketSHM::fContextInitialized = false;
|
||||
atomic<bool> FairMQSocketSHM::fInterrupted(false);
|
||||
|
||||
FairMQSocketSHM::FairMQSocketSHM(const string& type, const string& name, const int numIoThreads, const string& id /*= ""*/)
|
||||
FairMQSocketSHM::FairMQSocketSHM(const string& type, const string& name, const string& id /*= ""*/, void* context)
|
||||
: FairMQSocket(ZMQ_SNDMORE, ZMQ_RCVMORE, ZMQ_DONTWAIT)
|
||||
, fSocket(NULL)
|
||||
, fId()
|
||||
@@ -32,18 +29,8 @@ FairMQSocketSHM::FairMQSocketSHM(const string& type, const string& name, const i
|
||||
{
|
||||
fId = id + "." + name + "." + type;
|
||||
|
||||
if (!fContextInitialized)
|
||||
{
|
||||
fContext = unique_ptr<FairMQContextSHM>(new FairMQContextSHM(1));
|
||||
fContextInitialized = true;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -409,14 +396,6 @@ void FairMQSocketSHM::Close()
|
||||
fSocket = NULL;
|
||||
}
|
||||
|
||||
void FairMQSocketSHM::Terminate()
|
||||
{
|
||||
if (zmq_ctx_destroy(fContext->GetContext()) != 0)
|
||||
{
|
||||
LOG(ERROR) << "Failed terminating context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQSocketSHM::Interrupt()
|
||||
{
|
||||
FairMQMessageSHM::fInterrupted = true;
|
||||
|
@@ -14,13 +14,12 @@
|
||||
|
||||
#include "FairMQSocket.h"
|
||||
#include "FairMQMessage.h"
|
||||
#include "FairMQContextSHM.h"
|
||||
#include "FairMQShmManager.h"
|
||||
|
||||
class FairMQSocketSHM : public FairMQSocket
|
||||
{
|
||||
public:
|
||||
FairMQSocketSHM(const std::string& type, const std::string& name, const int numIoThreads, const std::string& id = "");
|
||||
FairMQSocketSHM(const std::string& type, const std::string& name, const std::string& id = "", void* context = nullptr);
|
||||
FairMQSocketSHM(const FairMQSocketSHM&) = delete;
|
||||
FairMQSocketSHM operator=(const FairMQSocketSHM&) = delete;
|
||||
|
||||
@@ -38,7 +37,6 @@ class FairMQSocketSHM : public FairMQSocket
|
||||
virtual void* GetSocket() const;
|
||||
virtual int GetSocket(int nothing) const;
|
||||
virtual void Close();
|
||||
virtual void Terminate();
|
||||
|
||||
virtual void Interrupt();
|
||||
virtual void Resume();
|
||||
@@ -68,8 +66,6 @@ class FairMQSocketSHM : public FairMQSocket
|
||||
std::atomic<unsigned long> fMessagesTx;
|
||||
std::atomic<unsigned long> fMessagesRx;
|
||||
|
||||
static std::unique_ptr<FairMQContextSHM> fContext;
|
||||
static bool fContextInitialized;
|
||||
static std::atomic<bool> fInterrupted;
|
||||
};
|
||||
|
||||
|
@@ -7,19 +7,59 @@
|
||||
********************************************************************************/
|
||||
#include "zmq.h"
|
||||
#include <boost/version.hpp>
|
||||
#include <boost/interprocess/managed_shared_memory.hpp>
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
#include "FairMQShmManager.h"
|
||||
#include "FairMQTransportFactorySHM.h"
|
||||
#include "../options/FairMQProgOptions.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace FairMQ::shmem;
|
||||
|
||||
FairMQ::Transport FairMQTransportFactorySHM::fTransportType = FairMQ::Transport::SHM;
|
||||
|
||||
FairMQTransportFactorySHM::FairMQTransportFactorySHM()
|
||||
: fContext(nullptr)
|
||||
{
|
||||
int major, minor, patch;
|
||||
zmq_version(&major, &minor, &patch);
|
||||
LOG(DEBUG) << "Transport: Using ZeroMQ (" << major << "." << minor << "." << patch << ") & "
|
||||
<< "boost::interprocess (" << (BOOST_VERSION / 100000) << "." << (BOOST_VERSION / 100 % 1000) << "." << (BOOST_VERSION % 100) << ")";
|
||||
|
||||
fContext = zmq_ctx_new();
|
||||
if (!fContext)
|
||||
{
|
||||
LOG(ERROR) << "failed creating context, reason: " << zmq_strerror(errno);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQTransportFactorySHM::Initialize(const FairMQProgOptions* config)
|
||||
{
|
||||
int numIoThreads = 1;
|
||||
if (config)
|
||||
{
|
||||
numIoThreads = config->GetValue<int>("io-threads");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(WARN) << "shmem: 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);
|
||||
}
|
||||
|
||||
Manager::Instance().InitializeSegment("open_or_create", "FairMQSharedMemory", 2000000000);
|
||||
LOG(DEBUG) << "shmem: created/opened shared memory segment of 2000000000 bytes. Available are " << Manager::Instance().Segment()->get_free_memory() << " bytes.";
|
||||
}
|
||||
|
||||
FairMQMessagePtr FairMQTransportFactorySHM::CreateMessage() const
|
||||
@@ -37,9 +77,10 @@ FairMQMessagePtr FairMQTransportFactorySHM::CreateMessage(void* data, const size
|
||||
return unique_ptr<FairMQMessage>(new FairMQMessageSHM(data, size, ffn, hint));
|
||||
}
|
||||
|
||||
FairMQSocketPtr FairMQTransportFactorySHM::CreateSocket(const string& type, const string& name, const int numIoThreads, const string& id /*= ""*/) const
|
||||
FairMQSocketPtr FairMQTransportFactorySHM::CreateSocket(const string& type, const string& name, const string& id /*= ""*/) const
|
||||
{
|
||||
return unique_ptr<FairMQSocket>(new FairMQSocketSHM(type, name, numIoThreads, id));
|
||||
assert(fContext);
|
||||
return unique_ptr<FairMQSocket>(new FairMQSocketSHM(type, name, id, fContext));
|
||||
}
|
||||
|
||||
FairMQPollerPtr FairMQTransportFactorySHM::CreatePoller(const vector<FairMQChannel>& channels) const
|
||||
@@ -57,6 +98,46 @@ FairMQPollerPtr FairMQTransportFactorySHM::CreatePoller(const FairMQSocket& cmdS
|
||||
return unique_ptr<FairMQPoller>(new FairMQPollerSHM(cmdSocket, dataSocket));
|
||||
}
|
||||
|
||||
void FairMQTransportFactorySHM::Shutdown()
|
||||
{
|
||||
if (zmq_ctx_shutdown(fContext) != 0)
|
||||
{
|
||||
LOG(ERROR) << "shmem: failed shutting down context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQTransportFactorySHM::Terminate()
|
||||
{
|
||||
if (fContext)
|
||||
{
|
||||
if (zmq_ctx_term(fContext) != 0)
|
||||
{
|
||||
if (errno == EINTR)
|
||||
{
|
||||
LOG(ERROR) << "shmem: failed closing context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
else
|
||||
{
|
||||
fContext = NULL;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << "shmem: Terminate(): context now available for shutdown";
|
||||
}
|
||||
|
||||
if (boost::interprocess::shared_memory_object::remove("FairMQSharedMemory"))
|
||||
{
|
||||
LOG(DEBUG) << "shmem: successfully removed shared memory segment after the device has stopped.";
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(DEBUG) << "shmem: did not remove shared memory segment after the device stopped. Already removed?";
|
||||
}
|
||||
}
|
||||
|
||||
FairMQ::Transport FairMQTransportFactorySHM::GetType() const
|
||||
{
|
||||
return fTransportType;
|
||||
|
@@ -12,7 +12,6 @@
|
||||
#include <string>
|
||||
|
||||
#include "FairMQTransportFactory.h"
|
||||
#include "FairMQContextSHM.h"
|
||||
#include "FairMQMessageSHM.h"
|
||||
#include "FairMQSocketSHM.h"
|
||||
#include "FairMQPollerSHM.h"
|
||||
@@ -22,11 +21,13 @@ class FairMQTransportFactorySHM : public FairMQTransportFactory
|
||||
public:
|
||||
FairMQTransportFactorySHM();
|
||||
|
||||
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;
|
||||
@@ -34,10 +35,14 @@ class FairMQTransportFactorySHM : public FairMQTransportFactory
|
||||
|
||||
virtual FairMQ::Transport GetType() const;
|
||||
|
||||
virtual void Shutdown();
|
||||
virtual void Terminate();
|
||||
|
||||
virtual ~FairMQTransportFactorySHM() {};
|
||||
|
||||
private:
|
||||
static FairMQ::Transport fTransportType;
|
||||
void* fContext;
|
||||
};
|
||||
|
||||
#endif /* FAIRMQTRANSPORTFACTORYSHM_H_ */
|
||||
|
Reference in New Issue
Block a user