|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +#include "Framework/AlgorithmSpec.h" |
| 13 | +#include "Framework/DataProcessorSpec.h" |
| 14 | +#include "Framework/DataRefUtils.h" |
| 15 | +#include "Framework/ExternalFairMQDeviceProxy.h" |
| 16 | +#include "Framework/ControlService.h" |
| 17 | +#include "Framework/CallbackService.h" |
| 18 | +#include "Framework/EndOfStreamContext.h" |
| 19 | +#include "Framework/RawDeviceService.h" |
| 20 | +#include "Framework/DeviceSpec.h" |
| 21 | +#include "Framework/CompletionPolicy.h" |
| 22 | +#include "Framework/CompletionPolicyHelpers.h" |
| 23 | +#include "Framework/InputRecordWalker.h" |
| 24 | +#include "Framework/Logger.h" |
| 25 | +#include "Headers/DataHeader.h" |
| 26 | +#include "Headers/Stack.h" |
| 27 | +#include "MemoryResources/MemoryResources.h" |
| 28 | +#include "fairmq/FairMQDevice.h" |
| 29 | +#include <memory> |
| 30 | +#include <random> |
| 31 | + |
| 32 | +using namespace o2::framework; |
| 33 | +using DataHeader = o2::header::DataHeader; |
| 34 | +using Stack = o2::header::Stack; |
| 35 | + |
| 36 | +// we need to specify customizations before including Framework/runDataProcessing |
| 37 | +// customize consumer to process immediately what comes in |
| 38 | +void customize(std::vector<o2::framework::CompletionPolicy>& policies) |
| 39 | +{ |
| 40 | + // we customize the pipeline processors to consume data as it comes |
| 41 | + using CompletionPolicy = o2::framework::CompletionPolicy; |
| 42 | + using CompletionPolicyHelpers = o2::framework::CompletionPolicyHelpers; |
| 43 | + policies.push_back(CompletionPolicyHelpers::defineByName("consumer", CompletionPolicy::CompletionOp::Consume)); |
| 44 | + policies.push_back(CompletionPolicyHelpers::defineByName("spectator", CompletionPolicy::CompletionOp::Consume)); |
| 45 | +} |
| 46 | + |
| 47 | +#include "Framework/runDataProcessing.h" |
| 48 | + |
| 49 | +#define ASSERT_ERROR(condition) \ |
| 50 | + if ((condition) == false) { \ |
| 51 | + LOG(FATAL) << R"(Test condition ")" #condition R"(" failed)"; \ |
| 52 | + } |
| 53 | + |
| 54 | +namespace test |
| 55 | +{ |
| 56 | +// a header with the information expected in the payload |
| 57 | +// will be sent on the header stack |
| 58 | +struct SequenceDesc : public o2::header::BaseHeader { |
| 59 | + //static data for this header type/version |
| 60 | + static constexpr uint32_t sVersion{1}; |
| 61 | + static constexpr o2::header::HeaderType sHeaderType{o2::header::String2<uint64_t>("SequDesc")}; |
| 62 | + static constexpr o2::header::SerializationMethod sSerializationMethod{o2::header::gSerializationMethodNone}; |
| 63 | + |
| 64 | + size_t iteration = 0; |
| 65 | + size_t nPayloads = 0; |
| 66 | + size_t initialValue = 0; |
| 67 | + |
| 68 | + constexpr SequenceDesc(size_t i, size_t n, size_t v) |
| 69 | + : BaseHeader(sizeof(SequenceDesc), sHeaderType, sSerializationMethod, sVersion), iteration(i), nPayloads(n), initialValue(v) |
| 70 | + { |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +} // namespace test |
| 75 | + |
| 76 | +std::vector<DataProcessorSpec> defineDataProcessing(ConfigContext const& config) |
| 77 | +{ |
| 78 | + struct Attributes { |
| 79 | + using EngineT = std::mt19937; |
| 80 | + using DistributionT = std::uniform_int_distribution<>; |
| 81 | + size_t nRolls = 2; |
| 82 | + EngineT gen; |
| 83 | + DistributionT distrib; |
| 84 | + size_t iteration = 0; |
| 85 | + std::string channelName; |
| 86 | + }; |
| 87 | + |
| 88 | + std::random_device rd; |
| 89 | + auto attributes = std::make_shared<Attributes>(); |
| 90 | + attributes->nRolls = 4; |
| 91 | + attributes->gen = std::mt19937(rd()); |
| 92 | + attributes->distrib = std::uniform_int_distribution<>{1, 20}; |
| 93 | + |
| 94 | + std::vector<DataProcessorSpec> workflow; |
| 95 | + ////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 96 | + // a producer process steered by a timer |
| 97 | + // |
| 98 | + // the compute callback of the producer |
| 99 | + // Producing three types of output: |
| 100 | + // 1. via default DPL Allocator |
| 101 | + // 2. multiple payloads in split-payloads format (header-payload pairs) |
| 102 | + // 3. multiple payload sequence with one header |
| 103 | + auto producerCallback = [attributes](InputRecord& inputs, DataAllocator& outputs, ControlService& control, RawDeviceService& rds) { |
| 104 | + auto& counter = attributes->iteration; |
| 105 | + auto& channelName = attributes->channelName; |
| 106 | + auto& nRolls = attributes->nRolls; |
| 107 | + outputs.make<int>(OutputRef{"allocator", 0}) = counter; |
| 108 | + |
| 109 | + if (channelName.empty()) { |
| 110 | + OutputSpec const query{"TST", "PAIR", 0}; |
| 111 | + auto outputRoutes = rds.spec().outputs; |
| 112 | + for (auto& route : outputRoutes) { |
| 113 | + if (DataSpecUtils::match(route.matcher, query)) { |
| 114 | + channelName = route.channel; |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + ASSERT_ERROR(channelName.length() > 0); |
| 119 | + } |
| 120 | + FairMQDevice& device = *(rds.device()); |
| 121 | + auto transport = device.GetChannel(channelName, 0).Transport(); |
| 122 | + auto channelAlloc = o2::pmr::getTransportAllocator(transport); |
| 123 | + |
| 124 | + auto const* dph = DataRefUtils::getHeader<DataProcessingHeader*>(inputs.get("timer")); |
| 125 | + test::SequenceDesc sd{counter, 0, 0}; |
| 126 | + |
| 127 | + FairMQParts messages; |
| 128 | + auto createSequence = [&dph, &sd, &attributes, &transport, &channelAlloc, &messages](size_t nPayloads, DataHeader dh) -> void { |
| 129 | + // one header with index set to the number of split parts indicates sequence |
| 130 | + // of payloads without additional headers |
| 131 | + dh.payloadSize = sizeof(size_t); |
| 132 | + dh.payloadSerializationMethod = o2::header::gSerializationMethodNone; |
| 133 | + dh.splitPayloadIndex = nPayloads; |
| 134 | + dh.splitPayloadParts = nPayloads; |
| 135 | + sd.nPayloads = nPayloads; |
| 136 | + sd.initialValue = attributes->distrib(attributes->gen); |
| 137 | + FairMQMessagePtr header = o2::pmr::getMessage(Stack{channelAlloc, dh, *dph, sd}); |
| 138 | + messages.AddPart(std::move(header)); |
| 139 | + |
| 140 | + for (size_t i = 0; i < nPayloads; ++i) { |
| 141 | + FairMQMessagePtr payload = transport->CreateMessage(dh.payloadSize); |
| 142 | + *(reinterpret_cast<size_t*>(payload->GetData())) = sd.initialValue + i; |
| 143 | + messages.AddPart(std::move(payload)); |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + auto createPairs = [&dph, &transport, &channelAlloc, &messages](size_t nPayloads, DataHeader dh) -> void { |
| 148 | + // one header with index set to the number of split parts indicates sequence |
| 149 | + // of payloads without additional headers |
| 150 | + dh.payloadSize = sizeof(size_t); |
| 151 | + dh.payloadSerializationMethod = o2::header::gSerializationMethodNone; |
| 152 | + dh.splitPayloadIndex = 0; |
| 153 | + dh.splitPayloadParts = nPayloads; |
| 154 | + for (size_t i = 0; i < nPayloads; ++i) { |
| 155 | + dh.splitPayloadIndex = i; |
| 156 | + FairMQMessagePtr header = o2::pmr::getMessage(Stack{channelAlloc, dh, *dph}); |
| 157 | + messages.AddPart(std::move(header)); |
| 158 | + FairMQMessagePtr payload = transport->CreateMessage(dh.payloadSize); |
| 159 | + *(reinterpret_cast<size_t*>(payload->GetData())) = i; |
| 160 | + messages.AddPart(std::move(payload)); |
| 161 | + } |
| 162 | + }; |
| 163 | + |
| 164 | + //createSequence(attributes->distrib(attributes->gen), DataHeader{"SEQUENCE", "TST", 0}); |
| 165 | + createPairs(counter + 1, DataHeader{"PAIR", "TST", 0}); |
| 166 | + |
| 167 | + // using utility from ExternalFairMQDeviceProxy |
| 168 | + sendOnChannel(device, messages, channelName); |
| 169 | + |
| 170 | + if (++(counter) >= nRolls) { |
| 171 | + // send the end of stream signal, this is transferred by the proxies |
| 172 | + // and allows to properly terminate downstream devices |
| 173 | + control.endOfStream(); |
| 174 | + control.readyToQuit(QuitRequest::Me); |
| 175 | + } |
| 176 | + }; |
| 177 | + |
| 178 | + workflow.emplace_back(DataProcessorSpec{"producer", |
| 179 | + {InputSpec{"timer", "TST", "TIMER", 0, Lifetime::Timer}}, |
| 180 | + {OutputSpec{{"pair"}, "TST", "PAIR", 0, Lifetime::Timeframe}, |
| 181 | + OutputSpec{{"allocator"}, "TST", "ALLOCATOR", 0, Lifetime::Timeframe}}, |
| 182 | + AlgorithmSpec{adaptStateless(producerCallback)}, |
| 183 | + {ConfigParamSpec{"period-timer", VariantType::Int, 100000, {"period of timer"}}}}); |
| 184 | + |
| 185 | + ////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 186 | + // consumer utils used by two processes |
| 187 | + // |
| 188 | + using ConsumerCounters = std::map<std::string, int>; |
| 189 | + auto inputChecker = [](InputRecord& inputs, ConsumerCounters& counters) { |
| 190 | + size_t nSequencePayloads = 0; |
| 191 | + size_t expectedPayloads = 0; |
| 192 | + size_t iteration = 0; |
| 193 | + ConsumerCounters active; |
| 194 | + for (auto const& ref : InputRecordWalker(inputs)) { |
| 195 | + if (!inputs.isValid(ref.spec->binding)) { |
| 196 | + continue; |
| 197 | + } |
| 198 | + auto const* dh = DataRefUtils::getHeader<DataHeader*>(ref); |
| 199 | + ASSERT_ERROR(dh != nullptr) |
| 200 | + if (!dh) { |
| 201 | + continue; |
| 202 | + } |
| 203 | + active[ref.spec->binding] = 1; |
| 204 | + if (ref.spec->binding == "sequencein") { |
| 205 | + auto const* sd = DataRefUtils::getHeader<test::SequenceDesc*>(ref); |
| 206 | + ASSERT_ERROR(sd != nullptr); |
| 207 | + if (!sd) { |
| 208 | + continue; |
| 209 | + } |
| 210 | + iteration = sd->iteration; |
| 211 | + if (expectedPayloads == 0) { |
| 212 | + expectedPayloads = sd->nPayloads; |
| 213 | + } else { |
| 214 | + ASSERT_ERROR(expectedPayloads == sd->nPayloads); |
| 215 | + } |
| 216 | + ASSERT_ERROR(*reinterpret_cast<size_t const*>(ref.payload) == sd->initialValue + nSequencePayloads); |
| 217 | + ++nSequencePayloads; |
| 218 | + } |
| 219 | + //LOG(INFO) << "input " << ref.spec->binding << " has data {" << dh->dataOrigin.as<std::string>() << "/" << dh->dataDescription.as<std::string>() << "/" << dh->subSpecification << "}: " << *reinterpret_cast<size_t const*>(ref.payload); |
| 220 | + } |
| 221 | + for (auto const& [channel, count] : active) { |
| 222 | + ++counters[channel]; |
| 223 | + } |
| 224 | + }; |
| 225 | + |
| 226 | + auto createCounters = [](RawDeviceService& rds) -> std::shared_ptr<ConsumerCounters> { |
| 227 | + auto counters = std::make_shared<ConsumerCounters>(); |
| 228 | + ConsumerCounters& c = *counters; |
| 229 | + for (auto const& channelSpec : rds.spec().inputChannels) { |
| 230 | + // we would need the input spec here, while in the device spec we have the attributes |
| 231 | + // of the FairMQ Channels |
| 232 | + //(*counters)[channelSpec.name] = 0; |
| 233 | + } |
| 234 | + return counters; |
| 235 | + }; |
| 236 | + |
| 237 | + auto checkCounters = [nRolls = attributes->nRolls](std::shared_ptr<ConsumerCounters> const& counters) -> bool { |
| 238 | + bool sane = true; |
| 239 | + for (auto const& [channel, count] : *counters) { |
| 240 | + if (count != nRolls) { |
| 241 | + LOG(FATAL) << "inconsistent event count on input '" << channel << "': " << count << ", expected " << nRolls; |
| 242 | + sane = false; |
| 243 | + } |
| 244 | + } |
| 245 | + return sane; |
| 246 | + }; |
| 247 | + |
| 248 | + ////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 249 | + // the consumer process connects to the producer |
| 250 | + // |
| 251 | + auto consumerInit = [createCounters, checkCounters, inputChecker](RawDeviceService& rds, CallbackService& callbacks) { |
| 252 | + auto counters = createCounters(rds); |
| 253 | + callbacks.set(CallbackService::Id::Stop, [counters, checkCounters]() { |
| 254 | + ASSERT_ERROR(checkCounters(counters)); |
| 255 | + }); |
| 256 | + callbacks.set(CallbackService::Id::EndOfStream, [counters, checkCounters](EndOfStreamContext& context) { |
| 257 | + ASSERT_ERROR(checkCounters(counters)); |
| 258 | + context.services().get<ControlService>().readyToQuit(QuitRequest::Me); |
| 259 | + }); |
| 260 | + |
| 261 | + auto processing = [inputChecker, counters](InputRecord& inputs) { |
| 262 | + inputChecker(inputs, *counters); |
| 263 | + }; |
| 264 | + |
| 265 | + return adaptStateless(processing); |
| 266 | + }; |
| 267 | + |
| 268 | + workflow.emplace_back(DataProcessorSpec{"consumer", |
| 269 | + {InputSpec{"pairin", "TST", "PAIR", 0, Lifetime::Timeframe}, |
| 270 | + InputSpec{"dpldefault", "TST", "ALLOCATOR", 0, Lifetime::Timeframe}}, |
| 271 | + {}, |
| 272 | + AlgorithmSpec{adaptStateful(consumerInit)}}); |
| 273 | + |
| 274 | + ////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 275 | + // spectator process which should get the forwarded data |
| 276 | + // |
| 277 | + workflow.emplace_back(DataProcessorSpec{"spectator", |
| 278 | + {InputSpec{"pairin", "TST", "PAIR", 0, Lifetime::Timeframe}, |
| 279 | + InputSpec{"dpldefault", "TST", "ALLOCATOR", 0, Lifetime::Timeframe}}, |
| 280 | + {}, |
| 281 | + AlgorithmSpec{adaptStateful(consumerInit)}}); |
| 282 | + |
| 283 | + return workflow; |
| 284 | +} |
0 commit comments