Skip to content

Commit 13018ab

Browse files
committed
DPL: earlier forwarding
This anticipates the forwarding to the earliest possible moment, i.e. when we are about to insert the messages in a slot. This is the earliest moment we can guarantee messages will be seen only once.
1 parent 4325909 commit 13018ab

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

Framework/Core/include/Framework/DataRelayer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ class DataRelayer
114114

115115
using OnDropCallback = std::function<void(TimesliceSlot, std::vector<MessageSet>&, TimesliceIndex::OldestOutputInfo info)>;
116116

117+
// Callback for when some messages are about to be owned by the the DataRelayer
118+
using OnInsertionCallback = std::function<void(ServiceRegistryRef&, std::span<fair::mq::MessagePtr>&)>;
119+
117120
/// Prune all the pending entries in the cache.
118121
void prunePending(OnDropCallback);
119122
/// Prune the cache for a given slot
@@ -135,6 +138,7 @@ class DataRelayer
135138
InputInfo const& info,
136139
size_t nMessages,
137140
size_t nPayloads = 1,
141+
OnInsertionCallback onInsertion = nullptr,
138142
OnDropCallback onDrop = nullptr);
139143

140144
/// This is to set the oldest possible @a timeslice this relayer can

Framework/Core/src/DataProcessingDevice.cxx

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,11 +1854,56 @@ void DataProcessingDevice::handleData(ServiceRegistryRef ref, InputChannelInfo&
18541854
VariableContextHelpers::getTimeslice(variables);
18551855
forwardInputs(ref, slot, dropped, oldestOutputInfo, false, true);
18561856
};
1857+
1858+
auto onInsertion = [](ServiceRegistryRef& ref, std::span<fair::mq::MessagePtr>& messages) {
1859+
O2_SIGNPOST_ID_GENERATE(sid, forwarding);
1860+
1861+
auto& spec = ref.get<DeviceSpec const>();
1862+
bool hasForwards = spec.forwards.empty() == false;
1863+
auto& context = ref.get<DataProcessorContext>();
1864+
if (context.canForwardEarly && hasForwards) {
1865+
O2_SIGNPOST_EVENT_EMIT(device, sid, "device", "Early forwardinding before injecting data into relayer.");
1866+
auto& timesliceIndex = ref.get<TimesliceIndex>();
1867+
auto oldestTimeslice = timesliceIndex.getOldestPossibleOutput();
1868+
1869+
auto& proxy = ref.get<FairMQDeviceProxy>();
1870+
1871+
O2_SIGNPOST_ID_GENERATE(sid, forwarding);
1872+
O2_SIGNPOST_START(forwarding, sid, "forwardInputs", "Starting forwarding for incoming messages with oldestTimeslice %zu with copy",
1873+
oldestTimeslice.timeslice.value);
1874+
std::vector<fair::mq::Parts> forwardedParts;
1875+
forwardedParts.resize(proxy.getNumForwards());
1876+
DataProcessingHelpers::routeForwardedMessages(proxy, messages, forwardedParts, true, false);
1877+
1878+
for (int fi = 0; fi < proxy.getNumForwardChannels(); fi++) {
1879+
if (forwardedParts[fi].Size() == 0) {
1880+
continue;
1881+
}
1882+
ForwardChannelInfo info = proxy.getForwardChannelInfo(ChannelIndex{fi});
1883+
auto& parts = forwardedParts[fi];
1884+
if (info.policy == nullptr) {
1885+
O2_SIGNPOST_EVENT_EMIT_ERROR(forwarding, sid, "forwardInputs", "Forwarding to %{public}s %d has no policy.", info.name.c_str(), fi);
1886+
continue;
1887+
}
1888+
O2_SIGNPOST_EVENT_EMIT(forwarding, sid, "forwardInputs", "Forwarding to %{public}s %d", info.name.c_str(), fi);
1889+
info.policy->forward(parts, ChannelIndex{fi}, ref);
1890+
}
1891+
auto& asyncQueue = ref.get<AsyncQueue>();
1892+
auto& decongestion = ref.get<DecongestionService>();
1893+
O2_SIGNPOST_ID_GENERATE(aid, async_queue);
1894+
O2_SIGNPOST_EVENT_EMIT(async_queue, aid, "forwardInputs", "Queuing forwarding oldestPossible %zu", oldestTimeslice.timeslice.value);
1895+
AsyncQueueHelpers::post(asyncQueue, AsyncTask{.timeslice = oldestTimeslice.timeslice, .id = decongestion.oldestPossibleTimesliceTask, .debounce = -1, .callback = decongestionCallbackLate}
1896+
.user<DecongestionContext>({.ref = ref, .oldestTimeslice = oldestTimeslice}));
1897+
O2_SIGNPOST_END(forwarding, sid, "forwardInputs", "Forwarding done");
1898+
}
1899+
};
1900+
18571901
auto relayed = relayer.relay(parts.At(headerIndex)->GetData(),
18581902
&parts.At(headerIndex),
18591903
input,
18601904
nMessages,
18611905
nPayloadsPerHeader,
1906+
onInsertion,
18621907
onDrop);
18631908
switch (relayed.type) {
18641909
case DataRelayer::RelayChoice::Type::Backpressured:
@@ -2273,9 +2318,10 @@ bool DataProcessingDevice::tryDispatchComputation(ServiceRegistryRef ref, std::v
22732318
bool consumeSomething = action.op == CompletionPolicy::CompletionOp::Consume || action.op == CompletionPolicy::CompletionOp::ConsumeExisting;
22742319

22752320
if (context.canForwardEarly && hasForwards && consumeSomething) {
2276-
O2_SIGNPOST_EVENT_EMIT(device, aid, "device", "Early forwainding: %{public}s.", fmt::format("{}", action.op).c_str());
2277-
auto& timesliceIndex = ref.get<TimesliceIndex>();
2278-
forwardInputs(ref, action.slot, currentSetOfInputs, timesliceIndex.getOldestPossibleOutput(), true, action.op == CompletionPolicy::CompletionOp::Consume);
2321+
// We used to do fowarding here, however we now do it much earlier.
2322+
// We still need to clean the inputs which were already consumed
2323+
// via ConsumeExisting and which still have an header to hold the slot.
2324+
DataProcessingHelpers::cleanForwardedMessageSet(currentSetOfInputs);
22792325
}
22802326
markInputsAsDone(action.slot);
22812327

Framework/Core/src/DataRelayer.cxx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,8 @@ DataRelayer::RelayChoice
436436
InputInfo const& info,
437437
size_t nMessages,
438438
size_t nPayloads,
439-
std::function<void(TimesliceSlot, std::vector<MessageSet>&, TimesliceIndex::OldestOutputInfo)> onDrop)
439+
OnInsertionCallback onInsertion,
440+
OnDropCallback onDrop)
440441
{
441442
std::scoped_lock<O2_LOCKABLE(std::recursive_mutex)> lock(mMutex);
442443
DataProcessingHeader const* dph = o2::header::get<DataProcessingHeader*>(rawHeader);
@@ -482,6 +483,7 @@ DataRelayer::RelayChoice
482483
&messages,
483484
&nMessages,
484485
&nPayloads,
486+
&onInsertion,
485487
&cache = mCache,
486488
&services = mContext,
487489
numInputTypes = mDistinctRoutesIndex.size()](TimesliceId timeslice, int input, TimesliceSlot slot, InputInfo const& info) -> size_t {
@@ -512,7 +514,11 @@ DataRelayer::RelayChoice
512514
mi += nPayloads;
513515
continue;
514516
}
515-
target.add([&messages, &mi](size_t i) -> fair::mq::MessagePtr& { return messages[mi + i]; }, nPayloads + 1);
517+
auto span = std::span<fair::mq::MessagePtr>(messages + mi, messages + mi + nPayloads + 1);
518+
if (onInsertion) {
519+
onInsertion(services, span);
520+
}
521+
target.add([&span](size_t i) -> fair::mq::MessagePtr& { return span[i]; }, nPayloads + 1);
516522
mi += nPayloads;
517523
saved += nPayloads;
518524
}

0 commit comments

Comments
 (0)