Initialize shared memory only if it is used.

This commit is contained in:
Alexey Rybalchenko 2017-01-09 13:37:39 +01:00 committed by Mohammad Al-Turany
parent ac1da4db66
commit 8cf1cbb930
5 changed files with 43 additions and 35 deletions

View File

@ -50,20 +50,21 @@ FairMQMessageSHM::FairMQMessageSHM(void* data, const size_t size, fairmq_free_fn
, fReceiving(false) , fReceiving(false)
, fQueued(false) , fQueued(false)
{ {
InitializeChunk(size); if (InitializeChunk(size))
memcpy(fOwner->fPtr->GetData(), data, size);
if (ffn)
{ {
ffn(data, hint); memcpy(fOwner->fPtr->GetData(), data, size);
} if (ffn)
else {
{ ffn(data, hint);
free(data); }
else
{
free(data);
}
} }
} }
void FairMQMessageSHM::InitializeChunk(const size_t size) bool FairMQMessageSHM::InitializeChunk(const size_t size)
{ {
string chunkID = fDeviceID + "c" + to_string(fMessageID); string chunkID = fDeviceID + "c" + to_string(fMessageID);
string* ownerID = new string(fDeviceID + "o" + to_string(fMessageID)); string* ownerID = new string(fDeviceID + "o" + to_string(fMessageID));
@ -94,12 +95,17 @@ void FairMQMessageSHM::InitializeChunk(const size_t size)
} }
} }
if (zmq_msg_init_data(&fMessage, const_cast<char*>(ownerID->c_str()), ownerID->length(), StringDeleter, ownerID) != 0) if (success)
{ {
LOG(ERROR) << "failed initializing meta message, reason: " << zmq_strerror(errno); if (zmq_msg_init_data(&fMessage, const_cast<char*>(ownerID->c_str()), ownerID->length(), StringDeleter, ownerID) != 0)
{
LOG(ERROR) << "failed initializing meta message, reason: " << zmq_strerror(errno);
}
++fMessageID;
} }
++fMessageID; return success;
} }
void FairMQMessageSHM::Rebuild() void FairMQMessageSHM::Rebuild()
@ -132,16 +138,17 @@ void FairMQMessageSHM::Rebuild(void* data, const size_t size, fairmq_free_fn* ff
fReceiving = false; fReceiving = false;
fQueued = false; fQueued = false;
InitializeChunk(size); if (InitializeChunk(size))
memcpy(fOwner->fPtr->GetData(), data, size);
if (ffn)
{ {
ffn(data, hint); memcpy(fOwner->fPtr->GetData(), data, size);
} if (ffn)
else {
{ ffn(data, hint);
free(data); }
else
{
free(data);
}
} }
} }
@ -192,9 +199,10 @@ void FairMQMessageSHM::Copy(const unique_ptr<FairMQMessage>& msg)
FairMQ::shmem::ShPtrOwner* otherOwner = static_cast<FairMQMessageSHM*>(msg.get())->fOwner; FairMQ::shmem::ShPtrOwner* otherOwner = static_cast<FairMQMessageSHM*>(msg.get())->fOwner;
if (otherOwner) if (otherOwner)
{ {
InitializeChunk(otherOwner->fPtr->GetSize()); if (InitializeChunk(otherOwner->fPtr->GetSize()))
{
memcpy(fOwner->fPtr->GetData(), otherOwner->fPtr->GetData(), otherOwner->fPtr->GetSize()); memcpy(fOwner->fPtr->GetData(), otherOwner->fPtr->GetData(), otherOwner->fPtr->GetSize());
}
} }
else else
{ {

View File

@ -28,7 +28,7 @@ class FairMQMessageSHM : public FairMQMessage
FairMQMessageSHM(const FairMQMessageSHM&) = delete; FairMQMessageSHM(const FairMQMessageSHM&) = delete;
FairMQMessageSHM operator=(const FairMQMessageSHM&) = delete; FairMQMessageSHM operator=(const FairMQMessageSHM&) = delete;
void InitializeChunk(const size_t size); bool InitializeChunk(const size_t size);
virtual void Rebuild(); virtual void Rebuild();
virtual void Rebuild(const size_t size); virtual void Rebuild(const size_t size);

View File

@ -17,8 +17,8 @@ using namespace std;
using namespace FairMQ::shmem; using namespace FairMQ::shmem;
// Context to hold the ZeroMQ sockets // Context to hold the ZeroMQ sockets
unique_ptr<FairMQContextSHM> FairMQSocketSHM::fContext = unique_ptr<FairMQContextSHM>(new FairMQContextSHM(1)); unique_ptr<FairMQContextSHM> FairMQSocketSHM::fContext; // = unique_ptr<FairMQContextSHM>(new FairMQContextSHM(1));
// bool FairMQSocketSHM::fContextInitialized = false; bool FairMQSocketSHM::fContextInitialized = false;
FairMQSocketSHM::FairMQSocketSHM(const string& type, const string& name, const int numIoThreads, const string& id /*= ""*/) FairMQSocketSHM::FairMQSocketSHM(const string& type, const string& name, const int numIoThreads, const string& id /*= ""*/)
: FairMQSocket(ZMQ_SNDMORE, ZMQ_RCVMORE, ZMQ_DONTWAIT) : FairMQSocket(ZMQ_SNDMORE, ZMQ_RCVMORE, ZMQ_DONTWAIT)
@ -31,11 +31,11 @@ FairMQSocketSHM::FairMQSocketSHM(const string& type, const string& name, const i
{ {
fId = id + "." + name + "." + type; fId = id + "." + name + "." + type;
// if (!fContextInitialized) if (!fContextInitialized)
// { {
// fContext = unique_ptr<FairMQContextSHM>(new FairMQContextSHM(1)); fContext = unique_ptr<FairMQContextSHM>(new FairMQContextSHM(1));
// fContextInitialized = true; fContextInitialized = true;
// } }
if (zmq_ctx_set(fContext->GetContext(), ZMQ_IO_THREADS, numIoThreads) != 0) if (zmq_ctx_set(fContext->GetContext(), ZMQ_IO_THREADS, numIoThreads) != 0)
{ {

View File

@ -70,7 +70,7 @@ class FairMQSocketSHM : public FairMQSocket
std::atomic<unsigned long> fMessagesRx; std::atomic<unsigned long> fMessagesRx;
static std::unique_ptr<FairMQContextSHM> fContext; static std::unique_ptr<FairMQContextSHM> fContext;
// static bool fContextInitialized; static bool fContextInitialized;
}; };
#endif /* FAIRMQSOCKETSHM_H_ */ #endif /* FAIRMQSOCKETSHM_H_ */

View File

@ -115,7 +115,7 @@ FairMQPollerZMQ::FairMQPollerZMQ(const unordered_map<string, vector<FairMQChanne
{ {
LOG(ERROR) << "At least one of the provided channel keys for poller initialization is invalid"; LOG(ERROR) << "At least one of the provided channel keys for poller initialization is invalid";
LOG(ERROR) << "Out of Range error: " << oor.what() << '\n'; LOG(ERROR) << "Out of Range error: " << oor.what() << '\n';
exit(EXIT_FAILURE); throw std::out_of_range("Invalid channel during poller initialization");
} }
} }