245 static std::vector<bool> present;
246 static std::vector<bool> ignored;
247 static std::vector<size_t> dataSizes;
248 static std::vector<bool> showSize;
250 present.resize(
routes.size(),
false);
252 ignored.resize(
routes.size(),
false);
254 dataSizes.resize(
routes.size(), 0);
256 showSize.resize(
routes.size(),
false);
258 static std::vector<size_t> unmatchedDescriptions;
259 unmatchedDescriptions.clear();
265 size_t expectedDataSpecs = 0;
266 for (
size_t pi = 0; pi < present.size(); ++pi) {
267 auto& spec =
routes[pi].matcher;
272 if (
routes[pi].timeslice == 0) {
277 size_t foundDataSpecs = 0;
278 bool skipAsAllFound =
false;
279 for (
int msgidx = 0; msgidx < parts.Size(); msgidx += 2) {
280 bool allFound =
true;
282 const auto dh = o2::header::get<DataHeader*>(parts.At(msgidx)->GetData());
283 auto const sih = o2::header::get<SourceInfoHeader*>(parts.At(msgidx)->GetData());
284 if (sih !=
nullptr) {
288 if (parts.At(msgidx).get() ==
nullptr) {
289 LOG(error) <<
"unexpected nullptr found. Skipping message pair.";
293 LOG(error) <<
"data on input " << msgidx <<
" does not follow the O2 data model, DataHeader missing";
299 if (firstDH ==
nullptr) {
301 if (doPrintSizes && firstDH->
tfCounter % doPrintSizes != 0) {
306 if (dph ==
nullptr) {
307 dph = o2::header::get<DataProcessingHeader*>(parts.At(msgidx)->GetData());
308 for (
size_t pi = 0; pi < present.size(); ++pi) {
314 for (
size_t pi = 0; pi < present.size(); ++pi) {
315 if ((present[pi] || ignored[pi]) && !doPrintSizes) {
324 auto& spec =
routes[pi].matcher;
325 OutputSpec query{dh->dataOrigin, dh->dataDescription, dh->subSpecification};
327 if (!present[pi] && !ignored[pi]) {
337 if (addToSize >= 0) {
338 int increment = (dh->splitPayloadParts > 0 && dh->splitPayloadParts == dh->splitPayloadIndex) ? 1 : 2;
339 for (
int msgidx2 = msgidx + 1; msgidx2 < msgidxLast; msgidx2 += increment) {
340 dataSizes[addToSize] += parts.At(msgidx2)->GetSize();
344 msgidx = msgidxLast - 2;
345 if (allFound && !doPrintSizes) {
346 skipAsAllFound =
true;
352 for (
size_t pi = 0; pi < present.size(); ++pi) {
353 if (present[pi] && !ignored[pi]) {
356 if (!present[pi] && !ignored[pi]) {
358 unmatchedDescriptions.push_back(pi);
361 int timeframeCompleteness = emptyTf ? 0 : (unmatchedDescriptions.size() ? -1 : 1);
362 (
void)timeframeCompleteness;
364 if (skipAsAllFound && !doPrintSizes) {
368 if (firstDH && doPrintSizes) {
369 std::string
sizes =
"";
370 size_t totalSize = 0;
371 for (
size_t pi = 0; pi < present.size(); ++pi) {
373 totalSize += dataSizes[pi];
374 auto& spec =
routes[pi].matcher;
378 LOGP(important,
"RAW {} size report:{}- Total:{}", firstDH->
tfCounter,
sizes, fmt::group_digits(totalSize));
381 if (!doInjectMissingData) {
385 if (unmatchedDescriptions.size() > 0) {
388 LOG(error) <<
"Received an EndOfStream message together with data. This should not happen.";
390 LOG(detail) <<
"This is an End Of Stream message. Not injecting anything.";
393 if (firstDH ==
nullptr) {
394 LOG(error) <<
"Input proxy received incomplete data without any data header. This should not happen! Cannot inject missing data as requsted.";
397 if (dph ==
nullptr) {
398 LOG(error) <<
"Input proxy received incomplete data without any data processing header. This should happen! Cannot inject missing data as requsted.";
401 std::string missing =
"";
402 bool showAlarm =
false;
403 uint32_t runNumber = 0;
405 runNumber = strtoul(device.fConfig->GetProperty<std::string>(
"runNumber",
"").c_str(),
nullptr, 10);
408 for (
auto mi : unmatchedDescriptions) {
409 auto& spec =
routes[mi].matcher;
415 if (subSpec == std::nullopt) {
416 *subSpec = 0xDEADBEEF;
421 dh.subSpecification = *subSpec;
423 dh.runNumber = runNumber;
424 dh.splitPayloadParts = 0;
425 dh.splitPayloadIndex = 0;
428 auto& channelName =
routes[mi].channel;
429 auto& channelInfo = device.GetChannel(channelName);
430 auto channelAlloc = o2::pmr::getTransportAllocator(channelInfo.Transport());
432 parts.AddPart(std::move(headerMessage));
434 parts.AddPart(device.NewMessageFor(channelName, 0, 0));
439 static int maxWarn = 10;
440 static int contDeadBeef = 0;
441 if (showAlarm && ++contDeadBeef <= maxWarn) {
442 LOGP(alarm,
"Found {}/{} data specs, missing data specs: {}, injecting 0xDEADBEEF{}", foundDataSpecs, expectedDataSpecs, missing, contDeadBeef == maxWarn ?
" - disabling alarm now to stop flooding the log" :
"");
451 class DroppedDataSpecs
454 DroppedDataSpecs() =
default;
460 [[nodiscard]]
bool find(std::string
const& desc)
const
462 return descriptions.find(desc) != std::string::npos;
465 void add(std::string
const& desc)
467 descriptions +=
"\n " + desc;
472 if (not descriptions.empty()) {
473 LOG(warning) <<
"Some input data could not be matched by filter rules to output specs\n"
474 <<
"Active rules: " << descriptions <<
"\n"
475 <<
"DROPPING OF THESE MESSAGES HAS BEEN ENABLED BY CONFIGURATION";
480 std::string descriptions;
483 return [filterSpecs = std::move(filterSpecs), throwOnUnmatchedInputs, droppedDataSpecs = std::make_shared<DroppedDataSpecs>()](
TimingInfo& timingInfo,
ServiceRegistryRef const& services, fair::mq::Parts& parts,
ChannelRetriever channelRetriever,
size_t newTimesliceId,
bool& stop) {
485 std::unordered_map<std::string, fair::mq::Parts> outputs;
486 std::vector<std::string> unmatchedDescriptions;
489 static bool override_creation_env = getenv(
"DPL_RAWPROXY_OVERRIDE_ORBITRESET");
490 bool override_creation =
false;
491 uint64_t creationVal = 0;
492 if (override_creation_env) {
493 static uint64_t creationValBase = std::stoul(getenv(
"DPL_RAWPROXY_OVERRIDE_ORBITRESET"));
494 creationVal = creationValBase;
495 override_creation =
true;
497 auto orbitResetTimeUrl = device->fConfig->GetProperty<std::string>(
"orbit-reset-time",
"ccdb://CTP/Calib/OrbitResetTime");
499 creationVal = std::strtoll(orbitResetTimeUrl.c_str(), &err, 10);
500 if (err && *err == 0 && creationVal) {
501 override_creation =
true;
505 int fmqRunNumber = -1;
507 fmqRunNumber = atoi(device->fConfig->GetProperty<std::string>(
"runNumber",
"").c_str());
511 for (
int msgidx = 0; msgidx < parts.Size(); msgidx += 2) {
512 if (parts.At(msgidx).get() ==
nullptr) {
513 LOG(error) <<
"unexpected nullptr found. Skipping message pair.";
516 auto* header = parts.At(msgidx)->GetData();
517 const auto dh = o2::header::get<DataHeader*>(header);
519 LOG(error) <<
"data on input " << msgidx <<
" does not follow the O2 data model, DataHeader missing";
525 auto dph = o2::header::get<DataProcessingHeader*>(header);
527 LOG(error) <<
"data on input " << msgidx <<
" does not follow the O2 data model, DataProcessingHeader missing";
531 if (override_creation) {
535 timingInfo.creation = dph->creation;
536 timingInfo.firstTForbit = dh->firstTForbit;
537 timingInfo.runNumber = dh->runNumber;
538 timingInfo.tfCounter = dh->tfCounter;
539 LOG(
debug) << msgidx <<
": " <<
DataSpecUtils::describe(
OutputSpec{dh->dataOrigin, dh->dataDescription, dh->subSpecification}) <<
" part " << dh->splitPayloadIndex <<
" of " << dh->splitPayloadParts <<
" payload " << parts.At(msgidx + 1)->GetSize();
541 LOG(error) <<
"INVALID runNumber / tfCounter: runNumber " << dh->runNumber
542 <<
", tfCounter " << dh->tfCounter <<
", FMQ runNumber " << fmqRunNumber
543 <<
" for msgidx " << msgidx <<
": " <<
DataSpecUtils::describe(
OutputSpec{dh->dataOrigin, dh->dataDescription, dh->subSpecification}) <<
" part " << dh->splitPayloadIndex <<
" of " << dh->splitPayloadParts <<
" payload " << parts.At(msgidx + 1)->GetSize();
546 OutputSpec query{dh->dataOrigin, dh->dataDescription, dh->subSpecification};
547 LOG(
debug) <<
"processing " <<
DataSpecUtils::describe(
OutputSpec{dh->dataOrigin, dh->dataDescription, dh->subSpecification}) <<
" time slice " << dph->startTime <<
" part " << dh->splitPayloadIndex <<
" of " << dh->splitPayloadParts;
548 int finalBlockIndex = 0;
549 std::string channelName =
"";
551 for (
auto const& spec : filterSpecs) {
555 channelName = channelRetriever(query, dph->startTime);
564 if (finalBlockIndex > parts.Size()) {
570 if (!channelName.empty()) {
584 LOGP(
debug,
"associating {} part(s) at index {} to channel {} ({})", finalBlockIndex - msgidx, msgidx, channelName, outputs[channelName].
Size());
585 for (; msgidx < finalBlockIndex; ++msgidx) {
586 outputs[channelName].AddPart(std::move(parts.At(msgidx)));
590 msgidx = finalBlockIndex - 2;
597 bool didSendParts =
false;
598 for (
auto& [channelName, channelParts] : outputs) {
599 if (channelParts.Size() == 0) {
603 sendOnChannel(*device, channelParts, channelName, newTimesliceId);
605 if (not unmatchedDescriptions.empty()) {
606 if (throwOnUnmatchedInputs) {
607 std::string descriptions;
608 for (
auto const& desc : unmatchedDescriptions) {
609 descriptions +=
"\n " + desc;
611 throw std::runtime_error(
"No matching filter rule for input data " + descriptions +
612 "\n Add appropriate matcher(s) to dataspec definition or allow to drop unmatched data");
614 bool changed =
false;
615 for (
auto const& desc : unmatchedDescriptions) {
616 if (not droppedDataSpecs->find(desc)) {
618 droppedDataSpecs->add(desc);
623 droppedDataSpecs->warning();
670 std::vector<OutputSpec>
const& outputs,
671 char const* defaultChannelConfig,
675 bool doInjectMissingData,
676 unsigned int doPrintSizes)
682 static std::vector<std::string>
channels;
683 static std::vector<int> numberOfEoS(
channels.size(), 0);
684 static std::vector<int> eosPeersCount(
channels.size(), 0);
692 auto outputChannels = ctx.services().get<
RawDeviceService>().spec().outputChannels;
701 auto channelConfigurationChecker = [device, deviceName, services = ctx.services()]() {
705 eosPeersCount.clear();
706 for (
auto& [channelName, _] : services.get<
RawDeviceService>().device()->GetChannels()) {
708 if (strncmp(channelName.c_str(), deviceName.c_str(), deviceName.size()) == 0) {
713 LOGP(detail,
"Injecting channel '{}' into DPL configuration", channel);
715 auto& channelPtr = services.get<
RawDeviceService>().device()->GetChannel(channel, 0);
718 .hasPendingEvents =
false,
720 .channel = &channelPtr,
725 numberOfEoS.resize(
channels.size(), 0);
726 eosPeersCount.resize(
channels.size(), 0);
734 static bool wasRunning =
false;
735 if (fair::mq::State{
state} == fair::mq::State::Running) {
738 if (fair::mq::State{
state} != fair::mq::State::Ready || !wasRunning) {
741 uv_update_time(deviceState.loop);
746 int64_t cleanupCount = deviceState.cleanupCount.load();
751 doDrain = device->NewStatePending() ==
false && deviceState.cleanupCount == cleanupCount;
752 fair::mq::Parts parts;
753 for (
size_t ci = 0; ci < deviceState.inputChannelInfos.size(); ++ci) {
754 auto& info = deviceState.inputChannelInfos[ci];
759 info.channel->Receive(parts, 10);
762 uv_run(deviceState.loop, UV_RUN_NOWAIT);
766 ctx.services().get<
CallbackService>().set<CallbackService::Id::Start>(channelConfigurationChecker);
767 if (ctx.options().get<std::string>(
"ready-state-policy") ==
"drain") {
768 LOG(info) <<
"Drain mode requested while in Ready state";
769 ctx.services().get<
CallbackService>().set<CallbackService::Id::DeviceStateChanged>(drainMessages);
772 static auto countEoS = [](fair::mq::Parts& inputs) ->
int {
774 for (
int msgidx = 0; msgidx < inputs.Size() / 2; ++msgidx) {
777 if (inputs.At(msgidx * 2).get() ==
nullptr) {
780 auto const sih = o2::header::get<SourceInfoHeader*>(inputs.At(msgidx * 2)->GetData());
789 auto dataHandler = [converter, doInjectMissingData, doPrintSizes,
790 outputRoutes = std::move(outputRoutes),
798 static std::string emptyChannel =
"";
799 for (
auto& route : outputRoutes) {
801 if (
DataSpecUtils::match(route.matcher, query) && ((timeslice % route.maxTimeslices) == route.timeslice)) {
802 return route.channel;
808 std::string
const& channel =
channels[ci];
810 int nEos = countEoS(inputs);
812 std::fill(numberOfEoS.begin(), numberOfEoS.end(), 0);
813 std::fill(eosPeersCount.begin(), eosPeersCount.end(), 0);
815 numberOfEoS[ci] += nEos;
816 if (numberOfEoS[ci]) {
817 eosPeersCount[ci] = std::max<int>(eosPeersCount[ci], device->GetNumberOfConnectedPeers(channel));
820 bool shouldstop =
false;
821 if (doInjectMissingData) {
822 injectMissingData(*device, inputs, outputRoutes, doInjectMissingData, doPrintSizes);
824 bool didSendParts = converter(timingInfo,
ref, inputs, channelRetriever, timesliceIndex->getOldestPossibleOutput().timeslice.value, shouldstop);
832 bool everyEoS = shouldstop;
833 if (!shouldstop && nEos) {
835 for (
unsigned int i = 0;
i < numberOfEoS.size();
i++) {
836 if (numberOfEoS[
i] < eosPeersCount[
i]) {
844 LOG(info) <<
"Received (on channel " << ci <<
") " << numberOfEoS[ci] <<
" end-of-stream from " << eosPeersCount[ci] <<
" peers, forwarding end-of-stream (shouldstop " << (
int)shouldstop <<
", nEos " << nEos <<
", newRun " << (
int)newRun <<
")";
846 for (
auto& info : deviceState->inputChannelInfos) {
849 std::fill(numberOfEoS.begin(), numberOfEoS.end(), 0);
850 std::fill(eosPeersCount.begin(), eosPeersCount.end(), 0);
851 control->endOfStream();
858 static size_t currentRunNumber = -1;
859 static bool inStopTransition =
false;
862 if (limiter.
check(ctx, std::stoi(device->fConfig->GetValue<std::string>(
"timeframes-rate-limit")), minSHM)) {
863 inStopTransition =
true;
866 bool didSendParts =
false;
867 for (
size_t ci = 0; ci <
channels.size(); ++ci) {
869 if (ci > 0 && ci % 10 == 0) {
872 std::string
const& channel =
channels[ci];
873 int waitTime =
channels.size() == 1 ? -1 : 1;
875 while (maxRead-- > 0) {
876 fair::mq::Parts parts;
877 auto res = device->Receive(parts, channel, 0, waitTime);
878 if (
res == (
size_t)fair::mq::TransferCode::error) {
879 LOGP(error,
"Error while receiving on channel {}", channel);
882 unsigned int nReceived = parts.Size();
883 if (nReceived != 0) {
884 auto const dh = o2::header::get<DataHeader*>(parts.At(0)->GetData());
885 auto& timingInfo = ctx.services().get<
TimingInfo>();
887 if (currentRunNumber != -1 && dh->runNumber != 0 && dh->runNumber != currentRunNumber) {
889 inStopTransition =
false;
891 if (currentRunNumber == -1 || dh->runNumber != 0) {
894 timingInfo.runNumber = dh->runNumber;
895 timingInfo.firstTForbit = dh->firstTForbit;
896 timingInfo.tfCounter = dh->tfCounter;
898 auto const dph = o2::header::get<DataProcessingHeader*>(parts.At(0)->GetData());
899 if (dph !=
nullptr) {
900 timingInfo.timeslice = dph->startTime;
901 timingInfo.creation = dph->creation;
903 if (!inStopTransition) {
904 didSendParts |= dataHandler(ctx.services(), timingInfo, parts, 0, ci, newRun);
907 ctx.services().get<o2::monitoring::Monitoring>().send(o2::monitoring::Metric{(uint64_t)timingInfo.tfCounter,
"df-sent"}.addTag(o2::monitoring::tags::Key::Subsystem, o2::monitoring::tags::Value::DPL));
910 if (nReceived == 0 ||
channels.size() == 1) {
923 decongestion.nextEnumerationTimeslice -= 1;
929 const char* d = strdup(((std::string(defaultChannelConfig).find(
"name=") == std::string::npos ? (std::string(
"name=") +
name +
",") :
"") + std::string(defaultChannelConfig)).c_str());
944 const char* defaultChannelConfig)
955 auto channelConfig = options.
get<std::string>(
"channel-config");
956 std::regex
r{R
"(name=([^,]*))"};
957 std::vector<std::string> values{std::sregex_token_iterator{std::begin(channelConfig), std::end(channelConfig), r, 1},
958 std::sregex_token_iterator{}};
960 throw std::runtime_error(
"failed to extract channel name from channel configuration parameter '" + channelConfig +
"'");
962 std::string outputChannelName =
values[0];
964 auto* device = rds.
device();
968 auto channelConfigurationChecker = [inputSpecs = std::move(inputSpecs), device, outputChannelName]() {
969 LOG(info) <<
"checking channel configuration";
970 if (device->GetChannels().count(outputChannelName) == 0) {
971 throw std::runtime_error(
"no corresponding output channel found for input '" + outputChannelName +
"'");
975 auto lastDataProcessingHeader = std::make_shared<DataProcessingHeader>(0, 0);
977 auto& spec =
const_cast<DeviceSpec&
>(deviceSpec);
979 for (
auto const& inputSpec : inputSpecs) {
985 .matcher = inputSpec,
986 .channel = outputChannelName,
988 spec.forwards.emplace_back(route);
991 auto forwardEos = [device, lastDataProcessingHeader, outputChannelName](
EndOfStreamContext&) {
995 for (
auto& channelInfo : device->GetChannels()) {
996 auto& channelName = channelInfo.first;
997 if (channelName != outputChannelName) {
1001 uint32_t runNumber = 0;
1003 runNumber = strtoul(device->fConfig->GetProperty<std::string>(
"runNumber",
"").c_str(),
nullptr, 10);
1018 auto channelAlloc = o2::pmr::getTransportAllocator(channelInfo.second[0].Transport());
1020 fair::mq::Parts out;
1021 out.AddPart(std::move(headerMessage));
1023 out.AddPart(device->NewMessageFor(channelName, 0, 0));
1030 for (
size_t ii = 0; ii != inputs.
size(); ++ii) {
1031 for (
size_t pi = 0; pi < inputs.
getNofParts(ii); ++pi) {
1032 auto part = inputs.
getByPos(ii, pi);
1033 const auto* dph = o2::header::get<DataProcessingHeader*>(part.header);
1036 lastDataProcessingHeader->startTime = dph->startTime;
1037 lastDataProcessingHeader->duration = dph->duration;
1038 lastDataProcessingHeader->creation = dph->creation;
1044 const char* d = strdup(((std::string(defaultChannelConfig).find(
"name=") == std::string::npos ? (std::string(
"name=") +
name +
",") :
"") + std::string(defaultChannelConfig)).c_str());
1053 Inputs const& inputSpecs,
1054 const char* defaultChannelConfig,
1061 spec.
inputs = inputSpecs;
1064 auto device = rds.
device();
1069 auto channelNames = std::make_shared<std::vector<std::string>>();
1070 auto channelConfigurationInitializer = [&proxy, inputSpecs = std::move(inputSpecs), device, channelSelector, &deviceSpec, channelNames]() {
1071 channelNames->clear();
1072 auto& mutableDeviceSpec =
const_cast<DeviceSpec&
>(deviceSpec);
1073 for (
auto const& spec : inputSpecs) {
1074 auto channel = channelSelector(spec, device->GetChannels());
1075 if (device->GetChannels().count(channel) == 0) {
1076 throw std::runtime_error(
"no corresponding output channel found for input '" + channel +
"'");
1089 mutableDeviceSpec.forwards.emplace_back(route);
1091 channelNames->emplace_back(std::move(channel));
1093 proxy.
bind(mutableDeviceSpec.outputs, mutableDeviceSpec.inputs, mutableDeviceSpec.forwards, *device);
1096 auto channelConfigurationDisposer = [&deviceSpec]() {
1097 auto& mutableDeviceSpec =
const_cast<DeviceSpec&
>(deviceSpec);
1098 mutableDeviceSpec.
forwards.clear();
1103 auto lastDataProcessingHeader = std::make_shared<DataProcessingHeader>(0, 0);
1104 auto forwardEos = [device, lastDataProcessingHeader, channelNames](
EndOfStreamContext&) {
1108 for (
auto& channelInfo : device->GetChannels()) {
1109 auto& channelName = channelInfo.first;
1110 auto checkChannel = [channelNames = std::move(*channelNames)](std::string
const&
name) ->
bool {
1111 for (
auto const&
n : channelNames) {
1118 if (!checkChannel(channelName)) {
1121 uint32_t runNumber = 0;
1123 runNumber = strtoul(device->fConfig->GetProperty<std::string>(
"runNumber",
"").c_str(),
nullptr, 10);
1138 auto channelAlloc = o2::pmr::getTransportAllocator(channelInfo.second[0].Transport());
1140 fair::mq::Parts out;
1141 out.AddPart(std::move(headerMessage));
1143 out.AddPart(device->NewMessageFor(channelName, 0, 0));
1144 LOGP(detail,
"Forwarding EoS to {}", channelName);
1154 for (
size_t ii = 0; ii != inputs.
size(); ++ii) {
1155 for (
size_t pi = 0; pi < inputs.
getNofParts(ii); ++pi) {
1156 auto part = inputs.
getByPos(ii, pi);
1157 const auto* dph = o2::header::get<DataProcessingHeader*>(part.header);
1160 lastDataProcessingHeader->startTime = dph->startTime;
1161 lastDataProcessingHeader->duration = dph->duration;
1162 lastDataProcessingHeader->creation = dph->creation;
1168 const char* d = strdup(((std::string(defaultChannelConfig).find(
"name=") == std::string::npos ? (std::string(
"name=") +
name +
",") :
"") + std::string(defaultChannelConfig)).c_str());