FairMQ: Introduce callbacks for the FairMQUnmanagedRegion.

Callbacks are called when the data buffer of the message assiciated
with the corresponding region is no longer needed by the transport.
Example in examples/advanced/Region/
This commit is contained in:
Alexey Rybalchenko 2017-11-14 17:00:37 +01:00 committed by Mohammad Al-Turany
parent 0fc0a37ada
commit ce162364fa
4 changed files with 45 additions and 13 deletions

View File

@ -16,29 +16,54 @@
#include "FairMQLogger.h" #include "FairMQLogger.h"
#include "FairMQProgOptions.h" // device->fConfig #include "FairMQProgOptions.h" // device->fConfig
#include <thread>
#include <atomic>
using namespace std; using namespace std;
FairMQExampleRegionSampler::FairMQExampleRegionSampler() FairMQExampleRegionSampler::FairMQExampleRegionSampler()
: fMsgSize(10000) : fMsgSize(10000)
, fRegion(nullptr)
, fNumUnackedMsgs(0)
{ {
} }
void FairMQExampleRegionSampler::InitTask() void FairMQExampleRegionSampler::InitTask()
{ {
fMsgSize = fConfig->GetValue<int>("msg-size"); fMsgSize = fConfig->GetValue<int>("msg-size");
fRegion = FairMQUnmanagedRegionPtr(NewUnmanagedRegionFor("data",
0,
10000000,
[this](void* data, size_t size) { --fNumUnackedMsgs; } // callback to be called when message buffers no longer needed by transport
));
} }
void FairMQExampleRegionSampler::Run() bool FairMQExampleRegionSampler::ConditionalRun()
{ {
FairMQChannel& dataOutChannel = fChannels.at("data").at(0); FairMQMessagePtr msg(NewMessageFor("data", // channel
0, // sub-channel
FairMQUnmanagedRegionPtr region(NewUnmanagedRegionFor("data", 0, 10000000)); fRegion, // region
fRegion->GetData(), // ptr within region
while (CheckCurrentState(RUNNING)) fMsgSize // offset from ptr
));
if (Send(msg, "data", 0) > 0)
{ {
FairMQMessagePtr msg(NewMessageFor("data", 0, region, region->GetData(), fMsgSize)); ++fNumUnackedMsgs;
dataOutChannel.Send(msg);
} }
return true;
}
void FairMQExampleRegionSampler::ResetTask()
{
// if not all messages acknowledged, wait for a bit. But only once, since receiver could be already dead.
if (fNumUnackedMsgs != 0)
{
LOG(DEBUG) << "waiting for all acknowledgements... (" << fNumUnackedMsgs << ")";
this_thread::sleep_for(chrono::milliseconds(500));
}
fRegion.reset();
} }
FairMQExampleRegionSampler::~FairMQExampleRegionSampler() FairMQExampleRegionSampler::~FairMQExampleRegionSampler()

View File

@ -16,6 +16,7 @@
#define FAIRMQEXAMPLEREGIONSAMPLER_H_ #define FAIRMQEXAMPLEREGIONSAMPLER_H_
#include <string> #include <string>
#include <atomic>
#include "FairMQDevice.h" #include "FairMQDevice.h"
@ -26,10 +27,14 @@ class FairMQExampleRegionSampler : public FairMQDevice
virtual ~FairMQExampleRegionSampler(); virtual ~FairMQExampleRegionSampler();
protected: protected:
int fMsgSize;
virtual void InitTask(); virtual void InitTask();
virtual void Run(); virtual bool ConditionalRun();
virtual void ResetTask();
private:
int fMsgSize;
FairMQUnmanagedRegionPtr fRegion;
std::atomic<uint64_t> fNumUnackedMsgs;
}; };
#endif /* FAIRMQEXAMPLEREGIONSAMPLER_H_ */ #endif /* FAIRMQEXAMPLEREGIONSAMPLER_H_ */

View File

@ -1,8 +1,8 @@
/******************************************************************************** /********************************************************************************
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* * * *
* This software is distributed under the terms of the * * This software is distributed under the terms of the *
* GNU Lesser General Public Licence version 3 (LGPL) version 3, * * GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" * * copied verbatim in the file "LICENSE" *
********************************************************************************/ ********************************************************************************/
/** /**
@ -29,6 +29,7 @@ void FairMQExampleRegionSink::Run()
{ {
FairMQMessagePtr msg(dataInChannel.Transport()->CreateMessage()); FairMQMessagePtr msg(dataInChannel.Transport()->CreateMessage());
dataInChannel.Receive(msg); dataInChannel.Receive(msg);
void* ptr = msg->GetData();
} }
} }

View File

@ -10,6 +10,7 @@ fi
SAMPLER="ex-region-sampler" SAMPLER="ex-region-sampler"
SAMPLER+=" --id sampler1" SAMPLER+=" --id sampler1"
SAMPLER+=" --msg-size $msgSize" SAMPLER+=" --msg-size $msgSize"
# SAMPLER+=" --rate 10"
SAMPLER+=" --transport shmem" SAMPLER+=" --transport shmem"
SAMPLER+=" --mq-config $exRegionConfig" SAMPLER+=" --mq-config $exRegionConfig"
xterm -geometry 80x23+0+0 -hold -e @CMAKE_BINARY_DIR@/bin/examples/advanced/Region/$SAMPLER & xterm -geometry 80x23+0+0 -hold -e @CMAKE_BINARY_DIR@/bin/examples/advanced/Region/$SAMPLER &