Project
Loading...
Searching...
No Matches
WorkflowHelpers.cxx
Go to the documentation of this file.
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#include "WorkflowHelpers.h"
28#include "Framework/Signpost.h"
30
31#include "Framework/Variant.h"
32#include "Headers/DataHeader.h"
33#include <algorithm>
34#include <list>
35#include <set>
36#include <utility>
37#include <vector>
38#include <climits>
39#include <numeric>
40
41O2_DECLARE_DYNAMIC_LOG(workflow_helpers);
42
43namespace o2::framework
44{
45std::ostream& operator<<(std::ostream& out, TopoIndexInfo const& info)
46{
47 out << "(" << info.index << ", " << info.layer << ")";
48 return out;
49}
50
51std::vector<TopoIndexInfo>
53 int const* edgeIn,
54 int const* edgeOut,
55 size_t byteStride,
56 size_t edgesCount)
57{
58 size_t stride = byteStride / sizeof(int);
59 using EdgeIndex = int;
60 // Create the index which will be returned.
61 std::vector<TopoIndexInfo> index(nodeCount);
62 for (auto wi = 0; static_cast<size_t>(wi) < nodeCount; ++wi) {
63 index[wi] = {wi, 0};
64 }
65 std::vector<EdgeIndex> remainingEdgesIndex(edgesCount);
66 for (EdgeIndex ei = 0; static_cast<size_t>(ei) < edgesCount; ++ei) {
67 remainingEdgesIndex[ei] = ei;
68 }
69
70 // Create a vector where at each position we have true
71 // if the vector has dependencies, false otherwise
72 std::vector<bool> nodeDeps(nodeCount, false);
73 for (EdgeIndex ei = 0; static_cast<size_t>(ei) < edgesCount; ++ei) {
74 nodeDeps[*(edgeOut + ei * stride)] = true;
75 }
76
77 // We start with all those which do not have any dependencies
78 // They are layer 0.
79 std::list<TopoIndexInfo> L;
80 for (auto ii = 0; static_cast<size_t>(ii) < index.size(); ++ii) {
81 if (nodeDeps[ii] == false) {
82 L.push_back({ii, 0});
83 }
84 }
85
86 // The final result.
87 std::vector<TopoIndexInfo> S;
88 // The set of vertices which can be reached by the current node
89 std::set<TopoIndexInfo> nextVertex;
90 // The set of edges which are not related to the current node.
91 std::vector<EdgeIndex> nextEdges;
92 while (!L.empty()) {
93 auto node = L.front();
94 S.push_back(node);
95 L.pop_front();
96 nextVertex.clear();
97 nextEdges.clear();
98
99 // After this, nextVertex will contain all the vertices
100 // which have the current node as incoming.
101 // nextEdges will contain all the edges which are not related
102 // to the current node.
103 for (auto const& ei : remainingEdgesIndex) {
104 if (*(edgeIn + ei * stride) == node.index) {
105 nextVertex.insert({*(edgeOut + ei * stride), node.layer + 1});
106 } else {
107 nextEdges.push_back(ei);
108 }
109 }
110 remainingEdgesIndex.swap(nextEdges);
111
112 // Of all the vertices which have node as incoming,
113 // check if there is any other incoming node.
114 std::set<TopoIndexInfo> hasPredecessors;
115 for (auto const& ei : remainingEdgesIndex) {
116 for (auto& m : nextVertex) {
117 if (m.index == *(edgeOut + ei * stride)) {
118 hasPredecessors.insert({m.index, m.layer});
119 }
120 }
121 }
122 std::vector<TopoIndexInfo> withPredecessor;
123 std::set_difference(nextVertex.begin(), nextVertex.end(),
124 hasPredecessors.begin(), hasPredecessors.end(),
125 std::back_inserter(withPredecessor));
126 std::copy(withPredecessor.begin(), withPredecessor.end(), std::back_inserter(L));
127 }
128 return S;
129}
130
131// get the default value for condition-backend
133{
134 static bool explicitBackend = getenv("DPL_CONDITION_BACKEND");
135 static DeploymentMode deploymentMode = DefaultsHelpers::deploymentMode();
136 if (explicitBackend) {
137 return getenv("DPL_CONDITION_BACKEND");
138 } else if (deploymentMode == DeploymentMode::OnlineDDS || deploymentMode == DeploymentMode::OnlineECS) {
139 return "http://o2-ccdb.internal";
140 } else {
141 return "http://alice-ccdb.cern.ch";
142 }
143}
144
145// get the default value for condition query rate
147{
148 return getenv("DPL_CONDITION_QUERY_RATE") ? std::stoi(getenv("DPL_CONDITION_QUERY_RATE")) : 0;
149}
150
151// get the default value for condition query rate multiplier
153{
154 return getenv("DPL_CONDITION_QUERY_RATE_MULTIPLIER") ? std::stoi(getenv("DPL_CONDITION_QUERY_RATE_MULTIPLIER")) : 1;
155}
156
158{
159 int rateLimitingIPCID = std::stoi(ctx.options().get<std::string>("timeframes-rate-limit-ipcid"));
160 DataProcessorSpec ccdbBackend{
161 .name = "internal-dpl-ccdb-backend",
162 .outputs = {},
163 .options = {{"condition-backend", VariantType::String, defaultConditionBackend(), {"URL for CCDB"}},
164 {"condition-timestamp", VariantType::Int64, 0ll, {"Force timestamp for CCDB lookup"}},
165 {"condition-not-before", VariantType::Int64, 0ll, {"do not fetch from CCDB objects created before provide timestamp"}},
166 {"condition-not-after", VariantType::Int64, 3385078236000ll, {"do not fetch from CCDB objects created after the timestamp"}},
167 {"condition-remap", VariantType::String, "", {"remap condition path in CCDB based on the provided string."}},
168 {"condition-tf-per-query", VariantType::Int, defaultConditionQueryRate(), {"check condition validity per requested number of TFs, fetch only once if <=0"}},
169 {"condition-tf-per-query-multiplier", VariantType::Int, defaultConditionQueryRateMultiplier(), {"check conditions once per this amount of nominal checks (>0) or on module of TFcounter (<0)"}},
170 {"condition-use-slice-for-prescaling", VariantType::Int, 0, {"use TFslice instead of TFcounter to control validation frequency. If > query rate, do not allow TFCounter excursion exceeding it"}},
171 {"condition-time-tolerance", VariantType::Int64, 5000ll, {"prefer creation time if its difference to orbit-derived time exceeds threshold (ms), impose if <0"}},
172 {"orbit-offset-enumeration", VariantType::Int64, 0ll, {"initial value for the orbit"}},
173 {"orbit-multiplier-enumeration", VariantType::Int64, 0ll, {"multiplier to get the orbit from the counter"}},
174 {"start-value-enumeration", VariantType::Int64, 0ll, {"initial value for the enumeration"}},
175 {"end-value-enumeration", VariantType::Int64, -1ll, {"final value for the enumeration"}},
176 {"step-value-enumeration", VariantType::Int64, 1ll, {"step between one value and the other"}}},
177 };
178 DataProcessorSpec analysisCCDBBackend{
179 .name = "internal-dpl-aod-ccdb",
180 .inputs = {},
181 .outputs = {},
182 .algorithm = AlgorithmSpec::dummyAlgorithm(),
183 .options = {{"condition-backend", VariantType::String, defaultConditionBackend(), {"URL for CCDB"}},
184 {"condition-not-before", VariantType::Int64, 0ll, {"do not fetch from CCDB objects created before provide timestamp"}},
185 {"condition-not-after", VariantType::Int64, 3385078236000ll, {"do not fetch from CCDB objects created after the timestamp"}},
186 {"condition-remap", VariantType::String, "", {"remap condition path in CCDB based on the provided string."}},
187 {"condition-tf-per-query", VariantType::Int, defaultConditionQueryRate(), {"check condition validity per requested number of TFs, fetch only once if <=0"}},
188 {"condition-tf-per-query-multiplier", VariantType::Int, defaultConditionQueryRateMultiplier(), {"check conditions once per this amount of nominal checks (>0) or on module of TFcounter (<0)"}},
189 {"condition-use-slice-for-prescaling", VariantType::Int, 0, {"use TFslice instead of TFcounter to control validation frequency. If > query rate, do not allow TFCounter excursion exceeding it"}},
190 {"condition-time-tolerance", VariantType::Int64, 5000ll, {"prefer creation time if its difference to orbit-derived time exceeds threshold (ms), impose if <0"}},
191 {"start-value-enumeration", VariantType::Int64, 0ll, {"initial value for the enumeration"}},
192 {"end-value-enumeration", VariantType::Int64, -1ll, {"final value for the enumeration"}},
193 {"step-value-enumeration", VariantType::Int64, 1ll, {"step between one value and the other"}}}};
194 DataProcessorSpec transientStore{"internal-dpl-transient-store",
195 {},
196 {},
198 DataProcessorSpec qaStore{"internal-dpl-qa-store",
199 {},
200 {},
202 DataProcessorSpec timer{"internal-dpl-clock",
203 {},
204 {},
206
207 // In case InputSpec of origin AOD are
208 // requested but not available as part of the workflow,
209 // we insert in the configuration something which
210 // reads them from file.
211 //
212 // FIXME: source branch is DataOrigin, for the moment. We should
213 // make it configurable via ConfigParamsOptions
214 auto aodLifetime = Lifetime::Enumeration;
215
216 DataProcessorSpec aodReader{
217 .name = "internal-dpl-aod-reader",
218 .inputs = {InputSpec{"enumeration",
219 "DPL",
220 "ENUM",
221 static_cast<DataAllocator::SubSpecificationType>(compile_time_hash("internal-dpl-aod-reader")),
222 aodLifetime}},
223 .algorithm = AlgorithmSpec::dummyAlgorithm(),
224 .options = {ConfigParamSpec{"aod-file-private", VariantType::String, ctx.options().get<std::string>("aod-file"), {"AOD file"}},
225 ConfigParamSpec{"aod-max-io-rate", VariantType::Float, 0.f, {"Maximum I/O rate in MB/s"}},
226 ConfigParamSpec{"aod-reader-backend", VariantType::String, "root", {"AOD reader: root, native (without ROOT), or auto (native, falling back to root per file)"}},
227 ConfigParamSpec{"aod-reader-threads", VariantType::Int, 1, {"Decompression threads of the native AOD reader"}},
228 ConfigParamSpec{"aod-reader-prefetch", VariantType::Int, 1, {"DFs the native AOD reader prepares ahead for remote files: 0 (off) or 1"}},
229 ConfigParamSpec{"aod-reader-local-io", VariantType::String, "pread", {"How the native AOD reader reads local files: pread or mmap"}},
230 ConfigParamSpec{"aod-reader-buffer-mb", VariantType::Int, -1, {"Most MB the native AOD reader holds in read buffers: 0 for no limit, -1 for 16 MB for local files and 64 MB for remote ones"}},
231 ConfigParamSpec{"aod-reader-json", VariantType::String, {"json configuration file"}},
232 ConfigParamSpec{"time-limit", VariantType::Int64, 0ll, {"Maximum run time limit in seconds"}},
233 ConfigParamSpec{"orbit-offset-enumeration", VariantType::Int64, 0ll, {"initial value for the orbit"}},
234 ConfigParamSpec{"orbit-multiplier-enumeration", VariantType::Int64, 0ll, {"multiplier to get the orbit from the counter"}},
235 ConfigParamSpec{"start-value-enumeration", VariantType::Int64, 0ll, {"initial value for the enumeration"}},
236 ConfigParamSpec{"end-value-enumeration", VariantType::Int64, -1ll, {"final value for the enumeration"}},
237 ConfigParamSpec{"step-value-enumeration", VariantType::Int64, 1ll, {"step between one value and the other"}}},
238 .requiredServices = CommonServices::defaultServices("O2FrameworkAnalysisSupport:RunSummary")};
239
240 ctx.services().registerService(ServiceRegistryHelpers::handleForService<DanglingEdgesContext>(new DanglingEdgesContext));
241 auto& dec = ctx.services().get<DanglingEdgesContext>();
242
243 std::vector<InputSpec> requestedCCDBs;
244 std::vector<OutputSpec> providedCCDBs;
245
246 for (size_t wi = 0; wi < workflow.size(); ++wi) {
247 auto& processor = workflow[wi];
248 auto name = processor.name;
249 uint32_t hash = runtime_hash(name.c_str());
250 dec.outTskMap.push_back({hash, name});
251
252 std::string prefix = "internal-dpl-";
253 if (processor.inputs.empty() && processor.name.compare(0, prefix.size(), prefix) != 0) {
254 processor.inputs.push_back(InputSpec{"enumeration", "DPL", "ENUM", static_cast<DataAllocator::SubSpecificationType>(runtime_hash(processor.name.c_str())), Lifetime::Enumeration});
255 ConfigParamsHelper::addOptionIfMissing(processor.options, ConfigParamSpec{"orbit-offset-enumeration", VariantType::Int64, 0ll, {"1st injected orbit"}});
256 ConfigParamsHelper::addOptionIfMissing(processor.options, ConfigParamSpec{"orbit-multiplier-enumeration", VariantType::Int64, 0ll, {"orbits/TForbit"}});
257 processor.options.push_back(ConfigParamSpec{"start-value-enumeration", VariantType::Int64, 0ll, {"initial value for the enumeration"}});
258 processor.options.push_back(ConfigParamSpec{"end-value-enumeration", VariantType::Int64, -1ll, {"final value for the enumeration"}});
259 processor.options.push_back(ConfigParamSpec{"step-value-enumeration", VariantType::Int64, 1ll, {"step between one value and the other"}});
260 }
261 bool hasTimeframeInputs = std::ranges::any_of(processor.inputs, [](auto const& input) { return input.lifetime == Lifetime::Timeframe; });
262 bool hasTimeframeOutputs = std::ranges::any_of(processor.outputs, [](auto const& output) { return output.lifetime == Lifetime::Timeframe; });
263
264 // A timeframeSink consumes timeframes without creating new
265 // timeframe data.
266 bool timeframeSink = hasTimeframeInputs && !hasTimeframeOutputs;
267 if (rateLimitingIPCID != -1) {
268 if (timeframeSink && processor.name.find("internal-dpl-injected-dummy-sink") == std::string::npos) {
269 O2_SIGNPOST_ID_GENERATE(sid, workflow_helpers);
270 bool hasMatch = false;
271 ConcreteDataMatcher summaryMatcher = ConcreteDataMatcher{"DPL", "SUMMARY", static_cast<DataAllocator::SubSpecificationType>(hash)};
272 auto summaryOutput = std::ranges::find_if(processor.outputs, [&summaryMatcher](auto const& output) { return DataSpecUtils::match(output, summaryMatcher); });
273 if (summaryOutput != processor.outputs.end()) {
274 O2_SIGNPOST_EVENT_EMIT(workflow_helpers, sid, "output enumeration", "%{public}s already there in %{public}s",
275 DataSpecUtils::describe(*summaryOutput).c_str(), processor.name.c_str());
276 hasMatch = true;
277 }
278
279 if (!hasMatch) {
280 O2_SIGNPOST_EVENT_EMIT(workflow_helpers, sid, "output enumeration", "Adding DPL/SUMMARY/%d to %{public}s", hash, processor.name.c_str());
281 processor.outputs.push_back(OutputSpec{{"dpl-summary"}, ConcreteDataMatcher{"DPL", "SUMMARY", static_cast<DataAllocator::SubSpecificationType>(hash)}});
282 }
283 }
284 }
285 bool hasConditionOption = false;
286 for (size_t ii = 0; ii < processor.inputs.size(); ++ii) {
287 auto& input = processor.inputs[ii];
288 bool hasProjectors = false;
289 bool hasIndexRecords = false;
290 bool hasCCDBURLs = false;
291 bool wasAOD = false;
292 // all three options are exclusive
293 for (auto const& p : input.metadata) {
294 if (p.name.starts_with("aod-origin-replaced")) {
295 wasAOD = true;
296 }
297 if (p.name.compare("projectors") == 0) {
298 hasProjectors = true;
299 break;
300 }
301 if (p.name.compare("index-records") == 0) {
302 hasIndexRecords = true;
303 break;
304 }
305 if (p.name.starts_with("ccdb:")) {
306 hasCCDBURLs = true;
307 break;
308 }
309 }
310 switch (input.lifetime) {
311 case Lifetime::Timer: {
312 auto concrete = DataSpecUtils::asConcreteDataMatcher(input);
313 auto hasOption = std::ranges::any_of(processor.options, [&input](auto const& option) { return (option.name == "period-" + input.binding); });
314 if (hasOption == false) {
315 processor.options.push_back(ConfigParamSpec{"period-" + input.binding, VariantType::Int, 1000, {"period of the timer in milliseconds"}});
316 }
317 timer.outputs.emplace_back(OutputSpec{concrete.origin, concrete.description, concrete.subSpec, Lifetime::Timer});
318 } break;
319 case Lifetime::Signal: {
320 auto concrete = DataSpecUtils::asConcreteDataMatcher(input);
321 timer.outputs.emplace_back(OutputSpec{concrete.origin, concrete.description, concrete.subSpec, Lifetime::Signal});
322 } break;
323 case Lifetime::Enumeration: {
324 auto concrete = DataSpecUtils::asConcreteDataMatcher(input);
325 timer.outputs.emplace_back(OutputSpec{concrete.origin, concrete.description, concrete.subSpec, Lifetime::Enumeration});
326 } break;
327 case Lifetime::Condition: {
328 requestedCCDBs.emplace_back(input);
329 if ((hasConditionOption == false) && std::ranges::none_of(processor.options, [](auto const& option) { return (option.name.compare("condition-backend") == 0); })) {
330 processor.options.emplace_back(ConfigParamSpec{"condition-backend", VariantType::String, defaultConditionBackend(), {"URL for CCDB"}});
331 processor.options.emplace_back(ConfigParamSpec{"condition-timestamp", VariantType::Int64, 0ll, {"Force timestamp for CCDB lookup"}});
332 hasConditionOption = true;
333 }
334 } break;
335 case Lifetime::OutOfBand: {
336 auto concrete = DataSpecUtils::asConcreteDataMatcher(input);
337 auto hasOption = std::ranges::any_of(processor.options, [&input](auto const& option) { return (option.name == "out-of-band-channel-name-" + input.binding); });
338 if (hasOption == false) {
339 processor.options.push_back(ConfigParamSpec{"out-of-band-channel-name-" + input.binding, VariantType::String, "out-of-band", {"channel to listen for out of band data"}});
340 }
341 timer.outputs.emplace_back(OutputSpec{concrete.origin, concrete.description, concrete.subSpec, Lifetime::Enumeration});
342 } break;
343 case Lifetime::QA:
344 case Lifetime::Transient:
345 case Lifetime::Timeframe:
346 case Lifetime::Optional:
347 break;
348 }
349 if (hasProjectors) {
350 DataSpecUtils::updateInputList(dec.requestedDYNs, InputSpec{input});
351 } else if (hasIndexRecords) {
352 DataSpecUtils::updateInputList(dec.requestedIDXs, InputSpec{input});
353 } else if (hasCCDBURLs) {
354 DataSpecUtils::updateInputList(dec.requestedTIMs, InputSpec{input});
355 } else if (DataSpecUtils::partialMatch(input, AODOrigins) || wasAOD) {
356 DataSpecUtils::updateInputList(dec.requestedAODs, InputSpec{input});
357 }
358 }
359
360 std::ranges::stable_sort(timer.outputs, [](OutputSpec const& a, OutputSpec const& b) { return *DataSpecUtils::getOptionalSubSpec(a) < *DataSpecUtils::getOptionalSubSpec(b); });
361
362 for (auto& output : processor.outputs) {
363 bool hasProjectors = false;
364 bool hasIndexRecords = false;
365 bool hasCCDBURLs = false;
366 bool wasAOD = false;
367 // all three options are exclusive
368 for (auto const& p : output.metadata) {
369 if (p.name.starts_with("aod-origin-replaced")) {
370 wasAOD = true;
371 }
372 if (p.name.compare("projectors") == 0) {
373 hasProjectors = true;
374 break;
375 }
376 if (p.name.compare("index-records") == 0) {
377 hasIndexRecords = true;
378 break;
379 }
380 if (p.name.starts_with("ccdb:")) {
381 hasCCDBURLs = true;
382 break;
383 }
384 }
385 if (hasProjectors) {
386 dec.providedDYNs.emplace_back(output);
387 } else if (hasCCDBURLs) {
388 dec.providedTIMs.emplace_back(output);
389 } else if (hasIndexRecords) {
390 dec.providedIDXs.emplace_back(output);
391 } else if (DataSpecUtils::partialMatch(output, AODOrigins) || wasAOD) {
392 dec.providedAODs.emplace_back(output);
394 dec.providedOutputObjHist.emplace_back(output);
395 auto it = std::ranges::find_if(dec.outObjHistMap, [&](auto&& x) { return x.id == hash; });
396 if (it == dec.outObjHistMap.end()) {
397 dec.outObjHistMap.push_back({hash, {output.binding.value}});
398 } else {
399 it->bindings.push_back(output.binding.value);
400 }
401 }
402
403 if (output.lifetime == Lifetime::Condition) {
404 providedCCDBs.push_back(output);
405 }
406 }
407 }
408
409 auto inputSpecLessThan = [](InputSpec const& lhs, InputSpec const& rhs) { return DataSpecUtils::describe(lhs) < DataSpecUtils::describe(rhs); };
410 auto outputSpecLessThan = [](OutputSpec const& lhs, OutputSpec const& rhs) { return DataSpecUtils::describe(lhs) < DataSpecUtils::describe(rhs); };
411
412 DataProcessorSpec indexBuilder{
413 "internal-dpl-aod-index-builder",
414 {},
415 {},
416 AlgorithmSpec::dummyAlgorithm(), // real algorithm will be set in adjustTopology
417 {}};
418 std::ranges::sort(dec.requestedIDXs, inputSpecLessThan);
419 std::ranges::sort(dec.providedIDXs, outputSpecLessThan);
420 dec.requestedIDXs | views::filter_not_matching(dec.providedIDXs) | sinks::append_to{dec.builderInputs};
421 AnalysisSupportHelpers::addMissingOutputsToBuilder(dec.builderInputs, dec.requestedAODs, dec.requestedDYNs, indexBuilder);
422
423 std::ranges::sort(dec.requestedTIMs, inputSpecLessThan);
424 std::ranges::sort(dec.providedTIMs, outputSpecLessThan);
425 dec.requestedTIMs | views::filter_not_matching(dec.providedTIMs) | sinks::append_to{dec.analysisCCDBInputs};
426 AnalysisSupportHelpers::addMissingOutputsToBuilder(dec.analysisCCDBInputs, dec.requestedAODs, dec.requestedDYNs, analysisCCDBBackend);
427
428 std::ranges::sort(dec.requestedDYNs, inputSpecLessThan);
429 std::ranges::sort(dec.providedDYNs, outputSpecLessThan);
430 dec.requestedDYNs | views::filter_not_matching(dec.providedDYNs) | sinks::append_to{dec.spawnerInputs};
431
432 DataProcessorSpec aodSpawner{
433 "internal-dpl-aod-spawner",
434 {},
435 {},
436 AlgorithmSpec::dummyAlgorithm(), // real algorithm will be set in adjustTopology
437 {}};
438 AnalysisSupportHelpers::addMissingOutputsToSpawner({}, dec.spawnerInputs, dec.requestedAODs, aodSpawner);
439
440 std::ranges::sort(dec.requestedAODs, inputSpecLessThan);
441 std::ranges::sort(dec.providedAODs, outputSpecLessThan);
442 AnalysisSupportHelpers::addMissingOutputsToReader(dec.providedAODs, dec.requestedAODs, aodReader);
443
444 std::ranges::sort(requestedCCDBs, inputSpecLessThan);
445 std::ranges::sort(providedCCDBs, outputSpecLessThan);
446 AnalysisSupportHelpers::addMissingOutputsToReader(providedCCDBs, requestedCCDBs, ccdbBackend);
447
448 std::vector<DataProcessorSpec> extraSpecs;
449
450 if (transientStore.outputs.empty() == false) {
451 extraSpecs.push_back(transientStore);
452 }
453 if (qaStore.outputs.empty() == false) {
454 extraSpecs.push_back(qaStore);
455 }
456
457 if (aodSpawner.outputs.empty() == false) {
458 extraSpecs.push_back(timePipeline(aodSpawner, ctx.options().get<int64_t>("spawners")));
459 }
460
461 if (indexBuilder.outputs.empty() == false) {
462 extraSpecs.push_back(indexBuilder);
463 }
464
465 // add the Analysys CCDB backend which reads CCDB objects using a provided table
467 if (deploymentMode != DeploymentMode::OnlineDDS && deploymentMode != DeploymentMode::OnlineECS) {
468 if (analysisCCDBBackend.outputs.empty() == false) {
469 extraSpecs.push_back(analysisCCDBBackend);
470 }
471 }
472
473 auto tfnsource = std::ranges::find_if(workflow, [](DataProcessorSpec const& spec) {
474 return std::ranges::any_of(spec.outputs, [](OutputSpec const& output) {
475 return DataSpecUtils::match(output, "TFN", "TFNumber", 0);
476 });
477 });
478
479 // add the reader
480 if (aodReader.outputs.empty() == false) {
481 if (tfnsource == workflow.end()) {
482 // add normal reader
483 aodReader.outputs.emplace_back(OutputSpec{"TFN", "TFNumber"});
484 aodReader.outputs.emplace_back(OutputSpec{"TFF", "TFFilename"});
485 } else {
486 // AODs are being injected the tfnsource is the entry point, add error-handler reader
487 aodReader.algorithm = AlgorithmSpec{
489 [](DeviceSpec const& spec) {
490 LOGP(warn, "Workflow with injected AODs has unsatisfied inputs:");
491 for (auto const& output : spec.outputs) {
492 LOGP(warn, " {}", DataSpecUtils::describe(output.matcher));
493 }
494 LOGP(fatal, "Stopping.");
495 // to ensure the output type for adaptStateful
496 return adaptStateless([](DataAllocator&) {});
497 })};
498 }
499 auto concrete = DataSpecUtils::asConcreteDataMatcher(aodReader.inputs[0]);
500 timer.outputs.emplace_back(concrete.origin, concrete.description, concrete.subSpec, Lifetime::Enumeration);
501 extraSpecs.push_back(timePipeline(aodReader, ctx.options().get<int64_t>("readers")));
502 }
503
504 InputSpec matcher{"dstf", "FLP", "DISTSUBTIMEFRAME", 0xccdb};
505 auto& dstf = std::get<ConcreteDataMatcher>(matcher.matcher);
506 // Check if any of the provided outputs is a DISTSTF
507 // Check if any of the requested inputs is for a 0xccdb message
508 bool providesDISTSTF = std::ranges::any_of(workflow,
509 [&matcher](auto const& dp) {
510 return std::any_of(dp.outputs.begin(), dp.outputs.end(), [&matcher](auto const& output) {
511 return DataSpecUtils::match(matcher, output);
512 });
513 });
514
515 // If there is no CCDB requested, but we still ask for a FLP/DISTSUBTIMEFRAME/0xccdb
516 // we add to the first data processor which has no inputs (apart from
517 // enumerations / timers) the responsibility to provide the DISTSUBTIMEFRAME
518 bool requiresDISTSUBTIMEFRAME = std::ranges::any_of(workflow,
519 [&dstf](auto const& dp) {
520 return std::any_of(dp.inputs.begin(), dp.inputs.end(), [&dstf](auto const& input) {
521 return DataSpecUtils::match(input, dstf);
522 });
523 });
524
525 // We find the first device which has either just enumerations or
526 // just timers, and we will add the DISTSUBTIMEFRAME to it.
527 // Notice how we do so in a stable manner by sorting the devices
528 // by name.
529 int enumCandidate = -1;
530 int timerCandidate = -1;
531 for (auto wi = 0U; wi < workflow.size(); ++wi) {
532 auto& dp = workflow[wi];
533 if (dp.inputs.size() != 1) {
534 continue;
535 }
536 auto lifetime = dp.inputs[0].lifetime;
537 if (lifetime == Lifetime::Enumeration && (enumCandidate == -1 || workflow[enumCandidate].name > dp.name)) {
538 enumCandidate = wi;
539 }
540 if (lifetime == Lifetime::Timer && (timerCandidate == -1 || workflow[timerCandidate].name > dp.name)) {
541 timerCandidate = wi;
542 }
543 }
544
545 // * If there are AOD outputs we use TFNumber as the CCDB clock
546 // * If one device provides a DISTSTF we use that as the CCDB clock
547 // * If one of the devices provides a timer we use that as the CCDB clock
548 // * If none of the above apply, add to the first data processor
549 // which has no inputs apart from enumerations the responsibility
550 // to provide the DISTSUBTIMEFRAME.
551 if (ccdbBackend.outputs.empty() == false) {
552 if (aodReader.outputs.empty() == false) {
553 // fetcher clock follows AOD source (TFNumber)
554 ccdbBackend.inputs.push_back(InputSpec{"tfn", "TFN", "TFNumber"});
555 } else if (providesDISTSTF) {
556 // fetcher clock follows DSTF/ccdb source (DISTSUBTIMEFRAME)
557 ccdbBackend.inputs.push_back(InputSpec{"tfn", dstf, Lifetime::Timeframe});
558 } else {
559 if (enumCandidate != -1) {
560 // add DSTF/ccdb source to the enumeration-driven source explicitly
561 // fetcher clock is provided by enumeration-driven source (DISTSUBTIMEFRAME)
562 DataSpecUtils::updateOutputList(workflow[enumCandidate].outputs, OutputSpec{{"ccdb-diststf"}, dstf, Lifetime::Timeframe});
563 ccdbBackend.inputs.push_back(InputSpec{"tfn", dstf, Lifetime::Timeframe});
564 } else if (timerCandidate != -1) {
565 // fetcher clock is proived by timer source
566 auto timer_dstf = DataSpecUtils::asConcreteDataMatcher(workflow[timerCandidate].outputs[0]);
567 ccdbBackend.inputs.push_back(InputSpec{"tfn", timer_dstf, Lifetime::Timeframe});
568 }
569 }
570
571 ccdbBackend.outputs.push_back(OutputSpec{"CTP", "OrbitReset", 0});
572 // Load the CCDB backend from the plugin
573 ccdbBackend.algorithm = PluginManager::loadAlgorithmFromPlugin("O2FrameworkCCDBSupport", "CCDBFetcherPlugin", ctx);
574 extraSpecs.push_back(timePipeline(ccdbBackend, ctx.options().get<int64_t>("ccdb-fetchers")));
575 } else if (requiresDISTSUBTIMEFRAME && enumCandidate != -1) {
576 // add DSTF/ccdb source to the enumeration-driven source explicitly if it is required in the workflow
577 DataSpecUtils::updateOutputList(workflow[enumCandidate].outputs, OutputSpec{{"ccdb-diststf"}, dstf, Lifetime::Timeframe});
578 }
579
580 // add the timer
581 if (timer.outputs.empty() == false) {
582 extraSpecs.push_back(timer);
583 }
584
585 // This is to inject a file sink so that any dangling ATSK object is written
586 // to a ROOT file.
587 if (dec.providedOutputObjHist.empty() == false) {
589 extraSpecs.push_back(rootSink);
590 }
591
592 workflow.insert(workflow.end(), extraSpecs.begin(), extraSpecs.end());
593 extraSpecs.clear();
594
595 injectAODWriter(workflow, ctx);
596
597 // Select dangling outputs which are not of type AOD
598 std::vector<InputSpec> redirectedOutputsInputs;
599 for (auto ii = 0u; ii < dec.outputsInputs.size(); ii++) {
600 if (ctx.options().get<std::string>("forwarding-policy") == "none") {
601 continue;
602 }
603 // We forward to the output proxy all the inputs only if they are dangling
604 // or if the forwarding policy is "proxy".
605 if (!dec.isDangling[ii] && (ctx.options().get<std::string>("forwarding-policy") != "all")) {
606 continue;
607 }
608 // AODs are skipped in any case.
609 if (DataSpecUtils::partialMatch(dec.outputsInputs[ii], AODOrigins)) {
610 continue;
611 }
612 redirectedOutputsInputs.emplace_back(dec.outputsInputs[ii]);
613 }
614
615 std::vector<InputSpec> unmatched;
616 auto forwardingDestination = ctx.options().get<std::string>("forwarding-destination");
617 // update tfnsource iterator (could be aod-reader)
618 tfnsource = std::ranges::find_if(workflow, [](DataProcessorSpec const& spec) {
619 return std::ranges::any_of(spec.outputs, [](OutputSpec const& output) {
620 return DataSpecUtils::match(output, "TFN", "TFNumber", 0);
621 });
622 });
623 if (redirectedOutputsInputs.size() > 0 && forwardingDestination == "file") {
624 auto fileSink = CommonDataProcessors::getGlobalFileSink(redirectedOutputsInputs, unmatched);
625 if (unmatched.size() != redirectedOutputsInputs.size()) {
626 extraSpecs.push_back(fileSink);
627 }
628 } else if (redirectedOutputsInputs.size() > 0 && forwardingDestination == "fairmq") {
629 auto fairMQSink = CommonDataProcessors::getGlobalFairMQSink(redirectedOutputsInputs);
630 extraSpecs.push_back(fairMQSink);
631 } else if (forwardingDestination != "drop") {
632 throw runtime_error_f("Unknown forwarding destination %s", forwardingDestination.c_str());
633 }
634 if ((unmatched.size() > 0) || (redirectedOutputsInputs.size() > 0) || (tfnsource != workflow.end())) {
635 std::vector<InputSpec> ignored = unmatched;
636 ignored.insert(ignored.end(), redirectedOutputsInputs.begin(), redirectedOutputsInputs.end());
637 for (auto& ignoredInput : ignored) {
638 ignoredInput.lifetime = Lifetime::Sporadic;
639 }
640
641 // Use the new dummy sink when the AOD reader is there
642 O2_SIGNPOST_ID_GENERATE(sid, workflow_helpers);
643 if (tfnsource != workflow.end()) {
644 DataSpecUtils::updateInputList(ignored, InputSpec{"tfn", "TFN", "TFNumber", 0, Lifetime::Sporadic});
645 DataSpecUtils::updateInputList(ignored, InputSpec{"tff", "TFF", "TFFilename", 0, Lifetime::Sporadic});
646 }
647
648 if (tfnsource != workflow.end() && !tfnsource->name.starts_with("aod-producer-workflow")) { // any tfnsource except the aod-producer should use scheduled sink
649 O2_SIGNPOST_EVENT_EMIT(workflow_helpers, sid, "injectServiceDevices", "Injecting scheduled dummy sink");
650 // if there is a tfnsource, make sure the sink gets TFN/TFF
651 extraSpecs.push_back(CommonDataProcessors::getScheduledDummySink(ignored));
652 } else { // if there is no tfn source or if that source is aod-producer-workflow, out-of-band channel is used to propagate the number of consumed timeframes
653 O2_SIGNPOST_EVENT_EMIT(workflow_helpers, sid, "injectServiceDevices", "Injecting rate limited dummy sink");
654 std::string rateLimitingChannelConfigOutput;
655 if (rateLimitingIPCID != -1) {
656 rateLimitingChannelConfigOutput = fmt::format("name=metric-feedback,type=push,method=bind,address=ipc://{}metric-feedback-{},transport=shmem,rateLogging=0", ChannelSpecHelpers::defaultIPCFolder(), rateLimitingIPCID);
657 }
658 extraSpecs.push_back(CommonDataProcessors::getDummySink(ignored, rateLimitingChannelConfigOutput));
659 }
660 }
661
662 workflow.insert(workflow.end(), extraSpecs.begin(), extraSpecs.end());
663 extraSpecs.clear();
664}
665
667{
668 unsigned int distSTFCount = 0;
669 for (auto& spec : workflow) {
670 auto& inputs = spec.inputs;
671 bool allSporadic = true;
672 bool hasTimer = false;
673 bool hasSporadic = false;
674 bool hasOptionals = false;
675 for (auto& input : inputs) {
676 if (input.lifetime == Lifetime::Optional) {
677 hasOptionals = true;
678 }
679 }
680 for (auto& input : inputs) {
681 // Any InputSpec that is DPL/DISTSUBTIMEFRAME/0 will actually be replaced by one
682 // which looks like DPL/DISTSUBTIMEFRAME/<incremental number> for devices that
683 // have Optional inputs as well.
684 // This is done to avoid the race condition where the DISTSUBTIMEFRAME/0 gets
685 // forwarded before actual RAWDATA arrives.
686 if (DataSpecUtils::match(input, ConcreteDataTypeMatcher{"FLP", "DISTSUBTIMEFRAME"}) &&
687 !DataSpecUtils::match(input, ConcreteDataMatcher{"FLP", "DISTSUBTIMEFRAME", 0})) {
688 LOGP(error,
689 "Only FLP/DISTSUBTIMEFRAME/0 is supported as input "
690 "provided by the user. Please replace {} with FLP/DISTSUBTIMEFRAME/0 in {}.",
691 DataSpecUtils::describe(input), input.binding);
692 }
693 if (hasOptionals && DataSpecUtils::match(input, ConcreteDataMatcher{"FLP", "DISTSUBTIMEFRAME", 0})) {
694 // The first one remains unchanged, therefore we use the postincrement
695 DataSpecUtils::updateMatchingSubspec(input, distSTFCount++);
696 continue;
697 }
698 // Timers are sporadic only when they are not
699 // alone.
700 if (input.lifetime == Lifetime::Timer) {
701 hasTimer = true;
702 continue;
703 }
704 if (input.lifetime == Lifetime::Sporadic) {
705 hasSporadic = true;
706 } else {
707 allSporadic = false;
708 }
709 }
710
711 LOGP(debug, "WorkflowHelpers::adjustTopology: spec {} hasTimer {} hasSporadic {} allSporadic {}", spec.name, hasTimer, hasSporadic, allSporadic);
712
713 // If they are not all sporadic (excluding timers)
714 // we leave things as they are.
715 if (allSporadic == false) {
716 continue;
717 }
718 // A timer alone is not sporadic.
719 if (hasSporadic == false) {
720 continue;
721 }
725 for (auto& output : spec.outputs) {
726 if (output.lifetime == Lifetime::Timeframe) {
727 output.lifetime = Lifetime::Sporadic;
728 }
729 }
730 }
731
732 if (distSTFCount > 0) {
733 for (auto& spec : workflow) {
734 if (std::ranges::any_of(spec.outputs, [](auto const& output) { return DataSpecUtils::match(output, ConcreteDataMatcher{"FLP", "DISTSUBTIMEFRAME", 0}); })) {
735 for (unsigned int i = 1; i < distSTFCount; ++i) {
736 spec.outputs.emplace_back(OutputSpec{ConcreteDataMatcher{"FLP", "DISTSUBTIMEFRAME", i}, Lifetime::Timeframe});
737 }
738 break;
739 }
740 }
741 }
742}
743
745{
746 auto& dec = ctx.services().get<DanglingEdgesContext>();
748 std::tie(dec.outputsInputs, dec.isDangling) = analyzeOutputs(workflow);
749
750 // create DataOutputDescriptor
751 std::shared_ptr<DataOutputDirector> dod = AnalysisSupportHelpers::getDataOutputDirector(ctx);
752
753 // select outputs of type AOD which need to be saved
754 dec.outputsInputsAOD.clear();
755 for (auto ii = 0u; ii < dec.outputsInputs.size(); ii++) {
756 if (DataSpecUtils::partialMatch(dec.outputsInputs[ii], AODOrigins)) {
757 auto ds = dod->getDataOutputDescriptors(dec.outputsInputs[ii]);
758 if (ds.size() > 0 || dec.isDangling[ii]) {
759 dec.outputsInputsAOD.emplace_back(dec.outputsInputs[ii]);
760 }
761 }
762 }
763
764 // file sink for any AOD output
765 if (dec.outputsInputsAOD.size() > 0) {
766 // add TFNumber and TFFilename as input to the writer
767 DataSpecUtils::updateInputList(dec.outputsInputsAOD, InputSpec{"tfn", "TFN", "TFNumber"});
768 DataSpecUtils::updateInputList(dec.outputsInputsAOD, InputSpec{"tff", "TFF", "TFFilename"});
769 auto fileSink = AnalysisSupportHelpers::getGlobalAODSink(ctx);
770 workflow.push_back(fileSink);
771
772 auto it = std::find_if(dec.outputsInputs.begin(), dec.outputsInputs.end(), [](InputSpec const& spec) -> bool {
773 return DataSpecUtils::partialMatch(spec, o2::header::DataOrigin("TFN"));
774 });
775 dec.isDangling[std::distance(dec.outputsInputs.begin(), it)] = false;
776
777 it = std::find_if(dec.outputsInputs.begin(), dec.outputsInputs.end(), [](InputSpec const& spec) -> bool {
778 return DataSpecUtils::partialMatch(spec, o2::header::DataOrigin("TFF"));
779 });
780 dec.isDangling[std::distance(dec.outputsInputs.begin(), it)] = false;
781 }
782}
783
785 std::vector<DeviceConnectionEdge>& logicalEdges,
786 std::vector<OutputSpec>& outputs,
787 std::vector<LogicalForwardInfo>& forwardedInputsInfo)
788{
789 // In case the workflow is empty, we do not have anything to do.
790 if (workflow.empty()) {
791 return;
792 }
793
794 // This is the state. Oif is the iterator I use for the searches.
795 std::vector<LogicalOutputInfo> availableOutputsInfo;
796 auto const& constOutputs = outputs; // const version of the outputs
797 // Forwards is a local cache to avoid adding forwards before time.
798 std::vector<LogicalOutputInfo> forwards;
799
800 // Notice that availableOutputsInfo MUST be updated first, since it relies on
801 // the size of outputs to be the one before the update.
802 auto enumerateAvailableOutputs = [&workflow, &outputs, &availableOutputsInfo]() {
803 O2_SIGNPOST_ID_GENERATE(sid, workflow_helpers);
804 for (size_t wi = 0; wi < workflow.size(); ++wi) {
805 auto& producer = workflow[wi];
806 if (producer.outputs.empty()) {
807 O2_SIGNPOST_EVENT_EMIT(workflow_helpers, sid, "output enumeration", "No outputs for [%zu] %{public}s", wi, producer.name.c_str());
808 }
809 O2_SIGNPOST_START(workflow_helpers, sid, "output enumeration", "Enumerating outputs for producer [%zu] %{}s public", wi, producer.name.c_str());
810
811 for (size_t oi = 0; oi < producer.outputs.size(); ++oi) {
812 auto& out = producer.outputs[oi];
813 auto uniqueOutputId = outputs.size();
814 availableOutputsInfo.emplace_back(LogicalOutputInfo{wi, uniqueOutputId, false});
815 O2_SIGNPOST_EVENT_EMIT(workflow_helpers, sid, "output enumeration", "- [%zu, %zu] %{public}s",
816 oi, uniqueOutputId, DataSpecUtils::describe(out).c_str());
817 outputs.push_back(out);
818 }
819 O2_SIGNPOST_END(workflow_helpers, sid, "output enumeration", "");
820 }
821 };
822
823 auto errorDueToMissingOutputFor = [&workflow, &constOutputs](size_t ci, size_t ii) {
824 auto input = workflow[ci].inputs[ii];
825 std::ostringstream str;
826 str << "No matching output found for "
827 << DataSpecUtils::describe(input) << " as requested by data processor \"" << workflow[ci].name << "\". Candidates:\n";
828
829 for (auto& output : constOutputs) {
830 str << "-" << DataSpecUtils::describe(output) << "\n";
831 }
832
833 throw std::runtime_error(str.str());
834 };
835
836 // This is the outer loop
837 //
838 // Here we iterate over dataprocessor items in workflow and we consider them
839 // as consumer, since we are interested in their inputs.
840 // Notice also we need to search for all the matching inputs, since
841 // we could have more than one source that matches (e.g. in the
842 // case of a time merger).
843 // Once consumed, an output is not actually used anymore, however
844 // we append it as a forward.
845 // Finally, If a device has n-way pipelining, we need to create one node per
846 // parallel pipeline and add an edge for each.
847 enumerateAvailableOutputs();
848
849 std::vector<bool> matches(constOutputs.size());
850 for (size_t consumer = 0; consumer < workflow.size(); ++consumer) {
851 O2_SIGNPOST_ID_GENERATE(sid, workflow_helpers);
852 O2_SIGNPOST_START(workflow_helpers, sid, "input matching", "Matching inputs of consumer [%zu] %{}s public", consumer, workflow[consumer].name.c_str());
853 for (size_t input = 0; input < workflow[consumer].inputs.size(); ++input) {
854 forwards.clear();
855 for (size_t i = 0; i < constOutputs.size(); i++) {
856 matches[i] = DataSpecUtils::match(workflow[consumer].inputs[input], constOutputs[i]);
857 if (matches[i]) {
858 O2_SIGNPOST_EVENT_EMIT(workflow_helpers, sid, "output", "Input %{public}s matches %{public}s",
859 DataSpecUtils::describe(workflow[consumer].inputs[input]).c_str(),
860 DataSpecUtils::describe(constOutputs[i]).c_str());
861 }
862 }
863
864 for (size_t i = 0; i < availableOutputsInfo.size(); i++) {
865 // Notice that if the output is actually a forward, we need to store that information so that when we add it at device level we know which output channel we need to connect it too.
866 if (!matches[availableOutputsInfo[i].outputGlobalIndex]) {
867 continue;
868 }
869 auto* oif = &availableOutputsInfo[i];
870 if (oif->forward) {
871 forwardedInputsInfo.emplace_back(LogicalForwardInfo{consumer, input, oif->outputGlobalIndex});
872 }
873 auto producer = oif->specIndex;
874 auto uniqueOutputId = oif->outputGlobalIndex;
875 for (size_t tpi = 0; tpi < workflow[consumer].maxInputTimeslices; ++tpi) {
876 for (size_t ptpi = 0; ptpi < workflow[producer].maxInputTimeslices; ++ptpi) {
877 O2_SIGNPOST_EVENT_EMIT(workflow_helpers, sid, "output", "Adding edge between %{public}s and %{public}s", workflow[consumer].name.c_str(),
878 workflow[producer].name.c_str());
879 logicalEdges.emplace_back(DeviceConnectionEdge{producer, consumer, tpi, ptpi, uniqueOutputId, input, oif->forward});
880 }
881 }
882 forwards.push_back(LogicalOutputInfo{consumer, uniqueOutputId, true});
883 // We have consumed the input, therefore we remove it from the list. We will insert the forwarded inputs only at the end of the iteration.
884 oif->enabled = false;
885 }
886 if (forwards.empty()) {
887 errorDueToMissingOutputFor(consumer, input);
888 }
889 availableOutputsInfo.erase(std::remove_if(availableOutputsInfo.begin(), availableOutputsInfo.end(), [](auto const& info) { return info.enabled == false; }), availableOutputsInfo.end());
890 std::ranges::copy(forwards, std::back_inserter(availableOutputsInfo));
891 }
892 O2_SIGNPOST_END(workflow_helpers, sid, "input matching", "");
893 }
894}
895
896std::vector<EdgeAction>
898 const std::vector<DeviceConnectionEdge>& edges,
899 const std::vector<size_t>& index)
900{
901 DeviceConnectionEdge last{ULONG_MAX, ULONG_MAX, ULONG_MAX, ULONG_MAX, ULONG_MAX, ULONG_MAX};
902
903 assert(edges.size() == index.size());
904 std::vector<EdgeAction> actions(edges.size(), EdgeAction{false, false});
905 for (size_t i : index) {
906 auto& edge = edges[i];
907 auto& action = actions[i];
908 action.requiresNewDevice = last.producer != edge.producer || last.producerTimeIndex != edge.producerTimeIndex;
909 action.requiresNewChannel = last.consumer != edge.consumer || last.producer != edge.producer || last.timeIndex != edge.timeIndex || last.producerTimeIndex != edge.producerTimeIndex;
910 last = edge;
911 }
912 return actions;
913}
914
915std::vector<EdgeAction>
917 const std::vector<DeviceConnectionEdge>& edges,
918 const std::vector<size_t>& index)
919{
920 DeviceConnectionEdge last{ULONG_MAX, ULONG_MAX, ULONG_MAX, ULONG_MAX, ULONG_MAX, ULONG_MAX};
921
922 assert(edges.size() == index.size());
923 std::vector<EdgeAction> actions(edges.size(), EdgeAction{false, false});
924 for (size_t i : index) {
925 auto& edge = edges[i];
926 auto& action = actions[i];
927 // Calculate which actions need to be taken for this edge.
928 action.requiresNewDevice = last.consumer != edge.consumer || last.timeIndex != edge.timeIndex;
929 action.requiresNewChannel =
930 last.consumer != edge.consumer || last.timeIndex != edge.timeIndex || last.producer != edge.producer || last.producerTimeIndex != edge.producerTimeIndex;
931
932 last = edge;
933 }
934 return actions;
935}
936
937void WorkflowHelpers::sortEdges(std::vector<size_t>& inEdgeIndex,
938 std::vector<size_t>& outEdgeIndex,
939 const std::vector<DeviceConnectionEdge>& edges)
940{
941 inEdgeIndex.resize(edges.size());
942 outEdgeIndex.resize(edges.size());
943 std::iota(inEdgeIndex.begin(), inEdgeIndex.end(), 0);
944 std::iota(outEdgeIndex.begin(), outEdgeIndex.end(), 0);
945
946 // Two indexes, one to bind the outputs, the other
947 // one to connect the inputs. The
948 auto outSorter = [&edges](size_t i, size_t j) {
949 auto& a = edges[i];
950 auto& b = edges[j];
951 return std::tie(a.producer, a.producerTimeIndex, a.timeIndex, a.consumer) < std::tie(b.producer, b.producerTimeIndex, b.timeIndex, b.consumer);
952 };
953 auto inSorter = [&edges](size_t i, size_t j) {
954 auto& a = edges[i];
955 auto& b = edges[j];
956 return std::tie(a.consumer, a.timeIndex, a.producer, a.producerTimeIndex) < std::tie(b.consumer, b.timeIndex, b.producer, b.producerTimeIndex);
957 };
958
959 std::sort(inEdgeIndex.begin(), inEdgeIndex.end(), inSorter);
960 std::sort(outEdgeIndex.begin(), outEdgeIndex.end(), outSorter);
961}
962
964{
965 if (workflow.empty()) {
967 }
968 std::set<std::string> validNames;
969 // std::vector<OutputSpec> availableOutputs;
970 // std::vector<InputSpec> requiredInputs;
971
972 // An index many to one index to go from a given input to the
973 // associated spec
974 // std::map<size_t, size_t> inputToSpec;
975 // A one to one index to go from a given output to the Spec emitting it
976 // std::map<size_t, size_t> outputToSpec;
977
978 std::ostringstream ss;
979
980 for (auto& spec : workflow) {
981 if (spec.name.empty()) {
982 throw std::runtime_error("Invalid DataProcessorSpec name");
983 }
984 if (strpbrk(spec.name.data(), ",;:\"'$") != nullptr) {
985 throw std::runtime_error("Cannot use any of ,;:\"'$ as DataProcessor name");
986 }
987 if (validNames.find(spec.name) != validNames.end()) {
988 throw std::runtime_error("Name " + spec.name + " is used twice.");
989 }
990 validNames.insert(spec.name);
991 for (auto& option : spec.options) {
992 if (option.defaultValue.type() != VariantType::Empty &&
993 option.type != option.defaultValue.type()) {
994 ss << "Mismatch between declared option type (" << (int)option.type << ") and default value type (" << (int)option.defaultValue.type()
995 << ") for " << option.name << " in DataProcessorSpec of "
996 << spec.name;
997 throw std::runtime_error(ss.str());
998 }
999 }
1000 for (size_t ii = 0; ii < spec.inputs.size(); ++ii) {
1001 InputSpec const& input = spec.inputs[ii];
1002 if (DataSpecUtils::validate(input) == false) {
1003 ss << "In spec " << spec.name << " input specification "
1004 << ii << " requires binding, description and origin"
1005 " to be fully specified (found "
1006 << input.binding << ":" << DataSpecUtils::describe(input) << ")";
1007 throw std::runtime_error(ss.str());
1008 }
1009 }
1010 }
1012}
1013
1014using UnifiedDataSpecType = std::variant<InputSpec, OutputSpec>;
1017 size_t id;
1018};
1019
1020std::tuple<std::vector<InputSpec>, std::vector<bool>> WorkflowHelpers::analyzeOutputs(WorkflowSpec const& workflow)
1021{
1022 // compute total number of input/output
1023 size_t totalInputs = 0;
1024 size_t totalOutputs = 0;
1025 for (auto& spec : workflow) {
1026 totalInputs += spec.inputs.size();
1027 totalOutputs += spec.outputs.size();
1028 }
1029
1030 std::vector<DataMatcherId> inputs;
1031 std::vector<DataMatcherId> outputs;
1032 inputs.reserve(totalInputs);
1033 outputs.reserve(totalOutputs);
1034
1035 std::vector<InputSpec> results;
1036 std::vector<bool> isDangling;
1037 results.reserve(totalOutputs);
1038 isDangling.reserve(totalOutputs);
1039
1041 for (size_t wi = 0, we = workflow.size(); wi != we; ++wi) {
1042 auto& spec = workflow[wi];
1043 for (size_t ii = 0, ie = spec.inputs.size(); ii != ie; ++ii) {
1044 inputs.emplace_back(DataMatcherId{wi, ii});
1045 }
1046 for (size_t oi = 0, oe = spec.outputs.size(); oi != oe; ++oi) {
1047 outputs.emplace_back(DataMatcherId{wi, oi});
1048 }
1049 }
1050
1051 for (size_t oi = 0, oe = outputs.size(); oi != oe; ++oi) {
1052 auto& output = outputs[oi];
1053 auto& outputSpec = workflow[output.workflowId].outputs[output.id];
1054
1055 // is dangling output?
1056 bool matched = false;
1057 for (size_t ii = 0, ie = inputs.size(); ii != ie; ++ii) {
1058 auto& input = inputs[ii];
1059 // Inputs of the same workflow cannot match outputs
1060 if (output.workflowId == input.workflowId) {
1061 continue;
1062 }
1063 auto& inputSpec = workflow[input.workflowId].inputs[input.id];
1064 if (DataSpecUtils::match(inputSpec, outputSpec)) {
1065 matched = true;
1066 break;
1067 }
1068 }
1069
1070 auto input = DataSpecUtils::matchingInput(outputSpec);
1071 char buf[64];
1072 input.binding = (snprintf(buf, 63, "output_%zu_%zu", output.workflowId, output.id), buf);
1073
1074 // make sure that entries are unique
1075 if (std::ranges::find(results, input) == results.end()) {
1076 results.emplace_back(input);
1077 isDangling.emplace_back(matched == false);
1078 }
1079 }
1080
1081 // make sure that results is unique
1082 return std::make_tuple(results, isDangling);
1083}
1084
1085std::vector<InputSpec> WorkflowHelpers::computeDanglingOutputs(WorkflowSpec const& workflow)
1086{
1087
1088 auto [outputsInputs, isDangling] = analyzeOutputs(workflow);
1089
1090 std::vector<InputSpec> results;
1091 for (auto ii = 0u; ii < outputsInputs.size(); ii++) {
1092 if (isDangling[ii]) {
1093 results.emplace_back(outputsInputs[ii]);
1094 }
1095 }
1096
1097 return results;
1098}
1099
1100bool validateLifetime(std::ostream& errors,
1101 DataProcessorSpec const& producer, OutputSpec const& output, DataProcessorPoliciesInfo const& producerPolicies,
1102 DataProcessorSpec const& consumer, InputSpec const& input, DataProcessorPoliciesInfo const& consumerPolicies)
1103{
1104 // In case the completion policy is consume-any, we do not need to check anything.
1105 if (consumerPolicies.completionPolicyName == "consume-any") {
1106 return true;
1107 }
1108 if (input.lifetime == Lifetime::Timeframe && output.lifetime == Lifetime::Sporadic) {
1109 errors << fmt::format("Input {} of {} has lifetime Timeframe, but output {} of {} has lifetime Sporadic\n",
1110 DataSpecUtils::describe(input).c_str(), consumer.name,
1111 DataSpecUtils::describe(output).c_str(), producer.name);
1112 return false;
1113 }
1114 return true;
1115}
1116
1117bool validateExpendable(std::ostream& errors,
1118 DataProcessorSpec const& producer, OutputSpec const& output, DataProcessorPoliciesInfo const& producerPolicies,
1119 DataProcessorSpec const& consumer, InputSpec const& input, DataProcessorPoliciesInfo const& consumerPolicies)
1120{
1121 auto isExpendable = [](DataProcessorLabel const& label) {
1122 return label.value == "expendable";
1123 };
1124 auto isResilient = [](DataProcessorLabel const& label) {
1125 return label.value == "expendable" || label.value == "resilient";
1126 };
1127 bool producerExpendable = std::find_if(producer.labels.begin(), producer.labels.end(), isExpendable) != producer.labels.end();
1128 bool consumerCritical = std::find_if(consumer.labels.begin(), consumer.labels.end(), isResilient) == consumer.labels.end();
1129 if (producerExpendable && consumerCritical) {
1130 errors << fmt::format("Critical consumer {} depends on expendable producer {}\n",
1131 consumer.name,
1132 producer.name);
1133 return false;
1134 }
1135 return true;
1136}
1137
1138using Validator = std::function<bool(std::ostream& errors,
1139 DataProcessorSpec const& producer, OutputSpec const& output, DataProcessorPoliciesInfo const& producerPolicies,
1140 DataProcessorSpec const& consumer, InputSpec const& input, DataProcessorPoliciesInfo const& consumerPolicies)>;
1141
1143 std::vector<DataProcessorPoliciesInfo> const& policies,
1144 std::vector<DeviceConnectionEdge> const& edges,
1145 std::vector<OutputSpec> const& outputs)
1146{
1147 static bool disableLifetimeCheck = getenv("DPL_WORKAROUND_DO_NOT_CHECK_FOR_CORRECT_WORKFLOW_LIFETIMES") && atoi(getenv("DPL_WORKAROUND_DO_NOT_CHECK_FOR_CORRECT_WORKFLOW_LIFETIMES"));
1148 std::vector<Validator> defaultValidators = {validateExpendable};
1149 if (!disableLifetimeCheck) {
1150 defaultValidators.emplace_back(validateLifetime);
1151 }
1152 std::stringstream errors;
1153 // Iterate over all the edges.
1154 // Get the input lifetime and the output lifetime.
1155 // Output lifetime must be Timeframe if the input lifetime is Timeframe.
1156 bool hasErrors = false;
1157 for (auto const& edge : edges) {
1158 DataProcessorSpec const& producer = workflow[edge.producer];
1159 DataProcessorSpec const& consumer = workflow[edge.consumer];
1160 DataProcessorPoliciesInfo const& producerPolicies = policies[edge.producer];
1161 DataProcessorPoliciesInfo const& consumerPolicies = policies[edge.consumer];
1162 OutputSpec const& output = outputs[edge.outputGlobalIndex];
1163 InputSpec const& input = consumer.inputs[edge.consumerInputIndex];
1164 for (auto const& validator : defaultValidators) {
1165 hasErrors |= !validator(errors, producer, output, producerPolicies, consumer, input, consumerPolicies);
1166 }
1167 }
1168 if (hasErrors) {
1169 throw std::runtime_error(errors.str());
1170 }
1171}
1172
1173} // namespace o2::framework
uint32_t hash
std::ostringstream debug
std::unique_ptr< expressions::Node > node
unsigned int uint32_t
int32_t i
configStandalone Reconstruction rec_tpc multiplier for the combined cluster track error during track following area in cm for the search of for z only used if decrease removal by when above this local occupancy rowx Shift of TPC revert cluster flag edge bit distance to edge exceeds this ZOffset for secondary multiple of radius max P of electron K BB bands crossing max P of electron p BB bands crossing max P of electron d BB bands crossing max P of electron t BB bands crossing min accepted dEdxMaxTPC of the track Additive extra cluster error for Y2 if edge flag set Additive extra cluster error for Y2 if single set Additive extra cluster error for Y2 if or single set Additive extra cluster error for Z2 if or single set Extra errors to add to track when crossing depending on addErrorsCECrossing Inner portion of y range in sector that is not used in searching for extrapolated track candidates factor2YS for track merging factor2K for track merging Margin in cm of Y position when rejecting edge clusters based on uncorrected track Y Max fraction of shared clusters for tracklet Except pad from cluster finding if total number of charges in a fragment is above this Except pad from cluster finding if number of consecutive charges in a fragment is above this Threshold where a timebin is considered disabling the noisy pad check for that pad Treat signal above this minimum as saturated Smoothing factor for the exponential Highly Ionising Particle tail filter Number of timebins to use for the averaging Add additional custom track errors when crossing relative as fraction of Do N fit passes in final fit of< 0:reject at max -n hits") AddOptionRTC(rejectIFCLowRadiusCluster, uint8_t, 1, "", 0, "Reject clusters that get the IFC mask error during refit") AddOptionRTC(dEdxTruncLow, uint8_t, 2, "", 0, "Low truncation threshold, fraction of 128") AddOptionRTC(dEdxTruncHigh, uint8_t, 77, "", 0, "High truncation threshold, fraction of 128") AddOptionRTC(extrapolationTracking, int8_t, 1, "", 0, "Enable Extrapolation Tracking(prolong tracks to adjacent sectors to find short segments)") AddOptionRTC(disableRefitAttachment, uint8_t, 0, "", 0, "Bitmask to disable certain attachment steps during refit(1:attachment, 2:propagation, 4:loop following, 8:mirroring)") AddOptionRTC(rejectionStrategy, uint8_t, o2::gpu::GPUSettings::RejectionStrategyA, "", 0, "Enable rejection of TPC clusters for compression(0=no, 1=strategy A, 2=strategy B)") AddOptionRTC(mergeLoopersAfterburner, uint8_t, 1, "", 0, "Run afterburner for additional looper merging") AddOptionRTC(compressionTypeMask, uint8_t, o2::gpu::GPUSettings::CompressionFull, "", 0, "TPC Compression mode bits(1=truncate charge/width LSB, 2=differences, 4=track-model)") AddOptionRTC(compressionSortOrder, uint8_t, o2::gpu::GPUSettings::SortTime, "", 0, "Sort order of TPC compression(0=time, 1=pad, 2=Z-time-pad, 3=Z-pad-time, 4=no sorting(use incoming order))") AddOptionRTC(sigBitsCharge, uint8_t, 4, "", 0, "Number of significant bits for TPC cluster charge in compression mode 1") AddOptionRTC(sigBitsWidth, uint8_t, 3, "", 0, "Number of significant bits for TPC cluster width in compression mode 1") AddOptionRTC(dropLoopers, uint8_t, 0, "", 0, "Drop looping tracks starting from second loop") AddOptionRTC(mergerInterpolateErrors, uint8_t, 1, "", 0, "Use interpolation instead of extrapolation for chi2 based cluster rejection") AddOptionRTC(mergerInterpolateRejectAlsoOnCurrentPosition, uint8_t, 1, "", 0, "When using mergerInterpolateErrors, reject based on chi2 twice computed with interpolated and current track position starting from NDF > mergerNonInterpolateRejectMinNDF Merge tracks accross the central electrode Enable PID response Add sub threshold clusters in TPC dEdx computation OR mask of TPC flags that will reject the cluster in alternative dEdx Reject edge clusters based on uncorrected track Y during track fit Increase decrease removal by when on this number of pads from the edge h configStandalone Reconstruction rec_trd Max chi2 for TRD tracklets to be matched to a track Chi2 cut for strict matching mode Number of sigmas for ITS TPC track time error estimate Addition to search road around track prolongation along Y in cm Optimal tracklet rphi resolution in maximum chi2 per attached tracklet for TRD tracks take into account the full likelihood of the slope including exponential tails Tracks with less attached tracklets are discarded after the tracking Post trigger Pile up integration time in BCs fits all iterations but the last one in projections Use material lookup table for TPC refit Apply workarounds to avoid cyclic merge should not be needed tpc dyn configStandalone Processing proc_rtc Replace constant variables static by constexpr expressions Compile RTC in deterministic with NO_FAST_MATH flags and GPUCA_DETERMINISTIC_MODE define Use RTC to optimize GPU code h configStandalone Processing proc_rtctech Use a file lock to serialize access to the cache folder Print launch bounds used for RTC code as debugging option
char error[CURL_ERROR_SIZE]
void output(const std::map< std::string, ChannelStat > &channels)
Definition rawdump.cxx:197
std::vector< SidecarEdge > edges
uint32_t j
Definition RawData.h:0
#define O2_DECLARE_DYNAMIC_LOG(name)
Definition Signpost.h:490
#define O2_SIGNPOST_END(log, id, name, format,...)
Definition Signpost.h:609
#define O2_SIGNPOST_ID_GENERATE(name, log)
Definition Signpost.h:507
#define O2_SIGNPOST_EVENT_EMIT(log, id, name, format,...)
Definition Signpost.h:523
#define O2_SIGNPOST_START(log, id, name, format,...)
Definition Signpost.h:603
constexpr uint32_t runtime_hash(char const *str)
consteval uint32_t compile_time_hash(Ts... Vs)
o2::header::DataHeader::SubSpecificationType SubSpecificationType
GLint GLenum GLint x
Definition glcorearb.h:403
const GLfloat * m
Definition glcorearb.h:4066
GLuint index
Definition glcorearb.h:781
GLuint const GLchar * name
Definition glcorearb.h:781
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLuint GLsizei const GLchar * label
Definition glcorearb.h:2519
GLint GLenum GLboolean GLsizei stride
Definition glcorearb.h:867
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition glcorearb.h:2514
constexpr framework::ConcreteDataMatcher matcher()
Definition ASoA.h:388
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
int defaultConditionQueryRate()
bool validateLifetime(std::ostream &errors, DataProcessorSpec const &producer, OutputSpec const &output, DataProcessorPoliciesInfo const &producerPolicies, DataProcessorSpec const &consumer, InputSpec const &input, DataProcessorPoliciesInfo const &consumerPolicies)
std::ostream & operator<<(std::ostream &s, ChannelType const &type)
Stream operators so that we can use ChannelType with Boost.Test.
std::string defaultConditionBackend()
int defaultConditionQueryRateMultiplier()
bool validateExpendable(std::ostream &errors, DataProcessorSpec const &producer, OutputSpec const &output, DataProcessorPoliciesInfo const &producerPolicies, DataProcessorSpec const &consumer, InputSpec const &input, DataProcessorPoliciesInfo const &consumerPolicies)
std::vector< DataProcessorSpec > WorkflowSpec
DataProcessorSpec timePipeline(DataProcessorSpec original, size_t count)
AlgorithmSpec::ProcessCallback adaptStateless(LAMBDA l)
std::function< bool(std::ostream &errors, DataProcessorSpec const &producer, OutputSpec const &output, DataProcessorPoliciesInfo const &producerPolicies, DataProcessorSpec const &consumer, InputSpec const &input, DataProcessorPoliciesInfo const &consumerPolicies)> Validator
RuntimeErrorRef runtime_error_f(const char *,...)
std::variant< InputSpec, OutputSpec > UnifiedDataSpecType
AlgorithmSpec::InitCallback adaptStateful(LAMBDA l)
Descriptor< gSizeDataOriginString > DataOrigin
Definition DataHeader.h:553
const track::TrackFitContext< NLayers > const TrackFollowContext< NLayers > & ctx
D const SVectorGPU< T, D > & rhs
Definition SMatrixGPU.h:193
static AlgorithmSpec dummyAlgorithm()
static void addMissingOutputsToBuilder(std::vector< InputSpec > const &requestedSpecials, std::vector< InputSpec > &requestedAODs, std::vector< InputSpec > &requestedDYNs, DataProcessorSpec &publisher)
static std::shared_ptr< DataOutputDirector > getDataOutputDirector(ConfigContext const &ctx)
Get the data director.
static void addMissingOutputsToReader(std::vector< OutputSpec > const &providedOutputs, std::vector< InputSpec > const &requestedInputs, DataProcessorSpec &publisher)
static void addMissingOutputsToSpawner(std::vector< OutputSpec > const &providedSpecials, std::vector< InputSpec > const &requestedSpecials, std::vector< InputSpec > &requestedAODs, DataProcessorSpec &publisher)
static DataProcessorSpec getGlobalAODSink(ConfigContext const &)
writes inputs of kind AOD to file
static DataProcessorSpec getOutputObjHistSink(ConfigContext const &)
static DataProcessorSpec getScheduledDummySink(std::vector< InputSpec > const &danglingInputs)
static DataProcessorSpec getGlobalFileSink(std::vector< InputSpec > const &danglingInputs, std::vector< InputSpec > &unmatched)
static DataProcessorSpec getGlobalFairMQSink(std::vector< InputSpec > const &danglingInputs)
static DataProcessorSpec getDummySink(std::vector< InputSpec > const &danglingInputs, std::string rateLimitingChannelConfig)
static std::vector< ServiceSpec > defaultServices(std::string extraPlugins="", int numWorkers=0)
Split a string into a vector of strings using : as a separator.
static void addOptionIfMissing(std::vector< ConfigParamSpec > &specs, const ConfigParamSpec &spec)
A label that can be associated to a DataProcessorSpec.
std::vector< DataProcessorLabel > labels
static bool partialMatch(InputSpec const &spec, o2::header::DataOrigin const &origin)
static std::string describe(InputSpec const &spec)
static void updateOutputList(std::vector< OutputSpec > &list, OutputSpec &&input)
Updates list of OutputSpecs by merging metadata (or adding output).
static bool validate(InputSpec const &input)
static ConcreteDataMatcher asConcreteDataMatcher(InputSpec const &input)
static InputSpec matchingInput(OutputSpec const &spec)
static void updateMatchingSubspec(InputSpec &in, header::DataHeader::SubSpecificationType subSpec)
static void updateInputList(std::vector< InputSpec > &list, InputSpec &&input)
Updates list of InputSpecs by merging metadata.
static bool match(InputSpec const &spec, ConcreteDataMatcher const &target)
static DeploymentMode deploymentMode()
enum Lifetime lifetime
Definition InputSpec.h:73
std::string binding
A mnemonic name for the input spec.
Definition InputSpec.h:66
static auto loadAlgorithmFromPlugin(std::string library, std::string plugin, ConfigContext const &context) -> AlgorithmSpec
Helper struct to keep track of the results of the topological sort.
static void validateEdges(WorkflowSpec const &workflow, std::vector< DataProcessorPoliciesInfo > const &policiesInfos, std::vector< DeviceConnectionEdge > const &edges, std::vector< OutputSpec > const &outputs)
static void adjustTopology(WorkflowSpec &workflow, ConfigContext const &ctx)
static void constructGraph(const WorkflowSpec &workflow, std::vector< DeviceConnectionEdge > &logicalEdges, std::vector< OutputSpec > &outputs, std::vector< LogicalForwardInfo > &availableForwardsInfo)
static void injectServiceDevices(WorkflowSpec &workflow, ConfigContext &ctx)
static WorkflowParsingState verifyWorkflow(const WorkflowSpec &workflow)
static std::vector< InputSpec > computeDanglingOutputs(WorkflowSpec const &workflow)
returns only dangling outputs
static std::vector< EdgeAction > computeOutEdgeActions(const std::vector< DeviceConnectionEdge > &edges, const std::vector< size_t > &index)
static std::tuple< std::vector< InputSpec >, std::vector< bool > > analyzeOutputs(WorkflowSpec const &workflow)
static void sortEdges(std::vector< size_t > &inEdgeIndex, std::vector< size_t > &outEdgeIndex, const std::vector< DeviceConnectionEdge > &edges)
static std::vector< EdgeAction > computeInEdgeActions(const std::vector< DeviceConnectionEdge > &edges, const std::vector< size_t > &index)
static std::vector< TopoIndexInfo > topologicalSort(size_t nodeCount, int const *edgeIn, int const *edgeOut, size_t byteStride, size_t edgesCount)
static void injectAODWriter(WorkflowSpec &workflow, ConfigContext const &ctx)
o2::mch::DsIndex ds
const std::string str